Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[clang-format]: Split alignment of declarations around assignment #69340

Merged
merged 10 commits into from Jan 11, 2024
68 changes: 68 additions & 0 deletions clang/docs/ClangFormatStyleOptions.rst
Expand Up @@ -392,6 +392,23 @@ the configuration (without a prefix: ``Auto``).
a &= 2;
bbb = 2;

* ``bool AlignFunctionPointers`` Only for ``AlignConsecutiveDeclarations``. Whether function pointers are
aligned.

.. code-block:: c++

true:
unsigned i;
int &r;
int *p;
int (*f)();

false:
unsigned i;
int &r;
int *p;
int (*f)();

* ``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 @@ -517,6 +534,23 @@ the configuration (without a prefix: ``Auto``).
a &= 2;
bbb = 2;

* ``bool AlignFunctionPointers`` Only for ``AlignConsecutiveDeclarations``. Whether function pointers are
aligned.

.. code-block:: c++

true:
unsigned i;
int &r;
int *p;
int (*f)();

false:
unsigned i;
int &r;
int *p;
int (*f)();

* ``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 @@ -642,6 +676,23 @@ the configuration (without a prefix: ``Auto``).
a &= 2;
bbb = 2;

* ``bool AlignFunctionPointers`` Only for ``AlignConsecutiveDeclarations``. Whether function pointers are
aligned.

.. code-block:: c++

true:
unsigned i;
int &r;
int *p;
int (*f)();

false:
unsigned i;
int &r;
int *p;
int (*f)();

* ``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 @@ -768,6 +819,23 @@ the configuration (without a prefix: ``Auto``).
a &= 2;
bbb = 2;

* ``bool AlignFunctionPointers`` Only for ``AlignConsecutiveDeclarations``. Whether function pointers are
aligned.

.. code-block:: c++

true:
unsigned i;
int &r;
int *p;
int (*f)();

false:
unsigned i;
int &r;
int *p;
int (*f)();

* ``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
1 change: 1 addition & 0 deletions clang/docs/ReleaseNotes.rst
Expand Up @@ -1079,6 +1079,7 @@ clang-format
- Add ``ObjCPropertyAttributeOrder`` which can be used to sort ObjC property
attributes (like ``nonatomic, strong, nullable``).
- Add ``.clang-format-ignore`` files.
- Add ``AlignFunctionPointers`` sub-option for ``AlignConsecutiveDeclarations``.

libclang
--------
Expand Down
20 changes: 19 additions & 1 deletion clang/include/clang/Format/Format.h
Expand Up @@ -225,6 +225,22 @@ struct FormatStyle {
/// bbb = 2;
/// \endcode
bool AlignCompound;
/// Only for ``AlignConsecutiveDeclarations``. Whether function pointers are
/// aligned.
/// \code
/// true:
/// unsigned i;
/// int &r;
/// int *p;
/// int (*f)();
///
/// false:
/// unsigned i;
/// int &r;
/// int *p;
/// int (*f)();
/// \endcode
bool AlignFunctionPointers;
/// 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 @@ -247,7 +263,9 @@ struct FormatStyle {
bool operator==(const AlignConsecutiveStyle &R) const {
return Enabled == R.Enabled && AcrossEmptyLines == R.AcrossEmptyLines &&
AcrossComments == R.AcrossComments &&
AlignCompound == R.AlignCompound && PadOperators == R.PadOperators;
AlignCompound == R.AlignCompound &&
AlignFunctionPointers == R.AlignFunctionPointers &&
PadOperators == R.PadOperators;
}
bool operator!=(const AlignConsecutiveStyle &R) const {
return !(*this == R);
Expand Down
30 changes: 15 additions & 15 deletions clang/lib/Format/Format.cpp
Expand Up @@ -76,48 +76,47 @@ template <> struct MappingTraits<FormatStyle::AlignConsecutiveStyle> {
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/false, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*PadOperators=*/true}));
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
IO.enumCase(Value, "Consecutive",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*PadOperators=*/true}));
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
IO.enumCase(Value, "AcrossEmptyLines",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/true,
/*AcrossComments=*/false, /*AlignCompound=*/false,
/*PadOperators=*/true}));
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
IO.enumCase(Value, "AcrossComments",
FormatStyle::AlignConsecutiveStyle({/*Enabled=*/true,
/*AcrossEmptyLines=*/false,
/*AcrossComments=*/true,
/*AlignCompound=*/false,
/*PadOperators=*/true}));
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/true, /*AlignCompound=*/false,
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
IO.enumCase(Value, "AcrossEmptyLinesAndComments",
FormatStyle::AlignConsecutiveStyle({/*Enabled=*/true,
/*AcrossEmptyLines=*/true,
/*AcrossComments=*/true,
/*AlignCompound=*/false,
/*PadOperators=*/true}));
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/true,
/*AcrossComments=*/true, /*AlignCompound=*/false,
/*AlignFunctionPointers=*/false, /*PadOperators=*/true}));

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

