[PATCH] D18271: Avoid -Wshadow warnings about constructor parameters named after fields

Reid Kleckner via cfe-commits cfe-commits at lists.llvm.org
Fri Apr 15 09:35:05 PDT 2016


rnk added inline comments.

================
Comment at: lib/Sema/SemaDecl.cpp:6417-6425
@@ +6416,11 @@
+    if (isa<CXXConstructorDecl>(NewDC) && isa<ParmVarDecl>(D)) {
+      if (Diags.isIgnored(diag::warn_ctor_parm_shadows_field, R.getNameLoc())) {
+        D = D->getCanonicalDecl();
+        ShadowingDecls.insert({D, FD});
+      } else {
+        Diag(R.getNameLoc(), diag::warn_ctor_parm_shadows_field)
+            << D << FD << FD->getParent();
+        Diag(FD->getLocation(), diag::note_previous_declaration);
+      }
+      return;
+    }
----------------
rsmith wrote:
> I would prefer for us to produce the -Wshadow "modifying shadowed" warning where it applies, even when -Wshadow-all is enabled. The usual model is to act as if -W* flags are just a filter over the diagnostic output, and this diverges from that.
> 
> Is the goal to ensure that -Wshadow-field-in-constructor diagnoses all constructor parameter / field shadowing, even if -Wshadow is not enabled?
Well, if we do that we'll get two warnings about the shadowed field: one on the site of the parameter declaration and another on the modification. That felt a bit redundant.

I can remove the if/else and we'll get the behavior you're asking for. Most users will not use -Wshadow-all, and even if they do they'll only see the double diagnostic in very rare cases.


http://reviews.llvm.org/D18271





More information about the cfe-commits mailing list