[PATCH] D151761: clang-format: Add AlignConsecutiveShortCaseLabels

Galen Elias via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Tue May 30 16:01:18 PDT 2023


galenelias created this revision.
galenelias added a project: clang-format.
Herald added projects: All, clang.
Herald added a subscriber: cfe-commits.
Herald added reviewers: rymiel, HazardyKnusperkeks, owenpan, MyDeveloperDay.
galenelias requested review of this revision.

This resolves https://github.com/llvm/llvm-project/issues/55475.

This adds a new `AlignConsecutiveShortCaseLabels` option in line with the existing AlignConsecutiveStyle options, which when `AllowShortCaseLabelsOnASingleLine` is enabled, will align the tokens after the case statement's colon.

For the implementation, I went with re-using the `AlignTokens` method, however since consecutive case labels require traversing scopes, the default behavior to recurse upon entering a new scope (and breaking formatting across scopes) doesn't work.  I opted to add a new `IgnoreNestedScopes` parameter to prevent this scope traversal, but it feels a touch hacky.

The other options would be to either write a new version of the alignment code, or to try to leverage one of the other existing aligning functions (seems the other one-off ones are `alignTrailingComments` and `alignConsecutiveMacros`).  Using the logic from `WhitespaceManager::alignConsecutiveMacros` technically seemed to work here, so I could factor the token matching function out of that and re-use it without any other edits, but other than not traversing scopes, I wasn't quite sure what special logic it was doing that might not make sense to re-use generically, so opted for trying to leverage the more general purpose AlignTokens instead.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D151761

Files:
  clang/docs/ClangFormatStyleOptions.rst
  clang/docs/ReleaseNotes.rst
  clang/include/clang/Format/Format.h
  clang/lib/Format/Format.cpp
  clang/lib/Format/WhitespaceManager.cpp
  clang/lib/Format/WhitespaceManager.h
  clang/unittests/Format/ConfigParseTest.cpp
  clang/unittests/Format/FormatTest.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D151761.526827.patch
Type: text/x-patch
Size: 15136 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230530/31d3e863/attachment-0001.bin>


More information about the cfe-commits mailing list