static void mapping(IO &IO, FormatStyle::AlignConsecutiveStyle &Value) {
IO.mapOptional("Enabled", Value.Enabled);
IO.mapOptional("AcrossEmptyLines", Value.AcrossEmptyLines);
IO.mapOptional("AcrossComments", Value.AcrossComments);
IO.mapOptional("AlignCompound", Value.AlignCompound);
IO.mapOptional("AlignFunctionPointers", Value.AlignFunctionPointers);
IO.mapOptional("PadOperators", Value.PadOperators);
}
};
Expand Down Expand Up @@ -1432,6 +1431,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.AlignConsecutiveAssignments.AcrossEmptyLines = false;
LLVMStyle.AlignConsecutiveAssignments.AcrossComments = false;
LLVMStyle.AlignConsecutiveAssignments.AlignCompound = false;
LLVMStyle.AlignConsecutiveAssignments.AlignFunctionPointers = false;
LLVMStyle.AlignConsecutiveAssignments.PadOperators = true;
LLVMStyle.AlignConsecutiveBitFields = {};
LLVMStyle.AlignConsecutiveDeclarations = {};
Expand Down
9 changes: 8 additions & 1 deletion clang/lib/Format/WhitespaceManager.cpp
Expand Up @@ -978,7 +978,14 @@ void WhitespaceManager::alignConsecutiveDeclarations() {

AlignTokens(
Style,
[](Change const &C) {
[&](Change const &C) {
if (Style.AlignConsecutiveDeclarations.AlignFunctionPointers) {
for (const auto *Prev = C.Tok->Previous; Prev; Prev = Prev->Previous)
if (Prev->is(tok::equal))
return false;
if (C.Tok->is(TT_FunctionTypeLParen))
return true;
}
if (C.Tok->is(TT_FunctionDeclarationName))
return true;
if (C.Tok->isNot(TT_StartOfName))
Expand Down
66 changes: 36 additions & 30 deletions clang/unittests/Format/ConfigParseTest.cpp
Expand Up @@ -289,37 +289,43 @@ TEST(ConfigParseTest, ParsesConfiguration) {
#define CHECK_ALIGN_CONSECUTIVE(FIELD) \
do { \
Style.FIELD.Enabled = true; \
CHECK_PARSE(#FIELD ": None", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/false, /*AcrossEmptyLines=*/false, \
/*AcrossComments=*/false, /*AlignCompound=*/false, \
/*PadOperators=*/true})); \
CHECK_PARSE(#FIELD ": Consecutive", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/true, /*AcrossEmptyLines=*/false, \
/*AcrossComments=*/false, /*AlignCompound=*/false, \
/*PadOperators=*/true})); \
CHECK_PARSE(#FIELD ": AcrossEmptyLines", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/true, /*AcrossEmptyLines=*/true, \
/*AcrossComments=*/false, /*AlignCompound=*/false, \
/*PadOperators=*/true})); \
CHECK_PARSE(#FIELD ": AcrossEmptyLinesAndComments", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/true, /*AcrossEmptyLines=*/true, \
/*AcrossComments=*/true, /*AlignCompound=*/false, \
/*PadOperators=*/true})); \
CHECK_PARSE( \
#FIELD ": None", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/false, /*AcrossEmptyLines=*/false, \
/*AcrossComments=*/false, /*AlignCompound=*/false, \
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
CHECK_PARSE( \
#FIELD ": Consecutive", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/true, /*AcrossEmptyLines=*/false, \
/*AcrossComments=*/false, /*AlignCompound=*/false, \
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
CHECK_PARSE( \
#FIELD ": AcrossEmptyLines", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/true, /*AcrossEmptyLines=*/true, \
/*AcrossComments=*/false, /*AlignCompound=*/false, \
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
CHECK_PARSE( \
#FIELD ": AcrossEmptyLinesAndComments", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/true, /*AcrossEmptyLines=*/true, \
/*AcrossComments=*/true, /*AlignCompound=*/false, \
/*AlignFunctionPointers=*/false, /*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, \
/*PadOperators=*/true})); \
CHECK_PARSE(#FIELD ": true", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/true, /*AcrossEmptyLines=*/false, \
/*AcrossComments=*/false, /*AlignCompound=*/false, \
/*PadOperators=*/true})); \
CHECK_PARSE( \
#FIELD ": false", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/false, /*AcrossEmptyLines=*/false, \
/*AcrossComments=*/false, /*AlignCompound=*/false, \
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
CHECK_PARSE( \
#FIELD ": true", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/true, /*AcrossEmptyLines=*/false, \
/*AcrossComments=*/false, /*AlignCompound=*/false, \
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
\
CHECK_PARSE_NESTED_BOOL(FIELD, Enabled); \
CHECK_PARSE_NESTED_BOOL(FIELD, AcrossEmptyLines); \
Expand Down