]> granicus.if.org Git - clang/commitdiff
Generate error on declaration containing 'static' and '__attribute__((weak))'
authorFariborz Jahanian <fjahanian@apple.com>
Thu, 16 Jul 2009 01:12:24 +0000 (01:12 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Thu, 16 Jul 2009 01:12:24 +0000 (01:12 +0000)
Patch by Ryan Flynn

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

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDeclAttr.cpp
test/Sema/attr-weak.c

index f552a2faf04ce26434e94a928eee35bead977d07..9b9f96e5dc9e05cfd14b9c3d206fe5c25da05583 100644 (file)
@@ -488,6 +488,8 @@ def warn_attribute_weak_on_field : Warning<
   "__weak attribute cannot be specified on a field declaration">;
 def warn_attribute_weak_on_local : Warning<
   "__weak attribute cannot be specified on an automatic variable">;
+def err_attribute_weak_static : Error<
+  "weak declaration of '%0' must be public">;
 def warn_attribute_weak_import_invalid_on_definition : Warning<
   "'weak_import' attribute cannot be specified on a definition">;
 def warn_attribute_wrong_decl_type : Warning<
index f5babc19be1e28f4e38e957ac3eaee3e0badd5bb..09cb96b900b6f00d14fa8000f6172fe500681da5 100644 (file)
@@ -796,6 +796,19 @@ static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
     return;
   }
 
+  /* weak only applies to non-static declarations */
+  bool isStatic = false;
+  if (VarDecl *VD = dyn_cast<VarDecl>(D)) {
+    isStatic = VD->getStorageClass() == VarDecl::Static;
+  } else if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D)) {
+    isStatic = FD->getStorageClass() == FunctionDecl::Static;
+  }
+  if (isStatic) {
+    S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
+      dyn_cast<NamedDecl>(D)->getNameAsString();
+    return;
+  }
+
   // TODO: could also be applied to methods?
   if (!isa<FunctionDecl>(D) && !isa<VarDecl>(D)) {
     S.Diag(Attr.getLoc(), diag::warn_attribute_wrong_decl_type)
index b79e1e7dfca781969c56f64d2cc7b73bf49ef828..4e288673feb6cf59b9cb2acf98ad6417c4eb434f 100644 (file)
@@ -11,3 +11,5 @@ int __attribute__((weak_import)) g5(void) {
 struct __attribute__((weak)) s0 {}; // expected-warning {{'weak' attribute only applies to variable and function types}}
 struct __attribute__((weak_import)) s1 {}; // expected-warning {{'weak_import' attribute only applies to variable and function types}}
 
+static int x __attribute__((weak)); // expected-error {{weak declaration of 'x' must be public}}
+