Skip to content

Commit 046682e

Browse files
authored
[clang-format] Add AlignConsecutiveTableGenCondOperatorColons option. (#82878)
To align colons inside TableGen !cond operators.
1 parent 9c5ca6b commit 046682e

File tree

6 files changed

+199
-3
lines changed

6 files changed

+199
-3
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -955,6 +955,146 @@ the configuration (without a prefix: ``Auto``).
955955
}
956956

957957

958+
.. _AlignConsecutiveTableGenCondOperatorColons:
959+
960+
**AlignConsecutiveTableGenCondOperatorColons** (``AlignConsecutiveStyle``) :versionbadge:`clang-format 19` :ref:`<AlignConsecutiveTableGenCondOperatorColons>`
961+
Style of aligning consecutive TableGen cond operator colons.
962+
Align the colons of cases inside !cond operators.
963+
964+
.. code-block:: c++
965+
966+
!cond(!eq(size, 1) : 1,
967+
!eq(size, 16): 1,
968+
true : 0)
969+
970+
Nested configuration flags:
971+
972+
Alignment options.
973+
974+
They can also be read as a whole for compatibility. The choices are:
975+
- None
976+
- Consecutive
977+
- AcrossEmptyLines
978+
- AcrossComments
979+
- AcrossEmptyLinesAndComments
980+
981+
For example, to align across empty lines and not across comments, either
982+
of these work.
983+
984+
.. code-block:: c++
985+
986+
AlignConsecutiveMacros: AcrossEmptyLines
987+
988+
AlignConsecutiveMacros:
989+
Enabled: true
990+
AcrossEmptyLines: true
991+
AcrossComments: false
992+
993+
* ``bool Enabled`` Whether aligning is enabled.
994+
995+
.. code-block:: c++
996+
997+
#define SHORT_NAME 42
998+
#define LONGER_NAME 0x007f
999+
#define EVEN_LONGER_NAME (2)
1000+
#define foo(x) (x * x)
1001+
#define bar(y, z) (y + z)
1002+
1003+
int a = 1;
1004+
int somelongname = 2;
1005+
double c = 3;
1006+
1007+
int aaaa : 1;
1008+
int b : 12;
1009+
int ccc : 8;
1010+
1011+
int aaaa = 12;
1012+
float b = 23;
1013+
std::string ccc;
1014+
1015+
* ``bool AcrossEmptyLines`` Whether to align across empty lines.
1016+
1017+
.. code-block:: c++
1018+
1019+
true:
1020+
int a = 1;
1021+
int somelongname = 2;
1022+
double c = 3;
1023+
1024+
int d = 3;
1025+
1026+
false:
1027+
int a = 1;
1028+
int somelongname = 2;
1029+
double c = 3;
1030+
1031+
int d = 3;
1032+
1033+
* ``bool AcrossComments`` Whether to align across comments.
1034+
1035+
.. code-block:: c++
1036+
1037+
true:
1038+
int d = 3;
1039+
/* A comment. */
1040+
double e = 4;
1041+
1042+
false:
1043+
int d = 3;
1044+
/* A comment. */
1045+
double e = 4;
1046+
1047+
* ``bool AlignCompound`` Only for ``AlignConsecutiveAssignments``. Whether compound assignments
1048+
like ``+=`` are aligned along with ``=``.
1049+
1050+
.. code-block:: c++
1051+
1052+
true:
1053+
a &= 2;
1054+
bbb = 2;
1055+
1056+
false:
1057+
a &= 2;
1058+
bbb = 2;
1059+
1060+
* ``bool AlignFunctionPointers`` Only for ``AlignConsecutiveDeclarations``. Whether function pointers are
1061+
aligned.
1062+
1063+
.. code-block:: c++
1064+
1065+
true:
1066+
unsigned i;
1067+
int &r;
1068+
int *p;
1069+
int (*f)();
1070+
1071+
false:
1072+
unsigned i;
1073+
int &r;
1074+
int *p;
1075+
int (*f)();
1076+
1077+
* ``bool PadOperators`` Only for ``AlignConsecutiveAssignments``. Whether short assignment
1078+
operators are left-padded to the same length as long ones in order to
1079+
put all assignment operators to the right of the left hand side.
1080+
1081+
.. code-block:: c++
1082+
1083+
true:
1084+
a >>= 2;
1085+
bbb = 2;
1086+
1087+
a = 2;
1088+
bbb >>= 2;
1089+
1090+
false:
1091+
a >>= 2;
1092+
bbb = 2;
1093+
1094+
a = 2;
1095+
bbb >>= 2;
1096+
1097+
9581098
.. _AlignEscapedNewlines:
9591099

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

