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