This is an archive of the discontinued LLVM Phabricator instance.

[SEMA] Added warn_decl_shadow support for structured bindings
ClosedPublic

Authored by Vexthil on Feb 5 2021, 8:45 AM.

Details

Summary

https://bugs.llvm.org/show_bug.cgi?id=40858

CheckShadow is now called for each binding in the structured binding to make sure it does not shadow any other variable in scope. This does use a custom implementation of getShadowedDeclaration though because a BindingDecl is not a VarDecl

Added a few unit tests for this. In theory though all the other shadow unit tests should be duplicated for the structured binding variables too but whether it is probably not worth it as they use common code. The MyTuple and std interface code has been copied from live-bindings-test.cpp

Diff Detail

Event Timeline

Vexthil requested review of this revision.Feb 5 2021, 8:45 AM
Vexthil created this revision.

I have no idea what this error means or what to do about it i'm afraid but it does not look related to the changes I have made. I also do not have access to a Linux machine easily so reproducing it locally is not going to be easy. If anyone knows this error better and can give some pointers that would be great.

class/union requires a filename
!87 = distinct !DICompositeType(tag: DW_TAG_union_type, name: "kmp_cmplrdata_t", size: 64, elements: !2)
Vexthil updated this revision to Diff 321860.Feb 5 2021, 1:13 PM

Updating clang format

Vexthil updated this revision to Diff 321930.Feb 6 2021, 5:02 AM

Fixed the update by doing a full diff rather than just the additive update

Thanks!

clang/lib/Sema/SemaDecl.cpp
7571

isa<VarDecl>(ShadowedDecl) || isa<FieldDecl>(ShadowedDecl) can be simplified to isa<VarDecl, FieldDecl>(ShadowedDecl).

7571–7573

I think we should also warn if a BindingDecl shadows another BindingDecl, or if a VarDecl shadows a BindingDecl.

clang/lib/Sema/SemaDeclCXX.cpp
872–874

Should this be an else for the if (!Previous.empty()) below? Do we get two diagnostics for:

int a;
struct X { int n; };
auto [a] = X();

(one for shadowing and one for redefinition)?

clang/test/SemaCXX/warn-shadow.cpp
274

It doesn't seem important to test different kinds of bindings here, since the shadowing check for bindings doesn't depend on how we perform the decomposition. So I'd suggest you simplify this test by using only built-in bindings, eg:

namespace structured_binding_tests {
int x; // expected-note {{previous declaration is here}}
int y; // expected-note {{previous declaration is here}}
struct S { int a, b; };

void test1() {
  const auto [x, y] = S(); // expected-warning 2 {{declaration shadows a variable in namespace 'structured_binding_tests'}}
}
Vexthil updated this revision to Diff 325252.Feb 20 2021, 3:27 PM

Update with changes suggested by @rsmith .

Vexthil updated this revision to Diff 325256.Feb 20 2021, 4:21 PM

Fixing clang format issues. The SemaDecl.cpp has a pile of incorrect clang format issues but i've only updated changes relevant to changes I have made

rsmith accepted this revision.Feb 21 2021, 11:49 PM

Looks good, thanks! Do you need someone to commit this for you?

This revision is now accepted and ready to land.Feb 21 2021, 11:49 PM

Hm, before we go forward with this: can we get a test for the "shadows a structured binding" case, please? You might also need to add a new case to the "declaration shadows ..." warning text.

Vexthil updated this revision to Diff 325578.Feb 22 2021, 2:53 PM

Added unit tests for "shadows a structured binding"

.. And yes I will need someone to commit this for me I believe once you are happy with it.

This revision was automatically updated to reflect the committed changes.

This is very belated, sorry :)

Our shadowing warnings have special behavior for lambdas and local variables: the warning for lambdas shadowing uncaptured local variables falls under -Wshadow-uncaptured-local, which isn't turned on for -Wshadow (only for -Wshadow-all). However, this doesn't extend to structured bindings. Compare https://godbolt.org/z/nP6b9f8o4, which doesn't fire for -Wshadow, to https://godbolt.org/z/9he8Ezsbb, which is the equivalent with structured bindings and does fire for -Wshadow. This is triggering a whole bunch of new -Wshadow warnings in our codebase, and I'm wondering if we'd consider also separating the lambda + structured binding case into its own warning group, to match the behavior for lambdas + local variables.

Herald added a project: Restricted Project. · View Herald TranscriptJun 15 2022, 4:20 PM