From a9a7f108a4a10da1f08fd4538a15f441bfee72c1 Mon Sep 17 00:00:00 2001 From: Manuel Klimek Date: Fri, 21 Jun 2013 17:25:42 +0000 Subject: [PATCH] Add an option to not indent declarations when breaking after the type. Make that option the default for LLVM style. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@184563 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/clang/Format/Format.h | 9 ++++-- lib/Format/Format.cpp | 7 ++++- unittests/Format/FormatTest.cpp | 53 ++++++++++++++++++++++----------- 3 files changed, 48 insertions(+), 21 deletions(-) diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index 595e798dd3..24eeb76d06 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -131,6 +131,10 @@ struct FormatStyle { /// \brief If \c true, format { 1 }, otherwise {1}. bool SpacesInBracedLists; + /// \brief If \c true, indent when breaking function declarations which + /// are not also definitions after the type. + bool IndentFunctionDeclarationAfterType; + bool operator==(const FormatStyle &R) const { return AccessModifierOffset == R.AccessModifierOffset && AlignEscapedNewlinesLeft == R.AlignEscapedNewlinesLeft && @@ -157,9 +161,10 @@ struct FormatStyle { PointerBindsToType == R.PointerBindsToType && SpacesBeforeTrailingComments == R.SpacesBeforeTrailingComments && SpacesInBracedLists == R.SpacesInBracedLists && - Standard == R.Standard && UseTab == R.UseTab; + Standard == R.Standard && UseTab == R.UseTab && + IndentFunctionDeclarationAfterType == + R.IndentFunctionDeclarationAfterType; } - }; /// \brief Returns a format style complying with the LLVM coding standards: diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 86437d4c18..a81f9d0533 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -109,6 +109,8 @@ template <> struct MappingTraits { IO.mapOptional("IndentWidth", Style.IndentWidth); IO.mapOptional("UseTab", Style.UseTab); IO.mapOptional("BreakBeforeBraces", Style.BreakBeforeBraces); + IO.mapOptional("IndentFunctionDeclarationAfterType", + Style.IndentFunctionDeclarationAfterType); } }; } @@ -143,6 +145,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.IndentWidth = 2; LLVMStyle.UseTab = false; LLVMStyle.BreakBeforeBraces = FormatStyle::BS_Attach; + LLVMStyle.IndentFunctionDeclarationAfterType = false; return LLVMStyle; } @@ -172,6 +175,7 @@ FormatStyle getGoogleStyle() { GoogleStyle.IndentWidth = 2; GoogleStyle.UseTab = false; GoogleStyle.BreakBeforeBraces = FormatStyle::BS_Attach; + GoogleStyle.IndentFunctionDeclarationAfterType = true; return GoogleStyle; } @@ -524,7 +528,8 @@ private: State.Column = State.Stack.back().VariablePos; } else if (Previous.ClosesTemplateDeclaration || (Current.Type == TT_StartOfName && State.ParenLevel == 0 && - Line.StartsDefinition)) { + (!Style.IndentFunctionDeclarationAfterType || + Line.StartsDefinition))) { State.Column = State.Stack.back().Indent; } else if (Current.Type == TT_ObjCSelectorName) { if (State.Stack.back().ColonPos > Current.CodePointCount) { diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index e4d4c7afeb..08dd84222b 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -2008,7 +2008,7 @@ TEST_F(FormatTest, LayoutStatementsAroundPreprocessorDirectives) { EXPECT_EQ("int\n" "#define A\n" " a;", - format("int\n#define A\na;")); + format("int\n#define A\na;", getGoogleStyle())); verifyFormat("functionCallTo(\n" " someOtherFunction(\n" " withSomeParameters, whichInSequence,\n" @@ -2337,19 +2337,22 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) { // 2) break after return type. verifyFormat( - "Aaaaaaaaaaaaaaaaaaaaaaaa\n" - " bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);"); + "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " bbbbbbbbbbbbbb(Cccccccccccccc cccccccccccccccccccccccccc);", + getGoogleStyle()); // 3) break after (. verifyFormat( "Aaaaaaaaaaaaaaaaaaaaaaaa bbbbbbbbbbbbbbbbbb(\n" - " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);"); + " Cccccccccccccccccccccccccccccc cccccccccccccccccccccccccccccccc);", + getGoogleStyle()); // 4) break before after nested name specifiers. verifyFormat( "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " SomeClasssssssssssssssssssssssssssssssssssssss::\n" - " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);"); + " bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc);", + getGoogleStyle()); // However, there are exceptions, if a sufficient amount of lines can be // saved. @@ -2361,10 +2364,11 @@ TEST_F(FormatTest, BreaksFunctionDeclarations) { " Cccccccccccccc cccccccccc,\n" " Cccccccccccccc cccccccccc);"); verifyFormat( - "Aaaaaaaaaaaaaaaaaa\n" + "Aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " bbbbbbbbbbb(Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc,\n" - " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);"); + " Cccccccccccccc cccccccccc, Cccccccccccccc cccccccccc);", + getGoogleStyle()); verifyFormat( "Aaaaaaaaaa bbbbbbbbbbbbbbbbbbbbbbbbbbbbbb(Cccccccccccccc cccccccccc,\n" " Cccccccccccccc cccccccccc,\n" @@ -2419,11 +2423,11 @@ TEST_F(FormatTest, BreaksFunctionDeclarationsWithTrailingTokens) { verifyFormat("bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" " __attribute__((unused));"); verifyFormat( - "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" - " GUARDED_BY(aaaaaaaaaaaa);"); + "bool aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa\n" + " GUARDED_BY(aaaaaaaaaaaa);", + getGoogleStyle()); } - TEST_F(FormatTest, BreaksDesireably) { verifyFormat("if (aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" " aaaaaaaaaaaaaaaaaaa(aaaaaaaaaaaaaaa) ||\n" @@ -2745,9 +2749,10 @@ TEST_F(FormatTest, DeclarationsOfMultipleVariables) { " ***c = ccccccccccccccccccc, ***d = ddddddddddddddd;"); // FIXME: If multiple variables are defined, the "*" needs to move to the new // line. Also fix indent for breaking after the type, this looks bad. - verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa *\n" + verifyFormat("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa*\n" " aaaaaaaaaaaaaaaaaaaaaaaaaaaaa = aaaaaaaaaaaaaaaaaaa,\n" - " *b = bbbbbbbbbbbbbbbbbbb;"); + " *b = bbbbbbbbbbbbbbbbbbb;", + getGoogleStyle()); // Not ideal, but pointer-with-type does not allow much here. verifyGoogleFormat( @@ -3400,17 +3405,21 @@ TEST_F(FormatTest, FormatsFunctionTypes) { TEST_F(FormatTest, BreaksLongDeclarations) { verifyFormat("typedef LoooooooooooooooooooooooooooooooooooooooongType\n" - " AnotherNameForTheLongType;"); + " AnotherNameForTheLongType;", + getGoogleStyle()); verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType\n" - " LoooooooooooooooooooooooooooooooooooooooongVariable;"); + " LoooooooooooooooooooooooooooooooooooooooongVariable;", + getGoogleStyle()); verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" - " LoooooooooooooooooooooooooooooooongFunctionDeclaration();"); + " LoooooooooooooooooooooooooooooooongFunctionDeclaration();", + getGoogleStyle()); verifyFormat("LoooooooooooooooooooooooooooooooooooooooongReturnType\n" "LooooooooooooooooooooooooooooooooooongFunctionDefinition() {}"); // FIXME: Without the comment, this breaks after "(". - verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" - " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();"); + verifyFormat("LoooooooooooooooooooooooooooooooooooooooongType // break\n" + " (*LoooooooooooooooooooooooooooongFunctionTypeVarialbe)();", + getGoogleStyle()); verifyFormat("int *someFunction(int LoooooooooooooooooooongParam1,\n" " int LoooooooooooooooooooongParam2) {}"); @@ -3431,7 +3440,8 @@ TEST_F(FormatTest, BreaksLongDeclarations) { "Function() {}"); verifyFormat( "aaaaaaaaaaaaaaaa::aaaaaaaaaaaaaaaa\n" - " aaaaaaaaaaaaaaaaaaaaaaa;"); + " aaaaaaaaaaaaaaaaaaaaaaa;", + getGoogleStyle()); verifyGoogleFormat( "TypeSpecDecl* TypeSpecDecl::Create(ASTContext& C, DeclContext* DC,\n" @@ -4808,6 +4818,12 @@ TEST_F(FormatTest, ConfigurableIndentWidth) { EightIndent); } +TEST_F(FormatTest, ConfigureableFunctionDeclarationIndentAfterType) { + verifyFormat("void\n" + "f();", + getLLVMStyleWithColumns(8)); +} + TEST_F(FormatTest, ConfigurableUseOfTab) { FormatStyle Tab = getLLVMStyleWithColumns(42); Tab.IndentWidth = 8; @@ -4975,6 +4991,7 @@ TEST_F(FormatTest, ParsesConfiguration) { CHECK_PARSE_BOOL(PointerBindsToType); CHECK_PARSE_BOOL(SpacesInBracedLists); CHECK_PARSE_BOOL(UseTab); + CHECK_PARSE_BOOL(IndentFunctionDeclarationAfterType); CHECK_PARSE("AccessModifierOffset: -1234", AccessModifierOffset, -1234); CHECK_PARSE("ColumnLimit: 1234", ColumnLimit, 1234u); -- 2.40.0