From 1ac3e05bbd99d43c4be88a5d10d5c884a2b7d2b4 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 5 Feb 2013 10:07:47 +0000 Subject: Initial support for formatting ObjC method declarations/calls. We can now format stuff like: - (void)doSomethingWith:(GTMFoo *)theFoo rect:(NSRect)theRect interval:(float)theInterval { [myObject doFooWith:arg1 // name:arg2 error:arg3]; } This seems to fix everything mentioned in llvm.org/PR14939. llvm-svn: 174364 --- clang/lib/Format/Format.cpp | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) (limited to 'clang/lib/Format/Format.cpp') diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index 06ebc7808cc..bbebec3652f 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -268,7 +268,7 @@ private: : Indent(Indent), LastSpace(LastSpace), AssignmentColumn(0), FirstLessLess(0), BreakBeforeClosingBrace(false), QuestionColumn(0), AvoidBinPacking(AvoidBinPacking), BreakAfterComma(false), - HasMultiParameterLine(HasMultiParameterLine) { + HasMultiParameterLine(HasMultiParameterLine), ColonPos(0) { } /// \brief The position to which a specific parenthesis level needs to be @@ -312,6 +312,9 @@ private: /// \brief This context already has a line with more than one parameter. bool HasMultiParameterLine; + /// \brief The position of the colon in an ObjC method declaration/call. + unsigned ColonPos; + bool operator<(const ParenState &Other) const { if (Indent != Other.Indent) return Indent < Other.Indent; @@ -331,6 +334,8 @@ private: return BreakAfterComma; if (HasMultiParameterLine != Other.HasMultiParameterLine) return HasMultiParameterLine; + if (ColonPos != Other.ColonPos) + return ColonPos < Other.ColonPos; return false; } }; @@ -427,6 +432,17 @@ private: } else if (Previous.Type == TT_BinaryOperator && State.Stack.back().AssignmentColumn != 0) { State.Column = State.Stack.back().AssignmentColumn; + } else if (Current.Type == TT_ObjCSelectorName) { + if (State.Stack.back().ColonPos > Current.FormatTok.TokenLength) { + State.Column = + State.Stack.back().ColonPos - Current.FormatTok.TokenLength; + } else { + State.Column = State.Stack.back().Indent; + State.Stack.back().ColonPos = + State.Column + Current.FormatTok.TokenLength; + } + } else if (Previous.Type == TT_ObjCMethodExpr) { + State.Column = State.Stack.back().Indent + 4; } else { State.Column = State.Stack[ParenLevel].Indent; } @@ -461,6 +477,17 @@ private: if (!DryRun) Whitespaces.replaceWhitespace(Current, 0, Spaces, State.Column, Style); + if (Current.Type == TT_ObjCSelectorName && + State.Stack.back().ColonPos == 0) { + if (State.Stack.back().Indent + Current.LongestObjCSelectorName > + State.Column + Spaces + Current.FormatTok.TokenLength) + State.Stack.back().ColonPos = + State.Stack.back().Indent + Current.LongestObjCSelectorName; + else + State.Stack.back().ColonPos = + State.Column + Spaces + Current.LongestObjCSelectorName; + } + // FIXME: Do we need to do this for assignments nested in other // expressions? if (RootToken.isNot(tok::kw_for) && ParenLevel == 0 && @@ -699,6 +726,9 @@ private: State.Stack.back().BreakAfterComma && !isTrailingComment(*State.NextToken)) return true; + if (State.NextToken->Type == TT_ObjCSelectorName && + State.Stack.back().ColonPos != 0) + return true; if ((State.NextToken->Type == TT_CtorInitializerColon || (State.NextToken->Parent->ClosesTemplateDeclaration && State.Stack.size() == 1))) -- cgit v1.2.3