From: Richard Smith Date: Sat, 6 Jan 2018 01:07:05 +0000 (+0000) Subject: Serialize the IDNS for a UsingShadowDecl rather than recomputing it. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=0c0f1eba6b488bd9ee5b468fd55628e416701542;p=clang Serialize the IDNS for a UsingShadowDecl rather than recomputing it. Attempting to recompute it are doomed to fail because the IDNS of a declaration is not necessarily preserved across serialization and deserialization (in turn because whether a friend declaration is visible depends on whether some prior non-friend declaration exists). git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@321921 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/DeclCXX.h b/include/clang/AST/DeclCXX.h index 88dc9a6559..9f19c060f2 100644 --- a/include/clang/AST/DeclCXX.h +++ b/include/clang/AST/DeclCXX.h @@ -3129,10 +3129,14 @@ public: /// \brief Sets the underlying declaration which has been brought into the /// local scope. - void setTargetDecl(NamedDecl* ND) { + void setTargetDecl(NamedDecl *ND) { assert(ND && "Target decl is null!"); Underlying = ND; - IdentifierNamespace = ND->getIdentifierNamespace(); + // A UsingShadowDecl is never a friend or local extern declaration, even + // if it is a shadow declaration for one. + IdentifierNamespace = + ND->getIdentifierNamespace() & + ~(IDNS_OrdinaryFriend | IDNS_TagFriend | IDNS_LocalExtern); } /// \brief Gets the using declaration to which this declaration is tied. diff --git a/lib/AST/DeclCXX.cpp b/lib/AST/DeclCXX.cpp index 41f2449a9d..afbc3f315f 100644 --- a/lib/AST/DeclCXX.cpp +++ b/lib/AST/DeclCXX.cpp @@ -2387,10 +2387,10 @@ UsingShadowDecl::UsingShadowDecl(Kind K, ASTContext &C, DeclContext *DC, SourceLocation Loc, UsingDecl *Using, NamedDecl *Target) : NamedDecl(K, DC, Loc, Using ? Using->getDeclName() : DeclarationName()), - redeclarable_base(C), Underlying(Target), + redeclarable_base(C), Underlying(), UsingOrNextShadow(cast(Using)) { if (Target) - IdentifierNamespace = Target->getIdentifierNamespace(); + setTargetDecl(Target); setImplicit(); } diff --git a/lib/Serialization/ASTReaderDecl.cpp b/lib/Serialization/ASTReaderDecl.cpp index efbaf92a84..0cf9afb075 100644 --- a/lib/Serialization/ASTReaderDecl.cpp +++ b/lib/Serialization/ASTReaderDecl.cpp @@ -1500,7 +1500,8 @@ void ASTDeclReader::VisitUsingPackDecl(UsingPackDecl *D) { void ASTDeclReader::VisitUsingShadowDecl(UsingShadowDecl *D) { RedeclarableResult Redecl = VisitRedeclarable(D); VisitNamedDecl(D); - D->setTargetDecl(ReadDeclAs()); + D->Underlying = ReadDeclAs(); + D->IdentifierNamespace = Record.readInt(); D->UsingOrNextShadow = ReadDeclAs(); UsingShadowDecl *Pattern = ReadDeclAs(); if (Pattern) diff --git a/lib/Serialization/ASTWriterDecl.cpp b/lib/Serialization/ASTWriterDecl.cpp index bb72a3b383..341f0e1607 100644 --- a/lib/Serialization/ASTWriterDecl.cpp +++ b/lib/Serialization/ASTWriterDecl.cpp @@ -1192,6 +1192,7 @@ void ASTDeclWriter::VisitUsingShadowDecl(UsingShadowDecl *D) { VisitRedeclarable(D); VisitNamedDecl(D); Record.AddDeclRef(D->getTargetDecl()); + Record.push_back(D->getIdentifierNamespace()); Record.AddDeclRef(D->UsingOrNextShadow); Record.AddDeclRef(Context.getInstantiatedFromUsingShadowDecl(D)); Code = serialization::DECL_USING_SHADOW; diff --git a/test/Modules/using-decl-friend-2.cpp b/test/Modules/using-decl-friend-2.cpp new file mode 100644 index 0000000000..0c71c37765 --- /dev/null +++ b/test/Modules/using-decl-friend-2.cpp @@ -0,0 +1,39 @@ +// RUN: %clang_cc1 -fmodules %s -verify +// expected-no-diagnostics + +#pragma clang module build A +module A {} +#pragma clang module contents +#pragma clang module begin A +namespace N { class X; } +#pragma clang module end +#pragma clang module endbuild + +#pragma clang module build B +module B {} +#pragma clang module contents +#pragma clang module begin B +namespace N { class Friendly { friend class X; }; } +#pragma clang module end +#pragma clang module endbuild + +#pragma clang module build C +module C {} +#pragma clang module contents +#pragma clang module begin C +#pragma clang module import A +void use_X(N::X *p); +#pragma clang module import B +// UsingShadowDecl names the friend declaration +using N::X; +#pragma clang module end +#pragma clang module endbuild + +#pragma clang module import B +namespace N { class AlsoFriendly { friend class X; }; } +#pragma clang module import A +#pragma clang module import C +// The friend declaration from N::Friendly is now the first in the redecl +// chain, so is not ordinarily visible. We need the IDNS of the UsingShadowDecl +// to still consider it to be visible, though. +X *p;