Skip to content

Commit

Permalink
Issue warning if weak_import attribute is added to an already
Browse files Browse the repository at this point in the history
declared variable and ignore it. // rdar://9538608


git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@133654 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Fariborz Jahanian committed Jun 22, 2011
1 parent 9c7f469 commit d543130
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 5 deletions.
2 changes: 2 additions & 0 deletions include/clang/Basic/DiagnosticSemaKinds.td
Expand Up @@ -2287,6 +2287,8 @@ def err_inline_declaration_block_scope : Error<
"inline declaration of %0 not allowed in block scope">;
def err_static_non_static : Error<
"static declaration of %0 follows non-static declaration">;
def warn_weak_import : Warning <
"an already-declared variable is made a weak_import declaration %0">;
def warn_static_non_static : ExtWarn<
"static declaration of %0 follows non-static declaration">;
def err_non_static_static : Error<
Expand Down
13 changes: 9 additions & 4 deletions lib/Sema/SemaDecl.cpp
Expand Up @@ -2039,12 +2039,17 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) {
}

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

// Merge the types.
MergeVarDeclTypes(New, Old);
Expand Down
2 changes: 1 addition & 1 deletion test/CodeGen/attr-weak-import.c
Expand Up @@ -20,7 +20,7 @@ extern int E __attribute__((weak_import));

// CHECK: @A = global i32
// CHECK-NOT: @B =
// CHECK: @C = global i32
// CHECK: @C = common global i32
// CHECK: @D = global i32
// CHECK: @E = global i32

4 changes: 4 additions & 0 deletions test/Sema/attr-weak.c
Expand Up @@ -12,3 +12,7 @@ struct __attribute__((weak)) s0 {}; // expected-warning {{'weak' attribute only
struct __attribute__((weak_import)) s1 {}; // expected-warning {{'weak_import' attribute only applies to variables and functions}}

static int x __attribute__((weak)); // expected-error {{weak declaration cannot have internal linkage}}

// 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}}

0 comments on commit d543130

Please sign in to comment.