Skip to content

Commit 87e5c75

Browse files
hnakamura5chencha3
authored andcommitted
[clang-format] Added AlignConsecutiveTableGenBreakingDAGArgColons option. (llvm#86150)
The option to specify the style of alignment of the colons inside TableGen's DAGArg.
1 parent 1c50964 commit 87e5c75

File tree

9 files changed

+236
-5
lines changed

9 files changed

+236
-5
lines changed

clang/docs/ClangFormatStyleOptions.rst

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

957957

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

9601105
**AlignConsecutiveTableGenCondOperatorColons** (``AlignConsecutiveStyle``) :versionbadge:`clang-format 19` :ref:`<AlignConsecutiveTableGenCondOperatorColons>`

clang/include/clang/Format/Format.h

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

417+
/// Style of aligning consecutive TableGen DAGArg operator colons.
418+
/// If enabled, align the colon inside DAGArg which have line break inside.
419+
/// This works only when TableGenBreakInsideDAGArg is BreakElements or
420+
/// BreakAll and the DAGArg is not excepted by
421+
/// TableGenBreakingDAGArgOperators's effect.
422+
/// \code
423+
/// let dagarg = (ins
424+
/// a :$src1,
425+
/// aa :$src2,
426+
/// aaa:$src3
427+
/// )
428+
/// \endcode
429+
/// \version 19
430+
AlignConsecutiveStyle AlignConsecutiveTableGenBreakingDAGArgColons;
431+
417432
/// Style of aligning consecutive TableGen cond operator colons.
418433
/// Align the colons of cases inside !cond operators.
419434
/// \code
@@ -4879,6 +4894,8 @@ struct FormatStyle {
48794894
AlignConsecutiveMacros == R.AlignConsecutiveMacros &&
48804895
AlignConsecutiveShortCaseStatements ==
48814896
R.AlignConsecutiveShortCaseStatements &&
4897+
AlignConsecutiveTableGenBreakingDAGArgColons ==
4898+
R.AlignConsecutiveTableGenBreakingDAGArgColons &&
48824899
AlignConsecutiveTableGenCondOperatorColons ==
48834900
R.AlignConsecutiveTableGenCondOperatorColons &&
48844901
AlignConsecutiveTableGenDefinitionColons ==

clang/lib/Format/Format.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -895,6 +895,8 @@ template <> struct MappingTraits<FormatStyle> {
895895
IO.mapOptional("AlignConsecutiveMacros", Style.AlignConsecutiveMacros);
896896
IO.mapOptional("AlignConsecutiveShortCaseStatements",
897897
Style.AlignConsecutiveShortCaseStatements);
898+
IO.mapOptional("AlignConsecutiveTableGenBreakingDAGArgColons",
899+
Style.AlignConsecutiveTableGenBreakingDAGArgColons);
898900
IO.mapOptional("AlignConsecutiveTableGenCondOperatorColons",
899901
Style.AlignConsecutiveTableGenCondOperatorColons);
900902
IO.mapOptional("AlignConsecutiveTableGenDefinitionColons",
@@ -1408,6 +1410,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
14081410
LLVMStyle.AlignConsecutiveDeclarations = {};
14091411
LLVMStyle.AlignConsecutiveMacros = {};
14101412
LLVMStyle.AlignConsecutiveShortCaseStatements = {};
1413+
LLVMStyle.AlignConsecutiveTableGenBreakingDAGArgColons = {};
14111414
LLVMStyle.AlignConsecutiveTableGenCondOperatorColons = {};
14121415
LLVMStyle.AlignConsecutiveTableGenDefinitionColons = {};
14131416
LLVMStyle.AlignEscapedNewlines = FormatStyle::ENAS_Right;

clang/lib/Format/FormatToken.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ namespace format {
152152
TYPE(TableGenCondOperatorComma) \
153153
TYPE(TableGenDAGArgCloser) \
154154
TYPE(TableGenDAGArgListColon) \
155+
TYPE(TableGenDAGArgListColonToAlign) \
155156
TYPE(TableGenDAGArgListComma) \
156157
TYPE(TableGenDAGArgListCommaToBreak) \
157158
TYPE(TableGenDAGArgOpener) \

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -975,12 +975,15 @@ class AnnotatingParser {
975975

976976
// DagArg ::= Value [":" TokVarName] | TokVarName
977977
// Appears as a part of SimpleValue6.
978-
bool parseTableGenDAGArg() {
978+
bool parseTableGenDAGArg(bool AlignColon = false) {
979979
if (tryToParseTableGenTokVar())
980980
return true;
981981
if (parseTableGenValue()) {
982982
if (CurrentToken && CurrentToken->is(tok::colon)) {
983-
CurrentToken->setType(TT_TableGenDAGArgListColon);
983+
if (AlignColon)
984+
CurrentToken->setType(TT_TableGenDAGArgListColonToAlign);
985+
else
986+
CurrentToken->setType(TT_TableGenDAGArgListColon);
984987
skipToNextNonComment();
985988
return tryToParseTableGenTokVar();
986989
}
@@ -1051,8 +1054,11 @@ class AnnotatingParser {
10511054
skipToNextNonComment();
10521055
return true;
10531056
}
1054-
if (!parseTableGenDAGArg())
1057+
if (!parseTableGenDAGArg(
1058+
BreakInside &&
1059+
Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled)) {
10551060
return false;
1061+
}
10561062
FirstDAGArgListElm = false;
10571063
}
10581064
return false;
@@ -5130,8 +5136,10 @@ bool TokenAnnotator::spaceRequiredBefore(const AnnotatedLine &Line,
51305136
if (Left.is(tok::r_brace) && Right.is(tok::r_square))
51315137
return true;
51325138
// Do not insert around colon in DAGArg and cond operator.
5133-
if (Right.is(TT_TableGenDAGArgListColon) ||
5134-
Left.is(TT_TableGenDAGArgListColon)) {
5139+
if (Right.isOneOf(TT_TableGenDAGArgListColon,
5140+
TT_TableGenDAGArgListColonToAlign) ||
5141+
Left.isOneOf(TT_TableGenDAGArgListColon,
5142+
TT_TableGenDAGArgListColonToAlign)) {
51355143
return false;
51365144
}
51375145
if (Right.is(TT_TableGenCondOperatorColon))

clang/lib/Format/WhitespaceManager.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ const tooling::Replacements &WhitespaceManager::generateReplacements() {
112112
alignConsecutiveBitFields();
113113
alignConsecutiveAssignments();
114114
if (Style.isTableGen()) {
115+
alignConsecutiveTableGenBreakingDAGArgColons();
115116
alignConsecutiveTableGenCondOperatorColons();
116117
alignConsecutiveTableGenDefinitions();
117118
}
@@ -981,6 +982,11 @@ void WhitespaceManager::alignConsecutiveShortCaseStatements() {
981982
Changes);
982983
}
983984

985+
void WhitespaceManager::alignConsecutiveTableGenBreakingDAGArgColons() {
986+
alignConsecutiveColons(Style.AlignConsecutiveTableGenBreakingDAGArgColons,
987+
TT_TableGenDAGArgListColonToAlign);
988+
}
989+
984990
void WhitespaceManager::alignConsecutiveTableGenCondOperatorColons() {
985991
alignConsecutiveColons(Style.AlignConsecutiveTableGenCondOperatorColons,
986992
TT_TableGenCondOperatorColon);

clang/lib/Format/WhitespaceManager.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,9 @@ class WhitespaceManager {
235235
/// Align consecutive short case statements over all \c Changes.
236236
void alignConsecutiveShortCaseStatements();
237237

238+
/// Align consecutive TableGen DAGArg colon over all \c Changes.
239+
void alignConsecutiveTableGenBreakingDAGArgColons();
240+
238241
/// Align consecutive TableGen cond operator colon over all \c Changes.
239242
void alignConsecutiveTableGenCondOperatorColons();
240243

clang/unittests/Format/FormatTestTableGen.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -411,6 +411,38 @@ TEST_F(FormatTestTableGen, DAGArgBreakAll) {
411411
Style);
412412
}
413413

414+
TEST_F(FormatTestTableGen, DAGArgAlignment) {
415+
FormatStyle Style = getGoogleStyle(FormatStyle::LK_TableGen);
416+
Style.ColumnLimit = 60;
417+
Style.TableGenBreakInsideDAGArg = FormatStyle::DAS_BreakAll;
418+
Style.TableGenBreakingDAGArgOperators = {"ins", "outs"};
419+
verifyFormat("def Def : Parent {\n"
420+
" let dagarg = (ins\n"
421+
" a:$src1,\n"
422+
" aa:$src2,\n"
423+
" aaa:$src3\n"
424+
" )\n"
425+
"}\n",
426+
Style);
427+
verifyFormat("def Def : Parent {\n"
428+
" let dagarg = (not a:$src1, aa:$src2, aaa:$src2)\n"
429+
"}\n",
430+
Style);
431+
Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled = true;
432+
verifyFormat("def Def : Parent {\n"
433+
" let dagarg = (ins\n"
434+
" a :$src1,\n"
435+
" aa :$src2,\n"
436+
" aaa:$src3\n"
437+
" )\n"
438+
"}\n",
439+
Style);
440+
verifyFormat("def Def : Parent {\n"
441+
" let dagarg = (not a:$src1, aa:$src2, aaa:$src2)\n"
442+
"}\n",
443+
Style);
444+
}
445+
414446
TEST_F(FormatTestTableGen, CondOperatorAlignment) {
415447
FormatStyle Style = getGoogleStyle(FormatStyle::LK_TableGen);
416448
Style.ColumnLimit = 60;

clang/unittests/Format/TokenAnnotatorTest.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,6 +2424,22 @@ TEST_F(TokenAnnotatorTest, UnderstandTableGenTokens) {
24242424
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_Unknown); // other
24252425
EXPECT_TOKEN(Tokens[5], tok::comma, TT_TableGenDAGArgListComma);
24262426
EXPECT_TOKEN(Tokens[9], tok::r_paren, TT_TableGenDAGArgCloser);
2427+
2428+
// If TableGenBreakingDAGArgOperators is enabled, it uses
2429+
// TT_TableGenDAGArgListColonToAlign to annotate the colon to align.
2430+
Style.AlignConsecutiveTableGenBreakingDAGArgColons.Enabled = true;
2431+
Tokens = AnnotateValue("(ins type1:$src1, type2:$src2)");
2432+
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2433+
EXPECT_TOKEN(Tokens[1], tok::identifier,
2434+
TT_TableGenDAGArgOperatorToBreak); // ins
2435+
EXPECT_TOKEN(Tokens[3], tok::colon, TT_TableGenDAGArgListColonToAlign);
2436+
EXPECT_TOKEN(Tokens[7], tok::colon, TT_TableGenDAGArgListColonToAlign);
2437+
2438+
Tokens = AnnotateValue("(other type1:$src1, type2:$src2)");
2439+
ASSERT_EQ(Tokens.size(), 10u) << Tokens;
2440+
EXPECT_TOKEN(Tokens[1], tok::identifier, TT_Unknown); // other
2441+
EXPECT_TOKEN(Tokens[3], tok::colon, TT_TableGenDAGArgListColon);
2442+
EXPECT_TOKEN(Tokens[7], tok::colon, TT_TableGenDAGArgListColon);
24272443
}
24282444

24292445
TEST_F(TokenAnnotatorTest, UnderstandConstructors) {

0 commit comments

Comments
 (0)