[cfe-commits] r117526 - in /cfe/trunk: lib/AST/Decl.cpp test/CodeGen/visibility.c

John McCall rjmccall at apple.com
Wed Oct 27 21:18:25 PDT 2010


Author: rjmccall
Date: Wed Oct 27 23:18:25 2010
New Revision: 117526

URL: http://llvm.org/viewvc/llvm-project?rev=117526&view=rev
Log:
When computing visibility, use the latest declaration's explicit visibility
attribute.

Part of rdar://problem/8595231


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

Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=117526&r1=117525&r2=117526&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Wed Oct 27 23:18:25 2010
@@ -33,6 +33,23 @@
 // NamedDecl Implementation
 //===----------------------------------------------------------------------===//
 
+static const VisibilityAttr *GetExplicitVisibility(const Decl *D) {
+  // If the decl is redeclarable, make sure we use the explicit
+  // visibility attribute from the most recent declaration.
+  //
+  // Note that this isn't necessary for tags, which can't have their
+  // visibility adjusted.
+  if (isa<VarDecl>(D)) {
+    return cast<VarDecl>(D)->getMostRecentDeclaration()
+      ->getAttr<VisibilityAttr>();
+  } else if (isa<FunctionDecl>(D)) {
+    return cast<FunctionDecl>(D)->getMostRecentDeclaration()
+      ->getAttr<VisibilityAttr>();
+  } else {
+    return D->getAttr<VisibilityAttr>();
+  }
+}
+
 static Visibility GetVisibilityFromAttr(const VisibilityAttr *A) {
   switch (A->getVisibility()) {
   case VisibilityAttr::Default:
@@ -180,6 +197,8 @@
   if (D->isInAnonymousNamespace())
     return LVPair(UniqueExternalLinkage, DefaultVisibility);
 
+  const VisibilityAttr *ExplicitVisibility = GetExplicitVisibility(D);
+
   // Set up the defaults.
 
   // C99 6.2.2p5:
@@ -218,7 +237,7 @@
     // 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 && !Var->isExternC() &&
-        !Var->hasAttr<VisibilityAttr>()) {
+        !ExplicitVisibility) {
       LVPair TypeLV = Var->getType()->getLinkageAndVisibility();
       if (TypeLV.first != ExternalLinkage)
         return LVPair(UniqueExternalLinkage, DefaultVisibility);
@@ -252,7 +271,7 @@
     // Modify the function's LV by the LV of its type unless this is
     // C or extern "C".  See the comment above about variables.
     if (Context.getLangOptions().CPlusPlus && !Function->isExternC() &&
-        !Function->hasAttr<VisibilityAttr>()) {
+        !ExplicitVisibility) {
       LVPair TypeLV = Function->getType()->getLinkageAndVisibility();
       if (TypeLV.first != ExternalLinkage)
         return LVPair(UniqueExternalLinkage, DefaultVisibility);
@@ -376,9 +395,8 @@
     Visibility StandardV;
 
     // If we have an explicit visibility attribute, merge that in.
-    const VisibilityAttr *VA = D->getAttr<VisibilityAttr>();
-    if (VA)
-      StandardV = GetVisibilityFromAttr(VA);
+    if (ExplicitVisibility)
+      StandardV = GetVisibilityFromAttr(ExplicitVisibility);
     else if (ConsiderDashFVisibility)
       StandardV = Context.getLangOptions().getVisibilityMode();
     else
@@ -416,7 +434,7 @@
   LVPair LV = ClassLV;
 
   // If we have an explicit visibility attribute, merge that in.
-  const VisibilityAttr *VA = D->getAttr<VisibilityAttr>();
+  const VisibilityAttr *VA = GetExplicitVisibility(D);
   if (VA) LV.second = minVisibility(LV.second, GetVisibilityFromAttr(VA));
 
   // If it's a value declaration and we don't have an explicit visibility
@@ -510,7 +528,7 @@
         return LVPair(UniqueExternalLinkage, DefaultVisibility);
 
       LVPair LV(ExternalLinkage, DefaultVisibility);
-      if (const VisibilityAttr *VA = Function->getAttr<VisibilityAttr>())
+      if (const VisibilityAttr *VA = GetExplicitVisibility(Function))
         LV.second = GetVisibilityFromAttr(VA);
 
       if (const FunctionDecl *Prev = Function->getPreviousDeclaration()) {
@@ -531,7 +549,7 @@
         LVPair LV(ExternalLinkage, DefaultVisibility);
         if (Var->getStorageClass() == SC_PrivateExtern)
           LV.second = HiddenVisibility;
-        else if (const VisibilityAttr *VA = Var->getAttr<VisibilityAttr>())
+        else if (const VisibilityAttr *VA = GetExplicitVisibility(Var))
           LV.second = GetVisibilityFromAttr(VA);
 
         if (const VarDecl *Prev = Var->getPreviousDeclaration()) {

Modified: cfe/trunk/test/CodeGen/visibility.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/visibility.c?rev=117526&r1=117525&r2=117526&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/visibility.c (original)
+++ cfe/trunk/test/CodeGen/visibility.c Wed Oct 27 23:18:25 2010
@@ -46,3 +46,10 @@
 // CHECK-HIDDEN: define void @test1(
 struct Test1 { int field; };
 void  __attribute__((visibility("default"))) test1(struct Test1 *v) { }
+
+// rdar://problem/8595231
+// CHECK-DEFAULT: define void @test2()
+// CHECK-PROTECTED: define void @test2()
+// CHECK-HIDDEN: define void @test2()
+void test2(void);
+void __attribute__((visibility("default"))) test2(void) {}





More information about the cfe-commits mailing list