From f1d2f0ea21a5a66864da5cd8d39404df5b3fd652 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Wed, 16 Jan 2013 23:11:15 +0000 Subject: [PATCH] Check for internal weak decls after merging. This fixes pr14946. The problem was that the linkage computation was done too early, so things like "extern int a;" would be given external linkage, even if a previous declaration was static. llvm-svn: 172667 --- clang/lib/Sema/SemaDecl.cpp | 13 +++++++++++++ clang/lib/Sema/SemaDeclAttr.cpp | 6 ------ clang/test/Sema/attr-weak.c | 6 ++++++ clang/test/SemaCXX/attr-weak.cpp | 7 +++++++ 4 files changed, 26 insertions(+), 6 deletions(-) diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 994b212..0316654 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -4312,6 +4312,15 @@ bool Sema::inferObjCARCLifetime(ValueDecl *decl) { return false; } +static void checkAttributesAfterMerging(Sema &S, NamedDecl &ND) { + // 'weak' only applies to declarations with external linkage. + WeakAttr *WA = ND.getAttr(); + if (WA && ND.getLinkage() != ExternalLinkage) { + S.Diag(WA->getLocation(), diag::err_attribute_weak_static); + ND.dropAttr(); + } +} + NamedDecl* Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC, TypeSourceInfo *TInfo, LookupResult &Previous, @@ -4590,6 +4599,8 @@ Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC, NewVD->setInvalidDecl(); } + checkAttributesAfterMerging(*this, *NewVD); + // If this is a locally-scoped extern C variable, update the map of // such variables. if (CurContext->isFunctionOrMethod() && NewVD->isExternC() && @@ -6056,6 +6067,8 @@ Sema::ActOnFunctionDeclarator(Scope *S, Declarator &D, DeclContext *DC, } } + checkAttributesAfterMerging(*this, *NewFD); + AddKnownFunctionAttributes(NewFD); if (NewFD->hasAttr() && diff --git a/clang/lib/Sema/SemaDeclAttr.cpp b/clang/lib/Sema/SemaDeclAttr.cpp index 85f48ec..efeafa6 100644 --- a/clang/lib/Sema/SemaDeclAttr.cpp +++ b/clang/lib/Sema/SemaDeclAttr.cpp @@ -2511,12 +2511,6 @@ static void handleWeakAttr(Sema &S, Decl *D, const AttributeList &Attr) { NamedDecl *nd = cast(D); - // 'weak' only applies to declarations with external linkage. - if (hasEffectivelyInternalLinkage(nd)) { - S.Diag(Attr.getLoc(), diag::err_attribute_weak_static); - return; - } - nd->addAttr(::new (S.Context) WeakAttr(Attr.getRange(), S.Context)); } diff --git a/clang/test/Sema/attr-weak.c b/clang/test/Sema/attr-weak.c index adedf12..df74554 100644 --- a/clang/test/Sema/attr-weak.c +++ b/clang/test/Sema/attr-weak.c @@ -16,3 +16,9 @@ static int x __attribute__((weak)); // expected-error {{weak declaration cannot // rdar://9538608 int C; // expected-note {{previous definition is here}} extern int C __attribute__((weak_import)); // expected-warning {{an already-declared variable is made a weak_import declaration}} + +static int pr14946_x; +extern int pr14946_x __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}} + +static void pr14946_f(); +void pr14946_f() __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}} diff --git a/clang/test/SemaCXX/attr-weak.cpp b/clang/test/SemaCXX/attr-weak.cpp index b6a9e0a..8939a28 100644 --- a/clang/test/SemaCXX/attr-weak.cpp +++ b/clang/test/SemaCXX/attr-weak.cpp @@ -21,9 +21,16 @@ namespace { }; } +// GCC rejects the instantiation with the internal type, but some existing +// code expects it. It is also not that different from giving hidden visibility +// to parts of a template that have explicit default visibility, so we accept +// this. template struct Test7 { void test7() __attribute__((weak)) {} + static int var __attribute__((weak)); }; +template +int Test7::var; namespace { class Internal; } template struct Test7; template struct Test7; -- 2.7.4