llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Brad House (bradh352) <details> <summary>Changes</summary> 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 Signed-off-by: Brad House <brad@<!-- -->brad-house.com> --- Full diff: https://github.com/llvm/llvm-project/pull/108241.diff 6 Files Affected: - (modified) clang/docs/ClangFormatStyleOptions.rst (+105) - (modified) clang/include/clang/Format/Format.h (+15) - (modified) clang/lib/Format/Format.cpp (+24-7) - (modified) clang/lib/Format/WhitespaceManager.cpp (+1-1) - (modified) clang/unittests/Format/ConfigParseTest.cpp (+18-6) - (modified) clang/unittests/Format/FormatTest.cpp (+21-2) ``````````diff diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index a427d7cd40fcdd..c862d57a19b954 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. @@ -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. diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index d8b62c7652a0f6..23aa98128d02ae 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -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. @@ -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 { diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index d2463b892fbb96..d4c3e2af58e68f 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -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) { @@ -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); } }; @@ -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 = {}; 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..c1b841bc790bdd 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -305,38 +305,50 @@ 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); \ diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 5ebf0d7068dd6c..aea7df5ad147fc 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -20010,6 +20010,18 @@ 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) { @@ -20253,9 +20265,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" `````````` </details> https://github.com/llvm/llvm-project/pull/108241 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits