[clang] [clang-format] Fix annotating paren after in (PR #206810)
Björn Schäpers via cfe-commits
cfe-commits at lists.llvm.org
Thu Jul 2 12:09:07 PDT 2026
https://github.com/HazardyKnusperkeks updated https://github.com/llvm/llvm-project/pull/206810
>From f09076ca6e723a0cb1a3f8eff6ca98d513ee9877 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <bjoern at hazardy.de>
Date: Tue, 30 Jun 2026 21:20:30 +0200
Subject: [PATCH 1/2] [clang-format] Fix annotating paren after in
The y.in(foo) was considered a cast, and thus the following - annotated as unary operator.
Fixes #206339
---
clang/lib/Format/TokenAnnotator.cpp | 6 ++++--
clang/unittests/Format/TokenAnnotatorTest.cpp | 4 ++++
2 files changed, 8 insertions(+), 2 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 4a771a4a92a43..6378913734452 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2821,8 +2821,10 @@ class AnnotatingParser {
// If there is an identifier (or with a few exceptions a keyword) right
// before the parentheses, this is unlikely to be a cast.
if (LeftOfParens->Tok.getIdentifierInfo() &&
- LeftOfParens->isNoneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
- tok::kw_delete, tok::kw_throw)) {
+ (LeftOfParens->isNoneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
+ tok::kw_delete, tok::kw_throw) ||
+ (LeftOfParens->Previous &&
+ LeftOfParens->Previous->is(tok::period)))) {
return false;
}
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 25ad923af8f4a..d9fbc62401cc7 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -997,6 +997,10 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) {
EXPECT_TOKEN(Tokens[10], tok::r_paren, TT_CastRParen);
EXPECT_TOKEN(Tokens[11], tok::amp, TT_UnaryOperator);
+ Tokens = annotate("auto x = y.in(foo) - w;");
+ ASSERT_EQ(Tokens.size(), 13u) << Tokens;
+ EXPECT_TOKEN(Tokens[8], tok::r_paren, TT_Unknown); // NOT TT_CastRParen
+
auto Style = getLLVMStyle();
Style.TypeNames.push_back("Foo");
Tokens = annotate("#define FOO(bar) foo((Foo)&bar)", Style);
>From c2c9c2a318bbc192785b85ade46f7d823d0ea263 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Bj=C3=B6rn=20Sch=C3=A4pers?= <bjoern at hazardy.de>
Date: Thu, 2 Jul 2026 21:08:51 +0200
Subject: [PATCH 2/2] Address review comments
---
clang/lib/Format/TokenAnnotator.cpp | 6 ++----
clang/unittests/Format/TokenAnnotatorTest.cpp | 1 +
2 files changed, 3 insertions(+), 4 deletions(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 6378913734452..df640cf503c9d 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2821,10 +2821,8 @@ class AnnotatingParser {
// If there is an identifier (or with a few exceptions a keyword) right
// before the parentheses, this is unlikely to be a cast.
if (LeftOfParens->Tok.getIdentifierInfo() &&
- (LeftOfParens->isNoneOf(Keywords.kw_in, tok::kw_return, tok::kw_case,
- tok::kw_delete, tok::kw_throw) ||
- (LeftOfParens->Previous &&
- LeftOfParens->Previous->is(tok::period)))) {
+ LeftOfParens->isNoneOf(TT_ObjCForIn, tok::kw_return, tok::kw_case,
+ tok::kw_delete, tok::kw_throw)) {
return false;
}
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index d9fbc62401cc7..8ef7e7387e269 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -1000,6 +1000,7 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) {
Tokens = annotate("auto x = y.in(foo) - w;");
ASSERT_EQ(Tokens.size(), 13u) << Tokens;
EXPECT_TOKEN(Tokens[8], tok::r_paren, TT_Unknown); // NOT TT_CastRParen
+ EXPECT_TOKEN(Tokens[9], tok::minus, TT_BinaryOperator);
auto Style = getLLVMStyle();
Style.TypeNames.push_back("Foo");
More information about the cfe-commits
mailing list