Skip to content

Commit e2091a7

Browse files
committed
Add AlignFunctionDeclarations attribute to AlignConsecutiveDeclarations
Enabling AlignConsecutiveDeclarations also aligns function prototypes or declarations. This is often unexpected as typically function prototypes, especially in public headers, don't use any padding. Setting AlignFunctionDeclarations to false will skip this alignment. It is by default set to true to keep compatibility with prior versions to not make unexpected changes. Signed-off-by: Brad House <[email protected]>
1 parent ba4bcce commit e2091a7

File tree

6 files changed

+184
-16
lines changed

6 files changed

+184
-16
lines changed

clang/docs/ClangFormatStyleOptions.rst

+105
Original file line numberDiff line numberDiff line change
@@ -409,6 +409,21 @@ the configuration (without a prefix: ``Auto``).
409409
int *p;
410410
int (*f)();
411411
412+
* ``bool AlignFunctionDeclarations`` Only for ``AlignConsecutiveDeclarations``. Whether function declarations
413+
are aligned.
414+
415+
.. code-block:: c++
416+
417+
true:
418+
unsigned int f1(void);
419+
void f2(void);
420+
size_t f3(void);
421+
422+
false:
423+
unsigned int f1(void);
424+
void f2(void);
425+
size_t f3(void);
426+
412427
* ``bool PadOperators`` Only for ``AlignConsecutiveAssignments``. Whether short assignment
413428
operators are left-padded to the same length as long ones in order to
414429
put all assignment operators to the right of the left hand side.
@@ -551,6 +566,21 @@ the configuration (without a prefix: ``Auto``).
551566
int *p;
552567
int (*f)();
553568
569+
* ``bool AlignFunctionDeclarations`` Only for ``AlignConsecutiveDeclarations``. Whether function declarations
570+
are aligned.
571+
572+
.. code-block:: c++
573+
574+
true:
575+
unsigned int f1(void);
576+
void f2(void);
577+
size_t f3(void);
578+
579+
false:
580+
unsigned int f1(void);
581+
void f2(void);
582+
size_t f3(void);
583+
554584
* ``bool PadOperators`` Only for ``AlignConsecutiveAssignments``. Whether short assignment
555585
operators are left-padded to the same length as long ones in order to
556586
put all assignment operators to the right of the left hand side.
@@ -693,6 +723,21 @@ the configuration (without a prefix: ``Auto``).
693723
int *p;
694724
int (*f)();
695725
726+
* ``bool AlignFunctionDeclarations`` Only for ``AlignConsecutiveDeclarations``. Whether function declarations
727+
are aligned.
728+
729+
.. code-block:: c++
730+
731+
true:
732+
unsigned int f1(void);
733+
void f2(void);
734+
size_t f3(void);
735+
736+
false:
737+
unsigned int f1(void);
738+
void f2(void);
739+
size_t f3(void);
740+
696741
* ``bool PadOperators`` Only for ``AlignConsecutiveAssignments``. Whether short assignment
697742
operators are left-padded to the same length as long ones in order to
698743
put all assignment operators to the right of the left hand side.
@@ -836,6 +881,21 @@ the configuration (without a prefix: ``Auto``).
836881
int *p;
837882
int (*f)();
838883
884+
* ``bool AlignFunctionDeclarations`` Only for ``AlignConsecutiveDeclarations``. Whether function declarations
885+
are aligned.
886+
887+
.. code-block:: c++
888+
889+
true:
890+
unsigned int f1(void);
891+
void f2(void);
892+
size_t f3(void);
893+
894+
false:
895+
unsigned int f1(void);
896+
void f2(void);
897+
size_t f3(void);
898+
839899
* ``bool PadOperators`` Only for ``AlignConsecutiveAssignments``. Whether short assignment
840900
operators are left-padded to the same length as long ones in order to
841901
put all assignment operators to the right of the left hand side.
@@ -1098,6 +1158,21 @@ the configuration (without a prefix: ``Auto``).
10981158
int *p;
10991159
int (*f)();
11001160
1161+
* ``bool AlignFunctionDeclarations`` Only for ``AlignConsecutiveDeclarations``. Whether function declarations
1162+
are aligned.
1163+
1164+
.. code-block:: c++
1165+
1166+
true:
1167+
unsigned int f1(void);
1168+
void f2(void);
1169+
size_t f3(void);
1170+
1171+
false:
1172+
unsigned int f1(void);
1173+
void f2(void);
1174+
size_t f3(void);
1175+
11011176
* ``bool PadOperators`` Only for ``AlignConsecutiveAssignments``. Whether short assignment
11021177
operators are left-padded to the same length as long ones in order to
11031178
put all assignment operators to the right of the left hand side.
@@ -1238,6 +1313,21 @@ the configuration (without a prefix: ``Auto``).
12381313
int *p;
12391314
int (*f)();
12401315
1316+
* ``bool AlignFunctionDeclarations`` Only for ``AlignConsecutiveDeclarations``. Whether function declarations
1317+
are aligned.
1318+
1319+
.. code-block:: c++
1320+
1321+
true:
1322+
unsigned int f1(void);
1323+
void f2(void);
1324+
size_t f3(void);
1325+
1326+
false:
1327+
unsigned int f1(void);
1328+
void f2(void);
1329+
size_t f3(void);
1330+
12411331
* ``bool PadOperators`` Only for ``AlignConsecutiveAssignments``. Whether short assignment
12421332
operators are left-padded to the same length as long ones in order to
12431333
put all assignment operators to the right of the left hand side.
@@ -1378,6 +1468,21 @@ the configuration (without a prefix: ``Auto``).
13781468
int *p;
13791469
int (*f)();
13801470
1471+
* ``bool AlignFunctionDeclarations`` Only for ``AlignConsecutiveDeclarations``. Whether function declarations
1472+
are aligned.
1473+
1474+
.. code-block:: c++
1475+
1476+
true:
1477+
unsigned int f1(void);
1478+
void f2(void);
1479+
size_t f3(void);
1480+
1481+
false:
1482+
unsigned int f1(void);
1483+
void f2(void);
1484+
size_t f3(void);
1485+
13811486
* ``bool PadOperators`` Only for ``AlignConsecutiveAssignments``. Whether short assignment
13821487
operators are left-padded to the same length as long ones in order to
13831488
put all assignment operators to the right of the left hand side.

