summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorFariborz Jahanian <fjahanian@apple.com>2011-06-22 22:08:50 +0000
committerFariborz Jahanian <fjahanian@apple.com>2011-06-22 22:08:50 +0000
commit33e022650adee965c65f9aea086ee74f3fd1bad5 (patch)
tree3493a7c5bb5f6e2cb564000278fc3b5ed186844d
parenta60a269e671989b27d46f08a0404ab33109ee22e (diff)
downloadbcm5719-llvm-33e022650adee965c65f9aea086ee74f3fd1bad5.tar.gz
bcm5719-llvm-33e022650adee965c65f9aea086ee74f3fd1bad5.zip
Issue warning if weak_import attribute is added to an already
declared variable and ignore it. // rdar://9538608 llvm-svn: 133654
-rw-r--r--clang/include/clang/Basic/DiagnosticSemaKinds.td2
-rw-r--r--clang/lib/Sema/SemaDecl.cpp13
-rw-r--r--clang/test/CodeGen/attr-weak-import.c2
-rw-r--r--clang/test/Sema/attr-weak.c4
4 files changed, 16 insertions, 5 deletions
diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td
index 2de09873f6f..6b710f5f1ef 100644
--- a/clang/include/clang/Basic/DiagnosticSemaKinds.td
+++ b/clang/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/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp
index fe3b3b4f7c1..daf9f034ac9 100644
--- a/clang/lib/Sema/SemaDecl.cpp
+++ b/clang/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<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);
diff --git a/clang/test/CodeGen/attr-weak-import.c b/clang/test/CodeGen/attr-weak-import.c
index f02d09e8150..0707f59a608 100644
--- a/clang/test/CodeGen/attr-weak-import.c
+++ b/clang/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/clang/test/Sema/attr-weak.c b/clang/test/Sema/attr-weak.c
index 41c9fd7165a..adedf1231f9 100644
--- a/clang/test/Sema/attr-weak.c
+++ b/clang/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}}
OpenPOWER on IntegriCloud