diff --git a/clang/lib/Format/TokenAnnotator.cpp b/clang/lib/Format/TokenAnnotator.cpp --- a/clang/lib/Format/TokenAnnotator.cpp +++ b/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); } diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp --- a/clang/unittests/Format/FormatTest.cpp +++ b/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;"); @@ -20009,15 +20010,13 @@ TEST_F(FormatTest, CountsUTF8CharactersProperly) { verifyFormat("\"Однажды в студёную зимнюю пору...\"", getLLVMStyleWithColumns(35)); - verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", - getLLVMStyleWithColumns(31)); + verifyFormat("\"一 二 三 四 五 六 七 八 九 十\"", getLLVMStyleWithColumns(31)); verifyFormat("// Однажды в студёную зимнюю пору...", getLLVMStyleWithColumns(36)); verifyFormat("// 一 二 三 四 五 六 七 八 九 十", getLLVMStyleWithColumns(32)); verifyFormat("/* Однажды в студёную зимнюю пору... */", getLLVMStyleWithColumns(39)); - verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", - getLLVMStyleWithColumns(35)); + verifyFormat("/* 一 二 三 四 五 六 七 八 九 十 */", getLLVMStyleWithColumns(35)); } TEST_F(FormatTest, SplitsUTF8Strings) { @@ -20037,21 +20036,20 @@ "\"пору,\"", format("\"Однажды, в студёную зимнюю пору,\"", getLLVMStyleWithColumns(13))); + EXPECT_EQ("\"一 二 三 \"\n" + "\"四 五六 \"\n" + "\"七 八 九 \"\n" + "\"十\"", + format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); EXPECT_EQ( - "\"一 二 三 \"\n" - "\"四 五六 \"\n" - "\"七 八 九 \"\n" - "\"十\"", - format("\"一 二 三 四 五六 七 八 九 十\"", getLLVMStyleWithColumns(11))); - EXPECT_EQ("\"一\t\"\n" - "\"二 \t\"\n" - "\"三 四 \"\n" - "\"五\t\"\n" - "\"六 \t\"\n" - "\"七 \"\n" - "\"八九十\tqq\"", - format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", - getLLVMStyleWithColumns(11))); + "\"一\t\"\n" + "\"二 \t\"\n" + "\"三 四 \"\n" + "\"五\t\"\n" + "\"六 \t\"\n" + "\"七 \"\n" + "\"八九十\tqq\"", + format("\"一\t二 \t三 四 五\t六 \t七 八九十\tqq\"", getLLVMStyleWithColumns(11))); // UTF8 character in an escape sequence. EXPECT_EQ("\"aaaaaa\"\n" @@ -20096,16 +20094,16 @@ format("/* Гляжу, поднимается медленно в гору\n" " * Лошадка, везущая хворосту воз. */", getLLVMStyleWithColumns(13))); - EXPECT_EQ( - "/* 一二三\n" - " * 四五六七\n" - " * 八 九\n" - " * 十 */", - format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); + EXPECT_EQ("/* 一二三\n" + " * 四五六七\n" + " * 八 九\n" + " * 十 */", + format("/* 一二三 四五六七 八 九 十 */", getLLVMStyleWithColumns(9))); EXPECT_EQ("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯\n" " * 𝕓𝕪𝕥𝕖\n" " * 𝖀𝕿𝕱-𝟠 */", - format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12))); + format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", + getLLVMStyleWithColumns(12))); } #endif // _MSC_VER diff --git a/clang/unittests/Format/TokenAnnotatorTest.cpp b/clang/unittests/Format/TokenAnnotatorTest.cpp --- a/clang/unittests/Format/TokenAnnotatorTest.cpp +++ b/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