]> granicus.if.org Git - clang/commitdiff
Fixes a corner case bug whereby declaring and defining an extern variable in a
authorFariborz Jahanian <fjahanian@apple.com>
Mon, 21 Jun 2010 16:08:37 +0000 (16:08 +0000)
committerFariborz Jahanian <fjahanian@apple.com>
Mon, 21 Jun 2010 16:08:37 +0000 (16:08 +0000)
particular sequence causes its definition to not be generated in the object file.
(fixes radar 8071804).

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

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

index 25687e15c4cfad8dd17e7633a6776bbf7e98ede7..c912af878a814d8265e64827d74237000e24f2c8 100644 (file)
@@ -701,7 +701,15 @@ VarDecl::DefinitionKind VarDecl::isThisDeclarationADefinition() const {
   // 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
index 528cc65abfcbbe15b005432e3e882a4de8baf969..4a7c877fd98782a3c9d6f4c2748a27c06297e767 100644 (file)
@@ -1455,6 +1455,10 @@ void Sema::MergeVarDecl(VarDecl *New, LookupResult &Previous) {
     return;
   }
 
+  if (New->hasExternalStorage() &&
+      Old->getLinkage() == InternalLinkage)
+    New->setStorageClass(Old->getStorageClass());
+
   // Keep a chain of previous declarations.
   New->setPreviousDeclaration(Old);
 
index 4263891e57f51971ab302f3fba281378f6caf9e4..9fdb7274e146964dd05d2bea964ca5b7ffbe2b80 100644 (file)
@@ -17,3 +17,40 @@ Anon anon1;
 // 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;
+}
index 71d7ea1baee0fdcdcc36b1e86693c07e4ed918a3..f7576b6c0255e5a28586adb7e1cb5fe0bdcdb53d 100644 (file)
@@ -58,5 +58,5 @@ int *p=&g19; // expected-error{{use of undeclared identifier 'g19'}} \
 
 // 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}}