Skip to content
This repository was archived by the owner on Mar 28, 2020. It is now read-only.

Commit 36e125b

Browse files
committed
Changed every use of ASTImporter::Import to Import_New
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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@357913 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent 9a63380 commit 36e125b

File tree

5 files changed

+113
-49
lines changed

5 files changed

+113
-49
lines changed

lib/AST/ASTImporter.cpp

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7792,9 +7792,10 @@ Expected<DeclContext *> ASTImporter::ImportContext(DeclContext *FromDC) {
77927792
if (!FromDC)
77937793
return FromDC;
77947794

7795-
auto *ToDC = cast_or_null<DeclContext>(Import(cast<Decl>(FromDC)));
7796-
if (!ToDC)
7797-
return nullptr;
7795+
ExpectedDecl ToDCOrErr = Import_New(cast<Decl>(FromDC));
7796+
if (!ToDCOrErr)
7797+
return ToDCOrErr.takeError();
7798+
auto *ToDC = cast<DeclContext>(*ToDCOrErr);
77987799

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

85918592
bool ASTImporter::IsStructurallyEquivalent(QualType From, QualType To,
85928593
bool Complain) {
8593-
llvm::DenseMap<const Type *, const Type *>::iterator Pos
8594-
= ImportedTypes.find(From.getTypePtr());
8595-
if (Pos != ImportedTypes.end() && ToContext.hasSameType(Import(From), To))
8596-
return true;
8594+
llvm::DenseMap<const Type *, const Type *>::iterator Pos =
8595+
ImportedTypes.find(From.getTypePtr());
8596+
if (Pos != ImportedTypes.end()) {
8597+
if (ExpectedType ToFromOrErr = Import_New(From)) {
8598+
if (ToContext.hasSameType(*ToFromOrErr, To))
8599+
return true;
8600+
} else {
8601+
llvm::consumeError(ToFromOrErr.takeError());
8602+
}
8603+
}
85978604

85988605
StructuralEquivalenceContext Ctx(FromContext, ToContext, NonEquivalentDecls,
85998606
getStructuralEquivalenceKind(*this), false,

lib/AST/ExternalASTMerger.cpp

Lines changed: 37 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,12 @@ LookupSameContext(Source<TranslationUnitDecl *> SourceTU, const DeclContext *DC,
5656
}
5757
auto *ND = cast<NamedDecl>(DC);
5858
DeclarationName Name = ND->getDeclName();
59-
Source<DeclarationName> SourceName = ReverseImporter.Import(Name);
59+
auto SourceNameOrErr = ReverseImporter.Import_New(Name);
60+
if (!SourceNameOrErr) {
61+
llvm::consumeError(SourceNameOrErr.takeError());
62+
return nullptr;
63+
}
64+
Source<DeclarationName> SourceName = *SourceNameOrErr;
6065
DeclContext::lookup_result SearchResult =
6166
SourceParentDC.get()->lookup(SourceName.get());
6267
size_t SearchResultSize = SearchResult.size();
@@ -354,9 +359,13 @@ void ExternalASTMerger::RemoveSources(llvm::ArrayRef<ImporterSource> Sources) {
354359

355360
template <typename DeclTy>
356361
static bool importSpecializations(DeclTy *D, ASTImporter *Importer) {
357-
for (auto *Spec : D->specializations())
358-
if (!Importer->Import(Spec))
362+
for (auto *Spec : D->specializations()) {
363+
auto ImportedSpecOrError = Importer->Import_New(Spec);
364+
if (!ImportedSpecOrError) {
365+
llvm::consumeError(ImportedSpecOrError.takeError());
359366
return true;
367+
}
368+
}
360369
return false;
361370
}
362371

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

386-
ForEachMatchingDC(DC, [&](ASTImporter &Forward, ASTImporter &Reverse,
387-
Source<const DeclContext *> SourceDC) -> bool {
388-
DeclarationName FromName = Reverse.Import(Name);
389-
DeclContextLookupResult Result = SourceDC.get()->lookup(FromName);
390-
for (NamedDecl *FromD : Result) {
391-
FilterFoundDecl(std::make_pair(FromD, &Forward));
392-
}
393-
return false;
394-
});
395+
ForEachMatchingDC(DC,
396+
[&](ASTImporter &Forward, ASTImporter &Reverse,
397+
Source<const DeclContext *> SourceDC) -> bool {
398+
auto FromNameOrErr = Reverse.Import_New(Name);
399+
if (!FromNameOrErr) {
400+
llvm::consumeError(FromNameOrErr.takeError());
401+
return false;
402+
}
403+
DeclContextLookupResult Result =
404+
SourceDC.get()->lookup(*FromNameOrErr);
405+
for (NamedDecl *FromD : Result) {
406+
FilterFoundDecl(std::make_pair(FromD, &Forward));
407+
}
408+
return false;
409+
});
395410

396411
if (Candidates.empty())
397412
return false;
@@ -400,7 +415,10 @@ bool ExternalASTMerger::FindExternalVisibleDeclsByName(const DeclContext *DC,
400415
for (const Candidate &C : Candidates) {
401416
Decl *LookupRes = C.first.get();
402417
ASTImporter *Importer = C.second;
403-
NamedDecl *ND = cast_or_null<NamedDecl>(Importer->Import(LookupRes));
418+
auto NDOrErr = Importer->Import_New(LookupRes);
419+
assert(NDOrErr);
420+
(void)static_cast<bool>(NDOrErr);
421+
NamedDecl *ND = cast_or_null<NamedDecl>(*NDOrErr);
404422
assert(ND);
405423
// If we don't import specialization, they are not available via lookup
406424
// because the lookup result is imported TemplateDecl and it does not
@@ -422,9 +440,12 @@ void ExternalASTMerger::FindExternalLexicalDecls(
422440
Source<const DeclContext *> SourceDC) -> bool {
423441
for (const Decl *SourceDecl : SourceDC.get()->decls()) {
424442
if (IsKindWeWant(SourceDecl->getKind())) {
425-
Decl *ImportedDecl = Forward.Import(const_cast<Decl *>(SourceDecl));
426-
assert(!ImportedDecl || IsSameDC(ImportedDecl->getDeclContext(), DC));
427-
(void)ImportedDecl;
443+
auto ImportedDeclOrErr = Forward.Import_New(SourceDecl);
444+
if (ImportedDeclOrErr)
445+
assert(!(*ImportedDeclOrErr) ||
446+
IsSameDC((*ImportedDeclOrErr)->getDeclContext(), DC));
447+
else
448+
llvm::consumeError(ImportedDeclOrErr.takeError());
428449
}
429450
}
430451
return false;

lib/CrossTU/CrossTranslationUnit.cpp

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -357,13 +357,28 @@ CrossTranslationUnitContext::importDefinition(const FunctionDecl *FD) {
357357
assert(FD->hasBody() && "Functions to be imported should have body.");
358358

359359
ASTImporter &Importer = getOrCreateASTImporter(FD->getASTContext());
360-
auto *ToDecl =
361-
cast_or_null<FunctionDecl>(Importer.Import(const_cast<FunctionDecl *>(FD)));
362-
if (!ToDecl)
360+
auto ToDeclOrError = Importer.Import_New(FD);
361+
if (!ToDeclOrError) {
362+
handleAllErrors(ToDeclOrError.takeError(),
363+
[&](const ImportError &IE) {
364+
switch (IE.Error) {
365+
case ImportError::NameConflict:
366+
// FIXME: Add statistic.
367+
break;
368+
case ImportError::UnsupportedConstruct:
369+
// FIXME: Add statistic.
370+
break;
371+
case ImportError::Unknown:
372+
llvm_unreachable("Unknown import error happened.");
373+
break;
374+
}
375+
});
363376
return llvm::make_error<IndexError>(index_error_code::failed_import);
364-
assert(ToDecl->hasBody());
365-
assert(FD->hasBody() && "Functions already imported should have body.");
377+
}
378+
auto *ToDecl = cast<FunctionDecl>(*ToDeclOrError);
379+
assert(ToDecl->hasBody() && "Imported function should have body.");
366380
++NumGetCTUSuccess;
381+
367382
return ToDecl;
368383
}
369384

lib/Frontend/ASTMerge.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ void ASTMergeAction::ExecuteAction() {
6565
if (II->isStr("__va_list_tag") || II->isStr("__builtin_va_list"))
6666
continue;
6767

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

70-
if (ToD) {
71-
DeclGroupRef DGR(ToD);
70+
if (ToDOrError) {
71+
DeclGroupRef DGR(*ToDOrError);
7272
CI.getASTConsumer().HandleTopLevelDecl(DGR);
73+
} else {
74+
llvm::consumeError(ToDOrError.takeError());
7375
}
7476
}
7577
}

unittests/AST/ASTImporterTest.cpp

Lines changed: 37 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ class TestImportBase : public CompilerOptionSpecificTest,
8989
public ::testing::WithParamInterface<ArgVector> {
9090

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

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

103-
auto Imported = Importer.Import(Node);
103+
auto Imported = Importer.Import_New(Node);
104104

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

111-
// This traverses the AST to catch certain bugs like poorly or not
112-
// implemented subtrees.
113-
Imported->dump(ToNothing);
112+
// This traverses the AST to catch certain bugs like poorly or not
113+
// implemented subtrees.
114+
(*Imported)->dump(ToNothing);
115+
}
114116

115117
return Imported;
116118
}
@@ -151,11 +153,16 @@ class TestImportBase : public CompilerOptionSpecificTest,
151153
EXPECT_TRUE(Verifier.match(ToImport, WrapperMatcher));
152154

153155
auto Imported = importNode(FromAST.get(), ToAST.get(), Importer, ToImport);
154-
if (!Imported)
155-
return testing::AssertionFailure() << "Import failed, nullptr returned!";
156-
156+
if (!Imported) {
157+
std::string ErrorText;
158+
handleAllErrors(
159+
Imported.takeError(),
160+
[&ErrorText](const ImportError &Err) { ErrorText = Err.message(); });
161+
return testing::AssertionFailure()
162+
<< "Import failed, error: \"" << ErrorText << "\"!";
163+
}
157164

158-
return Verifier.match(Imported, WrapperMatcher);
165+
return Verifier.match(*Imported, WrapperMatcher);
159166
}
160167

161168
template <typename NodeType>
@@ -277,7 +284,9 @@ class TestImportBase : public CompilerOptionSpecificTest,
277284
EXPECT_TRUE(FoundDecl.size() == 1);
278285
const Decl *ToImport = selectFirst<Decl>(DeclToImportID, FoundDecl);
279286
auto Imported = importNode(From, To, *ImporterRef, ToImport);
280-
EXPECT_TRUE(Imported);
287+
EXPECT_TRUE(static_cast<bool>(Imported));
288+
if (!Imported)
289+
llvm::consumeError(Imported.takeError());
281290
}
282291

283292
// Find the declaration and import it.
@@ -339,13 +348,23 @@ class ASTImporterTestBase : public CompilerOptionSpecificTest {
339348
Decl *import(ASTImporterLookupTable &LookupTable, ASTUnit *ToAST,
340349
Decl *FromDecl) {
341350
lazyInitImporter(LookupTable, ToAST);
342-
return Importer->Import(FromDecl);
351+
if (auto ImportedOrErr = Importer->Import_New(FromDecl))
352+
return *ImportedOrErr;
353+
else {
354+
llvm::consumeError(ImportedOrErr.takeError());
355+
return nullptr;
356+
}
343357
}
344358

345359
QualType import(ASTImporterLookupTable &LookupTable, ASTUnit *ToAST,
346360
QualType FromType) {
347361
lazyInitImporter(LookupTable, ToAST);
348-
return Importer->Import(FromType);
362+
if (auto ImportedOrErr = Importer->Import_New(FromType))
363+
return *ImportedOrErr;
364+
else {
365+
llvm::consumeError(ImportedOrErr.takeError());
366+
return QualType{};
367+
}
349368
}
350369
};
351370

0 commit comments

Comments
 (0)