]> granicus.if.org Git - clang/commitdiff
When redeclaring a local extern in the same scope, make sure that we
authorDouglas Gregor <dgregor@apple.com>
Wed, 29 Jun 2011 21:22:02 +0000 (21:22 +0000)
committerDouglas Gregor <dgregor@apple.com>
Wed, 29 Jun 2011 21:22:02 +0000 (21:22 +0000)
replace the existing declaration appropriately. Patch by Jordy Rose,
fixes PR10013 / <rdar://problem/9584157>.

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

lib/Sema/SemaDecl.cpp
test/Sema/extern-redecl.c

index 2779faeb15cfafcf256bf662322f0222c594bb7d..8069eb438594ef052adfac00e457c4275c186f0c 100644 (file)
@@ -3299,8 +3299,18 @@ Sema::RegisterLocallyScopedExternCDecl(NamedDecl *ND,
   if (S && IdResolver.ReplaceDecl(PrevDecl, ND)) {
     // The previous declaration was found on the identifer resolver
     // chain, so remove it from its scope.
-    while (S && !S->isDeclScope(PrevDecl))
-      S = S->getParent();
+
+    if (S->isDeclScope(PrevDecl)) {
+      // Special case for redeclarations in the SAME scope.
+      // Because this declaration is going to be added to the identifier chain
+      // later, we should temporarily take it OFF the chain.
+      IdResolver.RemoveDecl(ND);
+
+    } else {
+      // Find the scope for the original declaration.
+      while (S && !S->isDeclScope(PrevDecl))
+        S = S->getParent();
+    }
 
     if (S)
       S->RemoveDecl(PrevDecl);
index 067e3c21e4c4309d587189cf429d81214002a2bb..c176725df66b9516ca41996260a21ec3785e699f 100644 (file)
@@ -1,4 +1,4 @@
-// RUN: %clang_cc1 -fsyntax-only %s
+// RUN: %clang_cc1 -fsyntax-only -verify %s
 
 // rdar: // 8125274
 static int a16[];  // expected-warning {{tentative array definition assumed to have one element}}
@@ -7,3 +7,16 @@ void f16(void) {
     extern int a16[];
 }
 
+
+// PR10013: Scope of extern declarations extend past enclosing block
+extern int PR10013_x;
+int PR10013(void) {
+  int *PR10013_x = 0;
+  {
+    extern int PR10013_x;
+    extern int PR10013_x; 
+  }
+  
+  return PR10013_x; // expected-warning{{incompatible pointer to integer conversion}}
+}
+