[PATCH] D119117: [clang-format] Fix formatting of the array form of delete.
Marek Kurdej via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 9 13:36:25 PST 2022
This revision was landed with ongoing or failed builds.
This revision was automatically updated to reflect the committed changes.
curdeius marked an inline comment as done.
Closed by commit rGa77c67f93917: [clang-format] Fix formatting of the array form of delete. (authored by curdeius).
Changed prior to commit:
https://reviews.llvm.org/D119117?vs=406363&id=407280#toc
Repository:
rG LLVM Github Monorepo
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D119117/new/
https://reviews.llvm.org/D119117
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
@@ -97,6 +97,28 @@
EXPECT_TOKEN(Tokens[4], tok::l_brace, TT_Unknown);
}
+TEST_F(TokenAnnotatorTest, UnderstandsDelete) {
+ auto Tokens = annotate("delete (void *)p;");
+ EXPECT_EQ(Tokens.size(), 8u) << Tokens;
+ EXPECT_TOKEN(Tokens[4], tok::r_paren, TT_CastRParen);
+
+ Tokens = annotate("delete[] (void *)p;");
+ EXPECT_EQ(Tokens.size(), 10u) << Tokens;
+ EXPECT_TOKEN(Tokens[6], tok::r_paren, TT_CastRParen);
+
+ Tokens = annotate("delete[] /*comment*/ (void *)p;");
+ EXPECT_EQ(Tokens.size(), 11u) << Tokens;
+ EXPECT_TOKEN(Tokens[7], tok::r_paren, TT_CastRParen);
+
+ Tokens = annotate("delete[/*comment*/] (void *)p;");
+ EXPECT_EQ(Tokens.size(), 11u) << Tokens;
+ EXPECT_TOKEN(Tokens[7], tok::r_paren, TT_CastRParen);
+
+ Tokens = annotate("delete/*comment*/[] (void *)p;");
+ EXPECT_EQ(Tokens.size(), 11u) << Tokens;
+ EXPECT_TOKEN(Tokens[7], tok::r_paren, TT_CastRParen);
+}
+
} // namespace
} // namespace format
} // namespace clang
Index: clang/unittests/Format/FormatTest.cpp
===================================================================
--- clang/unittests/Format/FormatTest.cpp
+++ clang/unittests/Format/FormatTest.cpp
@@ -9744,6 +9744,7 @@
" new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
" typename aaaaaaaaaaaaaaaaaaaaaaaa();");
verifyFormat("delete[] h->p;");
+ verifyFormat("delete[] (void *)p;");
verifyFormat("void operator delete(void *foo) ATTRIB;");
verifyFormat("void operator new(void *foo) ATTRIB;");
Index: clang/lib/Format/TokenAnnotator.cpp
===================================================================
--- clang/lib/Format/TokenAnnotator.cpp
+++ clang/lib/Format/TokenAnnotator.cpp
@@ -1883,6 +1883,25 @@
LeftOfParens = LeftOfParens->MatchingParen->Previous;
}
+ if (LeftOfParens->is(tok::r_square)) {
+ // delete[] (void *)ptr;
+ auto MayBeArrayDelete = [](FormatToken *Tok) -> FormatToken * {
+ if (Tok->isNot(tok::r_square))
+ return nullptr;
+
+ Tok = Tok->getPreviousNonComment();
+ if (!Tok || Tok->isNot(tok::l_square))
+ return nullptr;
+
+ Tok = Tok->getPreviousNonComment();
+ if (!Tok || Tok->isNot(tok::kw_delete))
+ return nullptr;
+ return Tok;
+ };
+ if (FormatToken *MaybeDelete = MayBeArrayDelete(LeftOfParens))
+ LeftOfParens = MaybeDelete;
+ }
+
// The Condition directly below this one will see the operator arguments
// as a (void *foo) cast.
// void operator delete(void *foo) ATTRIB;
@@ -3227,7 +3246,10 @@
if (Left.isOneOf(tok::kw_try, Keywords.kw___except, tok::kw_catch))
return Style.SpaceBeforeParensOptions.AfterControlStatements ||
spaceRequiredBeforeParens(Right);
- if (Left.isOneOf(tok::kw_new, tok::kw_delete))
+ if (Left.isOneOf(tok::kw_new, tok::kw_delete) ||
+ (Left.is(tok::r_square) && Left.MatchingParen &&
+ Left.MatchingParen->Previous &&
+ Left.MatchingParen->Previous->is(tok::kw_delete)))
return Style.SpaceBeforeParens != FormatStyle::SBPO_Never ||
spaceRequiredBeforeParens(Right);
}
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D119117.407280.patch
Type: text/x-patch
Size: 3466 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220209/22ac0c7e/attachment.bin>
More information about the cfe-commits
mailing list