clang  6.0.0svn
WhitespaceManager.h
Go to the documentation of this file.
1 //===--- WhitespaceManager.h - Format C++ code ------------------*- C++ -*-===//
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 WhitespaceManager class manages whitespace around tokens and their
12 /// replacements.
13 ///
14 //===----------------------------------------------------------------------===//
15 
16 #ifndef LLVM_CLANG_LIB_FORMAT_WHITESPACEMANAGER_H
17 #define LLVM_CLANG_LIB_FORMAT_WHITESPACEMANAGER_H
18 
19 #include "TokenAnnotator.h"
21 #include "clang/Format/Format.h"
22 #include <string>
23 
24 namespace clang {
25 namespace format {
26 
27 /// \brief Manages the whitespaces around tokens and their replacements.
28 ///
29 /// This includes special handling for certain constructs, e.g. the alignment of
30 /// trailing line comments.
31 ///
32 /// To guarantee correctness of alignment operations, the \c WhitespaceManager
33 /// must be informed about every token in the source file; for each token, there
34 /// must be exactly one call to either \c replaceWhitespace or
35 /// \c addUntouchableToken.
36 ///
37 /// There may be multiple calls to \c breakToken for a given token.
39 public:
40  WhitespaceManager(const SourceManager &SourceMgr, const FormatStyle &Style,
41  bool UseCRLF)
42  : SourceMgr(SourceMgr), Style(Style), UseCRLF(UseCRLF) {}
43 
44  /// \brief Replaces the whitespace in front of \p Tok. Only call once for
45  /// each \c AnnotatedToken.
46  ///
47  /// \p StartOfTokenColumn is the column at which the token will start after
48  /// this replacement. It is needed for determining how \p Spaces is turned
49  /// into tabs and spaces for some format styles.
50  void replaceWhitespace(FormatToken &Tok, unsigned Newlines, unsigned Spaces,
51  unsigned StartOfTokenColumn,
52  bool InPPDirective = false);
53 
54  /// \brief Adds information about an unchangeable token's whitespace.
55  ///
56  /// Needs to be called for every token for which \c replaceWhitespace
57  /// was not called.
58  void addUntouchableToken(const FormatToken &Tok, bool InPPDirective);
59 
61 
62  /// \brief Inserts or replaces whitespace in the middle of a token.
63  ///
64  /// Inserts \p PreviousPostfix, \p Newlines, \p Spaces and \p CurrentPrefix
65  /// (in this order) at \p Offset inside \p Tok, replacing \p ReplaceChars
66  /// characters.
67  ///
68  /// Note: \p Spaces can be negative to retain information about initial
69  /// relative column offset between a line of a block comment and the start of
70  /// the comment. This negative offset may be compensated by trailing comment
71  /// alignment here. In all other cases negative \p Spaces will be truncated to
72  /// 0.
73  ///
74  /// When \p InPPDirective is true, escaped newlines are inserted. \p Spaces is
75  /// used to align backslashes correctly.
76  void replaceWhitespaceInToken(const FormatToken &Tok, unsigned Offset,
77  unsigned ReplaceChars,
78  StringRef PreviousPostfix,
79  StringRef CurrentPrefix, bool InPPDirective,
80  unsigned Newlines, int Spaces);
81 
82  /// \brief Returns all the \c Replacements created during formatting.
84 
85  /// \brief Represents a change before a token, a break inside a token,
86  /// or the layout of an unchanged token (or whitespace within).
87  struct Change {
88  /// \brief Functor to sort changes in original source order.
90  public:
91  IsBeforeInFile(const SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
92  bool operator()(const Change &C1, const Change &C2) const;
93 
94  private:
95  const SourceManager &SourceMgr;
96  };
97 
98  /// \brief Creates a \c Change.
99  ///
100  /// The generated \c Change will replace the characters at
101  /// \p OriginalWhitespaceRange with a concatenation of
102  /// \p PreviousLinePostfix, \p NewlinesBefore line breaks, \p Spaces spaces
103  /// and \p CurrentLinePrefix.
104  ///
105  /// \p StartOfTokenColumn and \p InPPDirective will be used to lay out
106  /// trailing comments and escaped newlines.
107  Change(const FormatToken &Tok, bool CreateReplacement,
109  unsigned StartOfTokenColumn, unsigned NewlinesBefore,
110  StringRef PreviousLinePostfix, StringRef CurrentLinePrefix,
112 
113  // The kind of the token whose whitespace this change replaces, or in which
114  // this change inserts whitespace.
115  // FIXME: Currently this is not set correctly for breaks inside comments, as
116  // the \c BreakableToken is still doing its own alignment.
117  const FormatToken *Tok;
118 
120  // Changes might be in the middle of a token, so we cannot just keep the
121  // FormatToken around to query its information.
124  unsigned NewlinesBefore;
125  std::string PreviousLinePostfix;
126  std::string CurrentLinePrefix;
128 
129  // The number of spaces in front of the token or broken part of the token.
130  // This will be adapted when aligning tokens.
131  // Can be negative to retain information about the initial relative offset
132  // of the lines in a block comment. This is used when aligning trailing
133  // comments. Uncompensated negative offset is truncated to 0.
134  int Spaces;
135 
136  // If this change is inside of a token but not at the start of the token or
137  // directly after a newline.
139 
140  // \c IsTrailingComment, \c TokenLength, \c PreviousEndOfTokenColumn and
141  // \c EscapedNewlineColumn will be calculated in
142  // \c calculateLineBreakInformation.
144  unsigned TokenLength;
147 
148  // These fields are used to retain correct relative line indentation in a
149  // block comment when aligning trailing comments.
150  //
151  // If this Change represents a continuation of a block comment,
152  // \c StartOfBlockComment is pointer to the first Change in the block
153  // comment. \c IndentationOffset is a relative column offset to this
154  // change, so that the correct column can be reconstructed at the end of
155  // the alignment process.
158 
159  // A combination of indent level and nesting level, which are used in
160  // tandem to compute lexical scope, for the purposes of deciding
161  // when to stop consecutive alignment runs.
162  std::pair<unsigned, unsigned> indentAndNestingLevel() const {
163  return std::make_pair(Tok->IndentLevel, Tok->NestingLevel);
164  }
165  };
166 
167 private:
168  /// \brief Calculate \c IsTrailingComment, \c TokenLength for the last tokens
169  /// or token parts in a line and \c PreviousEndOfTokenColumn and
170  /// \c EscapedNewlineColumn for the first tokens or token parts in a line.
171  void calculateLineBreakInformation();
172 
173  /// \brief Align consecutive assignments over all \c Changes.
174  void alignConsecutiveAssignments();
175 
176  /// \brief Align consecutive declarations over all \c Changes.
177  void alignConsecutiveDeclarations();
178 
179  /// \brief Align trailing comments over all \c Changes.
180  void alignTrailingComments();
181 
182  /// \brief Align trailing comments from change \p Start to change \p End at
183  /// the specified \p Column.
184  void alignTrailingComments(unsigned Start, unsigned End, unsigned Column);
185 
186  /// \brief Align escaped newlines over all \c Changes.
187  void alignEscapedNewlines();
188 
189  /// \brief Align escaped newlines from change \p Start to change \p End at
190  /// the specified \p Column.
191  void alignEscapedNewlines(unsigned Start, unsigned End, unsigned Column);
192 
193  /// \brief Fill \c Replaces with the replacements for all effective changes.
194  void generateChanges();
195 
196  /// \brief Stores \p Text as the replacement for the whitespace in \p Range.
197  void storeReplacement(SourceRange Range, StringRef Text);
198  void appendNewlineText(std::string &Text, unsigned Newlines);
199  void appendEscapedNewlineText(std::string &Text, unsigned Newlines,
200  unsigned PreviousEndOfTokenColumn,
201  unsigned EscapedNewlineColumn);
202  void appendIndentText(std::string &Text, unsigned IndentLevel,
203  unsigned Spaces, unsigned WhitespaceStartColumn);
204 
206  const SourceManager &SourceMgr;
207  tooling::Replacements Replaces;
208  const FormatStyle &Style;
209  bool UseCRLF;
210 };
211 
212 } // namespace format
213 } // namespace clang
214 
215 #endif
Change(const FormatToken &Tok, bool CreateReplacement, SourceRange OriginalWhitespaceRange, int Spaces, unsigned StartOfTokenColumn, unsigned NewlinesBefore, StringRef PreviousLinePostfix, StringRef CurrentLinePrefix, bool ContinuesPPDirective, bool IsInsideToken)
Creates a Change.
unsigned NestingLevel
The nesting level of this token, i.e.
Definition: FormatToken.h:228
Defines the SourceManager interface.
std::pair< unsigned, unsigned > indentAndNestingLevel() const
Maintains a set of replacements that are conflict-free.
Definition: Replacement.h:205
void replaceWhitespaceInToken(const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars, StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective, unsigned Newlines, int Spaces)
Inserts or replaces whitespace in the middle of a token.
This file implements a token annotator, i.e.
Manages the whitespaces around tokens and their replacements.
uint32_t Offset
Definition: CacheTokens.cpp:43
const FormatToken & Tok
AvailabilityChange Changes[NumAvailabilitySlots]
Definition: AttributeList.h:57
WhitespaceManager(const SourceManager &SourceMgr, const FormatStyle &Style, bool UseCRLF)
A text replacement.
Definition: Replacement.h:81
SourceLocation End
llvm::Error addReplacement(const tooling::Replacement &Replacement)
A wrapper around a Token storing information about the whitespace characters preceding it...
Definition: FormatToken.h:120
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...
void replaceWhitespace(FormatToken &Tok, unsigned Newlines, unsigned Spaces, unsigned StartOfTokenColumn, bool InPPDirective=false)
Replaces the whitespace in front of Tok.
const bool InPPDirective
Various functions to configurably format source code.
The FormatStyle is used to configure the formatting to follow specific guidelines.
Definition: Format.h:46
unsigned IndentLevel
The indent level of this token. Copied from the surrounding line.
Definition: FormatToken.h:231
Dataflow Directional Tag Classes.
Functor to sort changes in original source order.
void addUntouchableToken(const FormatToken &Tok, bool InPPDirective)
Adds information about an unchangeable token&#39;s whitespace.
bool operator()(const Change &C1, const Change &C2) const
const Expr * Replacement
Definition: AttributeList.h:59
StringRef Text
Definition: Format.cpp:1346
A trivial tuple used to represent a source range.
This class handles loading and caching of source files into memory.
const FormatStyle & Style