Skip to content

Commit

Permalink
Changed every use of ASTImporter::Import to Import_New
Browse files Browse the repository at this point in the history
Reviewers: a.sidorin, shafik, martong, a_sidorin

Reviewed By: a_sidorin

Subscribers: rnkovacs, dkrupp, martong, Szelethus, gamesh411, cfe-commits

Tags: #clang

Differential Revision: https://reviews.llvm.org/D55049

llvm-svn: 357913
  • Loading branch information
balazske committed Apr 8, 2019
1 parent 50c3b29 commit a1f6b10
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 49 deletions.
21 changes: 14 additions & 7 deletions clang/lib/AST/ASTImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7792,9 +7792,10 @@ Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
if (!FromDC)
return FromDC;

auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
if (!ToDC)
return nullptr;
ExpectedDecl ToDCOrErr = Import_New(cast<Decl>(FromDC));
if (!ToDCOrErr)
return ToDCOrErr.takeError();
auto *ToDC = cast<DeclContext>(*ToDCOrErr);

// When we're using a record/enum/Objective-C class/protocol as a context, we
// need it to have a definition.
Expand Down Expand Up @@ -8590,10 +8591,16 @@ Decl *ASTImporter::MapImported(Decl *From, Decl *To) {

bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
bool Complain) {
llvm::DenseMap<const Type *, const Type *>::iterator Pos
= ImportedTypes.find(From.getTypePtr());
if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
return true;
llvm::DenseMap<const Type *, const Type *>::iterator Pos =
ImportedTypes.find(From.getTypePtr());
if (Pos != ImportedTypes.end()) {
if (ExpectedType ToFromOrErr = Import_New(From)) {
if (ToContext.hasSameType(*ToFromOrErr, To))
return true;
} else {
llvm::consumeError(ToFromOrErr.takeError());
}
}

StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
getStructuralEquivalenceKind(*this), false,
Expand Down
53 changes: 37 additions & 16 deletions clang/lib/AST/ExternalASTMerger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ LookupSameContext(Source<TranslationUnitDecl *> SourceTU, const DeclContext *DC,
}
auto *ND = cast<NamedDecl>(DC);
DeclarationName Name = ND->getDeclName();
Source<DeclarationName> SourceName = ReverseImporter.Import(Name);
auto SourceNameOrErr = ReverseImporter.Import_New(Name);
if (!SourceNameOrErr) {
llvm::consumeError(SourceNameOrErr.takeError());
return nullptr;
}
Source<DeclarationName> SourceName = *SourceNameOrErr;
DeclContext::lookup_result SearchResult =
SourceParentDC.get()->lookup(SourceName.get());
size_t SearchResultSize = SearchResult.size();
Expand Down Expand Up @@ -354,9 +359,13 @@ void ExternalASTMerger::RemoveSources(llvm::ArrayRef<ImporterSource> Sources) {

template <typename DeclTy>
static bool importSpecializations(DeclTy *D, ASTImporter *Importer) {
for (auto *Spec : D->specializations())
if (!Importer->Import(Spec))
for (auto *Spec : D->specializations()) {
auto ImportedSpecOrError = Importer->Import_New(Spec);
if (!ImportedSpecOrError) {
llvm::consumeError(ImportedSpecOrError.takeError());
return true;
}
}
return false;
}

Expand All @@ -383,15 +392,21 @@ bool ExternalASTMerger::FindExternalVisibleDeclsByName(const DeclContext *DC,
Candidates.push_back(C);
};

ForEachMatchingDC(DC, [&](ASTImporter &Forward, ASTImporter &Reverse,
Source<const DeclContext *> SourceDC) -> bool {
DeclarationName FromName = Reverse.Import(Name);
DeclContextLookupResult Result = SourceDC.get()->lookup(FromName);
for (NamedDecl *FromD : Result) {
FilterFoundDecl(std::make_pair(FromD, &Forward));
}
return false;
});
ForEachMatchingDC(DC,
[&](ASTImporter &Forward, ASTImporter &Reverse,
Source<const DeclContext *> SourceDC) -> bool {
auto FromNameOrErr = Reverse.Import_New(Name);
if (!FromNameOrErr) {
llvm::consumeError(FromNameOrErr.takeError());
return false;
}
DeclContextLookupResult Result =
SourceDC.get()->lookup(*FromNameOrErr);
for (NamedDecl *FromD : Result) {
FilterFoundDecl(std::make_pair(FromD, &Forward));
}
return false;
});

if (Candidates.empty())
return false;
Expand All @@ -400,7 +415,10 @@ bool ExternalASTMerger::FindExternalVisibleDeclsByName(const DeclContext *DC,
for (const Candidate &C : Candidates) {
Decl *LookupRes = C.first.get();
ASTImporter *Importer = C.second;
NamedDecl *ND = cast_or_null<NamedDecl>(Importer->Import(LookupRes));
auto NDOrErr = Importer->Import_New(LookupRes);
assert(NDOrErr);
(void)static_cast<bool>(NDOrErr);
NamedDecl *ND = cast_or_null<NamedDecl>(*NDOrErr);
assert(ND);
// If we don't import specialization, they are not available via lookup
// because the lookup result is imported TemplateDecl and it does not
Expand All @@ -422,9 +440,12 @@ void ExternalASTMerger::FindExternalLexicalDecls(
Source<const DeclContext *> SourceDC) -> bool {
for (const Decl *SourceDecl : SourceDC.get()->decls()) {
if (IsKindWeWant(SourceDecl->getKind())) {
Decl *ImportedDecl = Forward.Import(const_cast<Decl *>(SourceDecl));
assert(!ImportedDecl || IsSameDC(ImportedDecl->getDeclContext(), DC));
(void)ImportedDecl;
auto ImportedDeclOrErr = Forward.Import_New(SourceDecl);
if (ImportedDeclOrErr)
assert(!(*ImportedDeclOrErr) ||
IsSameDC((*ImportedDeclOrErr)->getDeclContext(), DC));
else
llvm::consumeError(ImportedDeclOrErr.takeError());
}
}
return false;
Expand Down
25 changes: 20 additions & 5 deletions clang/lib/CrossTU/CrossTranslationUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -357,13 +357,28 @@ CrossTranslationUnitContext::importDefinition(const FunctionDecl *FD) {
assert(FD->hasBody() && "Functions to be imported should have body.");

ASTImporter &Importer = getOrCreateASTImporter(FD->getASTContext());
auto *ToDecl =
cast_or_null<FunctionDecl>(Importer.Import(const_cast<FunctionDecl *>(FD)));
if (!ToDecl)
auto ToDeclOrError = Importer.Import_New(FD);
if (!ToDeclOrError) {
handleAllErrors(ToDeclOrError.takeError(),
[&](const ImportError &IE) {
switch (IE.Error) {
case ImportError::NameConflict:
// FIXME: Add statistic.
break;
case ImportError::UnsupportedConstruct:
// FIXME: Add statistic.
break;
case ImportError::Unknown:
llvm_unreachable("Unknown import error happened.");
break;
}
});
return llvm::make_error<IndexError>(index_error_code::failed_import);
assert(ToDecl->hasBody());
assert(FD->hasBody() && "Functions already imported should have body.");
}
auto *ToDecl = cast<FunctionDecl>(*ToDeclOrError);
assert(ToDecl->hasBody() && "Imported function should have body.");
++NumGetCTUSuccess;

return ToDecl;
}

Expand Down
8 changes: 5 additions & 3 deletions clang/lib/Frontend/ASTMerge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,11 +65,13 @@ void ASTMergeAction::ExecuteAction() {
if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
continue;

Decl *ToD = Importer.Import(D);
llvm::Expected<Decl *> ToDOrError = Importer.Import_New(D);

if (ToD) {
DeclGroupRef DGR(ToD);
if (ToDOrError) {
DeclGroupRef DGR(*ToDOrError);
CI.getASTConsumer().HandleTopLevelDecl(DGR);
} else {
llvm::consumeError(ToDOrError.takeError());
}
}
}
Expand Down
55 changes: 37 additions & 18 deletions clang/unittests/AST/ASTImporterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ class TestImportBase : public CompilerOptionSpecificTest,
public ::testing::WithParamInterface<ArgVector> {

template <typename NodeType>
NodeType importNode(ASTUnit *From, ASTUnit *To, ASTImporter &Importer,
NodeType Node) {
llvm::Expected<NodeType> importNode(ASTUnit *From, ASTUnit *To,
ASTImporter &Importer, NodeType Node) {
ASTContext &ToCtx = To->getASTContext();

// Add 'From' file to virtual file system so importer can 'find' it
Expand All @@ -100,17 +100,19 @@ class TestImportBase : public CompilerOptionSpecificTest,
createVirtualFileIfNeeded(To, FromFileName,
From->getBufferForFile(FromFileName));

auto Imported = Importer.Import(Node);
auto Imported = Importer.Import_New(Node);

// This should dump source locations and assert if some source locations
// were not imported.
SmallString<1024> ImportChecker;
llvm::raw_svector_ostream ToNothing(ImportChecker);
ToCtx.getTranslationUnitDecl()->print(ToNothing);
if (Imported) {
// This should dump source locations and assert if some source locations
// were not imported.
SmallString<1024> ImportChecker;
llvm::raw_svector_ostream ToNothing(ImportChecker);
ToCtx.getTranslationUnitDecl()->print(ToNothing);

// This traverses the AST to catch certain bugs like poorly or not
// implemented subtrees.
Imported->dump(ToNothing);
// This traverses the AST to catch certain bugs like poorly or not
// implemented subtrees.
(*Imported)->dump(ToNothing);
}

return Imported;
}
Expand Down Expand Up @@ -151,11 +153,16 @@ class TestImportBase : public CompilerOptionSpecificTest,
EXPECT_TRUE(Verifier.match(ToImport, WrapperMatcher));

auto Imported = importNode(FromAST.get(), ToAST.get(), Importer, ToImport);
if (!Imported)
return testing::AssertionFailure() << "Import failed, nullptr returned!";

if (!Imported) {
std::string ErrorText;
handleAllErrors(
Imported.takeError(),
[&ErrorText](const ImportError &Err) { ErrorText = Err.message(); });
return testing::AssertionFailure()
<< "Import failed, error: \"" << ErrorText << "\"!";
}

return Verifier.match(Imported, WrapperMatcher);
return Verifier.match(*Imported, WrapperMatcher);
}

template <typename NodeType>
Expand Down Expand Up @@ -277,7 +284,9 @@ class TestImportBase : public CompilerOptionSpecificTest,
EXPECT_TRUE(FoundDecl.size() == 1);
const Decl *ToImport = selectFirst<Decl>(DeclToImportID, FoundDecl);
auto Imported = importNode(From, To, *ImporterRef, ToImport);
EXPECT_TRUE(Imported);
EXPECT_TRUE(static_cast<bool>(Imported));
if (!Imported)
llvm::consumeError(Imported.takeError());
}

// Find the declaration and import it.
Expand Down Expand Up @@ -339,13 +348,23 @@ class ASTImporterTestBase : public CompilerOptionSpecificTest {
Decl *import(ASTImporterLookupTable &LookupTable, ASTUnit *ToAST,
Decl *FromDecl) {
lazyInitImporter(LookupTable, ToAST);
return Importer->Import(FromDecl);
if (auto ImportedOrErr = Importer->Import_New(FromDecl))
return *ImportedOrErr;
else {
llvm::consumeError(ImportedOrErr.takeError());
return nullptr;
}
}

QualType import(ASTImporterLookupTable &LookupTable, ASTUnit *ToAST,
QualType FromType) {
lazyInitImporter(LookupTable, ToAST);
return Importer->Import(FromType);
if (auto ImportedOrErr = Importer->Import_New(FromType))
return *ImportedOrErr;
else {
llvm::consumeError(ImportedOrErr.takeError());
return QualType{};
}
}
};

Expand Down

0 comments on commit a1f6b10

Please sign in to comment.