]> granicus.if.org Git - clang/commitdiff
[Sema] Emit warning for visibility attribute on internal-linkage declaration
authorScott Linder <scott@scottlinder.com>
Thu, 2 May 2019 19:03:57 +0000 (19:03 +0000)
committerScott Linder <scott@scottlinder.com>
Thu, 2 May 2019 19:03:57 +0000 (19:03 +0000)
GCC warns on these cases, but we currently just silently ignore the attribute.

Differential Revision: https://reviews.llvm.org/D61097

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

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDeclAttr.cpp
test/Sema/attr-visibility.c
test/SemaCXX/ast-print.cpp
test/SemaCXX/attr-visibility.cpp

index cccc9e4aa0646776876f1110349772e2b39468a4..229df8f25f796c8a865934e278b1eb553a3395ea 100644 (file)
@@ -2778,6 +2778,9 @@ def warn_attribute_ignored : Warning<"%0 attribute ignored">,
 def warn_attribute_ignored_on_inline :
   Warning<"%0 attribute ignored on inline function">,
   InGroup<IgnoredAttributes>;
+def warn_attribute_ignored_on_non_external :
+  Warning<"%0 attribute is ignored on a non-external symbol">,
+  InGroup<IgnoredAttributes>;
 def warn_nocf_check_attribute_ignored :
   Warning<"'nocf_check' attribute ignored; use -fcf-protection to enable the attribute">,
   InGroup<IgnoredAttributes>;
index f8a34573f3c828b2101842e000d8e09fa7f4a30b..1da852e7d4264f28591e5519f2a59040fffa4f5d 100644 (file)
@@ -2615,6 +2615,14 @@ static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL,
     return;
   }
 
+  // Visibility attributes have no effect on symbols with internal linkage.
+  if (const auto *ND = dyn_cast<NamedDecl>(D)) {
+    if (!ND->isExternallyVisible())
+      S.Diag(AL.getRange().getBegin(),
+             diag::warn_attribute_ignored_on_non_external)
+          << AL;
+  }
+
   // Check that the argument is a string literal.
   StringRef TypeStr;
   SourceLocation LiteralLoc;
index 798d6dcd78fce4db9b69875362b53c3e2432501f..792721a1acb73f94b88b7cd76bd297c5f2ea22e8 100644 (file)
@@ -26,3 +26,9 @@ typedef int __attribute__((visibility("default"))) bar; // expected-warning {{'v
 int x __attribute__((type_visibility("default"))); // expected-error {{'type_visibility' attribute only applies to types and namespaces}}
 
 int PR17105 __attribute__((visibility(hidden))); // expected-error {{'visibility' attribute requires a string}}
+
+static int test8 __attribute__((visibility("default"))); // expected-warning {{'visibility' attribute is ignored on a non-external symbol}}
+static int test9 __attribute__((visibility("hidden"))); // expected-warning {{'visibility' attribute is ignored on a non-external symbol}}
+static int test10 __attribute__((visibility("internal"))); // expected-warning {{'visibility' attribute is ignored on a non-external symbol}}
+
+static int test11() __attribute__((visibility("default"))); // expected-warning {{'visibility' attribute is ignored on a non-external symbol}}
index fd1d3fe84fac3875419ffb36bd6520d22ee52db0..6d53e604205840a696665117f27fcad0f1cfb357 100644 (file)
@@ -209,10 +209,8 @@ void test(int i) {
 }
 }
 
-namespace {
 // CHECK: struct {{\[\[gnu::visibility\(\"hidden\"\)\]\]}} S;
 struct [[gnu::visibility("hidden")]] S;
-}
 
 // CHECK:      struct CXXFunctionalCastExprPrint {
 // CHECK-NEXT: } fce = CXXFunctionalCastExprPrint{};
index 05aa5a33c72e83ec271d890a463f40743050a481..1610631a71151ab3c82d9060b42fd4b319ecf4ac 100644 (file)
@@ -18,3 +18,9 @@ void foo<int>() {
 struct x3 {
   static int y;
 } __attribute((visibility("default"))); // expected-warning {{attribute 'visibility' after definition is ignored}}
+
+const int test4 __attribute__((visibility("default"))) = 0; // expected-warning {{'visibility' attribute is ignored on a non-external symbol}}
+
+namespace {
+  int test5 __attribute__((visibility("default"))); // expected-warning {{'visibility' attribute is ignored on a non-external symbol}}
+};