WhitespaceManager.h revision 263508
1//===--- WhitespaceManager.h - Format C++ code ------------------*- C++ -*-===// 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 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. 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. 38class WhitespaceManager { 39public: 40 WhitespaceManager(SourceManager &SourceMgr, const FormatStyle &Style, 41 bool UseCRLF) 42 : SourceMgr(SourceMgr), Style(Style), UseCRLF(UseCRLF) {} 43 44 /// \brief Prepares the \c WhitespaceManager for another run. 45 void reset(); 46 47 /// \brief Replaces the whitespace in front of \p Tok. Only call once for 48 /// each \c AnnotatedToken. 49 void replaceWhitespace(FormatToken &Tok, unsigned Newlines, 50 unsigned IndentLevel, unsigned Spaces, 51 unsigned StartOfTokenColumn, 52 bool InPPDirective = false); 53 54 /// \brief Adds information about an unchangable token's whitespace. 55 /// 56 /// Needs to be called for every token for which \c replaceWhitespace 57 /// was not called. 58 void addUntouchableToken(const FormatToken &Tok, bool InPPDirective); 59 60 /// \brief Inserts or replaces whitespace in the middle of a token. 61 /// 62 /// Inserts \p PreviousPostfix, \p Newlines, \p Spaces and \p CurrentPrefix 63 /// (in this order) at \p Offset inside \p Tok, replacing \p ReplaceChars 64 /// characters. 65 /// 66 /// When \p InPPDirective is true, escaped newlines are inserted. \p Spaces is 67 /// used to align backslashes correctly. 68 void replaceWhitespaceInToken(const FormatToken &Tok, unsigned Offset, 69 unsigned ReplaceChars, 70 StringRef PreviousPostfix, 71 StringRef CurrentPrefix, bool InPPDirective, 72 unsigned Newlines, unsigned IndentLevel, 73 unsigned Spaces); 74 75 /// \brief Returns all the \c Replacements created during formatting. 76 const tooling::Replacements &generateReplacements(); 77 78private: 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; 87 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 IndentLevel, 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 nested blocks the token is in. This is used to add tabs 125 // only for the indentation, and not for alignment, when 126 // UseTab = US_ForIndentation. 127 unsigned IndentLevel; 128 129 // The number of spaces in front of the token or broken part of the token. 130 // This will be adapted when aligning tokens. 131 unsigned Spaces; 132 133 // \c IsTrailingComment, \c TokenLength, \c PreviousEndOfTokenColumn and 134 // \c EscapedNewlineColumn will be calculated in 135 // \c calculateLineBreakInformation. 136 bool IsTrailingComment; 137 unsigned TokenLength; 138 unsigned PreviousEndOfTokenColumn; 139 unsigned EscapedNewlineColumn; 140 }; 141 142 /// \brief Calculate \c IsTrailingComment, \c TokenLength for the last tokens 143 /// or token parts in a line and \c PreviousEndOfTokenColumn and 144 /// \c EscapedNewlineColumn for the first tokens or token parts in a line. 145 void calculateLineBreakInformation(); 146 147 /// \brief Align trailing comments over all \c Changes. 148 void alignTrailingComments(); 149 150 /// \brief Align trailing comments from change \p Start to change \p End at 151 /// the specified \p Column. 152 void alignTrailingComments(unsigned Start, unsigned End, unsigned Column); 153 154 /// \brief Align escaped newlines over all \c Changes. 155 void alignEscapedNewlines(); 156 157 /// \brief Align escaped newlines from change \p Start to change \p End at 158 /// the specified \p Column. 159 void alignEscapedNewlines(unsigned Start, unsigned End, unsigned Column); 160 161 /// \brief Fill \c Replaces with the replacements for all effective changes. 162 void generateChanges(); 163 164 /// \brief Stores \p Text as the replacement for the whitespace in \p Range. 165 void storeReplacement(const SourceRange &Range, StringRef Text); 166 void appendNewlineText(std::string &Text, unsigned Newlines); 167 void appendNewlineText(std::string &Text, unsigned Newlines, 168 unsigned PreviousEndOfTokenColumn, 169 unsigned EscapedNewlineColumn); 170 void appendIndentText(std::string &Text, unsigned IndentLevel, 171 unsigned Spaces, unsigned WhitespaceStartColumn); 172 173 SmallVector<Change, 16> Changes; 174 SourceManager &SourceMgr; 175 tooling::Replacements Replaces; 176 const FormatStyle &Style; 177 bool UseCRLF; 178}; 179 180} // namespace format 181} // namespace clang 182 183#endif // LLVM_CLANG_FORMAT_WHITESPACEMANAGER_H 184