Daniel Jasper | 2972d04 | 2013-04-25 08:56:26 +0000 | [diff] [blame] | 1 | //===--- WhitespaceManager.h - Format C++ code ------------------*- C++ -*-===// |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 2 | // |
| 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 | |
| 24 | namespace clang { |
| 25 | namespace 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 Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 31 | /// |
| 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 Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 38 | class WhitespaceManager { |
| 39 | public: |
Alexander Kornienko | 73d845c | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 40 | WhitespaceManager(SourceManager &SourceMgr, const FormatStyle &Style, |
| 41 | bool UseCRLF) |
| 42 | : SourceMgr(SourceMgr), Style(Style), UseCRLF(UseCRLF) {} |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 43 | |
| 44 | /// \brief Replaces the whitespace in front of \p Tok. Only call once for |
| 45 | /// each \c AnnotatedToken. |
Manuel Klimek | b398701 | 2013-05-29 14:47:47 +0000 | [diff] [blame] | 46 | void replaceWhitespace(const FormatToken &Tok, unsigned Newlines, |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 47 | unsigned Spaces, unsigned StartOfTokenColumn, |
| 48 | bool InPPDirective = false); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 49 | |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 50 | /// \brief Adds information about an unchangable token's whitespace. |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 51 | /// |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 52 | /// Needs to be called for every token for which \c replaceWhitespace |
| 53 | /// was not called. |
| 54 | void addUntouchableToken(const FormatToken &Tok, bool InPPDirective); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 55 | |
Alexander Kornienko | 2b2faa5 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 56 | /// \brief Inserts or replaces whitespace in the middle of a token. |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 57 | /// |
Alexander Kornienko | 2b2faa5 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 58 | /// Inserts \p PreviousPostfix, \p Newlines, \p Spaces and \p CurrentPrefix |
| 59 | /// (in this order) at \p Offset inside \p Tok, replacing \p ReplaceChars |
| 60 | /// characters. |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 61 | /// |
Alexander Kornienko | 2b2faa5 | 2013-06-11 16:01:49 +0000 | [diff] [blame] | 62 | /// When \p InPPDirective is true, escaped newlines are inserted. \p Spaces is |
| 63 | /// used to align backslashes correctly. |
| 64 | void replaceWhitespaceInToken(const FormatToken &Tok, unsigned Offset, |
| 65 | unsigned ReplaceChars, |
| 66 | StringRef PreviousPostfix, |
| 67 | StringRef CurrentPrefix, bool InPPDirective, |
| 68 | unsigned Newlines, unsigned Spaces); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 69 | |
| 70 | /// \brief Returns all the \c Replacements created during formatting. |
| 71 | const tooling::Replacements &generateReplacements(); |
| 72 | |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 73 | private: |
| 74 | /// \brief Represents a change before a token, a break inside a token, |
| 75 | /// or the layout of an unchanged token (or whitespace within). |
| 76 | struct Change { |
| 77 | /// \brief Functor to sort changes in original source order. |
| 78 | class IsBeforeInFile { |
Daniel Jasper | 2a409b6 | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 79 | public: |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 80 | IsBeforeInFile(const SourceManager &SourceMgr) : SourceMgr(SourceMgr) {} |
| 81 | bool operator()(const Change &C1, const Change &C2) const; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 82 | |
Daniel Jasper | 2a409b6 | 2013-07-08 14:34:09 +0000 | [diff] [blame] | 83 | private: |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 84 | const SourceManager &SourceMgr; |
| 85 | }; |
| 86 | |
| 87 | Change() {} |
| 88 | |
| 89 | /// \brief Creates a \c Change. |
| 90 | /// |
| 91 | /// The generated \c Change will replace the characters at |
| 92 | /// \p OriginalWhitespaceRange with a concatenation of |
| 93 | /// \p PreviousLinePostfix, \p NewlinesBefore line breaks, \p Spaces spaces |
| 94 | /// and \p CurrentLinePrefix. |
| 95 | /// |
| 96 | /// \p StartOfTokenColumn and \p InPPDirective will be used to lay out |
| 97 | /// trailing comments and escaped newlines. |
| 98 | Change(bool CreateReplacement, const SourceRange &OriginalWhitespaceRange, |
| 99 | unsigned Spaces, unsigned StartOfTokenColumn, |
| 100 | unsigned NewlinesBefore, StringRef PreviousLinePostfix, |
| 101 | StringRef CurrentLinePrefix, tok::TokenKind Kind, |
| 102 | bool ContinuesPPDirective); |
| 103 | |
| 104 | bool CreateReplacement; |
| 105 | // Changes might be in the middle of a token, so we cannot just keep the |
| 106 | // FormatToken around to query its information. |
| 107 | SourceRange OriginalWhitespaceRange; |
| 108 | unsigned StartOfTokenColumn; |
| 109 | unsigned NewlinesBefore; |
| 110 | std::string PreviousLinePostfix; |
| 111 | std::string CurrentLinePrefix; |
| 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 | tok::TokenKind Kind; |
| 117 | bool ContinuesPPDirective; |
| 118 | |
| 119 | // The number of spaces in front of the token or broken part of the token. |
| 120 | // This will be adapted when aligning tokens. |
| 121 | unsigned Spaces; |
| 122 | |
| 123 | // \c IsTrailingComment, \c TokenLength, \c PreviousEndOfTokenColumn and |
| 124 | // \c EscapedNewlineColumn will be calculated in |
| 125 | // \c calculateLineBreakInformation. |
| 126 | bool IsTrailingComment; |
| 127 | unsigned TokenLength; |
| 128 | unsigned PreviousEndOfTokenColumn; |
| 129 | unsigned EscapedNewlineColumn; |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | /// \brief Calculate \c IsTrailingComment, \c TokenLength for the last tokens |
| 133 | /// or token parts in a line and \c PreviousEndOfTokenColumn and |
| 134 | /// \c EscapedNewlineColumn for the first tokens or token parts in a line. |
| 135 | void calculateLineBreakInformation(); |
| 136 | |
| 137 | /// \brief Align trailing comments over all \c Changes. |
| 138 | void alignTrailingComments(); |
| 139 | |
| 140 | /// \brief Align trailing comments from change \p Start to change \p End at |
| 141 | /// the specified \p Column. |
| 142 | void alignTrailingComments(unsigned Start, unsigned End, unsigned Column); |
| 143 | |
| 144 | /// \brief Align escaped newlines over all \c Changes. |
Daniel Jasper | 2972d04 | 2013-04-25 08:56:26 +0000 | [diff] [blame] | 145 | void alignEscapedNewlines(); |
Daniel Jasper | af84976 | 2013-04-24 06:33:59 +0000 | [diff] [blame] | 146 | |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 147 | /// \brief Align escaped newlines from change \p Start to change \p End at |
| 148 | /// the specified \p Column. |
| 149 | void alignEscapedNewlines(unsigned Start, unsigned End, unsigned Column); |
| 150 | |
| 151 | /// \brief Fill \c Replaces with the replacements for all effective changes. |
| 152 | void generateChanges(); |
| 153 | |
| 154 | /// \brief Stores \p Text as the replacement for the whitespace in \p Range. |
| 155 | void storeReplacement(const SourceRange &Range, StringRef Text); |
Alexander Kornienko | 73d845c | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 156 | void appendNewlineText(std::string &Text, unsigned Newlines); |
| 157 | void appendNewlineText(std::string &Text, unsigned Newlines, |
| 158 | unsigned PreviousEndOfTokenColumn, |
| 159 | unsigned EscapedNewlineColumn); |
Alexander Kornienko | acf8e90 | 2013-09-27 09:45:40 +0000 | [diff] [blame^] | 160 | void appendIndentText(std::string &Text, unsigned Spaces, |
| 161 | unsigned WhitespaceStartColumn); |
Manuel Klimek | 7c9a93e | 2013-05-13 09:22:11 +0000 | [diff] [blame] | 162 | |
Manuel Klimek | e573c3f | 2013-05-22 12:51:29 +0000 | [diff] [blame] | 163 | SmallVector<Change, 16> Changes; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 164 | SourceManager &SourceMgr; |
| 165 | tooling::Replacements Replaces; |
| 166 | const FormatStyle &Style; |
Alexander Kornienko | 73d845c | 2013-09-11 12:25:57 +0000 | [diff] [blame] | 167 | bool UseCRLF; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 168 | }; |
| 169 | |
| 170 | } // namespace format |
| 171 | } // namespace clang |
| 172 | |
| 173 | #endif // LLVM_CLANG_FORMAT_WHITESPACEMANAGER_H |