<div dir="auto">Is it easy to mention variables first in the diag? That's probably the most common subject for this attribute. (If it's not easy, nevermind -- definitely a polish thing.)</div><br><div class="gmail_quote"><div dir="ltr">On Sat, Mar 3, 2018, 4:04 PM Aaron Ballman via cfe-commits <<a href="mailto:cfe-commits@lists.llvm.org">cfe-commits@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Author: aaronballman<br>
Date: Sat Mar  3 13:02:09 2018<br>
New Revision: 326665<br>
<br>
URL: <a href="http://llvm.org/viewvc/llvm-project?rev=326665&view=rev" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project?rev=326665&view=rev</a><br>
Log:<br>
Create a subject list for the `used` attribute rather than use custom checking logic.<br>
<br>
This changes the diagnostic wording somewhat, but otherwise intends no functional change to the attribute.<br>
<br>
Modified:<br>
    cfe/trunk/include/clang/Basic/Attr.td<br>
    cfe/trunk/lib/Sema/SemaDeclAttr.cpp<br>
    cfe/trunk/test/Sema/attr-used.c<br>
<br>
Modified: cfe/trunk/include/clang/Basic/Attr.td<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Attr.td?rev=326665&r1=326664&r2=326665&view=diff" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/Attr.td?rev=326665&r1=326664&r2=326665&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/include/clang/Basic/Attr.td (original)<br>
+++ cfe/trunk/include/clang/Basic/Attr.td Sat Mar  3 13:02:09 2018<br>
@@ -83,6 +83,9 @@ def LocalVar : SubsetSubject<Var,<br>
 def NonParmVar : SubsetSubject<Var,<br>
                                [{S->getKind() != Decl::ParmVar}],<br>
                                "variables">;<br>
+def NonLocalVar : SubsetSubject<Var,<br>
+                                [{!S->hasLocalStorage()}],<br>
+                                "variables with non-local storage">;<br>
 def NonBitField : SubsetSubject<Field,<br>
                                 [{!S->isBitField()}],<br>
                                 "non-bit-field non-static data members">;<br>
@@ -2007,6 +2010,7 @@ def Unused : InheritableAttr {<br>
<br>
 def Used : InheritableAttr {<br>
   let Spellings = [GCC<"used">];<br>
+  let Subjects = SubjectList<[Function, ObjCMethod, NonLocalVar]>;<br>
   let Documentation = [Undocumented];<br>
 }<br>
<br>
<br>
Modified: cfe/trunk/lib/Sema/SemaDeclAttr.cpp<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=326665&r1=326664&r2=326665&view=diff" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDeclAttr.cpp?rev=326665&r1=326664&r2=326665&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/lib/Sema/SemaDeclAttr.cpp (original)<br>
+++ cfe/trunk/lib/Sema/SemaDeclAttr.cpp Sat Mar  3 13:02:09 2018<br>
@@ -2086,23 +2086,6 @@ static void handleDisableTailCallsAttr(S<br>
       AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));<br>
 }<br>
<br>
-static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &AL) {<br>
-  if (const auto *VD = dyn_cast<VarDecl>(D)) {<br>
-    if (VD->hasLocalStorage()) {<br>
-      S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL.getName();<br>
-      return;<br>
-    }<br>
-  } else if (!isFunctionOrMethod(D)) {<br>
-    S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)<br>
-      << AL.getName() << ExpectedVariableOrFunction;<br>
-    return;<br>
-  }<br>
-<br>
-  D->addAttr(::new (S.Context)<br>
-             UsedAttr(AL.getRange(), S.Context,<br>
-                      AL.getAttributeSpellingListIndex()));<br>
-}<br>
-<br>
 static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &AL) {<br>
   bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName();<br>
<br>
@@ -6248,7 +6231,7 @@ static void ProcessDeclAttribute(Sema &S<br>
     handleDisableTailCallsAttr(S, D, AL);<br>
     break;<br>
   case AttributeList::AT_Used:<br>
-    handleUsedAttr(S, D, AL);<br>
+    handleSimpleAttribute<UsedAttr>(S, D, AL);<br>
     break;<br>
   case AttributeList::AT_Visibility:<br>
     handleVisibilityAttr(S, D, AL, false);<br>
<br>
Modified: cfe/trunk/test/Sema/attr-used.c<br>
URL: <a href="http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-used.c?rev=326665&r1=326664&r2=326665&view=diff" rel="noreferrer noreferrer" target="_blank">http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-used.c?rev=326665&r1=326664&r2=326665&view=diff</a><br>
==============================================================================<br>
--- cfe/trunk/test/Sema/attr-used.c (original)<br>
+++ cfe/trunk/test/Sema/attr-used.c Sat Mar  3 13:02:09 2018<br>
@@ -3,7 +3,7 @@<br>
 extern int l0 __attribute__((used)); // expected-warning {{'used' attribute ignored}}<br>
 __private_extern__ int l1 __attribute__((used)); // expected-warning {{'used' attribute ignored}}<br>
<br>
-struct __attribute__((used)) s { // expected-warning {{'used' attribute only applies to variables and functions}}<br>
+struct __attribute__((used)) s { // expected-warning {{'used' attribute only applies to functions, Objective-C methods, and variables with non-local storage}}<br>
   int x;<br>
 };<br>
<br>
@@ -14,7 +14,7 @@ static void __attribute__((used)) f0(voi<br>
<br>
 void f1() {<br>
   static int a __attribute__((used));<br>
-  int b __attribute__((used)); // expected-warning {{'used' attribute ignored}}<br>
+  int b __attribute__((used)); // expected-warning {{'used' attribute only applies to functions, Objective-C methods, and variables with non-local storage}}<br>
 }<br>
<br>
 static void __attribute__((used)) f0(void);<br>
<br>
<br>
_______________________________________________<br>
cfe-commits mailing list<br>
<a href="mailto:cfe-commits@lists.llvm.org" target="_blank" rel="noreferrer">cfe-commits@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits" rel="noreferrer noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits</a><br>
</blockquote></div>