This is an archive of the discontinued LLVM Phabricator instance.

ClangFormat - When adding a comment at the end of a return type declaration, the function name is incorrectly indented
ClosedPublic

Authored by Abpostelnicu on Mar 6 2017, 5:35 AM.

Details

Summary

This addresses the problem that when having a comment at the end of a return type declaration the function name is incorrectly indented.
The bellow example, having this input:

template<typename T> // Templates on own line.
static int           // Return type on own line for top-level functions.
  MyFunction(int a)

The result would be:

template<typename T> // Templates on own line.
static int           // Return type on own line for top-level functions.
  MyFunction(int a)

The above result is wrong the correct one is:

template<typename T> // Templates on own line.
static int           // Return type on own line for top-level functions.
MyFunction(int a)

This was happening because in function isStartOfName the possibility oh having a comment before the actual name was not considered thus the comment was not ignored, like the const keyword is.

Test case will follow.

Diff Detail

Repository
rL LLVM

Event Timeline

Abpostelnicu created this revision.Mar 6 2017, 5:35 AM
Abpostelnicu edited the summary of this revision. (Show Details)Mar 6 2017, 5:39 AM
Abpostelnicu edited the summary of this revision. (Show Details)
Abpostelnicu added a subscriber: sylvestre.ledru.
Abpostelnicu edited the summary of this revision. (Show Details)Mar 6 2017, 6:13 AM

test attached

Abpostelnicu added a project: Restricted Project.Mar 6 2017, 6:24 AM
djasper edited edge metadata.Mar 6 2017, 8:07 AM

Thanks for catching and fixing this.
Generally, please upload diffs with the full file as context. That way, phabricator allows expanding the code around your changes.

lib/Format/TokenAnnotator.cpp
1170 ↗(On Diff #90688)

Just do:

FormatToken *PreviousNotConst = Tok.getPreviousNonComment();
while (PreviousNotConst && PreviousNotConst->is(tok::kw_const))
  PreviousNotConst = PreviousNotConst.getPreviousNonComment();

No Other changes should be necessary.

unittests/Format/FormatTest.cpp
8342 ↗(On Diff #90688)

Please make the comment shorter so it doesn't need to be wrapped.

Also, put this into the test function where the rest of this functionality is covered, probably BreaksLongDeclarations.

Thanks for catching and fixing this.
Generally, please upload diffs with the full file as context. That way, phabricator allows expanding the code around your changes.

lib/Format/TokenAnnotator.cpp
1170 ↗(On Diff #90688)

You are right but I've specially wanted to make things clearer :).

djasper added inline comments.Mar 6 2017, 8:26 AM
lib/Format/TokenAnnotator.cpp
1170 ↗(On Diff #90688)

I see, I think it doesn't help much. I think comments should almost always be ignored and we shouldn't even need to make this explicit in the code. I have even thought ways to refactor the code to completely ignore them by default and only look at them in the few very specific cases where we do care.

Abpostelnicu added inline comments.Mar 6 2017, 8:36 AM
lib/Format/TokenAnnotator.cpp
1170 ↗(On Diff #90688)

Ok I see your point there. I'll re update the patch.

Abpostelnicu marked 5 inline comments as done.
djasper accepted this revision.Mar 7 2017, 6:36 AM

Looks good, thank you!

This revision is now accepted and ready to land.Mar 7 2017, 6:36 AM
This revision was automatically updated to reflect the committed changes.
krasimir added a subscriber: krasimir.EditedMar 7 2017, 7:36 AM

Thank you for the fix!

Pushed a new change since the test case was broken, and broke the build.