Skip to content

[clang-format] Add AlignConsecutiveTableGenCondOperatorColons option. #82878

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
140 changes: 140 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -955,6 +955,146 @@ the configuration (without a prefix: ``Auto``).
}


.. _AlignConsecutiveTableGenCondOperatorColons:

**AlignConsecutiveTableGenCondOperatorColons** (``AlignConsecutiveStyle``) :versionbadge:`clang-format 19` :ref:`¶ <AlignConsecutiveTableGenCondOperatorColons>`
Style of aligning consecutive TableGen cond operator colons.
Align the colons of cases inside !cond operators.

.. code-block:: c++

!cond(!eq(size, 1) : 1,
!eq(size, 16): 1,
true : 0)

Nested configuration flags:

Alignment options.

They can also be read as a whole for compatibility. The choices are:
- None
- Consecutive
- AcrossEmptyLines
- AcrossComments
- AcrossEmptyLinesAndComments

For example, to align across empty lines and not across comments, either
of these work.

.. code-block:: c++

AlignConsecutiveMacros: AcrossEmptyLines

AlignConsecutiveMacros:
Enabled: true
AcrossEmptyLines: true
AcrossComments: false

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

.. code-block:: c++

#define SHORT_NAME 42
#define LONGER_NAME 0x007f
#define EVEN_LONGER_NAME (2)
#define foo(x) (x * x)
#define bar(y, z) (y + z)

int a = 1;
int somelongname = 2;
double c = 3;

int aaaa : 1;
int b : 12;
int ccc : 8;

int aaaa = 12;
float b = 23;
std::string ccc;

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

.. code-block:: c++

true:
int a = 1;
int somelongname = 2;
double c = 3;

int d = 3;

false:
int a = 1;
int somelongname = 2;
double c = 3;

int d = 3;

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

.. code-block:: c++

true:
int d = 3;
/* A comment. */
double e = 4;

false:
int d = 3;
/* A comment. */
double e = 4;

* ``bool AlignCompound`` Only for ``AlignConsecutiveAssignments``. Whether compound assignments
like ``+=`` are aligned along with ``=``.

.. code-block:: c++

true:
a &= 2;
bbb = 2;

false:
a &= 2;
bbb = 2;

* ``bool AlignFunctionPointers`` Only for ``AlignConsecutiveDeclarations``. Whether function pointers are
aligned.

.. code-block:: c++

true:
unsigned i;
int &r;
int *p;
int (*f)();

false:
unsigned i;
int &r;
int *p;
int (*f)();

* ``bool PadOperators`` Only for ``AlignConsecutiveAssignments``. Whether short assignment
operators are left-padded to the same length as long ones in order to
put all assignment operators to the right of the left hand side.

.. code-block:: c++

true:
a >>= 2;
bbb = 2;

a = 2;
bbb >>= 2;

false:
a >>= 2;
bbb = 2;

a = 2;
bbb >>= 2;


.. _AlignEscapedNewlines:

