djasper updated this revision to Diff 107885.
djasper added a comment.

Updated to be a bit more strict (warn if declarations from the same context get
shadowed).


https://reviews.llvm.org/D35783

Files:
  lib/Sema/SemaDecl.cpp
  test/SemaCXX/warn-shadow.cpp


Index: test/SemaCXX/warn-shadow.cpp
===================================================================
--- test/SemaCXX/warn-shadow.cpp
+++ test/SemaCXX/warn-shadow.cpp
@@ -168,6 +168,13 @@
   using bob2=int; // expected-warning {{declaration shadows a type alias in 
the global namespace}}
 }
 
+void macro() {
+  int x; // expected-note 1 {{previous declaration is here}}
+#define A(a) { int a = 0; }
+  A(x); // expected-warning {{declaration shadows a local variable}}
+  A(i);
+}
+
 namespace rdar29067894 {
 
 void avoidWarningWhenRedefining(int b) { // expected-note {{previous 
definition is here}}
@@ -199,13 +206,13 @@
   using l=char; // no warning or error.
   using l=char; // no warning or error.
   typedef char l; // no warning or error.
- 
-  typedef char n; // no warning or error. 
+
+  typedef char n; // no warning or error.
   typedef char n; // no warning or error.
   using n=char; // no warning or error.
 }
 
-}
+} // namespace rdar29067894
 
 extern "C" {
 typedef int externC; // expected-note {{previous declaration is here}}
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -6946,6 +6946,10 @@
 void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
                        const LookupResult &R) {
   DeclContext *NewDC = D->getDeclContext();
+  DeclContext *OldDC = ShadowedDecl->getDeclContext()->getRedeclContext();
+
+  if (D->getLocation().isMacroID() && NewDC != OldDC)
+    return;
 
   if (FieldDecl *FD = dyn_cast<FieldDecl>(ShadowedDecl)) {
     // Fields are not shadowed by variables in C++ static methods.
@@ -6975,8 +6979,6 @@
         }
     }
 
-  DeclContext *OldDC = ShadowedDecl->getDeclContext()->getRedeclContext();
-
   unsigned WarningDiag = diag::warn_decl_shadow;
   SourceLocation CaptureLoc;
   if (isa<VarDecl>(D) && isa<VarDecl>(ShadowedDecl) && NewDC &&


Index: test/SemaCXX/warn-shadow.cpp
===================================================================
--- test/SemaCXX/warn-shadow.cpp
+++ test/SemaCXX/warn-shadow.cpp
@@ -168,6 +168,13 @@
   using bob2=int; // expected-warning {{declaration shadows a type alias in the global namespace}}
 }
 
+void macro() {
+  int x; // expected-note 1 {{previous declaration is here}}
+#define A(a) { int a = 0; }
+  A(x); // expected-warning {{declaration shadows a local variable}}
+  A(i);
+}
+
 namespace rdar29067894 {
 
 void avoidWarningWhenRedefining(int b) { // expected-note {{previous definition is here}}
@@ -199,13 +206,13 @@
   using l=char; // no warning or error.
   using l=char; // no warning or error.
   typedef char l; // no warning or error.
- 
-  typedef char n; // no warning or error. 
+
+  typedef char n; // no warning or error.
   typedef char n; // no warning or error.
   using n=char; // no warning or error.
 }
 
-}
+} // namespace rdar29067894
 
 extern "C" {
 typedef int externC; // expected-note {{previous declaration is here}}
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -6946,6 +6946,10 @@
 void Sema::CheckShadow(NamedDecl *D, NamedDecl *ShadowedDecl,
                        const LookupResult &R) {
   DeclContext *NewDC = D->getDeclContext();
+  DeclContext *OldDC = ShadowedDecl->getDeclContext()->getRedeclContext();
+
+  if (D->getLocation().isMacroID() && NewDC != OldDC)
+    return;
 
   if (FieldDecl *FD = dyn_cast<FieldDecl>(ShadowedDecl)) {
     // Fields are not shadowed by variables in C++ static methods.
@@ -6975,8 +6979,6 @@
         }
     }
 
-  DeclContext *OldDC = ShadowedDecl->getDeclContext()->getRedeclContext();
-
   unsigned WarningDiag = diag::warn_decl_shadow;
   SourceLocation CaptureLoc;
   if (isa<VarDecl>(D) && isa<VarDecl>(ShadowedDecl) && NewDC &&
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to