blob: 25b7690a154f9f8dd364339159f0e80f8fa4d53d [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
55 /// \brief Inserts a line break into the middle of a token.
56 ///
Manuel Klimeke573c3f2013-05-22 12:51:29 +000057 /// Will break at \p Offset inside \p Tok, putting \p PreviousPostfix before
58 /// the line break and \p CurrentPrefix before the rest of the token starts in
59 /// the next line.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000060 ///
Manuel Klimeke573c3f2013-05-22 12:51:29 +000061 /// \p InPPDirective and \p Spaces are used to generate the correct line
62 /// break.
Alexander Kornienko70ce7882013-04-15 14:28:00 +000063 void breakToken(const FormatToken &Tok, unsigned Offset,
Manuel Klimeke573c3f2013-05-22 12:51:29 +000064 unsigned ReplaceChars, StringRef PreviousPostfix,
65 StringRef CurrentPrefix, bool InPPDirective, unsigned Spaces);
Alexander Kornienko70ce7882013-04-15 14:28:00 +000066
67 /// \brief Returns all the \c Replacements created during formatting.
68 const tooling::Replacements &generateReplacements();
69
Manuel Klimeke573c3f2013-05-22 12:51:29 +000070private:
71 /// \brief Represents a change before a token, a break inside a token,
72 /// or the layout of an unchanged token (or whitespace within).
73 struct Change {
74 /// \brief Functor to sort changes in original source order.
75 class IsBeforeInFile {
76 public:
77 IsBeforeInFile(const SourceManager &SourceMgr) : SourceMgr(SourceMgr) {}
78 bool operator()(const Change &C1, const Change &C2) const;
Alexander Kornienko70ce7882013-04-15 14:28:00 +000079
Manuel Klimeke573c3f2013-05-22 12:51:29 +000080 private:
81 const SourceManager &SourceMgr;
82 };
83
84 Change() {}
85
86 /// \brief Creates a \c Change.
87 ///
88 /// The generated \c Change will replace the characters at
89 /// \p OriginalWhitespaceRange with a concatenation of
90 /// \p PreviousLinePostfix, \p NewlinesBefore line breaks, \p Spaces spaces
91 /// and \p CurrentLinePrefix.
92 ///
93 /// \p StartOfTokenColumn and \p InPPDirective will be used to lay out
94 /// trailing comments and escaped newlines.
95 Change(bool CreateReplacement, const SourceRange &OriginalWhitespaceRange,
96 unsigned Spaces, unsigned StartOfTokenColumn,
97 unsigned NewlinesBefore, StringRef PreviousLinePostfix,
98 StringRef CurrentLinePrefix, tok::TokenKind Kind,
99 bool ContinuesPPDirective);
100
101 bool CreateReplacement;
102 // Changes might be in the middle of a token, so we cannot just keep the
103 // FormatToken around to query its information.
104 SourceRange OriginalWhitespaceRange;
105 unsigned StartOfTokenColumn;
106 unsigned NewlinesBefore;
107 std::string PreviousLinePostfix;
108 std::string CurrentLinePrefix;
109 // The kind of the token whose whitespace this change replaces, or in which
110 // this change inserts whitespace.
111 // FIXME: Currently this is not set correctly for breaks inside comments, as
112 // the \c BreakableToken is still doing its own alignment.
113 tok::TokenKind Kind;
114 bool ContinuesPPDirective;
115
116 // The number of spaces in front of the token or broken part of the token.
117 // This will be adapted when aligning tokens.
118 unsigned Spaces;
119
120 // \c IsTrailingComment, \c TokenLength, \c PreviousEndOfTokenColumn and
121 // \c EscapedNewlineColumn will be calculated in
122 // \c calculateLineBreakInformation.
123 bool IsTrailingComment;
124 unsigned TokenLength;
125 unsigned PreviousEndOfTokenColumn;
126 unsigned EscapedNewlineColumn;
127
128 };
129
130 /// \brief Calculate \c IsTrailingComment, \c TokenLength for the last tokens
131 /// or token parts in a line and \c PreviousEndOfTokenColumn and
132 /// \c EscapedNewlineColumn for the first tokens or token parts in a line.
133 void calculateLineBreakInformation();
134
135 /// \brief Align trailing comments over all \c Changes.
136 void alignTrailingComments();
137
138 /// \brief Align trailing comments from change \p Start to change \p End at
139 /// the specified \p Column.
140 void alignTrailingComments(unsigned Start, unsigned End, unsigned Column);
141
142 /// \brief Align escaped newlines over all \c Changes.
Daniel Jasper2972d042013-04-25 08:56:26 +0000143 void alignEscapedNewlines();
Daniel Jasperaf849762013-04-24 06:33:59 +0000144
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000145 /// \brief Align escaped newlines from change \p Start to change \p End at
146 /// the specified \p Column.
147 void alignEscapedNewlines(unsigned Start, unsigned End, unsigned Column);
148
149 /// \brief Fill \c Replaces with the replacements for all effective changes.
150 void generateChanges();
151
152 /// \brief Stores \p Text as the replacement for the whitespace in \p Range.
153 void storeReplacement(const SourceRange &Range, StringRef Text);
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000154 std::string getNewLineText(unsigned NewLines, unsigned Spaces);
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000155 std::string getNewLineText(unsigned NewLines, unsigned Spaces,
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000156 unsigned PreviousEndOfTokenColumn,
Daniel Jasper2972d042013-04-25 08:56:26 +0000157 unsigned EscapedNewlineColumn);
Manuel Klimek7c9a93e2013-05-13 09:22:11 +0000158 std::string getIndentText(unsigned Spaces);
159
Manuel Klimeke573c3f2013-05-22 12:51:29 +0000160 SmallVector<Change, 16> Changes;
Alexander Kornienko70ce7882013-04-15 14:28:00 +0000161 SourceManager &SourceMgr;
162 tooling::Replacements Replaces;
163 const FormatStyle &Style;
164};
165
166} // namespace format
167} // namespace clang
168
169#endif // LLVM_CLANG_FORMAT_WHITESPACEMANAGER_H