Skip to content

Commit

Permalink
Add AlignFunctionDeclarations attribute to AlignConsecutiveDeclarations
Browse files Browse the repository at this point in the history
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 <brad@brad-house.com>
  • Loading branch information
bradh352 committed Sep 12, 2024
1 parent ba4bcce commit 89d2388
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 16 deletions.
105 changes: 105 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -409,6 +409,21 @@ the configuration (without a prefix: ``Auto``).
int *p;
int (*f)();
* ``bool AlignFunctionDeclarations`` Only for ``AlignConsecutiveDeclarations``. Whether function declarations
are aligned.

.. code-block:: c++

true:
unsigned int f1(void);
void f2(void);
size_t f3(void);

false:
unsigned int f1(void);
void f2(void);
size_t f3(void);

* ``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.
Expand Down Expand Up @@ -551,6 +566,21 @@ the configuration (without a prefix: ``Auto``).
int *p;
int (*f)();
* ``bool AlignFunctionDeclarations`` Only for ``AlignConsecutiveDeclarations``. Whether function declarations
are aligned.

.. code-block:: c++

true:
unsigned int f1(void);
void f2(void);
size_t f3(void);

false:
unsigned int f1(void);
void f2(void);
size_t f3(void);

* ``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.
Expand Down Expand Up @@ -693,6 +723,21 @@ the configuration (without a prefix: ``Auto``).
int *p;
int (*f)();
* ``bool AlignFunctionDeclarations`` Only for ``AlignConsecutiveDeclarations``. Whether function declarations
are aligned.

.. code-block:: c++

true:
unsigned int f1(void);
void f2(void);
size_t f3(void);

false:
unsigned int f1(void);
void f2(void);
size_t f3(void);

* ``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.
Expand Down Expand Up @@ -836,6 +881,21 @@ the configuration (without a prefix: ``Auto``).
int *p;
int (*f)();
* ``bool AlignFunctionDeclarations`` Only for ``AlignConsecutiveDeclarations``. Whether function declarations
are aligned.

.. code-block:: c++

true:
unsigned int f1(void);
void f2(void);
size_t f3(void);

false:
unsigned int f1(void);
void f2(void);
size_t f3(void);

* ``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.
Expand Down Expand Up @@ -1098,6 +1158,21 @@ the configuration (without a prefix: ``Auto``).
int *p;
int (*f)();
* ``bool AlignFunctionDeclarations`` Only for ``AlignConsecutiveDeclarations``. Whether function declarations
are aligned.

.. code-block:: c++

true:
unsigned int f1(void);
void f2(void);
size_t f3(void);

false:
unsigned int f1(void);
void f2(void);
size_t f3(void);

* ``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.
Expand Down Expand Up @@ -1238,6 +1313,21 @@ the configuration (without a prefix: ``Auto``).
int *p;
int (*f)();
* ``bool AlignFunctionDeclarations`` Only for ``AlignConsecutiveDeclarations``. Whether function declarations
are aligned.

.. code-block:: c++

true:
unsigned int f1(void);
void f2(void);
size_t f3(void);

false:
unsigned int f1(void);
void f2(void);
size_t f3(void);

* ``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.
Expand Down Expand Up @@ -1378,6 +1468,21 @@ the configuration (without a prefix: ``Auto``).
int *p;
int (*f)();
* ``bool AlignFunctionDeclarations`` Only for ``AlignConsecutiveDeclarations``. Whether function declarations
are aligned.

.. code-block:: c++

true:
unsigned int f1(void);
void f2(void);
size_t f3(void);

false:
unsigned int f1(void);
void f2(void);
size_t f3(void);