**AlignEscapedNewlines** (``EscapedNewlineAlignmentStyle``) :versionbadge:`clang-format 5` :ref:`¶ <AlignEscapedNewlines>`
Expand Down
12 changes: 12 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -414,6 +414,16 @@ struct FormatStyle {
/// \version 17
ShortCaseStatementsAlignmentStyle AlignConsecutiveShortCaseStatements;

/// Style of aligning consecutive TableGen cond operator colons.
/// Align the colons of cases inside !cond operators.
/// \code
/// !cond(!eq(size, 1) : 1,
/// !eq(size, 16): 1,
/// true : 0)
/// \endcode
/// \version 19
AlignConsecutiveStyle AlignConsecutiveTableGenCondOperatorColons;

/// Different styles for aligning escaped newlines.
enum EscapedNewlineAlignmentStyle : int8_t {
/// Don't align escaped newlines.
Expand Down Expand Up @@ -4804,6 +4814,8 @@ struct FormatStyle {
AlignConsecutiveMacros == R.AlignConsecutiveMacros &&
AlignConsecutiveShortCaseStatements ==
R.AlignConsecutiveShortCaseStatements &&
AlignConsecutiveTableGenCondOperatorColons ==
R.AlignConsecutiveTableGenCondOperatorColons &&
AlignEscapedNewlines == R.AlignEscapedNewlines &&
AlignOperands == R.AlignOperands &&
AlignTrailingComments == R.AlignTrailingComments &&
Expand Down
3 changes: 3 additions & 0 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,8 @@ template <> struct MappingTraits<FormatStyle> {
IO.mapOptional("AlignConsecutiveMacros", Style.AlignConsecutiveMacros);
IO.mapOptional("AlignConsecutiveShortCaseStatements",
Style.AlignConsecutiveShortCaseStatements);
IO.mapOptional("AlignConsecutiveTableGenCondOperatorColons",
Style.AlignConsecutiveTableGenCondOperatorColons);
IO.mapOptional("AlignEscapedNewlines", Style.AlignEscapedNewlines);
IO.mapOptional("AlignOperands", Style.AlignOperands);
IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
Expand Down Expand Up @@ -1420,6 +1422,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.AlignConsecutiveDeclarations = {};
LLVMStyle.AlignConsecutiveMacros = {};
LLVMStyle.AlignConsecutiveShortCaseStatements = {};
LLVMStyle.AlignConsecutiveTableGenCondOperatorColons = {};
LLVMStyle.AlignEscapedNewlines = FormatStyle::ENAS_Right;
LLVMStyle.AlignOperands = FormatStyle::OAS_Align;
LLVMStyle.AlignTrailingComments = {};
Expand Down
18 changes: 15 additions & 3 deletions clang/lib/Format/WhitespaceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ const tooling::Replacements &WhitespaceManager::generateReplacements() {
alignConsecutiveDeclarations();
alignConsecutiveBitFields();
alignConsecutiveAssignments();
if (Style.isTableGen())
alignConsecutiveTableGenCondOperatorColons();
alignChainedConditionals();
alignTrailingComments();
alignEscapedNewlines();
Expand Down Expand Up @@ -849,7 +851,12 @@ void WhitespaceManager::alignConsecutiveAssignments() {
}

void WhitespaceManager::alignConsecutiveBitFields() {
if (!Style.AlignConsecutiveBitFields.Enabled)
alignConsecutiveColons(Style.AlignConsecutiveBitFields, TT_BitFieldColon);
}

void WhitespaceManager::alignConsecutiveColons(
const FormatStyle::AlignConsecutiveStyle &AlignStyle, TokenType Type) {
if (!AlignStyle.Enabled)
return;

AlignTokens(
Expand All @@ -863,9 +870,9 @@ void WhitespaceManager::alignConsecutiveBitFields() {
if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
return false;

return C.Tok->is(TT_BitFieldColon);
return C.Tok->is(Type);
},
Changes, /*StartAt=*/0, Style.AlignConsecutiveBitFields);
Changes, /*StartAt=*/0, AlignStyle);
}

void WhitespaceManager::alignConsecutiveShortCaseStatements() {
Expand Down Expand Up @@ -972,6 +979,11 @@ void WhitespaceManager::alignConsecutiveShortCaseStatements() {
Changes);
}

void WhitespaceManager::alignConsecutiveTableGenCondOperatorColons() {
alignConsecutiveColons(Style.AlignConsecutiveTableGenCondOperatorColons,
TT_TableGenCondOperatorColon);
}

void WhitespaceManager::alignConsecutiveDeclarations() {
if (!Style.AlignConsecutiveDeclarations.Enabled)
return;
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/Format/WhitespaceManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,11 @@ class WhitespaceManager {
/// Align consecutive bitfields over all \c Changes.
void alignConsecutiveBitFields();

/// Align consecutive colon. For bitfields, TableGen DAGArgs and defintions.
void
alignConsecutiveColons(const FormatStyle::AlignConsecutiveStyle &AlignStyle,
TokenType Type);

/// Align consecutive declarations over all \c Changes.
void alignConsecutiveDeclarations();

Expand All @@ -235,6 +240,9 @@ class WhitespaceManager {
/// Align consecutive short case statements over all \c Changes.
void alignConsecutiveShortCaseStatements();

/// Align consecutive TableGen cond operator colon over all \c Changes.
void alignConsecutiveTableGenCondOperatorColons();

/// Align trailing comments over all \c Changes.
void alignTrailingComments();

Expand Down
21 changes: 21 additions & 0 deletions clang/unittests/Format/FormatTestTableGen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ class FormatTestTableGen : public ::testing::Test {
static void verifyFormat(llvm::StringRef Result, llvm::StringRef MessedUp) {
EXPECT_EQ(Result, format(MessedUp));
}

static void verifyFormat(llvm::StringRef Code, const FormatStyle &Style) {
EXPECT_EQ(Code.str(), format(Code, 0, Code.size(), Style))
<< "Expected code is not stable";
auto MessUp = test::messUp(Code);
EXPECT_EQ(Code.str(), format(MessUp, 0, MessUp.size(), Style));
}
};

TEST_F(FormatTestTableGen, FormatStringBreak) {
Expand Down Expand Up @@ -325,5 +332,19 @@ TEST_F(FormatTestTableGen, Assert) {
verifyFormat("assert !le(DefVar1, 0), \"Assert1\";\n");
}

TEST_F(FormatTestTableGen, CondOperatorAlignment) {
FormatStyle Style = getGoogleStyle(FormatStyle::LK_TableGen);
Style.ColumnLimit = 60;
verifyFormat("let CondOpe1 = !cond(!eq(size, 1): 1,\n"
" !eq(size, 16): 1,\n"
" true: 0);\n",
Style);
Style.AlignConsecutiveTableGenCondOperatorColons.Enabled = true;
verifyFormat("let CondOpe1 = !cond(!eq(size, 1) : 1,\n"
" !eq(size, 16): 1,\n"
" true : 0);\n",
Style);
}

} // namespace format
} // end namespace clang