curdeius updated this revision to Diff 406363. curdeius added a comment. Revert unrelated changes.
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 @@ -91,6 +91,28 @@ EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_RecordLBrace); } +TEST_F(TokenAnnotatorTest, UnderstandsNewAndDelete) { + 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 @@ -9725,6 +9725,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); }
Index: clang/unittests/Format/TokenAnnotatorTest.cpp =================================================================== --- clang/unittests/Format/TokenAnnotatorTest.cpp +++ clang/unittests/Format/TokenAnnotatorTest.cpp @@ -91,6 +91,28 @@ EXPECT_TOKEN(Tokens[2], tok::l_brace, TT_RecordLBrace); } +TEST_F(TokenAnnotatorTest, UnderstandsNewAndDelete) { + 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 @@ -9725,6 +9725,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); }
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits