[clang] [clang-format] Fix a bug in annotating CastRParen before unary && (PR #89346)
Owen Pan via cfe-commits
cfe-commits at lists.llvm.org
Thu Apr 18 22:26:32 PDT 2024
https://github.com/owenca created https://github.com/llvm/llvm-project/pull/89346
Also fix a bug in annotating TrailingAnnotation.
Closes #61233.
>From 53b2846e35e0e8541ca0c3c6f63d31821dc9441c Mon Sep 17 00:00:00 2001
From: Owen Pan <owenpiano at gmail.com>
Date: Thu, 18 Apr 2024 21:21:47 -0700
Subject: [PATCH] [clang-format] Fix a bug in annotating CastRParen before
unary &&
Also fix a bug in annotating TrailingAnnotation.
Closes #61233.
---
clang/lib/Format/TokenAnnotator.cpp | 5 ++++-
clang/unittests/Format/TokenAnnotatorTest.cpp | 6 ++++++
2 files changed, 10 insertions(+), 1 deletion(-)
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index a679683077ac94..cdfb4256e41d93 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1543,6 +1543,7 @@ class AnnotatingParser {
return false;
if (Line.MustBeDeclaration && Contexts.size() == 1 &&
!Contexts.back().IsExpression && !Line.startsWith(TT_ObjCProperty) &&
+ !Line.startsWith(tok::l_paren) &&
!Tok->isOneOf(TT_TypeDeclarationParen, TT_RequiresExpressionLParen)) {
if (const auto *Previous = Tok->Previous;
!Previous ||
@@ -2726,8 +2727,10 @@ class AnnotatingParser {
}
}
- if (Tok.Next->isOneOf(tok::question, tok::ampamp))
+ if (Tok.Next->is(tok::question) ||
+ (Tok.Next->is(tok::ampamp) && !Tok.Previous->isTypeName(IsCpp))) {
return false;
+ }
// `foreach((A a, B b) in someList)` should not be seen as a cast.
if (Tok.Next->is(Keywords.kw_in) && Style.isCSharp())
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 4f445c64ab303a..34999b7376397b 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -599,6 +599,12 @@ TEST_F(TokenAnnotatorTest, UnderstandsCasts) {
ASSERT_EQ(Tokens.size(), 6u) << Tokens;
EXPECT_TOKEN(Tokens[2], tok::r_paren, TT_CastRParen);
+ Tokens = annotate("(uint32_t)&&label;");
+ ASSERT_EQ(Tokens.size(), 7u) << Tokens;
+ EXPECT_TOKEN(Tokens[2], tok::r_paren, TT_CastRParen);
+ EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_UnaryOperator);
+ EXPECT_TOKEN(Tokens[4], tok::identifier, TT_Unknown);
+
Tokens = annotate("auto x = (Foo)p;");
ASSERT_EQ(Tokens.size(), 9u) << Tokens;
EXPECT_TOKEN(Tokens[5], tok::r_paren, TT_CastRParen);
More information about the cfe-commits
mailing list