Index: include/clang/AST/ASTImporter.h =================================================================== --- include/clang/AST/ASTImporter.h +++ include/clang/AST/ASTImporter.h @@ -32,6 +32,7 @@ namespace clang { class ASTContext; +class Attr; class ASTImporterLookupTable; class CXXBaseSpecifier; class CXXCtorInitializer; @@ -42,8 +43,8 @@ class NamedDecl; class Stmt; class TagDecl; +class TranslationUnitDecl; class TypeSourceInfo; -class Attr; class ImportError : public llvm::ErrorInfo { public: @@ -115,6 +116,10 @@ /// context to the corresponding declarations in the "to" context. llvm::DenseMap ImportedDecls; + /// Mapping from the already-imported declarations in the "to" + /// context to the corresponding declarations in the "from" context. + llvm::DenseMap ImportedFromDecls; + /// Mapping from the already-imported statements in the "from" /// context to the corresponding statements in the "to" context. llvm::DenseMap ImportedStmts; @@ -226,9 +231,13 @@ /// Return the copy of the given declaration in the "to" context if /// it has already been imported from the "from" context. Otherwise return - /// NULL. + /// nullptr. Decl *GetAlreadyImportedOrNull(const Decl *FromD) const; + /// Return the translation unit from where the declaration was + /// imported. If it does not exist nullptr is returned. + TranslationUnitDecl *GetFromTU(Decl *ToD); + /// Import the given declaration context from the "from" /// AST context into the "to" AST context. /// Index: lib/AST/ASTImporter.cpp =================================================================== --- lib/AST/ASTImporter.cpp +++ lib/AST/ASTImporter.cpp @@ -437,6 +437,9 @@ Error ImportTemplateInformation(FunctionDecl *FromFD, FunctionDecl *ToFD); + template + bool hasSameVisibilityContext(T *Found, T *From); + bool IsStructuralMatch(Decl *From, Decl *To, bool Complain); bool IsStructuralMatch(RecordDecl *FromRecord, RecordDecl *ToRecord, bool Complain = true); @@ -2944,6 +2947,20 @@ return FoundSpec; } +template +bool ASTNodeImporter::hasSameVisibilityContext(T *Found, T *From) { + if (From->hasExternalFormalLinkage()) + return Found->hasExternalFormalLinkage(); + else if (Importer.GetFromTU(Found) == From->getTranslationUnitDecl()) { + if (From->isInAnonymousNamespace()) + return Found->isInAnonymousNamespace(); + else + return !Found->isInAnonymousNamespace() && + !Found->hasExternalFormalLinkage(); + } + return false; +} + ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { SmallVector Redecls = getCanonicalForwardRedeclChain(D); @@ -2997,33 +3014,30 @@ continue; if (auto *FoundFunction = dyn_cast(FoundDecl)) { - if (FoundFunction->hasExternalFormalLinkage() && - D->hasExternalFormalLinkage()) { - if (IsStructuralMatch(D, FoundFunction)) { - const FunctionDecl *Definition = nullptr; - if (D->doesThisDeclarationHaveABody() && - FoundFunction->hasBody(Definition)) { - return Importer.MapImported( - D, const_cast(Definition)); - } - FoundByLookup = FoundFunction; - break; - } - - // FIXME: Check for overloading more carefully, e.g., by boosting - // Sema::IsOverload out to the AST library. + if (!hasSameVisibilityContext(FoundFunction, D)) + continue; + + if (IsStructuralMatch(D, FoundFunction)) { + const FunctionDecl *Definition = nullptr; + if (D->doesThisDeclarationHaveABody() && + FoundFunction->hasBody(Definition)) + return Importer.MapImported(D, + const_cast(Definition)); + FoundByLookup = FoundFunction; + break; + } + // FIXME: Check for overloading more carefully, e.g., by boosting + // Sema::IsOverload out to the AST library. - // Function overloading is okay in C++. - if (Importer.getToContext().getLangOpts().CPlusPlus) - continue; + // Function overloading is okay in C++. + if (Importer.getToContext().getLangOpts().CPlusPlus) + continue; - // Complain about inconsistent function types. - Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent) + // Complain about inconsistent function types. + Importer.ToDiag(Loc, diag::err_odr_function_type_inconsistent) << Name << D->getType() << FoundFunction->getType(); - Importer.ToDiag(FoundFunction->getLocation(), - diag::note_odr_value_here) + Importer.ToDiag(FoundFunction->getLocation(), diag::note_odr_value_here) << FoundFunction->getType(); - } } ConflictingDecls.push_back(FoundDecl); @@ -3579,58 +3593,56 @@ continue; if (auto *FoundVar = dyn_cast(FoundDecl)) { - // We have found a variable that we may need to merge with. Check it. - if (FoundVar->hasExternalFormalLinkage() && - D->hasExternalFormalLinkage()) { - if (Importer.IsStructurallyEquivalent(D->getType(), - FoundVar->getType())) { - - // The VarDecl in the "From" context has a definition, but in the - // "To" context we already have a definition. - VarDecl *FoundDef = FoundVar->getDefinition(); - if (D->isThisDeclarationADefinition() && FoundDef) - // FIXME Check for ODR error if the two definitions have - // different initializers? - return Importer.MapImported(D, FoundDef); - - // The VarDecl in the "From" context has an initializer, but in the - // "To" context we already have an initializer. - const VarDecl *FoundDInit = nullptr; - if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit)) - // FIXME Diagnose ODR error if the two initializers are different? - return Importer.MapImported(D, const_cast(FoundDInit)); + if (!hasSameVisibilityContext(FoundVar, D)) + continue; + if (Importer.IsStructurallyEquivalent(D->getType(), + FoundVar->getType())) { + + // The VarDecl in the "From" context has a definition, but in the + // "To" context we already have a definition. + VarDecl *FoundDef = FoundVar->getDefinition(); + if (D->isThisDeclarationADefinition() && FoundDef) + // FIXME Check for ODR error if the two definitions have + // different initializers? + return Importer.MapImported(D, FoundDef); + + // The VarDecl in the "From" context has an initializer, but in the + // "To" context we already have an initializer. + const VarDecl *FoundDInit = nullptr; + if (D->getInit() && FoundVar->getAnyInitializer(FoundDInit)) + // FIXME Diagnose ODR error if the two initializers are different? + return Importer.MapImported(D, const_cast(FoundDInit)); + + FoundByLookup = FoundVar; + break; + } + const ArrayType *FoundArray + = Importer.getToContext().getAsArrayType(FoundVar->getType()); + const ArrayType *TArray + = Importer.getToContext().getAsArrayType(D->getType()); + if (FoundArray && TArray) { + if (isa(FoundArray) && + isa(TArray)) { + // Import the type. + QualType T = Importer.Import(D->getType()); + if (T.isNull()) + return nullptr; + + FoundVar->setType(T); + FoundByLookup = FoundVar; + break; + } else if (isa(TArray) && + isa(FoundArray)) { FoundByLookup = FoundVar; break; } - - const ArrayType *FoundArray - = Importer.getToContext().getAsArrayType(FoundVar->getType()); - const ArrayType *TArray - = Importer.getToContext().getAsArrayType(D->getType()); - if (FoundArray && TArray) { - if (isa(FoundArray) && - isa(TArray)) { - // Import the type. - if (auto TyOrErr = import(D->getType())) - FoundVar->setType(*TyOrErr); - else - return TyOrErr.takeError(); - - FoundByLookup = FoundVar; - break; - } else if (isa(TArray) && - isa(FoundArray)) { - FoundByLookup = FoundVar; - break; - } - } - - Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent) - << Name << D->getType() << FoundVar->getType(); - Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) - << FoundVar->getType(); } + + Importer.ToDiag(Loc, diag::err_odr_variable_type_inconsistent) + << Name << D->getType() << FoundVar->getType(); + Importer.ToDiag(FoundVar->getLocation(), diag::note_odr_value_here) + << FoundVar->getType(); } ConflictingDecls.push_back(FoundDecl); @@ -7761,6 +7773,13 @@ return nullptr; } +TranslationUnitDecl *ASTImporter::GetFromTU(Decl *ToD) { + auto FromDPos = ImportedFromDecls.find(ToD); + if (FromDPos == ImportedFromDecls.end()) + return nullptr; + return FromDPos->second->getTranslationUnitDecl(); +} + Expected ASTImporter::Import_New(Decl *FromD) { Decl *ToD = Import(FromD); if (!ToD && FromD) @@ -8511,6 +8530,9 @@ if (Pos != ImportedDecls.end()) return Pos->second; ImportedDecls[From] = To; + // This mapping should be maintained only in this function. Therefore do not + // check for additional consistency. + ImportedFromDecls[To] = From; return To; } Index: unittests/AST/ASTImporterTest.cpp =================================================================== --- unittests/AST/ASTImporterTest.cpp +++ unittests/AST/ASTImporterTest.cpp @@ -10,6 +10,10 @@ // //===----------------------------------------------------------------------===// +// Define this to have ::testing::Combine available. +// FIXME: Better solution for this? +#define GTEST_HAS_COMBINE 1 + #include "clang/AST/ASTImporter.h" #include "MatchVerifier.h" #include "clang/AST/ASTContext.h" @@ -58,23 +62,31 @@ // Common base for the different families of ASTImporter tests that are // parameterized on the compiler options which may result a different AST. E.g. // -fms-compatibility or -fdelayed-template-parsing. -struct ParameterizedTestsFixture : ::testing::TestWithParam { +class CompilerOptionSpecificTest : public ::testing::Test { +protected: + // Return the extra arguments appended to runtime options at compilation. + virtual ArgVector getExtraArgs() const { return ArgVector(); } // Returns the argument vector used for a specific language option, this set // can be tweaked by the test parameters. ArgVector getArgVectorForLanguage(Language Lang) const { ArgVector Args = getBasicRunOptionsForLanguage(Lang); - ArgVector ExtraArgs = GetParam(); + ArgVector ExtraArgs = getExtraArgs(); for (const auto &Arg : ExtraArgs) { Args.push_back(Arg); } return Args; } - }; +auto DefaultTestValuesForRunOptions = ::testing::Values( + ArgVector(), ArgVector{"-fdelayed-template-parsing"}, + ArgVector{"-fms-compatibility"}, + ArgVector{"-fdelayed-template-parsing", "-fms-compatibility"}); + // Base class for those tests which use the family of `testImport` functions. -class TestImportBase : public ParameterizedTestsFixture { +class TestImportBase : public CompilerOptionSpecificTest, + public ::testing::WithParamInterface { template NodeType importNode(ASTUnit *From, ASTUnit *To, ASTImporter &Importer, @@ -159,6 +171,9 @@ VerificationMatcher); } +protected: + ArgVector getExtraArgs() const override { return GetParam(); } + public: /// Test how AST node named "declToImport" located in the translation unit @@ -284,7 +299,7 @@ // This class provides generic methods to write tests which can check internal // attributes of AST nodes like getPreviousDecl(), isVirtual(), etc. Also, // this fixture makes it possible to import from several "From" contexts. -class ASTImporterTestBase : public ParameterizedTestsFixture { +class ASTImporterTestBase : public CompilerOptionSpecificTest { const char *const InputFileName = "input.cc"; const char *const OutputFileName = "output.cc"; @@ -450,6 +465,10 @@ return FromTU->import(*LookupTablePtr, ToAST.get(), From); } + template DeclT *Import(DeclT *From, Language Lang) { + return cast_or_null(Import(cast(From), Lang)); + } + QualType ImportType(QualType FromType, Decl *TUDecl, Language ToLang) { lazyInitToAST(ToLang, "", OutputFileName); TU *FromTU = findFromTU(TUDecl); @@ -473,11 +492,18 @@ } }; +class ASTImporterOptionSpecificTestBase + : public ASTImporterTestBase, + public ::testing::WithParamInterface { +protected: + ArgVector getExtraArgs() const override { return GetParam(); } +}; + struct ImportExpr : TestImportBase {}; struct ImportType : TestImportBase {}; struct ImportDecl : TestImportBase {}; -struct CanonicalRedeclChain : ASTImporterTestBase {}; +struct CanonicalRedeclChain : ASTImporterOptionSpecificTestBase {}; TEST_P(CanonicalRedeclChain, ShouldBeConsequentWithMatchers) { Decl *FromTU = getTuDecl("void f();", Lang_CXX); @@ -1000,7 +1026,7 @@ has(declStmt(hasSingleDecl(varDecl(hasName("d"))))))))); } -TEST_P(ASTImporterTestBase, ImportRecordTypeInFunc) { +TEST_P(ASTImporterOptionSpecificTestBase, ImportRecordTypeInFunc) { Decl *FromTU = getTuDecl("int declToImport() { " " struct data_t {int a;int b;};" " struct data_t d;" @@ -1015,7 +1041,7 @@ EXPECT_FALSE(ToType.isNull()); } -TEST_P(ASTImporterTestBase, ImportRecordDeclInFuncParams) { +TEST_P(ASTImporterOptionSpecificTestBase, ImportRecordDeclInFuncParams) { // This construct is not supported by ASTImporter. Decl *FromTU = getTuDecl( "int declToImport(struct data_t{int a;int b;} ***d){ return 0; }", @@ -1027,7 +1053,7 @@ EXPECT_EQ(To, nullptr); } -TEST_P(ASTImporterTestBase, ImportRecordDeclInFuncFromMacro) { +TEST_P(ASTImporterOptionSpecificTestBase, ImportRecordDeclInFuncFromMacro) { Decl *FromTU = getTuDecl( "#define NONAME_SIZEOF(type) sizeof(struct{type *dummy;}) \n" "int declToImport(){ return NONAME_SIZEOF(int); }", @@ -1042,7 +1068,8 @@ hasDescendant(unaryExprOrTypeTraitExpr())))); } -TEST_P(ASTImporterTestBase, ImportRecordDeclInFuncParamsFromMacro) { +TEST_P(ASTImporterOptionSpecificTestBase, + ImportRecordDeclInFuncParamsFromMacro) { // This construct is not supported by ASTImporter. Decl *FromTU = getTuDecl( "#define PAIR_STRUCT(type) struct data_t{type a;type b;} \n" @@ -1194,7 +1221,8 @@ has(fieldDecl(hasType(dependentSizedArrayType()))))))); } -TEST_P(ASTImporterTestBase, ImportOfTemplatedDeclOfClassTemplateDecl) { +TEST_P(ASTImporterOptionSpecificTestBase, + ImportOfTemplatedDeclOfClassTemplateDecl) { Decl *FromTU = getTuDecl("template struct S{};", Lang_CXX); auto From = FirstDeclMatcher().match(FromTU, classTemplateDecl()); @@ -1207,7 +1235,8 @@ EXPECT_EQ(ToTemplated1, ToTemplated); } -TEST_P(ASTImporterTestBase, ImportOfTemplatedDeclOfFunctionTemplateDecl) { +TEST_P(ASTImporterOptionSpecificTestBase, + ImportOfTemplatedDeclOfFunctionTemplateDecl) { Decl *FromTU = getTuDecl("template void f(){}", Lang_CXX); auto From = FirstDeclMatcher().match( FromTU, functionTemplateDecl()); @@ -1220,7 +1249,7 @@ EXPECT_EQ(ToTemplated1, ToTemplated); } -TEST_P(ASTImporterTestBase, +TEST_P(ASTImporterOptionSpecificTestBase, ImportOfTemplatedDeclShouldImportTheClassTemplateDecl) { Decl *FromTU = getTuDecl("template struct S{};", Lang_CXX); auto FromFT = @@ -1236,7 +1265,7 @@ EXPECT_TRUE(ToFT); } -TEST_P(ASTImporterTestBase, +TEST_P(ASTImporterOptionSpecificTestBase, ImportOfTemplatedDeclShouldImportTheFunctionTemplateDecl) { Decl *FromTU = getTuDecl("template void f(){}", Lang_CXX); auto FromFT = FirstDeclMatcher().match( @@ -1252,7 +1281,7 @@ EXPECT_TRUE(ToFT); } -TEST_P(ASTImporterTestBase, ImportCorrectTemplatedDecl) { +TEST_P(ASTImporterOptionSpecificTestBase, ImportCorrectTemplatedDecl) { auto Code = R"( namespace x { @@ -1283,7 +1312,8 @@ ASSERT_EQ(ToTemplated1, ToTemplated); } -TEST_P(ASTImporterTestBase, ImportFunctionWithBackReferringParameter) { +TEST_P(ASTImporterOptionSpecificTestBase, + ImportFunctionWithBackReferringParameter) { Decl *From, *To; std::tie(From, To) = getImportedDecl( R"( @@ -1310,7 +1340,7 @@ EXPECT_TRUE(Verifier.match(To, Matcher)); } -TEST_P(ASTImporterTestBase, +TEST_P(ASTImporterOptionSpecificTestBase, TUshouldNotContainTemplatedDeclOfFunctionTemplates) { Decl *From, *To; std::tie(From, To) = @@ -1336,7 +1366,8 @@ EXPECT_TRUE(Check(To)); } -TEST_P(ASTImporterTestBase, TUshouldNotContainTemplatedDeclOfClassTemplates) { +TEST_P(ASTImporterOptionSpecificTestBase, + TUshouldNotContainTemplatedDeclOfClassTemplates) { Decl *From, *To; std::tie(From, To) = getImportedDecl("template struct declToImport { T t; };" @@ -1361,7 +1392,8 @@ EXPECT_TRUE(Check(To)); } -TEST_P(ASTImporterTestBase, TUshouldNotContainTemplatedDeclOfTypeAlias) { +TEST_P(ASTImporterOptionSpecificTestBase, + TUshouldNotContainTemplatedDeclOfTypeAlias) { Decl *From, *To; std::tie(From, To) = getImportedDecl( @@ -1388,9 +1420,8 @@ EXPECT_TRUE(Check(To)); } -TEST_P( - ASTImporterTestBase, - TUshouldNotContainClassTemplateSpecializationOfImplicitInstantiation) { +TEST_P(ASTImporterOptionSpecificTestBase, + TUshouldNotContainClassTemplateSpecializationOfImplicitInstantiation) { Decl *From, *To; std::tie(From, To) = getImportedDecl( @@ -1431,7 +1462,7 @@ return Index == Order.size(); } -TEST_P(ASTImporterTestBase, +TEST_P(ASTImporterOptionSpecificTestBase, TUshouldContainClassTemplateSpecializationOfExplicitInstantiation) { Decl *From, *To; std::tie(From, To) = getImportedDecl( @@ -1458,7 +1489,8 @@ EXPECT_TRUE(MatchVerifier{}.match(To, Pattern)); } -TEST_P(ASTImporterTestBase, CXXRecordDeclFieldsShouldBeInCorrectOrder) { +TEST_P(ASTImporterOptionSpecificTestBase, + CXXRecordDeclFieldsShouldBeInCorrectOrder) { Decl *From, *To; std::tie(From, To) = getImportedDecl( @@ -1470,7 +1502,7 @@ EXPECT_TRUE(Verifier.match(To, cxxRecordDecl(hasFieldOrder({"a", "b"})))); } -TEST_P(ASTImporterTestBase, +TEST_P(ASTImporterOptionSpecificTestBase, DISABLED_CXXRecordDeclFieldOrderShouldNotDependOnImportOrder) { Decl *From, *To; std::tie(From, To) = getImportedDecl( @@ -1492,7 +1524,7 @@ Verifier.match(To, cxxRecordDecl(hasFieldOrder({"a", "b", "c"})))); } -TEST_P(ASTImporterTestBase, ShouldImportImplicitCXXRecordDecl) { +TEST_P(ASTImporterOptionSpecificTestBase, ShouldImportImplicitCXXRecordDecl) { Decl *From, *To; std::tie(From, To) = getImportedDecl( R"( @@ -1508,7 +1540,8 @@ EXPECT_TRUE(Verifier.match(To, Matcher)); } -TEST_P(ASTImporterTestBase, ShouldImportImplicitCXXRecordDeclOfClassTemplate) { +TEST_P(ASTImporterOptionSpecificTestBase, + ShouldImportImplicitCXXRecordDeclOfClassTemplate) { Decl *From, *To; std::tie(From, To) = getImportedDecl( R"( @@ -1525,9 +1558,8 @@ EXPECT_TRUE(Verifier.match(To, Matcher)); } -TEST_P( - ASTImporterTestBase, - ShouldImportImplicitCXXRecordDeclOfClassTemplateSpecializationDecl) { +TEST_P(ASTImporterOptionSpecificTestBase, + ShouldImportImplicitCXXRecordDeclOfClassTemplateSpecializationDecl) { Decl *From, *To; std::tie(From, To) = getImportedDecl( R"( @@ -1547,7 +1579,7 @@ MatchVerifier{}.match(To->getTranslationUnitDecl(), Pattern)); } -TEST_P(ASTImporterTestBase, IDNSOrdinary) { +TEST_P(ASTImporterOptionSpecificTestBase, IDNSOrdinary) { Decl *From, *To; std::tie(From, To) = getImportedDecl("void declToImport() {}", Lang_CXX, "", Lang_CXX); @@ -1559,7 +1591,7 @@ EXPECT_EQ(From->getIdentifierNamespace(), To->getIdentifierNamespace()); } -TEST_P(ASTImporterTestBase, IDNSOfNonmemberOperator) { +TEST_P(ASTImporterOptionSpecificTestBase, IDNSOfNonmemberOperator) { Decl *FromTU = getTuDecl( R"( struct X {}; @@ -1571,7 +1603,7 @@ EXPECT_EQ(From->getIdentifierNamespace(), To->getIdentifierNamespace()); } -TEST_P(ASTImporterTestBase, +TEST_P(ASTImporterOptionSpecificTestBase, ShouldImportMembersOfClassTemplateSpecializationDecl) { Decl *From, *To; std::tie(From, To) = getImportedDecl( @@ -1591,7 +1623,8 @@ MatchVerifier{}.match(To->getTranslationUnitDecl(), Pattern)); } -TEST_P(ASTImporterTestBase, ImportDefinitionOfClassTemplateAfterFwdDecl) { +TEST_P(ASTImporterOptionSpecificTestBase, + ImportDefinitionOfClassTemplateAfterFwdDecl) { { Decl *FromTU = getTuDecl( R"( @@ -1624,7 +1657,7 @@ } } -TEST_P(ASTImporterTestBase, +TEST_P(ASTImporterOptionSpecificTestBase, ImportDefinitionOfClassTemplateIfThereIsAnExistingFwdDeclAndDefinition) { Decl *ToTU = getToTuDecl( R"( @@ -1664,7 +1697,7 @@ .match(ToTU, classTemplateDecl())); } -TEST_P(ASTImporterTestBase, +TEST_P(ASTImporterOptionSpecificTestBase, ImportDefinitionOfClassIfThereIsAnExistingFwdDeclAndDefinition) { Decl *ToTU = getToTuDecl( R"( @@ -1707,7 +1740,7 @@ CompareSourceLocs(FullSourceLoc{ Range1.getEnd(), SM1 }, FullSourceLoc{ Range2.getEnd(), SM2 }); } -TEST_P(ASTImporterTestBase, ImportSourceLocs) { +TEST_P(ASTImporterOptionSpecificTestBase, ImportSourceLocs) { Decl *FromTU = getTuDecl( R"( #define MFOO(arg) arg = arg + 1 @@ -1737,7 +1770,7 @@ FromSM); } -TEST_P(ASTImporterTestBase, ImportNestedMacro) { +TEST_P(ASTImporterOptionSpecificTestBase, ImportNestedMacro) { Decl *FromTU = getTuDecl( R"( #define FUNC_INT void declToImport @@ -1755,9 +1788,8 @@ } TEST_P( - ASTImporterTestBase, - ImportDefinitionOfClassTemplateSpecIfThereIsAnExistingFwdDeclAndDefinition) -{ + ASTImporterOptionSpecificTestBase, + ImportDefinitionOfClassTemplateSpecIfThereIsAnExistingFwdDeclAndDefinition) { Decl *ToTU = getToTuDecl( R"( template @@ -1799,7 +1831,7 @@ .match(ToTU, classTemplateSpecializationDecl())); } -TEST_P(ASTImporterTestBase, ObjectsWithUnnamedStructType) { +TEST_P(ASTImporterOptionSpecificTestBase, ObjectsWithUnnamedStructType) { Decl *FromTU = getTuDecl( R"( struct { int a; int b; } object0 = { 2, 3 }; @@ -1823,7 +1855,7 @@ EXPECT_NE(To0->getCanonicalDecl(), To1->getCanonicalDecl()); } -TEST_P(ASTImporterTestBase, AnonymousRecords) { +TEST_P(ASTImporterOptionSpecificTestBase, AnonymousRecords) { auto *Code = R"( struct X { @@ -1849,7 +1881,7 @@ DeclCounter().match(ToTU, recordDecl(hasName("X")))); } -TEST_P(ASTImporterTestBase, AnonymousRecordsReversed) { +TEST_P(ASTImporterOptionSpecificTestBase, AnonymousRecordsReversed) { Decl *FromTU0 = getTuDecl( R"( struct X { @@ -1882,7 +1914,7 @@ DeclCounter().match(ToTU, recordDecl(hasName("X")))); } -TEST_P(ASTImporterTestBase, ImportDoesUpdateUsedFlag) { +TEST_P(ASTImporterOptionSpecificTestBase, ImportDoesUpdateUsedFlag) { auto Pattern = varDecl(hasName("x")); VarDecl *Imported1; { @@ -1908,7 +1940,7 @@ EXPECT_TRUE(Imported2->isUsed(false)); } -TEST_P(ASTImporterTestBase, ImportDoesUpdateUsedFlag2) { +TEST_P(ASTImporterOptionSpecificTestBase, ImportDoesUpdateUsedFlag2) { auto Pattern = varDecl(hasName("x")); VarDecl *ExistingD; { @@ -1926,7 +1958,7 @@ EXPECT_TRUE(ExistingD->isUsed(false)); } -TEST_P(ASTImporterTestBase, ImportDoesUpdateUsedFlag3) { +TEST_P(ASTImporterOptionSpecificTestBase, ImportDoesUpdateUsedFlag3) { auto Pattern = varDecl(hasName("a")); VarDecl *ExistingD; { @@ -1957,7 +1989,7 @@ EXPECT_TRUE(ExistingD->isUsed(false)); } -TEST_P(ASTImporterTestBase, ReimportWithUsedFlag) { +TEST_P(ASTImporterOptionSpecificTestBase, ReimportWithUsedFlag) { auto Pattern = varDecl(hasName("x")); Decl *FromTU = getTuDecl("int x;", Lang_CXX, "input0.cc"); @@ -1974,7 +2006,7 @@ EXPECT_TRUE(Imported2->isUsed(false)); } -struct ImportFunctions : ASTImporterTestBase {}; +struct ImportFunctions : ASTImporterOptionSpecificTestBase {}; TEST_P(ImportFunctions, DefinitionShouldBeImportedAsDefintionWhenThereIsAPrototype) { @@ -2232,6 +2264,267 @@ }).match(ToTU, functionDecl())); } +//FIXME Move these tests to a separate test file. +namespace TypeAndValueParameterizedTests { + +// Type parameters for type-parameterized test fixtures. +struct GetFunPattern { + using DeclTy = FunctionDecl; + BindableMatcher operator()() { return functionDecl(hasName("f")); } +}; +struct GetVarPattern { + using DeclTy = VarDecl; + BindableMatcher operator()() { return varDecl(hasName("v")); } +}; + +// Values for the value-parameterized test fixtures. +// FunctionDecl: +auto *ExternF = "void f();"; +auto *StaticF = "static void f();"; +auto *AnonF = "namespace { void f(); }"; +// VarDecl: +auto *ExternV = "extern int v;"; +auto *StaticV = "static int v;"; +auto *AnonV = "namespace { extern int v; }"; + +// First value in tuple: Compile options. +// Second value in tuple: Source code to be used in the test. +using ImportVisibilityChainParams = + ::testing::WithParamInterface>; +// Fixture to test the redecl chain of Decls with the same visibility. Gtest +// makes it possible to have either value-parameterized or type-parameterized +// fixtures. However, we cannot have both value- and type-parameterized test +// fixtures. This is a value-parameterized test fixture in the gtest sense. We +// intend to mimic gtest's type-parameters via the PatternFactory template +// parameter. We manually instantiate the different tests with the each types. +template +class ImportVisibilityChain + : public ASTImporterTestBase, public ImportVisibilityChainParams { +protected: + using DeclTy = typename PatternFactory::DeclTy; + ArgVector getExtraArgs() const override { return std::get<0>(GetParam()); } + std::string getCode() const { return std::get<1>(GetParam()); } + BindableMatcher getPattern() const { return PatternFactory()(); } + + // Type-parameterized test. + void TypedTest_ImportChain() { + std::string Code = getCode() + getCode(); + auto Pattern = getPattern(); + + TranslationUnitDecl *FromTu = getTuDecl(Code, Lang_CXX, "input0.cc"); + + auto *FromF0 = FirstDeclMatcher().match(FromTu, Pattern); + auto *FromF1 = LastDeclMatcher().match(FromTu, Pattern); + + auto *ToF0 = Import(FromF0, Lang_CXX); + auto *ToF1 = Import(FromF1, Lang_CXX); + + EXPECT_TRUE(ToF0); + ASSERT_TRUE(ToF1); + EXPECT_NE(ToF0, ToF1); + EXPECT_EQ(ToF1->getPreviousDecl(), ToF0); + } +}; + +// Manual instantiation of the fixture with each type. +using ImportFunctionsVisibilityChain = ImportVisibilityChain; +using ImportVariablesVisibilityChain = ImportVisibilityChain; +// Value-parameterized test for the first type. +TEST_P(ImportFunctionsVisibilityChain, ImportChain) { + TypedTest_ImportChain(); +} +// Value-parameterized test for the second type. +TEST_P(ImportVariablesVisibilityChain, ImportChain) { + TypedTest_ImportChain(); +} + +// Automatic instantiation of the value-parameterized tests. +INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportFunctionsVisibilityChain, + ::testing::Combine( + DefaultTestValuesForRunOptions, + ::testing::Values(ExternF, StaticF, AnonF)), ); +INSTANTIATE_TEST_CASE_P( + ParameterizedTests, ImportVariablesVisibilityChain, + ::testing::Combine( + DefaultTestValuesForRunOptions, + // There is no point to instantiate with StaticV, because in C++ we can + // forward declare a variable only with the 'extern' keyword. + // Consequently, each fwd declared variable has external linkage. This + // is different in the C language where any declaration without an + // initializer is a tentative definition, subsequent definitions may be + // provided but they must have the same linkage. See also the test + // ImportVariableChainInC which test for this special C Lang case. + ::testing::Values(ExternV, AnonV)), ); + +// First value in tuple: Compile options. +// Second value in tuple: Tuple with informations for the test. +// Code for first import (or initial code), code to import, whether the `f` +// functions are expected to be linked in a declaration chain. +// One value of this tuple is combined with every value of compile options. +// The test can have a single tuple as parameter only. +using ImportVisibilityParams = ::testing::WithParamInterface< + std::tuple>>; + +template +class ImportVisibility + : public ASTImporterTestBase, + public ImportVisibilityParams { +protected: + using DeclTy = typename PatternFactory::DeclTy; + ArgVector getExtraArgs() const override { return std::get<0>(GetParam()); } + std::string getCode0() const { return std::get<0>(std::get<1>(GetParam())); } + std::string getCode1() const { return std::get<1>(std::get<1>(GetParam())); } + bool shouldBeLinked() const { return std::get<2>(std::get<1>(GetParam())); } + BindableMatcher getPattern() const { return PatternFactory()(); } + + void TypedTest_ImportAfter() { + TranslationUnitDecl *ToTu = getToTuDecl(getCode0(), Lang_CXX); + TranslationUnitDecl *FromTu = getTuDecl(getCode1(), Lang_CXX, "input1.cc"); + + auto *ToF0 = FirstDeclMatcher().match(ToTu, getPattern()); + auto *FromF1 = FirstDeclMatcher().match(FromTu, getPattern()); + + auto *ToF1 = Import(FromF1, Lang_CXX); + + ASSERT_TRUE(ToF0); + ASSERT_TRUE(ToF1); + EXPECT_NE(ToF0, ToF1); + + if (shouldBeLinked()) + EXPECT_EQ(ToF1->getPreviousDecl(), ToF0); + else + EXPECT_FALSE(ToF1->getPreviousDecl()); + } + + void TypedTest_ImportAfterImport() { + TranslationUnitDecl *FromTu0 = getTuDecl(getCode0(), Lang_CXX, "input0.cc"); + TranslationUnitDecl *FromTu1 = getTuDecl(getCode1(), Lang_CXX, "input1.cc"); + auto *FromF0 = + FirstDeclMatcher().match(FromTu0, getPattern()); + auto *FromF1 = + FirstDeclMatcher().match(FromTu1, getPattern()); + auto *ToF0 = Import(FromF0, Lang_CXX); + auto *ToF1 = Import(FromF1, Lang_CXX); + ASSERT_TRUE(ToF0); + ASSERT_TRUE(ToF1); + EXPECT_NE(ToF0, ToF1); + if (shouldBeLinked()) + EXPECT_EQ(ToF1->getPreviousDecl(), ToF0); + else + EXPECT_FALSE(ToF1->getPreviousDecl()); + } +}; +using ImportFunctionsVisibility = ImportVisibility; +using ImportVariablesVisibility = ImportVisibility; + +// FunctionDecl. +TEST_P(ImportFunctionsVisibility, ImportAfter) { + TypedTest_ImportAfter(); +} +TEST_P(ImportFunctionsVisibility, ImportAfterImport) { + TypedTest_ImportAfterImport(); +} +// VarDecl. +TEST_P(ImportVariablesVisibility, ImportAfter) { + TypedTest_ImportAfter(); +} +TEST_P(ImportVariablesVisibility, ImportAfterImport) { + TypedTest_ImportAfterImport(); +} + +bool ExpectLink = true; +bool ExpectNotLink = false; + +INSTANTIATE_TEST_CASE_P( + ParameterizedTests, ImportFunctionsVisibility, + ::testing::Combine( + DefaultTestValuesForRunOptions, + ::testing::Values(std::make_tuple(ExternF, ExternF, ExpectLink), + std::make_tuple(ExternF, StaticF, ExpectNotLink), + std::make_tuple(ExternF, AnonF, ExpectNotLink), + std::make_tuple(StaticF, ExternF, ExpectNotLink), + std::make_tuple(StaticF, StaticF, ExpectNotLink), + std::make_tuple(StaticF, AnonF, ExpectNotLink), + std::make_tuple(AnonF, ExternF, ExpectNotLink), + std::make_tuple(AnonF, StaticF, ExpectNotLink), + std::make_tuple(AnonF, AnonF, ExpectNotLink))), ); +INSTANTIATE_TEST_CASE_P( + ParameterizedTests, ImportVariablesVisibility, + ::testing::Combine( + DefaultTestValuesForRunOptions, + ::testing::Values(std::make_tuple(ExternV, ExternV, ExpectLink), + std::make_tuple(ExternV, StaticV, ExpectNotLink), + std::make_tuple(ExternV, AnonV, ExpectNotLink), + std::make_tuple(StaticV, ExternV, ExpectNotLink), + std::make_tuple(StaticV, StaticV, ExpectNotLink), + std::make_tuple(StaticV, AnonV, ExpectNotLink), + std::make_tuple(AnonV, ExternV, ExpectNotLink), + std::make_tuple(AnonV, StaticV, ExpectNotLink), + std::make_tuple(AnonV, AnonV, ExpectNotLink))), ); + +} // namespace TypeAndValueParameterizedTests + +TEST_P(ASTImporterOptionSpecificTestBase, ImportVariableChainInC) { + std::string Code = "static int v; static int v = 0;"; + auto Pattern = varDecl(hasName("v")); + + TranslationUnitDecl *FromTu = getTuDecl(Code, Lang_C, "input0.c"); + + auto *From0 = FirstDeclMatcher().match(FromTu, Pattern); + auto *From1 = LastDeclMatcher().match(FromTu, Pattern); + + auto *To0 = Import(From0, Lang_C); + auto *To1 = Import(From1, Lang_C); + + EXPECT_TRUE(To0); + ASSERT_TRUE(To1); + EXPECT_NE(To0, To1); + EXPECT_EQ(To1->getPreviousDecl(), To0); +} + +TEST_P(ImportFunctions, ImportFromDifferentScopedAnonNamespace) { + TranslationUnitDecl *FromTu = getTuDecl( + "namespace NS0 { namespace { void f(); } }" + "namespace NS1 { namespace { void f(); } }", + Lang_CXX, "input0.cc"); + auto Pattern = functionDecl(hasName("f")); + + auto *FromF0 = FirstDeclMatcher().match(FromTu, Pattern); + auto *FromF1 = LastDeclMatcher().match(FromTu, Pattern); + + auto *ToF0 = Import(FromF0, Lang_CXX); + auto *ToF1 = Import(FromF1, Lang_CXX); + + EXPECT_TRUE(ToF0); + ASSERT_TRUE(ToF1); + EXPECT_NE(ToF0, ToF1); + EXPECT_FALSE(ToF1->getPreviousDecl()); +} + +TEST_P(ImportFunctions, ImportFunctionFromUnnamedNamespace) { + { + Decl *FromTU = getTuDecl("namespace { void f() {} } void g0() { f(); }", + Lang_CXX, "input0.cc"); + auto *FromD = FirstDeclMatcher().match( + FromTU, functionDecl(hasName("g0"))); + + Import(FromD, Lang_CXX); + } + { + Decl *FromTU = + getTuDecl("namespace { void f() { int a; } } void g1() { f(); }", + Lang_CXX, "input1.cc"); + auto *FromD = FirstDeclMatcher().match( + FromTU, functionDecl(hasName("g1"))); + Import(FromD, Lang_CXX); + } + + Decl *ToTU = ToAST->getASTContext().getTranslationUnitDecl(); + cast(ToTU)->dumpDeclContext(); + ASSERT_EQ(DeclCounter().match(ToTU, functionDecl(hasName("f"))), + 2u); +} + struct ImportFriendFunctions : ImportFunctions {}; TEST_P(ImportFriendFunctions, ImportFriendFunctionRedeclChainProto) { @@ -2700,7 +2993,7 @@ compoundStmt(has(callExpr(has(unresolvedMemberExpr()))))))))); } -class ImportImplicitMethods : public ASTImporterTestBase { +class ImportImplicitMethods : public ASTImporterOptionSpecificTestBase { public: static constexpr auto DefaultCode = R"( struct A { int x; }; @@ -2812,7 +3105,7 @@ testNoImportOf(cxxMethodDecl(hasName("f")), Code); } -TEST_P(ASTImporterTestBase, ImportOfEquivalentRecord) { +TEST_P(ASTImporterOptionSpecificTestBase, ImportOfEquivalentRecord) { Decl *ToR1; { Decl *FromTU = getTuDecl( @@ -2836,7 +3129,7 @@ EXPECT_EQ(ToR1, ToR2); } -TEST_P(ASTImporterTestBase, ImportOfNonEquivalentRecord) { +TEST_P(ASTImporterOptionSpecificTestBase, ImportOfNonEquivalentRecord) { Decl *ToR1; { Decl *FromTU = getTuDecl( @@ -2856,7 +3149,7 @@ EXPECT_NE(ToR1, ToR2); } -TEST_P(ASTImporterTestBase, ImportOfEquivalentField) { +TEST_P(ASTImporterOptionSpecificTestBase, ImportOfEquivalentField) { Decl *ToF1; { Decl *FromTU = getTuDecl( @@ -2876,7 +3169,7 @@ EXPECT_EQ(ToF1, ToF2); } -TEST_P(ASTImporterTestBase, ImportOfNonEquivalentField) { +TEST_P(ASTImporterOptionSpecificTestBase, ImportOfNonEquivalentField) { Decl *ToF1; { Decl *FromTU = getTuDecl( @@ -2896,7 +3189,7 @@ EXPECT_NE(ToF1, ToF2); } -TEST_P(ASTImporterTestBase, ImportOfEquivalentMethod) { +TEST_P(ASTImporterOptionSpecificTestBase, ImportOfEquivalentMethod) { Decl *ToM1; { Decl *FromTU = getTuDecl( @@ -2916,7 +3209,7 @@ EXPECT_EQ(ToM1, ToM2); } -TEST_P(ASTImporterTestBase, ImportOfNonEquivalentMethod) { +TEST_P(ASTImporterOptionSpecificTestBase, ImportOfNonEquivalentMethod) { Decl *ToM1; { Decl *FromTU = getTuDecl( @@ -2938,7 +3231,8 @@ EXPECT_NE(ToM1, ToM2); } -TEST_P(ASTImporterTestBase, ImportUnnamedStructsWithRecursingField) { +TEST_P(ASTImporterOptionSpecificTestBase, + ImportUnnamedStructsWithRecursingField) { Decl *FromTU = getTuDecl( R"( struct A { @@ -2970,7 +3264,7 @@ R1, recordDecl(has(fieldDecl(hasName("next")))))); } -TEST_P(ASTImporterTestBase, ImportUnnamedFieldsInCorrectOrder) { +TEST_P(ASTImporterOptionSpecificTestBase, ImportUnnamedFieldsInCorrectOrder) { Decl *FromTU = getTuDecl( R"( void f(int X, int Y, bool Z) { @@ -3005,7 +3299,8 @@ EXPECT_EQ(FromIndex, 3u); } -TEST_P(ASTImporterTestBase, MergeFieldDeclsOfClassTemplateSpecialization) { +TEST_P(ASTImporterOptionSpecificTestBase, + MergeFieldDeclsOfClassTemplateSpecialization) { std::string ClassTemplate = R"( template @@ -3050,7 +3345,8 @@ EXPECT_TRUE(ToField->getInClassInitializer()); } -TEST_P(ASTImporterTestBase, MergeFunctionOfClassTemplateSpecialization) { +TEST_P(ASTImporterOptionSpecificTestBase, + MergeFunctionOfClassTemplateSpecialization) { std::string ClassTemplate = R"( template @@ -3091,7 +3387,7 @@ EXPECT_TRUE(ToFun->hasBody()); } -TEST_P(ASTImporterTestBase, +TEST_P(ASTImporterOptionSpecificTestBase, ODRViolationOfClassTemplateSpecializationsShouldBeReported) { std::string ClassTemplate = R"( @@ -3138,7 +3434,8 @@ ToTU, classTemplateSpecializationDecl())); } -TEST_P(ASTImporterTestBase, MergeCtorOfClassTemplateSpecialization) { +TEST_P(ASTImporterOptionSpecificTestBase, + MergeCtorOfClassTemplateSpecialization) { std::string ClassTemplate = R"( template @@ -3179,7 +3476,7 @@ EXPECT_TRUE(ToCtor->hasBody()); } -TEST_P(ASTImporterTestBase, +TEST_P(ASTImporterOptionSpecificTestBase, ClassTemplatePartialSpecializationsShouldNotBeDuplicated) { auto Code = R"( @@ -3206,7 +3503,8 @@ ToTU, classTemplatePartialSpecializationDecl())); } -TEST_P(ASTImporterTestBase, ClassTemplateSpecializationsShouldNotBeDuplicated) { +TEST_P(ASTImporterOptionSpecificTestBase, + ClassTemplateSpecializationsShouldNotBeDuplicated) { auto Code = R"( // primary template @@ -3230,7 +3528,8 @@ ToTU, classTemplateSpecializationDecl())); } -TEST_P(ASTImporterTestBase, ClassTemplateFullAndPartialSpecsShouldNotBeMixed) { +TEST_P(ASTImporterOptionSpecificTestBase, + ClassTemplateFullAndPartialSpecsShouldNotBeMixed) { std::string PrimaryTemplate = R"( template @@ -3262,7 +3561,8 @@ unless(classTemplatePartialSpecializationDecl())))); } -TEST_P(ASTImporterTestBase, InitListExprValueKindShouldBeImported) { +TEST_P(ASTImporterOptionSpecificTestBase, + InitListExprValueKindShouldBeImported) { Decl *TU = getTuDecl( R"( const int &init(); @@ -3281,7 +3581,7 @@ EXPECT_TRUE(ToInitExpr->isGLValue()); } -struct ImportVariables : ASTImporterTestBase {}; +struct ImportVariables : ASTImporterOptionSpecificTestBase {}; TEST_P(ImportVariables, ImportOfOneDeclBringsInTheWholeChain) { Decl *FromTU = getTuDecl( @@ -3369,7 +3669,7 @@ EXPECT_TRUE(ImportedD->getDefinition()); } -struct ImportClasses : ASTImporterTestBase {}; +struct ImportClasses : ASTImporterOptionSpecificTestBase {}; TEST_P(ImportClasses, PrototypeShouldBeImportedAsAPrototypeWhenThereIsNoDefinition) { @@ -3577,7 +3877,7 @@ EXPECT_EQ(ToDef->getPreviousDecl(), ToProto); } -struct ImportClassTemplates : ASTImporterTestBase {}; +struct ImportClassTemplates : ASTImporterOptionSpecificTestBase {}; TEST_P(ImportClassTemplates, PrototypeShouldBeImportedAsAPrototypeWhenThereIsNoDefinition) { @@ -3741,7 +4041,7 @@ ToProto->getTemplatedDecl()); } -struct ImportFriendClasses : ASTImporterTestBase {}; +struct ImportFriendClasses : ASTImporterOptionSpecificTestBase {}; TEST_P(ImportFriendClasses, ImportOfFriendRecordDoesNotMergeDefinition) { Decl *FromTU = getTuDecl( @@ -3981,7 +4281,7 @@ EXPECT_EQ(ImportedFwd, ImportedDef->getPreviousDecl()); } -TEST_P(ASTImporterTestBase, FriendFunInClassTemplate) { +TEST_P(ASTImporterOptionSpecificTestBase, FriendFunInClassTemplate) { auto *Code = R"( template struct X { @@ -3999,7 +4299,7 @@ EXPECT_EQ(ImportedFoo, ToFoo); } -struct DeclContextTest : ASTImporterTestBase {}; +struct DeclContextTest : ASTImporterOptionSpecificTestBase {}; TEST_P(DeclContextTest, removeDeclOfClassTemplateSpecialization) { Decl *TU = getTuDecl( @@ -4062,7 +4362,8 @@ EXPECT_FALSE(DC->containsDecl(A0)); } -struct ImportFunctionTemplateSpecializations : ASTImporterTestBase {}; +struct ImportFunctionTemplateSpecializations + : ASTImporterOptionSpecificTestBase {}; TEST_P(ImportFunctionTemplateSpecializations, TUshouldNotContainFunctionTemplateImplicitInstantiation) { @@ -4377,7 +4678,7 @@ EXPECT_EQ(To1->getPreviousDecl(), To0); } -TEST_P(ASTImporterTestBase, +TEST_P(ASTImporterOptionSpecificTestBase, ImportShouldNotReportFalseODRErrorWhenRecordIsBeingDefined) { { Decl *FromTU = getTuDecl( @@ -4416,7 +4717,8 @@ } } -TEST_P(ASTImporterTestBase, ImportingTypedefShouldImportTheCompleteType) { +TEST_P(ASTImporterOptionSpecificTestBase, + ImportingTypedefShouldImportTheCompleteType) { // We already have an incomplete underlying type in the "To" context. auto Code = R"( @@ -4448,7 +4750,7 @@ EXPECT_FALSE(ImportedD->getUnderlyingType()->isIncompleteType()); } -struct ASTImporterLookupTableTest : ASTImporterTestBase {}; +struct ASTImporterLookupTableTest : ASTImporterOptionSpecificTestBase {}; TEST_P(ASTImporterLookupTableTest, OneDecl) { auto *ToTU = getToTuDecl("int a;", Lang_CXX); @@ -4865,12 +5167,6 @@ ParameterizedTests, CanonicalRedeclChain, ::testing::Values(ArgVector()),); -auto DefaultTestValuesForRunOptions = ::testing::Values( - ArgVector(), - ArgVector{"-fdelayed-template-parsing"}, - ArgVector{"-fms-compatibility"}, - ArgVector{"-fdelayed-template-parsing", "-fms-compatibility"}); - INSTANTIATE_TEST_CASE_P(ParameterizedTests, ASTImporterLookupTableTest, DefaultTestValuesForRunOptions, ); @@ -4883,7 +5179,7 @@ INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportDecl, DefaultTestValuesForRunOptions, ); -INSTANTIATE_TEST_CASE_P(ParameterizedTests, ASTImporterTestBase, +INSTANTIATE_TEST_CASE_P(ParameterizedTests, ASTImporterOptionSpecificTestBase, DefaultTestValuesForRunOptions, ); INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportFunctions,