[clang] [clang-format] Annotate ampamp after new/delete as BinaryOperator (PR #89033)
via cfe-commits
cfe-commits at lists.llvm.org
Wed Apr 17 00:50:45 PDT 2024
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-clang-format
Author: Owen Pan (owenca)
<details>
<summary>Changes</summary>
Fixes #<!-- -->78789.
---
Full diff: https://github.com/llvm/llvm-project/pull/89033.diff
2 Files Affected:
- (modified) clang/lib/Format/TokenAnnotator.cpp (+2)
- (modified) clang/unittests/Format/TokenAnnotatorTest.cpp (+10)
``````````diff
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 628f70417866c3..272d7bbb352daf 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 da02ced8c7a949..10e0eca62510a7 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) {
``````````
</details>
https://github.com/llvm/llvm-project/pull/89033
More information about the cfe-commits
mailing list