diff options
author | Backl1ght <backlight.zzk@gmail.com> | 2023-10-25 05:10:35 -0700 |
---|---|---|
committer | Owen Pan <owenpiano@gmail.com> | 2023-10-25 05:13:50 -0700 |
commit | 69209e30a7168b0493d8fb34989ddccd8c574670 (patch) | |
tree | ec02129fa944c725cc2e39012ef781e66851c410 /clang | |
parent | 66f4a1399d7de3d38312a5b251d4f8acd75237ca (diff) | |
download | llvm-69209e30a7168b0493d8fb34989ddccd8c574670.zip llvm-69209e30a7168b0493d8fb34989ddccd8c574670.tar.gz llvm-69209e30a7168b0493d8fb34989ddccd8c574670.tar.bz2 |
[clang-format] AllowShortCompoundRequirementOnASingleLine
clang-format brace wrapping did not take requires into consideration,
compound requirements will be affected by BraceWrapping.AfterFunction.
Closes #59412.
Differential Revision: https://reviews.llvm.org/D139834
Diffstat (limited to 'clang')
-rw-r--r-- | clang/docs/ClangFormatStyleOptions.rst | 21 | ||||
-rw-r--r-- | clang/include/clang/Format/Format.h | 21 | ||||
-rw-r--r-- | clang/lib/Format/Format.cpp | 3 | ||||
-rw-r--r-- | clang/lib/Format/UnwrappedLineFormatter.cpp | 2 | ||||
-rw-r--r-- | clang/unittests/Format/ConfigParseTest.cpp | 1 | ||||
-rw-r--r-- | clang/unittests/Format/FormatTest.cpp | 34 |
6 files changed, 82 insertions, 0 deletions
diff --git a/clang/docs/ClangFormatStyleOptions.rst b/clang/docs/ClangFormatStyleOptions.rst index 5f369ea..a552c97 100644 --- a/clang/docs/ClangFormatStyleOptions.rst +++ b/clang/docs/ClangFormatStyleOptions.rst @@ -1215,6 +1215,27 @@ the configuration (without a prefix: ``Auto``). return; } +.. _AllowShortCompoundRequirementOnASingleLine: + +**AllowShortCompoundRequirementOnASingleLine** (``Boolean``) :versionbadge:`clang-format 16` :ref:`¶ <AllowShortCompoundRequirementOnASingleLine>` + 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: **AllowShortEnumsOnASingleLine** (``Boolean``) :versionbadge:`clang-format 11` :ref:`¶ <AllowShortEnumsOnASingleLine>` diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index d0145fa..f2ee25b 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -685,6 +685,25 @@ struct FormatStyle { /// \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: @@ -4650,6 +4669,8 @@ struct FormatStyle { AllowShortBlocksOnASingleLine == R.AllowShortBlocksOnASingleLine && AllowShortCaseLabelsOnASingleLine == R.AllowShortCaseLabelsOnASingleLine && + AllowShortCompoundRequirementOnASingleLine == + R.AllowShortCompoundRequirementOnASingleLine && AllowShortEnumsOnASingleLine == R.AllowShortEnumsOnASingleLine && AllowShortFunctionsOnASingleLine == R.AllowShortFunctionsOnASingleLine && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index ff7cb09..edb33f4 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -938,6 +938,8 @@ template <> struct MappingTraits<FormatStyle> { Style.AllowShortBlocksOnASingleLine); IO.mapOptional("AllowShortCaseLabelsOnASingleLine", Style.AllowShortCaseLabelsOnASingleLine); + IO.mapOptional("AllowShortCompoundRequirementOnASingleLine", + Style.AllowShortCompoundRequirementOnASingleLine); IO.mapOptional("AllowShortEnumsOnASingleLine", Style.AllowShortEnumsOnASingleLine); IO.mapOptional("AllowShortFunctionsOnASingleLine", @@ -1441,6 +1443,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { 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; diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index 0dcf3b7..280485d 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -520,6 +520,8 @@ private: // Try to merge records. if (TheLine->Last->is(TT_EnumLBrace)) { ShouldMerge = Style.AllowShortEnumsOnASingleLine; + } else if (TheLine->Last->is(TT_RequiresExpressionLBrace)) { + 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 diff --git a/clang/unittests/Format/ConfigParseTest.cpp b/clang/unittests/Format/ConfigParseTest.cpp index ba79c8d..f90ed17 100644 --- a/clang/unittests/Format/ConfigParseTest.cpp +++ b/clang/unittests/Format/ConfigParseTest.cpp @@ -154,6 +154,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) { 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); diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 8329daa..b2d84f2 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -2767,6 +2767,40 @@ TEST_F(FormatTest, ShortEnums) { 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; |