clang/include/clang/Format/Format.h

+15
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,20 @@ struct FormatStyle {
241241
/// int (*f)();
242242
/// \endcode
243243
bool AlignFunctionPointers;
244+
/// Only for ``AlignConsecutiveDeclarations``. Whether function declarations
245+
/// are aligned.
246+
/// \code
247+
/// true:
248+
/// unsigned int f1(void);
249+
/// void f2(void);
250+
/// size_t f3(void);
251+
///
252+
/// false:
253+
/// unsigned int f1(void);
254+
/// void f2(void);
255+
/// size_t f3(void);
256+
/// \endcode
257+
bool AlignFunctionDeclarations;
244258
/// Only for ``AlignConsecutiveAssignments``. Whether short assignment
245259
/// operators are left-padded to the same length as long ones in order to
246260
/// put all assignment operators to the right of the left hand side.
@@ -265,6 +279,7 @@ struct FormatStyle {
265279
AcrossComments == R.AcrossComments &&
266280
AlignCompound == R.AlignCompound &&
267281
AlignFunctionPointers == R.AlignFunctionPointers &&
282+
AlignFunctionDeclarations == R.AlignFunctionDeclarations &&
268283
PadOperators == R.PadOperators;
269284
}
270285
bool operator!=(const AlignConsecutiveStyle &R) const {

clang/lib/Format/Format.cpp

+24-7
Original file line numberDiff line numberDiff line change
@@ -48,39 +48,53 @@ template <> struct MappingTraits<FormatStyle::AlignConsecutiveStyle> {
4848
FormatStyle::AlignConsecutiveStyle(
4949
{/*Enabled=*/false, /*AcrossEmptyLines=*/false,
5050
/*AcrossComments=*/false, /*AlignCompound=*/false,
51-
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
51+
/*AlignFunctionPointers=*/false,
52+
/*AlignFunctionDeclarations=*/true,
53+
/*PadOperators=*/true}));
5254
IO.enumCase(Value, "Consecutive",
5355
FormatStyle::AlignConsecutiveStyle(
5456
{/*Enabled=*/true, /*AcrossEmptyLines=*/false,
5557
/*AcrossComments=*/false, /*AlignCompound=*/false,
56-
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
58+
/*AlignFunctionPointers=*/false,
59+
/*AlignFunctionDeclarations=*/true,
60+
/*PadOperators=*/true}));
5761
IO.enumCase(Value, "AcrossEmptyLines",
5862
FormatStyle::AlignConsecutiveStyle(
5963
{/*Enabled=*/true, /*AcrossEmptyLines=*/true,
6064
/*AcrossComments=*/false, /*AlignCompound=*/false,
61-
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
65+
/*AlignFunctionPointers=*/false,
66+
/*AlignFunctionDeclarations=*/true,
67+
/*PadOperators=*/true}));
6268
IO.enumCase(Value, "AcrossComments",
6369
FormatStyle::AlignConsecutiveStyle(
6470
{/*Enabled=*/true, /*AcrossEmptyLines=*/false,
6571
/*AcrossComments=*/true, /*AlignCompound=*/false,
66-
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
72+
/*AlignFunctionPointers=*/false,
73+
/*AlignFunctionDeclarations=*/true,
74+
/*PadOperators=*/true}));
6775
IO.enumCase(Value, "AcrossEmptyLinesAndComments",
6876
FormatStyle::AlignConsecutiveStyle(
6977
{/*Enabled=*/true, /*AcrossEmptyLines=*/true,
7078
/*AcrossComments=*/true, /*AlignCompound=*/false,
71-
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
79+
/*AlignFunctionPointers=*/false,
80+
/*AlignFunctionDeclarations=*/true,
81+
/*PadOperators=*/true}));
7282

7383
// For backward compatibility.
7484
IO.enumCase(Value, "true",
7585
FormatStyle::AlignConsecutiveStyle(
7686
{/*Enabled=*/true, /*AcrossEmptyLines=*/false,
7787
/*AcrossComments=*/false, /*AlignCompound=*/false,
78-
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
88+
/*AlignFunctionPointers=*/false,
89+
/*AlignFunctionDeclarations=*/true,
90+
/*PadOperators=*/true}));
7991
IO.enumCase(Value, "false",
8092
FormatStyle::AlignConsecutiveStyle(
8193
{/*Enabled=*/false, /*AcrossEmptyLines=*/false,
8294
/*AcrossComments=*/false, /*AlignCompound=*/false,
83-
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
95+
/*AlignFunctionPointers=*/false,
96+
/*AlignFunctionDeclarations=*/true,
97+
/*PadOperators=*/true}));
8498
}
8599

86100
static void mapping(IO &IO, FormatStyle::AlignConsecutiveStyle &Value) {
@@ -89,6 +103,8 @@ template <> struct MappingTraits<FormatStyle::AlignConsecutiveStyle> {
89103
IO.mapOptional("AcrossComments", Value.AcrossComments);
90104
IO.mapOptional("AlignCompound", Value.AlignCompound);
91105
IO.mapOptional("AlignFunctionPointers", Value.AlignFunctionPointers);
106+
IO.mapOptional("AlignFunctionDeclarations",
107+
Value.AlignFunctionDeclarations);
92108
IO.mapOptional("PadOperators", Value.PadOperators);
93109
}
94110
};
@@ -1448,6 +1464,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
14481464
LLVMStyle.AlignConsecutiveAssignments.PadOperators = true;
14491465
LLVMStyle.AlignConsecutiveBitFields = {};
14501466
LLVMStyle.AlignConsecutiveDeclarations = {};
1467+
LLVMStyle.AlignConsecutiveDeclarations.AlignFunctionDeclarations = true;
14511468
LLVMStyle.AlignConsecutiveMacros = {};
14521469
LLVMStyle.AlignConsecutiveShortCaseStatements = {};
14531470
LLVMStyle.AlignConsecutiveTableGenBreakingDAGArgColons = {};

clang/lib/Format/WhitespaceManager.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1020,7 +1020,7 @@ void WhitespaceManager::alignConsecutiveDeclarations() {
10201020
return true;
10211021
}
10221022
if (C.Tok->is(TT_FunctionDeclarationName))
1023-
return true;
1023+
return Style.AlignConsecutiveDeclarations.AlignFunctionDeclarations;
10241024
if (C.Tok->isNot(TT_StartOfName))
10251025
return false;
10261026
if (C.Tok->Previous &&

clang/unittests/Format/ConfigParseTest.cpp

+18-6
Original file line numberDiff line numberDiff line change
@@ -305,38 +305,50 @@ TEST(ConfigParseTest, ParsesConfiguration) {
305305
FormatStyle::AlignConsecutiveStyle( \
306306
{/*Enabled=*/false, /*AcrossEmptyLines=*/false, \
307307
/*AcrossComments=*/false, /*AlignCompound=*/false, \
308-
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
308+
/*AlignFunctionPointers=*/false, \
309+
/*AlignFunctionDeclarations=*/true, \
310+
/*PadOperators=*/true})); \
309311
CHECK_PARSE( \
310312
#FIELD ": Consecutive", FIELD, \
311313
FormatStyle::AlignConsecutiveStyle( \
312314
{/*Enabled=*/true, /*AcrossEmptyLines=*/false, \
313315
/*AcrossComments=*/false, /*AlignCompound=*/false, \
314-
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
316+
/*AlignFunctionPointers=*/false, \
317+
/*AlignFunctionDeclarations=*/true, \
318+
/*PadOperators=*/true})); \
315319
CHECK_PARSE( \
316320
#FIELD ": AcrossEmptyLines", FIELD, \
317321
FormatStyle::AlignConsecutiveStyle( \
318322
{/*Enabled=*/true, /*AcrossEmptyLines=*/true, \
319323
/*AcrossComments=*/false, /*AlignCompound=*/false, \
320-
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
324+
/*AlignFunctionPointers=*/false, \
325+
/*AlignFunctionDeclarations=*/true, \
326+
/*PadOperators=*/true})); \
321327
CHECK_PARSE( \
322328
#FIELD ": AcrossEmptyLinesAndComments", FIELD, \
323329
FormatStyle::AlignConsecutiveStyle( \
324330
{/*Enabled=*/true, /*AcrossEmptyLines=*/true, \
325331
/*AcrossComments=*/true, /*AlignCompound=*/false, \
326-
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
332+
/*AlignFunctionPointers=*/false, \
333+
/*AlignFunctionDeclarations=*/true, \
334+
/*PadOperators=*/true})); \
327335
/* For backwards compability, false / true should still parse */ \
328336
CHECK_PARSE( \
329337
#FIELD ": false", FIELD, \
330338
FormatStyle::AlignConsecutiveStyle( \
331339
{/*Enabled=*/false, /*AcrossEmptyLines=*/false, \
332340
/*AcrossComments=*/false, /*AlignCompound=*/false, \
333-
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
341+
/*AlignFunctionPointers=*/false, \
342+
/*AlignFunctionDeclarations=*/true, \
343+
/*PadOperators=*/true})); \
334344
CHECK_PARSE( \
335345
#FIELD ": true", FIELD, \
336346
FormatStyle::AlignConsecutiveStyle( \
337347
{/*Enabled=*/true, /*AcrossEmptyLines=*/false, \
338348
/*AcrossComments=*/false, /*AlignCompound=*/false, \
339-
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
349+
/*AlignFunctionPointers=*/false, \
350+
/*AlignFunctionDeclarations=*/true, \
351+
/*PadOperators=*/true})); \
340352
\
341353
CHECK_PARSE_NESTED_BOOL(FIELD, Enabled); \
342354
CHECK_PARSE_NESTED_BOOL(FIELD, AcrossEmptyLines); \

clang/unittests/Format/FormatTest.cpp

+21-2
Original file line numberDiff line numberDiff line change
@@ -20010,6 +20010,18 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
2001020010
" return 0;\n"
2001120011
"}() };",
2001220012
BracedAlign);
20013+
20014+
20015+
Alignment.AlignConsecutiveDeclarations.AlignFunctionDeclarations = false;
20016+
verifyFormat("unsigned int f1(void);\n"
20017+
"void f2(void);\n"
20018+
"size_t f3(void);\n",
20019+
Alignment);
20020+
Alignment.AlignConsecutiveDeclarations.AlignFunctionDeclarations = true;
20021+
verifyFormat("unsigned int f1(void);\n"
20022+
"void f2(void);\n"
20023+
"size_t f3(void);\n",
20024+
Alignment);
2001320025
}
2001420026

