diff --git a/clang/lib/AST/ASTImporter.cpp b/clang/lib/AST/ASTImporter.cpp --- a/clang/lib/AST/ASTImporter.cpp +++ b/clang/lib/AST/ASTImporter.cpp @@ -932,6 +932,27 @@ EllipsisLoc); } +template +bool ASTNodeImporter::hasSameVisibilityContext(T *Found, T *From) { + if (From->hasExternalFormalLinkage()) + return Found->hasExternalFormalLinkage(); + if (Importer.GetFromTU(Found) != From->getTranslationUnitDecl()) + return false; + if (From->isInAnonymousNamespace()) + return Found->isInAnonymousNamespace(); + else + return !Found->isInAnonymousNamespace() && + !Found->hasExternalFormalLinkage(); +} + +template <> +bool ASTNodeImporter::hasSameVisibilityContext(TypedefNameDecl *Found, + TypedefNameDecl *From) { + if (From->isInAnonymousNamespace() && Found->isInAnonymousNamespace()) + return Importer.GetFromTU(Found) == From->getTranslationUnitDecl(); + return From->isInAnonymousNamespace() == Found->isInAnonymousNamespace(); +} + } // namespace clang //---------------------------------------------------------------------------- @@ -2352,6 +2373,9 @@ if (!FoundDecl->isInIdentifierNamespace(IDNS)) continue; if (auto *FoundTypedef = dyn_cast(FoundDecl)) { + if (!hasSameVisibilityContext(FoundTypedef, D)) + continue; + QualType FromUT = D->getUnderlyingType(); QualType FoundUT = FoundTypedef->getUnderlyingType(); if (Importer.IsStructurallyEquivalent(FromUT, FoundUT)) { @@ -3022,19 +3046,6 @@ return Error::success(); } -template -bool ASTNodeImporter::hasSameVisibilityContext(T *Found, T *From) { - if (From->hasExternalFormalLinkage()) - return Found->hasExternalFormalLinkage(); - if (Importer.GetFromTU(Found) != From->getTranslationUnitDecl()) - return false; - if (From->isInAnonymousNamespace()) - return Found->isInAnonymousNamespace(); - else - return !Found->isInAnonymousNamespace() && - !Found->hasExternalFormalLinkage(); -} - ExpectedDecl ASTNodeImporter::VisitFunctionDecl(FunctionDecl *D) { SmallVector Redecls = getCanonicalForwardRedeclChain(D); diff --git a/clang/unittests/AST/ASTImporterVisibilityTest.cpp b/clang/unittests/AST/ASTImporterVisibilityTest.cpp --- a/clang/unittests/AST/ASTImporterVisibilityTest.cpp +++ b/clang/unittests/AST/ASTImporterVisibilityTest.cpp @@ -39,6 +39,18 @@ using DeclTy = EnumDecl; BindableMatcher operator()() { return enumDecl(hasName("E")); } }; +struct GetTypedefPattern { + using DeclTy = TypedefDecl; + BindableMatcher operator()() { return typedefDecl(hasName("T")); } +}; +struct GetTypeAliasPattern { + using DeclTy = TypeAliasDecl; + BindableMatcher operator()() { return typeAliasDecl(hasName("T")); } +}; +struct GetTypedefNamePattern { + using DeclTy = TypedefNameDecl; + BindableMatcher operator()() { return typedefNameDecl(hasName("T")); } +}; // Values for the value-parameterized test fixtures. // FunctionDecl: @@ -55,6 +67,11 @@ // EnumDecl: const auto *ExternE = "enum E {};"; const auto *AnonE = "namespace { enum E {}; }"; +// TypedefNameDecl: +const auto *ExternTypedef = "typedef int T;"; +const auto *AnonTypedef = "namespace { typedef int T; }"; +const auto *ExternUsing = "using T = int;"; +const auto *AnonUsing = "namespace { using T = int; }"; // First value in tuple: Compile options. // Second value in tuple: Source code to be used in the test. @@ -243,6 +260,7 @@ using ImportVariablesVisibility = ImportVisibility; using ImportClassesVisibility = ImportVisibility; using ImportEnumsVisibility = ImportVisibility; +using ImportTypedefNameVisibility = ImportVisibility; // FunctionDecl. TEST_P(ImportFunctionsVisibility, ImportAfter) { @@ -272,6 +290,13 @@ TEST_P(ImportEnumsVisibility, ImportAfterImport) { TypedTest_ImportAfterImportWithMerge(); } +// TypedefNameDecl. +TEST_P(ImportTypedefNameVisibility, ImportAfter) { + TypedTest_ImportAfterWithMerge(); +} +TEST_P(ImportTypedefNameVisibility, ImportAfterImport) { + TypedTest_ImportAfterImportWithMerge(); +} const bool ExpectLink = true; const bool ExpectNotLink = false; @@ -318,6 +343,30 @@ std::make_tuple(ExternE, AnonE, ExpectNotLink), std::make_tuple(AnonE, ExternE, ExpectNotLink), std::make_tuple(AnonE, AnonE, ExpectNotLink))), ); +INSTANTIATE_TEST_CASE_P( + ParameterizedTests, ImportTypedefNameVisibility, + ::testing::Combine( + DefaultTestValuesForRunOptions, + ::testing::Values( + std::make_tuple(ExternTypedef, ExternTypedef, ExpectLink), + std::make_tuple(ExternTypedef, AnonTypedef, ExpectNotLink), + std::make_tuple(AnonTypedef, ExternTypedef, ExpectNotLink), + std::make_tuple(AnonTypedef, AnonTypedef, ExpectNotLink), + + std::make_tuple(ExternUsing, ExternUsing, ExpectLink), + std::make_tuple(ExternUsing, AnonUsing, ExpectNotLink), + std::make_tuple(AnonUsing, ExternUsing, ExpectNotLink), + std::make_tuple(AnonUsing, AnonUsing, ExpectNotLink), + + std::make_tuple(ExternUsing, ExternTypedef, ExpectLink), + std::make_tuple(ExternUsing, AnonTypedef, ExpectNotLink), + std::make_tuple(AnonUsing, ExternTypedef, ExpectNotLink), + std::make_tuple(AnonUsing, AnonTypedef, ExpectNotLink), + + std::make_tuple(ExternTypedef, ExternUsing, ExpectLink), + std::make_tuple(ExternTypedef, AnonUsing, ExpectNotLink), + std::make_tuple(AnonTypedef, ExternUsing, ExpectNotLink), + std::make_tuple(AnonTypedef, AnonUsing, ExpectNotLink))), ); } // end namespace ast_matchers } // end namespace clang