diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h index add2937..08dc1a0 100644 --- a/clang/include/clang/Format/Format.h +++ b/clang/include/clang/Format/Format.h @@ -21,6 +21,8 @@ #include "llvm/Support/Regex.h" #include +#define KEEP_LINE_BREAKS_FOR_NON_EMPTY_LINES_BACKPORTED + namespace llvm { namespace vfs { class FileSystem; @@ -1476,6 +1478,16 @@ struct FormatStyle { bool JavaScriptWrapImports; // clang-format on + /// If true, no line breaks are optimized out (works only with ColumnLimit = 0) + /// \code + /// true: false: + /// int foo(int a, vs. int foo(int a, int b) { + /// int b) { + /// bar(); bar(); + /// } } + /// \endcode + bool KeepLineBreaksForNonEmptyLines; + /// If true, the empty line at the start of blocks is kept. /// \code /// true: false: @@ -2126,6 +2138,7 @@ struct FormatStyle { JavaImportGroups == R.JavaImportGroups && JavaScriptQuotes == R.JavaScriptQuotes && JavaScriptWrapImports == R.JavaScriptWrapImports && + KeepLineBreaksForNonEmptyLines == R.KeepLineBreaksForNonEmptyLines && KeepEmptyLinesAtTheStartOfBlocks == R.KeepEmptyLinesAtTheStartOfBlocks && MacroBlockBegin == R.MacroBlockBegin && diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp index f12bca4..052095c 100644 --- a/clang/lib/Format/Format.cpp +++ b/clang/lib/Format/Format.cpp @@ -488,6 +488,8 @@ template <> struct MappingTraits { IO.mapOptional("JavaImportGroups", Style.JavaImportGroups); IO.mapOptional("JavaScriptQuotes", Style.JavaScriptQuotes); IO.mapOptional("JavaScriptWrapImports", Style.JavaScriptWrapImports); + IO.mapOptional("KeepLineBreaksForNonEmptyLines", + Style.KeepLineBreaksForNonEmptyLines); IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks", Style.KeepEmptyLinesAtTheStartOfBlocks); IO.mapOptional("MacroBlockBegin", Style.MacroBlockBegin); @@ -790,6 +792,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { LLVMStyle.JavaScriptWrapImports = true; LLVMStyle.TabWidth = 8; LLVMStyle.MaxEmptyLinesToKeep = 1; + LLVMStyle.KeepLineBreaksForNonEmptyLines = false; LLVMStyle.KeepEmptyLinesAtTheStartOfBlocks = true; LLVMStyle.NamespaceIndentation = FormatStyle::NI_None; LLVMStyle.ObjCBinPackProtocolList = FormatStyle::BPS_Auto; @@ -870,6 +873,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { GoogleStyle.IncludeStyle.IncludeIsMainRegex = "([-_](test|unittest))?$"; GoogleStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup; GoogleStyle.IndentCaseLabels = true; + GoogleStyle.KeepLineBreaksForNonEmptyLines = false; GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false; GoogleStyle.ObjCBinPackProtocolList = FormatStyle::BPS_Never; GoogleStyle.ObjCSpaceAfterProperty = false; diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp index fec85f1..85c1719 100644 --- a/clang/lib/Format/UnwrappedLineFormatter.cpp +++ b/clang/lib/Format/UnwrappedLineFormatter.cpp @@ -741,7 +741,7 @@ public: LineFormatter(ContinuationIndenter *Indenter, WhitespaceManager *Whitespaces, const FormatStyle &Style, UnwrappedLineFormatter *BlockFormatter) - : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style), + : Indenter(Indenter), Style(Style), Whitespaces(Whitespaces), BlockFormatter(BlockFormatter) {} virtual ~LineFormatter() {} @@ -782,7 +782,8 @@ protected: // assert so that we can simply call this function for all tokens. return true; - if (NewLine) { + if (NewLine || (Previous.Children[0]->First->MustBreakBefore && + Style.KeepLineBreaksForNonEmptyLines)) { int AdditionalIndent = State.Stack.back().Indent - Previous.Children[0]->Level * Style.IndentWidth; @@ -827,10 +828,10 @@ protected: } ContinuationIndenter *Indenter; + const FormatStyle &Style; private: WhitespaceManager *Whitespaces; - const FormatStyle &Style; UnwrappedLineFormatter *BlockFormatter; }; @@ -853,7 +854,7 @@ public: while (State.NextToken) { bool Newline = Indenter->mustBreak(State) || - (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0); + (State.NextToken->NewlinesBefore > 0 && Indenter->canBreak(State)); unsigned Penalty = 0; formatChildren(State, Newline, /*DryRun=*/false, Penalty); Indenter->addTokenToState(State, Newline, /*DryRun=*/false); diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp index ead6b47..6fd671d 100644 --- a/clang/lib/Format/UnwrappedLineParser.cpp +++ b/clang/lib/Format/UnwrappedLineParser.cpp @@ -2640,6 +2640,8 @@ void UnwrappedLineParser::nextToken(int LevelDifference) { else readTokenWithJavaScriptASI(); FormatTok->Previous = Previous; + if (FormatTok->NewlinesBefore && Style.KeepLineBreaksForNonEmptyLines) + FormatTok->MustBreakBefore = true; } void UnwrappedLineParser::distributeComments( diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp index 7f8a379..a2d477b 100644 --- a/clang/unittests/Format/FormatTest.cpp +++ b/clang/unittests/Format/FormatTest.cpp @@ -393,6 +393,22 @@ TEST_F(FormatTest, RemovesEmptyLines) { " void funk() {}\n" "};", Style)); + + Style.KeepLineBreaksForNonEmptyLines = true; + Style.ColumnLimit = 0; + EXPECT_EQ("int foo(int a,\n" + " int b)\n" + "{\n" + "}", + format("int foo(int a,\n" + "int b) {}", + Style)); + + EXPECT_EQ("[]() {\n" + " foo(); }", + format("[]() {\n" + "foo(); }", + Style)); } TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) {