aboutsummaryrefslogtreecommitdiff
path: root/lib/Format/Format.cpp
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2013-02-05 10:07:47 +0000
committerDaniel Jasper <djasper@google.com>2013-02-05 10:07:47 +0000
commit63d7cedca8616921c1908c88c2f23fcd67bbab99 (patch)
tree76ca1fb0f7f86634712e6fc2d578438933ca8d96 /lib/Format/Format.cpp
parentd399bfff5a57325d8e63116c70aa727b1fb43232 (diff)
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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@174364 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'lib/Format/Format.cpp')
-rw-r--r--lib/Format/Format.cpp32
1 files changed, 31 insertions, 1 deletions
diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp
index 06ebc7808c..bbebec3652 100644
--- a/lib/Format/Format.cpp
+++ b/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)))