This is an archive of the discontinued LLVM Phabricator instance.

[clangd] Improve PopulateSwitch tweak to work on non-empty switches
ClosedPublic

Authored by tdeo on Sep 28 2020, 10:43 AM.

Details

Summary

Improve the recently-added PopulateSwitch tweak to work on non-empty switches.

Diff Detail

Event Timeline

tdeo created this revision.Sep 28 2020, 10:43 AM
tdeo requested review of this revision.Sep 28 2020, 10:43 AM
sammccall accepted this revision.Sep 29 2020, 12:10 AM

Very nice, thank you!
A few nits about comments and asserts.

clang-tools-extra/clangd/refactor/tweaks/PopulateSwitch.cpp
113–129

Somewhere we should have a high-level comment about what we're doing here:

We trigger if there are fewer cases than enum values (and no case covers multiple values).
This guarantees we'll have at least one case to insert.
We don't yet determine what the cases are, as that means evaluating expressions.

120

Code is obvious here, say why instead?

"Default likely intends to cover cases we'd insert."?

(Sometimes this is still interesting in that case, so we could consider downgrading from "fix" to "refactoring" or so in this case. But it makes sense to be conservative for now)

124

nit: just cast<CaseStmt> and don't check for null (it'll automatically assert). There are no other options.

128

, so our counting argument doesn't work.

132

"We may not be able to work out which cases are covered"?

153

this can be an assert (and previous line a cast) - apply() never gets called unless prepare() returns true

157

similarly here

180

this is also an assert, right? I don't think we can get here.

clang-tools-extra/clangd/unittests/TweakTests.cpp
2838

is there an interesting difference between the single/multiple case that wouldn't be covered by just the multiple case?
(and below)

2838

can we add a case with default?
(And optionally template/gnu range, though those are pretty rare)

This revision is now accepted and ready to land.Sep 29 2020, 12:10 AM
tdeo updated this revision to Diff 294959.Sep 29 2020, 6:41 AM
  • Improve comments
  • In apply(), assert on the conditions we established in prepare() instead of proceeding.
  • Remove redundant tests and add new ones for value-dependent and GNU range cases.
tdeo marked 10 inline comments as done.Sep 29 2020, 6:43 AM

Thanks for the review! Please land this if there are no more issues.

sammccall accepted this revision.Sep 29 2020, 7:02 AM

Thank you!