summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDaniel Jasper <djasper@google.com>2013-10-18 10:38:14 +0000
committerDaniel Jasper <djasper@google.com>2013-10-18 10:38:14 +0000
commit6633ab8ad95b9cb8ecfbfb9249cc22462fddec5c (patch)
treeabbf7ea325da35c8edd7d59a952a69ad126e27e7
parent314e58fdcca09287a1b85076210894ce91de061b (diff)
downloadbcm5719-llvm-6633ab8ad95b9cb8ecfbfb9249cc22462fddec5c.tar.gz
bcm5719-llvm-6633ab8ad95b9cb8ecfbfb9249cc22462fddec5c.zip
clang-format: Make continuation indent width configurable.
Patch by Kim Gräsman. Thank you! llvm-svn: 192964
-rw-r--r--clang/include/clang/Format/Format.h6
-rw-r--r--clang/lib/Format/ContinuationIndenter.cpp25
-rw-r--r--clang/lib/Format/Format.cpp3
-rw-r--r--clang/unittests/Format/FormatTest.cpp19
4 files changed, 41 insertions, 12 deletions
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h
index 899087a1473..6745082546e 100644
--- a/clang/include/clang/Format/Format.h
+++ b/clang/include/clang/Format/Format.h
@@ -235,6 +235,9 @@ struct FormatStyle {
/// \brief If \c false, spaces will be removed before assignment operators.
bool SpaceBeforeAssignmentOperators;
+ /// \brief Indent width for line continuations.
+ unsigned ContinuationIndentWidth;
+
bool operator==(const FormatStyle &R) const {
return AccessModifierOffset == R.AccessModifierOffset &&
ConstructorInitializerIndentWidth ==
@@ -282,7 +285,8 @@ struct FormatStyle {
SpacesInCStyleCastParentheses == R.SpacesInCStyleCastParentheses &&
SpaceAfterControlStatementKeyword ==
R.SpaceAfterControlStatementKeyword &&
- SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators;
+ SpaceBeforeAssignmentOperators == R.SpaceBeforeAssignmentOperators &&
+ ContinuationIndentWidth == R.ContinuationIndentWidth;
}
};
diff --git a/clang/lib/Format/ContinuationIndenter.cpp b/clang/lib/Format/ContinuationIndenter.cpp
index 7632058199b..bbca06e6dfb 100644
--- a/clang/lib/Format/ContinuationIndenter.cpp
+++ b/clang/lib/Format/ContinuationIndenter.cpp
@@ -265,7 +265,7 @@ void ContinuationIndenter::addTokenOnCurrentLine(LineState &State, bool DryRun,
State.Column += Spaces;
if (Current.is(tok::l_paren) && Previous.isOneOf(tok::kw_if, tok::kw_for))
// Treat the condition inside an if as if it was a second function
- // parameter, i.e. let nested calls have an indent of 4.
+ // parameter, i.e. let nested calls have a continuation indent.
State.Stack.back().LastSpace = State.Column + 1; // 1 is length of "(".
else if (Previous.is(tok::comma))
State.Stack.back().LastSpace = State.Column;
@@ -303,9 +303,10 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
bool DryRun) {
FormatToken &Current = *State.NextToken;
const FormatToken &Previous = *State.NextToken->Previous;
- // If we are continuing an expression, we want to indent an extra 4 spaces.
+ // If we are continuing an expression, we want to use the continuation indent.
unsigned ContinuationIndent =
- std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) + 4;
+ std::max(State.Stack.back().LastSpace, State.Stack.back().Indent) +
+ Style.ContinuationIndentWidth;
// Extra penalty that needs to be added because of the way certain line
// breaks are chosen.
unsigned Penalty = 0;
@@ -384,10 +385,10 @@ unsigned ContinuationIndenter::addTokenOnNewLine(LineState &State,
State.Column = State.Stack.back().Indent;
} else {
State.Column = State.Stack.back().Indent;
- // Ensure that we fall back to indenting 4 spaces instead of just
+ // Ensure that we fall back to the continuation indent width instead of just
// flushing continuations left.
if (State.Column == State.FirstIndent)
- State.Column += 4;
+ State.Column += Style.ContinuationIndentWidth;
}
if (Current.is(tok::question))
@@ -478,9 +479,10 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
}
// In ObjC method declaration we align on the ":" of parameters, but we need
- // to ensure that we indent parameters on subsequent lines by at least 4.
+ // to ensure that we indent parameters on subsequent lines by at least our
+ // continuation indent width.
if (Current.Type == TT_ObjCMethodSpecifier)
- State.Stack.back().Indent += 4;
+ State.Stack.back().Indent += Style.ContinuationIndentWidth;
// Insert scopes created by fake parenthesis.
const FormatToken *Previous = Current.getPreviousNonComment();
@@ -521,7 +523,7 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
if (*I == prec::Conditional ||
(!SkipFirstExtraIndent && *I > prec::Assignment &&
!Style.BreakBeforeBinaryOperators))
- NewParenState.Indent += 4;
+ NewParenState.Indent += Style.ContinuationIndentWidth;
if (Previous && !Previous->opensScope())
NewParenState.BreakBeforeParameter = false;
State.Stack.push_back(NewParenState);
@@ -558,7 +560,7 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
} else {
NewIndent = State.Stack.back().LastSpace;
if (Style.Cpp11BracedListStyle)
- NewIndent += 4;
+ NewIndent += Style.ContinuationIndentWidth;
else {
NewIndent += Style.IndentWidth;
++NewIndentLevel;
@@ -569,8 +571,9 @@ unsigned ContinuationIndenter::moveStateToNextToken(LineState &State,
(NextNoComment &&
NextNoComment->Type == TT_DesignatedInitializerPeriod);
} else {
- NewIndent = 4 + std::max(State.Stack.back().LastSpace,
- State.Stack.back().StartOfFunctionCall);
+ NewIndent = Style.ContinuationIndentWidth +
+ std::max(State.Stack.back().LastSpace,
+ State.Stack.back().StartOfFunctionCall);
AvoidBinPacking = !Style.BinPackParameters ||
(Style.ExperimentalAutoDetectBinPacking &&
(Current.PackingKind == PPK_OnePerLine ||
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp
index ff857676272..3496712e3cf 100644
--- a/clang/lib/Format/Format.cpp
+++ b/clang/lib/Format/Format.cpp
@@ -163,6 +163,7 @@ template <> struct MappingTraits<clang::format::FormatStyle> {
Style.SpaceAfterControlStatementKeyword);
IO.mapOptional("SpaceBeforeAssignmentOperators",
Style.SpaceBeforeAssignmentOperators);
+ IO.mapOptional("ContinuationIndentWidth", Style.ContinuationIndentWidth);
}
};
}
@@ -214,6 +215,7 @@ FormatStyle getLLVMStyle() {
LLVMStyle.SpacesInCStyleCastParentheses = false;
LLVMStyle.SpaceAfterControlStatementKeyword = true;
LLVMStyle.SpaceBeforeAssignmentOperators = true;
+ LLVMStyle.ContinuationIndentWidth = 4;
setDefaultPenalties(LLVMStyle);
LLVMStyle.PenaltyReturnTypeOnItsOwnLine = 60;
@@ -257,6 +259,7 @@ FormatStyle getGoogleStyle() {
GoogleStyle.SpacesInCStyleCastParentheses = false;
GoogleStyle.SpaceAfterControlStatementKeyword = true;
GoogleStyle.SpaceBeforeAssignmentOperators = true;
+ GoogleStyle.ContinuationIndentWidth = 4;
setDefaultPenalties(GoogleStyle);
GoogleStyle.PenaltyReturnTypeOnItsOwnLine = 200;
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp
index 5a8b5d6f52b..1e898d8f721 100644
--- a/clang/unittests/Format/FormatTest.cpp
+++ b/clang/unittests/Format/FormatTest.cpp
@@ -6495,6 +6495,7 @@ TEST_F(FormatTest, ParsesConfiguration) {
CHECK_PARSE("SpacesBeforeTrailingComments: 1234",
SpacesBeforeTrailingComments, 1234u);
CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u);
+ CHECK_PARSE("ContinuationIndentWidth: 11", ContinuationIndentWidth, 11u);
Style.Standard = FormatStyle::LS_Auto;
CHECK_PARSE("Standard: Cpp03", Standard, FormatStyle::LS_Cpp03);
@@ -6876,5 +6877,23 @@ TEST_F(FormatTest, MunchSemicolonAfterBlocks) {
"};");
}
+TEST_F(FormatTest, ConfigurableContinuationIndentWidth) {
+ FormatStyle TwoIndent = getLLVMStyleWithColumns(15);
+ TwoIndent.ContinuationIndentWidth = 2;
+
+ EXPECT_EQ("int i =\n"
+ " longFunction(\n"
+ " arg);",
+ format("int i = longFunction(arg);", TwoIndent));
+
+ FormatStyle SixIndent = getLLVMStyleWithColumns(20);
+ SixIndent.ContinuationIndentWidth = 6;
+
+ EXPECT_EQ("int i =\n"
+ " longFunction(\n"
+ " arg);",
+ format("int i = longFunction(arg);", SixIndent));
+}
+
} // end namespace tooling
} // end namespace clang
OpenPOWER on IntegriCloud