Skip to content

Commit

Permalink
Improve diagnostic for ignored GNU 'used' attribute
Browse files Browse the repository at this point in the history
Differential Revision: https://reviews.llvm.org/D97161
  • Loading branch information
MaskRay committed Feb 22, 2021
1 parent 8c4638c commit bccdf6b
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 5 deletions.
3 changes: 3 additions & 0 deletions clang/include/clang/Basic/DiagnosticSemaKinds.td
Original file line number Diff line number Diff line change
Expand Up @@ -3159,6 +3159,9 @@ def warn_attribute_ignored : Warning<"%0 attribute ignored">,
def warn_nothrow_attribute_ignored : Warning<"'nothrow' attribute conflicts with"
" exception specification; attribute ignored">,
InGroup<IgnoredAttributes>;
def warn_attribute_ignored_on_non_definition :
Warning<"%0 attribute ignored on a non-definition declaration">,
InGroup<IgnoredAttributes>;
def warn_attribute_ignored_on_inline :
Warning<"%0 attribute ignored on inline function">,
InGroup<IgnoredAttributes>;
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/Sema/SemaDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13295,7 +13295,8 @@ void Sema::FinalizeDeclaration(Decl *ThisDecl) {

if (UsedAttr *Attr = VD->getAttr<UsedAttr>()) {
if (!Attr->isInherited() && !VD->isThisDeclarationADefinition()) {
Diag(Attr->getLocation(), diag::warn_attribute_ignored) << Attr;
Diag(Attr->getLocation(), diag::warn_attribute_ignored_on_non_definition)
<< Attr;
VD->dropAttr<UsedAttr>();
}
}
Expand Down
4 changes: 2 additions & 2 deletions clang/test/Sema/attr-used.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// RUN: %clang_cc1 -verify -fsyntax-only -Wno-private-extern %s

extern int l0 __attribute__((used)); // expected-warning {{'used' attribute ignored}}
__private_extern__ int l1 __attribute__((used)); // expected-warning {{'used' attribute ignored}}
extern int l0 __attribute__((used)); // expected-warning {{'used' attribute ignored on a non-definition declaration}}
__private_extern__ int l1 __attribute__((used)); // expected-warning {{'used' attribute ignored on a non-definition declaration}}

struct __attribute__((used)) s { // expected-warning {{'used' attribute only applies to variables with non-local storage, functions, and Objective-C methods}}
int x;
Expand Down
4 changes: 2 additions & 2 deletions clang/test/SemaCXX/attr-used.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// RUN: %clang_cc1 -fsyntax-only -verify %s

extern char test1[] __attribute__((used)); // expected-warning {{'used' attribute ignored}}
extern const char test2[] __attribute__((used)); // expected-warning {{'used' attribute ignored}}
extern char test1[] __attribute__((used)); // expected-warning {{'used' attribute ignored on a non-definition declaration}}
extern const char test2[] __attribute__((used)); // expected-warning {{'used' attribute ignored on a non-definition declaration}}
extern const char test3[] __attribute__((used)) = "";

0 comments on commit bccdf6b

Please sign in to comment.