clang-format: Add option for the offset of constructor initializers.

Some coding styles use a different indent for constructor initializers.

Patch by Klemens Baum. Thank you.
Review: http://llvm-reviews.chandlerc.com/D1360

Post review changes: Changed data type to unsigned as a negative indent
width does not make sense and added test for configuration parsing.

llvm-svn: 188260
This commit is contained in:
Daniel Jasper 2013-08-13 10:58:30 +00:00
parent 5bd3fab901
commit cdaffa45d4
3 changed files with 67 additions and 3 deletions

View File

@ -141,6 +141,10 @@ struct FormatStyle {
/// \brief The number of characters to use for indentation.
unsigned IndentWidth;
/// \brief The number of characters to use for indentation of constructor
/// initializer lists.
unsigned ConstructorInitializerIndentWidth;
/// \brief If \c true, always break after the \c template<...> of a template
/// declaration.
bool AlwaysBreakTemplateDeclarations;
@ -192,6 +196,8 @@ struct FormatStyle {
bool operator==(const FormatStyle &R) const {
return AccessModifierOffset == R.AccessModifierOffset &&
ConstructorInitializerIndentWidth ==
R.ConstructorInitializerIndentWidth &&
AlignEscapedNewlinesLeft == R.AlignEscapedNewlinesLeft &&
AlignTrailingComments == R.AlignTrailingComments &&
AllowAllParametersOfDeclarationOnNextLine ==

View File

@ -91,6 +91,8 @@ template <> struct MappingTraits<clang::format::FormatStyle> {
}
IO.mapOptional("AccessModifierOffset", Style.AccessModifierOffset);
IO.mapOptional("ConstructorInitializerIndentWidth",
Style.ConstructorInitializerIndentWidth);
IO.mapOptional("AlignEscapedNewlinesLeft", Style.AlignEscapedNewlinesLeft);
IO.mapOptional("AlignTrailingComments", Style.AlignTrailingComments);
IO.mapOptional("AllowAllParametersOfDeclarationOnNextLine",
@ -167,6 +169,7 @@ FormatStyle getLLVMStyle() {
LLVMStyle.BreakConstructorInitializersBeforeComma = false;
LLVMStyle.ColumnLimit = 80;
LLVMStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = false;
LLVMStyle.ConstructorInitializerIndentWidth = 4;
LLVMStyle.Cpp11BracedListStyle = false;
LLVMStyle.DerivePointerBinding = false;
LLVMStyle.ExperimentalAutoDetectBinPacking = false;
@ -203,6 +206,7 @@ FormatStyle getGoogleStyle() {
GoogleStyle.BreakConstructorInitializersBeforeComma = false;
GoogleStyle.ColumnLimit = 80;
GoogleStyle.ConstructorInitializerAllOnOneLineOrOnePerLine = true;
GoogleStyle.ConstructorInitializerIndentWidth = 4;
GoogleStyle.Cpp11BracedListStyle = true;
GoogleStyle.DerivePointerBinding = true;
GoogleStyle.ExperimentalAutoDetectBinPacking = false;
@ -651,6 +655,10 @@ private:
Previous.isOneOf(tok::coloncolon, tok::equal) ||
Previous.Type == TT_ObjCMethodExpr) {
State.Column = ContinuationIndent;
} else if (Current.Type == TT_CtorInitializerColon) {
State.Column = FirstIndent + Style.ConstructorInitializerIndentWidth;
} else if (Current.Type == TT_CtorInitializerComma) {
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
@ -821,8 +829,9 @@ private:
// : First(...), ...
// Next(...)
// ^ line up here.
if (!Style.BreakConstructorInitializersBeforeComma)
State.Stack.back().Indent = State.Column + 2;
State.Stack.back().Indent =
State.Column +
(Style.BreakConstructorInitializersBeforeComma ? 0 : 2);
if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine)
State.Stack.back().AvoidBinPacking = true;
State.Stack.back().BreakBeforeParameter = false;

View File

@ -5660,6 +5660,8 @@ TEST_F(FormatTest, ParsesConfiguration) {
CHECK_PARSE_BOOL(IndentFunctionDeclarationAfterType);
CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234);
CHECK_PARSE("ConstructorInitializerIndentWidth: 1234",
ConstructorInitializerIndentWidth, 1234u);
CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u);
CHECK_PARSE("MaxEmptyLinesToKeep: 1234", MaxEmptyLinesToKeep, 1234u);
CHECK_PARSE("PenaltyExcessCharacter: 1234", PenaltyExcessCharacter, 1234u);
@ -5789,6 +5791,53 @@ TEST_F(FormatTest, SplitsUTF8BlockComments) {
format("/* 𝓣𝓮𝓼𝓽 𝔣𝔬𝔲𝔯 𝕓𝕪𝕥𝕖 𝖀𝕿𝕱-𝟠 */", getLLVMStyleWithColumns(12)));
}
TEST_F(FormatTest, ConstructorInitializerIndentWidth) {
FormatStyle Style = getLLVMStyle();
Style.ConstructorInitializerIndentWidth = 4;
verifyFormat(
"SomeClass::Constructor()\n"
" : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
" aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
Style);
Style.ConstructorInitializerIndentWidth = 2;
verifyFormat(
"SomeClass::Constructor()\n"
" : aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
" aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
Style);
Style.ConstructorInitializerIndentWidth = 0;
verifyFormat(
"SomeClass::Constructor()\n"
": aaaaaaaaaaaaa(aaaaaaaaaaaaaa), aaaaaaaaaaaaa(aaaaaaaaaaaaaa),\n"
" aaaaaaaaaaaaa(aaaaaaaaaaaaaa) {}",
Style);
Style.BreakConstructorInitializersBeforeComma = true;
Style.ConstructorInitializerIndentWidth = 4;
verifyFormat("SomeClass::Constructor()\n"
" : a(a)\n"
" , b(b)\n"
" , c(c) {}",
Style);
Style.ConstructorInitializerIndentWidth = 2;
verifyFormat("SomeClass::Constructor()\n"
" : a(a)\n"
" , b(b)\n"
" , c(c) {}",
Style);
Style.ConstructorInitializerIndentWidth = 0;
verifyFormat("SomeClass::Constructor()\n"
": a(a)\n"
", b(b)\n"
", c(c) {}",
Style);
}
#endif
TEST_F(FormatTest, FormatsWithWebKitStyle) {