2001520027
TEST_F(FormatTest, AlignConsecutiveShortCaseStatements) {
@@ -20253,9 +20265,16 @@ TEST_F(FormatTest, AlignWithLineBreaks) {
2025320265
FormatStyle::AlignConsecutiveStyle(
2025420266
{/*Enabled=*/false, /*AcrossEmptyLines=*/false,
2025520267
/*AcrossComments=*/false, /*AlignCompound=*/false,
20256-
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
20268+
/*AlignFunctionPointers=*/false,
20269+
/*AlignFunctionDeclarations=*/false,
20270+
/*PadOperators=*/true}));
2025720271
EXPECT_EQ(Style.AlignConsecutiveDeclarations,
20258-
FormatStyle::AlignConsecutiveStyle({}));
20272+
FormatStyle::AlignConsecutiveStyle(
20273+
{/*Enabled=*/false, /*AcrossEmptyLines=*/false,
20274+
/*AcrossComments=*/false, /*AlignCompound=*/false,
20275+
/*AlignFunctionPointers=*/false,
20276+
/*AlignFunctionDeclarations=*/true,
20277+
/*PadOperators=*/false}));
2025920278
verifyFormat("void foo() {\n"
2026020279
" int myVar = 5;\n"
2026120280
" double x = 3.14;\n"

0 commit comments

Comments
 (0)