llvm / llvm-project

The LLVM Project is a collection of modular and reusable compiler and toolchain technologies.
http://llvm.org
Other
27.99k stars 11.55k forks source link

clang-format inserts a space after '->' for operator->() overloading #41762

Closed cb252c30-9e88-4eb4-891d-0e28e2d53015 closed 4 years ago

cb252c30-9e88-4eb4-891d-0e28e2d53015 commented 5 years ago
Bugzilla Link 42417
Resolution FIXED
Resolved on Oct 04, 2019 10:41
Version unspecified
OS Windows NT
CC @mydeveloperday
Fixed by commit(s) r373746

Extended Description

clang-format formats like:

class Bug { auto operator-> () -> int*; auto operator++(int) -> int; };

You can notice that a space inserted after 'operator->' while no space is inserted after 'operator++'.

mydeveloperday commented 4 years ago

Landed in https://reviews.llvm.org/rGa37a6dcd04b0c0a0f9eea93ba0a5070a5421a29a

mydeveloperday commented 4 years ago

Revision submitted https://reviews.llvm.org/D68242

Note: clang-format needs people to do code reviews, please consider reviewing others work as its the only way we can get fixes into the trunk.

mydeveloperday commented 4 years ago

From the debug output, the TokenAnnotator was marking the first -> as being a Trailing return arrow because of the presence of the auto.

AnnotatedTokens(L=1): M=0 C=0 T=Unknown S=1 B=0 BK=0 P=0 Name=auto L=4 PPK=2 FakeLParens=0/ FakeRParens=0 II=0x174ae7de0a8 Text='auto' M=0 C=1 T=Unknown S=1 B=0 BK=0 P=80 Name=operator L=13 PPK=2 FakeLParens= FakeRParens=0 II=0x174ae7dec58 Text='operator' M=0 C=0 T=TrailingReturnArrow S=0 B=0 BK=0 P=23 Name=arrow L=15 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='->' ^^^^^^^^^^^^^^^^^^^^^^ M=0 C=0 T=OverloadedOperatorLParen S=1 B=0 BK=0 P=23 Name=l_paren L=17 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='(' M=0 C=0 T=Unknown S=0 B=0 BK=0 P=140 Name=r_paren L=18 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text=')' M=0 C=1 T=TrailingReturnArrow S=1 B=0 BK=0 P=23 Name=arrow L=21 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text='->' M=0 C=0 T=Unknown S=1 B=0 BK=0 P=23 Name=int L=25 PPK=2 FakeLParens= FakeRParens=0 II=0x174ae7de3e0 Text='int' M=0 C=1 T=PointerOrReference S=1 B=0 BK=0 P=210 Name=star L=27 PPK=2 FakeLParens= FakeRParens=1 II=0x0 Text='*' M=0 C=0 T=Unknown S=0 B=0 BK=0 P=34 Name=semi L=28 PPK=2 FakeLParens= FakeRParens=0 II=0x0 Text=';'

I believe this is fairly easy to fix, let me prepare the revision and unit tests and send for review.

--- a/clang/lib/Format/TokenAnnotator.cpp +++ b/clang/lib/Format/TokenAnnotator.cpp @@ -1387,7 +1387,9 @@ private: Style.Language == FormatStyle::LK_Java) { Current.Type = TT_LambdaArrow; } else if (Current.is(tok::arrow) && AutoFound && Line.MustBeDeclaration &&