This is an archive of the discontinued LLVM Phabricator instance.

Fix spacing for function with ref-qualification when using SpacesInCStyleCastParentheses != SpacesInParentheses
ClosedPublic

Authored by jeanphilippeD on Feb 21 2015, 6:15 PM.

Details

Reviewers
djasper
Summary

If the approach is not right, I'll try to rework the fix with your feedback.

Problem:

FormatStyle Spaces = getLLVMStyle();
verifyFormat("Deleted &operator=(const Deleted &)& = default;");

Spaces.SpacesInParentheses= true;
verifyFormat("Deleted( const Deleted & )& = default;", Spaces); // Fail "Deleted(const Deleted &)& = default;"

Spaces.SpacesInCStyleCastParentheses = true;
Spaces.SpacesInParentheses= false;
verifyFormat("Deleted(const Deleted &)& = default;", Spaces); // Fail "Deleted( const Deleted & )& = default;"

Solution:

The cast seemed too eager: There does not seem to be a reason for a binary operator to follow a C-cast.

The type of the function reference qualification seems to make sense as TT_PointerOrReference:
-Extend in the case of to handle the '&&' case "Deleted(const Deleted &)&& = default;"
-Extend to handle the case without '=' for '*', '&' and '&&' case "Deleted(const Deleted &)&;"

Ensure no spaces between parenthesis and the reference qualification:
"Deleted &operator=(const Deleted &)&;" can be detected with TT_OverloadedOperatorLParen
"SomeType MemberFunction(const Deleted &)&;" can be detected with TT_FunctionDeclarationName

"Deleted(const Deleted &)&;" does not exist as ref-qualification does not make sense for constructors

Tests:

The set of unit tests is not quite minimal, it needs:

  • To cover LLVM, Google, SpacesInCStyleCastParentheses, SpacesInParentheses.
  • Both ref-qualifications : '&' and '&&'
  • Operator, function, and constructor.

Additional test:
Verify clang-format output is unchanged for the 3 modified files FormatToken.h, TokenAnnotator.cpp, FormatTest.cpp

Diff Detail

Event Timeline

jeanphilippeD retitled this revision from to Fix spacing for function with ref-qualification when using SpacesInCStyleCastParentheses != SpacesInParentheses.
jeanphilippeD updated this object.
jeanphilippeD edited the test plan for this revision. (Show Details)
jeanphilippeD added a subscriber: Unknown Object (MLST).
djasper added inline comments.Feb 23 2015, 4:29 AM
lib/Format/FormatToken.h
46 ↗(On Diff #20467)

I am not convinced that we actually need a separate token type to fix this.

lib/Format/TokenAnnotator.cpp
463

I think, this is wrong. "<identifier>(" can be different things.

769–771

Which of the new tests does this affect?

805

I think a */& before a semicolon is always a pointer/reference, no?

There is a test in determineStarAmpUsage:

if (NextToken->isOneOf(tok::kw_operator, tok::comma))

I think we can just add semi to that list instead of this change.

975

I think this must have been there for a reason. What broke when you just removed this?

1665–1673

In what cases do we want a space between ")" and a pointer/references? The case where it is a TT_CastRParen is already handled at this point. So my question is, what happens if you remove everything of what you have added except "Left.is(tok::r_paren)"?

jeanphilippeD updated this object.

Update as per review comment

Do not handle constructors anymore since there is no ref-qualification for constructors:
Remove TT_FunctionLParen since it is no longer needed.

Move the handling of '&;' to determineStarAmpUsage as per comment.

Kept restriction to operator overload and function to attach pointer reference to right parenthesis, rewrite without TT_FunctionLParen.

Thank you very much for the feedback.
I already published an updated patch.
I reply inline to the questions you raised during the review.

lib/Format/TokenAnnotator.cpp
769–771

The ones with the && at the end of the operator:

[ RUN      ] FormatTest.UnderstandsFunctionRefQualification
Actual: "Deleted &operator=(const Deleted &) && = delete;"
Expected: "Deleted &operator=(const Deleted &)&& = delete;"
Actual: "SomeType MemberFunction(const Deleted &) && = delete;"
Expected: "SomeType MemberFunction(const Deleted &)&& = delete;"
[  FAILED  ] FormatTest.UnderstandsFunctionRefQualification (140 ms)
975

Removing this from master just break these tests:

[ RUN      ] FormatTest.UnderstandsOverloadedOperators
Actual: "Deleted &operator=(const Deleted &) & = default;"
Expected: "Deleted &operator=(const Deleted &)& = default;"
Actual: "Deleted &operator=(const Deleted &) && = delete;"
Expected:"Deleted &operator=(const Deleted &)&& = delete;"
Actual: "Deleted& operator=(const Deleted&) && = delete;"
Expected:"Deleted& operator=(const Deleted&)&& = delete;"
[  FAILED  ] FormatTest.UnderstandsOverloadedOperators (234 ms)
[----------] Global test environment tear-down
[==========] 315 tests from 4 test cases ran. (47166 ms total)
[  PASSED  ] 314 tests.
[  FAILED  ] 1 test, listed below:
[  FAILED  ] FormatTest.UnderstandsOverloadedOperators
1665–1673

Ran with:

if (Right.is(TT_PointerOrReference))
  return !Left.is(tok::r_paren) &&
         (Left.Tok.isLiteral() ||
          (!Left.isOneOf(TT_PointerOrReference, tok::l_paren) &&
           Style.PointerAlignment != FormatStyle::PAS_Left));

[ RUN      ] FormatTest.UnderstandsUsesOfStarAndAmp
Actual: "typedef typeof(int(int, int))*MyFunc;"
Expected: "typedef typeof(int(int, int)) *MyFunc;"
Actual: "[](const decltype(*a)&value) {}"
Expected: "[](const decltype(*a) &value) {}"
[  FAILED  ] FormatTest.UnderstandsUsesOfStarAndAmp (1451 ms)
djasper accepted this revision.Feb 23 2015, 2:33 PM
djasper edited edge metadata.

Looks good. Thanks for working on this. Do you have commit access?

This revision is now accepted and ready to land.Feb 23 2015, 2:33 PM

Thank you.
No I do not have commit access.
I'd prefer if you proceed with the commits as I'm still getting familiar
with the process.

2015-02-23 22:33 GMT+00:00 Daniel Jasper <djasper@google.com>:

Looks good. Thanks for working on this. Do you have commit access?

http://reviews.llvm.org/D7813

EMAIL PREFERENCES

http://reviews.llvm.org/settings/panel/emailpreferences/
djasper closed this revision.Feb 25 2015, 2:32 AM

Submitted as r230473.