* ``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.
Expand Down
15 changes: 15 additions & 0 deletions clang/include/clang/Format/Format.h
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,20 @@ struct FormatStyle {
/// int (*f)();
/// \endcode
bool AlignFunctionPointers;
/// Only for ``AlignConsecutiveDeclarations``. Whether function declarations
/// are aligned.
/// \code
/// true:
/// unsigned int f1(void);
/// void f2(void);
/// size_t f3(void);
///
/// false:
/// unsigned int f1(void);
/// void f2(void);
/// size_t f3(void);
/// \endcode
bool AlignFunctionDeclarations;
/// 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.
Expand All @@ -265,6 +279,7 @@ struct FormatStyle {
AcrossComments == R.AcrossComments &&
AlignCompound == R.AlignCompound &&
AlignFunctionPointers == R.AlignFunctionPointers &&
AlignFunctionDeclarations == R.AlignFunctionDeclarations &&
PadOperators == R.PadOperators;
}
bool operator!=(const AlignConsecutiveStyle &R) const {
Expand Down
31 changes: 24 additions & 7 deletions clang/lib/Format/Format.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,39 +48,53 @@ template <> struct MappingTraits<FormatStyle::AlignConsecutiveStyle> {
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/false, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
/*AlignFunctionPointers=*/false,
/*AlignFunctionDeclarations=*/true,
/*PadOperators=*/true}));
IO.enumCase(Value, "Consecutive",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
/*AlignFunctionPointers=*/false,
/*AlignFunctionDeclarations=*/true,
/*PadOperators=*/true}));
IO.enumCase(Value, "AcrossEmptyLines",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/true,
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
/*AlignFunctionPointers=*/false,
/*AlignFunctionDeclarations=*/true,
/*PadOperators=*/true}));
IO.enumCase(Value, "AcrossComments",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/true, /*AlignCompound=*/false,
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
/*AlignFunctionPointers=*/false,
/*AlignFunctionDeclarations=*/true,
/*PadOperators=*/true}));
IO.enumCase(Value, "AcrossEmptyLinesAndComments",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/true,
/*AcrossComments=*/true, /*AlignCompound=*/false,
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
/*AlignFunctionPointers=*/false,
/*AlignFunctionDeclarations=*/true,
/*PadOperators=*/true}));

// For backward compatibility.
IO.enumCase(Value, "true",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
/*AlignFunctionPointers=*/false,
/*AlignFunctionDeclarations=*/true,
/*PadOperators=*/true}));
IO.enumCase(Value, "false",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/false, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
/*AlignFunctionPointers=*/false,
/*AlignFunctionDeclarations=*/true,
/*PadOperators=*/true}));
}

