[PATCH] D61097: [Sema] Emit warning for visibility attribute on internal-linkage declaration

Scott Linder via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu May 2 12:02:35 PDT 2019


This revision was automatically updated to reflect the committed changes.
Closed by commit rL359814: [Sema] Emit warning for visibility attribute on internal-linkage declaration (authored by scott.linder, committed by ).
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Changed prior to commit:
  https://reviews.llvm.org/D61097?vs=197570&id=197838#toc

Repository:
  rL LLVM

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D61097/new/

https://reviews.llvm.org/D61097

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


Index: cfe/trunk/lib/Sema/SemaDeclAttr.cpp
===================================================================
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp
@@ -2615,6 +2615,14 @@
     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: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
===================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
@@ -2778,6 +2778,9 @@
 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: cfe/trunk/test/SemaCXX/ast-print.cpp
===================================================================
--- cfe/trunk/test/SemaCXX/ast-print.cpp
+++ cfe/trunk/test/SemaCXX/ast-print.cpp
@@ -209,10 +209,8 @@
 }
 }
 
-namespace {
 // CHECK: struct {{\[\[gnu::visibility\(\"hidden\"\)\]\]}} S;
 struct [[gnu::visibility("hidden")]] S;
-}
 
 // CHECK:      struct CXXFunctionalCastExprPrint {
 // CHECK-NEXT: } fce = CXXFunctionalCastExprPrint{};
Index: cfe/trunk/test/SemaCXX/attr-visibility.cpp
===================================================================
--- cfe/trunk/test/SemaCXX/attr-visibility.cpp
+++ cfe/trunk/test/SemaCXX/attr-visibility.cpp
@@ -18,3 +18,9 @@
 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}}
+};
Index: cfe/trunk/test/Sema/attr-visibility.c
===================================================================
--- cfe/trunk/test/Sema/attr-visibility.c
+++ cfe/trunk/test/Sema/attr-visibility.c
@@ -26,3 +26,9 @@
 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}}


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D61097.197838.patch
Type: text/x-patch
Size: 3489 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190502/610a416d/attachment.bin>


More information about the cfe-commits mailing list