Skip to content

Commit 19cec9c

Browse files
authored
[clang-format] Add AlignConsecutiveTableGenDefinitions option. (#83008)
To align TableGen consecutive definitions.
1 parent a28a7d4 commit 19cec9c

File tree

6 files changed

+180
-1
lines changed

6 files changed

+180
-1
lines changed

clang/docs/ClangFormatStyleOptions.rst

+140
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,146 @@ the configuration (without a prefix: ``Auto``).
10951095
bbb >>= 2;
10961096

10971097

1098+
.. _AlignConsecutiveTableGenDefinitionColons:
1099+
1100+
**AlignConsecutiveTableGenDefinitionColons** (``AlignConsecutiveStyle``) :versionbadge:`clang-format 19` :ref:`<AlignConsecutiveTableGenDefinitionColons>`
1101+
Style of aligning consecutive TableGen definition colons.
1102+
This aligns the inheritance colons of consecutive definitions.
1103+
1104+
.. code-block:: c++
1105+
1106+
def Def : Parent {}
1107+
def DefDef : Parent {}
1108+
def DefDefDef : Parent {}
1109+
1110+
Nested configuration flags:
1111+
1112+
Alignment options.
1113+
1114+
They can also be read as a whole for compatibility. The choices are:
1115+
- None
1116+
- Consecutive
1117+
- AcrossEmptyLines
1118+
- AcrossComments
1119+
- AcrossEmptyLinesAndComments
1120+
1121+
For example, to align across empty lines and not across comments, either
1122+
of these work.
1123+
1124+
.. code-block:: c++
1125+
1126+
AlignConsecutiveMacros: AcrossEmptyLines
1127+
1128+
AlignConsecutiveMacros:
1129+
Enabled: true
1130+
AcrossEmptyLines: true
1131+
AcrossComments: false
1132+
1133+
* ``bool Enabled`` Whether aligning is enabled.
1134+
1135+
.. code-block:: c++
1136+
1137+
#define SHORT_NAME 42
1138+
#define LONGER_NAME 0x007f
1139+
#define EVEN_LONGER_NAME (2)
1140+
#define foo(x) (x * x)
1141+
#define bar(y, z) (y + z)
1142+
1143+
int a = 1;
1144+
int somelongname = 2;
1145+
double c = 3;
1146+
1147+
int aaaa : 1;
1148+
int b : 12;
1149+
int ccc : 8;
1150+
1151+
int aaaa = 12;
1152+
float b = 23;
1153+
std::string ccc;
1154+
1155+
* ``bool AcrossEmptyLines`` Whether to align across empty lines.
1156+
1157+
.. code-block:: c++
1158+
1159+
true:
1160+
int a = 1;
1161+
int somelongname = 2;
1162+
double c = 3;
1163+
1164+
int d = 3;
1165+
1166+
false:
1167+
int a = 1;
1168+
int somelongname = 2;
1169+
double c = 3;
1170+
1171+
int d = 3;
1172+
1173+
* ``bool AcrossComments`` Whether to align across comments.
1174+
1175+
.. code-block:: c++
1176+
1177+
true:
1178+
int d = 3;
1179+
/* A comment. */
1180+
double e = 4;
1181+
1182+
false:
1183+
int d = 3;
1184+
/* A comment. */
1185+
double e = 4;
1186+
1187+
* ``bool AlignCompound`` Only for ``AlignConsecutiveAssignments``. Whether compound assignments
1188+
like ``+=`` are aligned along with ``=``.
1189+
1190+
.. code-block:: c++
1191+
1192+
true:
1193+
a &= 2;
1194+
bbb = 2;
1195+
1196+
false:
1197+
a &= 2;
1198+
bbb = 2;
1199+
1200+
* ``bool AlignFunctionPointers`` Only for ``AlignConsecutiveDeclarations``. Whether function pointers are
1201+
aligned.
1202+
1203+
.. code-block:: c++
1204+
1205+
true:
1206+
unsigned i;
1207+
int &r;
1208+
int *p;
1209+
int (*f)();
1210+
1211+
false:
1212+
unsigned i;
1213+
int &r;
1214+
int *p;
1215+
int (*f)();
1216+
1217+
* ``bool PadOperators`` Only for ``AlignConsecutiveAssignments``. Whether short assignment
1218+
operators are left-padded to the same length as long ones in order to
1219+
put all assignment operators to the right of the left hand side.
1220+
1221+
.. code-block:: c++
1222+
1223+
true:
1224+
a >>= 2;
1225+
bbb = 2;
1226+
1227+
a = 2;
1228+
bbb >>= 2;
1229+
1230+
false:
1231+
a >>= 2;
1232+
bbb = 2;
1233+
1234+
a = 2;
1235+
bbb >>= 2;
1236+
1237+
10981238
.. _AlignEscapedNewlines:
10991239

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

clang/include/clang/Format/Format.h

+12
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,16 @@ struct FormatStyle {
424424
/// \version 19
425425
AlignConsecutiveStyle AlignConsecutiveTableGenCondOperatorColons;
426426

427+
/// Style of aligning consecutive TableGen definition colons.
428+
/// This aligns the inheritance colons of consecutive definitions.
429+
/// \code
430+
/// def Def : Parent {}
431+
/// def DefDef : Parent {}
432+
/// def DefDefDef : Parent {}
433+
/// \endcode
434+
/// \version 19
435+
AlignConsecutiveStyle AlignConsecutiveTableGenDefinitionColons;
436+
427437
/// Different styles for aligning escaped newlines.
428438
enum EscapedNewlineAlignmentStyle : int8_t {
429439
/// Don't align escaped newlines.
@@ -4817,6 +4827,8 @@ struct FormatStyle {
48174827
R.AlignConsecutiveShortCaseStatements &&
48184828
AlignConsecutiveTableGenCondOperatorColons ==
48194829
R.AlignConsecutiveTableGenCondOperatorColons &&
4830+
AlignConsecutiveTableGenDefinitionColons ==
4831+
R.AlignConsecutiveTableGenDefinitionColons &&
48204832
AlignEscapedNewlines == R.AlignEscapedNewlines &&
48214833
AlignOperands == R.AlignOperands &&
48224834
AlignTrailingComments == R.AlignTrailingComments &&

clang/lib/Format/Format.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -917,6 +917,8 @@ template <> struct MappingTraits<FormatStyle> {
917917
Style.AlignConsecutiveShortCaseStatements);
918918
IO.mapOptional("AlignConsecutiveTableGenCondOperatorColons",
919919
Style.AlignConsecutiveTableGenCondOperatorColons);
920+
IO.mapOptional("AlignConsecutiveTableGenDefinitionColons",
921+
Style.AlignConsecutiveTableGenDefinitionColons);
920922
IO.mapOptional("AlignEscapedNewlines", Style.AlignEscapedNewlines);
921923
IO.mapOptional("AlignOperands", Style.AlignOperands);
922924
IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
@@ -1423,6 +1425,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
14231425
LLVMStyle.AlignConsecutiveMacros = {};
14241426
LLVMStyle.AlignConsecutiveShortCaseStatements = {};
14251427
LLVMStyle.AlignConsecutiveTableGenCondOperatorColons = {};
1428+
LLVMStyle.AlignConsecutiveTableGenDefinitionColons = {};
14261429
LLVMStyle.AlignEscapedNewlines = FormatStyle::ENAS_Right;
14271430
LLVMStyle.AlignOperands = FormatStyle::OAS_Align;
14281431
LLVMStyle.AlignTrailingComments = {};

clang/lib/Format/WhitespaceManager.cpp

+8-1
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,10 @@ const tooling::Replacements &WhitespaceManager::generateReplacements() {
111111
alignConsecutiveDeclarations();
112112
alignConsecutiveBitFields();
113113
alignConsecutiveAssignments();
114-
if (Style.isTableGen())
114+
if (Style.isTableGen()) {
115115
alignConsecutiveTableGenCondOperatorColons();
116+
alignConsecutiveTableGenDefinitions();
117+
}
116118
alignChainedConditionals();
117119
alignTrailingComments();
118120
alignEscapedNewlines();
@@ -984,6 +986,11 @@ void WhitespaceManager::alignConsecutiveTableGenCondOperatorColons() {
984986
TT_TableGenCondOperatorColon);
985987
}
986988

989+
void WhitespaceManager::alignConsecutiveTableGenDefinitions() {
990+
alignConsecutiveColons(Style.AlignConsecutiveTableGenDefinitionColons,
991+
TT_InheritanceColon);
992+
}
993+
987994
void WhitespaceManager::alignConsecutiveDeclarations() {
988995
if (!Style.AlignConsecutiveDeclarations.Enabled)
989996
return;

clang/lib/Format/WhitespaceManager.h

+3
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ class WhitespaceManager {
243243
/// Align consecutive TableGen cond operator colon over all \c Changes.
244244
void alignConsecutiveTableGenCondOperatorColons();
245245

246+
/// Align consecutive TableGen definitions over all \c Changes.
247+
void alignConsecutiveTableGenDefinitions();
248+
246249
/// Align trailing comments over all \c Changes.
247250
void alignTrailingComments();
248251

clang/unittests/Format/FormatTestTableGen.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -346,5 +346,19 @@ TEST_F(FormatTestTableGen, CondOperatorAlignment) {
346346
Style);
347347
}
348348

349+
TEST_F(FormatTestTableGen, DefAlignment) {
350+
FormatStyle Style = getGoogleStyle(FormatStyle::LK_TableGen);
351+
Style.ColumnLimit = 60;
352+
verifyFormat("def Def : Parent {}\n"
353+
"def DefDef : Parent {}\n"
354+
"def DefDefDef : Parent {}\n",
355+
Style);
356+
Style.AlignConsecutiveTableGenDefinitionColons.Enabled = true;
357+
verifyFormat("def Def : Parent {}\n"
358+
"def DefDef : Parent {}\n"
359+
"def DefDefDef : Parent {}\n",
360+
Style);
361+
}
362+
349363
} // namespace format
350364
} // end namespace clang

0 commit comments

Comments
 (0)