]> granicus.if.org Git - clang/commitdiff
Issue warning if weak_import attribute is added to an already
authorFariborz Jahanian <fjahanian@apple.com>
Wed, 22 Jun 2011 22:08:50 +0000 (22:08 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Wed, 22 Jun 2011 22:08:50 +0000 (22:08 +0000)
declared variable and ignore it. // rdar://9538608

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@133654 91177308-0d34-0410-b5e6-96231b3b80d8

include/clang/Basic/DiagnosticSemaKinds.td
lib/Sema/SemaDecl.cpp
test/CodeGen/attr-weak-import.c
test/Sema/attr-weak.c

index 2de09873f6fbb9d59240fc5f656db59e30fc77dd..6b710f5f1efca70d89a1f1081e75a93ab508a9cd 100644 (file)
@@ -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<
index fe3b3b4f7c17f282d4886e62572fa879c422faac..daf9f034ac906d769ff6dd52cb9112770c2dd77a 100644 (file)
@@ -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);
index f02d09e8150998ae1ef51534580da3d516532e12..0707f59a608b9ab0bda2a03d847ec3029af7e3ba 100644 (file)
@@ -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
 
index 41c9fd7165abe72e7ba36dd600ba9e83928c5d8b..adedf1231f9e91500afbdcfca99d3ae19dcf61ad 100644 (file)
@@ -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}}