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 + /// concept c = requires(T x) { + /// { x + 1 } -> std::same_as; + /// }; + /// + /// false: + /// template + /// concept c = requires(T x) { + /// { + /// x + 1 + /// } -> std::same_as; + /// }; + /// \endcode + /// \version 16 + bool AllowShortCompoundRequirementOnASingleLine; + /// Allow short enums on a single line. /// \code /// true: @@ -714,6 +733,21 @@ /// \version 3.7 bool AllowShortLoopsOnASingleLine; + /// Allow short requires expression on a single line. + /// \code + /// true: + /// template + /// concept c = requires(T a, T b) { a + b; }; + /// + /// false: + /// template + /// concept c = requires(T a, T b) { + /// a + b; + /// }; + /// \endcode + /// \version 16 + bool AllowShortRequiresExpressionOnASingleLine; + /// Different ways to break after the function definition return type. /// This option is **deprecated** and is retained for backwards compatibility. enum DefinitionReturnTypeBreakingStyle : int8_t { @@ -4031,6 +4065,8 @@ AllowShortBlocksOnASingleLine == R.AllowShortBlocksOnASingleLine && AllowShortCaseLabelsOnASingleLine == R.AllowShortCaseLabelsOnASingleLine && + AllowShortCompoundRequirementOnASingleLine == + R.AllowShortCompoundRequirementOnASingleLine && AllowShortEnumsOnASingleLine == R.AllowShortEnumsOnASingleLine && AllowShortFunctionsOnASingleLine == R.AllowShortFunctionsOnASingleLine && @@ -4038,6 +4074,8 @@ R.AllowShortIfStatementsOnASingleLine && AllowShortLambdasOnASingleLine == R.AllowShortLambdasOnASingleLine && AllowShortLoopsOnASingleLine == R.AllowShortLoopsOnASingleLine && + AllowShortRequiresExpressionOnASingleLine == + R.AllowShortRequiresExpressionOnASingleLine && AlwaysBreakAfterReturnType == R.AlwaysBreakAfterReturnType && AlwaysBreakBeforeMultilineStrings == R.AlwaysBreakBeforeMultilineStrings && 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", @@ -816,6 +818,8 @@ Style.AllowShortLambdasOnASingleLine); IO.mapOptional("AllowShortLoopsOnASingleLine", Style.AllowShortLoopsOnASingleLine); + IO.mapOptional("AllowShortRequiresExpressionOnASingleLine", + Style.AllowShortRequiresExpressionOnASingleLine); IO.mapOptional("AlwaysBreakAfterDefinitionReturnType", Style.AlwaysBreakAfterDefinitionReturnType); IO.mapOptional("AlwaysBreakAfterReturnType", @@ -1260,11 +1264,13 @@ 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; LLVMStyle.AllowShortLambdasOnASingleLine = FormatStyle::SLS_All; LLVMStyle.AllowShortLoopsOnASingleLine = false; + LLVMStyle.AllowShortRequiresExpressionOnASingleLine = true; LLVMStyle.AlwaysBreakAfterReturnType = FormatStyle::RTBS_None; LLVMStyle.AlwaysBreakAfterDefinitionReturnType = FormatStyle::DRTBS_None; LLVMStyle.AlwaysBreakBeforeMultilineStrings = false; Index: clang/lib/Format/UnwrappedLineFormatter.cpp =================================================================== --- clang/lib/Format/UnwrappedLineFormatter.cpp +++ clang/lib/Format/UnwrappedLineFormatter.cpp @@ -502,6 +502,10 @@ // Try to merge records. if (TheLine->Last->is(TT_EnumLBrace)) { ShouldMerge = Style.AllowShortEnumsOnASingleLine; + } else if (TheLine->Last->is(TT_RequiresExpressionLBrace)) { + ShouldMerge = Style.AllowShortRequiresExpressionOnASingleLine; + } 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/UnwrappedLineParser.cpp =================================================================== --- clang/lib/Format/UnwrappedLineParser.cpp +++ clang/lib/Format/UnwrappedLineParser.cpp @@ -1085,6 +1085,12 @@ FormatTok->setBlockKind(BK_Block); const FormatToken *OpeningBrace = FormatTok; nextToken(); + + if (NextLBracesType == TT_CompoundRequirementLBrace && + !Style.AllowShortRequiresExpressionOnASingleLine) { + addUnwrappedLine(); + } + { bool SkipIndent = (Style.isJavaScript() && (isGoogScope(*Line) || isIIFE(*Line, Keywords))); @@ -1096,6 +1102,7 @@ flushComments(isOnNewLine(*FormatTok)); Line->Level -= SkipIndent ? 0 : 1; } + nextToken(); } Index: clang/unittests/Format/ConfigParseTest.cpp =================================================================== --- clang/unittests/Format/ConfigParseTest.cpp +++ clang/unittests/Format/ConfigParseTest.cpp @@ -147,8 +147,10 @@ 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(AllowShortRequiresExpressionOnASingleLine); CHECK_PARSE_BOOL(BinPackArguments); CHECK_PARSE_BOOL(BinPackParameters); CHECK_PARSE_BOOL(BreakAfterJavaFieldAnnotations); Index: clang/unittests/Format/FormatTest.cpp =================================================================== --- clang/unittests/Format/FormatTest.cpp +++ clang/unittests/Format/FormatTest.cpp @@ -2899,6 +2899,35 @@ Style); } +TEST_F(FormatTest, ShortRequiresExpression) { + FormatStyle Style = getLLVMStyle(); + EXPECT_TRUE(Style.AllowShortRequiresExpressionOnASingleLine); + verifyFormat("template \n" + "concept c = requires(T a, T b) { a + b; };"); + Style.AllowShortRequiresExpressionOnASingleLine = false; + verifyFormat("template \n" + "concept c = requires(T a, T b) {\n" + " a + b;\n" + "};"); + // TODO: support BraceWrapping.AfterRequiresExpression. +} + +TEST_F(FormatTest, ShortCompoundRequirement) { + FormatStyle Style = getLLVMStyle(); + EXPECT_TRUE(Style.AllowShortCompoundRequirementOnASingleLine); + verifyFormat("template \n" + "concept c = requires(T x) {\n" + " { x + 1 } -> std::same_as;\n" + "};"); + Style.AllowShortCompoundRequirementOnASingleLine = false; + verifyFormat("template \n" + "concept c = requires(T x) {\n" + " {\n" + " x + 1\n" + " } -> std::same_as;\n" + "};"); +} + TEST_F(FormatTest, ShortCaseLabels) { FormatStyle Style = getLLVMStyle(); Style.AllowShortCaseLabelsOnASingleLine = true;