blob: f08e71495cc2846d9837baa4ed0e8e0bf88bb11d [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// 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
Alexander Kornienkocb45bc12013-04-15 14:28:00 +00006//
7//===----------------------------------------------------------------------===//
8///
9/// \file
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000010/// WhitespaceManager class manages whitespace around tokens and their
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000011/// replacements.
12///
13//===----------------------------------------------------------------------===//
14
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +000015#ifndef LLVM_CLANG_LIB_FORMAT_WHITESPACEMANAGER_H
16#define LLVM_CLANG_LIB_FORMAT_WHITESPACEMANAGER_H
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000017
18#include "TokenAnnotator.h"
19#include "clang/Basic/SourceManager.h"
20#include "clang/Format/Format.h"
21#include <string>
22
23namespace clang {
24namespace format {
25
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000026/// Manages the whitespaces around tokens and their replacements.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000027///
28/// This includes special handling for certain constructs, e.g. the alignment of
29/// trailing line comments.
Manuel Klimek4fe43002013-05-22 12:51:29 +000030///
31/// To guarantee correctness of alignment operations, the \c WhitespaceManager
32/// must be informed about every token in the source file; for each token, there
33/// must be exactly one call to either \c replaceWhitespace or
34/// \c addUntouchableToken.
35///
36/// There may be multiple calls to \c breakToken for a given token.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000037class WhitespaceManager {
38public:
Eric Liu635423e2016-04-28 07:52:03 +000039 WhitespaceManager(const SourceManager &SourceMgr, const FormatStyle &Style,
Alexander Kornienko9e649af2013-09-11 12:25:57 +000040 bool UseCRLF)
41 : SourceMgr(SourceMgr), Style(Style), UseCRLF(UseCRLF) {}
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000042
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000043 /// Replaces the whitespace in front of \p Tok. Only call once for
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000044 /// each \c AnnotatedToken.
Krasimir Georgiev54ef4ba2017-06-13 14:58:55 +000045 ///
46 /// \p StartOfTokenColumn is the column at which the token will start after
47 /// this replacement. It is needed for determining how \p Spaces is turned
48 /// into tabs and spaces for some format styles.
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000049 void replaceWhitespace(FormatToken &Tok, unsigned Newlines, unsigned Spaces,
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +000050 unsigned StartOfTokenColumn,
Manuel Klimek4fe43002013-05-22 12:51:29 +000051 bool InPPDirective = false);
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000052
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000053 /// Adds information about an unchangeable token's whitespace.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000054 ///
Manuel Klimek4fe43002013-05-22 12:51:29 +000055 /// Needs to be called for every token for which \c replaceWhitespace
56 /// was not called.
57 void addUntouchableToken(const FormatToken &Tok, bool InPPDirective);
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000058
Krasimir Georgiev9ad83fe2017-10-30 14:01:50 +000059 llvm::Error addReplacement(const tooling::Replacement &Replacement);
60
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000061 /// Inserts or replaces whitespace in the middle of a token.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000062 ///
Alexander Kornienko555efc32013-06-11 16:01:49 +000063 /// Inserts \p PreviousPostfix, \p Newlines, \p Spaces and \p CurrentPrefix
64 /// (in this order) at \p Offset inside \p Tok, replacing \p ReplaceChars
65 /// characters.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000066 ///
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +000067 /// Note: \p Spaces can be negative to retain information about initial
68 /// relative column offset between a line of a block comment and the start of
69 /// the comment. This negative offset may be compensated by trailing comment
70 /// alignment here. In all other cases negative \p Spaces will be truncated to
71 /// 0.
72 ///
Alexander Kornienko555efc32013-06-11 16:01:49 +000073 /// When \p InPPDirective is true, escaped newlines are inserted. \p Spaces is
74 /// used to align backslashes correctly.
75 void replaceWhitespaceInToken(const FormatToken &Tok, unsigned Offset,
76 unsigned ReplaceChars,
77 StringRef PreviousPostfix,
78 StringRef CurrentPrefix, bool InPPDirective,
Daniel Jasper7d42f3f2017-01-31 11:25:01 +000079 unsigned Newlines, int Spaces);
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000080
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000081 /// Returns all the \c Replacements created during formatting.
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000082 const tooling::Replacements &generateReplacements();
83
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000084 /// Represents a change before a token, a break inside a token,
Manuel Klimek4fe43002013-05-22 12:51:29 +000085 /// or the layout of an unchanged token (or whitespace within).
86 struct Change {
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000087 /// Functor to sort changes in original source order.
Manuel Klimek4fe43002013-05-22 12:51:29 +000088 class IsBeforeInFile {
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +000089 public:
Manuel Klimek4fe43002013-05-22 12:51:29 +000090 IsBeforeInFile(const SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
91 bool operator()(const Change &C1, const Change &C2) const;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +000092
Daniel Jasper3ac9b9e2013-07-08 14:34:09 +000093 private:
Manuel Klimek4fe43002013-05-22 12:51:29 +000094 const SourceManager &SourceMgr;
95 };
96
Adrian Prantl9fc8faf2018-05-09 01:00:01 +000097 /// Creates a \c Change.
Manuel Klimek4fe43002013-05-22 12:51:29 +000098 ///
99 /// The generated \c Change will replace the characters at
100 /// \p OriginalWhitespaceRange with a concatenation of
101 /// \p PreviousLinePostfix, \p NewlinesBefore line breaks, \p Spaces spaces
102 /// and \p CurrentLinePrefix.
103 ///
104 /// \p StartOfTokenColumn and \p InPPDirective will be used to lay out
105 /// trailing comments and escaped newlines.
Daniel Jasper7d42f3f2017-01-31 11:25:01 +0000106 Change(const FormatToken &Tok, bool CreateReplacement,
107 SourceRange OriginalWhitespaceRange, int Spaces,
108 unsigned StartOfTokenColumn, unsigned NewlinesBefore,
109 StringRef PreviousLinePostfix, StringRef CurrentLinePrefix,
110 bool ContinuesPPDirective, bool IsInsideToken);
111
112 // The kind of the token whose whitespace this change replaces, or in which
113 // this change inserts whitespace.
114 // FIXME: Currently this is not set correctly for breaks inside comments, as
115 // the \c BreakableToken is still doing its own alignment.
116 const FormatToken *Tok;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000117
118 bool CreateReplacement;
119 // Changes might be in the middle of a token, so we cannot just keep the
120 // FormatToken around to query its information.
121 SourceRange OriginalWhitespaceRange;
122 unsigned StartOfTokenColumn;
123 unsigned NewlinesBefore;
124 std::string PreviousLinePostfix;
125 std::string CurrentLinePrefix;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000126 bool ContinuesPPDirective;
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000127
Manuel Klimek4fe43002013-05-22 12:51:29 +0000128 // The number of spaces in front of the token or broken part of the token.
129 // This will be adapted when aligning tokens.
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000130 // Can be negative to retain information about the initial relative offset
131 // of the lines in a block comment. This is used when aligning trailing
132 // comments. Uncompensated negative offset is truncated to 0.
133 int Spaces;
Manuel Klimek4fe43002013-05-22 12:51:29 +0000134
Benjamin Kramerdab50462016-01-11 16:27:16 +0000135 // If this change is inside of a token but not at the start of the token or
136 // directly after a newline.
137 bool IsInsideToken;
138
Manuel Klimek4fe43002013-05-22 12:51:29 +0000139 // \c IsTrailingComment, \c TokenLength, \c PreviousEndOfTokenColumn and
140 // \c EscapedNewlineColumn will be calculated in
141 // \c calculateLineBreakInformation.
142 bool IsTrailingComment;
143 unsigned TokenLength;
144 unsigned PreviousEndOfTokenColumn;
145 unsigned EscapedNewlineColumn;
Alexander Kornienko67d9c8c2014-04-17 16:12:46 +0000146
147 // These fields are used to retain correct relative line indentation in a
148 // block comment when aligning trailing comments.
149 //
150 // If this Change represents a continuation of a block comment,
151 // \c StartOfBlockComment is pointer to the first Change in the block
152 // comment. \c IndentationOffset is a relative column offset to this
153 // change, so that the correct column can be reconstructed at the end of
154 // the alignment process.
155 const Change *StartOfBlockComment;
156 int IndentationOffset;
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000157
Daniel Jasper4917af62017-08-25 19:14:53 +0000158 // A combination of indent level and nesting level, which are used in
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000159 // tandem to compute lexical scope, for the purposes of deciding
160 // when to stop consecutive alignment runs.
Daniel Jasper4917af62017-08-25 19:14:53 +0000161 std::pair<unsigned, unsigned> indentAndNestingLevel() const {
162 return std::make_pair(Tok->IndentLevel, Tok->NestingLevel);
Nikola Smiljanic92b397f2017-03-23 02:51:25 +0000163 }
Manuel Klimek4fe43002013-05-22 12:51:29 +0000164 };
165
Daniel Jasperec90e512015-12-01 12:00:43 +0000166private:
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000167 /// Calculate \c IsTrailingComment, \c TokenLength for the last tokens
Manuel Klimek4fe43002013-05-22 12:51:29 +0000168 /// or token parts in a line and \c PreviousEndOfTokenColumn and
169 /// \c EscapedNewlineColumn for the first tokens or token parts in a line.
170 void calculateLineBreakInformation();
171
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000172 /// Align consecutive assignments over all \c Changes.
Daniel Jaspera44991332015-04-29 13:06:49 +0000173 void alignConsecutiveAssignments();
174
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000175 /// Align consecutive declarations over all \c Changes.
Daniel Jaspere12597c2015-10-01 10:06:54 +0000176 void alignConsecutiveDeclarations();
177
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000178 /// Align trailing comments over all \c Changes.
Manuel Klimek4fe43002013-05-22 12:51:29 +0000179 void alignTrailingComments();
180
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000181 /// Align trailing comments from change \p Start to change \p End at
Manuel Klimek4fe43002013-05-22 12:51:29 +0000182 /// the specified \p Column.
183 void alignTrailingComments(unsigned Start, unsigned End, unsigned Column);
184
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000185 /// Align escaped newlines over all \c Changes.
Daniel Jasper6fe2f002013-04-25 08:56:26 +0000186 void alignEscapedNewlines();
Daniel Jasper770eb7c2013-04-24 06:33:59 +0000187
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000188 /// Align escaped newlines from change \p Start to change \p End at
Manuel Klimek4fe43002013-05-22 12:51:29 +0000189 /// the specified \p Column.
190 void alignEscapedNewlines(unsigned Start, unsigned End, unsigned Column);
191
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000192 /// Fill \c Replaces with the replacements for all effective changes.
Manuel Klimek4fe43002013-05-22 12:51:29 +0000193 void generateChanges();
194
Adrian Prantl9fc8faf2018-05-09 01:00:01 +0000195 /// Stores \p Text as the replacement for the whitespace in \p Range.
Craig Toppere335f252015-10-04 04:53:55 +0000196 void storeReplacement(SourceRange Range, StringRef Text);
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000197 void appendNewlineText(std::string &Text, unsigned Newlines);
Jacob Bandes-Storchd6a7e982017-08-10 00:15:31 +0000198 void appendEscapedNewlineText(std::string &Text, unsigned Newlines,
199 unsigned PreviousEndOfTokenColumn,
200 unsigned EscapedNewlineColumn);
Alexander Kornienko3c3d09c2013-09-27 16:14:22 +0000201 void appendIndentText(std::string &Text, unsigned IndentLevel,
202 unsigned Spaces, unsigned WhitespaceStartColumn);
Manuel Klimekb9eae4c2013-05-13 09:22:11 +0000203
Manuel Klimek4fe43002013-05-22 12:51:29 +0000204 SmallVector<Change, 16> Changes;
Eric Liu635423e2016-04-28 07:52:03 +0000205 const SourceManager &SourceMgr;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000206 tooling::Replacements Replaces;
207 const FormatStyle &Style;
Alexander Kornienko9e649af2013-09-11 12:25:57 +0000208 bool UseCRLF;
Alexander Kornienkocb45bc12013-04-15 14:28:00 +0000209};
210
211} // namespace format
212} // namespace clang
213
Benjamin Kramer2f5db8b2014-08-13 16:25:19 +0000214#endif