]> granicus.if.org Git - clang/commitdiff
DR1113: anonymous namespaces formally give their contents internal linkage.
authorRichard Smith <richard-llvm@metafoo.co.uk>
Fri, 22 Sep 2017 22:21:44 +0000 (22:21 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Fri, 22 Sep 2017 22:21:44 +0000 (22:21 +0000)
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.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@314037 91177308-0d34-0410-b5e6-96231b3b80d8

lib/AST/Decl.cpp
lib/AST/ItaniumMangle.cpp
test/CXX/basic/basic.link/p8.cpp
test/CXX/drs/dr11xx.cpp [new file with mode: 0644]
test/CodeGenCXX/anonymous-namespaces.cpp
test/SemaCXX/linkage2.cpp
test/SemaCXX/undefined-internal.cpp

index 19fbf878e7e01fb16bc6eaf7a72463f08e95159d..3f9eaf28ef94750605cc11a0a090a54ab6f17689 100644 (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)
index 5ce521715621fa037ae154b0b135fb2f9e5b55a1..8c5ebd0113ac9b57ee5a29fd534791e974d5b8c2 100644 (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);
index 07adc65b67a54cf35d442ca0928ebbaf7e2e7c0f..317fb31fb0388d8e3c416351810d930242d88f77 100644 (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;
diff --git a/test/CXX/drs/dr11xx.cpp b/test/CXX/drs/dr11xx.cpp
new file mode 100644 (file)
index 0000000..81fbc1e
--- /dev/null
@@ -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(); }
+}
index abc700fef6c29800c26b78f40ba2f3fc43aeca69..b2857ff41714d6aac22306fdccf97c672105bd5d 100644 (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;
   };
index a7eb15f7c6be6b2babb8fd2a0fc9496daf55cac6..8c80f4bdb9014596222082f3450071773146bc34 100644 (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) {}
   }
 }
 
index 32151b71ea17d759c2a97863cddf52c60ca9187a..60fa202384dee34a3f4cab8ffc0e1dcf5883a46f 100644 (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
   }
 }