Skip to content

Commit abc744d

Browse files
committed
Revert r366449: [CrossTU] Add a function to retrieve original source location.
Reason: the commit breaks layering by adding a dependency on ASTUnit (which is inside clangFrontend) from the ASTImporter (which is inside clangAST). llvm-svn: 366453
1 parent 6acdf83 commit abc744d

File tree

6 files changed

+25
-162
lines changed

6 files changed

+25
-162
lines changed

clang/include/clang/AST/ASTImporter.h

Lines changed: 1 addition & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ namespace clang {
3434

3535
class ASTContext;
3636
class ASTImporterSharedState;
37-
class ASTUnit;
3837
class Attr;
3938
class CXXBaseSpecifier;
4039
class CXXCtorInitializer;
@@ -230,11 +229,6 @@ class TypeSourceInfo;
230229
/// The file managers we're importing to and from.
231230
FileManager &ToFileManager, &FromFileManager;
232231

233-
/// The ASTUnit for the From context, if any.
234-
/// This is used to create a mapping of imported ('To') FileID's to the
235-
/// original ('From') FileID and ASTUnit.
236-
ASTUnit *FromUnit;
237-
238232
/// Whether to perform a minimal import.
239233
bool Minimal;
240234

@@ -283,11 +277,6 @@ class TypeSourceInfo;
283277

284278
void AddToLookupTable(Decl *ToD);
285279

286-
ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
287-
ASTContext &FromContext, FileManager &FromFileManager,
288-
ASTUnit *FromUnit, bool MinimalImport,
289-
std::shared_ptr<ASTImporterSharedState> SharedState);
290-
291280
protected:
292281
/// Can be overwritten by subclasses to implement their own import logic.
293282
/// The overwritten method should call this method if it didn't import the
@@ -298,6 +287,7 @@ class TypeSourceInfo;
298287
virtual bool returnWithErrorInTest() { return false; };
299288

300289
public:
290+
301291
/// \param ToContext The context we'll be importing into.
302292
///
303293
/// \param ToFileManager The file manager we'll be importing into.
@@ -317,23 +307,6 @@ class TypeSourceInfo;
317307
ASTContext &FromContext, FileManager &FromFileManager,
318308
bool MinimalImport,
319309
std::shared_ptr<ASTImporterSharedState> SharedState = nullptr);
320-
/// \param ToContext The context we'll be importing into.
321-
///
322-
/// \param ToFileManager The file manager we'll be importing into.
323-
///
324-
/// \param FromUnit Pointer to an ASTUnit that contains the context and
325-
/// file manager to import from.
326-
///
327-
/// \param MinimalImport If true, the importer will attempt to import
328-
/// as little as it can, e.g., by importing declarations as forward
329-
/// declarations that can be completed at a later point.
330-
///
331-
/// \param SharedState The importer specific lookup table which may be
332-
/// shared amongst several ASTImporter objects.
333-
/// If not set then the original C/C++ lookup is used.
334-
ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
335-
ASTUnit &FromUnit, bool MinimalImport,
336-
std::shared_ptr<ASTImporterSharedState> SharedState = nullptr);
337310

338311
virtual ~ASTImporter();
339312

clang/include/clang/AST/ASTImporterSharedState.h

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,18 @@
1717

1818
#include "clang/AST/ASTImporterLookupTable.h"
1919
#include "clang/AST/Decl.h"
20-
#include "clang/Basic/SourceLocation.h"
2120
#include "llvm/ADT/DenseMap.h"
2221
// FIXME We need this because of ImportError.
2322
#include "clang/AST/ASTImporter.h"
2423

