[PATCH] clang-format: Builder closing paren must be a call
Adam Strzelecki
ono at java.pl
Mon May 4 05:34:34 PDT 2015
Hi djasper,
We should somehow ensure that builder type call segment is preceded by a
closing parenthesis that comes from a call not a type cast or anything else.
Before:
(aaaa)
.aaaaaaaaaaaaaaa()
.aaaaaaaaaaaaaaa()
.aaaaaaaaaaaaaaa()
.aaaaaaaaaaaaaaa()
.aaaaaaaaaaaaaaa();
After:
(aaaa).aaaaaaaaaaaaaaa()
.aaaaaaaaaaaaaaa()
.aaaaaaaaaaaaaaa()
.aaaaaaaaaaaaaaa()
.aaaaaaaaaaaaaaa();
Some more real life example:
Before:
- (bool)isALabelWithText:(NSString *)anyCaseText {
return [((UILabel *)self)
.someLabelText isEqualToString:[anyCaseText toVeryUpperCase]];
}
After:
- (bool)isALabelWithText:(NSString *)anyCaseText {
return [((UILabel *)self).someLabelText
isEqualToString:[anyCaseText toVeryUpperCase]];
}
http://reviews.llvm.org/D9469
Files:
lib/Format/ContinuationIndenter.cpp
unittests/Format/FormatTest.cpp
Index: lib/Format/ContinuationIndenter.cpp
===================================================================
--- lib/Format/ContinuationIndenter.cpp
+++ lib/Format/ContinuationIndenter.cpp
@@ -41,7 +41,28 @@
// Returns \c true if \c Tok is the "." or "->" of a call and starts the next
// segment of a builder type call.
static bool startsSegmentOfBuilderTypeCall(const FormatToken &Tok) {
- return Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope();
+ if (Tok.isMemberAccess() && Tok.Previous && Tok.Previous->closesScope()) {
+ // Ensure that closing scope was function or method call, instantiation
+ // or indexed access, not eg. a type cast or Objective-C call.
+ const FormatToken *LeftParen = Tok.Previous->MatchingParen;
+ if (!LeftParen)
+ return false;
+ // We expect previous token is an identifier ...
+ const FormatToken *ExpectedIdentifier = LeftParen->getPreviousNonComment();
+ if (!ExpectedIdentifier)
+ return false;
+ // or template closing ...
+ if (ExpectedIdentifier->is(TT_TemplateCloser)) {
+ const FormatToken *TemplateOpener = ExpectedIdentifier->MatchingParen;
+ if (!TemplateOpener)
+ return false;
+ const FormatToken *ExpectedIdentifier = TemplateOpener->getPreviousNonComment();
+ // ... of template preceeded by identifier.
+ return ExpectedIdentifier && ExpectedIdentifier->is(tok::identifier);
+ }
+ return ExpectedIdentifier->is(tok::identifier);
+ }
+ return false;
}
// Returns \c true if \c Current starts a new parameter.
Index: unittests/Format/FormatTest.cpp
===================================================================
--- unittests/Format/FormatTest.cpp
+++ unittests/Format/FormatTest.cpp
@@ -4167,6 +4167,21 @@
verifyFormat("aaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaa()\n"
" ->aaaaaaaaaaaaaae(0)\n"
" ->aaaaaaaaaaaaaaa();");
+ verifyFormat("(aaaa).aaaaaaaaaaaaaaa()\n"
+ " .aaaaaaaaaaaaaaa()\n"
+ " .aaaaaaaaaaaaaaa()\n"
+ " .aaaaaaaaaaaaaaa()\n"
+ " .aaaaaaaaaaaaaaa();");
+ verifyFormat("aaaa()\n"
+ " .aaaaaaaaaaaaaaa()\n"
+ " .aaaaaaaaaaaaaaa()\n"
+ " .aaaaaaaaaaaaaaa()\n"
+ " .aaaaaaaaaaaaaaa()\n"
+ " .aaaaaaaaaaaaaaa();");
+ verifyFormat("[aaaaa aaaaaaaaaaaaaaa].aaaaaaaaaaaaaaa()\n"
+ " .aaaaaaaaaaaaaaa()\n"
+ " .aaaaaaaaaaaaaaa()\n"
+ " .aaaaaaaaaaaaaaa();");
// Don't linewrap after very short segments.
verifyFormat("a().aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa()\n"
EMAIL PREFERENCES
http://reviews.llvm.org/settings/panel/emailpreferences/
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D9469.24876.patch
Type: text/x-patch
Size: 2684 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20150504/b06912e4/attachment.bin>
More information about the cfe-commits
mailing list