]> granicus.if.org Git - clang/commitdiff
Create a subject list for the `used` attribute rather than use custom checking logic.
authorAaron Ballman <aaron@aaronballman.com>
Sat, 3 Mar 2018 21:02:09 +0000 (21:02 +0000)
committerAaron Ballman <aaron@aaronballman.com>
Sat, 3 Mar 2018 21:02:09 +0000 (21:02 +0000)
This changes the diagnostic wording somewhat, but otherwise intends no functional change to the attribute.

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

include/clang/Basic/Attr.td
lib/Sema/SemaDeclAttr.cpp
test/Sema/attr-used.c

index cf3208e2a330c11c76067b6aafe60cbcd19f8662..5969c161f78ced8d83447def95e3a7340afa1d0c 100644 (file)
@@ -83,6 +83,9 @@ def LocalVar : SubsetSubject<Var,
 def NonParmVar : SubsetSubject<Var,
                                [{S->getKind() != Decl::ParmVar}],
                                "variables">;
+def NonLocalVar : SubsetSubject<Var,
+                                [{!S->hasLocalStorage()}],
+                                "variables with non-local storage">;
 def NonBitField : SubsetSubject<Field,
                                 [{!S->isBitField()}],
                                 "non-bit-field non-static data members">;
@@ -2007,6 +2010,7 @@ def Unused : InheritableAttr {
 
 def Used : InheritableAttr {
   let Spellings = [GCC<"used">];
+  let Subjects = SubjectList<[Function, ObjCMethod, NonLocalVar]>;
   let Documentation = [Undocumented];
 }
 
index fee61e19d5d20a063f22929caa7fb098eb738296..a58726fae5fa928acd95aed54c52ec0e6d2ebc10 100644 (file)
@@ -2086,23 +2086,6 @@ static void handleDisableTailCallsAttr(Sema &S, Decl *D,
       AL.getRange(), S.Context, AL.getAttributeSpellingListIndex()));
 }
 
-static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &AL) {
-  if (const auto *VD = dyn_cast<VarDecl>(D)) {
-    if (VD->hasLocalStorage()) {
-      S.Diag(AL.getLoc(), diag::warn_attribute_ignored) << AL.getName();
-      return;
-    }
-  } else if (!isFunctionOrMethod(D)) {
-    S.Diag(AL.getLoc(), diag::warn_attribute_wrong_decl_type)
-      << AL.getName() << ExpectedVariableOrFunction;
-    return;
-  }
-
-  D->addAttr(::new (S.Context)
-             UsedAttr(AL.getRange(), S.Context,
-                      AL.getAttributeSpellingListIndex()));
-}
-
 static void handleUnusedAttr(Sema &S, Decl *D, const AttributeList &AL) {
   bool IsCXX17Attr = AL.isCXX11Attribute() && !AL.getScopeName();
 
@@ -6248,7 +6231,7 @@ static void ProcessDeclAttribute(Sema &S, Scope *scope, Decl *D,
     handleDisableTailCallsAttr(S, D, AL);
     break;
   case AttributeList::AT_Used:
-    handleUsedAttr(S, D, AL);
+    handleSimpleAttribute<UsedAttr>(S, D, AL);
     break;
   case AttributeList::AT_Visibility:
     handleVisibilityAttr(S, D, AL, false);
index 4e3bda7609bb10d740bebb28a268914ce2bd3ce2..344c772301c4fccfa1ad7d709d28e8df3ce382dc 100644 (file)
@@ -3,7 +3,7 @@
 extern int l0 __attribute__((used)); // expected-warning {{'used' attribute ignored}}
 __private_extern__ int l1 __attribute__((used)); // expected-warning {{'used' attribute ignored}}
 
-struct __attribute__((used)) s { // expected-warning {{'used' attribute only applies to variables and functions}}
+struct __attribute__((used)) s { // expected-warning {{'used' attribute only applies to functions, Objective-C methods, and variables with non-local storage}}
   int x;
 };
 
@@ -14,7 +14,7 @@ static void __attribute__((used)) f0(void) {
 
 void f1() {
   static int a __attribute__((used));
-  int b __attribute__((used)); // expected-warning {{'used' attribute ignored}}
+  int b __attribute__((used)); // expected-warning {{'used' attribute only applies to functions, Objective-C methods, and variables with non-local storage}}
 }
 
 static void __attribute__((used)) f0(void);