Index: lib/AST/ASTImporter.cpp =================================================================== --- lib/AST/ASTImporter.cpp +++ lib/AST/ASTImporter.cpp @@ -1631,7 +1631,12 @@ // We may already have a record of the same name; try to find and match it. RecordDecl *AdoptDecl = nullptr; RecordDecl *PrevDecl = nullptr; - if (!DC->isFunctionOrMethod()) { + + // There are unnamed structures that are not anonymous. + // They cause wrong conflict detections due to the null string name. + bool isNotAnonymousButUnnamed = (!D->isAnonymousStructOrUnion() && SearchName.getAsString() == ""); + + if (!DC->isFunctionOrMethod() && !isNotAnonymousButUnnamed) { SmallVector ConflictingDecls; SmallVector FoundDecls; DC->getRedeclContext()->localUncachedLookup(SearchName, FoundDecls); Index: unittests/AST/ASTImporterTest.cpp =================================================================== --- unittests/AST/ASTImporterTest.cpp +++ unittests/AST/ASTImporterTest.cpp @@ -485,6 +485,65 @@ has(atomicType())))))))))); } +TEST(ImportDecl, ImportUnnamedRecordDecl) { + MatchVerifier Verifier; + EXPECT_TRUE( + testImport( + "void declToImport() {" + " struct Root {" + " struct { int a; } A;" + " struct { float b; } B;" + " } root;" + "}", + Lang_C, "", Lang_C, Verifier, + functionDecl( + hasBody( + compoundStmt( + has( + declStmt( + has( + recordDecl( + has( + recordDecl( + has( + fieldDecl( + hasType(asString("int")))))), + has( + recordDecl( + has( + fieldDecl( + hasType(asString("float")))))) + ))))))))); +} + +TEST(ImportDecl, ImportAnonymousRecordDecl) { + MatchVerifier Verifier; + EXPECT_TRUE( + testImport( + "void declToImport() {" + " struct Root {" + " union { char a;};" + " union { int b;};" + " } root;" + "}", + Lang_C, "", Lang_C, Verifier, + functionDecl( + hasBody( + compoundStmt( + has( + declStmt( + has( + recordDecl( + has( + recordDecl( + has( + fieldDecl(hasType(asString("char")))))), + has( + recordDecl( + has( + fieldDecl(hasType(asString("int")))))) + ))))))))); +} } // end namespace ast_matchers } // end namespace clang