aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/clang/Basic/DiagnosticSemaKinds.td2
-rw-r--r--lib/Sema/SemaDeclAttr.cpp13
-rw-r--r--test/Sema/attr-weak.c2
3 files changed, 17 insertions, 0 deletions
diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td
index f552a2faf0..9b9f96e5dc 100644
--- a/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/include/clang/Basic/DiagnosticSemaKinds.td
@@ -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<
diff --git a/lib/Sema/SemaDeclAttr.cpp b/lib/Sema/SemaDeclAttr.cpp
index f5babc19be..09cb96b900 100644
--- a/lib/Sema/SemaDeclAttr.cpp
+++ b/lib/Sema/SemaDeclAttr.cpp
@@ -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)
diff --git a/test/Sema/attr-weak.c b/test/Sema/attr-weak.c
index b79e1e7dfc..4e288673fe 100644
--- a/test/Sema/attr-weak.c
+++ b/test/Sema/attr-weak.c
@@ -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}}
+