[clang] 513b10d - [clang-format] Fix formatting of `requires` expressions in braced initializers (#163005)
via cfe-commits
cfe-commits at lists.llvm.org
Sun Oct 12 16:43:08 PDT 2025
Author: Ruoyu Zhong
Date: 2025-10-12T16:43:04-07:00
New Revision: 513b10df4308a4704989655241090399173c69dd
URL: https://github.com/llvm/llvm-project/commit/513b10df4308a4704989655241090399173c69dd
DIFF: https://github.com/llvm/llvm-project/commit/513b10df4308a4704989655241090399173c69dd.diff
LOG: [clang-format] Fix formatting of `requires` expressions in braced initializers (#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 https://github.com/llvm/llvm-project/issues/162984.
Added:
Modified:
clang/lib/Format/UnwrappedLineParser.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
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/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index c21b118847d99..1152466b0179f 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -1490,6 +1490,11 @@ TEST_F(TokenAnnotatorTest, UnderstandsRequiresExpressions) {
EXPECT_TOKEN(Tokens[4], tok::l_paren, TT_RequiresExpressionLParen);
EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_RequiresExpressionLBrace);
+ Tokens = annotate("bool foo{requires { 0; }};");
+ ASSERT_EQ(Tokens.size(), 11u) << Tokens;
+ EXPECT_TOKEN(Tokens[3], tok::kw_requires, TT_RequiresExpression);
+ EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_RequiresExpressionLBrace);
+
Tokens = annotate("if (requires(int i) { i + 5; }) return;");
ASSERT_EQ(Tokens.size(), 17u) << Tokens;
EXPECT_TOKEN(Tokens[2], tok::kw_requires, TT_RequiresExpression);
More information about the cfe-commits
mailing list