This is an archive of the discontinued LLVM Phabricator instance.

[clang-format] Correctly format goto labels followed by blocks
ClosedPublic

Authored by sstwcw on Apr 16 2023, 4:49 PM.

Details

Summary

There doesn't seem to be an issue on GitHub. But previously, a space
would be inserted before the goto colon in the code below.

switch (x) {
case 0:
goto_0: {
  action();
  break;
}
}

Previously, the colon following a goto label would be annotated as
TT_InheritanceColon. A goto label followed by an opening brace
wasn't recognized. It is easy to add another line to have
spaceRequiredBefore function recognize the case, but I believed it
is more proper to avoid doing the same thing in UnwrappedLineParser
and TokenAnnotator. So now the label colons would be labeled in
UnwrappedLineParser, and spaceRequiredBefore would rely on that.

Previously we had the type TT_GotoLabelColon intended for both goto
labels and case labels. But since handling of goto labels and case
labels differ somewhat, I split it into separate types for goto and
case labels.

This patch doesn't change the behavior for case labels. I added the
lines annotating case labels because they would previously be
mistakenly annotated as TT_InheritanceColon just like goto labels.
And since I added the annotations, the checks for the case and
default keywords in spaceRequiredBefore are not necessary anymore.

Diff Detail

Event Timeline

sstwcw created this revision.Apr 16 2023, 4:49 PM
Herald added projects: Restricted Project, Restricted Project, Restricted Project. · View Herald TranscriptApr 16 2023, 4:49 PM
Herald added a subscriber: cfe-commits. · View Herald Transcript
sstwcw requested review of this revision.Apr 16 2023, 4:49 PM
sstwcw added inline comments.Apr 16 2023, 4:52 PM
clang/lib/Format/UnwrappedLineFormatter.cpp
714 ↗(On Diff #514060)

Should I make this change?

Without it:

label: { break; }

With it:

label: {
  break;
}

Without the entire patch:

label : { break; }
clang/lib/Format/TokenAnnotator.cpp
4550

how is the semi case handled or is it not needed

clang/lib/Format/UnwrappedLineFormatter.cpp
714 ↗(On Diff #514060)

I'd say no. To not change this (too much).

clang/unittests/Format/FormatTest.cpp
3020–3024

Why did you remove that?

clang/unittests/Format/TokenAnnotatorTest.cpp
1711

remove

sstwcw updated this revision to Diff 514249.Apr 17 2023, 8:21 AM
  • Remove change in line wrapping
sstwcw marked 3 inline comments as done.Apr 17 2023, 8:24 AM
sstwcw added inline comments.
clang/lib/Format/TokenAnnotator.cpp
4550

It is not needed anymore. It was added to handle a semicolon following a goto label. Now the type TT_GotoLabelColon includes the case.

clang/unittests/Format/FormatTest.cpp
3020–3024

It was an accident.

sstwcw marked 2 inline comments as done.Apr 17 2023, 8:24 AM

Is this patch accepted or rejected?

MyDeveloperDay accepted this revision.Apr 24 2023, 10:41 PM
This revision is now accepted and ready to land.Apr 24 2023, 10:41 PM
This revision was landed with ongoing or failed builds.Apr 30 2023, 3:35 PM
This revision was automatically updated to reflect the committed changes.