[PATCH] D35941: Fix -Wshadow false positives with function-local classes.

Alexander Kornienko via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Thu Jul 27 06:43:33 PDT 2017


alexfh created this revision.

Fixes http://llvm.org/PR33947.

https://godbolt.org/g/54XRMT

void f(int a) {

  struct A {
    void g(int a) {}
    A() { int a; }
  };

}

3 : <source>:3:16: warning: declaration shadows a local variable [-Wshadow]

  void g(int a) {}
             ^

1 : <source>:1:12: note: previous declaration is here
void f(int a) {

  ^

4 : <source>:4:15: warning: declaration shadows a local variable [-Wshadow]

  A() { int a; }
            ^

1 : <source>:1:12: note: previous declaration is here
void f(int a) {

  ^

2 warnings generated.

The local variable `a` of the function `f` can't be accessed from a method of
the function-local class A, thus no shadowing occurs and no diagnostic is
needed.


https://reviews.llvm.org/D35941

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
@@ -213,3 +213,12 @@
 void handleLinkageSpec() {
   typedef void externC; // expected-warning {{declaration shadows a typedef in the global namespace}}
 }
+
+namespace PR33947 {
+void f(int a) {
+  struct A {
+    void g(int a) {}
+    A() { int a; }
+  };
+}
+}
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -6999,6 +6999,21 @@
           return;
         }
       }
+
+      if (cast<VarDecl>(ShadowedDecl)->hasLocalStorage()) {
+        // A variable can't shadow a local variable in an enclosing scope, if
+        // they are separated by a non-capturing declaration context.
+        for (DeclContext *ParentDC = NewDC;
+             ParentDC && !ParentDC->Equals(OldDC);
+             ParentDC = getLambdaAwareParentOfDeclContext(ParentDC)) {
+          // Only block literals, captured statements, and lambda expressions
+          // can capture; other scopes don't.
+          if (!isa<BlockDecl>(ParentDC) && !isa<CapturedDecl>(ParentDC) &&
+              !isLambdaCallOperator(ParentDC)) {
+            return;
+          }
+        }
+      }
     }
   }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D35941.108456.patch
Type: text/x-patch
Size: 1346 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20170727/f1cd4fed/attachment.bin>


More information about the cfe-commits mailing list