This is an archive of the discontinued LLVM Phabricator instance.

clang-format: Format std::function template parameter consistently inside function
ClosedPublic

Authored by jeanphilippeD on Oct 25 2015, 5:43 PM.

Details

Reviewers
djasper
Summary

The function type declared in an std::function template parameter is confused for a cast:

Currently:
Pass: "std::function< void(int, int) > fct;", Spaces);
Fail: "void inFunction() { std::function< void(int, int) > fct; }",
Actual result "void inFunction() { std::function< void(int, int)> fct; }" (no space between ")>")

Root cause:
-Inside a function definition, Line.MustBeDeclaration is not true.
-This allows the context IsExpression to be true.
"Contexts.back().IsExpression = !ParametersOfFunctionType && !IsForOrCatch;"
-which then allow the right parenthesis to be marked incorrectly as cast, and the left there after.
"if (ParensAreType && !ParensCouldEndDecl && !IsSizeOfOrAlignOf &&

  (Contexts.size() > 1 && Contexts[Contexts.size() - 2].IsExpression))
IsCast = true;"

Because at the time of this marking, the angle bracket is not yet established as a template opener and closer, this fix address the issue by resetting the type to unknown for the parenthesis.(Unknown is the type the parenthesis hold in the case the line must be a declaration).
It seems there should be a better alternative, but I am not sure where I should look.

The tests are updated in 2 places as this incorrect deduction result in incorrect spacing before the angle bracket, but also inside the parenthesis if space is required there but not in cast.

Run all the test in FormatTests project and spot checked clang format output for TokenAnnotator.cpp and FormatTest.cpp is the same before and after the patch.

Diff Detail

Event Timeline

jeanphilippeD retitled this revision from to clang-format: Format std::function template parameter consistently inside function.
jeanphilippeD updated this object.
jeanphilippeD added a reviewer: djasper.
jeanphilippeD added a subscriber: cfe-commits.
djasper edited edge metadata.Oct 25 2015, 11:36 PM

I'll take a look. I agree that there should be a better approach.

Also, you seem to have created the inverse patch :-).

djasper accepted this revision.Oct 26 2015, 5:11 AM
djasper edited edge metadata.

Submitted tests and alternative fix in r251284. Seems to me, there can never be a cast right before a ">".

This revision is now accepted and ready to land.Oct 26 2015, 5:11 AM
djasper closed this revision.Oct 26 2015, 5:11 AM

Thanks a lot.
Yes, it makes a lot more sense this way.