This is an archive of the discontinued LLVM Phabricator instance.

Avoid -Wshadow warnings for shadowed variables that aren't captured by lambdas with an explicit capture list
ClosedPublic

Authored by arphaman on Nov 3 2016, 4:44 AM.

Details

Summary

This patch avoids the -Wshadow warning for variables which shadow variables that aren't captured by lambdas with an explicit capture list. It provides an additional note that points to location of the explicit capture.

I noticed that we have -Wshadow-all as well, but I wasn't sure how to preserve the old behaviour for it (since I don't think I can check if a diagnostic group is enabled), or if the old behaviour should be preserved at all. What do you think?

This patch fixes PR 21426, but I think that this is a partial fix, since that bug doesn't specify that the capture list should be explicit. I plan on working on a similar patch for lambdas with a default capture specifier and implicit variable captures, but I'm not sure it's 100% needed for them. Do you think this kind of change would make sense for implicit captures as well?

Diff Detail

Repository
rL LLVM

Event Timeline

arphaman updated this revision to Diff 76840.Nov 3 2016, 4:44 AM
arphaman retitled this revision from to Avoid -Wshadow warnings for shadowed variables that aren't captured by lambdas with an explicit capture list.
arphaman updated this object.
arphaman added reviewers: rsmith, rnk.
arphaman set the repository for this revision to rL LLVM.
arphaman added a subscriber: cfe-commits.
rnk edited edge metadata.Nov 7 2016, 1:02 PM

I'd like to preserve the old behavior under -Wshadow-all. The last time I came here to try to quiet a shadowing warning on constructor parameters, we went to great lengths to try to satisfy users who really want to warn on any and all kinds of shadowing. I think you can do it by adding a new diagnostic with the same text in a new group, and using the new diagnostic id if appropriate instead of returning like you do now.

arphaman updated this revision to Diff 77201.Nov 8 2016, 8:59 AM
arphaman edited edge metadata.

The updated patch preserves the old behaviour in -Wshadow-all

rnk added inline comments.Nov 8 2016, 9:14 AM
include/clang/Basic/DiagnosticSemaKinds.td
361 ↗(On Diff #77201)

Use the .Text accessor like we do here to avoid repeating the text:

def warn_objc_pointer_masking : Warning<
  "bitmasking for introspection of Objective-C object pointers is strongly "
  "discouraged">,
  InGroup<ObjCPointerIntrospect>;
def warn_objc_pointer_masking_performSelector : Warning<warn_objc_pointer_masking.Text>,
366 ↗(On Diff #77201)

Richard seemed to feel it was important to have a -Wshadow-field-in-constructor, so we should probably add a -Wshadow-uncaptured-local and put that in -Wshadow-all.

arphaman updated this revision to Diff 77205.Nov 8 2016, 9:38 AM
arphaman marked 2 inline comments as done.

The updated patch introduces a new warning group named -Wshadow-uncaptured-local that was suggested by Reid.

rnk accepted this revision.Nov 8 2016, 9:53 AM
rnk edited edge metadata.

lgtm, thanks!

This revision is now accepted and ready to land.Nov 8 2016, 9:53 AM
This revision was automatically updated to reflect the committed changes.