[cfe-commits] r106424 - in /cfe/trunk: lib/AST/Decl.cpp lib/Sema/SemaDecl.cpp test/CodeGenCXX/internal-linkage.cpp test/Sema/var-redecl.c

Fariborz Jahanian fjahanian at apple.com
Mon Jun 21 09:08:37 PDT 2010


Author: fjahanian
Date: Mon Jun 21 11:08:37 2010
New Revision: 106424

URL: http://llvm.org/viewvc/llvm-project?rev=106424&view=rev
Log:
Fixes a corner case bug whereby declaring and defining an extern variable in a 
particular sequence causes its definition to not be generated in the object file.
(fixes radar 8071804).

Modified:
    cfe/trunk/lib/AST/Decl.cpp
    cfe/trunk/lib/Sema/SemaDecl.cpp
    cfe/trunk/test/CodeGenCXX/internal-linkage.cpp
    cfe/trunk/test/Sema/var-redecl.c

Modified: cfe/trunk/lib/AST/Decl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/AST/Decl.cpp?rev=106424&r1=106423&r2=106424&view=diff
==============================================================================
--- cfe/trunk/lib/AST/Decl.cpp (original)
+++ cfe/trunk/lib/AST/Decl.cpp Mon Jun 21 11:08:37 2010
@@ -701,7 +701,15 @@
   // AST for 'extern "C" int foo;' is annotated with 'extern'.
   if (hasExternalStorage())
     return DeclarationOnly;
-
+  
+  if (getStorageClassAsWritten() == Extern ||
+       getStorageClassAsWritten() == PrivateExtern) {
+    for (const VarDecl *PrevVar = getPreviousDeclaration();
+         PrevVar; PrevVar = PrevVar->getPreviousDeclaration()) {
+      if (PrevVar->getLinkage() == InternalLinkage && PrevVar->hasInit())
+        return DeclarationOnly;
+    }
+  }
   // C99 6.9.2p2:
   //   A declaration of an object that has file scope without an initializer,
   //   and without a storage class specifier or the scs 'static', constitutes

Modified: cfe/trunk/lib/Sema/SemaDecl.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaDecl.cpp?rev=106424&r1=106423&r2=106424&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaDecl.cpp (original)
+++ cfe/trunk/lib/Sema/SemaDecl.cpp Mon Jun 21 11:08:37 2010
@@ -1455,6 +1455,10 @@
     return;
   }
 
+  if (New->hasExternalStorage() &&
+      Old->getLinkage() == InternalLinkage)
+    New->setStorageClass(Old->getStorageClass());
+
   // Keep a chain of previous declarations.
   New->setPreviousDeclaration(Old);
 

Modified: cfe/trunk/test/CodeGenCXX/internal-linkage.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/internal-linkage.cpp?rev=106424&r1=106423&r2=106424&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/internal-linkage.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/internal-linkage.cpp Mon Jun 21 11:08:37 2010
@@ -17,3 +17,40 @@
 // CHECK: @anon2 = internal global
 X<Anon> anon2;
 
+// rdar: // 8071804
+char const * const xyzzy = "Hello, world!";
+extern char const * const xyzzy;
+
+char const * const *test1()
+{
+   // CHECK: @_ZL5xyzzy = internal constant
+    return &xyzzy;
+}
+
+static char const * const static_xyzzy = "Hello, world!";
+extern char const * const static_xyzzy;
+
+char const * const *test2()
+{
+    // CHECK: @_ZL12static_xyzzy = internal constant
+    return &static_xyzzy;
+}
+
+static char const * static_nonconst_xyzzy = "Hello, world!";
+extern char const * static_nonconst_xyzzy;
+
+char const * *test3()
+{
+    // CHECK: @_ZL21static_nonconst_xyzzy = internal global
+    return &static_nonconst_xyzzy;
+}
+
+
+char const * extern_nonconst_xyzzy = "Hello, world!";
+extern char const * extern_nonconst_xyzzy;
+
+char const * *test4()
+{
+    // CHECK: @extern_nonconst_xyzzy = global
+    return &extern_nonconst_xyzzy;
+}

Modified: cfe/trunk/test/Sema/var-redecl.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/var-redecl.c?rev=106424&r1=106423&r2=106424&view=diff
==============================================================================
--- cfe/trunk/test/Sema/var-redecl.c (original)
+++ cfe/trunk/test/Sema/var-redecl.c Mon Jun 21 11:08:37 2010
@@ -58,5 +58,5 @@
 
 // PR3645
 static int a;
-extern int a;
-int a;
+extern int a; // expected-note {{previous definition is here}}
+int a;	// expected-error {{non-static declaration of 'a' follows static declaration}}





More information about the cfe-commits mailing list