diff options
author | Gabor Marton <martongabesz@gmail.com> | 2018-07-12 09:42:05 +0000 |
---|---|---|
committer | Gabor Marton <martongabesz@gmail.com> | 2018-07-12 09:42:05 +0000 |
commit | 2cd0afaeb39b51af80e3101638d01d047a1311b8 (patch) | |
tree | bce2ec30b0e724f16d7d24165a6d76acd043e783 /unittests | |
parent | ce51829da05caa49bfbaaef28694bc770f0eb233 (diff) |
[ASTImporter] Refactor Decl creation
Summary:
Generalize the creation of Decl nodes during Import. With this patch we do the
same things after and before a new AST node is created (::Create) The import
logic should be really simple, we create the node, then we mark that as
imported, then we recursively import the parts for that node and then set them
on that node. However, the AST is actually a graph, so we have to handle
circles. If we mark something as imported (`MapImported()`) then we return with
the corresponding `To` decl whenever we want to import that node again, this way
circles are handled. In order to make this algorithm work we must ensure
things, which are handled in the generic CreateDecl<> template:
* There are no `Import()` calls in between any node creation (::Create)
and the `MapImported()` call.
* Before actually creating an AST node (::Create), we must check if
the Node had been imported already, if yes then return with that one.
One very important case for this is connected to templates: we may
start an import both from the templated decl of a template and from
the template itself.
Now, the virtual `Imported` function is called in `ASTImporter::Impor(Decl *)`,
but only once, when the `Decl` is imported. One point of this refactor is to
separate responsibilities. The original `Imported()` had 3 responsibilities:
- notify subclasses when an import happened
- register the decl into `ImportedDecls`
- initialise the Decl (set attributes, etc)
Now all of these are in separate functions:
- `Imported`
- `MapImported`
- `InitializeImportedDecl`
I tried to check all the clients, I executed tests for `ExternalASTMerger.cpp`
and some unittests for lldb.
Reviewers: a.sidorin, balazske, xazax.hun, r.stahl
Subscribers: rnkovacs, dkrupp, cfe-commits
Differential Revision: https://reviews.llvm.org/D47632
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@336896 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'unittests')
-rw-r--r-- | unittests/AST/ASTImporterTest.cpp | 102 | ||||
-rw-r--r-- | unittests/AST/StructuralEquivalenceTest.cpp | 5 |
2 files changed, 74 insertions, 33 deletions
diff --git a/unittests/AST/ASTImporterTest.cpp b/unittests/AST/ASTImporterTest.cpp index 50f7285a39..1b3678c123 100644 --- a/unittests/AST/ASTImporterTest.cpp +++ b/unittests/AST/ASTImporterTest.cpp @@ -284,12 +284,15 @@ class ASTImporterTestBase : public ParameterizedTestsFixture { // Buffer for the To context, must live in the test scope. std::string ToCode; + // Represents a "From" translation unit and holds an importer object which we + // use to import from this translation unit. struct TU { // Buffer for the context, must live in the test scope. std::string Code; std::string FileName; std::unique_ptr<ASTUnit> Unit; TranslationUnitDecl *TUDecl = nullptr; + std::unique_ptr<ASTImporter> Importer; TU(StringRef Code, StringRef FileName, ArgVector Args) : Code(Code), FileName(FileName), Unit(tooling::buildASTFromCodeWithArgs(this->Code, Args, @@ -297,6 +300,16 @@ class ASTImporterTestBase : public ParameterizedTestsFixture { TUDecl(Unit->getASTContext().getTranslationUnitDecl()) { Unit->enableSourceFileDiagnostics(); } + + Decl *import(ASTUnit *ToAST, Decl *FromDecl) { + assert(ToAST); + if (!Importer) { + Importer.reset(new ASTImporter( + ToAST->getASTContext(), ToAST->getFileManager(), + Unit->getASTContext(), Unit->getFileManager(), false)); + } + return Importer->Import(FromDecl); + } }; // We may have several From contexts and related translation units. In each @@ -329,14 +342,10 @@ public: ToAST = tooling::buildASTFromCodeWithArgs(ToCode, ToArgs, OutputFileName); ToAST->enableSourceFileDiagnostics(); - ASTContext &FromCtx = FromTU.Unit->getASTContext(), - &ToCtx = ToAST->getASTContext(); + ASTContext &FromCtx = FromTU.Unit->getASTContext(); createVirtualFileIfNeeded(ToAST.get(), InputFileName, FromTU.Code); - ASTImporter Importer(ToCtx, ToAST->getFileManager(), FromCtx, - FromTU.Unit->getFileManager(), false); - IdentifierInfo *ImportedII = &FromCtx.Idents.get(Identifier); assert(ImportedII && "Declaration with the given identifier " "should be specified in test!"); @@ -347,7 +356,8 @@ public: assert(FoundDecls.size() == 1); - Decl *Imported = Importer.Import(FoundDecls.front()); + Decl *Imported = FromTU.import(ToAST.get(), FoundDecls.front()); + assert(Imported); return std::make_tuple(*FoundDecls.begin(), Imported); } @@ -401,11 +411,7 @@ public: assert(It != FromTUs.end()); createVirtualFileIfNeeded(ToAST.get(), It->FileName, It->Code); - ASTContext &FromCtx = From->getASTContext(), - &ToCtx = ToAST->getASTContext(); - ASTImporter Importer(ToCtx, ToAST->getFileManager(), FromCtx, - FromCtx.getSourceManager().getFileManager(), false); - return Importer.Import(From); + return It->import(ToAST.get(), From); } ~ASTImporterTestBase() { @@ -1089,8 +1095,7 @@ TEST_P(ASTImporterTestBase, ImportOfTemplatedDeclOfClassTemplateDecl) { EXPECT_EQ(ToTemplated1, ToTemplated); } -TEST_P(ASTImporterTestBase, - DISABLED_ImportOfTemplatedDeclOfFunctionTemplateDecl) { +TEST_P(ASTImporterTestBase, ImportOfTemplatedDeclOfFunctionTemplateDecl) { Decl *FromTU = getTuDecl("template<class X> void f(){}", Lang_CXX); auto From = FirstDeclMatcher<FunctionTemplateDecl>().match( FromTU, functionTemplateDecl()); @@ -1166,7 +1171,7 @@ TEST_P(ASTImporterTestBase, ImportCorrectTemplatedDecl) { ASSERT_EQ(ToTemplated1, ToTemplated); } -TEST_P(ASTImporterTestBase, DISABLED_ImportFunctionWithBackReferringParameter) { +TEST_P(ASTImporterTestBase, ImportFunctionWithBackReferringParameter) { Decl *From, *To; std::tie(From, To) = getImportedDecl( R"( @@ -1711,6 +1716,49 @@ TEST_P(ASTImporterTestBase, ObjectsWithUnnamedStructType) { EXPECT_NE(To0->getCanonicalDecl(), To1->getCanonicalDecl()); } +TEST_P(ASTImporterTestBase, ImportDoesUpdateUsedFlag) { + auto Pattern = varDecl(hasName("x")); + VarDecl *Imported1; + { + Decl *FromTU = getTuDecl("extern int x;", Lang_CXX, "input0.cc"); + auto *FromD = FirstDeclMatcher<VarDecl>().match(FromTU, Pattern); + Imported1 = cast<VarDecl>(Import(FromD, Lang_CXX)); + } + VarDecl *Imported2; + { + Decl *FromTU = getTuDecl("int x;", Lang_CXX, "input1.cc"); + auto *FromD = FirstDeclMatcher<VarDecl>().match(FromTU, Pattern); + Imported2 = cast<VarDecl>(Import(FromD, Lang_CXX)); + } + EXPECT_EQ(Imported1->getCanonicalDecl(), Imported2->getCanonicalDecl()); + EXPECT_FALSE(Imported2->isUsed(false)); + { + Decl *FromTU = + getTuDecl("extern int x; int f() { return x; }", Lang_CXX, "input2.cc"); + auto *FromD = + FirstDeclMatcher<FunctionDecl>().match(FromTU, functionDecl()); + Import(FromD, Lang_CXX); + } + EXPECT_TRUE(Imported2->isUsed(false)); +} + +TEST_P(ASTImporterTestBase, ReimportWithUsedFlag) { + auto Pattern = varDecl(hasName("x")); + + Decl *FromTU = getTuDecl("int x;", Lang_CXX, "input0.cc"); + auto *FromD = FirstDeclMatcher<VarDecl>().match(FromTU, Pattern); + + auto *Imported1 = cast<VarDecl>(Import(FromD, Lang_CXX)); + + ASSERT_FALSE(Imported1->isUsed(false)); + + FromD->setIsUsed(); + auto *Imported2 = cast<VarDecl>(Import(FromD, Lang_CXX)); + + EXPECT_EQ(Imported1, Imported2); + EXPECT_TRUE(Imported2->isUsed(false)); +} + struct ImportFunctions : ASTImporterTestBase {}; TEST_P(ImportFunctions, @@ -2043,14 +2091,10 @@ TEST_P(ImportFriendFunctions, EXPECT_EQ(ToFD->getPreviousDecl(), ImportedD); } -// This test is disabled, because ATM we create a redundant FunctionDecl. We -// start the import with the definition of `f` then we continue with the import -// of the type of `f` which involves `X`. During the import of `X` we start -// again the import of the definition of `f` and then finally we create the -// node. But then in the first frame of `VisitFunctionDecl` we create a node -// again since we do not check if such a node exists yet or not. This is being -// fixed in a separate patch: https://reviews.llvm.org/D47632 -// FIXME enable this test once the above patch is approved. +// Disabled temporarily, because the new structural equivalence check +// (https://reviews.llvm.org/D48628) breaks it. +// PreviousDecl is not set because there is no structural match. +// FIXME Enable! TEST_P(ImportFriendFunctions, DISABLED_ImportFriendFunctionRedeclChainDefWithClass) { auto Pattern = functionDecl(hasName("f")); @@ -2080,16 +2124,12 @@ TEST_P(ImportFriendFunctions, (*ImportedD->param_begin())->getOriginalType()); } -// This test is disabled, because ATM we create a redundant FunctionDecl. We -// start the import with the definition of `f` then we continue with the import -// of the type of `f` which involves `X`. During the import of `X` we start -// again the import of the definition of `f` and then finally we create the -// node. But then in the first frame of `VisitFunctionDecl` we create a node -// again since we do not check if such a node exists yet or not. This is being -// fixed in a separate patch: https://reviews.llvm.org/D47632 -// FIXME enable this test once the above patch is approved. +// Disabled temporarily, because the new structural equivalence check +// (https://reviews.llvm.org/D48628) breaks it. +// PreviousDecl is not set because there is no structural match. +// FIXME Enable! TEST_P(ImportFriendFunctions, - DISABLED_ImportFriendFunctionRedeclChainDefWithClass_ImportTheProto) { + DISABLED_ImportFriendFunctionRedeclChainDefWithClass_ImportTheProto) { auto Pattern = functionDecl(hasName("f")); Decl *FromTU = getTuDecl( diff --git a/unittests/AST/StructuralEquivalenceTest.cpp b/unittests/AST/StructuralEquivalenceTest.cpp index df37b72263..551986fc1c 100644 --- a/unittests/AST/StructuralEquivalenceTest.cpp +++ b/unittests/AST/StructuralEquivalenceTest.cpp @@ -64,8 +64,9 @@ struct StructuralEquivalenceTest : ::testing::Test { bool testStructuralMatch(NamedDecl *D0, NamedDecl *D1) { llvm::DenseSet<std::pair<Decl *, Decl *>> NonEquivalentDecls; - StructuralEquivalenceContext Ctx(D0->getASTContext(), D1->getASTContext(), - NonEquivalentDecls, false, false); + StructuralEquivalenceContext Ctx( + D0->getASTContext(), D1->getASTContext(), NonEquivalentDecls, + StructuralEquivalenceKind::Default, false, false); return Ctx.IsStructurallyEquivalent(D0, D1); } |