]> granicus.if.org Git - clang/commitdiff
Fix the diagnostic when we are shadowing an external variable and there exists a...
authorArgyrios Kyrtzidis <akyrtzi@gmail.com>
Mon, 31 Jan 2011 07:04:50 +0000 (07:04 +0000)
committerArgyrios Kyrtzidis <akyrtzi@gmail.com>
Mon, 31 Jan 2011 07:04:50 +0000 (07:04 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@124580 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaDecl.cpp
test/Sema/warn-shadow.c

index 24307c98a97f5359c52bc844561cc4e3deaed3de..253f1dcde87ba17b0ef27117676341ba28cf77ea 100644 (file)
@@ -3129,21 +3129,32 @@ void Sema::CheckShadow(Scope *S, VarDecl *D, const LookupResult& R) {
   if (!isa<VarDecl>(ShadowedDecl) && !isa<FieldDecl>(ShadowedDecl))
     return;
 
-  DeclContext *OldDC = ShadowedDecl->getDeclContext();
-
-  // Don't warn for this case:
-  //
-  // @code
-  // extern int bob;
-  // void f() {
-  //   extern int bob;
-  // }
-  // @endcode
-  if (D->isExternC() && NewDC->isFunctionOrMethod())
-    if (VarDecl *shadowedVar = dyn_cast<VarDecl>(ShadowedDecl))
-      if (shadowedVar->isExternC())
+  if (VarDecl *shadowedVar = dyn_cast<VarDecl>(ShadowedDecl))
+    if (shadowedVar->isExternC()) {
+      // Don't warn for this case:
+      //
+      // @code
+      // extern int bob;
+      // void f() {
+      //   extern int bob;
+      // }
+      // @endcode
+      if (D->isExternC())
         return;
 
+      // For shadowing external vars, make sure that we point to the global
+      // declaration, not a locally scoped extern declaration.
+      for (VarDecl::redecl_iterator
+             I = shadowedVar->redecls_begin(), E = shadowedVar->redecls_end();
+           I != E; ++I)
+        if (I->isFileVarDecl()) {
+          ShadowedDecl = *I;
+          break;
+        }
+    }
+
+  DeclContext *OldDC = ShadowedDecl->getDeclContext();
+
   // Only warn about certain kinds of shadowing for class members.
   if (NewDC && NewDC->isRecord()) {
     // In particular, don't warn about shadowing non-class members.
index c77bb0c450b43b38128c3424b60970cecf736b52..32aca8d612b21d6d412e47df0145a43bfe7f2e42 100644 (file)
@@ -49,8 +49,13 @@ void test5(int i);
 void test6(void (*f)(int i)) {}
 void test7(void *context, void (*callback)(void *context)) {}
 
+extern int bob; // expected-note {{previous declaration is here}}
+
 // rdar://8883302
-extern int bob;
 void rdar8883302() {
   extern int bob; // don't warn for shadowing.
 }
+
+void test8() {
+  int bob; // expected-warning {{declaration shadows a variable in the global scope}}
+}