Skip to content

Commit 7ee4236

Browse files
[clang-format] clang-format eats space in front of attributes for operator delete
llvm/llvm-project#27037 Sorry its taken so long to get to this issue! (got it before it hit its 6th birthday!) ``` void operator delete(void *foo)ATTRIB; ``` (void *foo) is incorrectly determined to be a C-Style Cast resulting in the space being removed after the ) and before the attrib, due to the detection of ``` delete (A* )a; ``` The following was previously unaffected ``` void operator new(void *foo) ATTRIB; ``` Fixes #27037 Reviewed By: curdeius, HazardyKnusperkeks Differential Revision: https://reviews.llvm.org/D116920
1 parent bb13036 commit 7ee4236

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,6 +1893,13 @@ class AnnotatingParser {
18931893
LeftOfParens = LeftOfParens->MatchingParen->Previous;
18941894
}
18951895

1896+
// The Condition directly below this one will see the operator arguments
1897+
// as a (void *foo) cast.
1898+
// void operator delete(void *foo) ATTRIB;
1899+
if (LeftOfParens->Tok.getIdentifierInfo() && LeftOfParens->Previous &&
1900+
LeftOfParens->Previous->is(tok::kw_operator))
1901+
return false;
1902+
18961903
// If there is an identifier (or with a few exceptions a keyword) right
18971904
// before the parentheses, this is unlikely to be a cast.
18981905
if (LeftOfParens->Tok.getIdentifierInfo() &&

clang/unittests/Format/FormatTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9459,6 +9459,11 @@ TEST_F(FormatTest, UnderstandsNewAndDelete) {
94599459
" new (aaaaaaaaaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaaaaaaaaaa))\n"
94609460
" typename aaaaaaaaaaaaaaaaaaaaaaaa();");
94619461
verifyFormat("delete[] h->p;");
9462+
9463+
verifyFormat("void operator delete(void *foo) ATTRIB;");
9464+
verifyFormat("void operator new(void *foo) ATTRIB;");
9465+
verifyFormat("void operator delete[](void *foo) ATTRIB;");
9466+
verifyFormat("void operator delete(void *ptr) noexcept;");
94629467
}
94639468

94649469
TEST_F(FormatTest, UnderstandsUsesOfStarAndAmp) {

0 commit comments

Comments
 (0)