Skip to content

Commit 74720d1

Browse files
Galen Eliasowenca
authored andcommitted
[clang-format] Add AlignConsecutiveShortCaseStatements
This adds a new AlignConsecutiveShortCaseStatements option in line with the existing AlignConsecutive* options , which when AllowShortCaseLabelsOnASingleLine is enabled will align the tokens after the case statement's colon. This also adds a AlignCaseColons option to allow aligning the case label colon itself rather than the token after it. Fixes #55475. Differential Revision: https://reviews.llvm.org/D151761
1 parent 87ad34f commit 74720d1

File tree

8 files changed

+569
-19
lines changed

8 files changed

+569
-19
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -789,6 +789,104 @@ the configuration (without a prefix: ``Auto``).
789789
bbb >>= 2;
790790

791791

792+
.. _AlignConsecutiveShortCaseStatements:
793+
794+
**AlignConsecutiveShortCaseStatements** (``ShortCaseStatementsAlignmentStyle``) :versionbadge:`clang-format 17` :ref:`<AlignConsecutiveShortCaseStatements>`
795+
Style of aligning consecutive short case labels.
796+
Only applies if ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
797+
798+
799+
.. code-block:: yaml
800+
801+
# Example of usage:
802+
AlignConsecutiveShortCaseStatements:
803+
Enabled: true
804+
AcrossEmptyLines: true
805+
AcrossComments: true
806+
AlignCaseColons: false
807+
808+
Nested configuration flags:
809+
810+
Alignment options.
811+
812+
* ``bool Enabled`` Whether aligning is enabled.
813+
814+
.. code-block:: c++
815+
816+
true:
817+
switch (level) {
818+
case log::info: return "info:";
819+
case log::warning: return "warning:";
820+
default: return "";
821+
}
822+
823+
false:
824+
switch (level) {
825+
case log::info: return "info:";
826+
case log::warning: return "warning:";
827+
default: return "";
828+
}
829+
830+
* ``bool AcrossEmptyLines`` Whether to align across empty lines.
831+
832+
.. code-block:: c++
833+
834+
true:
835+
switch (level) {
836+
case log::info: return "info:";
837+
case log::warning: return "warning:";
838+
839+
default: return "";
840+
}
841+
842+
false:
843+
switch (level) {
844+
case log::info: return "info:";
845+
case log::warning: return "warning:";
846+
847+
default: return "";
848+
}
849+
850+
* ``bool AcrossComments`` Whether to align across comments.
851+
852+
.. code-block:: c++
853+
854+
true:
855+
switch (level) {
856+
case log::info: return "info:";
857+
case log::warning: return "warning:";
858+
/* A comment. */
859+
default: return "";
860+
}
861+
862+
false:
863+
switch (level) {
864+
case log::info: return "info:";
865+
case log::warning: return "warning:";
866+
/* A comment. */
867+
default: return "";
868+
}
869+
870+
* ``bool AlignCaseColons`` Whether aligned case labels are aligned on the colon, or on the
871+
, or on the tokens after the colon.
872+
873+
.. code-block:: c++
874+
875+
true:
876+
switch (level) {
877+
case log::info : return "info:";
878+
case log::warning: return "warning:";
879+
default : return "";
880+
}
881+
882+
false:
883+
switch (level) {
884+
case log::info: return "info:";
885+
case log::warning: return "warning:";
886+
default: return "";
887+
}
888+
889+
792890
.. _AlignEscapedNewlines:
793891

794892
**AlignEscapedNewlines** (``EscapedNewlineAlignmentStyle``) :versionbadge:`clang-format 5` :ref:`<AlignEscapedNewlines>`

clang/docs/ReleaseNotes.rst

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -988,6 +988,8 @@ clang-format
988988
- Add ``KeepEmptyLinesAtEOF`` to keep empty lines at end of file.
989989
- Add ``RemoveParentheses`` to remove redundant parentheses.
990990
- Add ``TypeNames`` to treat listed non-keyword identifiers as type names.
991+
- Add ``AlignConsecutiveShortCaseStatements`` which can be used to align case
992+
labels in conjunction with ``AllowShortCaseLabelsOnASingleLine``.
991993

992994
libclang
993995
--------

