blob: 3f95aaa8475436ad791dee36123b42664978d601 [file] [log] [blame]
Daniel Jasper0df50932014-12-10 19:00:42 +00001//===--- UnwrappedLineFormatter.cpp - Format C++ code ---------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10#include "UnwrappedLineFormatter.h"
11#include "WhitespaceManager.h"
12#include "llvm/Support/Debug.h"
13
14#define DEBUG_TYPE "format-formatter"
15
16namespace clang {
17namespace format {
18
19namespace {
20
21bool startsExternCBlock(const AnnotatedLine &Line) {
22 const FormatToken *Next = Line.First->getNextNonComment();
23 const FormatToken *NextNext = Next ? Next->getNextNonComment() : nullptr;
24 return Line.First->is(tok::kw_extern) && Next && Next->isStringLiteral() &&
25 NextNext && NextNext->is(tok::l_brace);
26}
27
28class LineJoiner {
29public:
Nico Weberfac23712015-02-04 15:26:27 +000030 LineJoiner(const FormatStyle &Style, const AdditionalKeywords &Keywords)
31 : Style(Style), Keywords(Keywords) {}
Daniel Jasper0df50932014-12-10 19:00:42 +000032
33 /// \brief Calculates how many lines can be merged into 1 starting at \p I.
34 unsigned
35 tryFitMultipleLinesInOne(unsigned Indent,
36 SmallVectorImpl<AnnotatedLine *>::const_iterator I,
37 SmallVectorImpl<AnnotatedLine *>::const_iterator E) {
Daniel Jasper9ecb0e92015-03-13 13:32:11 +000038 // Can't join the last line with anything.
39 if (I + 1 == E)
40 return 0;
Daniel Jasper0df50932014-12-10 19:00:42 +000041 // We can never merge stuff if there are trailing line comments.
42 const AnnotatedLine *TheLine = *I;
43 if (TheLine->Last->is(TT_LineComment))
44 return 0;
Daniel Jasper9ecb0e92015-03-13 13:32:11 +000045 if (I[1]->Type == LT_Invalid || I[1]->First->MustBreakBefore)
46 return 0;
47 if (TheLine->InPPDirective &&
48 (!I[1]->InPPDirective || I[1]->First->HasUnescapedNewline))
49 return 0;
Daniel Jasper0df50932014-12-10 19:00:42 +000050
51 if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit)
52 return 0;
53
54 unsigned Limit =
55 Style.ColumnLimit == 0 ? UINT_MAX : Style.ColumnLimit - Indent;
56 // If we already exceed the column limit, we set 'Limit' to 0. The different
57 // tryMerge..() functions can then decide whether to still do merging.
58 Limit = TheLine->Last->TotalLength > Limit
59 ? 0
60 : Limit - TheLine->Last->TotalLength;
61
Daniel Jasper0df50932014-12-10 19:00:42 +000062 // FIXME: TheLine->Level != 0 might or might not be the right check to do.
63 // If necessary, change to something smarter.
64 bool MergeShortFunctions =
65 Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_All ||
66 (Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Empty &&
67 I[1]->First->is(tok::r_brace)) ||
68 (Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_Inline &&
69 TheLine->Level != 0);
70
71 if (TheLine->Last->is(TT_FunctionLBrace) &&
72 TheLine->First != TheLine->Last) {
73 return MergeShortFunctions ? tryMergeSimpleBlock(I, E, Limit) : 0;
74 }
75 if (TheLine->Last->is(tok::l_brace)) {
76 return Style.BreakBeforeBraces == FormatStyle::BS_Attach
77 ? tryMergeSimpleBlock(I, E, Limit)
78 : 0;
79 }
80 if (I[1]->First->is(TT_FunctionLBrace) &&
81 Style.BreakBeforeBraces != FormatStyle::BS_Attach) {
82 if (I[1]->Last->is(TT_LineComment))
83 return 0;
84
85 // Check for Limit <= 2 to account for the " {".
86 if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(TheLine)))
87 return 0;
88 Limit -= 2;
89
90 unsigned MergedLines = 0;
91 if (MergeShortFunctions) {
92 MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
93 // If we managed to merge the block, count the function header, which is
94 // on a separate line.
95 if (MergedLines > 0)
96 ++MergedLines;
97 }
98 return MergedLines;
99 }
100 if (TheLine->First->is(tok::kw_if)) {
101 return Style.AllowShortIfStatementsOnASingleLine
102 ? tryMergeSimpleControlStatement(I, E, Limit)
103 : 0;
104 }
105 if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while)) {
106 return Style.AllowShortLoopsOnASingleLine
107 ? tryMergeSimpleControlStatement(I, E, Limit)
108 : 0;
109 }
110 if (TheLine->First->isOneOf(tok::kw_case, tok::kw_default)) {
111 return Style.AllowShortCaseLabelsOnASingleLine
112 ? tryMergeShortCaseLabels(I, E, Limit)
113 : 0;
114 }
115 if (TheLine->InPPDirective &&
116 (TheLine->First->HasUnescapedNewline || TheLine->First->IsFirst)) {
117 return tryMergeSimplePPDirective(I, E, Limit);
118 }
119 return 0;
120 }
121
122private:
123 unsigned
124 tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
125 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
126 unsigned Limit) {
127 if (Limit == 0)
128 return 0;
Daniel Jasper0df50932014-12-10 19:00:42 +0000129 if (I + 2 != E && I[2]->InPPDirective && !I[2]->First->HasUnescapedNewline)
130 return 0;
131 if (1 + I[1]->Last->TotalLength > Limit)
132 return 0;
133 return 1;
134 }
135
136 unsigned tryMergeSimpleControlStatement(
137 SmallVectorImpl<AnnotatedLine *>::const_iterator I,
138 SmallVectorImpl<AnnotatedLine *>::const_iterator E, unsigned Limit) {
139 if (Limit == 0)
140 return 0;
141 if ((Style.BreakBeforeBraces == FormatStyle::BS_Allman ||
142 Style.BreakBeforeBraces == FormatStyle::BS_GNU) &&
143 (I[1]->First->is(tok::l_brace) && !Style.AllowShortBlocksOnASingleLine))
144 return 0;
145 if (I[1]->InPPDirective != (*I)->InPPDirective ||
146 (I[1]->InPPDirective && I[1]->First->HasUnescapedNewline))
147 return 0;
148 Limit = limitConsideringMacros(I + 1, E, Limit);
149 AnnotatedLine &Line = **I;
150 if (Line.Last->isNot(tok::r_paren))
151 return 0;
152 if (1 + I[1]->Last->TotalLength > Limit)
153 return 0;
Manuel Klimekd3585db2015-05-11 08:21:35 +0000154 if (I[1]->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, tok::kw_while,
155 TT_LineComment))
Daniel Jasper0df50932014-12-10 19:00:42 +0000156 return 0;
157 // Only inline simple if's (no nested if or else).
158 if (I + 2 != E && Line.First->is(tok::kw_if) &&
159 I[2]->First->is(tok::kw_else))
160 return 0;
161 return 1;
162 }
163
Manuel Klimekd3585db2015-05-11 08:21:35 +0000164 unsigned
165 tryMergeShortCaseLabels(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
166 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
167 unsigned Limit) {
Daniel Jasper0df50932014-12-10 19:00:42 +0000168 if (Limit == 0 || I + 1 == E ||
169 I[1]->First->isOneOf(tok::kw_case, tok::kw_default))
170 return 0;
171 unsigned NumStmts = 0;
172 unsigned Length = 0;
173 bool InPPDirective = I[0]->InPPDirective;
174 for (; NumStmts < 3; ++NumStmts) {
175 if (I + 1 + NumStmts == E)
176 break;
177 const AnnotatedLine *Line = I[1 + NumStmts];
178 if (Line->InPPDirective != InPPDirective)
179 break;
180 if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))
181 break;
182 if (Line->First->isOneOf(tok::kw_if, tok::kw_for, tok::kw_switch,
183 tok::kw_while, tok::comment))
184 return 0;
185 Length += I[1 + NumStmts]->Last->TotalLength + 1; // 1 for the space.
186 }
187 if (NumStmts == 0 || NumStmts == 3 || Length > Limit)
188 return 0;
189 return NumStmts;
190 }
191
192 unsigned
193 tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
194 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
195 unsigned Limit) {
196 AnnotatedLine &Line = **I;
197
198 // Don't merge ObjC @ keywords and methods.
Nico Weber33381f52015-02-07 01:57:32 +0000199 // FIXME: If an option to allow short exception handling clauses on a single
200 // line is added, change this to not return for @try and friends.
Daniel Jasper0df50932014-12-10 19:00:42 +0000201 if (Style.Language != FormatStyle::LK_Java &&
202 Line.First->isOneOf(tok::at, tok::minus, tok::plus))
203 return 0;
204
205 // Check that the current line allows merging. This depends on whether we
206 // are in a control flow statements as well as several style flags.
Daniel Jaspere9f53572015-04-30 09:24:17 +0000207 if (Line.First->isOneOf(tok::kw_else, tok::kw_case) ||
208 (Line.First->Next && Line.First->Next->is(tok::kw_else)))
Daniel Jasper0df50932014-12-10 19:00:42 +0000209 return 0;
210 if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::kw_try,
Nico Weberfac23712015-02-04 15:26:27 +0000211 tok::kw___try, tok::kw_catch, tok::kw___finally,
212 tok::kw_for, tok::r_brace) ||
213 Line.First->is(Keywords.kw___except)) {
Daniel Jasper0df50932014-12-10 19:00:42 +0000214 if (!Style.AllowShortBlocksOnASingleLine)
215 return 0;
216 if (!Style.AllowShortIfStatementsOnASingleLine &&
217 Line.First->is(tok::kw_if))
218 return 0;
219 if (!Style.AllowShortLoopsOnASingleLine &&
220 Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for))
221 return 0;
222 // FIXME: Consider an option to allow short exception handling clauses on
223 // a single line.
Nico Weberfac23712015-02-04 15:26:27 +0000224 // FIXME: This isn't covered by tests.
225 // FIXME: For catch, __except, __finally the first token on the line
226 // is '}', so this isn't correct here.
227 if (Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch,
228 Keywords.kw___except, tok::kw___finally))
Daniel Jasper0df50932014-12-10 19:00:42 +0000229 return 0;
230 }
231
232 FormatToken *Tok = I[1]->First;
233 if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore &&
234 (Tok->getNextNonComment() == nullptr ||
235 Tok->getNextNonComment()->is(tok::semi))) {
236 // We merge empty blocks even if the line exceeds the column limit.
237 Tok->SpacesRequiredBefore = 0;
238 Tok->CanBreakBefore = true;
239 return 1;
240 } else if (Limit != 0 && Line.First->isNot(tok::kw_namespace) &&
241 !startsExternCBlock(Line)) {
242 // We don't merge short records.
Daniel Jasper29647492015-05-05 08:12:50 +0000243 if (Line.First->isOneOf(tok::kw_class, tok::kw_union, tok::kw_struct,
244 Keywords.kw_interface))
Daniel Jasper0df50932014-12-10 19:00:42 +0000245 return 0;
246
247 // Check that we still have three lines and they fit into the limit.
248 if (I + 2 == E || I[2]->Type == LT_Invalid)
249 return 0;
250 Limit = limitConsideringMacros(I + 2, E, Limit);
251
252 if (!nextTwoLinesFitInto(I, Limit))
253 return 0;
254
255 // Second, check that the next line does not contain any braces - if it
256 // does, readability declines when putting it into a single line.
257 if (I[1]->Last->is(TT_LineComment))
258 return 0;
259 do {
260 if (Tok->is(tok::l_brace) && Tok->BlockKind != BK_BracedInit)
261 return 0;
262 Tok = Tok->Next;
263 } while (Tok);
264
265 // Last, check that the third line starts with a closing brace.
266 Tok = I[2]->First;
267 if (Tok->isNot(tok::r_brace))
268 return 0;
269
Daniel Jaspere9f53572015-04-30 09:24:17 +0000270 // Don't merge "if (a) { .. } else {".
271 if (Tok->Next && Tok->Next->is(tok::kw_else))
272 return 0;
273
Daniel Jasper0df50932014-12-10 19:00:42 +0000274 return 2;
275 }
276 return 0;
277 }
278
279 /// Returns the modified column limit for \p I if it is inside a macro and
280 /// needs a trailing '\'.
281 unsigned
282 limitConsideringMacros(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
283 SmallVectorImpl<AnnotatedLine *>::const_iterator E,
284 unsigned Limit) {
285 if (I[0]->InPPDirective && I + 1 != E &&
286 !I[1]->First->HasUnescapedNewline && !I[1]->First->is(tok::eof)) {
287 return Limit < 2 ? 0 : Limit - 2;
288 }
289 return Limit;
290 }
291
292 bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
293 unsigned Limit) {
294 if (I[1]->First->MustBreakBefore || I[2]->First->MustBreakBefore)
295 return false;
296 return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <= Limit;
297 }
298
299 bool containsMustBreak(const AnnotatedLine *Line) {
300 for (const FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
301 if (Tok->MustBreakBefore)
302 return true;
303 }
304 return false;
305 }
306
307 const FormatStyle &Style;
Nico Weberfac23712015-02-04 15:26:27 +0000308 const AdditionalKeywords &Keywords;
Daniel Jasper0df50932014-12-10 19:00:42 +0000309};
310
Daniel Jasperd1c13732015-01-23 19:37:25 +0000311static void markFinalized(FormatToken *Tok) {
312 for (; Tok; Tok = Tok->Next) {
313 Tok->Finalized = true;
314 for (AnnotatedLine *Child : Tok->Children)
315 markFinalized(Child->First);
316 }
317}
318
Manuel Klimekd3585db2015-05-11 08:21:35 +0000319#ifndef NDEBUG
320static void printLineState(const LineState &State) {
321 llvm::dbgs() << "State: ";
322 for (const ParenState &P : State.Stack) {
323 llvm::dbgs() << P.Indent << "|" << P.LastSpace << "|" << P.NestedBlockIndent
324 << " ";
325 }
326 llvm::dbgs() << State.NextToken->TokenText << "\n";
327}
328#endif
329
330/// \brief Base class for classes that format one \c AnnotatedLine.
331class LineFormatter {
332public:
333 LineFormatter(ContinuationIndenter *Indenter, WhitespaceManager *Whitespaces,
334 const FormatStyle &Style,
335 UnwrappedLineFormatter *BlockFormatter)
336 : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style),
337 BlockFormatter(BlockFormatter) {}
338 virtual ~LineFormatter() {}
339
340 /// \brief Formats an \c AnnotatedLine and returns the penalty.
341 ///
342 /// If \p DryRun is \c false, directly applies the changes.
343 virtual unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
344 bool DryRun) = 0;
345
346protected:
347 /// \brief If the \p State's next token is an r_brace closing a nested block,
348 /// format the nested block before it.
349 ///
350 /// Returns \c true if all children could be placed successfully and adapts
351 /// \p Penalty as well as \p State. If \p DryRun is false, also directly
352 /// creates changes using \c Whitespaces.
353 ///
354 /// The crucial idea here is that children always get formatted upon
355 /// encountering the closing brace right after the nested block. Now, if we
356 /// are currently trying to keep the "}" on the same line (i.e. \p NewLine is
357 /// \c false), the entire block has to be kept on the same line (which is only
358 /// possible if it fits on the line, only contains a single statement, etc.
359 ///
360 /// If \p NewLine is true, we format the nested block on separate lines, i.e.
361 /// break after the "{", format all lines with correct indentation and the put
362 /// the closing "}" on yet another new line.
363 ///
364 /// This enables us to keep the simple structure of the
365 /// \c UnwrappedLineFormatter, where we only have two options for each token:
366 /// break or don't break.
367 bool formatChildren(LineState &State, bool NewLine, bool DryRun,
368 unsigned &Penalty) {
369 const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
370 FormatToken &Previous = *State.NextToken->Previous;
371 if (!LBrace || LBrace->isNot(tok::l_brace) ||
372 LBrace->BlockKind != BK_Block || Previous.Children.size() == 0)
373 // The previous token does not open a block. Nothing to do. We don't
374 // assert so that we can simply call this function for all tokens.
375 return true;
376
377 if (NewLine) {
378 int AdditionalIndent = State.Stack.back().Indent -
379 Previous.Children[0]->Level * Style.IndentWidth;
380
381 Penalty +=
382 BlockFormatter->format(Previous.Children, DryRun, AdditionalIndent,
383 /*FixBadIndentation=*/true);
384 return true;
385 }
386
387 if (Previous.Children[0]->First->MustBreakBefore)
388 return false;
389
390 // Cannot merge multiple statements into a single line.
391 if (Previous.Children.size() > 1)
392 return false;
393
394 // Cannot merge into one line if this line ends on a comment.
395 if (Previous.is(tok::comment))
396 return false;
397
398 // We can't put the closing "}" on a line with a trailing comment.
399 if (Previous.Children[0]->Last->isTrailingComment())
400 return false;
401
402 // If the child line exceeds the column limit, we wouldn't want to merge it.
403 // We add +2 for the trailing " }".
404 if (Style.ColumnLimit > 0 &&
405 Previous.Children[0]->Last->TotalLength + State.Column + 2 >
406 Style.ColumnLimit)
407 return false;
408
409 if (!DryRun) {
410 Whitespaces->replaceWhitespace(
411 *Previous.Children[0]->First,
412 /*Newlines=*/0, /*IndentLevel=*/0, /*Spaces=*/1,
413 /*StartOfTokenColumn=*/State.Column, State.Line->InPPDirective);
414 }
415 Penalty += formatLine(*Previous.Children[0], State.Column + 1, DryRun);
416
417 State.Column += 1 + Previous.Children[0]->Last->TotalLength;
418 return true;
419 }
420
421 ContinuationIndenter *Indenter;
422
423private:
424 WhitespaceManager *Whitespaces;
425 const FormatStyle &Style;
426 UnwrappedLineFormatter *BlockFormatter;
427};
428
429/// \brief Formatter that keeps the existing line breaks.
430class NoColumnLimitLineFormatter : public LineFormatter {
431public:
432 NoColumnLimitLineFormatter(ContinuationIndenter *Indenter,
433 WhitespaceManager *Whitespaces,
434 const FormatStyle &Style,
435 UnwrappedLineFormatter *BlockFormatter)
436 : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {}
437
438 /// \brief Formats the line, simply keeping all of the input's line breaking
439 /// decisions.
440 unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
441 bool DryRun) override {
442 assert(!DryRun);
443 LineState State =
444 Indenter->getInitialState(FirstIndent, &Line, /*DryRun=*/false);
445 while (State.NextToken) {
446 bool Newline =
447 Indenter->mustBreak(State) ||
448 (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);
449 unsigned Penalty = 0;
450 formatChildren(State, Newline, /*DryRun=*/false, Penalty);
451 Indenter->addTokenToState(State, Newline, /*DryRun=*/false);
452 }
453 return 0;
454 }
455};
456
457/// \brief Formatter that puts all tokens into a single line without breaks.
458class NoLineBreakFormatter : public LineFormatter {
459public:
460 NoLineBreakFormatter(ContinuationIndenter *Indenter,
461 WhitespaceManager *Whitespaces, const FormatStyle &Style,
462 UnwrappedLineFormatter *BlockFormatter)
463 : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {}
464
465 /// \brief Puts all tokens into a single line.
466 unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
467 bool DryRun) {
468 unsigned Penalty = 0;
469 LineState State = Indenter->getInitialState(FirstIndent, &Line, DryRun);
470 while (State.NextToken) {
471 formatChildren(State, /*Newline=*/false, DryRun, Penalty);
472 Indenter->addTokenToState(State, /*Newline=*/false, DryRun);
473 }
474 return Penalty;
475 }
476};
477
478/// \brief Finds the best way to break lines.
479class OptimizingLineFormatter : public LineFormatter {
480public:
481 OptimizingLineFormatter(ContinuationIndenter *Indenter,
482 WhitespaceManager *Whitespaces,
483 const FormatStyle &Style,
484 UnwrappedLineFormatter *BlockFormatter)
485 : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {}
486
487 /// \brief Formats the line by finding the best line breaks with line lengths
488 /// below the column limit.
489 unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
490 bool DryRun) {
491 LineState State = Indenter->getInitialState(FirstIndent, &Line, DryRun);
492
493 // If the ObjC method declaration does not fit on a line, we should format
494 // it with one arg per line.
495 if (State.Line->Type == LT_ObjCMethodDecl)
496 State.Stack.back().BreakBeforeParameter = true;
497
498 // Find best solution in solution space.
499 return analyzeSolutionSpace(State, DryRun);
500 }
501
502private:
503 struct CompareLineStatePointers {
504 bool operator()(LineState *obj1, LineState *obj2) const {
505 return *obj1 < *obj2;
506 }
507 };
508
509 /// \brief A pair of <penalty, count> that is used to prioritize the BFS on.
510 ///
511 /// In case of equal penalties, we want to prefer states that were inserted
512 /// first. During state generation we make sure that we insert states first
513 /// that break the line as late as possible.
514 typedef std::pair<unsigned, unsigned> OrderedPenalty;
515
516 /// \brief An edge in the solution space from \c Previous->State to \c State,
517 /// inserting a newline dependent on the \c NewLine.
518 struct StateNode {
519 StateNode(const LineState &State, bool NewLine, StateNode *Previous)
520 : State(State), NewLine(NewLine), Previous(Previous) {}
521 LineState State;
522 bool NewLine;
523 StateNode *Previous;
524 };
525
526 /// \brief An item in the prioritized BFS search queue. The \c StateNode's
527 /// \c State has the given \c OrderedPenalty.
528 typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
529
530 /// \brief The BFS queue type.
531 typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
532 std::greater<QueueItem>> QueueType;
533
534 /// \brief Analyze the entire solution space starting from \p InitialState.
535 ///
536 /// This implements a variant of Dijkstra's algorithm on the graph that spans
537 /// the solution space (\c LineStates are the nodes). The algorithm tries to
538 /// find the shortest path (the one with lowest penalty) from \p InitialState
539 /// to a state where all tokens are placed. Returns the penalty.
540 ///
541 /// If \p DryRun is \c false, directly applies the changes.
542 unsigned analyzeSolutionSpace(LineState &InitialState, bool DryRun) {
543 std::set<LineState *, CompareLineStatePointers> Seen;
544
545 // Increasing count of \c StateNode items we have created. This is used to
546 // create a deterministic order independent of the container.
547 unsigned Count = 0;
548 QueueType Queue;
549
550 // Insert start element into queue.
551 StateNode *Node =
552 new (Allocator.Allocate()) StateNode(InitialState, false, nullptr);
553 Queue.push(QueueItem(OrderedPenalty(0, Count), Node));
554 ++Count;
555
556 unsigned Penalty = 0;
557
558 // While not empty, take first element and follow edges.
559 while (!Queue.empty()) {
560 Penalty = Queue.top().first.first;
561 StateNode *Node = Queue.top().second;
562 if (!Node->State.NextToken) {
563 DEBUG(llvm::dbgs() << "\n---\nPenalty for line: " << Penalty << "\n");
564 break;
565 }
566 Queue.pop();
567
568 // Cut off the analysis of certain solutions if the analysis gets too
569 // complex. See description of IgnoreStackForComparison.
570 if (Count > 10000)
571 Node->State.IgnoreStackForComparison = true;
572
573 if (!Seen.insert(&Node->State).second)
574 // State already examined with lower penalty.
575 continue;
576
577 FormatDecision LastFormat = Node->State.NextToken->Decision;
578 if (LastFormat == FD_Unformatted || LastFormat == FD_Continue)
579 addNextStateToQueue(Penalty, Node, /*NewLine=*/false, &Count, &Queue);
580 if (LastFormat == FD_Unformatted || LastFormat == FD_Break)
581 addNextStateToQueue(Penalty, Node, /*NewLine=*/true, &Count, &Queue);
582 }
583
584 if (Queue.empty()) {
585 // We were unable to find a solution, do nothing.
586 // FIXME: Add diagnostic?
587 DEBUG(llvm::dbgs() << "Could not find a solution.\n");
588 return 0;
589 }
590
591 // Reconstruct the solution.
592 if (!DryRun)
593 reconstructPath(InitialState, Queue.top().second);
594
595 DEBUG(llvm::dbgs() << "Total number of analyzed states: " << Count << "\n");
596 DEBUG(llvm::dbgs() << "---\n");
597
598 return Penalty;
599 }
600
601 /// \brief Add the following state to the analysis queue \c Queue.
602 ///
603 /// Assume the current state is \p PreviousNode and has been reached with a
604 /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
605 void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,
606 bool NewLine, unsigned *Count, QueueType *Queue) {
607 if (NewLine && !Indenter->canBreak(PreviousNode->State))
608 return;
609 if (!NewLine && Indenter->mustBreak(PreviousNode->State))
610 return;
611
612 StateNode *Node = new (Allocator.Allocate())
613 StateNode(PreviousNode->State, NewLine, PreviousNode);
614 if (!formatChildren(Node->State, NewLine, /*DryRun=*/true, Penalty))
615 return;
616
617 Penalty += Indenter->addTokenToState(Node->State, NewLine, true);
618
619 Queue->push(QueueItem(OrderedPenalty(Penalty, *Count), Node));
620 ++(*Count);
621 }
622
623 /// \brief Applies the best formatting by reconstructing the path in the
624 /// solution space that leads to \c Best.
625 void reconstructPath(LineState &State, StateNode *Best) {
626 std::deque<StateNode *> Path;
627 // We do not need a break before the initial token.
628 while (Best->Previous) {
629 Path.push_front(Best);
630 Best = Best->Previous;
631 }
632 for (std::deque<StateNode *>::iterator I = Path.begin(), E = Path.end();
633 I != E; ++I) {
634 unsigned Penalty = 0;
635 formatChildren(State, (*I)->NewLine, /*DryRun=*/false, Penalty);
636 Penalty += Indenter->addTokenToState(State, (*I)->NewLine, false);
637
638 DEBUG({
639 printLineState((*I)->Previous->State);
640 if ((*I)->NewLine) {
641 llvm::dbgs() << "Penalty for placing "
642 << (*I)->Previous->State.NextToken->Tok.getName() << ": "
643 << Penalty << "\n";
644 }
645 });
646 }
647 }
648
649 llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
650};
651
Daniel Jasper0df50932014-12-10 19:00:42 +0000652} // namespace
653
654unsigned
655UnwrappedLineFormatter::format(const SmallVectorImpl<AnnotatedLine *> &Lines,
656 bool DryRun, int AdditionalIndent,
657 bool FixBadIndentation) {
Nico Weberfac23712015-02-04 15:26:27 +0000658 LineJoiner Joiner(Style, Keywords);
Daniel Jasper0df50932014-12-10 19:00:42 +0000659
660 // Try to look up already computed penalty in DryRun-mode.
661 std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned> CacheKey(
662 &Lines, AdditionalIndent);
663 auto CacheIt = PenaltyCache.find(CacheKey);
664 if (DryRun && CacheIt != PenaltyCache.end())
665 return CacheIt->second;
666
667 assert(!Lines.empty());
668 unsigned Penalty = 0;
669 std::vector<int> IndentForLevel;
670 for (unsigned i = 0, e = Lines[0]->Level; i != e; ++i)
671 IndentForLevel.push_back(Style.IndentWidth * i + AdditionalIndent);
672 const AnnotatedLine *PreviousLine = nullptr;
673 for (SmallVectorImpl<AnnotatedLine *>::const_iterator I = Lines.begin(),
674 E = Lines.end();
675 I != E; ++I) {
676 const AnnotatedLine &TheLine = **I;
677 const FormatToken *FirstTok = TheLine.First;
678 int Offset = getIndentOffset(*FirstTok);
679
680 // Determine indent and try to merge multiple unwrapped lines.
681 unsigned Indent;
682 if (TheLine.InPPDirective) {
683 Indent = TheLine.Level * Style.IndentWidth;
684 } else {
685 while (IndentForLevel.size() <= TheLine.Level)
686 IndentForLevel.push_back(-1);
687 IndentForLevel.resize(TheLine.Level + 1);
688 Indent = getIndent(IndentForLevel, TheLine.Level);
689 }
690 unsigned LevelIndent = Indent;
691 if (static_cast<int>(Indent) + Offset >= 0)
692 Indent += Offset;
693
694 // Merge multiple lines if possible.
695 unsigned MergedLines = Joiner.tryFitMultipleLinesInOne(Indent, I, E);
696 if (MergedLines > 0 && Style.ColumnLimit == 0) {
697 // Disallow line merging if there is a break at the start of one of the
698 // input lines.
699 for (unsigned i = 0; i < MergedLines; ++i) {
700 if (I[i + 1]->First->NewlinesBefore > 0)
701 MergedLines = 0;
702 }
703 }
704 if (!DryRun) {
705 for (unsigned i = 0; i < MergedLines; ++i) {
706 join(*I[i], *I[i + 1]);
707 }
708 }
709 I += MergedLines;
710
711 bool FixIndentation =
712 FixBadIndentation && (LevelIndent != FirstTok->OriginalColumn);
Manuel Klimekec5c3db2015-05-07 12:26:30 +0000713 bool ShouldFormat = TheLine.Affected || FixIndentation;
Daniel Jasper0df50932014-12-10 19:00:42 +0000714 if (TheLine.First->is(tok::eof)) {
715 if (PreviousLine && PreviousLine->Affected && !DryRun) {
716 // Remove the file's trailing whitespace.
717 unsigned Newlines = std::min(FirstTok->NewlinesBefore, 1u);
718 Whitespaces->replaceWhitespace(*TheLine.First, Newlines,
719 /*IndentLevel=*/0, /*Spaces=*/0,
720 /*TargetColumn=*/0);
721 }
Manuel Klimekec5c3db2015-05-07 12:26:30 +0000722 } else if (TheLine.Type != LT_Invalid && ShouldFormat) {
Daniel Jasper0df50932014-12-10 19:00:42 +0000723 if (FirstTok->WhitespaceRange.isValid()) {
724 if (!DryRun)
725 formatFirstToken(*TheLine.First, PreviousLine, TheLine.Level, Indent,
726 TheLine.InPPDirective);
727 } else {
728 Indent = LevelIndent = FirstTok->OriginalColumn;
729 }
730
731 // If everything fits on a single line, just put it there.
732 unsigned ColumnLimit = Style.ColumnLimit;
733 if (I + 1 != E) {
734 AnnotatedLine *NextLine = I[1];
735 if (NextLine->InPPDirective && !NextLine->First->HasUnescapedNewline)
736 ColumnLimit = getColumnLimit(TheLine.InPPDirective);
737 }
738
739 if (TheLine.Last->TotalLength + Indent <= ColumnLimit ||
740 TheLine.Type == LT_ImportStatement) {
Manuel Klimekd3585db2015-05-11 08:21:35 +0000741 Penalty += NoLineBreakFormatter(Indenter, Whitespaces, Style, this)
742 .formatLine(TheLine, Indent, DryRun);
Daniel Jasper0df50932014-12-10 19:00:42 +0000743 } else if (Style.ColumnLimit == 0) {
Manuel Klimekd3585db2015-05-11 08:21:35 +0000744 NoColumnLimitLineFormatter(Indenter, Whitespaces, Style, this)
745 .formatLine(TheLine, Indent, DryRun);
Daniel Jasper0df50932014-12-10 19:00:42 +0000746 } else {
Manuel Klimekd3585db2015-05-11 08:21:35 +0000747 Penalty += OptimizingLineFormatter(Indenter, Whitespaces, Style, this)
748 .formatLine(TheLine, Indent, DryRun);
Daniel Jasper0df50932014-12-10 19:00:42 +0000749 }
750
751 if (!TheLine.InPPDirective)
752 IndentForLevel[TheLine.Level] = LevelIndent;
753 } else if (TheLine.ChildrenAffected) {
754 format(TheLine.Children, DryRun);
755 } else {
756 // Format the first token if necessary, and notify the WhitespaceManager
757 // about the unchanged whitespace.
758 for (FormatToken *Tok = TheLine.First; Tok; Tok = Tok->Next) {
759 if (Tok == TheLine.First && (Tok->NewlinesBefore > 0 || Tok->IsFirst)) {
760 unsigned LevelIndent = Tok->OriginalColumn;
761 if (!DryRun) {
762 // Remove trailing whitespace of the previous line.
763 if ((PreviousLine && PreviousLine->Affected) ||
764 TheLine.LeadingEmptyLinesAffected) {
765 formatFirstToken(*Tok, PreviousLine, TheLine.Level, LevelIndent,
766 TheLine.InPPDirective);
767 } else {
768 Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective);
769 }
770 }
771
772 if (static_cast<int>(LevelIndent) - Offset >= 0)
773 LevelIndent -= Offset;
Daniel Jaspereb45cb72015-04-29 08:29:26 +0000774 if ((Tok->isNot(tok::comment) ||
775 IndentForLevel[TheLine.Level] == -1) &&
776 !TheLine.InPPDirective)
Daniel Jasper0df50932014-12-10 19:00:42 +0000777 IndentForLevel[TheLine.Level] = LevelIndent;
778 } else if (!DryRun) {
779 Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective);
780 }
781 }
782 }
Manuel Klimekec5c3db2015-05-07 12:26:30 +0000783 if (TheLine.Type == LT_Invalid && ShouldFormat && IncompleteFormat)
784 *IncompleteFormat = true;
Daniel Jasperd1c13732015-01-23 19:37:25 +0000785 if (!DryRun)
786 markFinalized(TheLine.First);
Daniel Jasper0df50932014-12-10 19:00:42 +0000787 PreviousLine = *I;
788 }
789 PenaltyCache[CacheKey] = Penalty;
790 return Penalty;
791}
792
Daniel Jasper0df50932014-12-10 19:00:42 +0000793void UnwrappedLineFormatter::formatFirstToken(FormatToken &RootToken,
794 const AnnotatedLine *PreviousLine,
795 unsigned IndentLevel,
796 unsigned Indent,
797 bool InPPDirective) {
798 unsigned Newlines =
799 std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
800 // Remove empty lines before "}" where applicable.
801 if (RootToken.is(tok::r_brace) &&
802 (!RootToken.Next ||
803 (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)))
804 Newlines = std::min(Newlines, 1u);
805 if (Newlines == 0 && !RootToken.IsFirst)
806 Newlines = 1;
807 if (RootToken.IsFirst && !RootToken.HasUnescapedNewline)
808 Newlines = 0;
809
810 // Remove empty lines after "{".
811 if (!Style.KeepEmptyLinesAtTheStartOfBlocks && PreviousLine &&
812 PreviousLine->Last->is(tok::l_brace) &&
813 PreviousLine->First->isNot(tok::kw_namespace) &&
814 !startsExternCBlock(*PreviousLine))
815 Newlines = 1;
816
817 // Insert extra new line before access specifiers.
818 if (PreviousLine && PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) &&
819 RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1)
820 ++Newlines;
821
822 // Remove empty lines after access specifiers.
Daniel Jasperac5c97e32015-03-09 08:13:55 +0000823 if (PreviousLine && PreviousLine->First->isAccessSpecifier() &&
824 (!PreviousLine->InPPDirective || !RootToken.HasUnescapedNewline))
Daniel Jasper0df50932014-12-10 19:00:42 +0000825 Newlines = std::min(1u, Newlines);
826
827 Whitespaces->replaceWhitespace(RootToken, Newlines, IndentLevel, Indent,
828 Indent, InPPDirective &&
829 !RootToken.HasUnescapedNewline);
830}
831
832/// \brief Get the indent of \p Level from \p IndentForLevel.
833///
834/// \p IndentForLevel must contain the indent for the level \c l
835/// at \p IndentForLevel[l], or a value < 0 if the indent for
836/// that level is unknown.
837unsigned UnwrappedLineFormatter::getIndent(ArrayRef<int> IndentForLevel,
838 unsigned Level) {
839 if (IndentForLevel[Level] != -1)
840 return IndentForLevel[Level];
841 if (Level == 0)
842 return 0;
843 return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth;
844}
845
846void UnwrappedLineFormatter::join(AnnotatedLine &A, const AnnotatedLine &B) {
847 assert(!A.Last->Next);
848 assert(!B.First->Previous);
849 if (B.Affected)
850 A.Affected = true;
851 A.Last->Next = B.First;
852 B.First->Previous = A.Last;
853 B.First->CanBreakBefore = true;
854 unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
855 for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
856 Tok->TotalLength += LengthA;
857 A.Last = Tok;
858 }
859}
860
Daniel Jasper0df50932014-12-10 19:00:42 +0000861} // namespace format
862} // namespace clang