2524
namespace clang {
2625

27-
class ASTUnit;
2826
class TranslationUnitDecl;
2927

3028
/// Importer specific state, which may be shared amongst several ASTImporter
3129
/// objects.
3230
class ASTImporterSharedState {
33-
public:
34-
using ImportedFileIDMap =
35-
llvm::DenseMap<FileID, std::pair<FileID, ASTUnit *>>;
3631

37-
private:
3832
/// Pointer to the import specific lookup table.
3933
std::unique_ptr<ASTImporterLookupTable> LookupTable;
4034

@@ -49,16 +43,6 @@ class ASTImporterSharedState {
4943
// FIXME put ImportedFromDecls here!
5044
// And from that point we can better encapsulate the lookup table.
5145

52-
/// Map of imported FileID's (in "To" context) to FileID in "From" context
53-
/// and the ASTUnit that contains the preprocessor and source manager for the
54-
/// "From" FileID. This map is used to lookup a FileID and its SourceManager
55-
/// when knowing only the FileID in the 'To' context. The FileID could be
56-
/// imported by any of multiple ASTImporter objects. The map is used because
57-
/// we do not want to loop over all ASTImporter's to find the one that
58-
/// imported the FileID. (The ASTUnit is usable to get the SourceManager and
59-
/// additional data.)
60-
ImportedFileIDMap ImportedFileIDs;
61-
6246
public:
6347
ASTImporterSharedState() = default;
6448

@@ -91,12 +75,6 @@ class ASTImporterSharedState {
9175
void setImportDeclError(Decl *To, ImportError Error) {
9276
ImportErrors[To] = Error;
9377
}
94-
95-
ImportedFileIDMap &getImportedFileIDs() { return ImportedFileIDs; }
96-
97-
const ImportedFileIDMap &getImportedFileIDs() const {
98-
return ImportedFileIDs;
99-
}
10078
};
10179

10280
} // namespace clang

clang/include/clang/CrossTU/CrossTranslationUnit.h

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -153,30 +153,18 @@ class CrossTranslationUnitContext {
153153
/// was passed to the constructor.
154154
///
155155
/// \return Returns the resulting definition or an error.
156-
llvm::Expected<const FunctionDecl *> importDefinition(const FunctionDecl *FD,
157-
ASTUnit *Unit);
158-
llvm::Expected<const VarDecl *> importDefinition(const VarDecl *VD,
159-
ASTUnit *Unit);
156+
llvm::Expected<const FunctionDecl *> importDefinition(const FunctionDecl *FD);
157+
llvm::Expected<const VarDecl *> importDefinition(const VarDecl *VD);
160158

161159
/// Get a name to identify a named decl.
162160
static std::string getLookupName(const NamedDecl *ND);
163161

164162
/// Emit diagnostics for the user for potential configuration errors.
165163
void emitCrossTUDiagnostics(const IndexError &IE);
166164

167-
/// Determine the original source location in the original TU for an
168-
/// imported source location.
169-
/// \p ToLoc Source location in the imported-to AST.
170-
/// \return Source location in the imported-from AST and the corresponding
171-
/// ASTUnit.
172-
/// If any error happens (ToLoc is a non-imported source location) empty is
173-
/// returned.
174-
llvm::Optional<std::pair<SourceLocation /*FromLoc*/, ASTUnit *>>
175-
getImportedFromSourceLocation(const clang::SourceLocation &ToLoc) const;
176-
177165
private:
178166
void lazyInitImporterSharedSt(TranslationUnitDecl *ToTU);
179-
ASTImporter &getOrCreateASTImporter(ASTUnit *Unit);
167+
ASTImporter &getOrCreateASTImporter(ASTContext &From);
180168
template <typename T>
181169
llvm::Expected<const T *> getCrossTUDefinitionImpl(const T *D,
182170
StringRef CrossTUDir,
@@ -186,7 +174,7 @@ class CrossTranslationUnitContext {
186174
const T *findDefInDeclContext(const DeclContext *DC,
187175
StringRef LookupName);
188176
template <typename T>
189-
llvm::Expected<const T *> importDefinitionImpl(const T *D, ASTUnit *Unit);
177+
llvm::Expected<const T *> importDefinitionImpl(const T *D);
190178

191179
llvm::StringMap<std::unique_ptr<clang::ASTUnit>> FileASTUnitMap;
192180
llvm::StringMap<clang::ASTUnit *> NameASTUnitMap;

clang/lib/AST/ASTImporter.cpp

Lines changed: 3 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "clang/AST/ASTImporter.h"
15+
#include "clang/AST/ASTImporterSharedState.h"
1516
#include "clang/AST/ASTContext.h"
1617
#include "clang/AST/ASTDiagnostic.h"
17-
#include "clang/AST/ASTImporterSharedState.h"
1818
#include "clang/AST/ASTStructuralEquivalence.h"
1919
#include "clang/AST/Attr.h"
2020
#include "clang/AST/Decl.h"
@@ -52,14 +52,13 @@
5252
#include "clang/Basic/SourceLocation.h"
5353
#include "clang/Basic/SourceManager.h"
5454
#include "clang/Basic/Specifiers.h"
55-
#include "clang/Frontend/ASTUnit.h"
5655
#include "llvm/ADT/APSInt.h"
5756
#include "llvm/ADT/ArrayRef.h"
5857
#include "llvm/ADT/DenseMap.h"
5958
#include "llvm/ADT/None.h"
6059
#include "llvm/ADT/Optional.h"
61-
#include "llvm/ADT/STLExtras.h"
6260
#include "llvm/ADT/ScopeExit.h"
61+
#include "llvm/ADT/STLExtras.h"
6362
#include "llvm/ADT/SmallVector.h"
6463
#include "llvm/Support/Casting.h"
6564
#include "llvm/Support/ErrorHandling.h"
@@ -7717,22 +7716,9 @@ ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
77177716
ASTContext &FromContext, FileManager &FromFileManager,
77187717
bool MinimalImport,
77197718
std::shared_ptr<ASTImporterSharedState> SharedState)
7720-
: ASTImporter(ToContext, ToFileManager, FromContext, FromFileManager,
7721-
nullptr, MinimalImport, SharedState){}
7722-
ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
7723-
ASTUnit &FromUnit, bool MinimalImport,
7724-
std::shared_ptr<ASTImporterSharedState> SharedState)
7725-
: ASTImporter(ToContext, ToFileManager, FromUnit.getASTContext(),
7726-
FromUnit.getFileManager(), &FromUnit, MinimalImport,
7727-
SharedState){}
7728-
7729-
ASTImporter::ASTImporter(ASTContext &ToContext, FileManager &ToFileManager,
7730-
ASTContext &FromContext, FileManager &FromFileManager,
7731-
ASTUnit *FromUnit, bool MinimalImport,
7732-
std::shared_ptr<ASTImporterSharedState> SharedState)
77337719
: SharedState(SharedState), ToContext(ToContext), FromContext(FromContext),
77347720
ToFileManager(ToFileManager), FromFileManager(FromFileManager),
7735-
FromUnit(FromUnit), Minimal(MinimalImport) {
7721+
Minimal(MinimalImport) {
77367722

77377723
// Create a default state without the lookup table: LLDB case.
77387724
if (!SharedState) {
@@ -8435,13 +8421,6 @@ Expected<FileID> ASTImporter::Import(FileID FromID, bool IsBuiltin) {
84358421
assert(ToID.isValid() && "Unexpected invalid fileID was created.");
84368422

84378423
ImportedFileIDs[FromID] = ToID;
8438-
if (FromUnit) {
8439-
assert(SharedState->getImportedFileIDs().find(ToID) ==
8440-
SharedState->getImportedFileIDs().end() &&
8441-
"FileID already imported!");
8442-
SharedState->getImportedFileIDs()[ToID] = std::make_pair(FromID, FromUnit);
8443-
}
8444-
84458424
return ToID;
84468425
}
84478426

clang/lib/CrossTU/CrossTranslationUnit.cpp

Lines changed: 11 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -295,7 +295,7 @@ llvm::Expected<const T *> CrossTranslationUnitContext::getCrossTUDefinitionImpl(
295295

296296
TranslationUnitDecl *TU = Unit->getASTContext().getTranslationUnitDecl();
297297
if (const T *ResultDecl = findDefInDeclContext<T>(TU, LookupName))
298-
return importDefinition(ResultDecl, Unit);
298+
return importDefinition(ResultDecl);
299299
return llvm::make_error<IndexError>(index_error_code::failed_import);
300300
}
301301

@@ -411,13 +411,10 @@ llvm::Expected<ASTUnit *> CrossTranslationUnitContext::loadExternalAST(
411411

412412
template <typename T>
413413
llvm::Expected<const T *>
414-
CrossTranslationUnitContext::importDefinitionImpl(const T *D, ASTUnit *Unit) {
414+
CrossTranslationUnitContext::importDefinitionImpl(const T *D) {
415415
assert(hasBodyOrInit(D) && "Decls to be imported should have body or init.");
416416

417-
assert(&D->getASTContext() == &Unit->getASTContext() &&
418-
"ASTContext of Decl and the unit should match.");
419-
ASTImporter &Importer = getOrCreateASTImporter(Unit);
420-
417+
ASTImporter &Importer = getOrCreateASTImporter(D->getASTContext());
421418
auto ToDeclOrError = Importer.Import(D);
422419
if (!ToDeclOrError) {
423420
handleAllErrors(ToDeclOrError.takeError(),
@@ -444,15 +441,13 @@ CrossTranslationUnitContext::importDefinitionImpl(const T *D, ASTUnit *Unit) {
444441
}
445442

446443
llvm::Expected<const FunctionDecl *>
447-
CrossTranslationUnitContext::importDefinition(const FunctionDecl *FD,
448-
ASTUnit *Unit) {
449-
return importDefinitionImpl(FD, Unit);
444+
CrossTranslationUnitContext::importDefinition(const FunctionDecl *FD) {
445+
return importDefinitionImpl(FD);
450446
}
451447

452448
llvm::Expected<const VarDecl *>
453-
CrossTranslationUnitContext::importDefinition(const VarDecl *VD,
454-
ASTUnit *Unit) {
455-
return importDefinitionImpl(VD, Unit);
449+
CrossTranslationUnitContext::importDefinition(const VarDecl *VD) {
450+
return importDefinitionImpl(VD);
456451
}
457452

458453
void CrossTranslationUnitContext::lazyInitImporterSharedSt(
@@ -462,40 +457,17 @@ void CrossTranslationUnitContext::lazyInitImporterSharedSt(
462457
}
463458

464459
ASTImporter &
465-
CrossTranslationUnitContext::getOrCreateASTImporter(ASTUnit *Unit) {
466-
ASTContext &From = Unit->getASTContext();
467-
460+
CrossTranslationUnitContext::getOrCreateASTImporter(ASTContext &From) {
468461
auto I = ASTUnitImporterMap.find(From.getTranslationUnitDecl());
469462
if (I != ASTUnitImporterMap.end())
470463
return *I->second;
471464
lazyInitImporterSharedSt(Context.getTranslationUnitDecl());
472-
ASTImporter *NewImporter =
473-
new ASTImporter(Context, Context.getSourceManager().getFileManager(),
474-
*Unit, false, ImporterSharedSt);
465+
ASTImporter *NewImporter = new ASTImporter(
466+
Context, Context.getSourceManager().getFileManager(), From,
467+
From.getSourceManager().getFileManager(), false, ImporterSharedSt);
475468
ASTUnitImporterMap[From.getTranslationUnitDecl()].reset(NewImporter);
476469
return *NewImporter;
477470
}
478471

479-
llvm::Optional<std::pair<SourceLocation, ASTUnit *>>
480-
CrossTranslationUnitContext::getImportedFromSourceLocation(
481-
const clang::SourceLocation &ToLoc) const {
482-
if (!ImporterSharedSt)
483-
return {};
484-
485-
const SourceManager &SM = Context.getSourceManager();
486-
auto DecToLoc = SM.getDecomposedLoc(ToLoc);
487-
488-
auto I = ImporterSharedSt->getImportedFileIDs().find(DecToLoc.first);
489-
if (I == ImporterSharedSt->getImportedFileIDs().end())
490-
return {};
491-
492-
FileID FromID = I->second.first;
493-
clang::ASTUnit *Unit = I->second.second;
494-
SourceLocation FromLoc =
495-
Unit->getSourceManager().getComposedLoc(FromID, DecToLoc.second);
496-
497-
return std::make_pair(FromLoc, Unit);
498-
}
499-
500472
} // namespace cross_tu
501473
} // namespace clang

clang/unittests/CrossTU/CrossTranslationUnitTest.cpp

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,13 @@ class CTUASTConsumer : public clang::ASTConsumer {
2828
: CTU(CI), Success(Success) {}
2929

3030
void HandleTranslationUnit(ASTContext &Ctx) {
31-
auto FindFInTU = [](const TranslationUnitDecl *TU) {
32-
const FunctionDecl *FD = nullptr;
33-
for (const Decl *D : TU->decls()) {
34-
FD = dyn_cast<FunctionDecl>(D);
35-
if (FD && FD->getName() == "f")
36-
break;
37-
}
38-
return FD;
39-
};
40-
4131
const TranslationUnitDecl *TU = Ctx.getTranslationUnitDecl();
42-
const FunctionDecl *FD = FindFInTU(TU);
32+
const FunctionDecl *FD = nullptr;
33+
for (const Decl *D : TU->decls()) {
34+
FD = dyn_cast<FunctionDecl>(D);
35+
if (FD && FD->getName() == "f")
36+
break;
37+
}
4338
assert(FD && FD->getName() == "f");
4439
bool OrigFDHasBody = FD->hasBody();
4540

@@ -83,28 +78,6 @@ class CTUASTConsumer : public clang::ASTConsumer {
8378
if (NewFDorError) {
8479
const FunctionDecl *NewFD = *NewFDorError;
8580
*Success = NewFD && NewFD->hasBody() && !OrigFDHasBody;
86-
87-
if (NewFD) {
88-
// Check GetImportedFromSourceLocation.
89-
llvm::Optional<std::pair<SourceLocation, ASTUnit *>> SLocResult =
90-
CTU.getImportedFromSourceLocation(NewFD->getLocation());
91-
EXPECT_TRUE(SLocResult);
92-
if (SLocResult) {
93-
SourceLocation OrigSLoc = (*SLocResult).first;
94-
ASTUnit *OrigUnit = (*SLocResult).second;
95-
// OrigUnit is created internally by CTU (is not the
96-
// ASTWithDefinition).
97-
TranslationUnitDecl *OrigTU =
98-
OrigUnit->getASTContext().getTranslationUnitDecl();
99-
const FunctionDecl *FDWithDefinition = FindFInTU(OrigTU);
100-
EXPECT_TRUE(FDWithDefinition);
101-
if (FDWithDefinition) {
102-
EXPECT_EQ(FDWithDefinition->getName(), "f");
103-
EXPECT_TRUE(FDWithDefinition->isThisDeclarationADefinition());
104-
EXPECT_EQ(OrigSLoc, FDWithDefinition->getLocation());
105-
}
106-
}
107-
}
10881
}
10982
}
11083

0 commit comments

Comments
 (0)