static void mapping(IO &IO, FormatStyle::AlignConsecutiveStyle &Value) {
Expand All @@ -89,6 +103,8 @@ template <> struct MappingTraits<FormatStyle::AlignConsecutiveStyle> {
IO.mapOptional("AcrossComments", Value.AcrossComments);
IO.mapOptional("AlignCompound", Value.AlignCompound);
IO.mapOptional("AlignFunctionPointers", Value.AlignFunctionPointers);
IO.mapOptional("AlignFunctionDeclarations",
Value.AlignFunctionDeclarations);
IO.mapOptional("PadOperators", Value.PadOperators);
}
};
Expand Down Expand Up @@ -1448,6 +1464,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.AlignConsecutiveAssignments.PadOperators = true;
LLVMStyle.AlignConsecutiveBitFields = {};
LLVMStyle.AlignConsecutiveDeclarations = {};
LLVMStyle.AlignConsecutiveDeclarations.AlignFunctionDeclarations = true;
LLVMStyle.AlignConsecutiveMacros = {};
LLVMStyle.AlignConsecutiveShortCaseStatements = {};
LLVMStyle.AlignConsecutiveTableGenBreakingDAGArgColons = {};
Expand Down
2 changes: 1 addition & 1 deletion clang/lib/Format/WhitespaceManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1020,7 +1020,7 @@ void WhitespaceManager::alignConsecutiveDeclarations() {
return true;
}
if (C.Tok->is(TT_FunctionDeclarationName))
return true;
return Style.AlignConsecutiveDeclarations.AlignFunctionDeclarations;
if (C.Tok->isNot(TT_StartOfName))
return false;
if (C.Tok->Previous &&
Expand Down
18 changes: 12 additions & 6 deletions clang/unittests/Format/ConfigParseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -305,38 +305,44 @@ TEST(ConfigParseTest, ParsesConfiguration) {
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/false, /*AcrossEmptyLines=*/false, \
/*AcrossComments=*/false, /*AlignCompound=*/false, \
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
/*AlignFunctionPointers=*/false, \
/*AlignFunctionDeclarations=*/true, /*PadOperators=*/true})); \
CHECK_PARSE( \
#FIELD ": Consecutive", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/true, /*AcrossEmptyLines=*/false, \
/*AcrossComments=*/false, /*AlignCompound=*/false, \
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
/*AlignFunctionPointers=*/false, \
/*AlignFunctionDeclarations=*/true, /*PadOperators=*/true})); \
CHECK_PARSE( \
#FIELD ": AcrossEmptyLines", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/true, /*AcrossEmptyLines=*/true, \
/*AcrossComments=*/false, /*AlignCompound=*/false, \
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
/*AlignFunctionPointers=*/false, \
/*AlignFunctionDeclarations=*/true, /*PadOperators=*/true})); \
CHECK_PARSE( \
#FIELD ": AcrossEmptyLinesAndComments", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/true, /*AcrossEmptyLines=*/true, \
/*AcrossComments=*/true, /*AlignCompound=*/false, \
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
/*AlignFunctionPointers=*/false, \
/*AlignFunctionDeclarations=*/true, /*PadOperators=*/true})); \
/* For backwards compability, false / true should still parse */ \
CHECK_PARSE( \
#FIELD ": false", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/false, /*AcrossEmptyLines=*/false, \
/*AcrossComments=*/false, /*AlignCompound=*/false, \
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
/*AlignFunctionPointers=*/false, \
/*AlignFunctionDeclarations=*/true, /*PadOperators=*/true})); \
CHECK_PARSE( \
#FIELD ": true", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/true, /*AcrossEmptyLines=*/false, \
/*AcrossComments=*/false, /*AlignCompound=*/false, \
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
/*AlignFunctionPointers=*/false, \
/*AlignFunctionDeclarations=*/true, /*PadOperators=*/true})); \
\
CHECK_PARSE_NESTED_BOOL(FIELD, Enabled); \
CHECK_PARSE_NESTED_BOOL(FIELD, AcrossEmptyLines); \
Expand Down
22 changes: 20 additions & 2 deletions clang/unittests/Format/FormatTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20010,6 +20010,17 @@ TEST_F(FormatTest, AlignConsecutiveDeclarations) {
" return 0;\n"
"}() };",
BracedAlign);

Alignment.AlignConsecutiveDeclarations.AlignFunctionDeclarations = false;
verifyFormat("unsigned int f1(void);\n"
"void f2(void);\n"
"size_t f3(void);\n",
Alignment);
Alignment.AlignConsecutiveDeclarations.AlignFunctionDeclarations = true;
verifyFormat("unsigned int f1(void);\n"
"void f2(void);\n"
"size_t f3(void);\n",
Alignment);
}

TEST_F(FormatTest, AlignConsecutiveShortCaseStatements) {
Expand Down Expand Up @@ -20253,9 +20264,16 @@ TEST_F(FormatTest, AlignWithLineBreaks) {
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/false, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
/*AlignFunctionPointers=*/false,
/*AlignFunctionDeclarations=*/false,
/*PadOperators=*/true}));
EXPECT_EQ(Style.AlignConsecutiveDeclarations,
FormatStyle::AlignConsecutiveStyle({}));
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/false, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*AlignFunctionPointers=*/false,
/*AlignFunctionDeclarations=*/true,
/*PadOperators=*/false}));
verifyFormat("void foo() {\n"
" int myVar = 5;\n"
" double x = 3.14;\n"
Expand Down

0 comments on commit 89d2388

Please sign in to comment.