[clang] b6cc667 - [clang-format] Annotate ampamp after new/delete as BinaryOperator (#89033)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 17 19:53:28 PDT 2024
Author: Owen Pan
Date: 2024-04-17T19:53:24-07:00
New Revision: b6cc667190e3bee7485a225d3dadd8a57c0a22b6
URL: https://github.com/llvm/llvm-project/commit/b6cc667190e3bee7485a225d3dadd8a57c0a22b6
DIFF: https://github.com/llvm/llvm-project/commit/b6cc667190e3bee7485a225d3dadd8a57c0a22b6.diff
LOG: [clang-format] Annotate ampamp after new/delete as BinaryOperator (#89033)
Fixes #78789.
Added:
Modified:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 80e5605fb5778d..a679683077ac94 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2912,6 +2912,8 @@ class AnnotatingParser {
return TT_UnaryOperator;
if (PrevToken->is(TT_TypeName))
return TT_PointerOrReference;
+ if (PrevToken->isOneOf(tok::kw_new, tok::kw_delete) && Tok.is(tok::ampamp))
+ return TT_BinaryOperator;
const FormatToken *NextToken = Tok.getNextNonComment();
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index a71b6806476956..4f445c64ab303a 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -309,6 +309,16 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
EXPECT_TOKEN(Tokens[6], tok::l_paren, TT_OverloadedOperatorLParen);
EXPECT_TOKEN(Tokens[8], tok::l_brace, TT_FunctionLBrace);
EXPECT_TOKEN(Tokens[11], tok::amp, TT_PointerOrReference);
+
+ Tokens = annotate("if (new && num) {\n"
+ " new = 1;\n"
+ "}\n"
+ "if (!delete && num) {\n"
+ " delete = 1;\n"
+ "}");
+ ASSERT_EQ(Tokens.size(), 26u) << Tokens;
+ EXPECT_TOKEN(Tokens[3], tok::ampamp, TT_BinaryOperator);
+ EXPECT_TOKEN(Tokens[16], tok::ampamp, TT_BinaryOperator);
}
TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {
More information about the cfe-commits
mailing list