Skip to content

Commit 4287fc3

Browse files
committed
[clang-format] Fix formatting of requires expressions in braced initializers
When clang-format encountered a requires expression inside a braced initializer (e.g., `bool foo{requires { 0; }};`), it would incorrectly format the code to the following: bool bar{requires {0; } } ; The issue was that UnwrappedLineParser::parseBracedList had no explicit handling for the requires keyword, so it would just call nextToken() instead of properly parsing the requires expression. This fix adds a case for tok::kw_requires in parseBracedList, calling parseRequiresExpression to handle it correctly, matching the existing behavior in parseParens [1]. Fixes #162984. [1]: https://github.com/llvm/llvm-project/blob/7eee67202378932d03331ad04e7d07ed4d988381/clang/lib/Format/UnwrappedLineParser.cpp#L2713-L2718 Signed-off-by: Ruoyu Zhong <[email protected]>
1 parent 7eee672 commit 4287fc3

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

clang/lib/Format/UnwrappedLineParser.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2569,6 +2569,12 @@ bool UnwrappedLineParser::parseBracedList(bool IsAngleBracket, bool IsEnum) {
25692569
if (IsEnum && !Style.AllowShortEnumsOnASingleLine)
25702570
addUnwrappedLine();
25712571
break;
2572+
case tok::kw_requires: {
2573+
auto *RequiresToken = FormatTok;
2574+
nextToken();
2575+
parseRequiresExpression(RequiresToken);
2576+
break;
2577+
}
25722578
default:
25732579
nextToken();
25742580
break;

clang/unittests/Format/FormatTest.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26973,6 +26973,12 @@ TEST_F(FormatTest, RequiresExpressionIndentation) {
2697326973
Style);
2697426974
}
2697526975

26976+
TEST_F(FormatTest, RequiresExpressionInBracedInitializer) {
26977+
verifyFormat("bool foo{requires { 0; }};");
26978+
verifyFormat("int bar{requires(T t) { t.f(); }};");
26979+
verifyFormat("auto baz{requires { typename T::type; } && true};");
26980+
}
26981+
2697626982
TEST_F(FormatTest, StatementAttributeLikeMacros) {
2697726983
FormatStyle Style = getLLVMStyle();
2697826984
StringRef Source = "void Foo::slot() {\n"

0 commit comments

Comments
 (0)