]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/tools/clang/lib/Format/WhitespaceManager.h
MFV r319744,r319745: 8269 dtrace stddev aggregation is normalized incorrectly
[FreeBSD/FreeBSD.git] / contrib / llvm / tools / clang / lib / Format / WhitespaceManager.h
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"
20 #include "clang/Basic/SourceManager.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.
38 class WhitespaceManager {
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   void replaceWhitespace(FormatToken &Tok, unsigned Newlines,
47                          unsigned IndentLevel, unsigned Spaces,
48                          unsigned StartOfTokenColumn,
49                          bool InPPDirective = false);
50
51   /// \brief Adds information about an unchangeable token's whitespace.
52   ///
53   /// Needs to be called for every token for which \c replaceWhitespace
54   /// was not called.
55   void addUntouchableToken(const FormatToken &Tok, bool InPPDirective);
56
57   /// \brief Inserts or replaces whitespace in the middle of a token.
58   ///
59   /// Inserts \p PreviousPostfix, \p Newlines, \p Spaces and \p CurrentPrefix
60   /// (in this order) at \p Offset inside \p Tok, replacing \p ReplaceChars
61   /// characters.
62   ///
63   /// Note: \p Spaces can be negative to retain information about initial
64   /// relative column offset between a line of a block comment and the start of
65   /// the comment. This negative offset may be compensated by trailing comment
66   /// alignment here. In all other cases negative \p Spaces will be truncated to
67   /// 0.
68   ///
69   /// When \p InPPDirective is true, escaped newlines are inserted. \p Spaces is
70   /// used to align backslashes correctly.
71   void replaceWhitespaceInToken(const FormatToken &Tok, unsigned Offset,
72                                 unsigned ReplaceChars,
73                                 StringRef PreviousPostfix,
74                                 StringRef CurrentPrefix, bool InPPDirective,
75                                 unsigned Newlines, unsigned IndentLevel,
76                                 int Spaces);
77
78   /// \brief Returns all the \c Replacements created during formatting.
79   const tooling::Replacements &generateReplacements();
80
81   /// \brief Represents a change before a token, a break inside a token,
82   /// or the layout of an unchanged token (or whitespace within).
83   struct Change {
84     /// \brief Functor to sort changes in original source order.
85     class IsBeforeInFile {
86     public:
87       IsBeforeInFile(const SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
88       bool operator()(const Change &C1, const Change &C2) const;
89
90     private:
91       const SourceManager &SourceMgr;
92     };
93
94     Change() {}
95
96     /// \brief Creates a \c Change.
97     ///
98     /// The generated \c Change will replace the characters at
99     /// \p OriginalWhitespaceRange with a concatenation of
100     /// \p PreviousLinePostfix, \p NewlinesBefore line breaks, \p Spaces spaces
101     /// and \p CurrentLinePrefix.
102     ///
103     /// \p StartOfTokenColumn and \p InPPDirective will be used to lay out
104     /// trailing comments and escaped newlines.
105     Change(bool CreateReplacement, SourceRange OriginalWhitespaceRange,
106            unsigned IndentLevel, int Spaces, unsigned StartOfTokenColumn,
107            unsigned NewlinesBefore, StringRef PreviousLinePostfix,
108            StringRef CurrentLinePrefix, tok::TokenKind Kind,
109            bool ContinuesPPDirective, bool IsStartOfDeclName,
110            bool IsInsideToken);
111
112     bool CreateReplacement;
113     // Changes might be in the middle of a token, so we cannot just keep the
114     // FormatToken around to query its information.
115     SourceRange OriginalWhitespaceRange;
116     unsigned StartOfTokenColumn;
117     unsigned NewlinesBefore;
118     std::string PreviousLinePostfix;
119     std::string CurrentLinePrefix;
120     // The kind of the token whose whitespace this change replaces, or in which
121     // this change inserts whitespace.
122     // FIXME: Currently this is not set correctly for breaks inside comments, as
123     // the \c BreakableToken is still doing its own alignment.
124     tok::TokenKind Kind;
125     bool ContinuesPPDirective;
126     bool IsStartOfDeclName;
127
128     // The number of nested blocks the token is in. This is used to add tabs
129     // only for the indentation, and not for alignment, when
130     // UseTab = US_ForIndentation.
131     unsigned IndentLevel;
132
133     // The number of spaces in front of the token or broken part of the token.
134     // This will be adapted when aligning tokens.
135     // Can be negative to retain information about the initial relative offset
136     // of the lines in a block comment. This is used when aligning trailing
137     // comments. Uncompensated negative offset is truncated to 0.
138     int Spaces;
139
140     // If this change is inside of a token but not at the start of the token or
141     // directly after a newline.
142     bool IsInsideToken;
143
144     // \c IsTrailingComment, \c TokenLength, \c PreviousEndOfTokenColumn and
145     // \c EscapedNewlineColumn will be calculated in
146     // \c calculateLineBreakInformation.
147     bool IsTrailingComment;
148     unsigned TokenLength;
149     unsigned PreviousEndOfTokenColumn;
150     unsigned EscapedNewlineColumn;
151
152     // These fields are used to retain correct relative line indentation in a
153     // block comment when aligning trailing comments.
154     //
155     // If this Change represents a continuation of a block comment,
156     // \c StartOfBlockComment is pointer to the first Change in the block
157     // comment. \c IndentationOffset is a relative column offset to this
158     // change, so that the correct column can be reconstructed at the end of
159     // the alignment process.
160     const Change *StartOfBlockComment;
161     int IndentationOffset;
162   };
163
164 private:
165   /// \brief Calculate \c IsTrailingComment, \c TokenLength for the last tokens
166   /// or token parts in a line and \c PreviousEndOfTokenColumn and
167   /// \c EscapedNewlineColumn for the first tokens or token parts in a line.
168   void calculateLineBreakInformation();
169
170   /// \brief Align consecutive assignments over all \c Changes.
171   void alignConsecutiveAssignments();
172
173   /// \brief Align consecutive declarations over all \c Changes.
174   void alignConsecutiveDeclarations();
175
176   /// \brief Align trailing comments over all \c Changes.
177   void alignTrailingComments();
178
179   /// \brief Align trailing comments from change \p Start to change \p End at
180   /// the specified \p Column.
181   void alignTrailingComments(unsigned Start, unsigned End, unsigned Column);
182
183   /// \brief Align escaped newlines over all \c Changes.
184   void alignEscapedNewlines();
185
186   /// \brief Align escaped newlines from change \p Start to change \p End at
187   /// the specified \p Column.
188   void alignEscapedNewlines(unsigned Start, unsigned End, unsigned Column);
189
190   /// \brief Fill \c Replaces with the replacements for all effective changes.
191   void generateChanges();
192
193   /// \brief Stores \p Text as the replacement for the whitespace in \p Range.
194   void storeReplacement(SourceRange Range, StringRef Text);
195   void appendNewlineText(std::string &Text, unsigned Newlines);
196   void appendNewlineText(std::string &Text, unsigned Newlines,
197                          unsigned PreviousEndOfTokenColumn,
198                          unsigned EscapedNewlineColumn);
199   void appendIndentText(std::string &Text, unsigned IndentLevel,
200                         unsigned Spaces, unsigned WhitespaceStartColumn);
201
202   SmallVector<Change, 16> Changes;
203   const SourceManager &SourceMgr;
204   tooling::Replacements Replaces;
205   const FormatStyle &Style;
206   bool UseCRLF;
207 };
208
209 } // namespace format
210 } // namespace clang
211
212 #endif