[cfe-commits] r133654 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/SemaDecl.cpp test/CodeGen/attr-weak-import.c test/Sema/attr-weak.c

Fariborz Jahanian fjahanian at apple.com
Wed Jun 22 15:08:50 PDT 2011


Author: fjahanian
Date: Wed Jun 22 17:08:50 2011
New Revision: 133654

URL: http://llvm.org/viewvc/llvm-project?rev=133654&view=rev
Log:
Issue warning if weak_import attribute is added to an already
declared variable and ignore it. // rdar://9538608

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

Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=133654&r1=133653&r2=133654&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Wed Jun 22 17:08:50 2011
@@ -2287,6 +2287,8 @@
   "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<

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=133654&r1=133653&r2=133654&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Wed Jun 22 17:08:50 2011
@@ -2039,12 +2039,17 @@
   }
   
   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);

Modified: cfe/trunk/test/CodeGen/attr-weak-import.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGen/attr-weak-import.c?rev=133654&r1=133653&r2=133654&view=diff
==============================================================================
--- cfe/trunk/test/CodeGen/attr-weak-import.c (original)
+++ cfe/trunk/test/CodeGen/attr-weak-import.c Wed Jun 22 17:08:50 2011
@@ -20,7 +20,7 @@
 
 // CHECK: @A = global i32
 // CHECK-NOT: @B =
-// CHECK: @C = global i32
+// CHECK: @C = common global i32
 // CHECK: @D = global i32
 // CHECK: @E = global i32
 

Modified: cfe/trunk/test/Sema/attr-weak.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/attr-weak.c?rev=133654&r1=133653&r2=133654&view=diff
==============================================================================
--- cfe/trunk/test/Sema/attr-weak.c (original)
+++ cfe/trunk/test/Sema/attr-weak.c Wed Jun 22 17:08:50 2011
@@ -12,3 +12,7 @@
 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}}





More information about the cfe-commits mailing list