r371820 - [Clang][ASTImporter] Added visibility check for FunctionTemplateDecl.

Balazs Keri via cfe-commits cfe-commits at lists.llvm.org
Fri Sep 13 01:03:49 PDT 2019


Author: balazske
Date: Fri Sep 13 01:03:49 2019
New Revision: 371820

URL: http://llvm.org/viewvc/llvm-project?rev=371820&view=rev
Log:
[Clang][ASTImporter] Added visibility check for FunctionTemplateDecl.

Summary:
ASTImporter makes now difference between function templates with same
name in different translation units if these are not visible outside.

Reviewers: martong, a.sidorin, shafik, a_sidorin

Reviewed By: a_sidorin

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

Tags: #clang

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

Modified:
    cfe/trunk/lib/AST/ASTImporter.cpp
    cfe/trunk/unittests/AST/ASTImporterVisibilityTest.cpp

Modified: cfe/trunk/lib/AST/ASTImporter.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/ASTImporter.cpp?rev=371820&r1=371819&r2=371820&view=diff
==============================================================================
--- cfe/trunk/lib/AST/ASTImporter.cpp (original)
+++ cfe/trunk/lib/AST/ASTImporter.cpp Fri Sep 13 01:03:49 2019
@@ -5638,17 +5638,16 @@ ASTNodeImporter::VisitFunctionTemplateDe
         continue;
 
       if (auto *FoundTemplate = dyn_cast<FunctionTemplateDecl>(FoundDecl)) {
-        if (FoundTemplate->hasExternalFormalLinkage() &&
-            D->hasExternalFormalLinkage()) {
-          if (IsStructuralMatch(D, FoundTemplate)) {
-            FunctionTemplateDecl *TemplateWithDef =
-                getTemplateDefinition(FoundTemplate);
-            if (D->isThisDeclarationADefinition() && TemplateWithDef) {
-              return Importer.MapImported(D, TemplateWithDef);
-            }
-            FoundByLookup = FoundTemplate;
-            break;
-          }
+        if (!hasSameVisibilityContext(FoundTemplate, D))
+          continue;
+        if (IsStructuralMatch(D, FoundTemplate)) {
+          FunctionTemplateDecl *TemplateWithDef =
+              getTemplateDefinition(FoundTemplate);
+          if (D->isThisDeclarationADefinition() && TemplateWithDef)
+            return Importer.MapImported(D, TemplateWithDef);
+
+          FoundByLookup = FoundTemplate;
+          break;
           // TODO: handle conflicting names
         }
       }

Modified: cfe/trunk/unittests/AST/ASTImporterVisibilityTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/AST/ASTImporterVisibilityTest.cpp?rev=371820&r1=371819&r2=371820&view=diff
==============================================================================
--- cfe/trunk/unittests/AST/ASTImporterVisibilityTest.cpp (original)
+++ cfe/trunk/unittests/AST/ASTImporterVisibilityTest.cpp Fri Sep 13 01:03:49 2019
@@ -43,6 +43,12 @@ struct GetTypedefNamePattern {
   using DeclTy = TypedefNameDecl;
   BindableMatcher<Decl> operator()() { return typedefNameDecl(hasName("T")); }
 };
+struct GetFunTemplPattern {
+  using DeclTy = FunctionTemplateDecl;
+  BindableMatcher<Decl> operator()() {
+    return functionTemplateDecl(hasName("f"));
+  }
+};
 
 // Values for the value-parameterized test fixtures.
 // FunctionDecl:
@@ -64,6 +70,10 @@ const auto *ExternTypedef = "typedef int
 const auto *AnonTypedef = "namespace { typedef int T; }";
 const auto *ExternUsing = "using T = int;";
 const auto *AnonUsing = "namespace { using T = int; }";
+// FunctionTemplateDecl:
+const auto *ExternFT = "template <class> void f();";
+const auto *StaticFT = "template <class> static void f();";
+const auto *AnonFT = "namespace { template <class> void f(); }";
 
 // First value in tuple: Compile options.
 // Second value in tuple: Source code to be used in the test.
@@ -108,6 +118,9 @@ protected:
 using ImportFunctionsVisibilityChain = ImportVisibilityChain<GetFunPattern>;
 using ImportVariablesVisibilityChain = ImportVisibilityChain<GetVarPattern>;
 using ImportClassesVisibilityChain = ImportVisibilityChain<GetClassPattern>;
+using ImportFunctionTemplatesVisibilityChain =
+    ImportVisibilityChain<GetFunTemplPattern>;
+
 // Value-parameterized test for functions.
 TEST_P(ImportFunctionsVisibilityChain, ImportChain) {
   TypedTest_ImportChain();
@@ -120,6 +133,10 @@ TEST_P(ImportVariablesVisibilityChain, I
 TEST_P(ImportClassesVisibilityChain, ImportChain) {
   TypedTest_ImportChain();
 }
+// Value-parameterized test for function templates.
+TEST_P(ImportFunctionTemplatesVisibilityChain, ImportChain) {
+  TypedTest_ImportChain();
+}
 
 // Automatic instantiation of the value-parameterized tests.
 INSTANTIATE_TEST_CASE_P(ParameterizedTests, ImportFunctionsVisibilityChain,
@@ -143,6 +160,11 @@ INSTANTIATE_TEST_CASE_P(
     ::testing::Combine(
         DefaultTestValuesForRunOptions,
         ::testing::Values(ExternC, AnonC)), );
+INSTANTIATE_TEST_CASE_P(ParameterizedTests,
+                        ImportFunctionTemplatesVisibilityChain,
+                        ::testing::Combine(DefaultTestValuesForRunOptions,
+                                           ::testing::Values(ExternFT, StaticFT,
+                                                             AnonFT)), );
 
 // First value in tuple: Compile options.
 // Second value in tuple: Tuple with informations for the test.
@@ -253,6 +275,7 @@ using ImportVariablesVisibility = Import
 using ImportClassesVisibility = ImportVisibility<GetClassPattern>;
 using ImportEnumsVisibility = ImportVisibility<GetEnumPattern>;
 using ImportTypedefNameVisibility = ImportVisibility<GetTypedefNamePattern>;
+using ImportFunctionTemplatesVisibility = ImportVisibility<GetFunTemplPattern>;
 
 // FunctionDecl.
 TEST_P(ImportFunctionsVisibility, ImportAfter) {
@@ -289,6 +312,13 @@ TEST_P(ImportTypedefNameVisibility, Impo
 TEST_P(ImportTypedefNameVisibility, ImportAfterImport) {
   TypedTest_ImportAfterImportWithMerge();
 }
+// FunctionTemplateDecl.
+TEST_P(ImportFunctionTemplatesVisibility, ImportAfter) {
+  TypedTest_ImportAfter();
+}
+TEST_P(ImportFunctionTemplatesVisibility, ImportAfterImport) {
+  TypedTest_ImportAfterImport();
+}
 
 const bool ExpectLinkedDeclChain = true;
 const bool ExpectUnlinkedDeclChain = false;
@@ -367,6 +397,20 @@ INSTANTIATE_TEST_CASE_P(
             std::make_tuple(AnonTypedef, ExternUsing, ExpectUnlinkedDeclChain),
             std::make_tuple(AnonTypedef, AnonUsing,
                             ExpectUnlinkedDeclChain))), );
+INSTANTIATE_TEST_CASE_P(
+    ParameterizedTests, ImportFunctionTemplatesVisibility,
+    ::testing::Combine(
+        DefaultTestValuesForRunOptions,
+        ::testing::Values(
+            std::make_tuple(ExternFT, ExternFT, ExpectLinkedDeclChain),
+            std::make_tuple(ExternFT, StaticFT, ExpectUnlinkedDeclChain),
+            std::make_tuple(ExternFT, AnonFT, ExpectUnlinkedDeclChain),
+            std::make_tuple(StaticFT, ExternFT, ExpectUnlinkedDeclChain),
+            std::make_tuple(StaticFT, StaticFT, ExpectUnlinkedDeclChain),
+            std::make_tuple(StaticFT, AnonFT, ExpectUnlinkedDeclChain),
+            std::make_tuple(AnonFT, ExternFT, ExpectUnlinkedDeclChain),
+            std::make_tuple(AnonFT, StaticFT, ExpectUnlinkedDeclChain),
+            std::make_tuple(AnonFT, AnonFT, ExpectUnlinkedDeclChain))), );
 
 } // end namespace ast_matchers
 } // end namespace clang




More information about the cfe-commits mailing list