]> granicus.if.org Git - clang/commitdiff
Don't reject attribute used in an "extern const" variable definition.
authorRafael Espindola <rafael.espindola@gmail.com>
Fri, 16 Aug 2013 23:18:50 +0000 (23:18 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Fri, 16 Aug 2013 23:18:50 +0000 (23:18 +0000)
Before this patch we would warn and drop the attribute in
extern const char test3[] __attribute__((used)) = "";

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

lib/Sema/SemaDecl.cpp
lib/Sema/SemaDeclAttr.cpp
test/SemaCXX/attr-used.cpp [new file with mode: 0644]

index 9d4280ae9b9ce21d9c9ff647247f13189ea29920..e07d332d523bd5e58b8db82c0de6c03674077751 100644 (file)
@@ -8795,6 +8795,13 @@ Sema::FinalizeDeclaration(Decl *ThisDecl) {
   if (!VD)
     return;
 
+  if (UsedAttr *Attr = VD->getAttr<UsedAttr>()) {
+    if (!Attr->isInherited() && !VD->isThisDeclarationADefinition()) {
+      Diag(Attr->getLocation(), diag::warn_attribute_ignored) << "used";
+      VD->dropAttr<UsedAttr>();
+    }
+  }
+
   const DeclContext *DC = VD->getDeclContext();
   // If there's a #pragma GCC visibility in scope, and this isn't a class
   // member, set the visibility of this variable.
index 77b242d19f67895324fd27d6ba89acad5cc325e4..a82b3969075d92cac548dd6c5df7246e742f1a33 100644 (file)
@@ -1944,7 +1944,7 @@ static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
   }
 
   if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
-    if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
+    if (VD->hasLocalStorage()) {
       S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
       return;
     }
diff --git a/test/SemaCXX/attr-used.cpp b/test/SemaCXX/attr-used.cpp
new file mode 100644 (file)
index 0000000..9bae3ed
--- /dev/null
@@ -0,0 +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 const char test3[] __attribute__((used)) = "";