djasper created this revision.

The case, I am particularly interested in is:

  #define A(x)              \
    ...                     \
    if (...) {              \
      int SomeVariable = 1; \
      ...;                  \
    }

Here, SomeVariable never leaves the scope of the macro and at the same
time, it is very unlikely, that the macro code itself intended to use some
other declaration that is then shadowed by SomeVariable.

The patch currently disables -Wshadow for all declarations inside a
macro, but I am happy to make it stricter.


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,12 @@
   using bob2=int; // expected-warning {{declaration shadows a type alias in 
the global namespace}}
 }
 
+void macro() {
+  int x;
+#define A(a) { int x = 0; }
+  A(1);
+}
+
 namespace rdar29067894 {
 
 void avoidWarningWhenRedefining(int b) { // expected-note {{previous 
definition is here}}
@@ -199,13 +205,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
@@ -6947,6 +6947,9 @@
                        const LookupResult &R) {
   DeclContext *NewDC = D->getDeclContext();
 
+  if (D->getLocation().isMacroID())
+    return;
+
   if (FieldDecl *FD = dyn_cast<FieldDecl>(ShadowedDecl)) {
     // Fields are not shadowed by variables in C++ static methods.
     if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewDC))


Index: test/SemaCXX/warn-shadow.cpp
===================================================================
--- test/SemaCXX/warn-shadow.cpp
+++ test/SemaCXX/warn-shadow.cpp
@@ -168,6 +168,12 @@
   using bob2=int; // expected-warning {{declaration shadows a type alias in the global namespace}}
 }
 
+void macro() {
+  int x;
+#define A(a) { int x = 0; }
+  A(1);
+}
+
 namespace rdar29067894 {
 
 void avoidWarningWhenRedefining(int b) { // expected-note {{previous definition is here}}
@@ -199,13 +205,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
@@ -6947,6 +6947,9 @@
                        const LookupResult &R) {
   DeclContext *NewDC = D->getDeclContext();
 
+  if (D->getLocation().isMacroID())
+    return;
+
   if (FieldDecl *FD = dyn_cast<FieldDecl>(ShadowedDecl)) {
     // Fields are not shadowed by variables in C++ static methods.
     if (CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(NewDC))
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to