Skip to content

Conversation

ZhongRuoyu
Copy link
Contributor

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:

case tok::kw_requires: {
auto RequiresToken = FormatTok;
nextToken();
parseRequiresExpression(RequiresToken);
break;
}

Fixes #162984.

…alizers

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 llvm#162984.

[1]: https://github.com/llvm/llvm-project/blob/7eee67202378932d03331ad04e7d07ed4d988381/clang/lib/Format/UnwrappedLineParser.cpp#L2713-L2718

Signed-off-by: Ruoyu Zhong <zhongruoyu@outlook.com>
@llvmbot
Copy link
Member

llvmbot commented Oct 11, 2025

@llvm/pr-subscribers-clang-format

Author: Ruoyu Zhong (ZhongRuoyu)

Changes

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:

case tok::kw_requires: {
auto RequiresToken = FormatTok;
nextToken();
parseRequiresExpression(RequiresToken);
break;
}

Fixes #162984.


Full diff: https://github.com/llvm/llvm-project/pull/163005.diff

2 Files Affected:

  • (modified) clang/lib/Format/UnwrappedLineParser.cpp (+6)
  • (modified) clang/unittests/Format/FormatTest.cpp (+6)
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp
index 28797433e06e3..dec71191d7356 100644
--- a/clang/lib/Format/UnwrappedLineParser.cpp
+++ b/clang/lib/Format/UnwrappedLineParser.cpp
@@ -2569,6 +2569,12 @@ bool UnwrappedLineParser::parseBracedList(bool IsAngleBracket, bool IsEnum) {
       if (IsEnum && !Style.AllowShortEnumsOnASingleLine)
         addUnwrappedLine();
       break;
+    case tok::kw_requires: {
+      auto *RequiresToken = FormatTok;
+      nextToken();
+      parseRequiresExpression(RequiresToken);
+      break;
+    }
     default:
       nextToken();
       break;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index fef70365b5e18..5a8056e310d45 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -26973,6 +26973,12 @@ TEST_F(FormatTest, RequiresExpressionIndentation) {
                Style);
 }
 
+TEST_F(FormatTest, RequiresExpressionInBracedInitializer) {
+  verifyFormat("bool foo{requires { 0; }};");
+  verifyFormat("int bar{requires(T t) { t.f(); }};");
+  verifyFormat("auto baz{requires { typename T::type; } && true};");
+}
+
 TEST_F(FormatTest, StatementAttributeLikeMacros) {
   FormatStyle Style = getLLVMStyle();
   StringRef Source = "void Foo::slot() {\n"

@ZhongRuoyu ZhongRuoyu force-pushed the clang-format-requires-expr-in-braced-init branch from f769ff0 to e836a16 Compare October 12, 2025 05:40
@owenca owenca merged commit 513b10d into llvm:main Oct 12, 2025
10 checks passed
@ZhongRuoyu ZhongRuoyu deleted the clang-format-requires-expr-in-braced-init branch October 13, 2025 06:18
DharuniRAcharya pushed a commit to DharuniRAcharya/llvm-project that referenced this pull request Oct 13, 2025
…tializers (llvm#163005)

`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`.

Fixes llvm#162984.
akadutta pushed a commit to akadutta/llvm-project that referenced this pull request Oct 14, 2025
…tializers (llvm#163005)

`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`.

Fixes llvm#162984.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[clang-format] Incorrect formatting of requires expressions inside brace initialization

4 participants