clang  10.0.0svn
UnwrappedLineFormatter.cpp
Go to the documentation of this file.
1 //===--- UnwrappedLineFormatter.cpp - Format C++ code ---------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
11 #include "WhitespaceManager.h"
12 #include "llvm/Support/Debug.h"
13 #include <queue>
14 
15 #define DEBUG_TYPE "format-formatter"
16 
17 namespace clang {
18 namespace format {
19 
20 namespace {
21 
22 bool startsExternCBlock(const AnnotatedLine &Line) {
23  const FormatToken *Next = Line.First->getNextNonComment();
24  const FormatToken *NextNext = Next ? Next->getNextNonComment() : nullptr;
25  return Line.startsWith(tok::kw_extern) && Next && Next->isStringLiteral() &&
26  NextNext && NextNext->is(tok::l_brace);
27 }
28 
29 /// Tracks the indent level of \c AnnotatedLines across levels.
30 ///
31 /// \c nextLine must be called for each \c AnnotatedLine, after which \c
32 /// getIndent() will return the indent for the last line \c nextLine was called
33 /// with.
34 /// If the line is not formatted (and thus the indent does not change), calling
35 /// \c adjustToUnmodifiedLine after the call to \c nextLine will cause
36 /// subsequent lines on the same level to be indented at the same level as the
37 /// given line.
38 class LevelIndentTracker {
39 public:
40  LevelIndentTracker(const FormatStyle &Style,
41  const AdditionalKeywords &Keywords, unsigned StartLevel,
42  int AdditionalIndent)
43  : Style(Style), Keywords(Keywords), AdditionalIndent(AdditionalIndent) {
44  for (unsigned i = 0; i != StartLevel; ++i)
45  IndentForLevel.push_back(Style.IndentWidth * i + AdditionalIndent);
46  }
47 
48  /// Returns the indent for the current line.
49  unsigned getIndent() const { return Indent; }
50 
51  /// Update the indent state given that \p Line is going to be formatted
52  /// next.
53  void nextLine(const AnnotatedLine &Line) {
54  Offset = getIndentOffset(*Line.First);
55  // Update the indent level cache size so that we can rely on it
56  // having the right size in adjustToUnmodifiedline.
57  while (IndentForLevel.size() <= Line.Level)
58  IndentForLevel.push_back(-1);
59  if (Line.InPPDirective) {
60  Indent = Line.Level * Style.IndentWidth + AdditionalIndent;
61  } else {
62  IndentForLevel.resize(Line.Level + 1);
63  Indent = getIndent(IndentForLevel, Line.Level);
64  }
65  if (static_cast<int>(Indent) + Offset >= 0)
66  Indent += Offset;
67  }
68 
69  /// Update the indent state given that \p Line indent should be
70  /// skipped.
71  void skipLine(const AnnotatedLine &Line) {
72  while (IndentForLevel.size() <= Line.Level)
73  IndentForLevel.push_back(Indent);
74  }
75 
76  /// Update the level indent to adapt to the given \p Line.
77  ///
78  /// When a line is not formatted, we move the subsequent lines on the same
79  /// level to the same indent.
80  /// Note that \c nextLine must have been called before this method.
81  void adjustToUnmodifiedLine(const AnnotatedLine &Line) {
82  unsigned LevelIndent = Line.First->OriginalColumn;
83  if (static_cast<int>(LevelIndent) - Offset >= 0)
84  LevelIndent -= Offset;
85  if ((!Line.First->is(tok::comment) || IndentForLevel[Line.Level] == -1) &&
86  !Line.InPPDirective)
87  IndentForLevel[Line.Level] = LevelIndent;
88  }
89 
90 private:
91  /// Get the offset of the line relatively to the level.
92  ///
93  /// For example, 'public:' labels in classes are offset by 1 or 2
94  /// characters to the left from their level.
95  int getIndentOffset(const FormatToken &RootToken) {
96  if (Style.Language == FormatStyle::LK_Java ||
97  Style.Language == FormatStyle::LK_JavaScript || Style.isCSharp())
98  return 0;
99  if (RootToken.isAccessSpecifier(false) ||
100  RootToken.isObjCAccessSpecifier() ||
101  (RootToken.isOneOf(Keywords.kw_signals, Keywords.kw_qsignals) &&
102  RootToken.Next && RootToken.Next->is(tok::colon)))
103  return Style.AccessModifierOffset;
104  return 0;
105  }
106 
107  /// Get the indent of \p Level from \p IndentForLevel.
108  ///
109  /// \p IndentForLevel must contain the indent for the level \c l
110  /// at \p IndentForLevel[l], or a value < 0 if the indent for
111  /// that level is unknown.
112  unsigned getIndent(ArrayRef<int> IndentForLevel, unsigned Level) {
113  if (IndentForLevel[Level] != -1)
114  return IndentForLevel[Level];
115  if (Level == 0)
116  return 0;
117  return getIndent(IndentForLevel, Level - 1) + Style.IndentWidth;
118  }
119 
120  const FormatStyle &Style;
121  const AdditionalKeywords &Keywords;
122  const unsigned AdditionalIndent;
123 
124  /// The indent in characters for each level.
125  std::vector<int> IndentForLevel;
126 
127  /// Offset of the current line relative to the indent level.
128  ///
129  /// For example, the 'public' keywords is often indented with a negative
130  /// offset.
131  int Offset = 0;
132 
133  /// The current line's indent.
134  unsigned Indent = 0;
135 };
136 
137 const FormatToken *getMatchingNamespaceToken(
138  const AnnotatedLine *Line,
139  const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
140  if (!Line->startsWith(tok::r_brace))
141  return nullptr;
142  size_t StartLineIndex = Line->MatchingOpeningBlockLineIndex;
143  if (StartLineIndex == UnwrappedLine::kInvalidIndex)
144  return nullptr;
145  assert(StartLineIndex < AnnotatedLines.size());
146  return AnnotatedLines[StartLineIndex]->First->getNamespaceToken();
147 }
148 
149 StringRef getNamespaceTokenText(const AnnotatedLine *Line) {
150  const FormatToken *NamespaceToken = Line->First->getNamespaceToken();
151  return NamespaceToken ? NamespaceToken->TokenText : StringRef();
152 }
153 
154 StringRef getMatchingNamespaceTokenText(
155  const AnnotatedLine *Line,
156  const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines) {
157  const FormatToken *NamespaceToken =
158  getMatchingNamespaceToken(Line, AnnotatedLines);
159  return NamespaceToken ? NamespaceToken->TokenText : StringRef();
160 }
161 
162 class LineJoiner {
163 public:
164  LineJoiner(const FormatStyle &Style, const AdditionalKeywords &Keywords,
165  const SmallVectorImpl<AnnotatedLine *> &Lines)
166  : Style(Style), Keywords(Keywords), End(Lines.end()), Next(Lines.begin()),
167  AnnotatedLines(Lines) {}
168 
169  /// Returns the next line, merging multiple lines into one if possible.
170  const AnnotatedLine *getNextMergedLine(bool DryRun,
171  LevelIndentTracker &IndentTracker) {
172  if (Next == End)
173  return nullptr;
174  const AnnotatedLine *Current = *Next;
175  IndentTracker.nextLine(*Current);
176  unsigned MergedLines = tryFitMultipleLinesInOne(IndentTracker, Next, End);
177  if (MergedLines > 0 && Style.ColumnLimit == 0)
178  // Disallow line merging if there is a break at the start of one of the
179  // input lines.
180  for (unsigned i = 0; i < MergedLines; ++i)
181  if (Next[i + 1]->First->NewlinesBefore > 0)
182  MergedLines = 0;
183  if (!DryRun)
184  for (unsigned i = 0; i < MergedLines; ++i)
185  join(*Next[0], *Next[i + 1]);
186  Next = Next + MergedLines + 1;
187  return Current;
188  }
189 
190 private:
191  /// Calculates how many lines can be merged into 1 starting at \p I.
192  unsigned
193  tryFitMultipleLinesInOne(LevelIndentTracker &IndentTracker,
194  SmallVectorImpl<AnnotatedLine *>::const_iterator I,
195  SmallVectorImpl<AnnotatedLine *>::const_iterator E) {
196  const unsigned Indent = IndentTracker.getIndent();
197 
198  // Can't join the last line with anything.
199  if (I + 1 == E)
200  return 0;
201  // We can never merge stuff if there are trailing line comments.
202  const AnnotatedLine *TheLine = *I;
203  if (TheLine->Last->is(TT_LineComment))
204  return 0;
205  if (I[1]->Type == LT_Invalid || I[1]->First->MustBreakBefore)
206  return 0;
207  if (TheLine->InPPDirective &&
208  (!I[1]->InPPDirective || I[1]->First->HasUnescapedNewline))
209  return 0;
210 
211  if (Style.ColumnLimit > 0 && Indent > Style.ColumnLimit)
212  return 0;
213 
214  unsigned Limit =
215  Style.ColumnLimit == 0 ? UINT_MAX : Style.ColumnLimit - Indent;
216  // If we already exceed the column limit, we set 'Limit' to 0. The different
217  // tryMerge..() functions can then decide whether to still do merging.
218  Limit = TheLine->Last->TotalLength > Limit
219  ? 0
220  : Limit - TheLine->Last->TotalLength;
221 
222  if (TheLine->Last->is(TT_FunctionLBrace) &&
223  TheLine->First == TheLine->Last &&
224  !Style.BraceWrapping.SplitEmptyFunction &&
225  I[1]->First->is(tok::r_brace))
226  return tryMergeSimpleBlock(I, E, Limit);
227 
228  // Handle empty record blocks where the brace has already been wrapped
229  if (TheLine->Last->is(tok::l_brace) && TheLine->First == TheLine->Last &&
230  I != AnnotatedLines.begin()) {
231  bool EmptyBlock = I[1]->First->is(tok::r_brace);
232 
233  const FormatToken *Tok = I[-1]->First;
234  if (Tok && Tok->is(tok::comment))
235  Tok = Tok->getNextNonComment();
236 
237  if (Tok && Tok->getNamespaceToken())
238  return !Style.BraceWrapping.SplitEmptyNamespace && EmptyBlock
239  ? tryMergeSimpleBlock(I, E, Limit)
240  : 0;
241 
242  if (Tok && Tok->is(tok::kw_typedef))
243  Tok = Tok->getNextNonComment();
244  if (Tok && Tok->isOneOf(tok::kw_class, tok::kw_struct, tok::kw_union,
245  tok::kw_extern, Keywords.kw_interface))
246  return !Style.BraceWrapping.SplitEmptyRecord && EmptyBlock
247  ? tryMergeSimpleBlock(I, E, Limit)
248  : 0;
249  }
250 
251  // FIXME: TheLine->Level != 0 might or might not be the right check to do.
252  // If necessary, change to something smarter.
253  bool MergeShortFunctions =
254  Style.AllowShortFunctionsOnASingleLine == FormatStyle::SFS_All ||
255  (Style.AllowShortFunctionsOnASingleLine >= FormatStyle::SFS_Empty &&
256  I[1]->First->is(tok::r_brace)) ||
257  (Style.AllowShortFunctionsOnASingleLine & FormatStyle::SFS_InlineOnly &&
258  TheLine->Level != 0);
259 
260  if (Style.CompactNamespaces) {
261  if (auto nsToken = TheLine->First->getNamespaceToken()) {
262  int i = 0;
263  unsigned closingLine = TheLine->MatchingClosingBlockLineIndex - 1;
264  for (; I + 1 + i != E &&
265  nsToken->TokenText == getNamespaceTokenText(I[i + 1]) &&
266  closingLine == I[i + 1]->MatchingClosingBlockLineIndex &&
267  I[i + 1]->Last->TotalLength < Limit;
268  i++, closingLine--) {
269  // No extra indent for compacted namespaces
270  IndentTracker.skipLine(*I[i + 1]);
271 
272  Limit -= I[i + 1]->Last->TotalLength;
273  }
274  return i;
275  }
276 
277  if (auto nsToken = getMatchingNamespaceToken(TheLine, AnnotatedLines)) {
278  int i = 0;
279  unsigned openingLine = TheLine->MatchingOpeningBlockLineIndex - 1;
280  for (; I + 1 + i != E &&
281  nsToken->TokenText ==
282  getMatchingNamespaceTokenText(I[i + 1], AnnotatedLines) &&
283  openingLine == I[i + 1]->MatchingOpeningBlockLineIndex;
284  i++, openingLine--) {
285  // No space between consecutive braces
286  I[i + 1]->First->SpacesRequiredBefore = !I[i]->Last->is(tok::r_brace);
287 
288  // Indent like the outer-most namespace
289  IndentTracker.nextLine(*I[i + 1]);
290  }
291  return i;
292  }
293  }
294 
295  // Try to merge a function block with left brace unwrapped
296  if (TheLine->Last->is(TT_FunctionLBrace) &&
297  TheLine->First != TheLine->Last) {
298  return MergeShortFunctions ? tryMergeSimpleBlock(I, E, Limit) : 0;
299  }
300  // Try to merge a control statement block with left brace unwrapped
301  if (TheLine->Last->is(tok::l_brace) && TheLine->First != TheLine->Last &&
302  TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for)) {
303  return Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never
304  ? tryMergeSimpleBlock(I, E, Limit)
305  : 0;
306  }
307  // Try to merge a control statement block with left brace wrapped
308  if (I[1]->First->is(tok::l_brace) &&
309  (TheLine->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for,
310  tok::kw_switch, tok::kw_try, tok::kw_do) ||
311  (TheLine->First->is(tok::r_brace) && TheLine->First->Next &&
312  TheLine->First->Next->isOneOf(tok::kw_else, tok::kw_catch))) &&
313  Style.BraceWrapping.AfterControlStatement ==
314  FormatStyle::BWACS_MultiLine) {
315  // If possible, merge the next line's wrapped left brace with the current
316  // line. Otherwise, leave it on the next line, as this is a multi-line
317  // control statement.
318  return (Style.ColumnLimit == 0 ||
319  TheLine->Last->TotalLength <= Style.ColumnLimit)
320  ? 1
321  : 0;
322  } else if (I[1]->First->is(tok::l_brace) &&
323  TheLine->First->isOneOf(tok::kw_if, tok::kw_while,
324  tok::kw_for)) {
325  return (Style.BraceWrapping.AfterControlStatement ==
326  FormatStyle::BWACS_Always)
327  ? tryMergeSimpleBlock(I, E, Limit)
328  : 0;
329  }
330  // Try to merge either empty or one-line block if is precedeed by control
331  // statement token
332  if (TheLine->First->is(tok::l_brace) && TheLine->First == TheLine->Last &&
333  I != AnnotatedLines.begin() &&
334  I[-1]->First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_for)) {
335  unsigned MergedLines = 0;
336  if (Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never) {
337  MergedLines = tryMergeSimpleBlock(I - 1, E, Limit);
338  // If we managed to merge the block, discard the first merged line
339  // since we are merging starting from I.
340  if (MergedLines > 0)
341  --MergedLines;
342  }
343  return MergedLines;
344  }
345  // Don't merge block with left brace wrapped after ObjC special blocks
346  if (TheLine->First->is(tok::l_brace) && I != AnnotatedLines.begin() &&
347  I[-1]->First->is(tok::at) && I[-1]->First->Next) {
348  tok::ObjCKeywordKind kwId = I[-1]->First->Next->Tok.getObjCKeywordID();
349  if (kwId == clang::tok::objc_autoreleasepool ||
350  kwId == clang::tok::objc_synchronized)
351  return 0;
352  }
353  // Don't merge block with left brace wrapped after case labels
354  if (TheLine->First->is(tok::l_brace) && I != AnnotatedLines.begin() &&
355  I[-1]->First->isOneOf(tok::kw_case, tok::kw_default))
356  return 0;
357  // Try to merge a block with left brace wrapped that wasn't yet covered
358  if (TheLine->Last->is(tok::l_brace)) {
359  return !Style.BraceWrapping.AfterFunction ||
360  (I[1]->First->is(tok::r_brace) &&
361  !Style.BraceWrapping.SplitEmptyRecord)
362  ? tryMergeSimpleBlock(I, E, Limit)
363  : 0;
364  }
365  // Try to merge a function block with left brace wrapped
366  if (I[1]->First->is(TT_FunctionLBrace) &&
367  Style.BraceWrapping.AfterFunction) {
368  if (I[1]->Last->is(TT_LineComment))
369  return 0;
370 
371  // Check for Limit <= 2 to account for the " {".
372  if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(TheLine)))
373  return 0;
374  Limit -= 2;
375 
376  unsigned MergedLines = 0;
377  if (MergeShortFunctions ||
378  (Style.AllowShortFunctionsOnASingleLine >= FormatStyle::SFS_Empty &&
379  I[1]->First == I[1]->Last && I + 2 != E &&
380  I[2]->First->is(tok::r_brace))) {
381  MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
382  // If we managed to merge the block, count the function header, which is
383  // on a separate line.
384  if (MergedLines > 0)
385  ++MergedLines;
386  }
387  return MergedLines;
388  }
389  if (TheLine->First->is(tok::kw_if)) {
390  return Style.AllowShortIfStatementsOnASingleLine
391  ? tryMergeSimpleControlStatement(I, E, Limit)
392  : 0;
393  }
394  if (TheLine->First->isOneOf(tok::kw_for, tok::kw_while)) {
395  return Style.AllowShortLoopsOnASingleLine
396  ? tryMergeSimpleControlStatement(I, E, Limit)
397  : 0;
398  }
399  if (TheLine->First->isOneOf(tok::kw_case, tok::kw_default)) {
400  return Style.AllowShortCaseLabelsOnASingleLine
401  ? tryMergeShortCaseLabels(I, E, Limit)
402  : 0;
403  }
404  if (TheLine->InPPDirective &&
405  (TheLine->First->HasUnescapedNewline || TheLine->First->IsFirst)) {
406  return tryMergeSimplePPDirective(I, E, Limit);
407  }
408  return 0;
409  }
410 
411  unsigned
412  tryMergeSimplePPDirective(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
413  SmallVectorImpl<AnnotatedLine *>::const_iterator E,
414  unsigned Limit) {
415  if (Limit == 0)
416  return 0;
417  if (I + 2 != E && I[2]->InPPDirective && !I[2]->First->HasUnescapedNewline)
418  return 0;
419  if (1 + I[1]->Last->TotalLength > Limit)
420  return 0;
421  return 1;
422  }
423 
424  unsigned tryMergeSimpleControlStatement(
425  SmallVectorImpl<AnnotatedLine *>::const_iterator I,
426  SmallVectorImpl<AnnotatedLine *>::const_iterator E, unsigned Limit) {
427  if (Limit == 0)
428  return 0;
429  if (Style.BraceWrapping.AfterControlStatement ==
430  FormatStyle::BWACS_Always &&
431  I[1]->First->is(tok::l_brace) &&
432  Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never)
433  return 0;
434  if (I[1]->InPPDirective != (*I)->InPPDirective ||
435  (I[1]->InPPDirective && I[1]->First->HasUnescapedNewline))
436  return 0;
437  Limit = limitConsideringMacros(I + 1, E, Limit);
438  AnnotatedLine &Line = **I;
439  if (Line.Last->isNot(tok::r_paren))
440  return 0;
441  if (1 + I[1]->Last->TotalLength > Limit)
442  return 0;
443  if (I[1]->First->isOneOf(tok::semi, tok::kw_if, tok::kw_for, tok::kw_while,
444  TT_LineComment))
445  return 0;
446  // Only inline simple if's (no nested if or else), unless specified
447  if (Style.AllowShortIfStatementsOnASingleLine != FormatStyle::SIS_Always) {
448  if (I + 2 != E && Line.startsWith(tok::kw_if) &&
449  I[2]->First->is(tok::kw_else))
450  return 0;
451  }
452  return 1;
453  }
454 
455  unsigned
456  tryMergeShortCaseLabels(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
457  SmallVectorImpl<AnnotatedLine *>::const_iterator E,
458  unsigned Limit) {
459  if (Limit == 0 || I + 1 == E ||
460  I[1]->First->isOneOf(tok::kw_case, tok::kw_default))
461  return 0;
462  if (I[0]->Last->is(tok::l_brace) || I[1]->First->is(tok::l_brace))
463  return 0;
464  unsigned NumStmts = 0;
465  unsigned Length = 0;
466  bool EndsWithComment = false;
467  bool InPPDirective = I[0]->InPPDirective;
468  const unsigned Level = I[0]->Level;
469  for (; NumStmts < 3; ++NumStmts) {
470  if (I + 1 + NumStmts == E)
471  break;
472  const AnnotatedLine *Line = I[1 + NumStmts];
473  if (Line->InPPDirective != InPPDirective)
474  break;
475  if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))
476  break;
477  if (Line->First->isOneOf(tok::kw_if, tok::kw_for, tok::kw_switch,
478  tok::kw_while) ||
479  EndsWithComment)
480  return 0;
481  if (Line->First->is(tok::comment)) {
482  if (Level != Line->Level)
483  return 0;
484  SmallVectorImpl<AnnotatedLine *>::const_iterator J = I + 2 + NumStmts;
485  for (; J != E; ++J) {
486  Line = *J;
487  if (Line->InPPDirective != InPPDirective)
488  break;
489  if (Line->First->isOneOf(tok::kw_case, tok::kw_default, tok::r_brace))
490  break;
491  if (Line->First->isNot(tok::comment) || Level != Line->Level)
492  return 0;
493  }
494  break;
495  }
496  if (Line->Last->is(tok::comment))
497  EndsWithComment = true;
498  Length += I[1 + NumStmts]->Last->TotalLength + 1; // 1 for the space.
499  }
500  if (NumStmts == 0 || NumStmts == 3 || Length > Limit)
501  return 0;
502  return NumStmts;
503  }
504 
505  unsigned
506  tryMergeSimpleBlock(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
507  SmallVectorImpl<AnnotatedLine *>::const_iterator E,
508  unsigned Limit) {
509  AnnotatedLine &Line = **I;
510 
511  // Don't merge ObjC @ keywords and methods.
512  // FIXME: If an option to allow short exception handling clauses on a single
513  // line is added, change this to not return for @try and friends.
514  if (Style.Language != FormatStyle::LK_Java &&
515  Line.First->isOneOf(tok::at, tok::minus, tok::plus))
516  return 0;
517 
518  // Check that the current line allows merging. This depends on whether we
519  // are in a control flow statements as well as several style flags.
520  if (Line.First->isOneOf(tok::kw_else, tok::kw_case) ||
521  (Line.First->Next && Line.First->Next->is(tok::kw_else)))
522  return 0;
523  // default: in switch statement
524  if (Line.First->is(tok::kw_default)) {
525  const FormatToken *Tok = Line.First->getNextNonComment();
526  if (Tok && Tok->is(tok::colon))
527  return 0;
528  }
529  if (Line.First->isOneOf(tok::kw_if, tok::kw_while, tok::kw_do, tok::kw_try,
530  tok::kw___try, tok::kw_catch, tok::kw___finally,
531  tok::kw_for, tok::r_brace, Keywords.kw___except)) {
532  if (Style.AllowShortBlocksOnASingleLine == FormatStyle::SBS_Never)
533  return 0;
534  // Don't merge when we can't except the case when
535  // the control statement block is empty
536  if (!Style.AllowShortIfStatementsOnASingleLine &&
537  Line.startsWith(tok::kw_if) &&
538  !Style.BraceWrapping.AfterControlStatement &&
539  !I[1]->First->is(tok::r_brace))
540  return 0;
541  if (!Style.AllowShortIfStatementsOnASingleLine &&
542  Line.startsWith(tok::kw_if) &&
543  Style.BraceWrapping.AfterControlStatement ==
544  FormatStyle::BWACS_Always &&
545  I + 2 != E && !I[2]->First->is(tok::r_brace))
546  return 0;
547  if (!Style.AllowShortLoopsOnASingleLine &&
548  Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for) &&
549  !Style.BraceWrapping.AfterControlStatement &&
550  !I[1]->First->is(tok::r_brace))
551  return 0;
552  if (!Style.AllowShortLoopsOnASingleLine &&
553  Line.First->isOneOf(tok::kw_while, tok::kw_do, tok::kw_for) &&
554  Style.BraceWrapping.AfterControlStatement ==
555  FormatStyle::BWACS_Always &&
556  I + 2 != E && !I[2]->First->is(tok::r_brace))
557  return 0;
558  // FIXME: Consider an option to allow short exception handling clauses on
559  // a single line.
560  // FIXME: This isn't covered by tests.
561  // FIXME: For catch, __except, __finally the first token on the line
562  // is '}', so this isn't correct here.
563  if (Line.First->isOneOf(tok::kw_try, tok::kw___try, tok::kw_catch,
564  Keywords.kw___except, tok::kw___finally))
565  return 0;
566  }
567 
568  if (Line.Last->is(tok::l_brace)) {
569  FormatToken *Tok = I[1]->First;
570  if (Tok->is(tok::r_brace) && !Tok->MustBreakBefore &&
571  (Tok->getNextNonComment() == nullptr ||
572  Tok->getNextNonComment()->is(tok::semi))) {
573  // We merge empty blocks even if the line exceeds the column limit.
574  Tok->SpacesRequiredBefore = Style.SpaceInEmptyBlock ? 1 : 0;
575  Tok->CanBreakBefore = true;
576  return 1;
577  } else if (Limit != 0 && !Line.startsWithNamespace() &&
578  !startsExternCBlock(Line)) {
579  // We don't merge short records.
580  FormatToken *RecordTok = Line.First;
581  // Skip record modifiers.
582  while (RecordTok->Next &&
583  RecordTok->isOneOf(tok::kw_typedef, tok::kw_export,
584  Keywords.kw_declare, Keywords.kw_abstract,
585  tok::kw_default))
586  RecordTok = RecordTok->Next;
587  if (RecordTok &&
588  RecordTok->isOneOf(tok::kw_class, tok::kw_union, tok::kw_struct,
589  Keywords.kw_interface))
590  return 0;
591 
592  // Check that we still have three lines and they fit into the limit.
593  if (I + 2 == E || I[2]->Type == LT_Invalid)
594  return 0;
595  Limit = limitConsideringMacros(I + 2, E, Limit);
596 
597  if (!nextTwoLinesFitInto(I, Limit))
598  return 0;
599 
600  // Second, check that the next line does not contain any braces - if it
601  // does, readability declines when putting it into a single line.
602  if (I[1]->Last->is(TT_LineComment))
603  return 0;
604  do {
605  if (Tok->is(tok::l_brace) && Tok->BlockKind != BK_BracedInit)
606  return 0;
607  Tok = Tok->Next;
608  } while (Tok);
609 
610  // Last, check that the third line starts with a closing brace.
611  Tok = I[2]->First;
612  if (Tok->isNot(tok::r_brace))
613  return 0;
614 
615  // Don't merge "if (a) { .. } else {".
616  if (Tok->Next && Tok->Next->is(tok::kw_else))
617  return 0;
618 
619  // Don't merge a trailing multi-line control statement block like:
620  // } else if (foo &&
621  // bar)
622  // { <-- current Line
623  // baz();
624  // }
625  if (Line.First == Line.Last &&
626  Style.BraceWrapping.AfterControlStatement ==
627  FormatStyle::BWACS_MultiLine)
628  return 0;
629 
630  return 2;
631  }
632  } else if (I[1]->First->is(tok::l_brace)) {
633  if (I[1]->Last->is(TT_LineComment))
634  return 0;
635 
636  // Check for Limit <= 2 to account for the " {".
637  if (Limit <= 2 || (Style.ColumnLimit == 0 && containsMustBreak(*I)))
638  return 0;
639  Limit -= 2;
640  unsigned MergedLines = 0;
641  if (Style.AllowShortBlocksOnASingleLine != FormatStyle::SBS_Never ||
642  (I[1]->First == I[1]->Last && I + 2 != E &&
643  I[2]->First->is(tok::r_brace))) {
644  MergedLines = tryMergeSimpleBlock(I + 1, E, Limit);
645  // If we managed to merge the block, count the statement header, which
646  // is on a separate line.
647  if (MergedLines > 0)
648  ++MergedLines;
649  }
650  return MergedLines;
651  }
652  return 0;
653  }
654 
655  /// Returns the modified column limit for \p I if it is inside a macro and
656  /// needs a trailing '\'.
657  unsigned
658  limitConsideringMacros(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
659  SmallVectorImpl<AnnotatedLine *>::const_iterator E,
660  unsigned Limit) {
661  if (I[0]->InPPDirective && I + 1 != E &&
662  !I[1]->First->HasUnescapedNewline && !I[1]->First->is(tok::eof)) {
663  return Limit < 2 ? 0 : Limit - 2;
664  }
665  return Limit;
666  }
667 
668  bool nextTwoLinesFitInto(SmallVectorImpl<AnnotatedLine *>::const_iterator I,
669  unsigned Limit) {
670  if (I[1]->First->MustBreakBefore || I[2]->First->MustBreakBefore)
671  return false;
672  return 1 + I[1]->Last->TotalLength + 1 + I[2]->Last->TotalLength <= Limit;
673  }
674 
675  bool containsMustBreak(const AnnotatedLine *Line) {
676  for (const FormatToken *Tok = Line->First; Tok; Tok = Tok->Next) {
677  if (Tok->MustBreakBefore)
678  return true;
679  }
680  return false;
681  }
682 
683  void join(AnnotatedLine &A, const AnnotatedLine &B) {
684  assert(!A.Last->Next);
685  assert(!B.First->Previous);
686  if (B.Affected)
687  A.Affected = true;
688  A.Last->Next = B.First;
689  B.First->Previous = A.Last;
690  B.First->CanBreakBefore = true;
691  unsigned LengthA = A.Last->TotalLength + B.First->SpacesRequiredBefore;
692  for (FormatToken *Tok = B.First; Tok; Tok = Tok->Next) {
693  Tok->TotalLength += LengthA;
694  A.Last = Tok;
695  }
696  }
697 
698  const FormatStyle &Style;
699  const AdditionalKeywords &Keywords;
700  const SmallVectorImpl<AnnotatedLine *>::const_iterator End;
701 
702  SmallVectorImpl<AnnotatedLine *>::const_iterator Next;
703  const SmallVectorImpl<AnnotatedLine *> &AnnotatedLines;
704 };
705 
706 static void markFinalized(FormatToken *Tok) {
707  for (; Tok; Tok = Tok->Next) {
708  Tok->Finalized = true;
709  for (AnnotatedLine *Child : Tok->Children)
710  markFinalized(Child->First);
711  }
712 }
713 
714 #ifndef NDEBUG
715 static void printLineState(const LineState &State) {
716  llvm::dbgs() << "State: ";
717  for (const ParenState &P : State.Stack) {
718  llvm::dbgs() << (P.Tok ? P.Tok->TokenText : "F") << "|" << P.Indent << "|"
719  << P.LastSpace << "|" << P.NestedBlockIndent << " ";
720  }
721  llvm::dbgs() << State.NextToken->TokenText << "\n";
722 }
723 #endif
724 
725 /// Base class for classes that format one \c AnnotatedLine.
726 class LineFormatter {
727 public:
728  LineFormatter(ContinuationIndenter *Indenter, WhitespaceManager *Whitespaces,
729  const FormatStyle &Style,
730  UnwrappedLineFormatter *BlockFormatter)
731  : Indenter(Indenter), Whitespaces(Whitespaces), Style(Style),
732  BlockFormatter(BlockFormatter) {}
733  virtual ~LineFormatter() {}
734 
735  /// Formats an \c AnnotatedLine and returns the penalty.
736  ///
737  /// If \p DryRun is \c false, directly applies the changes.
738  virtual unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
739  unsigned FirstStartColumn, bool DryRun) = 0;
740 
741 protected:
742  /// If the \p State's next token is an r_brace closing a nested block,
743  /// format the nested block before it.
744  ///
745  /// Returns \c true if all children could be placed successfully and adapts
746  /// \p Penalty as well as \p State. If \p DryRun is false, also directly
747  /// creates changes using \c Whitespaces.
748  ///
749  /// The crucial idea here is that children always get formatted upon
750  /// encountering the closing brace right after the nested block. Now, if we
751  /// are currently trying to keep the "}" on the same line (i.e. \p NewLine is
752  /// \c false), the entire block has to be kept on the same line (which is only
753  /// possible if it fits on the line, only contains a single statement, etc.
754  ///
755  /// If \p NewLine is true, we format the nested block on separate lines, i.e.
756  /// break after the "{", format all lines with correct indentation and the put
757  /// the closing "}" on yet another new line.
758  ///
759  /// This enables us to keep the simple structure of the
760  /// \c UnwrappedLineFormatter, where we only have two options for each token:
761  /// break or don't break.
762  bool formatChildren(LineState &State, bool NewLine, bool DryRun,
763  unsigned &Penalty) {
764  const FormatToken *LBrace = State.NextToken->getPreviousNonComment();
765  FormatToken &Previous = *State.NextToken->Previous;
766  if (!LBrace || LBrace->isNot(tok::l_brace) ||
767  LBrace->BlockKind != BK_Block || Previous.Children.size() == 0)
768  // The previous token does not open a block. Nothing to do. We don't
769  // assert so that we can simply call this function for all tokens.
770  return true;
771 
772  if (NewLine) {
773  int AdditionalIndent = State.Stack.back().Indent -
774  Previous.Children[0]->Level * Style.IndentWidth;
775 
776  Penalty +=
777  BlockFormatter->format(Previous.Children, DryRun, AdditionalIndent,
778  /*FixBadIndentation=*/true);
779  return true;
780  }
781 
782  if (Previous.Children[0]->First->MustBreakBefore)
783  return false;
784 
785  // Cannot merge into one line if this line ends on a comment.
786  if (Previous.is(tok::comment))
787  return false;
788 
789  // Cannot merge multiple statements into a single line.
790  if (Previous.Children.size() > 1)
791  return false;
792 
793  const AnnotatedLine *Child = Previous.Children[0];
794  // We can't put the closing "}" on a line with a trailing comment.
795  if (Child->Last->isTrailingComment())
796  return false;
797 
798  // If the child line exceeds the column limit, we wouldn't want to merge it.
799  // We add +2 for the trailing " }".
800  if (Style.ColumnLimit > 0 &&
801  Child->Last->TotalLength + State.Column + 2 > Style.ColumnLimit)
802  return false;
803 
804  if (!DryRun) {
805  Whitespaces->replaceWhitespace(
806  *Child->First, /*Newlines=*/0, /*Spaces=*/1,
807  /*StartOfTokenColumn=*/State.Column, State.Line->InPPDirective);
808  }
809  Penalty +=
810  formatLine(*Child, State.Column + 1, /*FirstStartColumn=*/0, DryRun);
811 
812  State.Column += 1 + Child->Last->TotalLength;
813  return true;
814  }
815 
816  ContinuationIndenter *Indenter;
817 
818 private:
819  WhitespaceManager *Whitespaces;
820  const FormatStyle &Style;
821  UnwrappedLineFormatter *BlockFormatter;
822 };
823 
824 /// Formatter that keeps the existing line breaks.
825 class NoColumnLimitLineFormatter : public LineFormatter {
826 public:
827  NoColumnLimitLineFormatter(ContinuationIndenter *Indenter,
828  WhitespaceManager *Whitespaces,
829  const FormatStyle &Style,
830  UnwrappedLineFormatter *BlockFormatter)
831  : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {}
832 
833  /// Formats the line, simply keeping all of the input's line breaking
834  /// decisions.
835  unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
836  unsigned FirstStartColumn, bool DryRun) override {
837  assert(!DryRun);
838  LineState State = Indenter->getInitialState(FirstIndent, FirstStartColumn,
839  &Line, /*DryRun=*/false);
840  while (State.NextToken) {
841  bool Newline =
842  Indenter->mustBreak(State) ||
843  (Indenter->canBreak(State) && State.NextToken->NewlinesBefore > 0);
844  unsigned Penalty = 0;
845  formatChildren(State, Newline, /*DryRun=*/false, Penalty);
846  Indenter->addTokenToState(State, Newline, /*DryRun=*/false);
847  }
848  return 0;
849  }
850 };
851 
852 /// Formatter that puts all tokens into a single line without breaks.
853 class NoLineBreakFormatter : public LineFormatter {
854 public:
855  NoLineBreakFormatter(ContinuationIndenter *Indenter,
856  WhitespaceManager *Whitespaces, const FormatStyle &Style,
857  UnwrappedLineFormatter *BlockFormatter)
858  : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {}
859 
860  /// Puts all tokens into a single line.
861  unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
862  unsigned FirstStartColumn, bool DryRun) override {
863  unsigned Penalty = 0;
864  LineState State =
865  Indenter->getInitialState(FirstIndent, FirstStartColumn, &Line, DryRun);
866  while (State.NextToken) {
867  formatChildren(State, /*NewLine=*/false, DryRun, Penalty);
868  Indenter->addTokenToState(
869  State, /*Newline=*/State.NextToken->MustBreakBefore, DryRun);
870  }
871  return Penalty;
872  }
873 };
874 
875 /// Finds the best way to break lines.
876 class OptimizingLineFormatter : public LineFormatter {
877 public:
878  OptimizingLineFormatter(ContinuationIndenter *Indenter,
879  WhitespaceManager *Whitespaces,
880  const FormatStyle &Style,
881  UnwrappedLineFormatter *BlockFormatter)
882  : LineFormatter(Indenter, Whitespaces, Style, BlockFormatter) {}
883 
884  /// Formats the line by finding the best line breaks with line lengths
885  /// below the column limit.
886  unsigned formatLine(const AnnotatedLine &Line, unsigned FirstIndent,
887  unsigned FirstStartColumn, bool DryRun) override {
888  LineState State =
889  Indenter->getInitialState(FirstIndent, FirstStartColumn, &Line, DryRun);
890 
891  // If the ObjC method declaration does not fit on a line, we should format
892  // it with one arg per line.
893  if (State.Line->Type == LT_ObjCMethodDecl)
894  State.Stack.back().BreakBeforeParameter = true;
895 
896  // Find best solution in solution space.
897  return analyzeSolutionSpace(State, DryRun);
898  }
899 
900 private:
901  struct CompareLineStatePointers {
902  bool operator()(LineState *obj1, LineState *obj2) const {
903  return *obj1 < *obj2;
904  }
905  };
906 
907  /// A pair of <penalty, count> that is used to prioritize the BFS on.
908  ///
909  /// In case of equal penalties, we want to prefer states that were inserted
910  /// first. During state generation we make sure that we insert states first
911  /// that break the line as late as possible.
912  typedef std::pair<unsigned, unsigned> OrderedPenalty;
913 
914  /// An edge in the solution space from \c Previous->State to \c State,
915  /// inserting a newline dependent on the \c NewLine.
916  struct StateNode {
917  StateNode(const LineState &State, bool NewLine, StateNode *Previous)
918  : State(State), NewLine(NewLine), Previous(Previous) {}
919  LineState State;
920  bool NewLine;
921  StateNode *Previous;
922  };
923 
924  /// An item in the prioritized BFS search queue. The \c StateNode's
925  /// \c State has the given \c OrderedPenalty.
926  typedef std::pair<OrderedPenalty, StateNode *> QueueItem;
927 
928  /// The BFS queue type.
929  typedef std::priority_queue<QueueItem, std::vector<QueueItem>,
930  std::greater<QueueItem>>
931  QueueType;
932 
933  /// Analyze the entire solution space starting from \p InitialState.
934  ///
935  /// This implements a variant of Dijkstra's algorithm on the graph that spans
936  /// the solution space (\c LineStates are the nodes). The algorithm tries to
937  /// find the shortest path (the one with lowest penalty) from \p InitialState
938  /// to a state where all tokens are placed. Returns the penalty.
939  ///
940  /// If \p DryRun is \c false, directly applies the changes.
941  unsigned analyzeSolutionSpace(LineState &InitialState, bool DryRun) {
942  std::set<LineState *, CompareLineStatePointers> Seen;
943 
944  // Increasing count of \c StateNode items we have created. This is used to
945  // create a deterministic order independent of the container.
946  unsigned Count = 0;
947  QueueType Queue;
948 
949  // Insert start element into queue.
950  StateNode *Node =
951  new (Allocator.Allocate()) StateNode(InitialState, false, nullptr);
952  Queue.push(QueueItem(OrderedPenalty(0, Count), Node));
953  ++Count;
954 
955  unsigned Penalty = 0;
956 
957  // While not empty, take first element and follow edges.
958  while (!Queue.empty()) {
959  Penalty = Queue.top().first.first;
960  StateNode *Node = Queue.top().second;
961  if (!Node->State.NextToken) {
962  LLVM_DEBUG(llvm::dbgs()
963  << "\n---\nPenalty for line: " << Penalty << "\n");
964  break;
965  }
966  Queue.pop();
967 
968  // Cut off the analysis of certain solutions if the analysis gets too
969  // complex. See description of IgnoreStackForComparison.
970  if (Count > 50000)
971  Node->State.IgnoreStackForComparison = true;
972 
973  if (!Seen.insert(&Node->State).second)
974  // State already examined with lower penalty.
975  continue;
976 
977  FormatDecision LastFormat = Node->State.NextToken->Decision;
978  if (LastFormat == FD_Unformatted || LastFormat == FD_Continue)
979  addNextStateToQueue(Penalty, Node, /*NewLine=*/false, &Count, &Queue);
980  if (LastFormat == FD_Unformatted || LastFormat == FD_Break)
981  addNextStateToQueue(Penalty, Node, /*NewLine=*/true, &Count, &Queue);
982  }
983 
984  if (Queue.empty()) {
985  // We were unable to find a solution, do nothing.
986  // FIXME: Add diagnostic?
987  LLVM_DEBUG(llvm::dbgs() << "Could not find a solution.\n");
988  return 0;
989  }
990 
991  // Reconstruct the solution.
992  if (!DryRun)
993  reconstructPath(InitialState, Queue.top().second);
994 
995  LLVM_DEBUG(llvm::dbgs()
996  << "Total number of analyzed states: " << Count << "\n");
997  LLVM_DEBUG(llvm::dbgs() << "---\n");
998 
999  return Penalty;
1000  }
1001 
1002  /// Add the following state to the analysis queue \c Queue.
1003  ///
1004  /// Assume the current state is \p PreviousNode and has been reached with a
1005  /// penalty of \p Penalty. Insert a line break if \p NewLine is \c true.
1006  void addNextStateToQueue(unsigned Penalty, StateNode *PreviousNode,
1007  bool NewLine, unsigned *Count, QueueType *Queue) {
1008  if (NewLine && !Indenter->canBreak(PreviousNode->State))
1009  return;
1010  if (!NewLine && Indenter->mustBreak(PreviousNode->State))
1011  return;
1012 
1013  StateNode *Node = new (Allocator.Allocate())
1014  StateNode(PreviousNode->State, NewLine, PreviousNode);
1015  if (!formatChildren(Node->State, NewLine, /*DryRun=*/true, Penalty))
1016  return;
1017 
1018  Penalty += Indenter->addTokenToState(Node->State, NewLine, true);
1019 
1020  Queue->push(QueueItem(OrderedPenalty(Penalty, *Count), Node));
1021  ++(*Count);
1022  }
1023 
1024  /// Applies the best formatting by reconstructing the path in the
1025  /// solution space that leads to \c Best.
1026  void reconstructPath(LineState &State, StateNode *Best) {
1027  std::deque<StateNode *> Path;
1028  // We do not need a break before the initial token.
1029  while (Best->Previous) {
1030  Path.push_front(Best);
1031  Best = Best->Previous;
1032  }
1033  for (auto I = Path.begin(), E = Path.end(); I != E; ++I) {
1034  unsigned Penalty = 0;
1035  formatChildren(State, (*I)->NewLine, /*DryRun=*/false, Penalty);
1036  Penalty += Indenter->addTokenToState(State, (*I)->NewLine, false);
1037 
1038  LLVM_DEBUG({
1039  printLineState((*I)->Previous->State);
1040  if ((*I)->NewLine) {
1041  llvm::dbgs() << "Penalty for placing "
1042  << (*I)->Previous->State.NextToken->Tok.getName()
1043  << " on a new line: " << Penalty << "\n";
1044  }
1045  });
1046  }
1047  }
1048 
1049  llvm::SpecificBumpPtrAllocator<StateNode> Allocator;
1050 };
1051 
1052 } // anonymous namespace
1053 
1055  const SmallVectorImpl<AnnotatedLine *> &Lines, bool DryRun,
1056  int AdditionalIndent, bool FixBadIndentation, unsigned FirstStartColumn,
1057  unsigned NextStartColumn, unsigned LastStartColumn) {
1058  LineJoiner Joiner(Style, Keywords, Lines);
1059 
1060  // Try to look up already computed penalty in DryRun-mode.
1061  std::pair<const SmallVectorImpl<AnnotatedLine *> *, unsigned> CacheKey(
1062  &Lines, AdditionalIndent);
1063  auto CacheIt = PenaltyCache.find(CacheKey);
1064  if (DryRun && CacheIt != PenaltyCache.end())
1065  return CacheIt->second;
1066 
1067  assert(!Lines.empty());
1068  unsigned Penalty = 0;
1069  LevelIndentTracker IndentTracker(Style, Keywords, Lines[0]->Level,
1070  AdditionalIndent);
1071  const AnnotatedLine *PreviousLine = nullptr;
1072  const AnnotatedLine *NextLine = nullptr;
1073 
1074  // The minimum level of consecutive lines that have been formatted.
1075  unsigned RangeMinLevel = UINT_MAX;
1076 
1077  bool FirstLine = true;
1078  for (const AnnotatedLine *Line =
1079  Joiner.getNextMergedLine(DryRun, IndentTracker);
1080  Line; Line = NextLine, FirstLine = false) {
1081  const AnnotatedLine &TheLine = *Line;
1082  unsigned Indent = IndentTracker.getIndent();
1083 
1084  // We continue formatting unchanged lines to adjust their indent, e.g. if a
1085  // scope was added. However, we need to carefully stop doing this when we
1086  // exit the scope of affected lines to prevent indenting a the entire
1087  // remaining file if it currently missing a closing brace.
1088  bool PreviousRBrace =
1089  PreviousLine && PreviousLine->startsWith(tok::r_brace);
1090  bool ContinueFormatting =
1091  TheLine.Level > RangeMinLevel ||
1092  (TheLine.Level == RangeMinLevel && !PreviousRBrace &&
1093  !TheLine.startsWith(tok::r_brace));
1094 
1095  bool FixIndentation = (FixBadIndentation || ContinueFormatting) &&
1096  Indent != TheLine.First->OriginalColumn;
1097  bool ShouldFormat = TheLine.Affected || FixIndentation;
1098  // We cannot format this line; if the reason is that the line had a
1099  // parsing error, remember that.
1100  if (ShouldFormat && TheLine.Type == LT_Invalid && Status) {
1101  Status->FormatComplete = false;
1102  Status->Line =
1103  SourceMgr.getSpellingLineNumber(TheLine.First->Tok.getLocation());
1104  }
1105 
1106  if (ShouldFormat && TheLine.Type != LT_Invalid) {
1107  if (!DryRun) {
1108  bool LastLine = Line->First->is(tok::eof);
1109  formatFirstToken(TheLine, PreviousLine, Lines, Indent,
1110  LastLine ? LastStartColumn : NextStartColumn + Indent);
1111  }
1112 
1113  NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
1114  unsigned ColumnLimit = getColumnLimit(TheLine.InPPDirective, NextLine);
1115  bool FitsIntoOneLine =
1116  TheLine.Last->TotalLength + Indent <= ColumnLimit ||
1117  (TheLine.Type == LT_ImportStatement &&
1118  (Style.Language != FormatStyle::LK_JavaScript ||
1119  !Style.JavaScriptWrapImports)) ||
1120  (Style.isCSharp() &&
1121  TheLine.InPPDirective); // don't split #regions in C#
1122  if (Style.ColumnLimit == 0)
1123  NoColumnLimitLineFormatter(Indenter, Whitespaces, Style, this)
1124  .formatLine(TheLine, NextStartColumn + Indent,
1125  FirstLine ? FirstStartColumn : 0, DryRun);
1126  else if (FitsIntoOneLine)
1127  Penalty += NoLineBreakFormatter(Indenter, Whitespaces, Style, this)
1128  .formatLine(TheLine, NextStartColumn + Indent,
1129  FirstLine ? FirstStartColumn : 0, DryRun);
1130  else
1131  Penalty += OptimizingLineFormatter(Indenter, Whitespaces, Style, this)
1132  .formatLine(TheLine, NextStartColumn + Indent,
1133  FirstLine ? FirstStartColumn : 0, DryRun);
1134  RangeMinLevel = std::min(RangeMinLevel, TheLine.Level);
1135  } else {
1136  // If no token in the current line is affected, we still need to format
1137  // affected children.
1138  if (TheLine.ChildrenAffected)
1139  for (const FormatToken *Tok = TheLine.First; Tok; Tok = Tok->Next)
1140  if (!Tok->Children.empty())
1141  format(Tok->Children, DryRun);
1142 
1143  // Adapt following lines on the current indent level to the same level
1144  // unless the current \c AnnotatedLine is not at the beginning of a line.
1145  bool StartsNewLine =
1146  TheLine.First->NewlinesBefore > 0 || TheLine.First->IsFirst;
1147  if (StartsNewLine)
1148  IndentTracker.adjustToUnmodifiedLine(TheLine);
1149  if (!DryRun) {
1150  bool ReformatLeadingWhitespace =
1151  StartsNewLine && ((PreviousLine && PreviousLine->Affected) ||
1152  TheLine.LeadingEmptyLinesAffected);
1153  // Format the first token.
1154  if (ReformatLeadingWhitespace)
1155  formatFirstToken(TheLine, PreviousLine, Lines,
1156  TheLine.First->OriginalColumn,
1157  TheLine.First->OriginalColumn);
1158  else
1159  Whitespaces->addUntouchableToken(*TheLine.First,
1160  TheLine.InPPDirective);
1161 
1162  // Notify the WhitespaceManager about the unchanged whitespace.
1163  for (FormatToken *Tok = TheLine.First->Next; Tok; Tok = Tok->Next)
1164  Whitespaces->addUntouchableToken(*Tok, TheLine.InPPDirective);
1165  }
1166  NextLine = Joiner.getNextMergedLine(DryRun, IndentTracker);
1167  RangeMinLevel = UINT_MAX;
1168  }
1169  if (!DryRun)
1170  markFinalized(TheLine.First);
1171  PreviousLine = &TheLine;
1172  }
1173  PenaltyCache[CacheKey] = Penalty;
1174  return Penalty;
1175 }
1176 
1177 void UnwrappedLineFormatter::formatFirstToken(
1178  const AnnotatedLine &Line, const AnnotatedLine *PreviousLine,
1179  const SmallVectorImpl<AnnotatedLine *> &Lines, unsigned Indent,
1180  unsigned NewlineIndent) {
1181  FormatToken &RootToken = *Line.First;
1182  if (RootToken.is(tok::eof)) {
1183  unsigned Newlines = std::min(RootToken.NewlinesBefore, 1u);
1184  unsigned TokenIndent = Newlines ? NewlineIndent : 0;
1185  Whitespaces->replaceWhitespace(RootToken, Newlines, TokenIndent,
1186  TokenIndent);
1187  return;
1188  }
1189  unsigned Newlines =
1190  std::min(RootToken.NewlinesBefore, Style.MaxEmptyLinesToKeep + 1);
1191  // Remove empty lines before "}" where applicable.
1192  if (RootToken.is(tok::r_brace) &&
1193  (!RootToken.Next ||
1194  (RootToken.Next->is(tok::semi) && !RootToken.Next->Next)) &&
1195  // Do not remove empty lines before namespace closing "}".
1196  !getNamespaceToken(&Line, Lines))
1197  Newlines = std::min(Newlines, 1u);
1198  // Remove empty lines at the start of nested blocks (lambdas/arrow functions)
1199  if (PreviousLine == nullptr && Line.Level > 0)
1200  Newlines = std::min(Newlines, 1u);
1201  if (Newlines == 0 && !RootToken.IsFirst)
1202  Newlines = 1;
1203  if (RootToken.IsFirst && !RootToken.HasUnescapedNewline)
1204  Newlines = 0;
1205 
1206  // Remove empty lines after "{".
1207  if (!Style.KeepEmptyLinesAtTheStartOfBlocks && PreviousLine &&
1208  PreviousLine->Last->is(tok::l_brace) &&
1209  !PreviousLine->startsWithNamespace() &&
1210  !startsExternCBlock(*PreviousLine))
1211  Newlines = 1;
1212 
1213  // Insert extra new line before access specifiers.
1214  if (PreviousLine && PreviousLine->Last->isOneOf(tok::semi, tok::r_brace) &&
1215  RootToken.isAccessSpecifier() && RootToken.NewlinesBefore == 1)
1216  ++Newlines;
1217 
1218  // Remove empty lines after access specifiers.
1219  if (PreviousLine && PreviousLine->First->isAccessSpecifier() &&
1220  (!PreviousLine->InPPDirective || !RootToken.HasUnescapedNewline))
1221  Newlines = std::min(1u, Newlines);
1222 
1223  if (Newlines)
1224  Indent = NewlineIndent;
1225 
1226  // If in Whitemsmiths mode, indent start and end of blocks
1227  if (Style.BreakBeforeBraces == FormatStyle::BS_Whitesmiths) {
1228  if (RootToken.isOneOf(tok::l_brace, tok::r_brace, tok::kw_case))
1229  Indent += Style.IndentWidth;
1230  }
1231 
1232  // Preprocessor directives get indented before the hash only if specified
1233  if (Style.IndentPPDirectives != FormatStyle::PPDIS_BeforeHash &&
1234  (Line.Type == LT_PreprocessorDirective ||
1235  Line.Type == LT_ImportStatement))
1236  Indent = 0;
1237 
1238  Whitespaces->replaceWhitespace(RootToken, Newlines, Indent, Indent,
1239  Line.InPPDirective &&
1240  !RootToken.HasUnescapedNewline);
1241 }
1242 
1243 unsigned
1244 UnwrappedLineFormatter::getColumnLimit(bool InPPDirective,
1245  const AnnotatedLine *NextLine) const {
1246  // In preprocessor directives reserve two chars for trailing " \" if the
1247  // next line continues the preprocessor directive.
1248  bool ContinuesPPDirective =
1249  InPPDirective &&
1250  // If there is no next line, this is likely a child line and the parent
1251  // continues the preprocessor directive.
1252  (!NextLine ||
1253  (NextLine->InPPDirective &&
1254  // If there is an unescaped newline between this line and the next, the
1255  // next line starts a new preprocessor directive.
1256  !NextLine->First->HasUnescapedNewline));
1257  return Style.ColumnLimit - (ContinuesPPDirective ? 2 : 0);
1258 }
1259 
1260 } // namespace format
1261 } // namespace clang
unsigned format(const SmallVectorImpl< AnnotatedLine *> &Lines, bool DryRun=false, int AdditionalIndent=0, bool FixBadIndentation=false, unsigned FirstStartColumn=0, unsigned NextStartColumn=0, unsigned LastStartColumn=0)
Format the current block and return the penalty.
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T -> getSizeExpr()))
Token Tok
The Token.
Definition: FormatToken.h:133
StringRef getNamespaceTokenText(const AnnotatedLine *Line, const SmallVectorImpl< AnnotatedLine *> &AnnotatedLines)
unsigned OriginalColumn
The original 0-based column of this token, including expanded tabs.
Definition: FormatToken.h:220
StringRef P
const FormatToken * getNextNonComment() const
Returns the next token ignoring comments.
Definition: FormatToken.h:495
bool LeadingEmptyLinesAffected
True if the leading empty lines of this line intersect with one of the input ranges.
bool IsFirst
Indicates that this is the first token of the file.
Definition: FormatToken.h:165
bool isAccessSpecifier(bool ColonRequired=true) const
Definition: FormatToken.h:367
unsigned TotalLength
The total length of the unwrapped line up to and including this token.
Definition: FormatToken.h:216
unsigned NewlinesBefore
The number of newlines immediately before the Token.
Definition: FormatToken.h:139
FormatToken * Next
The next token in the unwrapped line.
Definition: FormatToken.h:298
LineState State
const FormatToken * getNamespaceToken(const AnnotatedLine *Line, const SmallVectorImpl< AnnotatedLine *> &AnnotatedLines)
unsigned SpacesRequiredBefore
The number of spaces that should be inserted before this token.
Definition: FormatToken.h:188
const FormatToken & Tok
bool ChildrenAffected
True if one of this line&#39;s children intersects with an input range.
#define UINT_MAX
Definition: limits.h:56
bool isOneOf(A K1, B K2) const
Definition: FormatToken.h:321
WhitespaceManager class manages whitespace around tokens and their replacements.
unsigned Offset
Definition: Format.cpp:1809
SourceLocation End
ContinuationIndenter * Indenter
const AnnotatedLine * Line
StateNode * Previous
static const size_t kInvalidIndex
SourceLocation getLocation() const
Return a source location identifier for the specified offset in the current file. ...
Definition: Token.h:126
A wrapper around a Token storing information about the whitespace characters preceding it...
Definition: FormatToken.h:129
Implements a combinartorial exploration of all the different linebreaks unwrapped lines can be format...
ObjCKeywordKind
Provides a namespace for Objective-C keywords which start with an &#39;@&#39;.
Definition: TokenKinds.h:40
bool Affected
True if this line should be formatted, i.e.
bool is(tok::TokenKind Kind) const
Definition: FormatToken.h:312
const bool InPPDirective
bool startsWith(Ts... Tokens) const
true if this line starts with the given tokens in order, ignoring comments.
ast_type_traits::DynTypedNode Node
Optional< types::ID > Type
Dataflow Directional Tag Classes.
bool startsWithNamespace() const
true if this line starts a namespace definition.
SmallVector< AnnotatedLine *, 1 > Children
If this token starts a block, this contains all the unwrapped lines in it.
Definition: FormatToken.h:302
static void skipLine(const char *&First, const char *const End)
__DEVICE__ int min(int __a, int __b)
bool MustBreakBefore
Whether there must be a line break before this token.
Definition: FormatToken.h:171
raw_ostream & Indent(raw_ostream &Out, const unsigned int Space, bool IsDot)
Definition: JsonSupport.h:20
This file declares NamespaceEndCommentsFixer, a TokenAnalyzer that fixes namespace end comments...
bool HasUnescapedNewline
Whether there is at least one unescaped newline before the Token.
Definition: FormatToken.h:143
const FormatStyle & Style