Backl1ght created this revision. Backl1ght added reviewers: HazardyKnusperkeks, rymiel. Backl1ght added a project: clang-format. Herald added a project: All. Backl1ght requested review of this revision. Herald added a project: clang. Herald added a subscriber: cfe-commits.
clang-format brace wrapping did not take requires into consideration, compound requirements will be affected by BraceWrapping.AfterFunction. I plan to add 3 options AllowShortRequiresExpressionOnASingleLine, AllowShortCompoundRequirementOnASingleLine and BraceWrapping.AfterRequiresExpression. This patch is for AllowShortCompoundRequirementOnASingleLine. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D139834 Files: clang/docs/ClangFormatStyleOptions.rst clang/include/clang/Format/Format.h clang/lib/Format/Format.cpp clang/lib/Format/UnwrappedLineFormatter.cpp clang/unittests/Format/ConfigParseTest.cpp clang/unittests/Format/FormatTest.cpp
Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -2899,6 +2899,40 @@ Style); } +TEST_F(FormatTest, ShortCompoundRequirement) { + FormatStyle Style = getLLVMStyle(); + EXPECT_TRUE(Style.AllowShortCompoundRequirementOnASingleLine); + verifyFormat("template <typename T>\n" + "concept c = requires(T x) {\n" + " { x + 1 } -> std::same_as<int>;\n" + "};", + Style); + verifyFormat("template <typename T>\n" + "concept c = requires(T x) {\n" + " { x + 1 } -> std::same_as<int>;\n" + " { x + 2 } -> std::same_as<int>;\n" + "};", + Style); + Style.AllowShortCompoundRequirementOnASingleLine = false; + verifyFormat("template <typename T>\n" + "concept c = requires(T x) {\n" + " {\n" + " x + 1\n" + " } -> std::same_as<int>;\n" + "};", + Style); + verifyFormat("template <typename T>\n" + "concept c = requires(T x) {\n" + " {\n" + " x + 1\n" + " } -> std::same_as<int>;\n" + " {\n" + " x + 2\n" + " } -> std::same_as<int>;\n" + "};", + Style); +} + TEST_F(FormatTest, ShortCaseLabels) { FormatStyle Style = getLLVMStyle(); Style.AllowShortCaseLabelsOnASingleLine = true; Index: clang/unittests/Format/ConfigParseTest.cpp =================================================================== --- clang/unittests/Format/ConfigParseTest.cpp +++ clang/unittests/Format/ConfigParseTest.cpp @@ -147,6 +147,7 @@ CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine); CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine); CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine); + CHECK_PARSE_BOOL(AllowShortCompoundRequirementOnASingleLine); CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine); CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine); CHECK_PARSE_BOOL(BinPackArguments); Index: clang/lib/Format/UnwrappedLineFormatter.cpp =================================================================== --- clang/lib/Format/UnwrappedLineFormatter.cpp +++ clang/lib/Format/UnwrappedLineFormatter.cpp @@ -502,6 +502,8 @@ // Try to merge records. if (TheLine->Last->is(TT_EnumLBrace)) { ShouldMerge = Style.AllowShortEnumsOnASingleLine; + } else if (TheLine->Last->is(TT_CompoundRequirementLBrace)) { + ShouldMerge = Style.AllowShortCompoundRequirementOnASingleLine; } else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace)) { // NOTE: We use AfterClass (whereas AfterStruct exists) for both classes // and structs, but it seems that wrapping is still handled correctly Index: clang/lib/Format/Format.cpp =================================================================== --- clang/lib/Format/Format.cpp +++ clang/lib/Format/Format.cpp @@ -806,6 +806,8 @@ Style.AllowShortBlocksOnASingleLine); IO.mapOptional("AllowShortCaseLabelsOnASingleLine", Style.AllowShortCaseLabelsOnASingleLine); + IO.mapOptional("AllowShortCompoundRequirementOnASingleLine", + Style.AllowShortCompoundRequirementOnASingleLine); IO.mapOptional("AllowShortEnumsOnASingleLine", Style.AllowShortEnumsOnASingleLine); IO.mapOptional("AllowShortFunctionsOnASingleLine", @@ -1260,6 +1262,7 @@ LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true; LLVMStyle.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never; LLVMStyle.AllowShortCaseLabelsOnASingleLine = false; + LLVMStyle.AllowShortCompoundRequirementOnASingleLine = true; LLVMStyle.AllowShortEnumsOnASingleLine = true; LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All; LLVMStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never; Index: clang/include/clang/Format/Format.h =================================================================== --- clang/include/clang/Format/Format.h +++ clang/include/clang/Format/Format.h @@ -537,6 +537,25 @@ /// \version 3.6 bool AllowShortCaseLabelsOnASingleLine; + /// Allow short compound requirement on a single line. + /// \code + /// true: + /// template <typename T> + /// concept c = requires(T x) { + /// { x + 1 } -> std::same_as<int>; + /// }; + /// + /// false: + /// template <typename T> + /// concept c = requires(T x) { + /// { + /// x + 1 + /// } -> std::same_as<int>; + /// }; + /// \endcode + /// \version 16 + bool AllowShortCompoundRequirementOnASingleLine; + /// Allow short enums on a single line. /// \code /// true: @@ -4031,6 +4050,8 @@ AllowShortBlocksOnASingleLine == R.AllowShortBlocksOnASingleLine && AllowShortCaseLabelsOnASingleLine == R.AllowShortCaseLabelsOnASingleLine && + AllowShortCompoundRequirementOnASingleLine == + R.AllowShortCompoundRequirementOnASingleLine && AllowShortEnumsOnASingleLine == R.AllowShortEnumsOnASingleLine && AllowShortFunctionsOnASingleLine == R.AllowShortFunctionsOnASingleLine && Index: clang/docs/ClangFormatStyleOptions.rst =================================================================== --- clang/docs/ClangFormatStyleOptions.rst +++ clang/docs/ClangFormatStyleOptions.rst @@ -1019,6 +1019,25 @@ return; } +**AllowShortCompoundRequirementOnASingleLine** (``Boolean``) :versionbadge:`clang-format 16` + Allow short compound requirement on a single line. + + .. code-block:: c++ + + true: + template <typename T> + concept c = requires(T x) { + { x + 1 } -> std::same_as<int>; + }; + + false: + template <typename T> + concept c = requires(T x) { + { + x + 1 + } -> std::same_as<int>; + }; + **AllowShortEnumsOnASingleLine** (``Boolean``) :versionbadge:`clang-format 11` Allow short enums on a single line.
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits