Skip to content

Commit

Permalink
Revert "Revert "[clang-format] Add AlignConsecutiveShortCaseStatements""
Browse files Browse the repository at this point in the history
This reverts commit 4f093b3.
  • Loading branch information
owenca committed Jul 25, 2023
1 parent 4f093b3 commit ac6e551
Show file tree
Hide file tree
Showing 8 changed files with 569 additions and 19 deletions.
98 changes: 98 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -789,6 +789,104 @@ the configuration (without a prefix: ``Auto``).
bbb >>= 2;


.. _AlignConsecutiveShortCaseStatements:

**AlignConsecutiveShortCaseStatements** (``ShortCaseStatementsAlignmentStyle``) :versionbadge:`clang-format 17` :ref:`<AlignConsecutiveShortCaseStatements>`
Style of aligning consecutive short case labels.
Only applies if ``AllowShortCaseLabelsOnASingleLine`` is ``true``.


.. code-block:: yaml
# Example of usage:
AlignConsecutiveShortCaseStatements:
Enabled: true
AcrossEmptyLines: true
AcrossComments: true
AlignCaseColons: false
Nested configuration flags:

Alignment options.

* ``bool Enabled`` Whether aligning is enabled.

.. code-block:: c++

true:
switch (level) {
case log::info: return "info:";
case log::warning: return "warning:";
default: return "";
}

false:
switch (level) {
case log::info: return "info:";
case log::warning: return "warning:";
default: return "";
}

* ``bool AcrossEmptyLines`` Whether to align across empty lines.

.. code-block:: c++

true:
switch (level) {
case log::info: return "info:";
case log::warning: return "warning:";

default: return "";
}

false:
switch (level) {
case log::info: return "info:";
case log::warning: return "warning:";

default: return "";
}

* ``bool AcrossComments`` Whether to align across comments.

.. code-block:: c++

true:
switch (level) {
case log::info: return "info:";
case log::warning: return "warning:";
/* A comment. */
default: return "";
}
false:
switch (level) {
case log::info: return "info:";
case log::warning: return "warning:";
/* A comment. */
default: return "";
}
* ``bool AlignCaseColons`` Whether aligned case labels are aligned on the colon, or on the
, or on the tokens after the colon.

.. code-block:: c++

true:
switch (level) {
case log::info : return "info:";
case log::warning: return "warning:";
default : return "";
}

false:
switch (level) {
case log::info: return "info:";
case log::warning: return "warning:";
default: return "";
}


.. _AlignEscapedNewlines:

**AlignEscapedNewlines** (``EscapedNewlineAlignmentStyle``) :versionbadge:`clang-format 5` :ref:`<AlignEscapedNewlines>`
Expand Down
2 changes: 2 additions & 0 deletions clang/docs/ReleaseNotes.rst
Original file line number Diff line number Diff line change
Expand Up @@ -988,6 +988,8 @@ clang-format
- Add ``KeepEmptyLinesAtEOF`` to keep empty lines at end of file.
- Add ``RemoveParentheses`` to remove redundant parentheses.
- Add ``TypeNames`` to treat listed non-keyword identifiers as type names.
- Add ``AlignConsecutiveShortCaseStatements`` which can be used to align case
labels in conjunction with ``AllowShortCaseLabelsOnASingleLine``.

