This is an archive of the discontinued LLVM Phabricator instance.

[analyzer] Fix liveness calculation for C++17 structured bindings
ClosedPublic

Authored by george.karpenkov on Mar 27 2018, 2:16 PM.

Details

Summary

C++ structured bindings for non-tuple-types are defined in a peculiar way, where the resulting declaration is not a VarDecl, but a BindingDecl.
That means a lot of existing machinery stops working.
Fortunately, the parent of a BindingDecl, the DecompositionDecl *is* a VarDecl, but it seems there's no direct way to get a pointer from a BindingDecl to a DecompositionDecl.

Applying this patch should be very safe: the behavior for VarDecl's remains the same, the behavior for binding decls would be either fixed, or in the worst case we would say that the variable is always dead.

Note that the patch should not cause crashes even if pre-processing computation fails, as all usages of bindingParentMap are guarded against.

rdar://36912381

Diff Detail

Event Timeline

Moreover, LiveVariables.cpp is only used by the analyzer, so it should not affect any other parts of the compiler.

NoQ added a comment.Mar 28 2018, 4:03 PM

I guess the interesting part here is what happens if only a but not b is used (or vice versa).

I guess the interesting part here is what happens if only a but not b is used (or vice versa).

Fixed

NoQ added inline comments.Mar 29 2018, 11:59 AM
lib/Analysis/LiveVariables.cpp
367–368

I want a comment here. Why can BindingDecl's killability be derived from the parent DecompositionDecl's killability?

test/Analysis/live-bindings-test.cpp
48–52

Could you also add:

int kill_one_binding() {
  auto [a, b] = GetNumbers();
  a = 100;
  return a;
}

(i guess it should warn but it's not too bad if it doesn't)

george.karpenkov updated this revision to Diff 140316.
george.karpenkov marked 2 inline comments as done.

Removed parent map.

NoQ accepted this revision.Mar 30 2018, 6:15 PM

LGTM!

test/Analysis/live-bindings-test.cpp
61

Should we, as a FIXME, specify that only 'b' is unused?

This revision is now accepted and ready to land.Mar 30 2018, 6:15 PM
test/Analysis/live-bindings-test.cpp
61

I'm not sure what do you mean; Here both variables are dead?

This revision was automatically updated to reflect the committed changes.
NoQ added inline comments.Mar 30 2018, 6:23 PM
test/Analysis/live-bindings-test.cpp
61

Whoops right.