def warn_weak_identifier_undeclared : Warning<
"weak identifier %0 never declared">;
def err_attribute_weak_static : Error<
- "weak declaration of '%0' must be public">;
+ "weak declaration cannot have internal linkage">;
def warn_attribute_weak_import_invalid_on_definition : Warning<
"'weak_import' attribute cannot be specified on a definition">;
def err_attribute_weakref_not_static : Error<
- "weakref declaration of '%0' must be static">;
+ "weakref declaration must have internal linkage">;
def err_attribute_weakref_not_global_context : Error<
"weakref declaration of '%0' must be in a global context">;
def err_attribute_weakref_without_alias : Error<
start, size));
}
-static bool isStaticVarOrStaticFunction(Decl *D) {
- if (VarDecl *VD = dyn_cast<VarDecl>(D))
- return VD->getStorageClass() == SC_Static;
- if (FunctionDecl *FD = dyn_cast<FunctionDecl>(D))
- return FD->getStorageClass() == SC_Static;
+/// Whether this declaration has internal linkage for the purposes of
+/// things that want to complain about things not have internal linkage.
+static bool hasEffectivelyInternalLinkage(NamedDecl *D) {
+ switch (D->getLinkage()) {
+ case NoLinkage:
+ case InternalLinkage:
+ return true;
+
+ // Template instantiations that go from external to unique-external
+ // shouldn't get diagnosed.
+ case UniqueExternalLinkage:
+ return true;
+
+ case ExternalLinkage:
+ return false;
+ }
+ llvm_unreachable("unknown linkage kind!");
return false;
}
return;
}
+ if (!isa<VarDecl>(d) && !isa<FunctionDecl>(d)) {
+ S.Diag(Attr.getLoc(), diag::err_attribute_wrong_decl_type)
+ << Attr.getName() << 2 /*variables and functions*/;
+ return;
+ }
+
+ NamedDecl *nd = cast<NamedDecl>(d);
+
// gcc rejects
// class c {
// static int a __attribute__((weakref ("v2")));
const DeclContext *Ctx = d->getDeclContext()->getRedeclContext();
if (!Ctx->isFileContext()) {
S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_global_context) <<
- dyn_cast<NamedDecl>(d)->getNameAsString();
+ nd->getNameAsString();
return;
}
// This looks like a bug in gcc. We reject that for now. We should revisit
// it if this behaviour is actually used.
- if (!isStaticVarOrStaticFunction(d)) {
- S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static) <<
- dyn_cast<NamedDecl>(d)->getNameAsString();
+ if (!hasEffectivelyInternalLinkage(nd)) {
+ S.Diag(Attr.getLoc(), diag::err_attribute_weakref_not_static);
return;
}
D->addAttr(::new (S.Context) WarnUnusedResultAttr(Attr.getLoc(), S.Context));
}
-static void HandleWeakAttr(Decl *D, const AttributeList &Attr, Sema &S) {
+static void HandleWeakAttr(Decl *d, const AttributeList &attr, Sema &S) {
// check the attribute arguments.
- if (Attr.getNumArgs() != 0) {
- S.Diag(Attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
+ if (attr.getNumArgs() != 0) {
+ S.Diag(attr.getLoc(), diag::err_attribute_wrong_number_arguments) << 0;
return;
}
- /* weak only applies to non-static declarations */
- if (isStaticVarOrStaticFunction(D)) {
- S.Diag(Attr.getLoc(), diag::err_attribute_weak_static) <<
- dyn_cast<NamedDecl>(D)->getNameAsString();
+ if (!isa<VarDecl>(d) && !isa<FunctionDecl>(d)) {
+ S.Diag(attr.getLoc(), diag::warn_attribute_wrong_decl_type)
+ << attr.getName() << 2 /*variables and functions*/;
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)
- << Attr.getName() << 2 /*variable and function*/;
+ NamedDecl *nd = cast<NamedDecl>(d);
+
+ // 'weak' only applies to declarations with external linkage.
+ if (hasEffectivelyInternalLinkage(nd)) {
+ S.Diag(attr.getLoc(), diag::err_attribute_weak_static);
return;
}
- D->addAttr(::new (S.Context) WeakAttr(Attr.getLoc(), S.Context));
+ nd->addAttr(::new (S.Context) WeakAttr(attr.getLoc(), S.Context));
}
static void HandleWeakImportAttr(Decl *D, const AttributeList &Attr, Sema &S) {
--- /dev/null
+// RUN: %clang_cc1 -triple x86_64-pc-linux-gnu -fsyntax-only -verify %s
+
+static int test0 __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
+static void test1() __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
+
+namespace test2 __attribute__((weak)) { // expected-warning {{'weak' attribute only applies to variables and functions}}
+}
+
+namespace {
+ int test3 __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
+ void test4() __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
+}
+
+struct Test5 {
+ static void test5() __attribute__((weak)); // no error
+};
+
+namespace {
+ struct Test6 {
+ static void test6() __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
+ };
+}
+
+template <class T> struct Test7 {
+ void test7() __attribute__((weak)) {}
+};
+namespace { class Internal; }
+template struct Test7<Internal>;
+template struct Test7<int>;