clang/include/clang/Format/Format.h

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,103 @@ struct FormatStyle {
299299
/// \version 3.8
300300
AlignConsecutiveStyle AlignConsecutiveDeclarations;
301301

302+
/// Alignment options.
303+
///
304+
struct ShortCaseStatementsAlignmentStyle {
305+
/// Whether aligning is enabled.
306+
/// \code
307+
/// true:
308+
/// switch (level) {
309+
/// case log::info: return "info:";
310+
/// case log::warning: return "warning:";
311+
/// default: return "";
312+
/// }
313+
///
314+
/// false:
315+
/// switch (level) {
316+
/// case log::info: return "info:";
317+
/// case log::warning: return "warning:";
318+
/// default: return "";
319+
/// }
320+
/// \endcode
321+
bool Enabled;
322+
/// Whether to align across empty lines.
323+
/// \code
324+
/// true:
325+
/// switch (level) {
326+
/// case log::info: return "info:";
327+
/// case log::warning: return "warning:";
328+
///
329+
/// default: return "";
330+
/// }
331+
///
332+
/// false:
333+
/// switch (level) {
334+
/// case log::info: return "info:";
335+
/// case log::warning: return "warning:";
336+
///
337+
/// default: return "";
338+
/// }
339+
/// \endcode
340+
bool AcrossEmptyLines;
341+
/// Whether to align across comments.
342+
/// \code
343+
/// true:
344+
/// switch (level) {
345+
/// case log::info: return "info:";
346+
/// case log::warning: return "warning:";
347+
/// /* A comment. */
348+
/// default: return "";
349+
/// }
350+
///
351+
/// false:
352+
/// switch (level) {
353+
/// case log::info: return "info:";
354+
/// case log::warning: return "warning:";
355+
/// /* A comment. */
356+
/// default: return "";
357+
/// }
358+
/// \endcode
359+
bool AcrossComments;
360+
/// Whether aligned case labels are aligned on the colon, or on the
361+
/// , or on the tokens after the colon.
362+
/// \code
363+
/// true:
364+
/// switch (level) {
365+
/// case log::info : return "info:";
366+
/// case log::warning: return "warning:";
367+
/// default : return "";
368+
/// }
369+
///
370+
/// false:
371+
/// switch (level) {
372+
/// case log::info: return "info:";
373+
/// case log::warning: return "warning:";
374+
/// default: return "";
375+
/// }
376+
/// \endcode
377+
bool AlignCaseColons;
378+
bool operator==(const ShortCaseStatementsAlignmentStyle &R) const {
379+
return Enabled == R.Enabled && AcrossEmptyLines == R.AcrossEmptyLines &&
380+
AcrossComments == R.AcrossComments &&
381+
AlignCaseColons == R.AlignCaseColons;
382+
}
383+
};
384+
385+
/// Style of aligning consecutive short case labels.
386+
/// Only applies if ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
387+
///
388+
/// \code{.yaml}
389+
/// # Example of usage:
390+
/// AlignConsecutiveShortCaseStatements:
391+
/// Enabled: true
392+
/// AcrossEmptyLines: true
393+
/// AcrossComments: true
394+
/// AlignCaseColons: false
395+
/// \endcode
396+
/// \version 17
397+
ShortCaseStatementsAlignmentStyle AlignConsecutiveShortCaseStatements;
398+
302399
/// Different styles for aligning escaped newlines.
303400
enum EscapedNewlineAlignmentStyle : int8_t {
304401
/// Don't align escaped newlines.
@@ -4357,6 +4454,8 @@ struct FormatStyle {
43574454
AlignConsecutiveBitFields == R.AlignConsecutiveBitFields &&
43584455
AlignConsecutiveDeclarations == R.AlignConsecutiveDeclarations &&
43594456
AlignConsecutiveMacros == R.AlignConsecutiveMacros &&
4457+
AlignConsecutiveShortCaseStatements ==
4458+
R.AlignConsecutiveShortCaseStatements &&
43604459
AlignEscapedNewlines == R.AlignEscapedNewlines &&
43614460
AlignOperands == R.AlignOperands &&
43624461
AlignTrailingComments == R.AlignTrailingComments &&

clang/lib/Format/Format.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,17 @@ template <> struct MappingTraits<FormatStyle::AlignConsecutiveStyle> {
111111
}
112112
};
113113

114+
template <>
115+
struct MappingTraits<FormatStyle::ShortCaseStatementsAlignmentStyle> {
116+
static void mapping(IO &IO,
117+
FormatStyle::ShortCaseStatementsAlignmentStyle &Value) {
118+
IO.mapOptional("Enabled", Value.Enabled);
119+
IO.mapOptional("AcrossEmptyLines", Value.AcrossEmptyLines);
120+
IO.mapOptional("AcrossComments", Value.AcrossComments);
121+
IO.mapOptional("AlignCaseColons", Value.AlignCaseColons);
122+
}
123+
};
124+
114125
template <>
115126
struct ScalarEnumerationTraits<FormatStyle::AttributeBreakingStyle> {
116127
static void enumeration(IO &IO, FormatStyle::AttributeBreakingStyle &Value) {
@@ -857,6 +868,8 @@ template <> struct MappingTraits<FormatStyle> {
857868
IO.mapOptional("AlignConsecutiveDeclarations",
858869
Style.AlignConsecutiveDeclarations);
859870
IO.mapOptional("AlignConsecutiveMacros", Style.AlignConsecutiveMacros);
871+
IO.mapOptional("AlignConsecutiveShortCaseStatements",
872+
Style.AlignConsecutiveShortCaseStatements);
860873
IO.mapOptional("AlignEscapedNewlines", Style.AlignEscapedNewlines);
861874
IO.mapOptional("AlignOperands", Style.AlignOperands);
862875
IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
@@ -1333,6 +1346,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
13331346
LLVMStyle.AlignConsecutiveBitFields = {};
13341347
LLVMStyle.AlignConsecutiveDeclarations = {};
13351348
LLVMStyle.AlignConsecutiveMacros = {};
1349+
LLVMStyle.AlignConsecutiveShortCaseStatements = {};
13361350
LLVMStyle.AlignTrailingComments = {};
13371351
LLVMStyle.AlignTrailingComments.Kind = FormatStyle::TCAS_Always;
13381352
LLVMStyle.AlignTrailingComments.OverEmptyLines = 0;

0 commit comments

Comments
 (0)