From: Fariborz Jahanian Date: Wed, 22 Jun 2011 22:08:50 +0000 (+0000) Subject: Issue warning if weak_import attribute is added to an already X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=d543130a3009a350017e2e411ea43bb3e5459b96;p=clang Issue warning if weak_import attribute is added to an already declared variable and ignore it. // rdar://9538608 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@133654 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 2de09873f6..6b710f5f1e 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -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< diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index fe3b3b4f7c..daf9f034ac 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -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() && Old->getStorageClass() == SC_None && - !Old->getAttr()) - Old->addAttr(::new (Context) WeakImportAttr(SourceLocation(), Context)); + !Old->getAttr()) { + 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); diff --git a/test/CodeGen/attr-weak-import.c b/test/CodeGen/attr-weak-import.c index f02d09e815..0707f59a60 100644 --- a/test/CodeGen/attr-weak-import.c +++ b/test/CodeGen/attr-weak-import.c @@ -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 diff --git a/test/Sema/attr-weak.c b/test/Sema/attr-weak.c index 41c9fd7165..adedf1231f 100644 --- a/test/Sema/attr-weak.c +++ b/test/Sema/attr-weak.c @@ -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}}