From 6315fec908431be8f11d9e3c29caadc77c748702 Mon Sep 17 00:00:00 2001 From: Daniel Jasper Date: Tue, 13 Aug 2013 10:58:30 +0000 Subject: [PATCH] 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. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@188260 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Format/Format.h | 6 ++++ lib/Format/Format.cpp | 13 +++++++-- unittests/Format/FormatTest.cpp | 51 ++++++++++++++++++++++++++++++++- 3 files changed, 67 insertions(+), 3 deletions(-) diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index d357d01663..d6062e5c6a 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -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 == diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 6fe4d45d1d..ec1497c87f 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -91,6 +91,8 @@ template <> struct MappingTraits { } 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; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 79e0186a81..27800cdcdd 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -4186,7 +4186,7 @@ TEST_F(FormatTest, UnderstandContextOfRecordTypeKeywords) { " 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, 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) { -- 2.50.1