From: Gabor Marton Date: Thu, 5 Jul 2018 09:51:13 +0000 (+0000) Subject: [ASTImporter] Fix import of objects with anonymous types X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=60f7669eb5ab57464faf42a85d8b0219e6e1fdf9;p=clang [ASTImporter] Fix import of objects with anonymous types Summary: Currently, anonymous types are merged into the same redecl chain even if they are structurally inequivalent. This results that global objects are not imported, if there are at least two global objects with different anonymous types. This patch provides a fix. Reviewers: a.sidorin, balazske, r.stahl Subscribers: rnkovacs, dkrupp, cfe-commits Differential Revision: https://reviews.llvm.org/D48773 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@336332 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/lib/AST/ASTImporter.cpp b/lib/AST/ASTImporter.cpp index bc75d80a55..759f9da30b 100644 --- a/lib/AST/ASTImporter.cpp +++ b/lib/AST/ASTImporter.cpp @@ -2072,17 +2072,8 @@ Decl *ASTNodeImporter::VisitRecordDecl(RecordDecl *D) { if (auto *FoundRecord = dyn_cast(Found)) { if (!SearchName) { - // If both unnamed structs/unions are in a record context, make sure - // they occur in the same location in the context records. - if (Optional Index1 = - StructuralEquivalenceContext::findUntaggedStructOrUnionIndex( - D)) { - if (Optional Index2 = StructuralEquivalenceContext:: - findUntaggedStructOrUnionIndex(FoundRecord)) { - if (*Index1 != *Index2) - continue; - } - } + if (!IsStructuralMatch(D, FoundRecord, false)) + continue; } PrevDecl = FoundRecord; diff --git a/unittests/AST/ASTImporterTest.cpp b/unittests/AST/ASTImporterTest.cpp index dfcb6c1893..1f86b91a88 100644 --- a/unittests/AST/ASTImporterTest.cpp +++ b/unittests/AST/ASTImporterTest.cpp @@ -1682,6 +1682,35 @@ TEST_P( .match(ToTU, classTemplateSpecializationDecl())); } +TEST_P(ASTImporterTestBase, ObjectsWithUnnamedStructType) { + Decl *FromTU = getTuDecl( + R"( + struct { int a; int b; } object0 = { 2, 3 }; + struct { int x; int y; int z; } object1; + )", + Lang_CXX, "input0.cc"); + + auto getRecordDecl = [](VarDecl *VD) { + auto *ET = cast(VD->getType().getTypePtr()); + return cast(ET->getNamedType().getTypePtr())->getDecl(); + }; + + auto *Obj0 = + FirstDeclMatcher().match(FromTU, varDecl(hasName("object0"))); + auto *From0 = getRecordDecl(Obj0); + auto *Obj1 = + FirstDeclMatcher().match(FromTU, varDecl(hasName("object1"))); + auto *From1 = getRecordDecl(Obj1); + + auto *To0 = Import(From0, Lang_CXX); + auto *To1 = Import(From1, Lang_CXX); + + EXPECT_TRUE(To0); + EXPECT_TRUE(To1); + EXPECT_NE(To0, To1); + EXPECT_NE(To0->getCanonicalDecl(), To1->getCanonicalDecl()); +} + struct ImportFunctions : ASTImporterTestBase {}; TEST_P(ImportFunctions,