From: Manuel Klimek Date: Mon, 13 May 2013 08:42:42 +0000 (+0000) Subject: Implements IndentWidth. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=07a64eca75d96ce77d1ce8cb73d1cb0877695d06;p=clang Implements IndentWidth. This is required for various styles that are for example based on 8-indent. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@181690 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index 9d17c77db7..dd8f666180 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -94,6 +94,9 @@ struct FormatStyle { /// Otherwise puts them into the right-most column. bool AlignEscapedNewlinesLeft; + /// \brief The number of characters to use for indentation. + unsigned IndentWidth; + bool operator==(const FormatStyle &R) const { return AccessModifierOffset == R.AccessModifierOffset && AlignEscapedNewlinesLeft == R.AlignEscapedNewlinesLeft && @@ -113,7 +116,8 @@ struct FormatStyle { PenaltyReturnTypeOnItsOwnLine == R.PenaltyReturnTypeOnItsOwnLine && PointerBindsToType == R.PointerBindsToType && SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments && - Standard == R.Standard; + Standard == R.Standard && + IndentWidth == IndentWidth; } }; diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 9a320f94bd..d5676814c1 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -85,6 +85,7 @@ template <> struct MappingTraits { IO.mapOptional("SpacesBeforeTrailingComments", Style.SpacesBeforeTrailingComments); IO.mapOptional("Standard", Style.Standard); + IO.mapOptional("IndentWidth", Style.IndentWidth); } }; } @@ -111,6 +112,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.PointerBindsToType = false; LLVMStyle.SpacesBeforeTrailingComments = 1; LLVMStyle.Standard = FormatStyle::LS_Cpp03; + LLVMStyle.IndentWidth = 2; return LLVMStyle; } @@ -132,6 +134,7 @@ FormatStyle getGoogleStyle() { GoogleStyle.PointerBindsToType = true; GoogleStyle.SpacesBeforeTrailingComments = 2; GoogleStyle.Standard = FormatStyle::LS_Auto; + GoogleStyle.IndentWidth = 2; return GoogleStyle; } @@ -429,7 +432,7 @@ private: if (Newline) { unsigned WhitespaceStartColumn = State.Column; if (Current.is(tok::r_brace)) { - State.Column = Line.Level * 2; + State.Column = Line.Level * Style.IndentWidth; } else if (Current.is(tok::string_literal) && State.StartOfStringLiteral != 0) { State.Column = State.StartOfStringLiteral; @@ -604,6 +607,11 @@ private: Current.LastInChainOfCalls ? 0 : State.Column + Current.FormatTok.TokenLength; if (Current.Type == TT_CtorInitializerColon) { + // Indent 2 from the column, so: + // SomeClass::SomeClass() + // : First(...), ... + // Next(...) + // ^ line up here. State.Stack.back().Indent = State.Column + 2; if (Style.ConstructorInitializerAllOnOneLineOrOnePerLine) State.Stack.back().AvoidBinPacking = true; @@ -656,7 +664,7 @@ private: unsigned NewIndent; bool AvoidBinPacking; if (Current.is(tok::l_brace)) { - NewIndent = 2 + State.Stack.back().LastSpace; + NewIndent = Style.IndentWidth + State.Stack.back().LastSpace; AvoidBinPacking = false; } else { NewIndent = 4 + std::max(State.Stack.back().LastSpace, @@ -1253,7 +1261,7 @@ private: return IndentForLevel[Level]; if (Level == 0) return 0; - return getIndent(IndentForLevel, Level - 1) + 2; + return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth; } /// \brief Get the offset of the line relatively to the level. diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index 9b3fb9cc06..f0319d5e48 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -3959,6 +3959,36 @@ TEST_F(FormatTest, DoNotCreateUnreasonableUnwrappedLines) { "}"); } +TEST_F(FormatTest, FormatsClosingBracesInEmptyNestedBlocks) { + verifyFormat("class X {\n" + " void f() {\n" + " }\n" + "};", + getLLVMStyleWithColumns(12)); +} + +TEST_F(FormatTest, ConfigurableIndentWidth) { + FormatStyle EightIndent = getLLVMStyleWithColumns(18); + EightIndent.IndentWidth = 8; + verifyFormat("void f() {\n" + " someFunction();\n" + " if (true) {\n" + " f();\n" + " }\n" + "}", + EightIndent); + verifyFormat("class X {\n" + " void f() {\n" + " }\n" + "};", + EightIndent); + verifyFormat("int x[] = {\n" + " call(),\n" + " call(),\n" + "};", + EightIndent); +} + bool allStylesEqual(ArrayRef Styles) { for (size_t i = 1; i < Styles.size(); ++i) if (!(Styles[0] == Styles[i])) @@ -4022,6 +4052,7 @@ TEST_F(FormatTest, ParsesConfiguration) { PenaltyReturnTypeOnItsOwnLine, 1234u); CHECK_PARSE("SpacesBeforeTrailingComments: 1234", SpacesBeforeTrailingComments, 1234u); + CHECK_PARSE("IndentWidth: 32", IndentWidth, 32u); Style.Standard = FormatStyle::LS_Auto; CHECK_PARSE("Standard: C++03", Standard, FormatStyle::LS_Cpp03);