[clang] a77c67f - [clang-format] Fix formatting of the array form of delete.
Marek Kurdej via cfe-commits
cfe-commits at lists.llvm.org
Wed Feb 9 13:36:17 PST 2022
Author: Marek Kurdej
Date: 2022-02-09T22:36:13+01:00
New Revision: a77c67f93917596f9eded9edaced4a9d355a4e1c
URL: https://github.com/llvm/llvm-project/commit/a77c67f93917596f9eded9edaced4a9d355a4e1c
DIFF: https://github.com/llvm/llvm-project/commit/a77c67f93917596f9eded9edaced4a9d355a4e1c.diff
LOG: [clang-format] Fix formatting of the array form of delete.
Fixes https://github.com/llvm/llvm-project/issues/53576.
There was an inconsistency in formatting of delete expressions.
Before:
```
delete (void*)a;
delete[](void*) a;
```
After this patch:
```
delete (void*)a;
delete[] (void*)a;
```
Reviewed By: HazardyKnusperkeks, owenpan
Differential Revision: https://reviews.llvm.org/D119117
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 76f623147d3a4..28b244b9c59f1 100644
--- a/clang/lib/Format/TokenAnnotator.cpp
+++ b/clang/lib/Format/TokenAnnotator.cpp
@@ -1883,6 +1883,25 @@ class AnnotatingParser {
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 @@ bool TokenAnnotator::spaceRequiredBetween(const AnnotatedLine &Line,
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);
}
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 14942d1ba420e..6f76a0c62edf9 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -9744,6 +9744,7 @@ TEST_F(FormatTest, UnderstandsNewAndDelete) {
" 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;");
diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp
index 88deee974bbf5..acb7386a89df9 100644
--- a/clang/unittests/Format/TokenAnnotatorTest.cpp
+++ b/clang/unittests/Format/TokenAnnotatorTest.cpp
@@ -97,6 +97,28 @@ TEST_F(TokenAnnotatorTest, UnderstandsLBracesInMacroDefinition) {
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
More information about the cfe-commits
mailing list