From: Daniel Jasper Date: Fri, 21 Mar 2014 13:43:14 +0000 (+0000) Subject: clang-format: Add flag for removing empty lines at the start of blocks. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b6e55caa1d10e2292cbb953a1934e91a3511b084;p=clang clang-format: Add flag for removing empty lines at the start of blocks. This unbreaks polly-formatting-tests and we can make a decision for LLVM style independently. git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@204467 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Format/Format.h b/include/clang/Format/Format.h index c322f552d1..fd180f4671 100644 --- a/include/clang/Format/Format.h +++ b/include/clang/Format/Format.h @@ -58,6 +58,9 @@ struct FormatStyle { /// \brief The maximum number of consecutive empty lines to keep. unsigned MaxEmptyLinesToKeep; + /// \brief If true, empty lines at the start of blocks are kept. + bool KeepEmptyLinesAtTheStartOfBlocks; + /// \brief The penalty for each line break introduced inside a comment. unsigned PenaltyBreakComment; @@ -333,6 +336,8 @@ struct FormatStyle { R.IndentFunctionDeclarationAfterType && IndentWidth == R.IndentWidth && Language == R.Language && MaxEmptyLinesToKeep == R.MaxEmptyLinesToKeep && + KeepEmptyLinesAtTheStartOfBlocks == + R.KeepEmptyLinesAtTheStartOfBlocks && NamespaceIndentation == R.NamespaceIndentation && ObjCSpaceAfterProperty == R.ObjCSpaceAfterProperty && ObjCSpaceBeforeProtocolList == R.ObjCSpaceBeforeProtocolList && diff --git a/lib/Format/Format.cpp b/lib/Format/Format.cpp index 976b546681..b810f4492e 100644 --- a/lib/Format/Format.cpp +++ b/lib/Format/Format.cpp @@ -163,6 +163,8 @@ template <> struct MappingTraits { Style.ExperimentalAutoDetectBinPacking); IO.mapOptional("IndentCaseLabels", Style.IndentCaseLabels); IO.mapOptional("MaxEmptyLinesToKeep", Style.MaxEmptyLinesToKeep); + IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks", + Style.KeepEmptyLinesAtTheStartOfBlocks); IO.mapOptional("NamespaceIndentation", Style.NamespaceIndentation); IO.mapOptional("ObjCSpaceAfterProperty", Style.ObjCSpaceAfterProperty); IO.mapOptional("ObjCSpaceBeforeProtocolList", @@ -267,6 +269,7 @@ FormatStyle getLLVMStyle() { LLVMStyle.IndentWidth = 2; LLVMStyle.TabWidth = 8; LLVMStyle.MaxEmptyLinesToKeep = 1; + LLVMStyle.KeepEmptyLinesAtTheStartOfBlocks = true; LLVMStyle.NamespaceIndentation = FormatStyle::NI_None; LLVMStyle.ObjCSpaceAfterProperty = false; LLVMStyle.ObjCSpaceBeforeProtocolList = true; @@ -308,6 +311,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { GoogleStyle.DerivePointerBinding = true; GoogleStyle.IndentCaseLabels = true; GoogleStyle.IndentFunctionDeclarationAfterType = true; + GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false; GoogleStyle.ObjCSpaceAfterProperty = false; GoogleStyle.ObjCSpaceBeforeProtocolList = false; GoogleStyle.PointerBindsToType = true; @@ -889,7 +893,8 @@ private: Newlines = 1; // Remove empty lines after "{". - if (PreviousLine && PreviousLine->Last->is(tok::l_brace) && + if (!Style.KeepEmptyLinesAtTheStartOfBlocks && PreviousLine && + PreviousLine->Last->is(tok::l_brace) && PreviousLine->First->isNot(tok::kw_namespace)) Newlines = 1; diff --git a/unittests/Format/FormatTest.cpp b/unittests/Format/FormatTest.cpp index dafaac4569..0ae08bcfc0 100644 --- a/unittests/Format/FormatTest.cpp +++ b/unittests/Format/FormatTest.cpp @@ -182,11 +182,14 @@ TEST_F(FormatTest, RemovesEmptyLines) { format("namespace N {\n" "\n" "int i;\n" - "}")); + "}", + getGoogleStyle())); // Remove empty lines at the beginning and end of blocks. EXPECT_EQ("void f() {\n" + "\n" " if (a) {\n" + "\n" " f();\n" " }\n" "}", @@ -198,7 +201,23 @@ TEST_F(FormatTest, RemovesEmptyLines) { "\n" " }\n" "\n" - "}")); + "}", + getLLVMStyle())); + EXPECT_EQ("void f() {\n" + " if (a) {\n" + " f();\n" + " }\n" + "}", + format("void f() {\n" + "\n" + " if (a) {\n" + "\n" + " f();\n" + "\n" + " }\n" + "\n" + "}", + getGoogleStyle())); // Don't remove empty lines in more complex control statements. EXPECT_EQ("void f() {\n" @@ -7490,6 +7509,7 @@ TEST_F(FormatTest, ParsesConfiguration) { CHECK_PARSE_BOOL(ConstructorInitializerAllOnOneLineOrOnePerLine); CHECK_PARSE_BOOL(DerivePointerBinding); CHECK_PARSE_BOOL(IndentCaseLabels); + CHECK_PARSE_BOOL(KeepEmptyLinesAtTheStartOfBlocks); CHECK_PARSE_BOOL(ObjCSpaceAfterProperty); CHECK_PARSE_BOOL(ObjCSpaceBeforeProtocolList); CHECK_PARSE_BOOL(PointerBindsToType);