blob: 9ca34806374d1744a26003247c4a809fc39c256e [file] [log] [blame]
Daniel Jasper2972d042013-04-25 08:56:26 +00001//===--- WhitespaceManager.h - Format C++ code ------------------*- C++ -*-===//
Alexander Kornienko70ce7882013-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
16#ifndef LLVM_CLANG_FORMAT_WHITESPACEMANAGER_H
17#define LLVM_CLANG_FORMAT_WHITESPACEMANAGER_H
18
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 Klimeke573c3f2013-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 Kornienko70ce7882013-04-15 14:28:00 +000038class WhitespaceManager {
39public:
40 WhitespaceManager(SourceManager &SourceMgr, const FormatStyle &Style)
41 : SourceMgr(SourceMgr), Style(Style) {}
42
43 /// \brief Replaces the whitespace in front of \p Tok. Only call once for
44 /// each \c AnnotatedToken.
Manuel Klimekb3987012013-05-29 14:47:47 +000045 void replaceWhitespace(const FormatToken &Tok, unsigned Newlines,
Manuel Klimeke573c3f2013-05-22 12:51:29 +000046 unsigned Spaces, unsigned StartOfTokenColumn,
47 bool InPPDirective = false);
Alexander Kornienko70ce7882013-04-15 14:28:00 +000048
Manuel Klimeke573c3f2013-05-22 12:51:29 +000049 /// \brief Adds information about an unchangable token's whitespace.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000050 ///
Manuel Klimeke573c3f2013-05-22 12:51:29 +000051 /// Needs to be called for every token for which \c replaceWhitespace
52 /// was not called.
53 void addUntouchableToken(const FormatToken &Tok, bool InPPDirective);
Alexander Kornienko70ce7882013-04-15 14:28:00 +000054
Alexander Kornienko2b2faa52013-06-11 16:01:49 +000055 /// \brief Inserts or replaces whitespace in the middle of a token.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000056 ///
Alexander Kornienko2b2faa52013-06-11 16:01:49 +000057 /// Inserts \p PreviousPostfix, \p Newlines, \p Spaces and \p CurrentPrefix
58 /// (in this order) at \p Offset inside \p Tok, replacing \p ReplaceChars
59 /// characters.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000060 ///
Alexander Kornienko2b2faa52013-06-11 16:01:49 +000061 /// When \p InPPDirective is true, escaped newlines are inserted. \p Spaces is
62 /// used to align backslashes correctly.
63 void replaceWhitespaceInToken(const FormatToken &Tok, unsigned Offset,
64 unsigned ReplaceChars,
65 StringRef PreviousPostfix,
66 StringRef CurrentPrefix, bool InPPDirective,
67 unsigned Newlines, unsigned Spaces);
Alexander Kornienko70ce7882013-04-15 14:28:00 +000068
69 /// \brief Returns all the \c Replacements created during formatting.
70 const tooling::Replacements &generateReplacements();
71
Manuel Klimeke573c3f2013-05-22 12:51:29 +000072private:
73 /// \brief Represents a change before a token, a break inside a token,
74 /// or the layout of an unchanged token (or whitespace within).
75 struct Change {
76 /// \brief Functor to sort changes in original source order.
77 class IsBeforeInFile {
Daniel Jasper2a409b62013-07-08 14:34:09 +000078 public:
Manuel Klimeke573c3f2013-05-22 12:51:29 +000079 IsBeforeInFile(const SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
80 bool operator()(const Change &C1, const Change &C2) const;
Alexander Kornienko70ce7882013-04-15 14:28:00 +000081
Daniel Jasper2a409b62013-07-08 14:34:09 +000082 private:
Manuel Klimeke573c3f2013-05-22 12:51:29 +000083 const SourceManager &SourceMgr;
84 };
85
86 Change() {}
87
88 /// \brief Creates a \c Change.
89 ///
90 /// The generated \c Change will replace the characters at
91 /// \p OriginalWhitespaceRange with a concatenation of
92 /// \p PreviousLinePostfix, \p NewlinesBefore line breaks, \p Spaces spaces
93 /// and \p CurrentLinePrefix.
94 ///
95 /// \p StartOfTokenColumn and \p InPPDirective will be used to lay out
96 /// trailing comments and escaped newlines.
97 Change(bool CreateReplacement, const SourceRange &OriginalWhitespaceRange,
98 unsigned Spaces, unsigned StartOfTokenColumn,
99 unsigned NewlinesBefore, StringRef PreviousLinePostfix,
100 StringRef CurrentLinePrefix, tok::TokenKind Kind,
101 bool ContinuesPPDirective);
102
103 bool CreateReplacement;
104 // Changes might be in the middle of a token, so we cannot just keep the
105 // FormatToken around to query its information.
106 SourceRange OriginalWhitespaceRange;
107 unsigned StartOfTokenColumn;
108 unsigned NewlinesBefore;
109 std::string PreviousLinePostfix;
110 std::string CurrentLinePrefix;
111 // The kind of the token whose whitespace this change replaces, or in which
112 // this change inserts whitespace.
113 // FIXME: Currently this is not set correctly for breaks inside comments, as
114 // the \c BreakableToken is still doing its own alignment.
115 tok::TokenKind Kind;
116 bool ContinuesPPDirective;
117
118 // The number of spaces in front of the token or broken part of the token.
119 // This will be adapted when aligning tokens.
120 unsigned Spaces;
121
122 // \c IsTrailingComment, \c TokenLength, \c PreviousEndOfTokenColumn and
123 // \c EscapedNewlineColumn will be calculated in
124 // \c calculateLineBreakInformation.
125 bool IsTrailingComment;
126 unsigned TokenLength;
127 unsigned PreviousEndOfTokenColumn;
128 unsigned EscapedNewlineColumn;
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000129 };
130
131 /// \brief Calculate \c IsTrailingComment, \c TokenLength for the last tokens
132 /// or token parts in a line and \c PreviousEndOfTokenColumn and
133 /// \c EscapedNewlineColumn for the first tokens or token parts in a line.
134 void calculateLineBreakInformation();
135
136 /// \brief Align trailing comments over all \c Changes.
137 void alignTrailingComments();
138
139 /// \brief Align trailing comments from change \p Start to change \p End at
140 /// the specified \p Column.
141 void alignTrailingComments(unsigned Start, unsigned End, unsigned Column);
142
143 /// \brief Align escaped newlines over all \c Changes.
Daniel Jasper2972d042013-04-25 08:56:26 +0000144 void alignEscapedNewlines();
Daniel Jasperaf849762013-04-24 06:33:59 +0000145
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000146 /// \brief Align escaped newlines from change \p Start to change \p End at
147 /// the specified \p Column.
148 void alignEscapedNewlines(unsigned Start, unsigned End, unsigned Column);
149
150 /// \brief Fill \c Replaces with the replacements for all effective changes.
151 void generateChanges();
152
153 /// \brief Stores \p Text as the replacement for the whitespace in \p Range.
154 void storeReplacement(const SourceRange &Range, StringRef Text);
Alexander Kornienko2b2faa52013-06-11 16:01:49 +0000155 std::string getNewlineText(unsigned Newlines, unsigned Spaces);
156 std::string getNewlineText(unsigned Newlines, unsigned Spaces,
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000157 unsigned PreviousEndOfTokenColumn,
Daniel Jasper2972d042013-04-25 08:56:26 +0000158 unsigned EscapedNewlineColumn);
Manuel Klimek7c9a93e2013-05-13 09:22:11 +0000159 std::string getIndentText(unsigned Spaces);
160
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000161 SmallVector<Change, 16> Changes;
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000162 SourceManager &SourceMgr;
163 tooling::Replacements Replaces;
164 const FormatStyle &Style;
165};
166
167} // namespace format
168} // namespace clang
169
170#endif // LLVM_CLANG_FORMAT_WHITESPACEMANAGER_H