r176352 - Normal indent for last element of builder-type call.
Daniel Jasper
djasper at google.com
Fri Mar 1 08:48:33 PST 2013
Author: djasper
Date: Fri Mar 1 10:48:32 2013
New Revision: 176352
URL: http://llvm.org/viewvc/llvm-project?rev=176352&view=rev
Log:
Normal indent for last element of builder-type call.
In builder type call, we indent to the laster function calls.
However, for the last element of such a call, we don't need to do
so, as that normally just wastes space and does not increase
readability.
Before:
aaaaaa->aaaaaa->aaaaaa( // break
aaaaaa);
aaaaaaaaaaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaa
->aaaaaaaaaaaaaaaaaaaaaaaaaa(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);
After:
aaaaaa->aaaaaa->aaaaaa( // break
aaaaaa);
aaaaaaaaaaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaa->aaaaaaaaaaaaaaaaaaaaaaaaaa(
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa);
Modified:
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/lib/Format/TokenAnnotator.cpp
cfe/trunk/lib/Format/TokenAnnotator.h
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=176352&r1=176351&r2=176352&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Fri Mar 1 10:48:32 2013
@@ -339,7 +339,8 @@ private:
: Indent(Indent), LastSpace(LastSpace), FirstLessLess(0),
BreakBeforeClosingBrace(false), QuestionColumn(0),
AvoidBinPacking(AvoidBinPacking), BreakBeforeParameter(false),
- HasMultiParameterLine(HasMultiParameterLine), ColonPos(0) {}
+ HasMultiParameterLine(HasMultiParameterLine), ColonPos(0),
+ StartOfFunctionCall(0) {}
/// \brief The position to which a specific parenthesis level needs to be
/// indented.
@@ -382,6 +383,9 @@ private:
/// \brief The position of the colon in an ObjC method declaration/call.
unsigned ColonPos;
+ /// \brief The start of the most recent function in a builder-type call.
+ unsigned StartOfFunctionCall;
+
bool operator<(const ParenState &Other) const {
if (Indent != Other.Indent)
return Indent < Other.Indent;
@@ -401,6 +405,8 @@ private:
return HasMultiParameterLine;
if (ColonPos != Other.ColonPos)
return ColonPos < Other.ColonPos;
+ if (StartOfFunctionCall != Other.StartOfFunctionCall)
+ return StartOfFunctionCall < Other.StartOfFunctionCall;
return false;
}
};
@@ -622,9 +628,6 @@ private:
// If this function has multiple parameters, indent nested calls from
// the start of the first parameter.
State.Stack.back().LastSpace = State.Column;
- else if ((Current.is(tok::period) || Current.is(tok::arrow)) &&
- Line.Type == LT_BuilderTypeCall && State.ParenLevel == 0)
- State.Stack.back().LastSpace = State.Column;
}
return moveStateToNextToken(State, DryRun);
@@ -642,6 +645,10 @@ private:
State.Stack.back().FirstLessLess = State.Column;
if (Current.is(tok::question))
State.Stack.back().QuestionColumn = State.Column;
+ if ((Current.is(tok::period) || Current.is(tok::arrow)) &&
+ Line.Type == LT_BuilderTypeCall && State.ParenLevel == 0)
+ State.Stack.back().StartOfFunctionCall =
+ Current.LastInChainOfCalls ? 0 : State.Column;
if (Current.Type == TT_CtorInitializerColon) {
if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
State.Stack.back().AvoidBinPacking = true;
@@ -667,7 +674,8 @@ private:
NewIndent = 2 + State.Stack.back().LastSpace;
AvoidBinPacking = false;
} else {
- NewIndent = 4 + State.Stack.back().LastSpace;
+ NewIndent = 4 + std::max(State.Stack.back().LastSpace,
+ State.Stack.back().StartOfFunctionCall);
AvoidBinPacking =
!Style.BinPackParameters || State.Stack.back().AvoidBinPacking;
}
Modified: cfe/trunk/lib/Format/TokenAnnotator.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.cpp?rev=176352&r1=176351&r2=176352&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.cpp (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.cpp Fri Mar 1 10:48:32 2013
@@ -464,6 +464,7 @@ private:
public:
LineType parseLine() {
int PeriodsAndArrows = 0;
+ AnnotatedToken *LastPeriodOrArrow = NULL;
bool CanBeBuilderTypeStmt = true;
if (CurrentToken->is(tok::hash)) {
parsePreprocessorDirective();
@@ -472,8 +473,10 @@ public:
while (CurrentToken != NULL) {
if (CurrentToken->is(tok::kw_virtual))
KeywordVirtualFound = true;
- if (CurrentToken->is(tok::period) || CurrentToken->is(tok::arrow))
+ if (CurrentToken->is(tok::period) || CurrentToken->is(tok::arrow)) {
++PeriodsAndArrows;
+ LastPeriodOrArrow = CurrentToken;
+ }
AnnotatedToken *TheToken = CurrentToken;
if (!consumeToken())
return LT_Invalid;
@@ -485,8 +488,10 @@ public:
return LT_VirtualFunctionDecl;
// Assume a builder-type call if there are 2 or more "." and "->".
- if (PeriodsAndArrows >= 2 && CanBeBuilderTypeStmt)
+ if (PeriodsAndArrows >= 2 && CanBeBuilderTypeStmt) {
+ LastPeriodOrArrow->LastInChainOfCalls = true;
return LT_BuilderTypeCall;
+ }
if (Line.First.Type == TT_ObjCMethodSpecifier) {
if (Contexts.back().FirstObjCSelectorName != NULL)
@@ -934,7 +939,7 @@ unsigned TokenAnnotator::splitPenalty(co
if (Level != prec::Unknown)
return Level;
-
+
return 3;
}
Modified: cfe/trunk/lib/Format/TokenAnnotator.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/TokenAnnotator.h?rev=176352&r1=176351&r2=176352&view=diff
==============================================================================
--- cfe/trunk/lib/Format/TokenAnnotator.h (original)
+++ cfe/trunk/lib/Format/TokenAnnotator.h Fri Mar 1 10:48:32 2013
@@ -75,7 +75,7 @@ public:
ClosesTemplateDeclaration(false), MatchingParen(NULL),
ParameterCount(0), BindingStrength(0), SplitPenalty(0),
LongestObjCSelectorName(0), Parent(NULL), FakeLParens(0),
- FakeRParens(0) {
+ FakeRParens(0), LastInChainOfCalls(false) {
}
bool is(tok::TokenKind Kind) const { return FormatTok.Tok.is(Kind); }
@@ -127,6 +127,9 @@ public:
/// \brief Insert this many fake ) after this token for correct indentation.
unsigned FakeRParens;
+ /// \brief Is this the last "." or "->" in a builder-type call?
+ bool LastInChainOfCalls;
+
const AnnotatedToken *getPreviousNoneComment() const {
AnnotatedToken *Tok = Parent;
while (Tok != NULL && Tok->is(tok::comment))
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=176352&r1=176351&r2=176352&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Fri Mar 1 10:48:32 2013
@@ -1331,7 +1331,7 @@ TEST_F(FormatTest, FormatsBuilderPattern
" ->aaaaaaaa(aaaaaaaaaaaaaaa);");
verifyFormat(
"aaaaaaaaaaaaaaaaaaa()->aaaaaa(bbbbb)->aaaaaaaaaaaaaaaaaaa( // break\n"
- " aaaaaaaaaaaaaa);");
+ " aaaaaaaaaaaaaa);");
verifyFormat(
"aaaaaaaaaaaaaaaaaaaaaaa *aaaaaaaaa = aaaaaa->aaaaaaaaaaaa()\n"
" ->aaaaaaaaaaaaaaaa(\n"
More information about the cfe-commits
mailing list