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. |
| 31 | class WhitespaceManager { |
| 32 | public: |
| 33 | WhitespaceManager(SourceManager &SourceMgr, const FormatStyle &Style) |
| 34 | : SourceMgr(SourceMgr), Style(Style) {} |
| 35 | |
| 36 | /// \brief Replaces the whitespace in front of \p Tok. Only call once for |
| 37 | /// each \c AnnotatedToken. |
| 38 | void replaceWhitespace(const AnnotatedToken &Tok, unsigned NewLines, |
| 39 | unsigned Spaces, unsigned WhitespaceStartColumn); |
| 40 | |
| 41 | /// \brief Like \c replaceWhitespace, but additionally adds right-aligned |
| 42 | /// backslashes to escape newlines inside a preprocessor directive. |
| 43 | /// |
| 44 | /// This function and \c replaceWhitespace have the same behavior if |
| 45 | /// \c Newlines == 0. |
| 46 | void replacePPWhitespace(const AnnotatedToken &Tok, unsigned NewLines, |
| 47 | unsigned Spaces, unsigned WhitespaceStartColumn); |
| 48 | |
| 49 | /// \brief Inserts a line break into the middle of a token. |
| 50 | /// |
| 51 | /// Will break at \p Offset inside \p Tok, putting \p Prefix before the line |
| 52 | /// break and \p Postfix before the rest of the token starts in the next line. |
| 53 | /// |
| 54 | /// \p InPPDirective, \p Spaces, \p WhitespaceStartColumn and \p Style are |
| 55 | /// used to generate the correct line break. |
| 56 | void breakToken(const FormatToken &Tok, unsigned Offset, |
| 57 | unsigned ReplaceChars, StringRef Prefix, StringRef Postfix, |
| 58 | bool InPPDirective, unsigned Spaces, |
| 59 | unsigned WhitespaceStartColumn); |
| 60 | |
| 61 | /// \brief Returns all the \c Replacements created during formatting. |
| 62 | const tooling::Replacements &generateReplacements(); |
| 63 | |
| 64 | void addReplacement(const SourceLocation &SourceLoc, unsigned ReplaceChars, |
| 65 | StringRef Text); |
| 66 | |
| 67 | void addUntouchableComment(unsigned Column); |
| 68 | |
Daniel Jasper | af84976 | 2013-04-24 06:33:59 +0000 | [diff] [blame] | 69 | /// \brief Try to align all stashed comments. |
| 70 | void alignComments(); |
Daniel Jasper | 2972d04 | 2013-04-25 08:56:26 +0000 | [diff] [blame] | 71 | /// \brief Try to align all stashed escaped newlines. |
| 72 | void alignEscapedNewlines(); |
Daniel Jasper | af84976 | 2013-04-24 06:33:59 +0000 | [diff] [blame] | 73 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 74 | private: |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 75 | std::string getNewLineText(unsigned NewLines, unsigned Spaces); |
| 76 | |
| 77 | std::string getNewLineText(unsigned NewLines, unsigned Spaces, |
Daniel Jasper | 2972d04 | 2013-04-25 08:56:26 +0000 | [diff] [blame] | 78 | unsigned WhitespaceStartColumn, |
| 79 | unsigned EscapedNewlineColumn); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 80 | |
Manuel Klimek | 7c9a93e | 2013-05-13 09:22:11 +0000 | [diff] [blame^] | 81 | std::string getIndentText(unsigned Spaces); |
| 82 | |
Daniel Jasper | 2972d04 | 2013-04-25 08:56:26 +0000 | [diff] [blame] | 83 | /// \brief Structure to store tokens for later layout and alignment. |
| 84 | struct StoredToken { |
| 85 | StoredToken(SourceLocation ReplacementLoc, unsigned ReplacementLength, |
| 86 | unsigned MinColumn, unsigned MaxColumn, unsigned NewLines, |
| 87 | unsigned Spaces) |
| 88 | : ReplacementLoc(ReplacementLoc), ReplacementLength(ReplacementLength), |
| 89 | MinColumn(MinColumn), MaxColumn(MaxColumn), NewLines(NewLines), |
| 90 | Spaces(Spaces), Untouchable(false) {} |
| 91 | SourceLocation ReplacementLoc; |
| 92 | unsigned ReplacementLength; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 93 | unsigned MinColumn; |
| 94 | unsigned MaxColumn; |
| 95 | unsigned NewLines; |
| 96 | unsigned Spaces; |
| 97 | bool Untouchable; |
Daniel Jasper | 2972d04 | 2013-04-25 08:56:26 +0000 | [diff] [blame] | 98 | std::string Prefix; |
| 99 | std::string Postfix; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 100 | }; |
Daniel Jasper | 2972d04 | 2013-04-25 08:56:26 +0000 | [diff] [blame] | 101 | SmallVector<StoredToken, 16> Comments; |
| 102 | SmallVector<StoredToken, 16> EscapedNewlines; |
| 103 | typedef SmallVector<StoredToken, 16>::iterator token_iterator; |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 104 | |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 105 | /// \brief Put all the comments between \p I and \p E into \p Column. |
Daniel Jasper | 2972d04 | 2013-04-25 08:56:26 +0000 | [diff] [blame] | 106 | void alignComments(token_iterator I, token_iterator E, unsigned Column); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 107 | |
| 108 | /// \brief Stores \p Text as the replacement for the whitespace in front of |
| 109 | /// \p Tok. |
Daniel Jasper | 2972d04 | 2013-04-25 08:56:26 +0000 | [diff] [blame] | 110 | void storeReplacement(SourceLocation Loc, unsigned Length, |
| 111 | const std::string Text); |
Alexander Kornienko | 70ce788 | 2013-04-15 14:28:00 +0000 | [diff] [blame] | 112 | |
| 113 | SourceManager &SourceMgr; |
| 114 | tooling::Replacements Replaces; |
| 115 | const FormatStyle &Style; |
| 116 | }; |
| 117 | |
| 118 | } // namespace format |
| 119 | } // namespace clang |
| 120 | |
| 121 | #endif // LLVM_CLANG_FORMAT_WHITESPACEMANAGER_H |