[PATCH] D155537: [ASTImporter] Fix import failed when anonymous union defined in class

Qizhi Hu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jul 17 19:09:15 PDT 2023


jcsxky created this revision.
jcsxky added a reviewer: danix800.
jcsxky added a project: clang.
Herald added a subscriber: martong.
Herald added a reviewer: a.sidorin.
Herald added a reviewer: shafik.
Herald added a project: All.
jcsxky requested review of this revision.
Herald added a subscriber: cfe-commits.

when import a class contains two anonymous unions, constructor accesses member in the second union would lead to import the second union, after that, import the first union will lead to conflict, skip when two union are both anonymous.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D155537

Files:
  clang/lib/AST/ASTImporter.cpp
  clang/unittests/AST/ASTImporterTest.cpp


Index: clang/unittests/AST/ASTImporterTest.cpp
===================================================================
--- clang/unittests/AST/ASTImporterTest.cpp
+++ clang/unittests/AST/ASTImporterTest.cpp
@@ -2462,6 +2462,36 @@
                 functionDecl(hasName("f"), hasDescendant(declRefExpr()))))));
 }
 
+TEST_P(ASTImporterOptionSpecificTestBase,
+       ImportAnnonymousUnionInClassTest) {
+  const char *Code =
+      R"(
+      class B{
+
+      public:
+
+        B(){
+          c=1;
+        }
+
+        void foo1(){}
+
+      private:
+        union{
+          int a;
+          int b;
+        };
+        union {
+          int c;
+          int d;
+        };
+      };
+      )";
+  Decl *FromTU = getTuDecl(Code, Lang_CXX11);
+  EXPECT_TRUE(FromTU);
+}
+
+
 struct ImportFunctionTemplates : ASTImporterOptionSpecificTestBase {};
 
 TEST_P(ImportFunctionTemplates, ImportFunctionTemplateInRecordDeclTwice) {
Index: clang/lib/AST/ASTImporter.cpp
===================================================================
--- clang/lib/AST/ASTImporter.cpp
+++ clang/lib/AST/ASTImporter.cpp
@@ -3861,7 +3861,9 @@
           ASTImporter::getFieldIndex(D) !=
           ASTImporter::getFieldIndex(FoundField))
         continue;
-
+      if (D->isAnonymousStructOrUnion() && FoundField->isAnonymousStructOrUnion()) {
+        continue;
+      }
       if (Importer.IsStructurallyEquivalent(D->getType(),
                                             FoundField->getType())) {
         Importer.MapImported(D, FoundField);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D155537.541304.patch
Type: text/x-patch
Size: 1528 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230718/a7bb741b/attachment.bin>


More information about the cfe-commits mailing list