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

GCC warns on these cases, but we currently just silently ignore the attribute.

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

llvm-svn: 359814
This commit is contained in:
Scott Linder 2019-05-02 19:03:57 +00:00
parent 21db1440f9
commit daa3c5b132
5 changed files with 23 additions and 2 deletions

View File

@ -2778,6 +2778,9 @@ def warn_attribute_ignored : Warning<"%0 attribute ignored">,
def warn_attribute_ignored_on_inline : def warn_attribute_ignored_on_inline :
Warning<"%0 attribute ignored on inline function">, Warning<"%0 attribute ignored on inline function">,
InGroup<IgnoredAttributes>; 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 : def warn_nocf_check_attribute_ignored :
Warning<"'nocf_check' attribute ignored; use -fcf-protection to enable the attribute">, Warning<"'nocf_check' attribute ignored; use -fcf-protection to enable the attribute">,
InGroup<IgnoredAttributes>; InGroup<IgnoredAttributes>;

View File

@ -2615,6 +2615,14 @@ static void handleVisibilityAttr(Sema &S, Decl *D, const ParsedAttr &AL,
return; 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. // Check that the argument is a string literal.
StringRef TypeStr; StringRef TypeStr;
SourceLocation LiteralLoc; SourceLocation LiteralLoc;

View 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 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}} 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}}

View File

@ -209,10 +209,8 @@ void test(int i) {
} }
} }
namespace {
// CHECK: struct {{\[\[gnu::visibility\(\"hidden\"\)\]\]}} S; // CHECK: struct {{\[\[gnu::visibility\(\"hidden\"\)\]\]}} S;
struct [[gnu::visibility("hidden")]] S; struct [[gnu::visibility("hidden")]] S;
}
// CHECK: struct CXXFunctionalCastExprPrint { // CHECK: struct CXXFunctionalCastExprPrint {
// CHECK-NEXT: } fce = CXXFunctionalCastExprPrint{}; // CHECK-NEXT: } fce = CXXFunctionalCastExprPrint{};

View File

@ -18,3 +18,9 @@ void foo<int>() {
struct x3 { struct x3 {
static int y; static int y;
} __attribute((visibility("default"))); // expected-warning {{attribute 'visibility' after definition is ignored}} } __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}}
};