From 8cbd895947aebc3f14ffbf9781665b79bcaf80c4 Mon Sep 17 00:00:00 2001 From: Richard Smith Date: Tue, 4 Aug 2015 02:05:09 +0000 Subject: [PATCH] [modules] Make IndirectFieldDecl mergeable to avoid lookup ambiguity when the same anonymous union is defined across multiple modules. llvm-svn: 243940 --- clang/include/clang/AST/Decl.h | 6 +++++- clang/lib/Serialization/ASTReaderDecl.cpp | 9 +++++++++ clang/test/Modules/Inputs/submodules-merge-defs/defs.h | 8 ++++++++ clang/test/Modules/submodules-merge-defs.cpp | 1 + 4 files changed, 23 insertions(+), 1 deletion(-) diff --git a/clang/include/clang/AST/Decl.h b/clang/include/clang/AST/Decl.h index 76c0585..76fda1c 100644 --- a/clang/include/clang/AST/Decl.h +++ b/clang/include/clang/AST/Decl.h @@ -2499,7 +2499,8 @@ public: /// IndirectFieldDecl - An instance of this class is created to represent a /// field injected from an anonymous union/struct into the parent scope. /// IndirectFieldDecl are always implicit. -class IndirectFieldDecl : public ValueDecl { +class IndirectFieldDecl : public ValueDecl, + public Mergeable { void anchor() override; NamedDecl **Chaining; unsigned ChainingSize; @@ -2537,6 +2538,9 @@ public: return dyn_cast(*chain_begin()); } + IndirectFieldDecl *getCanonicalDecl() override { return getFirstDecl(); } + const IndirectFieldDecl *getCanonicalDecl() const { return getFirstDecl(); } + // Implement isa/cast/dyncast/etc. static bool classof(const Decl *D) { return classofKind(D->getKind()); } static bool classofKind(Kind K) { return K == IndirectField; } diff --git a/clang/lib/Serialization/ASTReaderDecl.cpp b/clang/lib/Serialization/ASTReaderDecl.cpp index 45a3e45..0242e7e 100644 --- a/clang/lib/Serialization/ASTReaderDecl.cpp +++ b/clang/lib/Serialization/ASTReaderDecl.cpp @@ -1178,6 +1178,8 @@ void ASTDeclReader::VisitIndirectFieldDecl(IndirectFieldDecl *FD) { for (unsigned I = 0; I != FD->ChainingSize; ++I) FD->Chaining[I] = ReadDeclAs(Record, Idx); + + mergeMergeable(FD); } ASTDeclReader::RedeclarableResult ASTDeclReader::VisitVarDeclImpl(VarDecl *VD) { @@ -2638,6 +2640,13 @@ static bool isSameEntity(NamedDecl *X, NamedDecl *Y) { return X->getASTContext().hasSameType(FDX->getType(), FDY->getType()); } + // Indirect fields with the same target field match. + if (auto *IFDX = dyn_cast(X)) { + auto *IFDY = cast(Y); + return IFDX->getAnonField()->getCanonicalDecl() == + IFDY->getAnonField()->getCanonicalDecl(); + } + // Enumerators with the same name match. if (isa(X)) // FIXME: Also check the value is odr-equivalent. diff --git a/clang/test/Modules/Inputs/submodules-merge-defs/defs.h b/clang/test/Modules/Inputs/submodules-merge-defs/defs.h index e4c86d80..670c400 100644 --- a/clang/test/Modules/Inputs/submodules-merge-defs/defs.h +++ b/clang/test/Modules/Inputs/submodules-merge-defs/defs.h @@ -103,3 +103,11 @@ namespace RedeclDifferentDeclKind { typedef X X; using RedeclDifferentDeclKind::X; } + +namespace Anon { + struct X { + union { + int n; + }; + }; +} diff --git a/clang/test/Modules/submodules-merge-defs.cpp b/clang/test/Modules/submodules-merge-defs.cpp index 016b8a8..62cbbd5 100644 --- a/clang/test/Modules/submodules-merge-defs.cpp +++ b/clang/test/Modules/submodules-merge-defs.cpp @@ -104,6 +104,7 @@ template class K> struct J; J<> post_j2; FriendDefArg::Y friend_def_arg; FriendDefArg::D<> friend_def_arg_d; +int post_anon_x_n = Anon::X().n; MergeFunctionTemplateSpecializations::X::Q xiqc; -- 2.7.4