[PATCH] D132911: Fix annotating when deleting array of pointers

Jack Huang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 29 23:23:50 PDT 2022


jackhong12 created this revision.
jackhong12 added reviewers: owenpan, MyDeveloperDay, curdeius, HazardyKnusperkeks.
Herald added a project: All.
jackhong12 edited the summary of this revision.
jackhong12 published this revision for review.
Herald added a project: clang.

Fixes https://github.com/llvm/llvm-project/issues/57418

The token `*` below should be annotated as `PointerOrReference`.

  delete[] *ptr;


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D132911

Files:
  clang/lib/Format/TokenAnnotator.cpp
  clang/unittests/Format/FormatTest.cpp
  clang/unittests/Format/TokenAnnotatorTest.cpp


Index: clang/unittests/Format/TokenAnnotatorTest.cpp
===================================================================
--- clang/unittests/Format/TokenAnnotatorTest.cpp
+++ clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -120,6 +120,14 @@
   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_PointerOrReference);
+  Tokens = annotate("delete[] **ptr;");
+  EXPECT_EQ(Tokens.size(), 8u) << Tokens;
+  EXPECT_TOKEN(Tokens[3], tok::star, TT_PointerOrReference);
+  EXPECT_TOKEN(Tokens[4], tok::star, TT_PointerOrReference);
 }
 
 TEST_F(TokenAnnotatorTest, UnderstandsUsesOfPlusAndMinus) {
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -10535,6 +10535,10 @@
                "} &&ptr = {};",
                Style);
 
+  Style.PointerAlignment = FormatStyle::PAS_Right;
+  verifyFormat("delete[] *ptr;", Style);
+  verifyFormat("delete[] **ptr;", Style);
+
   verifyIndependentOfContext("MACRO(int *i);");
   verifyIndependentOfContext("MACRO(auto *a);");
   verifyIndependentOfContext("MACRO(const A *a);");
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -2372,6 +2372,12 @@
         !PrevToken->MatchingParen)
       return TT_PointerOrReference;
 
+    const FormatToken *Prev = PrevToken;
+    if (Prev->is(tok::r_square) && (Prev = Prev->getPreviousNonComment()) &&
+        Prev->is(tok::l_square) && (Prev = Prev->getPreviousNonComment()) &&
+        Prev->is(tok::kw_delete))
+      return TT_PointerOrReference;
+
     if (PrevToken->Tok.isLiteral() ||
         PrevToken->isOneOf(tok::r_paren, tok::r_square, tok::kw_true,
                            tok::kw_false, tok::r_brace)) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D132911.456547.patch
Type: text/x-patch
Size: 2114 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220830/0d1959f4/attachment.bin>


More information about the cfe-commits mailing list