libclang
--------
Expand Down
99 changes: 99 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,103 @@ struct FormatStyle {
/// \version 3.8
AlignConsecutiveStyle AlignConsecutiveDeclarations;

/// Alignment options.
///
struct ShortCaseStatementsAlignmentStyle {
/// Whether aligning is enabled.
/// \code
/// true:
/// switch (level) {
/// case log::info: return "info:";
/// case log::warning: return "warning:";
/// default: return "";
/// }
///
/// false:
/// switch (level) {
/// case log::info: return "info:";
/// case log::warning: return "warning:";
/// default: return "";
/// }
/// \endcode
bool Enabled;
/// Whether to align across empty lines.
/// \code
/// true:
/// switch (level) {
/// case log::info: return "info:";
/// case log::warning: return "warning:";
///
/// default: return "";
/// }
///
/// false:
/// switch (level) {
/// case log::info: return "info:";
/// case log::warning: return "warning:";
///
/// default: return "";
/// }
/// \endcode
bool AcrossEmptyLines;
/// Whether to align across comments.
/// \code
/// true:
/// switch (level) {
/// case log::info: return "info:";
/// case log::warning: return "warning:";
/// /* A comment. */
/// default: return "";
/// }
///
/// false:
/// switch (level) {
/// case log::info: return "info:";
/// case log::warning: return "warning:";
/// /* A comment. */
/// default: return "";
/// }
/// \endcode
bool AcrossComments;
/// Whether aligned case labels are aligned on the colon, or on the
/// , or on the tokens after the colon.
/// \code
/// true:
/// switch (level) {
/// case log::info : return "info:";
/// case log::warning: return "warning:";
/// default : return "";
/// }
///
/// false:
/// switch (level) {
/// case log::info: return "info:";
/// case log::warning: return "warning:";
/// default: return "";
/// }
/// \endcode
bool AlignCaseColons;
bool operator==(const ShortCaseStatementsAlignmentStyle &R) const {
return Enabled == R.Enabled && AcrossEmptyLines == R.AcrossEmptyLines &&
AcrossComments == R.AcrossComments &&
AlignCaseColons == R.AlignCaseColons;
}
};

/// Style of aligning consecutive short case labels.
/// Only applies if ``AllowShortCaseLabelsOnASingleLine`` is ``true``.
///
/// \code{.yaml}
/// # Example of usage:
/// AlignConsecutiveShortCaseStatements:
/// Enabled: true
/// AcrossEmptyLines: true
/// AcrossComments: true
/// AlignCaseColons: false
/// \endcode
/// \version 17
ShortCaseStatementsAlignmentStyle AlignConsecutiveShortCaseStatements;

/// Different styles for aligning escaped newlines.
enum EscapedNewlineAlignmentStyle : int8_t {
/// Don't align escaped newlines.
Expand Down Expand Up @@ -4357,6 +4454,8 @@ struct FormatStyle {
AlignConsecutiveBitFields == R.AlignConsecutiveBitFields &&
AlignConsecutiveDeclarations == R.AlignConsecutiveDeclarations &&
AlignConsecutiveMacros == R.AlignConsecutiveMacros &&
AlignConsecutiveShortCaseStatements ==
R.AlignConsecutiveShortCaseStatements &&
AlignEscapedNewlines == R.AlignEscapedNewlines &&
AlignOperands == R.AlignOperands &&
AlignTrailingComments == R.AlignTrailingComments &&
Expand Down
14 changes: 14 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,17 @@ template <> struct MappingTraits<FormatStyle::AlignConsecutiveStyle> {
}
};

template <>
struct MappingTraits<FormatStyle::ShortCaseStatementsAlignmentStyle> {
static void mapping(IO &IO,
FormatStyle::ShortCaseStatementsAlignmentStyle &Value) {
IO.mapOptional("Enabled", Value.Enabled);
IO.mapOptional("AcrossEmptyLines", Value.AcrossEmptyLines);
IO.mapOptional("AcrossComments", Value.AcrossComments);
IO.mapOptional("AlignCaseColons", Value.AlignCaseColons);
}
};

template <>
struct ScalarEnumerationTraits<FormatStyle::AttributeBreakingStyle> {
static void enumeration(IO &IO, FormatStyle::AttributeBreakingStyle &Value) {
Expand Down Expand Up @@ -857,6 +868,8 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("AlignConsecutiveDeclarations",
Style.AlignConsecutiveDeclarations);
IO.mapOptional("AlignConsecutiveMacros", Style.AlignConsecutiveMacros);
IO.mapOptional("AlignConsecutiveShortCaseStatements",
Style.AlignConsecutiveShortCaseStatements);
IO.mapOptional("AlignEscapedNewlines", Style.AlignEscapedNewlines);
IO.mapOptional("AlignOperands", Style.AlignOperands);
IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
Expand Down Expand Up @@ -1333,6 +1346,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.AlignConsecutiveBitFields = {};
LLVMStyle.AlignConsecutiveDeclarations = {};
LLVMStyle.AlignConsecutiveMacros = {};
LLVMStyle.AlignConsecutiveShortCaseStatements = {};
LLVMStyle.AlignTrailingComments = {};
LLVMStyle.AlignTrailingComments.Kind = FormatStyle::TCAS_Always;
LLVMStyle.AlignTrailingComments.OverEmptyLines = 0;
Expand Down

0 comments on commit ac6e551

Please sign in to comment.