Skip to content

Commit 69209e3

Browse files
Backl1ghtowenca
authored andcommitted
[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
1 parent 66f4a13 commit 69209e3

File tree

6 files changed

+82
-0
lines changed

6 files changed

+82
-0
lines changed

clang/docs/ClangFormatStyleOptions.rst

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1215,6 +1215,27 @@ the configuration (without a prefix: ``Auto``).
12151215
return;
12161216
}
12171217

1218+
.. _AllowShortCompoundRequirementOnASingleLine:
1219+
1220+
**AllowShortCompoundRequirementOnASingleLine** (``Boolean``) :versionbadge:`clang-format 16` :ref:`<AllowShortCompoundRequirementOnASingleLine>`
1221+
Allow short compound requirement on a single line.
1222+
1223+
.. code-block:: c++
1224+
1225+
true:
1226+
template <typename T>
1227+
concept c = requires(T x) {
1228+
{ x + 1 } -> std::same_as<int>;
1229+
};
1230+
1231+
false:
1232+
template <typename T>
1233+
concept c = requires(T x) {
1234+
{
1235+
x + 1
1236+
} -> std::same_as<int>;
1237+
};
1238+
12181239
.. _AllowShortEnumsOnASingleLine:
12191240

12201241
**AllowShortEnumsOnASingleLine** (``Boolean``) :versionbadge:`clang-format 11` :ref:`<AllowShortEnumsOnASingleLine>`

