clang API Documentation
00001 //===--- WhitespaceManager.h - Format C++ code ------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 /// 00010 /// \file 00011 /// \brief WhitespaceManager class manages whitespace around tokens and their 00012 /// replacements. 00013 /// 00014 //===----------------------------------------------------------------------===// 00015 00016 #ifndef LLVM_CLANG_LIB_FORMAT_WHITESPACEMANAGER_H 00017 #define LLVM_CLANG_LIB_FORMAT_WHITESPACEMANAGER_H 00018 00019 #include "TokenAnnotator.h" 00020 #include "clang/Basic/SourceManager.h" 00021 #include "clang/Format/Format.h" 00022 #include <string> 00023 00024 namespace clang { 00025 namespace format { 00026 00027 /// \brief Manages the whitespaces around tokens and their replacements. 00028 /// 00029 /// This includes special handling for certain constructs, e.g. the alignment of 00030 /// trailing line comments. 00031 /// 00032 /// To guarantee correctness of alignment operations, the \c WhitespaceManager 00033 /// must be informed about every token in the source file; for each token, there 00034 /// must be exactly one call to either \c replaceWhitespace or 00035 /// \c addUntouchableToken. 00036 /// 00037 /// There may be multiple calls to \c breakToken for a given token. 00038 class WhitespaceManager { 00039 public: 00040 WhitespaceManager(SourceManager &SourceMgr, const FormatStyle &Style, 00041 bool UseCRLF) 00042 : SourceMgr(SourceMgr), Style(Style), UseCRLF(UseCRLF) {} 00043 00044 /// \brief Prepares the \c WhitespaceManager for another run. 00045 void reset(); 00046 00047 /// \brief Replaces the whitespace in front of \p Tok. Only call once for 00048 /// each \c AnnotatedToken. 00049 void replaceWhitespace(FormatToken &Tok, unsigned Newlines, 00050 unsigned IndentLevel, unsigned Spaces, 00051 unsigned StartOfTokenColumn, 00052 bool InPPDirective = false); 00053 00054 /// \brief Adds information about an unchangeable token's whitespace. 00055 /// 00056 /// Needs to be called for every token for which \c replaceWhitespace 00057 /// was not called. 00058 void addUntouchableToken(const FormatToken &Tok, bool InPPDirective); 00059 00060 /// \brief Inserts or replaces whitespace in the middle of a token. 00061 /// 00062 /// Inserts \p PreviousPostfix, \p Newlines, \p Spaces and \p CurrentPrefix 00063 /// (in this order) at \p Offset inside \p Tok, replacing \p ReplaceChars 00064 /// characters. 00065 /// 00066 /// Note: \p Spaces can be negative to retain information about initial 00067 /// relative column offset between a line of a block comment and the start of 00068 /// the comment. This negative offset may be compensated by trailing comment 00069 /// alignment here. In all other cases negative \p Spaces will be truncated to 00070 /// 0. 00071 /// 00072 /// When \p InPPDirective is true, escaped newlines are inserted. \p Spaces is 00073 /// used to align backslashes correctly. 00074 void replaceWhitespaceInToken(const FormatToken &Tok, unsigned Offset, 00075 unsigned ReplaceChars, 00076 StringRef PreviousPostfix, 00077 StringRef CurrentPrefix, bool InPPDirective, 00078 unsigned Newlines, unsigned IndentLevel, 00079 int Spaces); 00080 00081 /// \brief Returns all the \c Replacements created during formatting. 00082 const tooling::Replacements &generateReplacements(); 00083 00084 private: 00085 /// \brief Represents a change before a token, a break inside a token, 00086 /// or the layout of an unchanged token (or whitespace within). 00087 struct Change { 00088 /// \brief Functor to sort changes in original source order. 00089 class IsBeforeInFile { 00090 public: 00091 IsBeforeInFile(const SourceManager &SourceMgr) : SourceMgr(SourceMgr) {} 00092 bool operator()(const Change &C1, const Change &C2) const; 00093 00094 private: 00095 const SourceManager &SourceMgr; 00096 }; 00097 00098 Change() {} 00099 00100 /// \brief Creates a \c Change. 00101 /// 00102 /// The generated \c Change will replace the characters at 00103 /// \p OriginalWhitespaceRange with a concatenation of 00104 /// \p PreviousLinePostfix, \p NewlinesBefore line breaks, \p Spaces spaces 00105 /// and \p CurrentLinePrefix. 00106 /// 00107 /// \p StartOfTokenColumn and \p InPPDirective will be used to lay out 00108 /// trailing comments and escaped newlines. 00109 Change(bool CreateReplacement, const SourceRange &OriginalWhitespaceRange, 00110 unsigned IndentLevel, int Spaces, unsigned StartOfTokenColumn, 00111 unsigned NewlinesBefore, StringRef PreviousLinePostfix, 00112 StringRef CurrentLinePrefix, tok::TokenKind Kind, 00113 bool ContinuesPPDirective); 00114 00115 bool CreateReplacement; 00116 // Changes might be in the middle of a token, so we cannot just keep the 00117 // FormatToken around to query its information. 00118 SourceRange OriginalWhitespaceRange; 00119 unsigned StartOfTokenColumn; 00120 unsigned NewlinesBefore; 00121 std::string PreviousLinePostfix; 00122 std::string CurrentLinePrefix; 00123 // The kind of the token whose whitespace this change replaces, or in which 00124 // this change inserts whitespace. 00125 // FIXME: Currently this is not set correctly for breaks inside comments, as 00126 // the \c BreakableToken is still doing its own alignment. 00127 tok::TokenKind Kind; 00128 bool ContinuesPPDirective; 00129 00130 // The number of nested blocks the token is in. This is used to add tabs 00131 // only for the indentation, and not for alignment, when 00132 // UseTab = US_ForIndentation. 00133 unsigned IndentLevel; 00134 00135 // The number of spaces in front of the token or broken part of the token. 00136 // This will be adapted when aligning tokens. 00137 // Can be negative to retain information about the initial relative offset 00138 // of the lines in a block comment. This is used when aligning trailing 00139 // comments. Uncompensated negative offset is truncated to 0. 00140 int Spaces; 00141 00142 // \c IsTrailingComment, \c TokenLength, \c PreviousEndOfTokenColumn and 00143 // \c EscapedNewlineColumn will be calculated in 00144 // \c calculateLineBreakInformation. 00145 bool IsTrailingComment; 00146 unsigned TokenLength; 00147 unsigned PreviousEndOfTokenColumn; 00148 unsigned EscapedNewlineColumn; 00149 00150 // These fields are used to retain correct relative line indentation in a 00151 // block comment when aligning trailing comments. 00152 // 00153 // If this Change represents a continuation of a block comment, 00154 // \c StartOfBlockComment is pointer to the first Change in the block 00155 // comment. \c IndentationOffset is a relative column offset to this 00156 // change, so that the correct column can be reconstructed at the end of 00157 // the alignment process. 00158 const Change *StartOfBlockComment; 00159 int IndentationOffset; 00160 }; 00161 00162 /// \brief Calculate \c IsTrailingComment, \c TokenLength for the last tokens 00163 /// or token parts in a line and \c PreviousEndOfTokenColumn and 00164 /// \c EscapedNewlineColumn for the first tokens or token parts in a line. 00165 void calculateLineBreakInformation(); 00166 00167 /// \brief Align trailing comments over all \c Changes. 00168 void alignTrailingComments(); 00169 00170 /// \brief Align trailing comments from change \p Start to change \p End at 00171 /// the specified \p Column. 00172 void alignTrailingComments(unsigned Start, unsigned End, unsigned Column); 00173 00174 /// \brief Align escaped newlines over all \c Changes. 00175 void alignEscapedNewlines(); 00176 00177 /// \brief Align escaped newlines from change \p Start to change \p End at 00178 /// the specified \p Column. 00179 void alignEscapedNewlines(unsigned Start, unsigned End, unsigned Column); 00180 00181 /// \brief Fill \c Replaces with the replacements for all effective changes. 00182 void generateChanges(); 00183 00184 /// \brief Stores \p Text as the replacement for the whitespace in \p Range. 00185 void storeReplacement(const SourceRange &Range, StringRef Text); 00186 void appendNewlineText(std::string &Text, unsigned Newlines); 00187 void appendNewlineText(std::string &Text, unsigned Newlines, 00188 unsigned PreviousEndOfTokenColumn, 00189 unsigned EscapedNewlineColumn); 00190 void appendIndentText(std::string &Text, unsigned IndentLevel, 00191 unsigned Spaces, unsigned WhitespaceStartColumn); 00192 00193 SmallVector<Change, 16> Changes; 00194 SourceManager &SourceMgr; 00195 tooling::Replacements Replaces; 00196 const FormatStyle &Style; 00197 bool UseCRLF; 00198 }; 00199 00200 } // namespace format 00201 } // namespace clang 00202 00203 #endif