Open Quuxplusone opened 3 years ago
Attached .clang-format
(3454 bytes, text/plain): Style file
This site is blocked for me, please put a simplified use case and .clang-format file into the issue (as text) not attachment
Remove all options from .clang-format that don't impact the issue being present or not
With the following saved as clang-format-c-cast.c:
nt main(int argc, char* argv[])
{
size_t idx = (size_t) (ptr - ((char*) file));
idx = (size_t) (ptr - ((char*) file));
return 0;
}
Run the following command:
clang-format --style="{SpaceAfterCStyleCast: true }" clang-format-c-cast.c
Notice that the casts to size_t
lose their spaces and the casts to char*
do not.
Breaking it down to the simplest form
size_t idx = (size_t) a;
size_t idx = (size_t)(a - 1);
I believe this shows what you mean
In the format the ) of ") a" is seen as a CastRParen
M=0 C=0 T=Unknown S=1 F=0 B=0 BK=0 P=0 Name=identifier L=6 PPK=2 FakeLParens=2/ FakeRParens=0 II=0x1c05770 Text='size_t'
M=0 C=1 T=StartOfName S=1 F=0 B=0 BK=0 P=220 Name=identifier L=10 PPK=2 FakeLParens= FakeRParens=0 II=0x1c05790 Text='idx'
M=0 C=0 T=BinaryOperator S=1 F=0 B=0 BK=0 P=22 Name=equal L=12 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='='
M=0 C=1 T=Unknown S=1 F=0 B=0 BK=0 P=22 Name=l_paren L=14 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='('
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=59 Name=identifier L=20 PPK=2 FakeLParens= FakeRParens=0 II=0x1c05770 Text='size_t'
M=0 C=0 T=CastRParen S=0 F=0 B=0 BK=0 P=43 Name=r_paren L=21 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text=')'
M=0 C=1 T=Unknown S=1 F=0 B=0 BK=0 P=120 Name=identifier L=23 PPK=2 FakeLParens= FakeRParens=1 II=0x1c057b0 Text='a'
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=23 Name=semi L=24 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text=';'
In the later its not:
M=0 C=0 T=Unknown S=1 F=0 B=0 BK=0 P=0 Name=identifier L=6 PPK=2 FakeLParens=2/ FakeRParens=0 II=0x1c05770 Text='size_t'
M=0 C=1 T=StartOfName S=1 F=0 B=0 BK=0 P=220 Name=identifier L=10 PPK=2 FakeLParens= FakeRParens=0 II=0x1c05790 Text='idx'
M=0 C=0 T=BinaryOperator S=1 F=0 B=0 BK=0 P=22 Name=equal L=12 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='='
M=0 C=1 T=Unknown S=1 F=0 B=0 BK=0 P=22 Name=l_paren L=14 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='('
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=59 Name=identifier L=20 PPK=2 FakeLParens= FakeRParens=0 II=0x1c05770 Text='size_t'
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=43 Name=r_paren L=21 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text=')'
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=23 Name=l_paren L=22 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='('
M=0 C=1 T=Unknown S=0 F=0 B=0 BK=0 P=59 Name=identifier L=23 PPK=2 FakeLParens=13/ FakeRParens=0 II=0x1c057b0 Text='a'
M=0 C=0 T=BinaryOperator S=1 F=0 B=0 BK=0 P=53 Name=minus L=25 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='-'
M=0 C=1 T=Unknown S=1 F=0 B=0 BK=0 P=53 Name=numeric_constant L=27 PPK=2 FakeLParens= FakeRParens=1 II=0x0 Text='1'
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=43 Name=r_paren L=28 PPK=2 FakeLParens= FakeRParens=1 II=0x0 Text=')'
M=0 C=0 T=Unknown S=0 F=0 B=0 BK=0 P=23 Name=semi L=29 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text=';'
As with your other issue, its not an exact science how "CastRParen" get set or
not set.
If I naive set the following in rParenEndsCast()
if (Tok.Next->is(tok::l_paren))
return true;
So that whenever we see ")(" we treat the first as a CastRParen, then all the
unit test pass except those that are handling a function ptr
(a->*foo)(arguments)
which would not get formatted as
(a->*foo) (arguments)
Its possible something more like
+ if (Tok.Next->is(tok::l_paren) &&
+ !(Tok.Previous && Tok.Previous->is(tok::identifier) &&
+ Tok.Previous->Previous &&
+ Tok.Previous->Previous->isOneOf(tok::arrowstar, tok::arrow,
+ tok::star)))
+ return true;
+
Could actually help, resulting in
size_t idx = (size_t) a;
size_t idx = (size_t) (a - 1);
size_t idx = (a->*b)(a - 1);
size_t idx = (a->foo)(a - 1);
size_t idx = (*foo)(a - 1);
Sounds good. What do I need to do to help?
Tenative fix https://reviews.llvm.org/D93626
.clang-format
(3454 bytes, text/plain)