From: Richard Smith Date: Tue, 10 Oct 2017 00:49:38 +0000 (+0000) Subject: [Modules TS] Avoid computing the linkage of the enclosing DeclContext for a declarati... X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=aad768a1a5d58a180b0027e2612b1fcf8893cbc7;p=clang [Modules TS] Avoid computing the linkage of the enclosing DeclContext for a declaration in the global module. This works around a language issue where adding a typedef name for linkage purposes changes the linkage of an already-defined class after it becomes complete. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@315256 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/AST/DeclBase.h b/include/clang/AST/DeclBase.h index aa278c301e..47515a848a 100644 --- a/include/clang/AST/DeclBase.h +++ b/include/clang/AST/DeclBase.h @@ -740,7 +740,10 @@ public: /// Get the module that owns this declaration for linkage purposes. /// There only ever is such a module under the C++ Modules TS. - Module *getOwningModuleForLinkage() const; + /// + /// \param IgnoreLinkage Ignore the linkage of the entity; assume that + /// all declarations in a global module fragment are unowned. + Module *getOwningModuleForLinkage(bool IgnoreLinkage = false) const; /// \brief Determine whether this declaration might be hidden from name /// lookup. Note that the declaration might be visible even if this returns diff --git a/include/clang/Sema/Sema.h b/include/clang/Sema/Sema.h index f32fafa447..277c62f543 100644 --- a/include/clang/Sema/Sema.h +++ b/include/clang/Sema/Sema.h @@ -3051,8 +3051,11 @@ public: RedeclarationKind forRedeclarationInCurContext() { // A declaration with an owning module for linkage can never link against - // anything that is not visible. - if (cast(CurContext)->getOwningModuleForLinkage()) + // anything that is not visible. We don't need to check linkage here; if + // the context has internal linkage, redeclaration lookup won't find things + // from other TUs, and we can't safely compute linkage yet in general. + if (cast(CurContext) + ->getOwningModuleForLinkage(/*IgnoreLinkage*/true)) return ForVisibleRedeclaration; return ForExternalRedeclaration; } diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index cdd89d20e6..4a9b9bea83 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -1395,7 +1395,7 @@ LinkageInfo LinkageComputer::getDeclLinkageAndVisibility(const NamedDecl *D) { : NamedDecl::VisibilityForValue)); } -Module *Decl::getOwningModuleForLinkage() const { +Module *Decl::getOwningModuleForLinkage(bool IgnoreLinkage) const { Module *M = getOwningModule(); if (!M) return nullptr; @@ -1413,6 +1413,8 @@ Module *Decl::getOwningModuleForLinkage() const { // for linkage purposes. But internal linkage declarations in the global // module fragment of a particular module are owned by that module for // linkage purposes. + if (IgnoreLinkage) + return nullptr; bool InternalLinkage; if (auto *ND = dyn_cast(this)) InternalLinkage = !ND->hasExternalFormalLinkage(); diff --git a/test/Modules/anon-linkage.cpp b/test/Modules/anon-linkage.cpp new file mode 100644 index 0000000000..590638292b --- /dev/null +++ b/test/Modules/anon-linkage.cpp @@ -0,0 +1,12 @@ +// RUN: %clang_cc1 -std=c++17 -fmodules-ts %s + +typedef struct { + int c; + union { + int n; + char c[4]; + } v; +} mbstate; + +export module M; +export using ::mbstate;