[PATCH] D18395: -Wshadow: don't warn on ctor parameters with the same name as a field name

Alexander Kornienko via cfe-commits cfe-commits at lists.llvm.org
Wed Mar 23 07:43:38 PDT 2016


alexfh created this revision.
alexfh added reviewers: rsmith, rnk.
alexfh added a subscriber: cfe-commits.

-Wshadow: don't warn on ctor parameters with the same name as a field
name. This fixes a broad class of false positives resulting from a widely used
pattern:

  struct A {
    int q;
    A(int q) : q(q) {}
  };

Fixes http://llvm.org/PR16088.

http://reviews.llvm.org/D18395

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
@@ -71,6 +71,14 @@
 };
 }
 
+// http://llvm.org/PR16088
+namespace PR16088 {
+struct S {
+  int i;
+  S(int i) : i(i) {}
+};
+}
+
 extern int bob; // expected-note {{previous declaration is here}}
 
 // rdar://8883302
Index: lib/Sema/SemaDecl.cpp
===================================================================
--- lib/Sema/SemaDecl.cpp
+++ lib/Sema/SemaDecl.cpp
@@ -6406,6 +6406,11 @@
         }
     }
 
+  // Don't warn on constructor parameters with the same name as a field name.
+  if (isa<FieldDecl>(ShadowedDecl) && isa<CXXConstructorDecl>(NewDC) &&
+      isa<ParmVarDecl>(D))
+    return;
+
   DeclContext *OldDC = ShadowedDecl->getDeclContext();
 
   // Only warn about certain kinds of shadowing for class members.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D18395.51416.patch
Type: text/x-patch
Size: 901 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20160323/162b1b5e/attachment.bin>


More information about the cfe-commits mailing list