clang/include/clang/Format/Format.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,25 @@ struct FormatStyle {
685685
/// \version 3.6
686686
bool AllowShortCaseLabelsOnASingleLine;
687687

688+
/// Allow short compound requirement on a single line.
689+
/// \code
690+
/// true:
691+
/// template <typename T>
692+
/// concept c = requires(T x) {
693+
/// { x + 1 } -> std::same_as<int>;
694+
/// };
695+
///
696+
/// false:
697+
/// template <typename T>
698+
/// concept c = requires(T x) {
699+
/// {
700+
/// x + 1
701+
/// } -> std::same_as<int>;
702+
/// };
703+
/// \endcode
704+
/// \version 16
705+
bool AllowShortCompoundRequirementOnASingleLine;
706+
688707
/// Allow short enums on a single line.
689708
/// \code
690709
/// true:
@@ -4650,6 +4669,8 @@ struct FormatStyle {
46504669
AllowShortBlocksOnASingleLine == R.AllowShortBlocksOnASingleLine &&
46514670
AllowShortCaseLabelsOnASingleLine ==
46524671
R.AllowShortCaseLabelsOnASingleLine &&
4672+
AllowShortCompoundRequirementOnASingleLine ==
4673+
R.AllowShortCompoundRequirementOnASingleLine &&
46534674
AllowShortEnumsOnASingleLine == R.AllowShortEnumsOnASingleLine &&
46544675
AllowShortFunctionsOnASingleLine ==
46554676
R.AllowShortFunctionsOnASingleLine &&

clang/lib/Format/Format.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -938,6 +938,8 @@ template <> struct MappingTraits<FormatStyle> {
938938
Style.AllowShortBlocksOnASingleLine);
939939
IO.mapOptional("AllowShortCaseLabelsOnASingleLine",
940940
Style.AllowShortCaseLabelsOnASingleLine);
941+
IO.mapOptional("AllowShortCompoundRequirementOnASingleLine",
942+
Style.AllowShortCompoundRequirementOnASingleLine);
941943
IO.mapOptional("AllowShortEnumsOnASingleLine",
942944
Style.AllowShortEnumsOnASingleLine);
943945
IO.mapOptional("AllowShortFunctionsOnASingleLine",
@@ -1441,6 +1443,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) {
14411443
LLVMStyle.AllowAllParametersOfDeclarationOnNextLine = true;
14421444
LLVMStyle.AllowShortBlocksOnASingleLine = FormatStyle::SBS_Never;
14431445
LLVMStyle.AllowShortCaseLabelsOnASingleLine = false;
1446+
LLVMStyle.AllowShortCompoundRequirementOnASingleLine = true;
14441447
LLVMStyle.AllowShortEnumsOnASingleLine = true;
14451448
LLVMStyle.AllowShortFunctionsOnASingleLine = FormatStyle::SFS_All;
14461449
LLVMStyle.AllowShortIfStatementsOnASingleLine = FormatStyle::SIS_Never;

clang/lib/Format/UnwrappedLineFormatter.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,8 @@ class LineJoiner {
520520
// Try to merge records.
521521
if (TheLine->Last->is(TT_EnumLBrace)) {
522522
ShouldMerge = Style.AllowShortEnumsOnASingleLine;
523+
} else if (TheLine->Last->is(TT_RequiresExpressionLBrace)) {
524+
ShouldMerge = Style.AllowShortCompoundRequirementOnASingleLine;
523525
} else if (TheLine->Last->isOneOf(TT_ClassLBrace, TT_StructLBrace)) {
524526
// NOTE: We use AfterClass (whereas AfterStruct exists) for both classes
525527
// and structs, but it seems that wrapping is still handled correctly

clang/unittests/Format/ConfigParseTest.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ TEST(ConfigParseTest, ParsesConfigurationBools) {
154154
CHECK_PARSE_BOOL(AllowAllArgumentsOnNextLine);
155155
CHECK_PARSE_BOOL(AllowAllParametersOfDeclarationOnNextLine);
156156
CHECK_PARSE_BOOL(AllowShortCaseLabelsOnASingleLine);
157+
CHECK_PARSE_BOOL(AllowShortCompoundRequirementOnASingleLine);
157158
CHECK_PARSE_BOOL(AllowShortEnumsOnASingleLine);
158159
CHECK_PARSE_BOOL(AllowShortLoopsOnASingleLine);
159160
CHECK_PARSE_BOOL(BinPackArguments);

clang/unittests/Format/FormatTest.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2767,6 +2767,40 @@ TEST_F(FormatTest, ShortEnums) {
27672767
Style);
27682768
}
27692769

2770+
TEST_F(FormatTest, ShortCompoundRequirement) {
2771+
FormatStyle Style = getLLVMStyle();
2772+
EXPECT_TRUE(Style.AllowShortCompoundRequirementOnASingleLine);
2773+
verifyFormat("template <typename T>\n"
2774+
"concept c = requires(T x) {\n"
2775+
" { x + 1 } -> std::same_as<int>;\n"
2776+
"};",
2777+
Style);
2778+
verifyFormat("template <typename T>\n"
2779+
"concept c = requires(T x) {\n"
2780+
" { x + 1 } -> std::same_as<int>;\n"
2781+
" { x + 2 } -> std::same_as<int>;\n"
2782+
"};",
2783+
Style);
2784+
Style.AllowShortCompoundRequirementOnASingleLine = false;
2785+
verifyFormat("template <typename T>\n"
2786+
"concept c = requires(T x) {\n"
2787+
" {\n"
2788+
" x + 1\n"
2789+
" } -> std::same_as<int>;\n"
2790+
"};",
2791+
Style);
2792+
verifyFormat("template <typename T>\n"
2793+
"concept c = requires(T x) {\n"
2794+
" {\n"
2795+
" x + 1\n"
2796+
" } -> std::same_as<int>;\n"
2797+
" {\n"
2798+
" x + 2\n"
2799+
" } -> std::same_as<int>;\n"
2800+
"};",
2801+
Style);
2802+
}
2803+
27702804
TEST_F(FormatTest, ShortCaseLabels) {
27712805
FormatStyle Style = getLLVMStyle();
27722806
Style.AllowShortCaseLabelsOnASingleLine = true;

0 commit comments

Comments
 (0)