clang  3.8.0
WhitespaceManager.cpp
Go to the documentation of this file.
1 //===--- WhitespaceManager.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 /// \file
11 /// \brief This file implements WhitespaceManager class.
12 ///
13 //===----------------------------------------------------------------------===//
14 
15 #include "WhitespaceManager.h"
16 #include "llvm/ADT/STLExtras.h"
17 
18 namespace clang {
19 namespace format {
20 
22 operator()(const Change &C1, const Change &C2) const {
23  return SourceMgr.isBeforeInTranslationUnit(
26 }
27 
30  unsigned IndentLevel, int Spaces, unsigned StartOfTokenColumn,
31  unsigned NewlinesBefore, StringRef PreviousLinePostfix,
34  : CreateReplacement(CreateReplacement),
35  OriginalWhitespaceRange(OriginalWhitespaceRange),
36  StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore),
37  PreviousLinePostfix(PreviousLinePostfix),
38  CurrentLinePrefix(CurrentLinePrefix), Kind(Kind),
39  ContinuesPPDirective(ContinuesPPDirective),
40  IsStartOfDeclName(IsStartOfDeclName), IndentLevel(IndentLevel),
41  Spaces(Spaces), IsInsideToken(IsInsideToken), IsTrailingComment(false),
44 
46  Changes.clear();
47  Replaces.clear();
48 }
49 
51  unsigned IndentLevel, unsigned Spaces,
52  unsigned StartOfTokenColumn,
53  bool InPPDirective) {
54  if (Tok.Finalized)
55  return;
56  Tok.Decision = (Newlines > 0) ? FD_Break : FD_Continue;
57  Changes.push_back(
58  Change(/*CreateReplacement=*/true, Tok.WhitespaceRange, IndentLevel,
59  Spaces, StartOfTokenColumn, Newlines, "", "", Tok.Tok.getKind(),
60  InPPDirective && !Tok.IsFirst,
61  Tok.is(TT_StartOfName) || Tok.is(TT_FunctionDeclarationName),
62  /*IsInsideToken=*/false));
63 }
64 
66  bool InPPDirective) {
67  if (Tok.Finalized)
68  return;
69  Changes.push_back(Change(
70  /*CreateReplacement=*/false, Tok.WhitespaceRange, /*IndentLevel=*/0,
71  /*Spaces=*/0, Tok.OriginalColumn, Tok.NewlinesBefore, "", "",
72  Tok.Tok.getKind(), InPPDirective && !Tok.IsFirst,
73  Tok.is(TT_StartOfName) || Tok.is(TT_FunctionDeclarationName),
74  /*IsInsideToken=*/false));
75 }
76 
78  const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars,
79  StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective,
80  unsigned Newlines, unsigned IndentLevel, int Spaces) {
81  if (Tok.Finalized)
82  return;
84  Changes.push_back(Change(
85  true, SourceRange(Start, Start.getLocWithOffset(ReplaceChars)),
86  IndentLevel, Spaces, std::max(0, Spaces), Newlines, PreviousPostfix,
87  CurrentPrefix, Tok.is(TT_LineComment) ? tok::comment : tok::unknown,
88  InPPDirective && !Tok.IsFirst,
89  Tok.is(TT_StartOfName) || Tok.is(TT_FunctionDeclarationName),
90  /*IsInsideToken=*/Newlines == 0));
91 }
92 
94  if (Changes.empty())
95  return Replaces;
96 
97  std::sort(Changes.begin(), Changes.end(), Change::IsBeforeInFile(SourceMgr));
98  calculateLineBreakInformation();
99  alignConsecutiveDeclarations();
100  alignConsecutiveAssignments();
101  alignTrailingComments();
102  alignEscapedNewlines();
103  generateChanges();
104 
105  return Replaces;
106 }
107 
108 void WhitespaceManager::calculateLineBreakInformation() {
109  Changes[0].PreviousEndOfTokenColumn = 0;
110  Change *LastOutsideTokenChange = &Changes[0];
111  for (unsigned i = 1, e = Changes.size(); i != e; ++i) {
112  unsigned OriginalWhitespaceStart =
113  SourceMgr.getFileOffset(Changes[i].OriginalWhitespaceRange.getBegin());
114  unsigned PreviousOriginalWhitespaceEnd = SourceMgr.getFileOffset(
115  Changes[i - 1].OriginalWhitespaceRange.getEnd());
116  Changes[i - 1].TokenLength = OriginalWhitespaceStart -
117  PreviousOriginalWhitespaceEnd +
118  Changes[i].PreviousLinePostfix.size() +
119  Changes[i - 1].CurrentLinePrefix.size();
120 
121  // If there are multiple changes in this token, sum up all the changes until
122  // the end of the line.
123  if (Changes[i - 1].IsInsideToken)
124  LastOutsideTokenChange->TokenLength +=
125  Changes[i - 1].TokenLength + Changes[i - 1].Spaces;
126  else
127  LastOutsideTokenChange = &Changes[i - 1];
128 
129  Changes[i].PreviousEndOfTokenColumn =
130  Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength;
131 
132  Changes[i - 1].IsTrailingComment =
133  (Changes[i].NewlinesBefore > 0 || Changes[i].Kind == tok::eof ||
134  (Changes[i].IsInsideToken && Changes[i].Kind == tok::comment)) &&
135  Changes[i - 1].Kind == tok::comment;
136  }
137  // FIXME: The last token is currently not always an eof token; in those
138  // cases, setting TokenLength of the last token to 0 is wrong.
139  Changes.back().TokenLength = 0;
140  Changes.back().IsTrailingComment = Changes.back().Kind == tok::comment;
141 
142  const WhitespaceManager::Change *LastBlockComment = nullptr;
143  for (auto &Change : Changes) {
144  // Reset the IsTrailingComment flag for changes inside of trailing comments
145  // so they don't get realigned later.
146  if (Change.IsInsideToken)
147  Change.IsTrailingComment = false;
148  Change.StartOfBlockComment = nullptr;
149  Change.IndentationOffset = 0;
150  if (Change.Kind == tok::comment) {
151  LastBlockComment = &Change;
152  } else if (Change.Kind == tok::unknown) {
153  if ((Change.StartOfBlockComment = LastBlockComment))
154  Change.IndentationOffset =
155  Change.StartOfTokenColumn -
156  Change.StartOfBlockComment->StartOfTokenColumn;
157  } else {
158  LastBlockComment = nullptr;
159  }
160  }
161 }
162 
163 // Align a single sequence of tokens, see AlignTokens below.
164 template <typename F>
165 static void
166 AlignTokenSequence(unsigned Start, unsigned End, unsigned Column, F &&Matches,
168  bool FoundMatchOnLine = false;
169  int Shift = 0;
170  for (unsigned i = Start; i != End; ++i) {
171  if (Changes[i].NewlinesBefore > 0) {
172  FoundMatchOnLine = false;
173  Shift = 0;
174  }
175 
176  // If this is the first matching token to be aligned, remember by how many
177  // spaces it has to be shifted, so the rest of the changes on the line are
178  // shifted by the same amount
179  if (!FoundMatchOnLine && Matches(Changes[i])) {
180  FoundMatchOnLine = true;
181  Shift = Column - Changes[i].StartOfTokenColumn;
182  Changes[i].Spaces += Shift;
183  }
184 
185  assert(Shift >= 0);
186  Changes[i].StartOfTokenColumn += Shift;
187  if (i + 1 != Changes.size())
188  Changes[i + 1].PreviousEndOfTokenColumn += Shift;
189  }
190 }
191 
192 // Walk through all of the changes and find sequences of matching tokens to
193 // align. To do so, keep track of the lines and whether or not a matching token
194 // was found on a line. If a matching token is found, extend the current
195 // sequence. If the current line cannot be part of a sequence, e.g. because
196 // there is an empty line before it or it contains only non-matching tokens,
197 // finalize the previous sequence.
198 template <typename F>
199 static void AlignTokens(const FormatStyle &Style, F &&Matches,
201  unsigned MinColumn = 0;
202  unsigned MaxColumn = UINT_MAX;
203 
204  // Line number of the start and the end of the current token sequence.
205  unsigned StartOfSequence = 0;
206  unsigned EndOfSequence = 0;
207 
208  // Keep track of the nesting level of matching tokens, i.e. the number of
209  // surrounding (), [], or {}. We will only align a sequence of matching
210  // token that share the same scope depth.
211  //
212  // FIXME: This could use FormatToken::NestingLevel information, but there is
213  // an outstanding issue wrt the brace scopes.
214  unsigned NestingLevelOfLastMatch = 0;
215  unsigned NestingLevel = 0;
216 
217  // Keep track of the number of commas before the matching tokens, we will only
218  // align a sequence of matching tokens if they are preceded by the same number
219  // of commas.
220  unsigned CommasBeforeLastMatch = 0;
221  unsigned CommasBeforeMatch = 0;
222 
223  // Whether a matching token has been found on the current line.
224  bool FoundMatchOnLine = false;
225 
226  // Aligns a sequence of matching tokens, on the MinColumn column.
227  //
228  // Sequences start from the first matching token to align, and end at the
229  // first token of the first line that doesn't need to be aligned.
230  //
231  // We need to adjust the StartOfTokenColumn of each Change that is on a line
232  // containing any matching token to be aligned and located after such token.
233  auto AlignCurrentSequence = [&] {
234  if (StartOfSequence > 0 && StartOfSequence < EndOfSequence)
235  AlignTokenSequence(StartOfSequence, EndOfSequence, MinColumn, Matches,
236  Changes);
237  MinColumn = 0;
238  MaxColumn = UINT_MAX;
239  StartOfSequence = 0;
240  EndOfSequence = 0;
241  };
242 
243  for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
244  if (Changes[i].NewlinesBefore != 0) {
245  CommasBeforeMatch = 0;
246  EndOfSequence = i;
247  // If there is a blank line, or if the last line didn't contain any
248  // matching token, the sequence ends here.
249  if (Changes[i].NewlinesBefore > 1 || !FoundMatchOnLine)
250  AlignCurrentSequence();
251 
252  FoundMatchOnLine = false;
253  }
254 
255  if (Changes[i].Kind == tok::comma) {
256  ++CommasBeforeMatch;
257  } else if (Changes[i].Kind == tok::r_brace ||
258  Changes[i].Kind == tok::r_paren ||
259  Changes[i].Kind == tok::r_square) {
260  --NestingLevel;
261  } else if (Changes[i].Kind == tok::l_brace ||
262  Changes[i].Kind == tok::l_paren ||
263  Changes[i].Kind == tok::l_square) {
264  // We want sequences to skip over child scopes if possible, but not the
265  // other way around.
266  NestingLevelOfLastMatch = std::min(NestingLevelOfLastMatch, NestingLevel);
267  ++NestingLevel;
268  }
269 
270  if (!Matches(Changes[i]))
271  continue;
272 
273  // If there is more than one matching token per line, or if the number of
274  // preceding commas, or the scope depth, do not match anymore, end the
275  // sequence.
276  if (FoundMatchOnLine || CommasBeforeMatch != CommasBeforeLastMatch ||
277  NestingLevel != NestingLevelOfLastMatch)
278  AlignCurrentSequence();
279 
280  CommasBeforeLastMatch = CommasBeforeMatch;
281  NestingLevelOfLastMatch = NestingLevel;
282  FoundMatchOnLine = true;
283 
284  if (StartOfSequence == 0)
285  StartOfSequence = i;
286 
287  unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
288  int LineLengthAfter = -Changes[i].Spaces;
289  for (unsigned j = i; j != e && Changes[j].NewlinesBefore == 0; ++j)
290  LineLengthAfter += Changes[j].Spaces + Changes[j].TokenLength;
291  unsigned ChangeMaxColumn = Style.ColumnLimit - LineLengthAfter;
292 
293  // If we are restricted by the maximum column width, end the sequence.
294  if (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn ||
295  CommasBeforeLastMatch != CommasBeforeMatch) {
296  AlignCurrentSequence();
297  StartOfSequence = i;
298  }
299 
300  MinColumn = std::max(MinColumn, ChangeMinColumn);
301  MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
302  }
303 
304  EndOfSequence = Changes.size();
305  AlignCurrentSequence();
306 }
307 
308 void WhitespaceManager::alignConsecutiveAssignments() {
309  if (!Style.AlignConsecutiveAssignments)
310  return;
311 
312  AlignTokens(Style,
313  [&](const Change &C) {
314  // Do not align on equal signs that are first on a line.
315  if (C.NewlinesBefore > 0)
316  return false;
317 
318  // Do not align on equal signs that are last on a line.
319  if (&C != &Changes.back() && (&C + 1)->NewlinesBefore > 0)
320  return false;
321 
322  return C.Kind == tok::equal;
323  },
324  Changes);
325 }
326 
327 void WhitespaceManager::alignConsecutiveDeclarations() {
328  if (!Style.AlignConsecutiveDeclarations)
329  return;
330 
331  // FIXME: Currently we don't handle properly the PointerAlignment: Right
332  // The * and & are not aligned and are left dangling. Something has to be done
333  // about it, but it raises the question of alignment of code like:
334  // const char* const* v1;
335  // float const* v2;
336  // SomeVeryLongType const& v3;
337 
338  AlignTokens(Style, [](Change const &C) { return C.IsStartOfDeclName; },
339  Changes);
340 }
341 
342 void WhitespaceManager::alignTrailingComments() {
343  unsigned MinColumn = 0;
344  unsigned MaxColumn = UINT_MAX;
345  unsigned StartOfSequence = 0;
346  bool BreakBeforeNext = false;
347  unsigned Newlines = 0;
348  for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
349  if (Changes[i].StartOfBlockComment)
350  continue;
351  Newlines += Changes[i].NewlinesBefore;
352  if (!Changes[i].IsTrailingComment)
353  continue;
354 
355  unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn;
356  unsigned ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength;
357 
358  // If we don't create a replacement for this change, we have to consider
359  // it to be immovable.
360  if (!Changes[i].CreateReplacement)
361  ChangeMaxColumn = ChangeMinColumn;
362 
363  if (i + 1 != e && Changes[i + 1].ContinuesPPDirective)
364  ChangeMaxColumn -= 2;
365  // If this comment follows an } in column 0, it probably documents the
366  // closing of a namespace and we don't want to align it.
367  bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 &&
368  Changes[i - 1].Kind == tok::r_brace &&
369  Changes[i - 1].StartOfTokenColumn == 0;
370  bool WasAlignedWithStartOfNextLine = false;
371  if (Changes[i].NewlinesBefore == 1) { // A comment on its own line.
372  unsigned CommentColumn = SourceMgr.getSpellingColumnNumber(
373  Changes[i].OriginalWhitespaceRange.getEnd());
374  for (unsigned j = i + 1; j != e; ++j) {
375  if (Changes[j].Kind != tok::comment) { // Skip over comments.
376  unsigned NextColumn = SourceMgr.getSpellingColumnNumber(
377  Changes[j].OriginalWhitespaceRange.getEnd());
378  // The start of the next token was previously aligned with the
379  // start of this comment.
380  WasAlignedWithStartOfNextLine =
381  CommentColumn == NextColumn ||
382  CommentColumn == NextColumn + Style.IndentWidth;
383  break;
384  }
385  }
386  }
387  if (!Style.AlignTrailingComments || FollowsRBraceInColumn0) {
388  alignTrailingComments(StartOfSequence, i, MinColumn);
389  MinColumn = ChangeMinColumn;
390  MaxColumn = ChangeMinColumn;
391  StartOfSequence = i;
392  } else if (BreakBeforeNext || Newlines > 1 ||
393  (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) ||
394  // Break the comment sequence if the previous line did not end
395  // in a trailing comment.
396  (Changes[i].NewlinesBefore == 1 && i > 0 &&
397  !Changes[i - 1].IsTrailingComment) ||
398  WasAlignedWithStartOfNextLine) {
399  alignTrailingComments(StartOfSequence, i, MinColumn);
400  MinColumn = ChangeMinColumn;
401  MaxColumn = ChangeMaxColumn;
402  StartOfSequence = i;
403  } else {
404  MinColumn = std::max(MinColumn, ChangeMinColumn);
405  MaxColumn = std::min(MaxColumn, ChangeMaxColumn);
406  }
407  BreakBeforeNext =
408  (i == 0) || (Changes[i].NewlinesBefore > 1) ||
409  // Never start a sequence with a comment at the beginning of
410  // the line.
411  (Changes[i].NewlinesBefore == 1 && StartOfSequence == i);
412  Newlines = 0;
413  }
414  alignTrailingComments(StartOfSequence, Changes.size(), MinColumn);
415 }
416 
417 void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End,
418  unsigned Column) {
419  for (unsigned i = Start; i != End; ++i) {
420  int Shift = 0;
421  if (Changes[i].IsTrailingComment) {
422  Shift = Column - Changes[i].StartOfTokenColumn;
423  }
424  if (Changes[i].StartOfBlockComment) {
425  Shift = Changes[i].IndentationOffset +
426  Changes[i].StartOfBlockComment->StartOfTokenColumn -
427  Changes[i].StartOfTokenColumn;
428  }
429  assert(Shift >= 0);
430  Changes[i].Spaces += Shift;
431  if (i + 1 != End)
432  Changes[i + 1].PreviousEndOfTokenColumn += Shift;
433  Changes[i].StartOfTokenColumn += Shift;
434  }
435 }
436 
437 void WhitespaceManager::alignEscapedNewlines() {
438  unsigned MaxEndOfLine =
439  Style.AlignEscapedNewlinesLeft ? 0 : Style.ColumnLimit;
440  unsigned StartOfMacro = 0;
441  for (unsigned i = 1, e = Changes.size(); i < e; ++i) {
442  Change &C = Changes[i];
443  if (C.NewlinesBefore > 0) {
444  if (C.ContinuesPPDirective) {
445  MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine);
446  } else {
447  alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine);
448  MaxEndOfLine = Style.AlignEscapedNewlinesLeft ? 0 : Style.ColumnLimit;
449  StartOfMacro = i;
450  }
451  }
452  }
453  alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine);
454 }
455 
456 void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End,
457  unsigned Column) {
458  for (unsigned i = Start; i < End; ++i) {
459  Change &C = Changes[i];
460  if (C.NewlinesBefore > 0) {
461  assert(C.ContinuesPPDirective);
462  if (C.PreviousEndOfTokenColumn + 1 > Column)
463  C.EscapedNewlineColumn = 0;
464  else
465  C.EscapedNewlineColumn = Column;
466  }
467  }
468 }
469 
470 void WhitespaceManager::generateChanges() {
471  for (unsigned i = 0, e = Changes.size(); i != e; ++i) {
472  const Change &C = Changes[i];
473  if (i > 0) {
474  assert(Changes[i - 1].OriginalWhitespaceRange.getBegin() !=
475  C.OriginalWhitespaceRange.getBegin() &&
476  "Generating two replacements for the same location");
477  }
478  if (C.CreateReplacement) {
479  std::string ReplacementText = C.PreviousLinePostfix;
480  if (C.ContinuesPPDirective)
481  appendNewlineText(ReplacementText, C.NewlinesBefore,
482  C.PreviousEndOfTokenColumn, C.EscapedNewlineColumn);
483  else
484  appendNewlineText(ReplacementText, C.NewlinesBefore);
485  appendIndentText(ReplacementText, C.IndentLevel, std::max(0, C.Spaces),
486  C.StartOfTokenColumn - std::max(0, C.Spaces));
487  ReplacementText.append(C.CurrentLinePrefix);
488  storeReplacement(C.OriginalWhitespaceRange, ReplacementText);
489  }
490  }
491 }
492 
493 void WhitespaceManager::storeReplacement(SourceRange Range,
494  StringRef Text) {
495  unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) -
496  SourceMgr.getFileOffset(Range.getBegin());
497  // Don't create a replacement, if it does not change anything.
498  if (StringRef(SourceMgr.getCharacterData(Range.getBegin()),
499  WhitespaceLength) == Text)
500  return;
501  Replaces.insert(tooling::Replacement(
502  SourceMgr, CharSourceRange::getCharRange(Range), Text));
503 }
504 
505 void WhitespaceManager::appendNewlineText(std::string &Text,
506  unsigned Newlines) {
507  for (unsigned i = 0; i < Newlines; ++i)
508  Text.append(UseCRLF ? "\r\n" : "\n");
509 }
510 
511 void WhitespaceManager::appendNewlineText(std::string &Text, unsigned Newlines,
512  unsigned PreviousEndOfTokenColumn,
513  unsigned EscapedNewlineColumn) {
514  if (Newlines > 0) {
515  unsigned Offset =
516  std::min<int>(EscapedNewlineColumn - 1, PreviousEndOfTokenColumn);
517  for (unsigned i = 0; i < Newlines; ++i) {
518  Text.append(EscapedNewlineColumn - Offset - 1, ' ');
519  Text.append(UseCRLF ? "\\\r\n" : "\\\n");
520  Offset = 0;
521  }
522  }
523 }
524 
525 void WhitespaceManager::appendIndentText(std::string &Text,
526  unsigned IndentLevel, unsigned Spaces,
527  unsigned WhitespaceStartColumn) {
528  switch (Style.UseTab) {
530  Text.append(Spaces, ' ');
531  break;
532  case FormatStyle::UT_Always: {
533  unsigned FirstTabWidth =
534  Style.TabWidth - WhitespaceStartColumn % Style.TabWidth;
535  // Indent with tabs only when there's at least one full tab.
536  if (FirstTabWidth + Style.TabWidth <= Spaces) {
537  Spaces -= FirstTabWidth;
538  Text.append("\t");
539  }
540  Text.append(Spaces / Style.TabWidth, '\t');
541  Text.append(Spaces % Style.TabWidth, ' ');
542  break;
543  }
545  if (WhitespaceStartColumn == 0) {
546  unsigned Indentation = IndentLevel * Style.IndentWidth;
547  // This happens, e.g. when a line in a block comment is indented less than
548  // the first one.
549  if (Indentation > Spaces)
550  Indentation = Spaces;
551  unsigned Tabs = Indentation / Style.TabWidth;
552  Text.append(Tabs, '\t');
553  Spaces -= Tabs * Style.TabWidth;
554  }
555  Text.append(Spaces, ' ');
556  break;
557  }
558 }
559 
560 } // namespace format
561 } // namespace clang
Use tabs only for indentation.
Definition: Format.h:582
Token Tok
The Token.
Definition: FormatToken.h:116
bool AlignEscapedNewlinesLeft
If true, aligns escaped newlines as far left as possible.
Definition: Format.h:99
std::set< Replacement > Replacements
A set of Replacements.
Definition: Replacement.h:141
unsigned IndentWidth
The number of columns to use for indentation.
Definition: Format.h:407
const char * getCharacterData(SourceLocation SL, bool *Invalid=nullptr) const
Return a pointer to the start of the specified location in the appropriate spelling MemoryBuffer...
unsigned OriginalColumn
The original 0-based column of this token, including expanded tabs.
Definition: FormatToken.h:207
bool AlignConsecutiveDeclarations
If true, aligns consecutive declarations.
Definition: Format.h:95
bool operator()(const Change &C1, const Change &C2) const
static void AlignTokenSequence(unsigned Start, unsigned End, unsigned Column, F &&Matches, SmallVector< WhitespaceManager::Change, 16 > &Changes)
bool IsFirst
Indicates that this is the first token.
Definition: FormatToken.h:148
unsigned NewlinesBefore
The number of newlines immediately before the Token.
Definition: FormatToken.h:122
Use tabs whenever we need to fill whitespace that spans at least from one tab stop to the next one...
Definition: Format.h:585
SourceLocation getLocWithOffset(int Offset) const
Return a source location with the specified offset from this SourceLocation.
uint32_t Offset
Definition: CacheTokens.cpp:44
void replaceWhitespace(FormatToken &Tok, unsigned Newlines, unsigned IndentLevel, unsigned Spaces, unsigned StartOfTokenColumn, bool InPPDirective=false)
Replaces the whitespace in front of Tok.
void reset()
Prepares the WhitespaceManager for another run.
tok::TokenKind getKind() const
Definition: Token.h:90
#define UINT_MAX
Definition: limits.h:72
bool AlignConsecutiveAssignments
If true, aligns consecutive assignments.
Definition: Format.h:84
unsigned ColumnLimit
The column limit.
Definition: Format.h:293
UseTabStyle UseTab
The way to use tab characters in the resulting file.
Definition: Format.h:589
const SmallVectorImpl< AnnotatedLine * >::const_iterator End
WhitespaceManager class manages whitespace around tokens and their replacements.
static void AlignTokens(const FormatStyle &Style, F &&Matches, SmallVector< WhitespaceManager::Change, 16 > &Changes)
bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const
Determines the order of 2 source locations in the translation unit.
void replaceWhitespaceInToken(const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars, StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective, unsigned Newlines, unsigned IndentLevel, int Spaces)
Inserts or replaces whitespace in the middle of a token.
A wrapper around a Token storing information about the whitespace characters preceding it...
Definition: FormatToken.h:112
const tooling::Replacements & generateReplacements()
Returns all the Replacements created during formatting.
Represents a change before a token, a break inside a token, or the layout of an unchanged token (or w...
static CharSourceRange getCharRange(SourceRange R)
#define false
Definition: stdbool.h:33
Kind
Encodes a location in the source.
unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid=nullptr) const
SourceRange WhitespaceRange
The range of the whitespace immediately preceding the Token.
Definition: FormatToken.h:129
TokenKind
Provides a simple uniform namespace for tokens from all C languages.
Definition: TokenKinds.h:25
bool Matches
SourceLocation getBegin() const
The FormatStyle is used to configure the formatting to follow specific guidelines.
Definition: Format.h:42
if(T->getSizeExpr()) TRY_TO(TraverseStmt(T-> getSizeExpr()))
Functor to sort changes in original source order.
void addUntouchableToken(const FormatToken &Tok, bool InPPDirective)
Adds information about an unchangeable token's whitespace.
bool Finalized
If true, this token has been fully formatted (indented and potentially re-formatted inside)...
Definition: FormatToken.h:279
bool is(tok::TokenKind Kind) const
Definition: FormatToken.h:281
unsigned TabWidth
The number of columns used for tab stops.
Definition: Format.h:575
SourceLocation getStartOfNonWhitespace() const
Returns actual token start location without leading escaped newlines and whitespace.
Definition: FormatToken.h:389
bool AlignTrailingComments
If true, aligns trailing comments.
Definition: Format.h:113
StringRef Text
Definition: Format.cpp:1724
unsigned getFileOffset(SourceLocation SpellingLoc) const
Returns the offset from the start of the file that the specified SourceLocation represents.
A trivial tuple used to represent a source range.
FormatDecision Decision
Stores the formatting decision for the token once it was made.
Definition: FormatToken.h:274
unsigned Column
Definition: Format.cpp:1349