This is an archive of the discontinued LLVM Phabricator instance.

Fix Bug 38713: clang-format mishandles a short block after "default:" in a switch statement
ClosedPublic

Authored by owenpan on Aug 27 2018, 3:12 AM.

Diff Detail

Repository
rL LLVM

Event Timeline

owenpan created this revision.Aug 27 2018, 3:12 AM
sammccall accepted this revision.Aug 28 2018, 2:48 AM

Thanks!

lib/Format/UnwrappedLineFormatter.cpp
486 ↗(On Diff #162646)

Just to check my understanding...
we want to treat default the same as case, but the heuristics are different:

  • case should only appear in a switch (but might be followed by a complex expression)
  • default has lots of meanings (but we can disambiguate: check if it's followed by a colon)

You could consider // default: in switch statement above this line.

unittests/Format/FormatTest.cpp
1012 ↗(On Diff #162646)

the intent of this test might be clearer if the cases were formatted as case 0: { return false; } on one line

This revision is now accepted and ready to land.Aug 28 2018, 2:48 AM
owenpan marked an inline comment as done.Aug 28 2018, 4:50 PM
owenpan added inline comments.
lib/Format/UnwrappedLineFormatter.cpp
486 ↗(On Diff #162646)

Exactly!

unittests/Format/FormatTest.cpp
1012 ↗(On Diff #162646)

I used the same test case that exposed this bug. Please see it in the bug report.

Because of the bug, "case" and "default" case labels were handled differently. The former was correctly left alone, but the latter was merged into one line. Therefore, the test case checks that neither is merged.

owenpan updated this revision to Diff 162989.Aug 28 2018, 4:54 PM
owenpan marked an inline comment as done.

Added a comment suggested by @sammccall

owenpan marked an inline comment as done and an inline comment as not done.Aug 28 2018, 4:55 PM

Commited for Owen in r341284.

This revision was automatically updated to reflect the committed changes.