r188260 - clang-format: Add option for the offset of constructor initializers.
Daniel Jasper
djasper at google.com
Tue Aug 13 03:58:30 PDT 2013
Author: djasper
Date: Tue Aug 13 05:58:30 2013
New Revision: 188260
URL: http://llvm.org/viewvc/llvm-project?rev=188260&view=rev
Log:
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.
Modified:
cfe/trunk/include/clang/Format/Format.h
cfe/trunk/lib/Format/Format.cpp
cfe/trunk/unittests/Format/FormatTest.cpp
Modified: cfe/trunk/include/clang/Format/Format.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Format/Format.h?rev=188260&r1=188259&r2=188260&view=diff
==============================================================================
--- cfe/trunk/include/clang/Format/Format.h (original)
+++ cfe/trunk/include/clang/Format/Format.h Tue Aug 13 05:58:30 2013
@@ -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 ==
Modified: cfe/trunk/lib/Format/Format.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Format/Format.cpp?rev=188260&r1=188259&r2=188260&view=diff
==============================================================================
--- cfe/trunk/lib/Format/Format.cpp (original)
+++ cfe/trunk/lib/Format/Format.cpp Tue Aug 13 05:58:30 2013
@@ -91,6 +91,8 @@ template <> struct MappingTraits<clang::
}
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;
Modified: cfe/trunk/unittests/Format/FormatTest.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/unittests/Format/FormatTest.cpp?rev=188260&r1=188259&r2=188260&view=diff
==============================================================================
--- cfe/trunk/unittests/Format/FormatTest.cpp (original)
+++ cfe/trunk/unittests/Format/FormatTest.cpp Tue Aug 13 05:58:30 2013
@@ -4186,7 +4186,7 @@ TEST_F(FormatTest, UnderstandContextOfRe
" f();\n");
// This is simply incomplete. Formatting is not important, but must not crash.
- verifyFormat("class A:");
+ verifyFormat("class A:");
}
TEST_F(FormatTest, DoNotInterfereWithErrorAndWarning) {
@@ -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, SplitsUTF8BlockCommen
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) {
More information about the cfe-commits
mailing list