[clang] e31ee64 - [clang][ASTImporter] Handle UsingType in friend declarations.

Balázs Kéri via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 30 01:06:45 PST 2023


Author: Balázs Kéri
Date: 2023-01-30T10:06:11+01:00
New Revision: e31ee6417c33a6e2f0e8440b1a86d5365279ad68

URL: https://github.com/llvm/llvm-project/commit/e31ee6417c33a6e2f0e8440b1a86d5365279ad68
DIFF: https://github.com/llvm/llvm-project/commit/e31ee6417c33a6e2f0e8440b1a86d5365279ad68.diff

LOG: [clang][ASTImporter] Handle UsingType in friend declarations.

ASTImporterLookupTable did not handle correctly friend declarations
where the friend type is an UsingType (type of a declaration that
comes from an using-declaration). The problem is fixed by handling
it in the same way as a friend with TypedefType.

Reviewed By: steakhal

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

Added: 
    

Modified: 
    clang/lib/AST/ASTImporterLookupTable.cpp
    clang/unittests/AST/ASTImporterTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/AST/ASTImporterLookupTable.cpp b/clang/lib/AST/ASTImporterLookupTable.cpp
index b7d17a5e92d00..a193e97bbf5a0 100644
--- a/clang/lib/AST/ASTImporterLookupTable.cpp
+++ b/clang/lib/AST/ASTImporterLookupTable.cpp
@@ -67,6 +67,8 @@ struct Builder : RecursiveASTVisitor<Builder> {
         } else if (isa<TypedefType>(Ty)) {
           // We do not put friend typedefs to the lookup table because
           // ASTImporter does not organize typedefs into redecl chains.
+        } else if (isa<UsingType>(Ty)) {
+          // Similar to TypedefType, not putting into lookup table.
         } else {
           llvm_unreachable("Unhandled type of friend class");
         }

diff  --git a/clang/unittests/AST/ASTImporterTest.cpp b/clang/unittests/AST/ASTImporterTest.cpp
index 1080b5acd4b30..78de31e478ebc 100644
--- a/clang/unittests/AST/ASTImporterTest.cpp
+++ b/clang/unittests/AST/ASTImporterTest.cpp
@@ -5180,6 +5180,39 @@ TEST_P(ASTImporterLookupTableTest,
   EXPECT_EQ(Res.count(Alias), 1u);
 }
 
+TEST_P(ASTImporterLookupTableTest,
+       LookupFindsFriendClassDeclWithUsingTypeDoesNotAssert) {
+  TranslationUnitDecl *ToTU = getToTuDecl(
+      R"(
+      namespace a {
+        namespace b { class InnerClass; }
+        using b::InnerClass;
+      }
+      class B {
+        friend a::InnerClass;
+      };
+      )",
+      Lang_CXX11);
+
+  // ASTImporterLookupTable constructor handles friend with using-type without
+  // asserts.
+  ASTImporterLookupTable LT(*ToTU);
+
+  auto *Using = FirstDeclMatcher<UsingDecl>().match(
+      ToTU, usingDecl(hasName("InnerClass")));
+  DeclarationName Name = Using->getDeclName();
+  auto Res = LT.lookup(ToTU, Name);
+  EXPECT_EQ(Res.size(), 0u);
+  auto *NsA = FirstDeclMatcher<NamespaceDecl>().match(
+      ToTU, namespaceDecl(hasName("a")));
+  auto *RecordB = FirstDeclMatcher<CXXRecordDecl>().match(
+      ToTU, cxxRecordDecl(hasName("B")));
+  auto Res1 = LT.lookup(NsA, Name);
+  EXPECT_EQ(Res1.count(Using), 1u);
+  auto Res2 = LT.lookup(RecordB, Name);
+  EXPECT_EQ(Res2.size(), 0u);
+}
+
 TEST_P(ASTImporterLookupTableTest, LookupFindsFwdFriendClassTemplateDecl) {
   TranslationUnitDecl *ToTU = getToTuDecl(
       R"(


        


More information about the cfe-commits mailing list