Skip to content

Commit 87198cd

Browse files
committed
Don't reject attribute used in an "extern const" variable definition.
Before this patch we would warn and drop the attribute in extern const char test3[] __attribute__((used)) = ""; llvm-svn: 188588
1 parent e19529b commit 87198cd

File tree

3 files changed

+13
-1
lines changed

3 files changed

+13
-1
lines changed

clang/lib/Sema/SemaDecl.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8795,6 +8795,13 @@ Sema::FinalizeDeclaration(Decl *ThisDecl) {
87958795
if (!VD)
87968796
return;
87978797

8798+
if (UsedAttr *Attr = VD->getAttr<UsedAttr>()) {
8799+
if (!Attr->isInherited() && !VD->isThisDeclarationADefinition()) {
8800+
Diag(Attr->getLocation(), diag::warn_attribute_ignored) << "used";
8801+
VD->dropAttr<UsedAttr>();
8802+
}
8803+
}
8804+
87988805
const DeclContext *DC = VD->getDeclContext();
87998806
// If there's a #pragma GCC visibility in scope, and this isn't a class
88008807
// member, set the visibility of this variable.

clang/lib/Sema/SemaDeclAttr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1944,7 +1944,7 @@ static void handleUsedAttr(Sema &S, Decl *D, const AttributeList &Attr) {
19441944
}
19451945

19461946
if (const VarDecl *VD = dyn_cast<VarDecl>(D)) {
1947-
if (VD->hasLocalStorage() || VD->hasExternalStorage()) {
1947+
if (VD->hasLocalStorage()) {
19481948
S.Diag(Attr.getLoc(), diag::warn_attribute_ignored) << "used";
19491949
return;
19501950
}

clang/test/SemaCXX/attr-used.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// RUN: %clang_cc1 -fsyntax-only -verify %s
2+
3+
extern char test1[] __attribute__((used)); // expected-warning {{used attribute ignored}}
4+
extern const char test2[] __attribute__((used)); // expected-warning {{used attribute ignored}}
5+
extern const char test3[] __attribute__((used)) = "";

0 commit comments

Comments
 (0)