clang/include/clang/Format/Format.h

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,16 @@ struct FormatStyle {
414414
/// \version 17
415415
ShortCaseStatementsAlignmentStyle AlignConsecutiveShortCaseStatements;
416416

417+
/// Style of aligning consecutive TableGen cond operator colons.
418+
/// Align the colons of cases inside !cond operators.
419+
/// \code
420+
/// !cond(!eq(size, 1) : 1,
421+
/// !eq(size, 16): 1,
422+
/// true : 0)
423+
/// \endcode
424+
/// \version 19
425+
AlignConsecutiveStyle AlignConsecutiveTableGenCondOperatorColons;
426+
417427
/// Different styles for aligning escaped newlines.
418428
enum EscapedNewlineAlignmentStyle : int8_t {
419429
/// Don't align escaped newlines.
@@ -4805,6 +4815,8 @@ struct FormatStyle {
48054815
AlignConsecutiveMacros == R.AlignConsecutiveMacros &&
48064816
AlignConsecutiveShortCaseStatements ==
48074817
R.AlignConsecutiveShortCaseStatements &&
4818+
AlignConsecutiveTableGenCondOperatorColons ==
4819+
R.AlignConsecutiveTableGenCondOperatorColons &&
48084820
AlignEscapedNewlines == R.AlignEscapedNewlines &&
48094821
AlignOperands == R.AlignOperands &&
48104822
AlignTrailingComments == R.AlignTrailingComments &&

clang/lib/Format/Format.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -915,6 +915,8 @@ template <> struct MappingTraits<FormatStyle> {
915915
IO.mapOptional("AlignConsecutiveMacros", Style.AlignConsecutiveMacros);
916916
IO.mapOptional("AlignConsecutiveShortCaseStatements",
917917
Style.AlignConsecutiveShortCaseStatements);
918+
IO.mapOptional("AlignConsecutiveTableGenCondOperatorColons",
919+
Style.AlignConsecutiveTableGenCondOperatorColons);
918920
IO.mapOptional("AlignEscapedNewlines", Style.AlignEscapedNewlines);
919921
IO.mapOptional("AlignOperands", Style.AlignOperands);
920922
IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
@@ -1420,6 +1422,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
14201422
LLVMStyle.AlignConsecutiveDeclarations = {};
14211423
LLVMStyle.AlignConsecutiveMacros = {};
14221424
LLVMStyle.AlignConsecutiveShortCaseStatements = {};
1425+
LLVMStyle.AlignConsecutiveTableGenCondOperatorColons = {};
14231426
LLVMStyle.AlignEscapedNewlines = FormatStyle::ENAS_Right;
14241427
LLVMStyle.AlignOperands = FormatStyle::OAS_Align;
14251428
LLVMStyle.AlignTrailingComments = {};

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ const tooling::Replacements &WhitespaceManager::generateReplacements() {
111111
alignConsecutiveDeclarations();
112112
alignConsecutiveBitFields();
113113
alignConsecutiveAssignments();
114+
if (Style.isTableGen())
115+
alignConsecutiveTableGenCondOperatorColons();
114116
alignChainedConditionals();
115117
alignTrailingComments();
116118
alignEscapedNewlines();
@@ -849,7 +851,12 @@ void WhitespaceManager::alignConsecutiveAssignments() {
849851
}
850852

851853
void WhitespaceManager::alignConsecutiveBitFields() {
852-
if (!Style.AlignConsecutiveBitFields.Enabled)
854+
alignConsecutiveColons(Style.AlignConsecutiveBitFields, TT_BitFieldColon);
855+
}
856+
857+
void WhitespaceManager::alignConsecutiveColons(
858+
const FormatStyle::AlignConsecutiveStyle &AlignStyle, TokenType Type) {
859+
if (!AlignStyle.Enabled)
853860
return;
854861

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

866-
return C.Tok->is(TT_BitFieldColon);
873+
return C.Tok->is(Type);
867874
},
868-
Changes, /*StartAt=*/0, Style.AlignConsecutiveBitFields);
875+
Changes, /*StartAt=*/0, AlignStyle);
869876
}
870877

871878
void WhitespaceManager::alignConsecutiveShortCaseStatements() {
@@ -972,6 +979,11 @@ void WhitespaceManager::alignConsecutiveShortCaseStatements() {
972979
Changes);
973980
}
974981

982+
void WhitespaceManager::alignConsecutiveTableGenCondOperatorColons() {
983+
alignConsecutiveColons(Style.AlignConsecutiveTableGenCondOperatorColons,
984+
TT_TableGenCondOperatorColon);
985+
}
986+
975987
void WhitespaceManager::alignConsecutiveDeclarations() {
976988
if (!Style.AlignConsecutiveDeclarations.Enabled)
977989
return;

clang/lib/Format/WhitespaceManager.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,11 @@ class WhitespaceManager {
226226
/// Align consecutive bitfields over all \c Changes.
227227
void alignConsecutiveBitFields();
228228

229+
/// Align consecutive colon. For bitfields, TableGen DAGArgs and defintions.
230+
void
231+
alignConsecutiveColons(const FormatStyle::AlignConsecutiveStyle &AlignStyle,
232+
TokenType Type);
233+
229234
/// Align consecutive declarations over all \c Changes.
230235
void alignConsecutiveDeclarations();
231236

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

243+
/// Align consecutive TableGen cond operator colon over all \c Changes.
244+
void alignConsecutiveTableGenCondOperatorColons();
245+
238246
/// Align trailing comments over all \c Changes.
239247
void alignTrailingComments();
240248

clang/unittests/Format/FormatTestTableGen.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,13 @@ class FormatTestTableGen : public ::testing::Test {
4444
static void verifyFormat(llvm::StringRef Result, llvm::StringRef MessedUp) {
4545
EXPECT_EQ(Result, format(MessedUp));
4646
}
47+
48+
static void verifyFormat(llvm::StringRef Code, const FormatStyle &Style) {
49+
EXPECT_EQ(Code.str(), format(Code, 0, Code.size(), Style))
50+
<< "Expected code is not stable";
51+
auto MessUp = test::messUp(Code);
52+
EXPECT_EQ(Code.str(), format(MessUp, 0, MessUp.size(), Style));
53+
}
4754
};
4855

4956
TEST_F(FormatTestTableGen, FormatStringBreak) {
@@ -325,5 +332,19 @@ TEST_F(FormatTestTableGen, Assert) {
325332
verifyFormat("assert !le(DefVar1, 0), \"Assert1\";\n");
326333
}
327334

335+
TEST_F(FormatTestTableGen, CondOperatorAlignment) {
336+
FormatStyle Style = getGoogleStyle(FormatStyle::LK_TableGen);
337+
Style.ColumnLimit = 60;
338+
verifyFormat("let CondOpe1 = !cond(!eq(size, 1): 1,\n"
339+
" !eq(size, 16): 1,\n"
340+
" true: 0);\n",
341+
Style);
342+
Style.AlignConsecutiveTableGenCondOperatorColons.Enabled = true;
343+
verifyFormat("let CondOpe1 = !cond(!eq(size, 1) : 1,\n"
344+
" !eq(size, 16): 1,\n"
345+
" true : 0);\n",
346+
Style);
347+
}
348+
328349
} // namespace format
329350
} // end namespace clang

0 commit comments

Comments
 (0)