Skip to content

Commit 5426b2f

Browse files
[clang-format] PR48535 clang-format Incorrectly Removes Space After C Style Cast When Type Is Not a Pointer
https://bugs.llvm.org/show_bug.cgi?id=48535 using `SpaceAfterCStyleCast: true` ``` size_t idx = (size_t) a; size_t idx = (size_t) (a - 1); ``` is formatted as: ``` size_t idx = (size_t) a; size_t idx = (size_t)(a - 1); ``` This revision aims to improve that by improving the function which tries to identify a CastRParen Reviewed By: curdeius Differential Revision: https://reviews.llvm.org/D93626
1 parent 6e60346 commit 5426b2f

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

clang/lib/Format/TokenAnnotator.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1915,6 +1915,13 @@ class AnnotatingParser {
19151915
if (Tok.Next->isOneOf(tok::identifier, tok::kw_this))
19161916
return true;
19171917

1918+
if (Tok.Next->is(tok::l_paren) &&
1919+
!(Tok.Previous && Tok.Previous->is(tok::identifier) &&
1920+
Tok.Previous->Previous &&
1921+
Tok.Previous->Previous->isOneOf(tok::arrowstar, tok::arrow,
1922+
tok::star)))
1923+
return true;
1924+
19181925
if (!Tok.Next->Next)
19191926
return false;
19201927

clang/unittests/Format/FormatTest.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11989,6 +11989,20 @@ TEST_F(FormatTest, ConfigurableSpacesInParentheses) {
1198911989
" do_something((int) i);\n"
1199011990
"} while (something( ));",
1199111991
Spaces);
11992+
11993+
verifyFormat("size_t idx = (size_t) (ptr - ((char *) file));", Spaces);
11994+
verifyFormat("size_t idx = (size_t) a;", Spaces);
11995+
verifyFormat("size_t idx = (size_t) (a - 1);", Spaces);
11996+
verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
11997+
verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
11998+
verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
11999+
Spaces.SpaceAfterCStyleCast = false;
12000+
verifyFormat("size_t idx = (size_t)(ptr - ((char *)file));", Spaces);
12001+
verifyFormat("size_t idx = (size_t)a;", Spaces);
12002+
verifyFormat("size_t idx = (size_t)(a - 1);", Spaces);
12003+
verifyFormat("size_t idx = (a->*foo)(a - 1);", Spaces);
12004+
verifyFormat("size_t idx = (a->foo)(a - 1);", Spaces);
12005+
verifyFormat("size_t idx = (*foo)(a - 1);", Spaces);
1199212006
}
1199312007

1199412008
TEST_F(FormatTest, ConfigurableSpacesInSquareBrackets) {

0 commit comments

Comments
 (0)