Skip to content

Commit 33e0226

Browse files
author
Fariborz Jahanian
committed
Issue warning if weak_import attribute is added to an already
declared variable and ignore it. // rdar://9538608 llvm-svn: 133654
1 parent a60a269 commit 33e0226

File tree

4 files changed

+16
-5
lines changed

4 files changed

+16
-5
lines changed

clang/include/clang/Basic/DiagnosticSemaKinds.td

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2287,6 +2287,8 @@ def err_inline_declaration_block_scope : Error<
22872287
"inline declaration of %0 not allowed in block scope">;
22882288
def err_static_non_static : Error<
22892289
"static declaration of %0 follows non-static declaration">;
2290+
def warn_weak_import : Warning <
2291+
"an already-declared variable is made a weak_import declaration %0">;
22902292
def warn_static_non_static : ExtWarn<
22912293
"static declaration of %0 follows non-static declaration">;
22922294
def err_non_static_static : Error<

clang/lib/Sema/SemaDecl.cpp

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2039,12 +2039,17 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) {
20392039
}
20402040

20412041
mergeDeclAttributes(New, Old, Context);
2042-
// weak_import on current declaration is applied to previous
2043-
// tentative definiton.
2042+
// Warn if an already-declared variable is made a weak_import in a subsequent declaration
20442043
if (New->getAttr<WeakImportAttr>() &&
20452044
Old->getStorageClass() == SC_None &&
2046-
!Old->getAttr<WeakImportAttr>())
2047-
Old->addAttr(::new (Context) WeakImportAttr(SourceLocation(), Context));
2045+
!Old->getAttr<WeakImportAttr>()) {
2046+
Diag(New->getLocation(), diag::warn_weak_import) << New->getDeclName();
2047+
Diag(Old->getLocation(), diag::note_previous_definition);
2048+
// Remove weak_import attribute on new declaration.
2049+
// I am just dropping all attributes in curernt decl. We have
2050+
// already issued a warning, so we are OK.
2051+
New->dropAttrs();
2052+
}
20482053

20492054
// Merge the types.
20502055
MergeVarDeclTypes(New, Old);

clang/test/CodeGen/attr-weak-import.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ extern int E __attribute__((weak_import));
2020

2121
// CHECK: @A = global i32
2222
// CHECK-NOT: @B =
23-
// CHECK: @C = global i32
23+
// CHECK: @C = common global i32
2424
// CHECK: @D = global i32
2525
// CHECK: @E = global i32
2626

clang/test/Sema/attr-weak.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,7 @@ struct __attribute__((weak)) s0 {}; // expected-warning {{'weak' attribute only
1212
struct __attribute__((weak_import)) s1 {}; // expected-warning {{'weak_import' attribute only applies to variables and functions}}
1313

1414
static int x __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}
15+
16+
// rdar://9538608
17+
int C; // expected-note {{previous definition is here}}
18+
extern int C __attribute__((weak_import)); // expected-warning {{an already-declared variable is made a weak_import declaration}}

0 commit comments

Comments
 (0)