Line 0
Link Here
|
|
|
1 |
From 9b992a0f7f160dd6c75f20a4dcfcf7c60a4894df Mon Sep 17 00:00:00 2001 |
2 |
From: Ivan Donchevskii <ivan.donchevskii@qt.io> |
3 |
Date: Fri, 12 Oct 2018 08:19:25 +0200 |
4 |
Subject: [clang-format] Introduce the flag which allows not to shrink lines |
5 |
|
6 |
https://reviews.llvm.org/D53072 |
7 |
|
8 |
Currently there's no way to prevent to lines optimization even |
9 |
if you have intentionally put <CR> to split the line. |
10 |
|
11 |
In general case it's fine. So I would prefer to have such option |
12 |
which you can enable in special cases (for me it's an IDE related use case). |
13 |
|
14 |
Revert this change if upstream clang-format offers better solution. |
15 |
|
16 |
Reviewed-by; Marco Bubke <marco.bubke@qt.io> |
17 |
|
18 |
Cherry picked from commit |
19 |
clang.git/fa1b9053729ec6a4425a44ec5502dd388928274a. |
20 |
|
21 |
Change-Id: I92296dd177c811593ebba7d4b5c3c5f472af10a2 |
22 |
Reviewed-by: Cristian Adam <cristian.adam@qt.io> |
23 |
--- |
24 |
clang/include/clang/Format/Format.h | 13 +++++++++++++ |
25 |
clang/lib/Format/Format.cpp | 4 ++++ |
26 |
clang/lib/Format/UnwrappedLineFormatter.cpp | 9 +++++---- |
27 |
clang/lib/Format/UnwrappedLineParser.cpp | 2 ++ |
28 |
clang/unittests/Format/FormatTest.cpp | 16 ++++++++++++++++ |
29 |
5 files changed, 40 insertions(+), 4 deletions(-) |
30 |
|
31 |
NB! Line numbers adjusted manually to get perfect patch. |
32 |
|
33 |
diff --git a/clang/include/clang/Format/Format.h b/clang/include/clang/Format/Format.h |
34 |
index 3549ec9e..1e6a4869 100755 |
35 |
--- a/clang/include/clang/Format/Format.h |
36 |
+++ b/clang/include/clang/Format/Format.h |
37 |
@@ -21,6 +21,8 @@ |
38 |
#include "llvm/Support/Regex.h" |
39 |
#include <system_error> |
40 |
|
41 |
+#define KEEP_LINE_BREAKS_FOR_NON_EMPTY_LINES_BACKPORTED |
42 |
+ |
43 |
namespace llvm { |
44 |
namespace vfs { |
45 |
class FileSystem; |
46 |
@@ -1666,6 +1668,16 @@ struct FormatStyle { |
47 |
bool JavaScriptWrapImports; |
48 |
// clang-format on |
49 |
|
50 |
+ /// If true, no line breaks are optimized out (works only with ColumnLimit = 0) |
51 |
+ /// \code |
52 |
+ /// true: false: |
53 |
+ /// int foo(int a, vs. int foo(int a, int b) { |
54 |
+ /// int b) { |
55 |
+ /// bar(); bar(); |
56 |
+ /// } } |
57 |
+ /// \endcode |
58 |
+ bool KeepLineBreaksForNonEmptyLines; |
59 |
+ |
60 |
/// If true, the empty line at the start of blocks is kept. |
61 |
/// \code |
62 |
/// true: false: |
63 |
@@ -2358,6 +2370,7 @@ struct FormatStyle { |
64 |
JavaImportGroups == R.JavaImportGroups && |
65 |
JavaScriptQuotes == R.JavaScriptQuotes && |
66 |
JavaScriptWrapImports == R.JavaScriptWrapImports && |
67 |
+ KeepLineBreaksForNonEmptyLines == R.KeepLineBreaksForNonEmptyLines && |
68 |
KeepEmptyLinesAtTheStartOfBlocks == |
69 |
R.KeepEmptyLinesAtTheStartOfBlocks && |
70 |
MacroBlockBegin == R.MacroBlockBegin && |
71 |
diff --git a/clang/lib/Format/Format.cpp b/clang/lib/Format/Format.cpp |
72 |
index 0d277a64..7875838b 100644 |
73 |
--- a/clang/lib/Format/Format.cpp |
74 |
+++ b/clang/lib/Format/Format.cpp |
75 |
@@ -532,6 +532,8 @@ template <> struct MappingTraits<FormatStyle> { |
76 |
IO.mapOptional("JavaImportGroups", Style.JavaImportGroups); |
77 |
IO.mapOptional("JavaScriptQuotes", Style.JavaScriptQuotes); |
78 |
IO.mapOptional("JavaScriptWrapImports", Style.JavaScriptWrapImports); |
79 |
+ IO.mapOptional("KeepLineBreaksForNonEmptyLines", |
80 |
+ Style.KeepLineBreaksForNonEmptyLines); |
81 |
IO.mapOptional("KeepEmptyLinesAtTheStartOfBlocks", |
82 |
Style.KeepEmptyLinesAtTheStartOfBlocks); |
83 |
IO.mapOptional("MacroBlockBegin", Style.MacroBlockBegin); |
84 |
@@ -889,6 +891,7 @@ FormatStyle getLLVMStyle(FormatStyle::LanguageKind Language) { |
85 |
LLVMStyle.JavaScriptWrapImports = true; |
86 |
LLVMStyle.TabWidth = 8; |
87 |
LLVMStyle.MaxEmptyLinesToKeep = 1; |
88 |
+ LLVMStyle.KeepLineBreaksForNonEmptyLines = false; |
89 |
LLVMStyle.KeepEmptyLinesAtTheStartOfBlocks = true; |
90 |
LLVMStyle.NamespaceIndentation = FormatStyle::NI_None; |
91 |
LLVMStyle.ObjCBinPackProtocolList = FormatStyle::BPS_Auto; |
92 |
@@ -973,6 +976,7 @@ FormatStyle getGoogleStyle(FormatStyle::LanguageKind Language) { |
93 |
GoogleStyle.IncludeStyle.IncludeIsMainRegex = "([-_](test|unittest))?$"; |
94 |
GoogleStyle.IncludeStyle.IncludeBlocks = tooling::IncludeStyle::IBS_Regroup; |
95 |
GoogleStyle.IndentCaseLabels = true; |
96 |
+ GoogleStyle.KeepLineBreaksForNonEmptyLines = false; |
97 |
GoogleStyle.KeepEmptyLinesAtTheStartOfBlocks = false; |
98 |
GoogleStyle.ObjCBinPackProtocolList = FormatStyle::BPS_Never; |
99 |
GoogleStyle.ObjCSpaceAfterProperty = false; |
100 |
diff --git a/clang/lib/Format/UnwrappedLineFormatter.cpp b/clang/lib/Format/UnwrappedLineFormatter.cpp |
101 |
index 22f27a66..5060b04f 100644 |
102 |
--- a/clang/lib/Format/UnwrappedLineFormatter.cpp |
103 |
+++ b/clang/lib/Format/UnwrappedLineFormatter.cpp |
104 |
@@ -732,7 +732,7 @@ public: |
105 |
LineFormatter(ContinuationIndenter *Indenter, WhitespaceManager *Whitespaces, |
106 |
const FormatStyle &Style, |
107 |
UnwrappedLineFormatter *BlockFormatter) |
108 |
- : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style), |
109 |
+ : Indenter(Indenter), Style(Style), Whitespaces(Whitespaces), |
110 |
BlockFormatter(BlockFormatter) {} |
111 |
virtual ~LineFormatter() {} |
112 |
|
113 |
@@ -773,7 +773,8 @@ protected: |
114 |
// assert so that we can simply call this function for all tokens. |
115 |
return true; |
116 |
|
117 |
- if (NewLine) { |
118 |
+ if (NewLine || (Previous.Children[0]->First->MustBreakBefore && |
119 |
+ Style.KeepLineBreaksForNonEmptyLines)) { |
120 |
int AdditionalIndent = State.Stack.back().Indent - |
121 |
Previous.Children[0]->Level * Style.IndentWidth; |
122 |
|
123 |
@@ -819,10 +820,10 @@ protected: |
124 |
} |
125 |
|
126 |
ContinuationIndenter *Indenter; |
127 |
+ const FormatStyle &Style; |
128 |
|
129 |
private: |
130 |
WhitespaceManager *Whitespaces; |
131 |
- const FormatStyle &Style; |
132 |
UnwrappedLineFormatter *BlockFormatter; |
133 |
}; |
134 |
|
135 |
@@ -845,7 +846,7 @@ public: |
136 |
while (State.NextToken) { |
137 |
bool Newline = |
138 |
Indenter->mustBreak(State) || |
139 |
- (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0); |
140 |
+ (State.NextToken->NewlinesBefore > 0 && Indenter->canBreak(State)); |
141 |
unsigned Penalty = 0; |
142 |
formatChildren(State, Newline, /*DryRun=*/false, Penalty); |
143 |
Indenter->addTokenToState(State, Newline, /*DryRun=*/false); |
144 |
diff --git a/clang/lib/Format/UnwrappedLineParser.cpp b/clang/lib/Format/UnwrappedLineParser.cpp |
145 |
index a3738642..f80c1116 100644 |
146 |
--- a/clang/lib/Format/UnwrappedLineParser.cpp |
147 |
+++ b/clang/lib/Format/UnwrappedLineParser.cpp |
148 |
@@ -2942,6 +2942,8 @@ void UnwrappedLineParser::nextToken(int LevelDifference) { |
149 |
else |
150 |
readTokenWithJavaScriptASI(); |
151 |
FormatTok->Previous = Previous; |
152 |
+ if (FormatTok->NewlinesBefore && Style.KeepLineBreaksForNonEmptyLines) |
153 |
+ FormatTok->MustBreakBefore = true; |
154 |
} |
155 |
|
156 |
void UnwrappedLineParser::distributeComments( |
157 |
diff --git a/clang/unittests/Format/FormatTest.cpp b/clang/unittests/Format/FormatTest.cpp |
158 |
index 17d302f0..48971623 100644 |
159 |
--- a/clang/unittests/Format/FormatTest.cpp |
160 |
+++ b/clang/unittests/Format/FormatTest.cpp |
161 |
@@ -393,6 +393,22 @@ TEST_F(FormatTest, RemovesEmptyLines) { |
162 |
" void funk() {}\n" |
163 |
"};", |
164 |
Style)); |
165 |
+ |
166 |
+ Style.KeepLineBreaksForNonEmptyLines = true; |
167 |
+ Style.ColumnLimit = 0; |
168 |
+ EXPECT_EQ("int foo(int a,\n" |
169 |
+ " int b)\n" |
170 |
+ "{\n" |
171 |
+ "}", |
172 |
+ format("int foo(int a,\n" |
173 |
+ "int b) {}", |
174 |
+ Style)); |
175 |
+ |
176 |
+ EXPECT_EQ("[]() {\n" |
177 |
+ " foo(); }", |
178 |
+ format("[]() {\n" |
179 |
+ "foo(); }", |
180 |
+ Style)); |
181 |
} |
182 |
|
183 |
TEST_F(FormatTest, RecognizesBinaryOperatorKeywords) { |
184 |
-- |
185 |
2.26.2 |
186 |
|