[cfe-commits] r117780 - in /cfe/trunk: lib/AST/Decl.cpp test/CodeGenCXX/visibility.cpp

John McCall rjmccall at apple.com
Sat Oct 30 02:18:50 PDT 2010


Author: rjmccall
Date: Sat Oct 30 04:18:49 2010
New Revision: 117780

URL: http://llvm.org/viewvc/llvm-project?rev=117780&view=rev
Log:
GCC faithfully calculates visibility for variables independently of
whether it's a declaration or not, then ignores that information for
declarations unless it was explicitly given.  It's not totally clear
how that should be mapped into a sane system, but make an effort.


Modified:
    cfe/trunk/lib/AST/Decl.cpp
    cfe/trunk/test/CodeGenCXX/visibility.cpp

Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=117780&r1=117779&r2=117780&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Sat Oct 30 04:18:49 2010
@@ -255,21 +255,17 @@
     //
     // Note that we don't want to make the variable non-external
     // because of this, but unique-external linkage suits us.
-    if (Context.getLangOptions().CPlusPlus && !ExplicitVisibility &&
-        !Var->isExternC()) {
+    if (Context.getLangOptions().CPlusPlus && !Var->isExternC()) {
       LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
       if (TypeLV.first != ExternalLinkage)
         return LVPair(UniqueExternalLinkage, DefaultVisibility);
-
-      // Otherwise, ignore type visibility for declarations.
-      if (!isDeclaration)
+      if (!isDeclaration && !ExplicitVisibility)
         LV.second = minVisibility(LV.second, TypeLV.second);
     }
 
     // Don't consider -fvisibility for pure declarations.
-    if (isDeclaration) {
+    if (isDeclaration)
       ConsiderGlobalVisibility = false;
-    }
 
     if (!Context.getLangOptions().CPlusPlus &&
         (Var->getStorageClass() == SC_Extern ||
@@ -524,24 +520,24 @@
 
   // Static data members.
   } else if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
-    // If we don't have explicit visibility information in the
-    // hierarchy, apply the LV from its type.  See the comment about
-    // namespace-scope variables for justification for this
-    // optimization.
-    if (!HasExplicitVisibility) {
-      LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
-      if (TypeLV.first != ExternalLinkage)
-        LV.first = minLinkage(LV.first, UniqueExternalLinkage);
-      LV.second = minVisibility(LV.second, TypeLV.second);
+    bool IsDefinition = (VD->getDefinition() &&
+                         VD->getTemplateSpecializationKind()
+                           != TSK_ExplicitInstantiationDeclaration);
+
+    // GCC just ignores the visibility of a variable declaration
+    // unless it's explicit.
+    if (!IsDefinition && !HasExplicitVisibility) {
+      LV.second = DefaultVisibility;
+      ConsiderGlobalVisibility = false;
     }
 
-    // Ignore global visibility if it's an extern template or
-    // just a declaration.
-    if (ConsiderGlobalVisibility)
-      ConsiderGlobalVisibility =
-        (VD->getDefinition() &&
-         VD->getTemplateSpecializationKind()
-           != TSK_ExplicitInstantiationDeclaration);
+    // Modify the variable's linkage by its type, but ignore the
+    // type's visibility unless it's a definition.
+    LVPair TypeLV = VD->getType()->getLinkageAndVisibility();
+    if (TypeLV.first != ExternalLinkage)
+      LV.first = minLinkage(LV.first, UniqueExternalLinkage);
+    if (IsDefinition && !HasExplicitVisibility)
+      LV.second = minVisibility(LV.second, TypeLV.second);
   }
 
   // Suppress -fvisibility if we have explicit visibility on any of

Modified: cfe/trunk/test/CodeGenCXX/visibility.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/visibility.cpp?rev=117780&r1=117779&r2=117780&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/visibility.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/visibility.cpp Sat Oct 30 04:18:49 2010
@@ -20,6 +20,8 @@
 // CHECK-HIDDEN: @_ZN6Test131C1aE = global
 // CHECK: @_ZN6Test143varE = external global
 // CHECK-HIDDEN: @_ZN6Test143varE = external global
+// CHECK: @_ZN6Test154TempINS_1AEE5Inner6bufferE = external global [0 x i8]
+// CHECK-HIDDEN: @_ZN6Test154TempINS_1AEE5Inner6bufferE = external global [0 x i8]
 // CHECK: @_ZTVN5Test63fooE = weak_odr hidden constant 
 
 namespace Test1 {
@@ -218,3 +220,17 @@
 
   struct A *test() { return var; }
 }
+
+// rdar://problem/8613093
+namespace Test15 {
+  struct A {};
+  template <class T> struct Temp {
+    struct Inner {
+      static char buffer[0];
+    };
+  };
+
+  char *test() {
+    return Temp<A>::Inner::buffer;
+  }
+}





More information about the cfe-commits mailing list