1 //===--- WhitespaceManager.cpp - Format C++ code --------------------------===// 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 This file implements WhitespaceManager class. 12 /// 13 //===----------------------------------------------------------------------===// 14 15 #include "WhitespaceManager.h" 16 #include "llvm/ADT/STLExtras.h" 17 18 namespace clang { 19 namespace format { 20 21 bool 22 WhitespaceManager::Change::IsBeforeInFile::operator()(const Change &C1, 23 const Change &C2) const { 24 return SourceMgr.isBeforeInTranslationUnit( 25 C1.OriginalWhitespaceRange.getBegin(), 26 C2.OriginalWhitespaceRange.getBegin()); 27 } 28 29 WhitespaceManager::Change::Change( 30 bool CreateReplacement, const SourceRange &OriginalWhitespaceRange, 31 unsigned IndentLevel, unsigned Spaces, unsigned StartOfTokenColumn, 32 unsigned NewlinesBefore, StringRef PreviousLinePostfix, 33 StringRef CurrentLinePrefix, tok::TokenKind Kind, bool ContinuesPPDirective) 34 : CreateReplacement(CreateReplacement), 35 OriginalWhitespaceRange(OriginalWhitespaceRange), 36 StartOfTokenColumn(StartOfTokenColumn), NewlinesBefore(NewlinesBefore), 37 PreviousLinePostfix(PreviousLinePostfix), 38 CurrentLinePrefix(CurrentLinePrefix), Kind(Kind), 39 ContinuesPPDirective(ContinuesPPDirective), IndentLevel(IndentLevel), 40 Spaces(Spaces) {} 41 42 void WhitespaceManager::replaceWhitespace(const FormatToken &Tok, 43 unsigned Newlines, 44 unsigned IndentLevel, unsigned Spaces, 45 unsigned StartOfTokenColumn, 46 bool InPPDirective) { 47 Changes.push_back(Change(true, Tok.WhitespaceRange, IndentLevel, Spaces, 48 StartOfTokenColumn, Newlines, "", "", 49 Tok.Tok.getKind(), InPPDirective && !Tok.IsFirst)); 50 } 51 52 void WhitespaceManager::addUntouchableToken(const FormatToken &Tok, 53 bool InPPDirective) { 54 Changes.push_back(Change(false, Tok.WhitespaceRange, /*IndentLevel=*/0, 55 /*Spaces=*/0, Tok.OriginalColumn, Tok.NewlinesBefore, 56 "", "", Tok.Tok.getKind(), 57 InPPDirective && !Tok.IsFirst)); 58 } 59 60 void WhitespaceManager::replaceWhitespaceInToken( 61 const FormatToken &Tok, unsigned Offset, unsigned ReplaceChars, 62 StringRef PreviousPostfix, StringRef CurrentPrefix, bool InPPDirective, 63 unsigned Newlines, unsigned IndentLevel, unsigned Spaces) { 64 Changes.push_back(Change( 65 true, SourceRange(Tok.getStartOfNonWhitespace().getLocWithOffset(Offset), 66 Tok.getStartOfNonWhitespace().getLocWithOffset( 67 Offset + ReplaceChars)), 68 IndentLevel, Spaces, Spaces, Newlines, PreviousPostfix, CurrentPrefix, 69 // If we don't add a newline this change doesn't start a comment. Thus, 70 // when we align line comments, we don't need to treat this change as one. 71 // FIXME: We still need to take this change in account to properly 72 // calculate the new length of the comment and to calculate the changes 73 // for which to do the alignment when aligning comments. 74 Tok.Type == TT_LineComment && Newlines > 0 ? tok::comment : tok::unknown, 75 InPPDirective && !Tok.IsFirst)); 76 } 77 78 const tooling::Replacements &WhitespaceManager::generateReplacements() { 79 if (Changes.empty()) 80 return Replaces; 81 82 std::sort(Changes.begin(), Changes.end(), Change::IsBeforeInFile(SourceMgr)); 83 calculateLineBreakInformation(); 84 alignTrailingComments(); 85 alignEscapedNewlines(); 86 generateChanges(); 87 88 return Replaces; 89 } 90 91 void WhitespaceManager::calculateLineBreakInformation() { 92 Changes[0].PreviousEndOfTokenColumn = 0; 93 for (unsigned i = 1, e = Changes.size(); i != e; ++i) { 94 unsigned OriginalWhitespaceStart = 95 SourceMgr.getFileOffset(Changes[i].OriginalWhitespaceRange.getBegin()); 96 unsigned PreviousOriginalWhitespaceEnd = SourceMgr.getFileOffset( 97 Changes[i - 1].OriginalWhitespaceRange.getEnd()); 98 Changes[i - 1].TokenLength = OriginalWhitespaceStart - 99 PreviousOriginalWhitespaceEnd + 100 Changes[i].PreviousLinePostfix.size() + 101 Changes[i - 1].CurrentLinePrefix.size(); 102 103 Changes[i].PreviousEndOfTokenColumn = 104 Changes[i - 1].StartOfTokenColumn + Changes[i - 1].TokenLength; 105 106 Changes[i - 1].IsTrailingComment = 107 (Changes[i].NewlinesBefore > 0 || Changes[i].Kind == tok::eof) && 108 Changes[i - 1].Kind == tok::comment; 109 } 110 // FIXME: The last token is currently not always an eof token; in those 111 // cases, setting TokenLength of the last token to 0 is wrong. 112 Changes.back().TokenLength = 0; 113 Changes.back().IsTrailingComment = Changes.back().Kind == tok::comment; 114 } 115 116 void WhitespaceManager::alignTrailingComments() { 117 unsigned MinColumn = 0; 118 unsigned MaxColumn = UINT_MAX; 119 unsigned StartOfSequence = 0; 120 bool BreakBeforeNext = false; 121 unsigned Newlines = 0; 122 for (unsigned i = 0, e = Changes.size(); i != e; ++i) { 123 unsigned ChangeMinColumn = Changes[i].StartOfTokenColumn; 124 // FIXME: Correctly handle ChangeMaxColumn in PP directives. 125 unsigned ChangeMaxColumn = Style.ColumnLimit - Changes[i].TokenLength; 126 Newlines += Changes[i].NewlinesBefore; 127 if (Changes[i].IsTrailingComment) { 128 // If this comment follows an } in column 0, it probably documents the 129 // closing of a namespace and we don't want to align it. 130 bool FollowsRBraceInColumn0 = i > 0 && Changes[i].NewlinesBefore == 0 && 131 Changes[i - 1].Kind == tok::r_brace && 132 Changes[i - 1].StartOfTokenColumn == 0; 133 bool WasAlignedWithStartOfNextLine = 134 // A comment on its own line. 135 Changes[i].NewlinesBefore == 1 && 136 // Not the last line. 137 i + 1 != e && 138 // The start of the next token was previously aligned with 139 // the start of this comment. 140 (SourceMgr.getSpellingColumnNumber( 141 Changes[i].OriginalWhitespaceRange.getEnd()) == 142 SourceMgr.getSpellingColumnNumber( 143 Changes[i + 1].OriginalWhitespaceRange.getEnd())) && 144 // Which is not a comment itself. 145 Changes[i + 1].Kind != tok::comment; 146 if (!Style.AlignTrailingComments || FollowsRBraceInColumn0) { 147 alignTrailingComments(StartOfSequence, i, MinColumn); 148 MinColumn = ChangeMinColumn; 149 MaxColumn = ChangeMinColumn; 150 StartOfSequence = i; 151 } else if (BreakBeforeNext || Newlines > 1 || 152 (ChangeMinColumn > MaxColumn || ChangeMaxColumn < MinColumn) || 153 // Break the comment sequence if the previous line did not end 154 // in a trailing comment. 155 (Changes[i].NewlinesBefore == 1 && i > 0 && 156 !Changes[i - 1].IsTrailingComment) || 157 WasAlignedWithStartOfNextLine) { 158 alignTrailingComments(StartOfSequence, i, MinColumn); 159 MinColumn = ChangeMinColumn; 160 MaxColumn = ChangeMaxColumn; 161 StartOfSequence = i; 162 } else { 163 MinColumn = std::max(MinColumn, ChangeMinColumn); 164 MaxColumn = std::min(MaxColumn, ChangeMaxColumn); 165 } 166 BreakBeforeNext = 167 (i == 0) || (Changes[i].NewlinesBefore > 1) || 168 // Never start a sequence with a comment at the beginning of 169 // the line. 170 (Changes[i].NewlinesBefore == 1 && StartOfSequence == i); 171 Newlines = 0; 172 } 173 } 174 alignTrailingComments(StartOfSequence, Changes.size(), MinColumn); 175 } 176 177 void WhitespaceManager::alignTrailingComments(unsigned Start, unsigned End, 178 unsigned Column) { 179 for (unsigned i = Start; i != End; ++i) { 180 if (Changes[i].IsTrailingComment) { 181 assert(Column >= Changes[i].StartOfTokenColumn); 182 Changes[i].Spaces += Column - Changes[i].StartOfTokenColumn; 183 Changes[i].StartOfTokenColumn = Column; 184 } 185 } 186 } 187 188 void WhitespaceManager::alignEscapedNewlines() { 189 unsigned MaxEndOfLine = 190 Style.AlignEscapedNewlinesLeft ? 0 : Style.ColumnLimit; 191 unsigned StartOfMacro = 0; 192 for (unsigned i = 1, e = Changes.size(); i < e; ++i) { 193 Change &C = Changes[i]; 194 if (C.NewlinesBefore > 0) { 195 if (C.ContinuesPPDirective) { 196 MaxEndOfLine = std::max(C.PreviousEndOfTokenColumn + 2, MaxEndOfLine); 197 } else { 198 alignEscapedNewlines(StartOfMacro + 1, i, MaxEndOfLine); 199 MaxEndOfLine = Style.AlignEscapedNewlinesLeft ? 0 : Style.ColumnLimit; 200 StartOfMacro = i; 201 } 202 } 203 } 204 alignEscapedNewlines(StartOfMacro + 1, Changes.size(), MaxEndOfLine); 205 } 206 207 void WhitespaceManager::alignEscapedNewlines(unsigned Start, unsigned End, 208 unsigned Column) { 209 for (unsigned i = Start; i < End; ++i) { 210 Change &C = Changes[i]; 211 if (C.NewlinesBefore > 0) { 212 assert(C.ContinuesPPDirective); 213 if (C.PreviousEndOfTokenColumn + 1 > Column) 214 C.EscapedNewlineColumn = 0; 215 else 216 C.EscapedNewlineColumn = Column; 217 } 218 } 219 } 220 221 void WhitespaceManager::generateChanges() { 222 for (unsigned i = 0, e = Changes.size(); i != e; ++i) { 223 const Change &C = Changes[i]; 224 if (C.CreateReplacement) { 225 std::string ReplacementText = C.PreviousLinePostfix; 226 if (C.ContinuesPPDirective) 227 appendNewlineText(ReplacementText, C.NewlinesBefore, 228 C.PreviousEndOfTokenColumn, C.EscapedNewlineColumn); 229 else 230 appendNewlineText(ReplacementText, C.NewlinesBefore); 231 appendIndentText(ReplacementText, C.IndentLevel, C.Spaces, 232 C.StartOfTokenColumn - C.Spaces); 233 ReplacementText.append(C.CurrentLinePrefix); 234 storeReplacement(C.OriginalWhitespaceRange, ReplacementText); 235 } 236 } 237 } 238 239 void WhitespaceManager::storeReplacement(const SourceRange &Range, 240 StringRef Text) { 241 unsigned WhitespaceLength = SourceMgr.getFileOffset(Range.getEnd()) - 242 SourceMgr.getFileOffset(Range.getBegin()); 243 // Don't create a replacement, if it does not change anything. 244 if (StringRef(SourceMgr.getCharacterData(Range.getBegin()), 245 WhitespaceLength) == Text) 246 return; 247 Replaces.insert(tooling::Replacement( 248 SourceMgr, CharSourceRange::getCharRange(Range), Text)); 249 } 250 251 void WhitespaceManager::appendNewlineText(std::string &Text, 252 unsigned Newlines) { 253 for (unsigned i = 0; i < Newlines; ++i) 254 Text.append(UseCRLF ? "\r\n" : "\n"); 255 } 256 257 void WhitespaceManager::appendNewlineText(std::string &Text, unsigned Newlines, 258 unsigned PreviousEndOfTokenColumn, 259 unsigned EscapedNewlineColumn) { 260 if (Newlines > 0) { 261 unsigned Offset = 262 std::min<int>(EscapedNewlineColumn - 1, PreviousEndOfTokenColumn); 263 for (unsigned i = 0; i < Newlines; ++i) { 264 Text.append(std::string(EscapedNewlineColumn - Offset - 1, ' ')); 265 Text.append(UseCRLF ? "\\\r\n" : "\\\n"); 266 Offset = 0; 267 } 268 } 269 } 270 271 void WhitespaceManager::appendIndentText(std::string &Text, 272 unsigned IndentLevel, unsigned Spaces, 273 unsigned WhitespaceStartColumn) { 274 switch (Style.UseTab) { 275 case FormatStyle::UT_Never: 276 Text.append(std::string(Spaces, ' ')); 277 break; 278 case FormatStyle::UT_Always: { 279 unsigned FirstTabWidth = 280 Style.TabWidth - WhitespaceStartColumn % Style.TabWidth; 281 // Indent with tabs only when there's at least one full tab. 282 if (FirstTabWidth + Style.TabWidth <= Spaces) { 283 Spaces -= FirstTabWidth; 284 Text.append("\t"); 285 } 286 Text.append(std::string(Spaces / Style.TabWidth, '\t')); 287 Text.append(std::string(Spaces % Style.TabWidth, ' ')); 288 break; 289 } 290 case FormatStyle::UT_ForIndentation: 291 if (WhitespaceStartColumn == 0) { 292 unsigned Indentation = IndentLevel * Style.IndentWidth; 293 // This happens, e.g. when a line in a block comment is indented less than 294 // the first one. 295 if (Indentation > Spaces) 296 Indentation = Spaces; 297 unsigned Tabs = Indentation / Style.TabWidth; 298 Text.append(std::string(Tabs, '\t')); 299 Spaces -= Tabs * Style.TabWidth; 300 } 301 Text.append(std::string(Spaces, ' ')); 302 break; 303 } 304 } 305 306 } // namespace format 307 } // namespace clang 308