DR1113: anonymous namespaces formally give their contents internal linkage.

This doesn't affect our code generation in any material way -- we already give
such declarations internal linkage from a codegen perspective -- but it has
some subtle effects on code validity.

We suppress the 'L' (internal linkage) marker for mangled names in anonymous
namespaces, because it is redundant (the information is already carried by the
namespace); this deviates from GCC's behavior if a variable or function in an
anonymous namespace is redundantly declared 'static' (where GCC does include
the 'L'), but GCC's behavior is incoherent because such a declaration can be
validly declared with or without the 'static'.

We still deviate from the standard in one regard here: extern "C" declarations
in anonymous namespaces are still granted external linkage. Changing those does
not appear to have been an intentional consequence of the standard change in
DR1113.

llvm-svn: 314037
This commit is contained in:
Richard Smith 2017-09-22 22:21:44 +00:00
parent 5fc4942888
commit df963a38a9
7 changed files with 69 additions and 10 deletions

View File

@ -619,16 +619,16 @@ LinkageComputer::getLVForNamespaceScopeDecl(const NamedDecl *D,
if (D->isInAnonymousNamespace()) {
const auto *Var = dyn_cast<VarDecl>(D);
const auto *Func = dyn_cast<FunctionDecl>(D);
// FIXME: In C++11 onwards, anonymous namespaces should give decls
// within them (including those inside extern "C" contexts) internal
// linkage, not unique external linkage:
// FIXME: The check for extern "C" here is not justified by the standard
// wording, but we retain it from the pre-DR1113 model to avoid breaking
// code.
//
// C++11 [basic.link]p4:
// An unnamed namespace or a namespace declared directly or indirectly
// within an unnamed namespace has internal linkage.
if ((!Var || !isFirstInExternCContext(Var)) &&
(!Func || !isFirstInExternCContext(Func)))
return LinkageInfo::uniqueExternal();
return getInternalLinkageFor(D);
}
// Set up the defaults.
@ -1130,7 +1130,7 @@ LinkageInfo LinkageComputer::getLVForLocalDecl(const NamedDecl *D,
if (const auto *Function = dyn_cast<FunctionDecl>(D)) {
if (Function->isInAnonymousNamespace() &&
!Function->isInExternCContext())
return LinkageInfo::uniqueExternal();
return getInternalLinkageFor(Function);
// This is a "void f();" which got merged with a file static.
if (Function->getCanonicalDecl()->getStorageClass() == SC_Static)
@ -1153,7 +1153,7 @@ LinkageInfo LinkageComputer::getLVForLocalDecl(const NamedDecl *D,
if (const auto *Var = dyn_cast<VarDecl>(D)) {
if (Var->hasExternalStorage()) {
if (Var->isInAnonymousNamespace() && !Var->isInExternCContext())
return LinkageInfo::uniqueExternal();
return getInternalLinkageFor(Var);
LinkageInfo LV;
if (Var->getStorageClass() == SC_PrivateExtern)

View File

@ -1287,9 +1287,15 @@ void CXXNameMangler::mangleUnqualifiedName(const NamedDecl *ND,
//
// void test() { extern void foo(); }
// static void foo();
//
// Don't bother with the L marker for names in anonymous namespaces; the
// 12_GLOBAL__N_1 mangling is quite sufficient there, and this better
// matches GCC anyway, because GCC does not treat anonymous namespaces as
// implying internal linkage.
if (ND && ND->getFormalLinkage() == InternalLinkage &&
!ND->isExternallyVisible() &&
getEffectiveDeclContext(ND)->isFileContext())
getEffectiveDeclContext(ND)->isFileContext() &&
!ND->isInAnonymousNamespace())
Out << 'L';
auto *FD = dyn_cast<FunctionDecl>(ND);

View File

@ -52,6 +52,13 @@ void use_visible_no_linkage() {
visible_no_linkage3(); // expected-note {{used here}}
}
namespace {
struct InternalLinkage {};
}
InternalLinkage internal_linkage(); // expected-error {{used but not defined}}
void use_internal_linkage() {
internal_linkage(); // expected-note {{used here}}
}
extern inline int not_defined; // expected-error {{not defined}}
extern inline int defined_after_use;

View File

@ -0,0 +1,30 @@
// RUN: %clang_cc1 -std=c++98 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
// RUN: %clang_cc1 -std=c++11 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
// RUN: %clang_cc1 -std=c++14 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
// RUN: %clang_cc1 -std=c++17 %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
// RUN: %clang_cc1 -std=c++2a %s -verify -fexceptions -fcxx-exceptions -pedantic-errors
namespace dr1113 { // dr1113: partial
namespace named {
extern int a; // expected-note {{previous}}
static int a; // expected-error {{static declaration of 'a' follows non-static}}
}
namespace {
extern int a;
static int a; // ok, both declarations have internal linkage
int b = a;
}
// FIXME: Per DR1113 and DR4, this is ill-formed due to ambiguity: the second
// 'f' has internal linkage, and so does not have C language linkage, so is
// not a redeclaration of the first 'f'.
//
// To avoid a breaking change here, Clang ignores the "internal linkage" effect
// of anonymous namespaces on declarations declared within an 'extern "C"'
// linkage-specification.
extern "C" void f();
namespace {
extern "C" void f();
}
void g() { f(); }
}

View File

@ -6,7 +6,8 @@ int f();
namespace {
// CHECK-1: @_ZN12_GLOBAL__N_11bE = internal global i32 0
// CHECK-1: @_ZN12_GLOBAL__N_1L1cE = internal global i32 0
// CHECK-1: @_ZN12_GLOBAL__N_11cE = internal global i32 0
// CHECK-1: @_ZN12_GLOBAL__N_12c2E = internal global i32 0
// CHECK-1: @_ZN12_GLOBAL__N_11D1dE = internal global i32 0
// CHECK-1: @_ZN12_GLOBAL__N_11aE = internal global i32 0
int a = 0;
@ -15,6 +16,12 @@ namespace {
static int c = f();
// Note, we can't use an 'L' mangling for c or c2 (like GCC does) based on
// the 'static' specifier, because the variable can be redeclared without it.
extern int c2;
int g() { return c2; }
static int c2 = f();
class D {
static int d;
};

View File

@ -143,9 +143,10 @@ namespace test13 {
}
namespace test14 {
// Anonymous namespace implies internal linkage, so 'static' has no effect.
namespace {
void a(void); // expected-note {{previous declaration is here}}
static void a(void) {} // expected-error {{static declaration of 'a' follows non-static declaration}}
void a(void);
static void a(void) {}
}
}

View File

@ -187,6 +187,10 @@ namespace OverloadUse {
void f();
void f(int); // expected-warning {{function 'OverloadUse::(anonymous namespace)::f' has internal linkage but is not defined}}
void f(int, int); // expected-warning {{function 'OverloadUse::(anonymous namespace)::f' has internal linkage but is not defined}}
#if __cplusplus < 201103L
// expected-note@-3 {{here}}
// expected-note@-3 {{here}}
#endif
}
template<void x()> void t() { x(); }
template<void x(int)> void t(int*) { x(10); }
@ -194,6 +198,10 @@ namespace OverloadUse {
void g(int n) {
t<f>(&n); // expected-note {{used here}}
t<f>(&n, &n); // expected-note {{used here}}
#if __cplusplus < 201103L
// expected-warning@-3 {{non-type template argument referring to function 'f' with internal linkage}}
// expected-warning@-3 {{non-type template argument referring to function 'f' with internal linkage}}
#endif
}
}