aboutsummaryrefslogtreecommitdiff
path: root/lib/Format/Format.cpp
diff options
context:
space:
mode:
authorNico Weber <nicolasweber@gmx.de>2013-01-10 23:11:41 +0000
committerNico Weber <nicolasweber@gmx.de>2013-01-10 23:11:41 +0000
commitcd52bdaaf076b0082c07c6b3d88937fb737054f1 (patch)
treefca5b9845970173d5ca3678e4ff7517407ff14b3 /lib/Format/Format.cpp
parent664566c37f81d70226df22c12aa05d1603b620f3 (diff)
Formatter: Put spaces in ObjC method decls in the right place for Google style.
Objective-C method declarations look like this: - (returntype)name:(type)argname anothername:(type)arg2name; In google style, there's no space after the leading '-' but one after "(returntype)" instead (but none after the argument types), see http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml#Method_Declarations_and_Definitions Not inserting the space after '-' is easy, but to insert the space after the return type, the formatter needs to know that a closing parenthesis ends the return type. To do this, I tweaked the code in parse() to check for this, which in turn required moving detection of TT_ObjCMethodSpecifier from annotate() to parse(), because parse() runs before annotate(). (To keep things interesting, the return type is optional, but it's almost always there in practice.) http://llvm-reviews.chandlerc.com/D280 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@172140 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/Format.cpp')
-rw-r--r--lib/Format/Format.cpp27
1 files changed, 20 insertions, 7 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index f84d3c5bc7..87b996a782 100644
--- a/lib/Format/Format.cpp
+++ b/lib/Format/Format.cpp
@@ -39,6 +39,7 @@ enum TokenType {
TT_ObjCBlockLParen,
TT_ObjCDecl,
TT_ObjCMethodSpecifier,
+ TT_ObjCSelectorStart,
TT_ObjCProperty,
TT_OverloadedOperator,
TT_PointerOrReference,
@@ -107,6 +108,7 @@ FormatStyle getLLVMStyle() {
LLVMStyle.IndentCaseLabels = false;
LLVMStyle.SpacesBeforeTrailingComments = 1;
LLVMStyle.ObjCSpaceBeforeProtocolList = true;
+ LLVMStyle.ObjCSpaceBeforeReturnType = true;
return LLVMStyle;
}
@@ -120,6 +122,7 @@ FormatStyle getGoogleStyle() {
GoogleStyle.IndentCaseLabels = true;
GoogleStyle.SpacesBeforeTrailingComments = 2;
GoogleStyle.ObjCSpaceBeforeProtocolList = false;
+ GoogleStyle.ObjCSpaceBeforeReturnType = false;
return GoogleStyle;
}
@@ -680,14 +683,26 @@ public:
AnnotatedToken *Tok = CurrentToken;
next();
switch (Tok->FormatTok.Tok.getKind()) {
- case tok::l_paren:
+ case tok::plus:
+ case tok::minus:
+ // At the start of the line, +/- specific ObjectiveC method
+ // declarations.
+ if (Tok->Parent == NULL)
+ Tok->Type = TT_ObjCMethodSpecifier;
+ break;
+ case tok::l_paren: {
+ bool ParensWereObjCReturnType =
+ Tok->Parent && Tok->Parent->Type == TT_ObjCMethodSpecifier;
if (!parseParens())
return false;
if (CurrentToken != NULL && CurrentToken->is(tok::colon)) {
CurrentToken->Type = TT_CtorInitializerColon;
next();
+ } else if (CurrentToken != NULL && ParensWereObjCReturnType) {
+ CurrentToken->Type = TT_ObjCSelectorStart;
+ next();
}
- break;
+ } break;
case tok::l_square:
if (!parseSquare())
return false;
@@ -948,10 +963,6 @@ private:
}
TokenType determinePlusMinusCaretUsage(const AnnotatedToken &Tok) {
- // At the start of the line, +/- specific ObjectiveC method declarations.
- if (Tok.Parent == NULL)
- return TT_ObjCMethodSpecifier;
-
// Use heuristics to recognize unary operators.
if (Tok.Parent->is(tok::equal) || Tok.Parent->is(tok::l_paren) ||
Tok.Parent->is(tok::comma) || Tok.Parent->is(tok::l_square) ||
@@ -1044,7 +1055,9 @@ private:
if (Tok.is(tok::colon))
return false;
if (Tok.Parent->Type == TT_ObjCMethodSpecifier)
- return true;
+ return Style.ObjCSpaceBeforeReturnType || Tok.isNot(tok::l_paren);
+ if (Tok.Type == TT_ObjCSelectorStart)
+ return !Style.ObjCSpaceBeforeReturnType;
if (Tok.Parent->is(tok::r_paren) && Tok.is(tok::identifier))
// Don't space between ')' and <id>
return false;