blob: af20dc5616a70b56ad630ae557651c49228a4696 [file] [log] [blame]
Daniel Jasper6fe2f002013-04-25 08:56:26 +00001//===--- WhitespaceManager.h - Format C++ code ------------------*- C++ -*-===//
Alexander Kornienkocb45bc12013-04-15 14:28:00 +00002//
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
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000016#ifndef LLVM_CLANG_LIB_FORMAT_WHITESPACEMANAGER_H
17#define LLVM_CLANG_LIB_FORMAT_WHITESPACEMANAGER_H
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000018
19#include "TokenAnnotator.h"
20#include "clang/Basic/SourceManager.h"
21#include "clang/Format/Format.h"
22#include <string>
23
24namespace clang {
25namespace 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.
Manuel Klimek4fe43002013-05-22 12:51:29 +000031///
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.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000038class WhitespaceManager {
39public:
Eric Liu635423e2016-04-28 07:52:03 +000040 WhitespaceManager(const SourceManager &SourceMgr, const FormatStyle &Style,
Alexander Kornienko9e649af2013-09-11 12:25:57 +000041 bool UseCRLF)
42 : SourceMgr(SourceMgr), Style(Style), UseCRLF(UseCRLF) {}
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000043
44 /// \brief Replaces the whitespace in front of \p Tok. Only call once for
45 /// each \c AnnotatedToken.
Krasimir Georgiev54ef4ba2017-06-13 14:58:55 +000046 ///
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.
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000050 void replaceWhitespace(FormatToken &Tok, unsigned Newlines, unsigned Spaces,
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +000051 unsigned StartOfTokenColumn,
Manuel Klimek4fe43002013-05-22 12:51:29 +000052 bool InPPDirective = false);
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000053
Alp Tokerf6a24ce2013-12-05 16:25:25 +000054 /// \brief Adds information about an unchangeable token's whitespace.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000055 ///
Manuel Klimek4fe43002013-05-22 12:51:29 +000056 /// Needs to be called for every token for which \c replaceWhitespace
57 /// was not called.
58 void addUntouchableToken(const FormatToken &Tok, bool InPPDirective);
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000059
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000060 llvm::Error addReplacement(const tooling::Replacement &Replacement);
61
Alexander Kornienko555efc32013-06-11 16:01:49 +000062 /// \brief Inserts or replaces whitespace in the middle of a token.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000063 ///
Alexander Kornienko555efc32013-06-11 16:01:49 +000064 /// 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.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000067 ///
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +000068 /// 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 ///
Alexander Kornienko555efc32013-06-11 16:01:49 +000074 /// 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,
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000080 unsigned Newlines, int Spaces);
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000081
82 /// \brief Returns all the \c Replacements created during formatting.
83 const tooling::Replacements &generateReplacements();
84
Manuel Klimek4fe43002013-05-22 12:51:29 +000085 /// \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.
89 class IsBeforeInFile {
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +000090 public:
Manuel Klimek4fe43002013-05-22 12:51:29 +000091 IsBeforeInFile(const SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
92 bool operator()(const Change &C1, const Change &C2) const;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000093
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +000094 private:
Manuel Klimek4fe43002013-05-22 12:51:29 +000095 const SourceManager &SourceMgr;
96 };
97
Manuel Klimek4fe43002013-05-22 12:51:29 +000098 /// \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.
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000107 Change(const FormatToken &Tok, bool CreateReplacement,
108 SourceRange OriginalWhitespaceRange, int Spaces,
109 unsigned StartOfTokenColumn, unsigned NewlinesBefore,
110 StringRef PreviousLinePostfix, StringRef CurrentLinePrefix,
111 bool ContinuesPPDirective, bool IsInsideToken);
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;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000118
119 bool CreateReplacement;
120 // Changes might be in the middle of a token, so we cannot just keep the
121 // FormatToken around to query its information.
122 SourceRange OriginalWhitespaceRange;
123 unsigned StartOfTokenColumn;
124 unsigned NewlinesBefore;
125 std::string PreviousLinePostfix;
126 std::string CurrentLinePrefix;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000127 bool ContinuesPPDirective;
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000128
Manuel Klimek4fe43002013-05-22 12:51:29 +0000129 // The number of spaces in front of the token or broken part of the token.
130 // This will be adapted when aligning tokens.
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000131 // 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;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000135
Benjamin Kramerdab50462016-01-11 16:27:16 +0000136 // If this change is inside of a token but not at the start of the token or
137 // directly after a newline.
138 bool IsInsideToken;
139
Manuel Klimek4fe43002013-05-22 12:51:29 +0000140 // \c IsTrailingComment, \c TokenLength, \c PreviousEndOfTokenColumn and
141 // \c EscapedNewlineColumn will be calculated in
142 // \c calculateLineBreakInformation.
143 bool IsTrailingComment;
144 unsigned TokenLength;
145 unsigned PreviousEndOfTokenColumn;
146 unsigned EscapedNewlineColumn;
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000147
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.
156 const Change *StartOfBlockComment;
157 int IndentationOffset;
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000158
Daniel Jasper4917af62017-08-25 19:14:53 +0000159 // A combination of indent level and nesting level, which are used in
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000160 // tandem to compute lexical scope, for the purposes of deciding
161 // when to stop consecutive alignment runs.
Daniel Jasper4917af62017-08-25 19:14:53 +0000162 std::pair<unsigned, unsigned> indentAndNestingLevel() const {
163 return std::make_pair(Tok->IndentLevel, Tok->NestingLevel);
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000164 }
Manuel Klimek4fe43002013-05-22 12:51:29 +0000165 };
166
Daniel Jasperec90e512015-12-01 12:00:43 +0000167private:
Manuel Klimek4fe43002013-05-22 12:51:29 +0000168 /// \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
Daniel Jaspera44991332015-04-29 13:06:49 +0000173 /// \brief Align consecutive assignments over all \c Changes.
174 void alignConsecutiveAssignments();
175
Daniel Jaspere12597c2015-10-01 10:06:54 +0000176 /// \brief Align consecutive declarations over all \c Changes.
177 void alignConsecutiveDeclarations();
178
Manuel Klimek4fe43002013-05-22 12:51:29 +0000179 /// \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.
Daniel Jasper6fe2f002013-04-25 08:56:26 +0000187 void alignEscapedNewlines();
Daniel Jasper770eb7c2013-04-24 06:33:59 +0000188
Manuel Klimek4fe43002013-05-22 12:51:29 +0000189 /// \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.
Craig Toppere335f252015-10-04 04:53:55 +0000197 void storeReplacement(SourceRange Range, StringRef Text);
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000198 void appendNewlineText(std::string &Text, unsigned Newlines);
Jacob Bandes-Storchd6a7e982017-08-10 00:15:31 +0000199 void appendEscapedNewlineText(std::string &Text, unsigned Newlines,
200 unsigned PreviousEndOfTokenColumn,
201 unsigned EscapedNewlineColumn);
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000202 void appendIndentText(std::string &Text, unsigned IndentLevel,
203 unsigned Spaces, unsigned WhitespaceStartColumn);
Manuel Klimekb9eae4c2013-05-13 09:22:11 +0000204
Manuel Klimek4fe43002013-05-22 12:51:29 +0000205 SmallVector<Change, 16> Changes;
Eric Liu635423e2016-04-28 07:52:03 +0000206 const SourceManager &SourceMgr;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000207 tooling::Replacements Replaces;
208 const FormatStyle &Style;
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000209 bool UseCRLF;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000210};
211
212} // namespace format
213} // namespace clang
214
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000215#endif