clang API Documentation
00001 //===--- TextDiagnostic.cpp - Text Diagnostic Pretty-Printing -------------===// 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 #include "clang/Frontend/TextDiagnostic.h" 00011 #include "clang/Basic/CharInfo.h" 00012 #include "clang/Basic/DiagnosticOptions.h" 00013 #include "clang/Basic/FileManager.h" 00014 #include "clang/Basic/SourceManager.h" 00015 #include "clang/Lex/Lexer.h" 00016 #include "llvm/ADT/SmallString.h" 00017 #include "llvm/ADT/StringExtras.h" 00018 #include "llvm/Support/ConvertUTF.h" 00019 #include "llvm/Support/ErrorHandling.h" 00020 #include "llvm/Support/Locale.h" 00021 #include "llvm/Support/MemoryBuffer.h" 00022 #include "llvm/Support/raw_ostream.h" 00023 #include <algorithm> 00024 00025 using namespace clang; 00026 00027 static const enum raw_ostream::Colors noteColor = 00028 raw_ostream::BLACK; 00029 static const enum raw_ostream::Colors remarkColor = 00030 raw_ostream::BLUE; 00031 static const enum raw_ostream::Colors fixitColor = 00032 raw_ostream::GREEN; 00033 static const enum raw_ostream::Colors caretColor = 00034 raw_ostream::GREEN; 00035 static const enum raw_ostream::Colors warningColor = 00036 raw_ostream::MAGENTA; 00037 static const enum raw_ostream::Colors templateColor = 00038 raw_ostream::CYAN; 00039 static const enum raw_ostream::Colors errorColor = raw_ostream::RED; 00040 static const enum raw_ostream::Colors fatalColor = raw_ostream::RED; 00041 // Used for changing only the bold attribute. 00042 static const enum raw_ostream::Colors savedColor = 00043 raw_ostream::SAVEDCOLOR; 00044 00045 /// \brief Add highlights to differences in template strings. 00046 static void applyTemplateHighlighting(raw_ostream &OS, StringRef Str, 00047 bool &Normal, bool Bold) { 00048 while (1) { 00049 size_t Pos = Str.find(ToggleHighlight); 00050 OS << Str.slice(0, Pos); 00051 if (Pos == StringRef::npos) 00052 break; 00053 00054 Str = Str.substr(Pos + 1); 00055 if (Normal) 00056 OS.changeColor(templateColor, true); 00057 else { 00058 OS.resetColor(); 00059 if (Bold) 00060 OS.changeColor(savedColor, true); 00061 } 00062 Normal = !Normal; 00063 } 00064 } 00065 00066 /// \brief Number of spaces to indent when word-wrapping. 00067 const unsigned WordWrapIndentation = 6; 00068 00069 static int bytesSincePreviousTabOrLineBegin(StringRef SourceLine, size_t i) { 00070 int bytes = 0; 00071 while (0<i) { 00072 if (SourceLine[--i]=='\t') 00073 break; 00074 ++bytes; 00075 } 00076 return bytes; 00077 } 00078 00079 /// \brief returns a printable representation of first item from input range 00080 /// 00081 /// This function returns a printable representation of the next item in a line 00082 /// of source. If the next byte begins a valid and printable character, that 00083 /// character is returned along with 'true'. 00084 /// 00085 /// Otherwise, if the next byte begins a valid, but unprintable character, a 00086 /// printable, escaped representation of the character is returned, along with 00087 /// 'false'. Otherwise a printable, escaped representation of the next byte 00088 /// is returned along with 'false'. 00089 /// 00090 /// \note The index is updated to be used with a subsequent call to 00091 /// printableTextForNextCharacter. 00092 /// 00093 /// \param SourceLine The line of source 00094 /// \param i Pointer to byte index, 00095 /// \param TabStop used to expand tabs 00096 /// \return pair(printable text, 'true' iff original text was printable) 00097 /// 00098 static std::pair<SmallString<16>, bool> 00099 printableTextForNextCharacter(StringRef SourceLine, size_t *i, 00100 unsigned TabStop) { 00101 assert(i && "i must not be null"); 00102 assert(*i<SourceLine.size() && "must point to a valid index"); 00103 00104 if (SourceLine[*i]=='\t') { 00105 assert(0 < TabStop && TabStop <= DiagnosticOptions::MaxTabStop && 00106 "Invalid -ftabstop value"); 00107 unsigned col = bytesSincePreviousTabOrLineBegin(SourceLine, *i); 00108 unsigned NumSpaces = TabStop - col%TabStop; 00109 assert(0 < NumSpaces && NumSpaces <= TabStop 00110 && "Invalid computation of space amt"); 00111 ++(*i); 00112 00113 SmallString<16> expandedTab; 00114 expandedTab.assign(NumSpaces, ' '); 00115 return std::make_pair(expandedTab, true); 00116 } 00117 00118 unsigned char const *begin, *end; 00119 begin = reinterpret_cast<unsigned char const *>(&*(SourceLine.begin() + *i)); 00120 end = begin + (SourceLine.size() - *i); 00121 00122 if (isLegalUTF8Sequence(begin, end)) { 00123 UTF32 c; 00124 UTF32 *cptr = &c; 00125 unsigned char const *original_begin = begin; 00126 unsigned char const *cp_end = begin+getNumBytesForUTF8(SourceLine[*i]); 00127 00128 ConversionResult res = ConvertUTF8toUTF32(&begin, cp_end, &cptr, cptr+1, 00129 strictConversion); 00130 (void)res; 00131 assert(conversionOK==res); 00132 assert(0 < begin-original_begin 00133 && "we must be further along in the string now"); 00134 *i += begin-original_begin; 00135 00136 if (!llvm::sys::locale::isPrint(c)) { 00137 // If next character is valid UTF-8, but not printable 00138 SmallString<16> expandedCP("<U+>"); 00139 while (c) { 00140 expandedCP.insert(expandedCP.begin()+3, llvm::hexdigit(c%16)); 00141 c/=16; 00142 } 00143 while (expandedCP.size() < 8) 00144 expandedCP.insert(expandedCP.begin()+3, llvm::hexdigit(0)); 00145 return std::make_pair(expandedCP, false); 00146 } 00147 00148 // If next character is valid UTF-8, and printable 00149 return std::make_pair(SmallString<16>(original_begin, cp_end), true); 00150 00151 } 00152 00153 // If next byte is not valid UTF-8 (and therefore not printable) 00154 SmallString<16> expandedByte("<XX>"); 00155 unsigned char byte = SourceLine[*i]; 00156 expandedByte[1] = llvm::hexdigit(byte / 16); 00157 expandedByte[2] = llvm::hexdigit(byte % 16); 00158 ++(*i); 00159 return std::make_pair(expandedByte, false); 00160 } 00161 00162 static void expandTabs(std::string &SourceLine, unsigned TabStop) { 00163 size_t i = SourceLine.size(); 00164 while (i>0) { 00165 i--; 00166 if (SourceLine[i]!='\t') 00167 continue; 00168 size_t tmp_i = i; 00169 std::pair<SmallString<16>,bool> res 00170 = printableTextForNextCharacter(SourceLine, &tmp_i, TabStop); 00171 SourceLine.replace(i, 1, res.first.c_str()); 00172 } 00173 } 00174 00175 /// This function takes a raw source line and produces a mapping from the bytes 00176 /// of the printable representation of the line to the columns those printable 00177 /// characters will appear at (numbering the first column as 0). 00178 /// 00179 /// If a byte 'i' corresponds to muliple columns (e.g. the byte contains a tab 00180 /// character) then the array will map that byte to the first column the 00181 /// tab appears at and the next value in the map will have been incremented 00182 /// more than once. 00183 /// 00184 /// If a byte is the first in a sequence of bytes that together map to a single 00185 /// entity in the output, then the array will map that byte to the appropriate 00186 /// column while the subsequent bytes will be -1. 00187 /// 00188 /// The last element in the array does not correspond to any byte in the input 00189 /// and instead is the number of columns needed to display the source 00190 /// 00191 /// example: (given a tabstop of 8) 00192 /// 00193 /// "a \t \u3042" -> {0,1,2,8,9,-1,-1,11} 00194 /// 00195 /// (\\u3042 is represented in UTF-8 by three bytes and takes two columns to 00196 /// display) 00197 static void byteToColumn(StringRef SourceLine, unsigned TabStop, 00198 SmallVectorImpl<int> &out) { 00199 out.clear(); 00200 00201 if (SourceLine.empty()) { 00202 out.resize(1u,0); 00203 return; 00204 } 00205 00206 out.resize(SourceLine.size()+1, -1); 00207 00208 int columns = 0; 00209 size_t i = 0; 00210 while (i<SourceLine.size()) { 00211 out[i] = columns; 00212 std::pair<SmallString<16>,bool> res 00213 = printableTextForNextCharacter(SourceLine, &i, TabStop); 00214 columns += llvm::sys::locale::columnWidth(res.first); 00215 } 00216 out.back() = columns; 00217 } 00218 00219 /// This function takes a raw source line and produces a mapping from columns 00220 /// to the byte of the source line that produced the character displaying at 00221 /// that column. This is the inverse of the mapping produced by byteToColumn() 00222 /// 00223 /// The last element in the array is the number of bytes in the source string 00224 /// 00225 /// example: (given a tabstop of 8) 00226 /// 00227 /// "a \t \u3042" -> {0,1,2,-1,-1,-1,-1,-1,3,4,-1,7} 00228 /// 00229 /// (\\u3042 is represented in UTF-8 by three bytes and takes two columns to 00230 /// display) 00231 static void columnToByte(StringRef SourceLine, unsigned TabStop, 00232 SmallVectorImpl<int> &out) { 00233 out.clear(); 00234 00235 if (SourceLine.empty()) { 00236 out.resize(1u, 0); 00237 return; 00238 } 00239 00240 int columns = 0; 00241 size_t i = 0; 00242 while (i<SourceLine.size()) { 00243 out.resize(columns+1, -1); 00244 out.back() = i; 00245 std::pair<SmallString<16>,bool> res 00246 = printableTextForNextCharacter(SourceLine, &i, TabStop); 00247 columns += llvm::sys::locale::columnWidth(res.first); 00248 } 00249 out.resize(columns+1, -1); 00250 out.back() = i; 00251 } 00252 00253 namespace { 00254 struct SourceColumnMap { 00255 SourceColumnMap(StringRef SourceLine, unsigned TabStop) 00256 : m_SourceLine(SourceLine) { 00257 00258 ::byteToColumn(SourceLine, TabStop, m_byteToColumn); 00259 ::columnToByte(SourceLine, TabStop, m_columnToByte); 00260 00261 assert(m_byteToColumn.size()==SourceLine.size()+1); 00262 assert(0 < m_byteToColumn.size() && 0 < m_columnToByte.size()); 00263 assert(m_byteToColumn.size() 00264 == static_cast<unsigned>(m_columnToByte.back()+1)); 00265 assert(static_cast<unsigned>(m_byteToColumn.back()+1) 00266 == m_columnToByte.size()); 00267 } 00268 int columns() const { return m_byteToColumn.back(); } 00269 int bytes() const { return m_columnToByte.back(); } 00270 00271 /// \brief Map a byte to the column which it is at the start of, or return -1 00272 /// if it is not at the start of a column (for a UTF-8 trailing byte). 00273 int byteToColumn(int n) const { 00274 assert(0<=n && n<static_cast<int>(m_byteToColumn.size())); 00275 return m_byteToColumn[n]; 00276 } 00277 00278 /// \brief Map a byte to the first column which contains it. 00279 int byteToContainingColumn(int N) const { 00280 assert(0 <= N && N < static_cast<int>(m_byteToColumn.size())); 00281 while (m_byteToColumn[N] == -1) 00282 --N; 00283 return m_byteToColumn[N]; 00284 } 00285 00286 /// \brief Map a column to the byte which starts the column, or return -1 if 00287 /// the column the second or subsequent column of an expanded tab or similar 00288 /// multi-column entity. 00289 int columnToByte(int n) const { 00290 assert(0<=n && n<static_cast<int>(m_columnToByte.size())); 00291 return m_columnToByte[n]; 00292 } 00293 00294 /// \brief Map from a byte index to the next byte which starts a column. 00295 int startOfNextColumn(int N) const { 00296 assert(0 <= N && N < static_cast<int>(m_columnToByte.size() - 1)); 00297 while (byteToColumn(++N) == -1) {} 00298 return N; 00299 } 00300 00301 /// \brief Map from a byte index to the previous byte which starts a column. 00302 int startOfPreviousColumn(int N) const { 00303 assert(0 < N && N < static_cast<int>(m_columnToByte.size())); 00304 while (byteToColumn(--N) == -1) {} 00305 return N; 00306 } 00307 00308 StringRef getSourceLine() const { 00309 return m_SourceLine; 00310 } 00311 00312 private: 00313 const std::string m_SourceLine; 00314 SmallVector<int,200> m_byteToColumn; 00315 SmallVector<int,200> m_columnToByte; 00316 }; 00317 } // end anonymous namespace 00318 00319 /// \brief When the source code line we want to print is too long for 00320 /// the terminal, select the "interesting" region. 00321 static void selectInterestingSourceRegion(std::string &SourceLine, 00322 std::string &CaretLine, 00323 std::string &FixItInsertionLine, 00324 unsigned Columns, 00325 const SourceColumnMap &map) { 00326 unsigned MaxColumns = std::max<unsigned>(map.columns(), 00327 std::max(CaretLine.size(), 00328 FixItInsertionLine.size())); 00329 // if the number of columns is less than the desired number we're done 00330 if (MaxColumns <= Columns) 00331 return; 00332 00333 // No special characters are allowed in CaretLine. 00334 assert(CaretLine.end() == 00335 std::find_if(CaretLine.begin(), CaretLine.end(), 00336 [](char c) { return c < ' ' || '~' < c; })); 00337 00338 // Find the slice that we need to display the full caret line 00339 // correctly. 00340 unsigned CaretStart = 0, CaretEnd = CaretLine.size(); 00341 for (; CaretStart != CaretEnd; ++CaretStart) 00342 if (!isWhitespace(CaretLine[CaretStart])) 00343 break; 00344 00345 for (; CaretEnd != CaretStart; --CaretEnd) 00346 if (!isWhitespace(CaretLine[CaretEnd - 1])) 00347 break; 00348 00349 // caret has already been inserted into CaretLine so the above whitespace 00350 // check is guaranteed to include the caret 00351 00352 // If we have a fix-it line, make sure the slice includes all of the 00353 // fix-it information. 00354 if (!FixItInsertionLine.empty()) { 00355 unsigned FixItStart = 0, FixItEnd = FixItInsertionLine.size(); 00356 for (; FixItStart != FixItEnd; ++FixItStart) 00357 if (!isWhitespace(FixItInsertionLine[FixItStart])) 00358 break; 00359 00360 for (; FixItEnd != FixItStart; --FixItEnd) 00361 if (!isWhitespace(FixItInsertionLine[FixItEnd - 1])) 00362 break; 00363 00364 // We can safely use the byte offset FixItStart as the column offset 00365 // because the characters up until FixItStart are all ASCII whitespace 00366 // characters. 00367 unsigned FixItStartCol = FixItStart; 00368 unsigned FixItEndCol 00369 = llvm::sys::locale::columnWidth(FixItInsertionLine.substr(0, FixItEnd)); 00370 00371 CaretStart = std::min(FixItStartCol, CaretStart); 00372 CaretEnd = std::max(FixItEndCol, CaretEnd); 00373 } 00374 00375 // CaretEnd may have been set at the middle of a character 00376 // If it's not at a character's first column then advance it past the current 00377 // character. 00378 while (static_cast<int>(CaretEnd) < map.columns() && 00379 -1 == map.columnToByte(CaretEnd)) 00380 ++CaretEnd; 00381 00382 assert((static_cast<int>(CaretStart) > map.columns() || 00383 -1!=map.columnToByte(CaretStart)) && 00384 "CaretStart must not point to a column in the middle of a source" 00385 " line character"); 00386 assert((static_cast<int>(CaretEnd) > map.columns() || 00387 -1!=map.columnToByte(CaretEnd)) && 00388 "CaretEnd must not point to a column in the middle of a source line" 00389 " character"); 00390 00391 // CaretLine[CaretStart, CaretEnd) contains all of the interesting 00392 // parts of the caret line. While this slice is smaller than the 00393 // number of columns we have, try to grow the slice to encompass 00394 // more context. 00395 00396 unsigned SourceStart = map.columnToByte(std::min<unsigned>(CaretStart, 00397 map.columns())); 00398 unsigned SourceEnd = map.columnToByte(std::min<unsigned>(CaretEnd, 00399 map.columns())); 00400 00401 unsigned CaretColumnsOutsideSource = CaretEnd-CaretStart 00402 - (map.byteToColumn(SourceEnd)-map.byteToColumn(SourceStart)); 00403 00404 char const *front_ellipse = " ..."; 00405 char const *front_space = " "; 00406 char const *back_ellipse = "..."; 00407 unsigned ellipses_space = strlen(front_ellipse) + strlen(back_ellipse); 00408 00409 unsigned TargetColumns = Columns; 00410 // Give us extra room for the ellipses 00411 // and any of the caret line that extends past the source 00412 if (TargetColumns > ellipses_space+CaretColumnsOutsideSource) 00413 TargetColumns -= ellipses_space+CaretColumnsOutsideSource; 00414 00415 while (SourceStart>0 || SourceEnd<SourceLine.size()) { 00416 bool ExpandedRegion = false; 00417 00418 if (SourceStart>0) { 00419 unsigned NewStart = map.startOfPreviousColumn(SourceStart); 00420 00421 // Skip over any whitespace we see here; we're looking for 00422 // another bit of interesting text. 00423 // FIXME: Detect non-ASCII whitespace characters too. 00424 while (NewStart && isWhitespace(SourceLine[NewStart])) 00425 NewStart = map.startOfPreviousColumn(NewStart); 00426 00427 // Skip over this bit of "interesting" text. 00428 while (NewStart) { 00429 unsigned Prev = map.startOfPreviousColumn(NewStart); 00430 if (isWhitespace(SourceLine[Prev])) 00431 break; 00432 NewStart = Prev; 00433 } 00434 00435 assert(map.byteToColumn(NewStart) != -1); 00436 unsigned NewColumns = map.byteToColumn(SourceEnd) - 00437 map.byteToColumn(NewStart); 00438 if (NewColumns <= TargetColumns) { 00439 SourceStart = NewStart; 00440 ExpandedRegion = true; 00441 } 00442 } 00443 00444 if (SourceEnd<SourceLine.size()) { 00445 unsigned NewEnd = map.startOfNextColumn(SourceEnd); 00446 00447 // Skip over any whitespace we see here; we're looking for 00448 // another bit of interesting text. 00449 // FIXME: Detect non-ASCII whitespace characters too. 00450 while (NewEnd < SourceLine.size() && isWhitespace(SourceLine[NewEnd])) 00451 NewEnd = map.startOfNextColumn(NewEnd); 00452 00453 // Skip over this bit of "interesting" text. 00454 while (NewEnd < SourceLine.size() && isWhitespace(SourceLine[NewEnd])) 00455 NewEnd = map.startOfNextColumn(NewEnd); 00456 00457 assert(map.byteToColumn(NewEnd) != -1); 00458 unsigned NewColumns = map.byteToColumn(NewEnd) - 00459 map.byteToColumn(SourceStart); 00460 if (NewColumns <= TargetColumns) { 00461 SourceEnd = NewEnd; 00462 ExpandedRegion = true; 00463 } 00464 } 00465 00466 if (!ExpandedRegion) 00467 break; 00468 } 00469 00470 CaretStart = map.byteToColumn(SourceStart); 00471 CaretEnd = map.byteToColumn(SourceEnd) + CaretColumnsOutsideSource; 00472 00473 // [CaretStart, CaretEnd) is the slice we want. Update the various 00474 // output lines to show only this slice, with two-space padding 00475 // before the lines so that it looks nicer. 00476 00477 assert(CaretStart!=(unsigned)-1 && CaretEnd!=(unsigned)-1 && 00478 SourceStart!=(unsigned)-1 && SourceEnd!=(unsigned)-1); 00479 assert(SourceStart <= SourceEnd); 00480 assert(CaretStart <= CaretEnd); 00481 00482 unsigned BackColumnsRemoved 00483 = map.byteToColumn(SourceLine.size())-map.byteToColumn(SourceEnd); 00484 unsigned FrontColumnsRemoved = CaretStart; 00485 unsigned ColumnsKept = CaretEnd-CaretStart; 00486 00487 // We checked up front that the line needed truncation 00488 assert(FrontColumnsRemoved+ColumnsKept+BackColumnsRemoved > Columns); 00489 00490 // The line needs some trunctiona, and we'd prefer to keep the front 00491 // if possible, so remove the back 00492 if (BackColumnsRemoved > strlen(back_ellipse)) 00493 SourceLine.replace(SourceEnd, std::string::npos, back_ellipse); 00494 00495 // If that's enough then we're done 00496 if (FrontColumnsRemoved+ColumnsKept <= Columns) 00497 return; 00498 00499 // Otherwise remove the front as well 00500 if (FrontColumnsRemoved > strlen(front_ellipse)) { 00501 SourceLine.replace(0, SourceStart, front_ellipse); 00502 CaretLine.replace(0, CaretStart, front_space); 00503 if (!FixItInsertionLine.empty()) 00504 FixItInsertionLine.replace(0, CaretStart, front_space); 00505 } 00506 } 00507 00508 /// \brief Skip over whitespace in the string, starting at the given 00509 /// index. 00510 /// 00511 /// \returns The index of the first non-whitespace character that is 00512 /// greater than or equal to Idx or, if no such character exists, 00513 /// returns the end of the string. 00514 static unsigned skipWhitespace(unsigned Idx, StringRef Str, unsigned Length) { 00515 while (Idx < Length && isWhitespace(Str[Idx])) 00516 ++Idx; 00517 return Idx; 00518 } 00519 00520 /// \brief If the given character is the start of some kind of 00521 /// balanced punctuation (e.g., quotes or parentheses), return the 00522 /// character that will terminate the punctuation. 00523 /// 00524 /// \returns The ending punctuation character, if any, or the NULL 00525 /// character if the input character does not start any punctuation. 00526 static inline char findMatchingPunctuation(char c) { 00527 switch (c) { 00528 case '\'': return '\''; 00529 case '`': return '\''; 00530 case '"': return '"'; 00531 case '(': return ')'; 00532 case '[': return ']'; 00533 case '{': return '}'; 00534 default: break; 00535 } 00536 00537 return 0; 00538 } 00539 00540 /// \brief Find the end of the word starting at the given offset 00541 /// within a string. 00542 /// 00543 /// \returns the index pointing one character past the end of the 00544 /// word. 00545 static unsigned findEndOfWord(unsigned Start, StringRef Str, 00546 unsigned Length, unsigned Column, 00547 unsigned Columns) { 00548 assert(Start < Str.size() && "Invalid start position!"); 00549 unsigned End = Start + 1; 00550 00551 // If we are already at the end of the string, take that as the word. 00552 if (End == Str.size()) 00553 return End; 00554 00555 // Determine if the start of the string is actually opening 00556 // punctuation, e.g., a quote or parentheses. 00557 char EndPunct = findMatchingPunctuation(Str[Start]); 00558 if (!EndPunct) { 00559 // This is a normal word. Just find the first space character. 00560 while (End < Length && !isWhitespace(Str[End])) 00561 ++End; 00562 return End; 00563 } 00564 00565 // We have the start of a balanced punctuation sequence (quotes, 00566 // parentheses, etc.). Determine the full sequence is. 00567 SmallString<16> PunctuationEndStack; 00568 PunctuationEndStack.push_back(EndPunct); 00569 while (End < Length && !PunctuationEndStack.empty()) { 00570 if (Str[End] == PunctuationEndStack.back()) 00571 PunctuationEndStack.pop_back(); 00572 else if (char SubEndPunct = findMatchingPunctuation(Str[End])) 00573 PunctuationEndStack.push_back(SubEndPunct); 00574 00575 ++End; 00576 } 00577 00578 // Find the first space character after the punctuation ended. 00579 while (End < Length && !isWhitespace(Str[End])) 00580 ++End; 00581 00582 unsigned PunctWordLength = End - Start; 00583 if (// If the word fits on this line 00584 Column + PunctWordLength <= Columns || 00585 // ... or the word is "short enough" to take up the next line 00586 // without too much ugly white space 00587 PunctWordLength < Columns/3) 00588 return End; // Take the whole thing as a single "word". 00589 00590 // The whole quoted/parenthesized string is too long to print as a 00591 // single "word". Instead, find the "word" that starts just after 00592 // the punctuation and use that end-point instead. This will recurse 00593 // until it finds something small enough to consider a word. 00594 return findEndOfWord(Start + 1, Str, Length, Column + 1, Columns); 00595 } 00596 00597 /// \brief Print the given string to a stream, word-wrapping it to 00598 /// some number of columns in the process. 00599 /// 00600 /// \param OS the stream to which the word-wrapping string will be 00601 /// emitted. 00602 /// \param Str the string to word-wrap and output. 00603 /// \param Columns the number of columns to word-wrap to. 00604 /// \param Column the column number at which the first character of \p 00605 /// Str will be printed. This will be non-zero when part of the first 00606 /// line has already been printed. 00607 /// \param Bold if the current text should be bold 00608 /// \param Indentation the number of spaces to indent any lines beyond 00609 /// the first line. 00610 /// \returns true if word-wrapping was required, or false if the 00611 /// string fit on the first line. 00612 static bool printWordWrapped(raw_ostream &OS, StringRef Str, 00613 unsigned Columns, 00614 unsigned Column = 0, 00615 bool Bold = false, 00616 unsigned Indentation = WordWrapIndentation) { 00617 const unsigned Length = std::min(Str.find('\n'), Str.size()); 00618 bool TextNormal = true; 00619 00620 // The string used to indent each line. 00621 SmallString<16> IndentStr; 00622 IndentStr.assign(Indentation, ' '); 00623 bool Wrapped = false; 00624 for (unsigned WordStart = 0, WordEnd; WordStart < Length; 00625 WordStart = WordEnd) { 00626 // Find the beginning of the next word. 00627 WordStart = skipWhitespace(WordStart, Str, Length); 00628 if (WordStart == Length) 00629 break; 00630 00631 // Find the end of this word. 00632 WordEnd = findEndOfWord(WordStart, Str, Length, Column, Columns); 00633 00634 // Does this word fit on the current line? 00635 unsigned WordLength = WordEnd - WordStart; 00636 if (Column + WordLength < Columns) { 00637 // This word fits on the current line; print it there. 00638 if (WordStart) { 00639 OS << ' '; 00640 Column += 1; 00641 } 00642 applyTemplateHighlighting(OS, Str.substr(WordStart, WordLength), 00643 TextNormal, Bold); 00644 Column += WordLength; 00645 continue; 00646 } 00647 00648 // This word does not fit on the current line, so wrap to the next 00649 // line. 00650 OS << '\n'; 00651 OS.write(&IndentStr[0], Indentation); 00652 applyTemplateHighlighting(OS, Str.substr(WordStart, WordLength), 00653 TextNormal, Bold); 00654 Column = Indentation + WordLength; 00655 Wrapped = true; 00656 } 00657 00658 // Append any remaning text from the message with its existing formatting. 00659 applyTemplateHighlighting(OS, Str.substr(Length), TextNormal, Bold); 00660 00661 assert(TextNormal && "Text highlighted at end of diagnostic message."); 00662 00663 return Wrapped; 00664 } 00665 00666 TextDiagnostic::TextDiagnostic(raw_ostream &OS, 00667 const LangOptions &LangOpts, 00668 DiagnosticOptions *DiagOpts) 00669 : DiagnosticRenderer(LangOpts, DiagOpts), OS(OS) {} 00670 00671 TextDiagnostic::~TextDiagnostic() {} 00672 00673 void 00674 TextDiagnostic::emitDiagnosticMessage(SourceLocation Loc, 00675 PresumedLoc PLoc, 00676 DiagnosticsEngine::Level Level, 00677 StringRef Message, 00678 ArrayRef<clang::CharSourceRange> Ranges, 00679 const SourceManager *SM, 00680 DiagOrStoredDiag D) { 00681 uint64_t StartOfLocationInfo = OS.tell(); 00682 00683 // Emit the location of this particular diagnostic. 00684 if (Loc.isValid()) 00685 emitDiagnosticLoc(Loc, PLoc, Level, Ranges, *SM); 00686 00687 if (DiagOpts->ShowColors) 00688 OS.resetColor(); 00689 00690 printDiagnosticLevel(OS, Level, DiagOpts->ShowColors, 00691 DiagOpts->CLFallbackMode); 00692 printDiagnosticMessage(OS, 00693 /*IsSupplemental*/ Level == DiagnosticsEngine::Note, 00694 Message, OS.tell() - StartOfLocationInfo, 00695 DiagOpts->MessageLength, DiagOpts->ShowColors); 00696 } 00697 00698 /*static*/ void 00699 TextDiagnostic::printDiagnosticLevel(raw_ostream &OS, 00700 DiagnosticsEngine::Level Level, 00701 bool ShowColors, 00702 bool CLFallbackMode) { 00703 if (ShowColors) { 00704 // Print diagnostic category in bold and color 00705 switch (Level) { 00706 case DiagnosticsEngine::Ignored: 00707 llvm_unreachable("Invalid diagnostic type"); 00708 case DiagnosticsEngine::Note: OS.changeColor(noteColor, true); break; 00709 case DiagnosticsEngine::Remark: OS.changeColor(remarkColor, true); break; 00710 case DiagnosticsEngine::Warning: OS.changeColor(warningColor, true); break; 00711 case DiagnosticsEngine::Error: OS.changeColor(errorColor, true); break; 00712 case DiagnosticsEngine::Fatal: OS.changeColor(fatalColor, true); break; 00713 } 00714 } 00715 00716 switch (Level) { 00717 case DiagnosticsEngine::Ignored: 00718 llvm_unreachable("Invalid diagnostic type"); 00719 case DiagnosticsEngine::Note: OS << "note"; break; 00720 case DiagnosticsEngine::Remark: OS << "remark"; break; 00721 case DiagnosticsEngine::Warning: OS << "warning"; break; 00722 case DiagnosticsEngine::Error: OS << "error"; break; 00723 case DiagnosticsEngine::Fatal: OS << "fatal error"; break; 00724 } 00725 00726 // In clang-cl /fallback mode, print diagnostics as "error(clang):". This 00727 // makes it more clear whether a message is coming from clang or cl.exe, 00728 // and it prevents MSBuild from concluding that the build failed just because 00729 // there is an "error:" in the output. 00730 if (CLFallbackMode) 00731 OS << "(clang)"; 00732 00733 OS << ": "; 00734 00735 if (ShowColors) 00736 OS.resetColor(); 00737 } 00738 00739 /*static*/ 00740 void TextDiagnostic::printDiagnosticMessage(raw_ostream &OS, 00741 bool IsSupplemental, 00742 StringRef Message, 00743 unsigned CurrentColumn, 00744 unsigned Columns, bool ShowColors) { 00745 bool Bold = false; 00746 if (ShowColors && !IsSupplemental) { 00747 // Print primary diagnostic messages in bold and without color, to visually 00748 // indicate the transition from continuation notes and other output. 00749 OS.changeColor(savedColor, true); 00750 Bold = true; 00751 } 00752 00753 if (Columns) 00754 printWordWrapped(OS, Message, Columns, CurrentColumn, Bold); 00755 else { 00756 bool Normal = true; 00757 applyTemplateHighlighting(OS, Message, Normal, Bold); 00758 assert(Normal && "Formatting should have returned to normal"); 00759 } 00760 00761 if (ShowColors) 00762 OS.resetColor(); 00763 OS << '\n'; 00764 } 00765 00766 /// \brief Print out the file/line/column information and include trace. 00767 /// 00768 /// This method handlen the emission of the diagnostic location information. 00769 /// This includes extracting as much location information as is present for 00770 /// the diagnostic and printing it, as well as any include stack or source 00771 /// ranges necessary. 00772 void TextDiagnostic::emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc, 00773 DiagnosticsEngine::Level Level, 00774 ArrayRef<CharSourceRange> Ranges, 00775 const SourceManager &SM) { 00776 if (PLoc.isInvalid()) { 00777 // At least print the file name if available: 00778 FileID FID = SM.getFileID(Loc); 00779 if (!FID.isInvalid()) { 00780 const FileEntry* FE = SM.getFileEntryForID(FID); 00781 if (FE && FE->isValid()) { 00782 OS << FE->getName(); 00783 if (FE->isInPCH()) 00784 OS << " (in PCH)"; 00785 OS << ": "; 00786 } 00787 } 00788 return; 00789 } 00790 unsigned LineNo = PLoc.getLine(); 00791 00792 if (!DiagOpts->ShowLocation) 00793 return; 00794 00795 if (DiagOpts->ShowColors) 00796 OS.changeColor(savedColor, true); 00797 00798 OS << PLoc.getFilename(); 00799 switch (DiagOpts->getFormat()) { 00800 case DiagnosticOptions::Clang: OS << ':' << LineNo; break; 00801 case DiagnosticOptions::Msvc: OS << '(' << LineNo; break; 00802 case DiagnosticOptions::Vi: OS << " +" << LineNo; break; 00803 } 00804 00805 if (DiagOpts->ShowColumn) 00806 // Compute the column number. 00807 if (unsigned ColNo = PLoc.getColumn()) { 00808 if (DiagOpts->getFormat() == DiagnosticOptions::Msvc) { 00809 OS << ','; 00810 // Visual Studio 2010 or earlier expects column number to be off by one 00811 if (LangOpts.MSCompatibilityVersion && 00812 LangOpts.MSCompatibilityVersion < 170000000) 00813 ColNo--; 00814 } else 00815 OS << ':'; 00816 OS << ColNo; 00817 } 00818 switch (DiagOpts->getFormat()) { 00819 case DiagnosticOptions::Clang: 00820 case DiagnosticOptions::Vi: OS << ':'; break; 00821 case DiagnosticOptions::Msvc: OS << ") : "; break; 00822 } 00823 00824 if (DiagOpts->ShowSourceRanges && !Ranges.empty()) { 00825 FileID CaretFileID = 00826 SM.getFileID(SM.getExpansionLoc(Loc)); 00827 bool PrintedRange = false; 00828 00829 for (ArrayRef<CharSourceRange>::const_iterator RI = Ranges.begin(), 00830 RE = Ranges.end(); 00831 RI != RE; ++RI) { 00832 // Ignore invalid ranges. 00833 if (!RI->isValid()) continue; 00834 00835 SourceLocation B = SM.getExpansionLoc(RI->getBegin()); 00836 SourceLocation E = SM.getExpansionLoc(RI->getEnd()); 00837 00838 // If the End location and the start location are the same and are a 00839 // macro location, then the range was something that came from a 00840 // macro expansion or _Pragma. If this is an object-like macro, the 00841 // best we can do is to highlight the range. If this is a 00842 // function-like macro, we'd also like to highlight the arguments. 00843 if (B == E && RI->getEnd().isMacroID()) 00844 E = SM.getExpansionRange(RI->getEnd()).second; 00845 00846 std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(B); 00847 std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E); 00848 00849 // If the start or end of the range is in another file, just discard 00850 // it. 00851 if (BInfo.first != CaretFileID || EInfo.first != CaretFileID) 00852 continue; 00853 00854 // Add in the length of the token, so that we cover multi-char 00855 // tokens. 00856 unsigned TokSize = 0; 00857 if (RI->isTokenRange()) 00858 TokSize = Lexer::MeasureTokenLength(E, SM, LangOpts); 00859 00860 OS << '{' << SM.getLineNumber(BInfo.first, BInfo.second) << ':' 00861 << SM.getColumnNumber(BInfo.first, BInfo.second) << '-' 00862 << SM.getLineNumber(EInfo.first, EInfo.second) << ':' 00863 << (SM.getColumnNumber(EInfo.first, EInfo.second)+TokSize) 00864 << '}'; 00865 PrintedRange = true; 00866 } 00867 00868 if (PrintedRange) 00869 OS << ':'; 00870 } 00871 OS << ' '; 00872 } 00873 00874 void TextDiagnostic::emitIncludeLocation(SourceLocation Loc, 00875 PresumedLoc PLoc, 00876 const SourceManager &SM) { 00877 if (DiagOpts->ShowLocation) 00878 OS << "In file included from " << PLoc.getFilename() << ':' 00879 << PLoc.getLine() << ":\n"; 00880 else 00881 OS << "In included file:\n"; 00882 } 00883 00884 void TextDiagnostic::emitImportLocation(SourceLocation Loc, PresumedLoc PLoc, 00885 StringRef ModuleName, 00886 const SourceManager &SM) { 00887 if (DiagOpts->ShowLocation) 00888 OS << "In module '" << ModuleName << "' imported from " 00889 << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n"; 00890 else 00891 OS << "In module " << ModuleName << "':\n"; 00892 } 00893 00894 void TextDiagnostic::emitBuildingModuleLocation(SourceLocation Loc, 00895 PresumedLoc PLoc, 00896 StringRef ModuleName, 00897 const SourceManager &SM) { 00898 if (DiagOpts->ShowLocation && PLoc.getFilename()) 00899 OS << "While building module '" << ModuleName << "' imported from " 00900 << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n"; 00901 else 00902 OS << "While building module '" << ModuleName << "':\n"; 00903 } 00904 00905 /// \brief Highlight a SourceRange (with ~'s) for any characters on LineNo. 00906 static void highlightRange(const CharSourceRange &R, 00907 unsigned LineNo, FileID FID, 00908 const SourceColumnMap &map, 00909 std::string &CaretLine, 00910 const SourceManager &SM, 00911 const LangOptions &LangOpts) { 00912 if (!R.isValid()) return; 00913 00914 SourceLocation Begin = R.getBegin(); 00915 SourceLocation End = R.getEnd(); 00916 00917 unsigned StartLineNo = SM.getExpansionLineNumber(Begin); 00918 if (StartLineNo > LineNo || SM.getFileID(Begin) != FID) 00919 return; // No intersection. 00920 00921 unsigned EndLineNo = SM.getExpansionLineNumber(End); 00922 if (EndLineNo < LineNo || SM.getFileID(End) != FID) 00923 return; // No intersection. 00924 00925 // Compute the column number of the start. 00926 unsigned StartColNo = 0; 00927 if (StartLineNo == LineNo) { 00928 StartColNo = SM.getExpansionColumnNumber(Begin); 00929 if (StartColNo) --StartColNo; // Zero base the col #. 00930 } 00931 00932 // Compute the column number of the end. 00933 unsigned EndColNo = map.getSourceLine().size(); 00934 if (EndLineNo == LineNo) { 00935 EndColNo = SM.getExpansionColumnNumber(End); 00936 if (EndColNo) { 00937 --EndColNo; // Zero base the col #. 00938 00939 // Add in the length of the token, so that we cover multi-char tokens if 00940 // this is a token range. 00941 if (R.isTokenRange()) 00942 EndColNo += Lexer::MeasureTokenLength(End, SM, LangOpts); 00943 } else { 00944 EndColNo = CaretLine.size(); 00945 } 00946 } 00947 00948 assert(StartColNo <= EndColNo && "Invalid range!"); 00949 00950 // Check that a token range does not highlight only whitespace. 00951 if (R.isTokenRange()) { 00952 // Pick the first non-whitespace column. 00953 while (StartColNo < map.getSourceLine().size() && 00954 (map.getSourceLine()[StartColNo] == ' ' || 00955 map.getSourceLine()[StartColNo] == '\t')) 00956 StartColNo = map.startOfNextColumn(StartColNo); 00957 00958 // Pick the last non-whitespace column. 00959 if (EndColNo > map.getSourceLine().size()) 00960 EndColNo = map.getSourceLine().size(); 00961 while (EndColNo && 00962 (map.getSourceLine()[EndColNo-1] == ' ' || 00963 map.getSourceLine()[EndColNo-1] == '\t')) 00964 EndColNo = map.startOfPreviousColumn(EndColNo); 00965 00966 // If the start/end passed each other, then we are trying to highlight a 00967 // range that just exists in whitespace, which must be some sort of other 00968 // bug. 00969 assert(StartColNo <= EndColNo && "Trying to highlight whitespace??"); 00970 } 00971 00972 assert(StartColNo <= map.getSourceLine().size() && "Invalid range!"); 00973 assert(EndColNo <= map.getSourceLine().size() && "Invalid range!"); 00974 00975 // Fill the range with ~'s. 00976 StartColNo = map.byteToContainingColumn(StartColNo); 00977 EndColNo = map.byteToContainingColumn(EndColNo); 00978 00979 assert(StartColNo <= EndColNo && "Invalid range!"); 00980 if (CaretLine.size() < EndColNo) 00981 CaretLine.resize(EndColNo,' '); 00982 std::fill(CaretLine.begin()+StartColNo,CaretLine.begin()+EndColNo,'~'); 00983 } 00984 00985 static std::string buildFixItInsertionLine(unsigned LineNo, 00986 const SourceColumnMap &map, 00987 ArrayRef<FixItHint> Hints, 00988 const SourceManager &SM, 00989 const DiagnosticOptions *DiagOpts) { 00990 std::string FixItInsertionLine; 00991 if (Hints.empty() || !DiagOpts->ShowFixits) 00992 return FixItInsertionLine; 00993 unsigned PrevHintEndCol = 0; 00994 00995 for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end(); 00996 I != E; ++I) { 00997 if (!I->CodeToInsert.empty()) { 00998 // We have an insertion hint. Determine whether the inserted 00999 // code contains no newlines and is on the same line as the caret. 01000 std::pair<FileID, unsigned> HintLocInfo 01001 = SM.getDecomposedExpansionLoc(I->RemoveRange.getBegin()); 01002 if (LineNo == SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) && 01003 StringRef(I->CodeToInsert).find_first_of("\n\r") == StringRef::npos) { 01004 // Insert the new code into the line just below the code 01005 // that the user wrote. 01006 // Note: When modifying this function, be very careful about what is a 01007 // "column" (printed width, platform-dependent) and what is a 01008 // "byte offset" (SourceManager "column"). 01009 unsigned HintByteOffset 01010 = SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second) - 1; 01011 01012 // The hint must start inside the source or right at the end 01013 assert(HintByteOffset < static_cast<unsigned>(map.bytes())+1); 01014 unsigned HintCol = map.byteToContainingColumn(HintByteOffset); 01015 01016 // If we inserted a long previous hint, push this one forwards, and add 01017 // an extra space to show that this is not part of the previous 01018 // completion. This is sort of the best we can do when two hints appear 01019 // to overlap. 01020 // 01021 // Note that if this hint is located immediately after the previous 01022 // hint, no space will be added, since the location is more important. 01023 if (HintCol < PrevHintEndCol) 01024 HintCol = PrevHintEndCol + 1; 01025 01026 // This should NOT use HintByteOffset, because the source might have 01027 // Unicode characters in earlier columns. 01028 unsigned NewFixItLineSize = FixItInsertionLine.size() + 01029 (HintCol - PrevHintEndCol) + I->CodeToInsert.size(); 01030 if (NewFixItLineSize > FixItInsertionLine.size()) 01031 FixItInsertionLine.resize(NewFixItLineSize, ' '); 01032 01033 std::copy(I->CodeToInsert.begin(), I->CodeToInsert.end(), 01034 FixItInsertionLine.end() - I->CodeToInsert.size()); 01035 01036 PrevHintEndCol = 01037 HintCol + llvm::sys::locale::columnWidth(I->CodeToInsert); 01038 } else { 01039 FixItInsertionLine.clear(); 01040 break; 01041 } 01042 } 01043 } 01044 01045 expandTabs(FixItInsertionLine, DiagOpts->TabStop); 01046 01047 return FixItInsertionLine; 01048 } 01049 01050 /// \brief Emit a code snippet and caret line. 01051 /// 01052 /// This routine emits a single line's code snippet and caret line.. 01053 /// 01054 /// \param Loc The location for the caret. 01055 /// \param Ranges The underlined ranges for this code snippet. 01056 /// \param Hints The FixIt hints active for this diagnostic. 01057 void TextDiagnostic::emitSnippetAndCaret( 01058 SourceLocation Loc, DiagnosticsEngine::Level Level, 01059 SmallVectorImpl<CharSourceRange>& Ranges, 01060 ArrayRef<FixItHint> Hints, 01061 const SourceManager &SM) { 01062 assert(!Loc.isInvalid() && "must have a valid source location here"); 01063 assert(Loc.isFileID() && "must have a file location here"); 01064 01065 // If caret diagnostics are enabled and we have location, we want to 01066 // emit the caret. However, we only do this if the location moved 01067 // from the last diagnostic, if the last diagnostic was a note that 01068 // was part of a different warning or error diagnostic, or if the 01069 // diagnostic has ranges. We don't want to emit the same caret 01070 // multiple times if one loc has multiple diagnostics. 01071 if (!DiagOpts->ShowCarets) 01072 return; 01073 if (Loc == LastLoc && Ranges.empty() && Hints.empty() && 01074 (LastLevel != DiagnosticsEngine::Note || Level == LastLevel)) 01075 return; 01076 01077 // Decompose the location into a FID/Offset pair. 01078 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 01079 FileID FID = LocInfo.first; 01080 unsigned FileOffset = LocInfo.second; 01081 01082 // Get information about the buffer it points into. 01083 bool Invalid = false; 01084 const char *BufStart = SM.getBufferData(FID, &Invalid).data(); 01085 if (Invalid) 01086 return; 01087 01088 unsigned LineNo = SM.getLineNumber(FID, FileOffset); 01089 unsigned ColNo = SM.getColumnNumber(FID, FileOffset); 01090 01091 // Arbitrarily stop showing snippets when the line is too long. 01092 static const size_t MaxLineLengthToPrint = 4096; 01093 if (ColNo > MaxLineLengthToPrint) 01094 return; 01095 01096 // Rewind from the current position to the start of the line. 01097 const char *TokPtr = BufStart+FileOffset; 01098 const char *LineStart = TokPtr-ColNo+1; // Column # is 1-based. 01099 01100 // Compute the line end. Scan forward from the error position to the end of 01101 // the line. 01102 const char *LineEnd = TokPtr; 01103 while (*LineEnd != '\n' && *LineEnd != '\r' && *LineEnd != '\0') 01104 ++LineEnd; 01105 01106 // Arbitrarily stop showing snippets when the line is too long. 01107 if (size_t(LineEnd - LineStart) > MaxLineLengthToPrint) 01108 return; 01109 01110 // Copy the line of code into an std::string for ease of manipulation. 01111 std::string SourceLine(LineStart, LineEnd); 01112 01113 // Create a line for the caret that is filled with spaces that is the same 01114 // length as the line of source code. 01115 std::string CaretLine(LineEnd-LineStart, ' '); 01116 01117 const SourceColumnMap sourceColMap(SourceLine, DiagOpts->TabStop); 01118 01119 // Highlight all of the characters covered by Ranges with ~ characters. 01120 for (SmallVectorImpl<CharSourceRange>::iterator I = Ranges.begin(), 01121 E = Ranges.end(); 01122 I != E; ++I) 01123 highlightRange(*I, LineNo, FID, sourceColMap, CaretLine, SM, LangOpts); 01124 01125 // Next, insert the caret itself. 01126 ColNo = sourceColMap.byteToContainingColumn(ColNo-1); 01127 if (CaretLine.size()<ColNo+1) 01128 CaretLine.resize(ColNo+1, ' '); 01129 CaretLine[ColNo] = '^'; 01130 01131 std::string FixItInsertionLine = buildFixItInsertionLine(LineNo, 01132 sourceColMap, 01133 Hints, SM, 01134 DiagOpts.get()); 01135 01136 // If the source line is too long for our terminal, select only the 01137 // "interesting" source region within that line. 01138 unsigned Columns = DiagOpts->MessageLength; 01139 if (Columns) 01140 selectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine, 01141 Columns, sourceColMap); 01142 01143 // If we are in -fdiagnostics-print-source-range-info mode, we are trying 01144 // to produce easily machine parsable output. Add a space before the 01145 // source line and the caret to make it trivial to tell the main diagnostic 01146 // line from what the user is intended to see. 01147 if (DiagOpts->ShowSourceRanges) { 01148 SourceLine = ' ' + SourceLine; 01149 CaretLine = ' ' + CaretLine; 01150 } 01151 01152 // Finally, remove any blank spaces from the end of CaretLine. 01153 while (CaretLine[CaretLine.size()-1] == ' ') 01154 CaretLine.erase(CaretLine.end()-1); 01155 01156 // Emit what we have computed. 01157 emitSnippet(SourceLine); 01158 01159 if (DiagOpts->ShowColors) 01160 OS.changeColor(caretColor, true); 01161 OS << CaretLine << '\n'; 01162 if (DiagOpts->ShowColors) 01163 OS.resetColor(); 01164 01165 if (!FixItInsertionLine.empty()) { 01166 if (DiagOpts->ShowColors) 01167 // Print fixit line in color 01168 OS.changeColor(fixitColor, false); 01169 if (DiagOpts->ShowSourceRanges) 01170 OS << ' '; 01171 OS << FixItInsertionLine << '\n'; 01172 if (DiagOpts->ShowColors) 01173 OS.resetColor(); 01174 } 01175 01176 // Print out any parseable fixit information requested by the options. 01177 emitParseableFixits(Hints, SM); 01178 } 01179 01180 void TextDiagnostic::emitSnippet(StringRef line) { 01181 if (line.empty()) 01182 return; 01183 01184 size_t i = 0; 01185 01186 std::string to_print; 01187 bool print_reversed = false; 01188 01189 while (i<line.size()) { 01190 std::pair<SmallString<16>,bool> res 01191 = printableTextForNextCharacter(line, &i, DiagOpts->TabStop); 01192 bool was_printable = res.second; 01193 01194 if (DiagOpts->ShowColors && was_printable == print_reversed) { 01195 if (print_reversed) 01196 OS.reverseColor(); 01197 OS << to_print; 01198 to_print.clear(); 01199 if (DiagOpts->ShowColors) 01200 OS.resetColor(); 01201 } 01202 01203 print_reversed = !was_printable; 01204 to_print += res.first.str(); 01205 } 01206 01207 if (print_reversed && DiagOpts->ShowColors) 01208 OS.reverseColor(); 01209 OS << to_print; 01210 if (print_reversed && DiagOpts->ShowColors) 01211 OS.resetColor(); 01212 01213 OS << '\n'; 01214 } 01215 01216 void TextDiagnostic::emitParseableFixits(ArrayRef<FixItHint> Hints, 01217 const SourceManager &SM) { 01218 if (!DiagOpts->ShowParseableFixits) 01219 return; 01220 01221 // We follow FixItRewriter's example in not (yet) handling 01222 // fix-its in macros. 01223 for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end(); 01224 I != E; ++I) { 01225 if (I->RemoveRange.isInvalid() || 01226 I->RemoveRange.getBegin().isMacroID() || 01227 I->RemoveRange.getEnd().isMacroID()) 01228 return; 01229 } 01230 01231 for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end(); 01232 I != E; ++I) { 01233 SourceLocation BLoc = I->RemoveRange.getBegin(); 01234 SourceLocation ELoc = I->RemoveRange.getEnd(); 01235 01236 std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(BLoc); 01237 std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(ELoc); 01238 01239 // Adjust for token ranges. 01240 if (I->RemoveRange.isTokenRange()) 01241 EInfo.second += Lexer::MeasureTokenLength(ELoc, SM, LangOpts); 01242 01243 // We specifically do not do word-wrapping or tab-expansion here, 01244 // because this is supposed to be easy to parse. 01245 PresumedLoc PLoc = SM.getPresumedLoc(BLoc); 01246 if (PLoc.isInvalid()) 01247 break; 01248 01249 OS << "fix-it:\""; 01250 OS.write_escaped(PLoc.getFilename()); 01251 OS << "\":{" << SM.getLineNumber(BInfo.first, BInfo.second) 01252 << ':' << SM.getColumnNumber(BInfo.first, BInfo.second) 01253 << '-' << SM.getLineNumber(EInfo.first, EInfo.second) 01254 << ':' << SM.getColumnNumber(EInfo.first, EInfo.second) 01255 << "}:\""; 01256 OS.write_escaped(I->CodeToInsert); 01257 OS << "\"\n"; 01258 } 01259 }