blob: 93d331e1251fcc7d9367b950de150231ebe83b09 [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:
Alexander Kornienko73d845c2013-09-11 12:25:57 +000040 WhitespaceManager(SourceManager &SourceMgr, const FormatStyle &Style,
41 bool UseCRLF)
42 : SourceMgr(SourceMgr), Style(Style), UseCRLF(UseCRLF) {}
Alexander Kornienko70ce7882013-04-15 14:28:00 +000043
44 /// \brief Replaces the whitespace in front of \p Tok. Only call once for
45 /// each \c AnnotatedToken.
Manuel Klimekb3987012013-05-29 14:47:47 +000046 void replaceWhitespace(const FormatToken &Tok, unsigned Newlines,
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +000047 unsigned IndentLevel, unsigned Spaces,
48 unsigned StartOfTokenColumn,
Manuel Klimeke573c3f2013-05-22 12:51:29 +000049 bool InPPDirective = false);
Alexander Kornienko70ce7882013-04-15 14:28:00 +000050
Manuel Klimeke573c3f2013-05-22 12:51:29 +000051 /// \brief Adds information about an unchangable token's whitespace.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000052 ///
Manuel Klimeke573c3f2013-05-22 12:51:29 +000053 /// Needs to be called for every token for which \c replaceWhitespace
54 /// was not called.
55 void addUntouchableToken(const FormatToken &Tok, bool InPPDirective);
Alexander Kornienko70ce7882013-04-15 14:28:00 +000056
Alexander Kornienko2b2faa52013-06-11 16:01:49 +000057 /// \brief Inserts or replaces whitespace in the middle of a token.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000058 ///
Alexander Kornienko2b2faa52013-06-11 16:01:49 +000059 /// 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.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000062 ///
Alexander Kornienko2b2faa52013-06-11 16:01:49 +000063 /// When \p InPPDirective is true, escaped newlines are inserted. \p Spaces is
64 /// used to align backslashes correctly.
65 void replaceWhitespaceInToken(const FormatToken &Tok, unsigned Offset,
66 unsigned ReplaceChars,
67 StringRef PreviousPostfix,
68 StringRef CurrentPrefix, bool InPPDirective,
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +000069 unsigned Newlines, unsigned IndentLevel,
70 unsigned Spaces);
Alexander Kornienko70ce7882013-04-15 14:28:00 +000071
72 /// \brief Returns all the \c Replacements created during formatting.
73 const tooling::Replacements &generateReplacements();
74
Manuel Klimeke573c3f2013-05-22 12:51:29 +000075private:
76 /// \brief Represents a change before a token, a break inside a token,
77 /// or the layout of an unchanged token (or whitespace within).
78 struct Change {
79 /// \brief Functor to sort changes in original source order.
80 class IsBeforeInFile {
Daniel Jasper2a409b62013-07-08 14:34:09 +000081 public:
Manuel Klimeke573c3f2013-05-22 12:51:29 +000082 IsBeforeInFile(const SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
83 bool operator()(const Change &C1, const Change &C2) const;
Alexander Kornienko70ce7882013-04-15 14:28:00 +000084
Daniel Jasper2a409b62013-07-08 14:34:09 +000085 private:
Manuel Klimeke573c3f2013-05-22 12:51:29 +000086 const SourceManager &SourceMgr;
87 };
88
89 Change() {}
90
91 /// \brief Creates a \c Change.
92 ///
93 /// The generated \c Change will replace the characters at
94 /// \p OriginalWhitespaceRange with a concatenation of
95 /// \p PreviousLinePostfix, \p NewlinesBefore line breaks, \p Spaces spaces
96 /// and \p CurrentLinePrefix.
97 ///
98 /// \p StartOfTokenColumn and \p InPPDirective will be used to lay out
99 /// trailing comments and escaped newlines.
100 Change(bool CreateReplacement, const SourceRange &OriginalWhitespaceRange,
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +0000101 unsigned IndentLevel, unsigned Spaces, unsigned StartOfTokenColumn,
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000102 unsigned NewlinesBefore, StringRef PreviousLinePostfix,
103 StringRef CurrentLinePrefix, tok::TokenKind Kind,
104 bool ContinuesPPDirective);
105
106 bool CreateReplacement;
107 // Changes might be in the middle of a token, so we cannot just keep the
108 // FormatToken around to query its information.
109 SourceRange OriginalWhitespaceRange;
110 unsigned StartOfTokenColumn;
111 unsigned NewlinesBefore;
112 std::string PreviousLinePostfix;
113 std::string CurrentLinePrefix;
114 // The kind of the token whose whitespace this change replaces, or in which
115 // this change inserts whitespace.
116 // FIXME: Currently this is not set correctly for breaks inside comments, as
117 // the \c BreakableToken is still doing its own alignment.
118 tok::TokenKind Kind;
119 bool ContinuesPPDirective;
120
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +0000121 // The number of nested blocks the token is in. This is used to add tabs
122 // only for the indentation, and not for alignment, when
123 // UseTab = US_ForIndentation.
124 unsigned IndentLevel;
125
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000126 // The number of spaces in front of the token or broken part of the token.
127 // This will be adapted when aligning tokens.
128 unsigned Spaces;
129
130 // \c IsTrailingComment, \c TokenLength, \c PreviousEndOfTokenColumn and
131 // \c EscapedNewlineColumn will be calculated in
132 // \c calculateLineBreakInformation.
133 bool IsTrailingComment;
134 unsigned TokenLength;
135 unsigned PreviousEndOfTokenColumn;
136 unsigned EscapedNewlineColumn;
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000137 };
138
139 /// \brief Calculate \c IsTrailingComment, \c TokenLength for the last tokens
140 /// or token parts in a line and \c PreviousEndOfTokenColumn and
141 /// \c EscapedNewlineColumn for the first tokens or token parts in a line.
142 void calculateLineBreakInformation();
143
144 /// \brief Align trailing comments over all \c Changes.
145 void alignTrailingComments();
146
147 /// \brief Align trailing comments from change \p Start to change \p End at
148 /// the specified \p Column.
149 void alignTrailingComments(unsigned Start, unsigned End, unsigned Column);
150
151 /// \brief Align escaped newlines over all \c Changes.
Daniel Jasper2972d042013-04-25 08:56:26 +0000152 void alignEscapedNewlines();
Daniel Jasperaf849762013-04-24 06:33:59 +0000153
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000154 /// \brief Align escaped newlines from change \p Start to change \p End at
155 /// the specified \p Column.
156 void alignEscapedNewlines(unsigned Start, unsigned End, unsigned Column);
157
158 /// \brief Fill \c Replaces with the replacements for all effective changes.
159 void generateChanges();
160
161 /// \brief Stores \p Text as the replacement for the whitespace in \p Range.
162 void storeReplacement(const SourceRange &Range, StringRef Text);
Alexander Kornienko73d845c2013-09-11 12:25:57 +0000163 void appendNewlineText(std::string &Text, unsigned Newlines);
164 void appendNewlineText(std::string &Text, unsigned Newlines,
165 unsigned PreviousEndOfTokenColumn,
166 unsigned EscapedNewlineColumn);
Alexander Kornienko3d9ffcf2013-09-27 16:14:22 +0000167 void appendIndentText(std::string &Text, unsigned IndentLevel,
168 unsigned Spaces, unsigned WhitespaceStartColumn);
Manuel Klimek7c9a93e2013-05-13 09:22:11 +0000169
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000170 SmallVector<Change, 16> Changes;
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000171 SourceManager &SourceMgr;
172 tooling::Replacements Replaces;
173 const FormatStyle &Style;
Alexander Kornienko73d845c2013-09-11 12:25:57 +0000174 bool UseCRLF;
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000175};
176
177} // namespace format
178} // namespace clang
179
180#endif // LLVM_CLANG_FORMAT_WHITESPACEMANAGER_H