[clang] f0bd62d - [clang-format] Add AlignFunctionDeclarations to AlignConsecutiveDeclarations (#108241)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Oct 6 17:46:46 PDT 2024
Author: Brad House
Date: 2024-10-06T17:46:43-07:00
New Revision: f0bd62d8709a49dd87eb75411e41e2e11e0ab59d
URL: https://github.com/llvm/llvm-project/commit/f0bd62d8709a49dd87eb75411e41e2e11e0ab59d
DIFF: https://github.com/llvm/llvm-project/commit/f0bd62d8709a49dd87eb75411e41e2e11e0ab59d.diff
LOG: [clang-format] Add AlignFunctionDeclarations to AlignConsecutiveDeclarations (#108241)
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.
Fixes #74320
Added:
Modified:
clang/docs/ClangFormatStyleOptions.rst
clang/docs/ReleaseNotes.rst
clang/include/clang/Format/Format.h
clang/lib/Format/Format.cpp
clang/lib/Format/WhitespaceManager.cpp
clang/unittests/Format/ConfigParseTest.cpp
clang/unittests/Format/FormatTest.cpp
Removed:
################################################################################
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst
index a16edb0989b05c..7a1508ee858c41 100644
--- a/clang/docs/ClangFormatStyleOptions.rst
+++ b/clang/docs/ClangFormatStyleOptions.rst
@@ -392,6 +392,21 @@ the configuration (without a prefix: ``Auto``).
a &= 2;
bbb = 2;
+ * ``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 AlignFunctionPointers`` Only for ``AlignConsecutiveDeclarations``. Whether function pointers are
aligned.
@@ -534,6 +549,21 @@ the configuration (without a prefix: ``Auto``).
a &= 2;
bbb = 2;
+ * ``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 AlignFunctionPointers`` Only for ``AlignConsecutiveDeclarations``. Whether function pointers are
aligned.
@@ -676,6 +706,21 @@ the configuration (without a prefix: ``Auto``).
a &= 2;
bbb = 2;
+ * ``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 AlignFunctionPointers`` Only for ``AlignConsecutiveDeclarations``. Whether function pointers are
aligned.
@@ -819,6 +864,21 @@ the configuration (without a prefix: ``Auto``).
a &= 2;
bbb = 2;
+ * ``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 AlignFunctionPointers`` Only for ``AlignConsecutiveDeclarations``. Whether function pointers are
aligned.
@@ -1081,6 +1141,21 @@ the configuration (without a prefix: ``Auto``).
a &= 2;
bbb = 2;
+ * ``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 AlignFunctionPointers`` Only for ``AlignConsecutiveDeclarations``. Whether function pointers are
aligned.
@@ -1221,6 +1296,21 @@ the configuration (without a prefix: ``Auto``).
a &= 2;
bbb = 2;
+ * ``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 AlignFunctionPointers`` Only for ``AlignConsecutiveDeclarations``. Whether function pointers are
aligned.
@@ -1361,6 +1451,21 @@ the configuration (without a prefix: ``Auto``).
a &= 2;
bbb = 2;
+ * ``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 AlignFunctionPointers`` Only for ``AlignConsecutiveDeclarations``. Whether function pointers are
aligned.
diff --git a/clang/docs/ReleaseNotes.rst b/clang/docs/ReleaseNotes.rst
index 44d5f348ed2d54..5cc9c8047a70ef 100644
--- a/clang/docs/ReleaseNotes.rst
+++ b/clang/docs/ReleaseNotes.rst
@@ -616,6 +616,7 @@ clang-format
- Adds ``BreakBinaryOperations`` option.
- Adds ``TemplateNames`` option.
+- Adds ``AlignFunctionDeclarations`` option to ``AlignConsecutiveDeclarations``.
libclang
--------
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 53a9577e0f72e7..82cd863e615a87 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -225,6 +225,20 @@ struct FormatStyle {
/// bbb = 2;
/// \endcode
bool AlignCompound;
+ /// 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 ``AlignConsecutiveDeclarations``. Whether function pointers are
/// aligned.
/// \code
@@ -264,6 +278,7 @@ struct FormatStyle {
return Enabled == R.Enabled && AcrossEmptyLines == R.AcrossEmptyLines &&
AcrossComments == R.AcrossComments &&
AlignCompound == R.AlignCompound &&
+ AlignFunctionDeclarations == R.AlignFunctionDeclarations &&
AlignFunctionPointers == R.AlignFunctionPointers &&
PadOperators == R.PadOperators;
}
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index 5350c66ea5132b..01b4b6f0b0d2c1 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -44,43 +44,45 @@ struct ScalarEnumerationTraits<FormatStyle::BreakBeforeNoexceptSpecifierStyle> {
template <> struct MappingTraits<FormatStyle::AlignConsecutiveStyle> {
static void enumInput(IO &IO, FormatStyle::AlignConsecutiveStyle &Value) {
- IO.enumCase(Value, "None",
- FormatStyle::AlignConsecutiveStyle(
- {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
- /*AcrossComments=*/false, /*AlignCompound=*/false,
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
+ IO.enumCase(Value, "None", FormatStyle::AlignConsecutiveStyle({}));
IO.enumCase(Value, "Consecutive",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/false, /*AlignCompound=*/false,
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
+ /*AlignFunctionDeclarations=*/true,
+ /*AlignFunctionPointers=*/false,
+ /*PadOperators=*/true}));
IO.enumCase(Value, "AcrossEmptyLines",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/true,
/*AcrossComments=*/false, /*AlignCompound=*/false,
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
+ /*AlignFunctionDeclarations=*/true,
+ /*AlignFunctionPointers=*/false,
+ /*PadOperators=*/true}));
IO.enumCase(Value, "AcrossComments",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/true, /*AlignCompound=*/false,
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
+ /*AlignFunctionDeclarations=*/true,
+ /*AlignFunctionPointers=*/false,
+ /*PadOperators=*/true}));
IO.enumCase(Value, "AcrossEmptyLinesAndComments",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/true,
/*AcrossComments=*/true, /*AlignCompound=*/false,
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
+ /*AlignFunctionDeclarations=*/true,
+ /*AlignFunctionPointers=*/false,
+ /*PadOperators=*/true}));
// For backward compatibility.
IO.enumCase(Value, "true",
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/true, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/false, /*AlignCompound=*/false,
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
- IO.enumCase(Value, "false",
- FormatStyle::AlignConsecutiveStyle(
- {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
- /*AcrossComments=*/false, /*AlignCompound=*/false,
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
+ /*AlignFunctionDeclarations=*/true,
+ /*AlignFunctionPointers=*/false,
+ /*PadOperators=*/true}));
+ IO.enumCase(Value, "false", FormatStyle::AlignConsecutiveStyle({}));
}
static void mapping(IO &IO, FormatStyle::AlignConsecutiveStyle &Value) {
@@ -88,6 +90,8 @@ template <> struct MappingTraits<FormatStyle::AlignConsecutiveStyle> {
IO.mapOptional("AcrossEmptyLines", Value.AcrossEmptyLines);
IO.mapOptional("AcrossComments", Value.AcrossComments);
IO.mapOptional("AlignCompound", Value.AlignCompound);
+ IO.mapOptional("AlignFunctionDeclarations",
+ Value.AlignFunctionDeclarations);
IO.mapOptional("AlignFunctionPointers", Value.AlignFunctionPointers);
IO.mapOptional("PadOperators", Value.PadOperators);
}
@@ -1449,6 +1453,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
LLVMStyle.AlignConsecutiveAssignments.PadOperators = true;
LLVMStyle.AlignConsecutiveBitFields = {};
LLVMStyle.AlignConsecutiveDeclarations = {};
+ LLVMStyle.AlignConsecutiveDeclarations.AlignFunctionDeclarations = true;
LLVMStyle.AlignConsecutiveMacros = {};
LLVMStyle.AlignConsecutiveShortCaseStatements = {};
LLVMStyle.AlignConsecutiveTableGenBreakingDAGArgColons = {};
diff --git a/clang/lib/Format/WhitespaceManager.cpp b/clang/lib/Format/WhitespaceManager.cpp
index fd4a40a86082e2..b6b24672f6a39d 100644
--- a/clang/lib/Format/WhitespaceManager.cpp
+++ b/clang/lib/Format/WhitespaceManager.cpp
@@ -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 &&
diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp
index b8bdfaaa74e10e..aa8fbb874e1516 100644
--- a/clang/unittests/Format/ConfigParseTest.cpp
+++ b/clang/unittests/Format/ConfigParseTest.cpp
@@ -300,48 +300,45 @@ 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, \
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
+ CHECK_PARSE(#FIELD ": None", FIELD, \
+ FormatStyle::AlignConsecutiveStyle({})); \
CHECK_PARSE( \
#FIELD ": Consecutive", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/true, /*AcrossEmptyLines=*/false, \
/*AcrossComments=*/false, /*AlignCompound=*/false, \
+ /*AlignFunctionDeclarations=*/true, \
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
CHECK_PARSE( \
#FIELD ": AcrossEmptyLines", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/true, /*AcrossEmptyLines=*/true, \
/*AcrossComments=*/false, /*AlignCompound=*/false, \
+ /*AlignFunctionDeclarations=*/true, \
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
CHECK_PARSE( \
#FIELD ": AcrossEmptyLinesAndComments", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/true, /*AcrossEmptyLines=*/true, \
/*AcrossComments=*/true, /*AlignCompound=*/false, \
+ /*AlignFunctionDeclarations=*/true, \
/*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, \
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
+ CHECK_PARSE(#FIELD ": false", FIELD, \
+ FormatStyle::AlignConsecutiveStyle({})); \
CHECK_PARSE( \
#FIELD ": true", FIELD, \
FormatStyle::AlignConsecutiveStyle( \
{/*Enabled=*/true, /*AcrossEmptyLines=*/false, \
/*AcrossComments=*/false, /*AlignCompound=*/false, \
+ /*AlignFunctionDeclarations=*/true, \
/*AlignFunctionPointers=*/false, /*PadOperators=*/true})); \
\
CHECK_PARSE_NESTED_BOOL(FIELD, Enabled); \
CHECK_PARSE_NESTED_BOOL(FIELD, AcrossEmptyLines); \
CHECK_PARSE_NESTED_BOOL(FIELD, AcrossComments); \
CHECK_PARSE_NESTED_BOOL(FIELD, AlignCompound); \
+ CHECK_PARSE_NESTED_BOOL(FIELD, AlignFunctionDeclarations); \
CHECK_PARSE_NESTED_BOOL(FIELD, PadOperators); \
} while (false)
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 5d386c1bbdbcd9..61287aafe8273d 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -20021,6 +20021,12 @@ 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);",
+ Alignment);
}
TEST_F(FormatTest, AlignConsecutiveShortCaseStatements) {
@@ -20264,9 +20270,16 @@ TEST_F(FormatTest, AlignWithLineBreaks) {
FormatStyle::AlignConsecutiveStyle(
{/*Enabled=*/false, /*AcrossEmptyLines=*/false,
/*AcrossComments=*/false, /*AlignCompound=*/false,
- /*AlignFunctionPointers=*/false, /*PadOperators=*/true}));
+ /*AlignFunctionDeclarations=*/false,
+ /*AlignFunctionPointers=*/false,
+ /*PadOperators=*/true}));
EXPECT_EQ(Style.AlignConsecutiveDeclarations,
- FormatStyle::AlignConsecutiveStyle({}));
+ FormatStyle::AlignConsecutiveStyle(
+ {/*Enabled=*/false, /*AcrossEmptyLines=*/false,
+ /*AcrossComments=*/false, /*AlignCompound=*/false,
+ /*AlignFunctionDeclarations=*/true,
+ /*AlignFunctionPointers=*/false,
+ /*PadOperators=*/false}));
verifyFormat("void foo() {\n"
" int myVar = 5;\n"
" double x = 3.14;\n"
More information about the cfe-commits
mailing list