[clang] 3bbdf06 - [clang-format] Fix annotating when deleting array of pointers
via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 2 14:57:03 PDT 2022
Author: jackh
Date: 2022-09-03T05:56:49+08:00
New Revision: 3bbdf06b28275a56ad36a844d271d84956d1d7e9
URL: https://github.com/llvm/llvm-project/commit/3bbdf06b28275a56ad36a844d271d84956d1d7e9
DIFF: https://github.com/llvm/llvm-project/commit/3bbdf06b28275a56ad36a844d271d84956d1d7e9.diff
LOG: [clang-format] Fix annotating when deleting array of pointers
Fixes https://github.com/llvm/llvm-project/issues/57418
The token `*` below should be annotated as `UnaryOperator`.
```
delete[] *ptr;
```
Reviewed By: owenpan, MyDeveloperDay
Differential Revision: https://reviews.llvm.org/D132911
Added:
Modified:
clang/lib/Format/TokenAnnotator.cpp
clang/unittests/Format/FormatTest.cpp
clang/unittests/Format/TokenAnnotatorTest.cpp
Removed:
################################################################################
diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp
index 94b897a962056..2a64a3bb9c413 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -2372,6 +2372,9 @@ class AnnotatingParser {
!PrevToken->MatchingParen)
return TT_PointerOrReference;
+ if (PrevToken->endsSequence(tok::r_square, tok::l_square, tok::kw_delete))
+ return TT_UnaryOperator;
+
if (PrevToken->Tok.isLiteral() ||
PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
tok::kw_false, tok::r_brace)) {
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 5d1129b851dff..57a6d83578f1d 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -10535,6 +10535,11 @@ TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {
"} &&ptr = {};",
Style);
+ Style.PointerAlignment = FormatStyle::PAS_Left;
+ verifyFormat("delete[] *ptr;", Style);
+ verifyFormat("delete[] **ptr;", Style);
+ verifyFormat("delete[] *(ptr);", Style);
+
verifyIndependentOfContext("MACRO(int *i);");
verifyIndependentOfContext("MACRO(auto *a);");
verifyIndependentOfContext("MACRO(const A *a);");
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 4e9d5c77f99e5..6a10d679fbc72 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -120,6 +120,17 @@ TEST_F(TokenAnnotatorTest, UnderstandsUsesOfStarAndAmp) {
Tokens = annotate("int i = int{42} * 2;");
EXPECT_EQ(Tokens.size(), 11u) << Tokens;
EXPECT_TOKEN(Tokens[7], tok::star, TT_BinaryOperator);
+
+ Tokens = annotate("delete[] *ptr;");
+ EXPECT_EQ(Tokens.size(), 7u) << Tokens;
+ EXPECT_TOKEN(Tokens[3], tok::star, TT_UnaryOperator);
+ Tokens = annotate("delete[] **ptr;");
+ EXPECT_EQ(Tokens.size(), 8u) << Tokens;
+ EXPECT_TOKEN(Tokens[3], tok::star, TT_UnaryOperator);
+ EXPECT_TOKEN(Tokens[4], tok::star, TT_UnaryOperator);
+ Tokens = annotate("delete[] *(ptr);");
+ EXPECT_EQ(Tokens.size(), 9u) << Tokens;
+ EXPECT_TOKEN(Tokens[3], tok::star, TT_UnaryOperator);
}
TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {
More information about the cfe-commits
mailing list