TextDiagnosticPrinter.cpp revision 1f0eb56d7cd1b24c21ac24112dfb41d3b29fb21c
1//===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===// 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// This diagnostic client prints out their diagnostic messages. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/Frontend/TextDiagnosticPrinter.h" 15#include "clang/Basic/SourceManager.h" 16#include "clang/Lex/Lexer.h" 17#include "llvm/Support/MemoryBuffer.h" 18#include "llvm/Support/raw_ostream.h" 19#include "llvm/ADT/SmallString.h" 20#include <algorithm> 21using namespace clang; 22 23/// \brief Number of spaces to indent when word-wrapping. 24const unsigned WordWrapIndentation = 6; 25 26void TextDiagnosticPrinter:: 27PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) { 28 if (Loc.isInvalid()) return; 29 30 PresumedLoc PLoc = SM.getPresumedLoc(Loc); 31 32 // Print out the other include frames first. 33 PrintIncludeStack(PLoc.getIncludeLoc(), SM); 34 35 if (ShowLocation) 36 OS << "In file included from " << PLoc.getFilename() 37 << ':' << PLoc.getLine() << ":\n"; 38 else 39 OS << "In included file:\n"; 40} 41 42/// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s) 43/// any characters in LineNo that intersect the SourceRange. 44void TextDiagnosticPrinter::HighlightRange(const SourceRange &R, 45 const SourceManager &SM, 46 unsigned LineNo, FileID FID, 47 std::string &CaretLine, 48 const std::string &SourceLine) { 49 assert(CaretLine.size() == SourceLine.size() && 50 "Expect a correspondence between source and caret line!"); 51 if (!R.isValid()) return; 52 53 SourceLocation Begin = SM.getInstantiationLoc(R.getBegin()); 54 SourceLocation End = SM.getInstantiationLoc(R.getEnd()); 55 56 // If the End location and the start location are the same and are a macro 57 // location, then the range was something that came from a macro expansion 58 // or _Pragma. If this is an object-like macro, the best we can do is to 59 // highlight the range. If this is a function-like macro, we'd also like to 60 // highlight the arguments. 61 if (Begin == End && R.getEnd().isMacroID()) 62 End = SM.getInstantiationRange(R.getEnd()).second; 63 64 unsigned StartLineNo = SM.getInstantiationLineNumber(Begin); 65 if (StartLineNo > LineNo || SM.getFileID(Begin) != FID) 66 return; // No intersection. 67 68 unsigned EndLineNo = SM.getInstantiationLineNumber(End); 69 if (EndLineNo < LineNo || SM.getFileID(End) != FID) 70 return; // No intersection. 71 72 // Compute the column number of the start. 73 unsigned StartColNo = 0; 74 if (StartLineNo == LineNo) { 75 StartColNo = SM.getInstantiationColumnNumber(Begin); 76 if (StartColNo) --StartColNo; // Zero base the col #. 77 } 78 79 // Pick the first non-whitespace column. 80 while (StartColNo < SourceLine.size() && 81 (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t')) 82 ++StartColNo; 83 84 // Compute the column number of the end. 85 unsigned EndColNo = CaretLine.size(); 86 if (EndLineNo == LineNo) { 87 EndColNo = SM.getInstantiationColumnNumber(End); 88 if (EndColNo) { 89 --EndColNo; // Zero base the col #. 90 91 // Add in the length of the token, so that we cover multi-char tokens. 92 EndColNo += Lexer::MeasureTokenLength(End, SM, *LangOpts); 93 } else { 94 EndColNo = CaretLine.size(); 95 } 96 } 97 98 // Pick the last non-whitespace column. 99 if (EndColNo <= SourceLine.size()) 100 while (EndColNo-1 && 101 (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t')) 102 --EndColNo; 103 else 104 EndColNo = SourceLine.size(); 105 106 // Fill the range with ~'s. 107 assert(StartColNo <= EndColNo && "Invalid range!"); 108 for (unsigned i = StartColNo; i < EndColNo; ++i) 109 CaretLine[i] = '~'; 110} 111 112/// \brief When the source code line we want to print is too long for 113/// the terminal, select the "interesting" region. 114static void SelectInterestingSourceRegion(std::string &SourceLine, 115 std::string &CaretLine, 116 std::string &FixItInsertionLine, 117 unsigned EndOfCaretToken, 118 unsigned Columns) { 119 if (CaretLine.size() > SourceLine.size()) 120 SourceLine.resize(CaretLine.size(), ' '); 121 122 // Find the slice that we need to display the full caret line 123 // correctly. 124 unsigned CaretStart = 0, CaretEnd = CaretLine.size(); 125 for (; CaretStart != CaretEnd; ++CaretStart) 126 if (!isspace(CaretLine[CaretStart])) 127 break; 128 129 for (; CaretEnd != CaretStart; --CaretEnd) 130 if (!isspace(CaretLine[CaretEnd - 1])) 131 break; 132 133 // Make sure we don't chop the string shorter than the caret token 134 // itself. 135 if (CaretEnd < EndOfCaretToken) 136 CaretEnd = EndOfCaretToken; 137 138 // If we have a fix-it line, make sure the slice includes all of the 139 // fix-it information. 140 if (!FixItInsertionLine.empty()) { 141 unsigned FixItStart = 0, FixItEnd = FixItInsertionLine.size(); 142 for (; FixItStart != FixItEnd; ++FixItStart) 143 if (!isspace(FixItInsertionLine[FixItStart])) 144 break; 145 146 for (; FixItEnd != FixItStart; --FixItEnd) 147 if (!isspace(FixItInsertionLine[FixItEnd - 1])) 148 break; 149 150 if (FixItStart < CaretStart) 151 CaretStart = FixItStart; 152 if (FixItEnd > CaretEnd) 153 CaretEnd = FixItEnd; 154 } 155 156 // CaretLine[CaretStart, CaretEnd) contains all of the interesting 157 // parts of the caret line. While this slice is smaller than the 158 // number of columns we have, try to grow the slice to encompass 159 // more context. 160 161 // If the end of the interesting region comes before we run out of 162 // space in the terminal, start at the beginning of the line. 163 if (Columns > 3 && CaretEnd < Columns - 3) 164 CaretStart = 0; 165 166 unsigned TargetColumns = Columns; 167 if (TargetColumns > 8) 168 TargetColumns -= 8; // Give us extra room for the ellipses. 169 unsigned SourceLength = SourceLine.size(); 170 while ((CaretEnd - CaretStart) < TargetColumns) { 171 bool ExpandedRegion = false; 172 // Move the start of the interesting region left until we've 173 // pulled in something else interesting. 174 if (CaretStart == 1) 175 CaretStart = 0; 176 else if (CaretStart > 1) { 177 unsigned NewStart = CaretStart - 1; 178 179 // Skip over any whitespace we see here; we're looking for 180 // another bit of interesting text. 181 while (NewStart && isspace(SourceLine[NewStart])) 182 --NewStart; 183 184 // Skip over this bit of "interesting" text. 185 while (NewStart && !isspace(SourceLine[NewStart])) 186 --NewStart; 187 188 // Move up to the non-whitespace character we just saw. 189 if (NewStart) 190 ++NewStart; 191 192 // If we're still within our limit, update the starting 193 // position within the source/caret line. 194 if (CaretEnd - NewStart <= TargetColumns) { 195 CaretStart = NewStart; 196 ExpandedRegion = true; 197 } 198 } 199 200 // Move the end of the interesting region right until we've 201 // pulled in something else interesting. 202 if (CaretEnd != SourceLength) { 203 unsigned NewEnd = CaretEnd; 204 205 // Skip over any whitespace we see here; we're looking for 206 // another bit of interesting text. 207 while (NewEnd != SourceLength && isspace(SourceLine[NewEnd - 1])) 208 ++NewEnd; 209 210 // Skip over this bit of "interesting" text. 211 while (NewEnd != SourceLength && !isspace(SourceLine[NewEnd - 1])) 212 ++NewEnd; 213 214 if (NewEnd - CaretStart <= TargetColumns) { 215 CaretEnd = NewEnd; 216 ExpandedRegion = true; 217 } 218 } 219 220 if (!ExpandedRegion) 221 break; 222 } 223 224 // [CaretStart, CaretEnd) is the slice we want. Update the various 225 // output lines to show only this slice, with two-space padding 226 // before the lines so that it looks nicer. 227 if (CaretEnd < SourceLine.size()) 228 SourceLine.replace(CaretEnd, std::string::npos, "..."); 229 if (CaretEnd < CaretLine.size()) 230 CaretLine.erase(CaretEnd, std::string::npos); 231 if (FixItInsertionLine.size() > CaretEnd) 232 FixItInsertionLine.erase(CaretEnd, std::string::npos); 233 234 if (CaretStart > 2) { 235 SourceLine.replace(0, CaretStart, " ..."); 236 CaretLine.replace(0, CaretStart, " "); 237 if (FixItInsertionLine.size() >= CaretStart) 238 FixItInsertionLine.replace(0, CaretStart, " "); 239 } 240} 241 242void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc, 243 SourceRange *Ranges, 244 unsigned NumRanges, 245 SourceManager &SM, 246 const CodeModificationHint *Hints, 247 unsigned NumHints, 248 unsigned Columns) { 249 assert(!Loc.isInvalid() && "must have a valid source location here"); 250 251 // If this is a macro ID, first emit information about where this was 252 // instantiated (recursively) then emit information about where. the token was 253 // spelled from. 254 if (!Loc.isFileID()) { 255 SourceLocation OneLevelUp = SM.getImmediateInstantiationRange(Loc).first; 256 // FIXME: Map ranges? 257 EmitCaretDiagnostic(OneLevelUp, Ranges, NumRanges, SM, 0, 0, Columns); 258 259 Loc = SM.getImmediateSpellingLoc(Loc); 260 261 // Map the ranges. 262 for (unsigned i = 0; i != NumRanges; ++i) { 263 SourceLocation S = Ranges[i].getBegin(), E = Ranges[i].getEnd(); 264 if (S.isMacroID()) S = SM.getImmediateSpellingLoc(S); 265 if (E.isMacroID()) E = SM.getImmediateSpellingLoc(E); 266 Ranges[i] = SourceRange(S, E); 267 } 268 269 if (ShowLocation) { 270 std::pair<FileID, unsigned> IInfo = SM.getDecomposedInstantiationLoc(Loc); 271 272 // Emit the file/line/column that this expansion came from. 273 OS << SM.getBuffer(IInfo.first)->getBufferIdentifier() << ':' 274 << SM.getLineNumber(IInfo.first, IInfo.second) << ':'; 275 if (ShowColumn) 276 OS << SM.getColumnNumber(IInfo.first, IInfo.second) << ':'; 277 OS << ' '; 278 } 279 OS << "note: instantiated from:\n"; 280 281 EmitCaretDiagnostic(Loc, Ranges, NumRanges, SM, Hints, NumHints, Columns); 282 return; 283 } 284 285 // Decompose the location into a FID/Offset pair. 286 std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); 287 FileID FID = LocInfo.first; 288 unsigned FileOffset = LocInfo.second; 289 290 // Get information about the buffer it points into. 291 std::pair<const char*, const char*> BufferInfo = SM.getBufferData(FID); 292 const char *BufStart = BufferInfo.first; 293 294 unsigned ColNo = SM.getColumnNumber(FID, FileOffset); 295 unsigned CaretEndColNo 296 = ColNo + Lexer::MeasureTokenLength(Loc, SM, *LangOpts); 297 298 // Rewind from the current position to the start of the line. 299 const char *TokPtr = BufStart+FileOffset; 300 const char *LineStart = TokPtr-ColNo+1; // Column # is 1-based. 301 302 303 // Compute the line end. Scan forward from the error position to the end of 304 // the line. 305 const char *LineEnd = TokPtr; 306 while (*LineEnd != '\n' && *LineEnd != '\r' && *LineEnd != '\0') 307 ++LineEnd; 308 309 // Copy the line of code into an std::string for ease of manipulation. 310 std::string SourceLine(LineStart, LineEnd); 311 312 // Create a line for the caret that is filled with spaces that is the same 313 // length as the line of source code. 314 std::string CaretLine(LineEnd-LineStart, ' '); 315 316 // Highlight all of the characters covered by Ranges with ~ characters. 317 if (NumRanges) { 318 unsigned LineNo = SM.getLineNumber(FID, FileOffset); 319 320 for (unsigned i = 0, e = NumRanges; i != e; ++i) 321 HighlightRange(Ranges[i], SM, LineNo, FID, CaretLine, SourceLine); 322 } 323 324 // Next, insert the caret itself. 325 if (ColNo-1 < CaretLine.size()) 326 CaretLine[ColNo-1] = '^'; 327 else 328 CaretLine.push_back('^'); 329 330 // Scan the source line, looking for tabs. If we find any, manually expand 331 // them to 8 characters and update the CaretLine to match. 332 for (unsigned i = 0; i != SourceLine.size(); ++i) { 333 if (SourceLine[i] != '\t') continue; 334 335 // Replace this tab with at least one space. 336 SourceLine[i] = ' '; 337 338 // Compute the number of spaces we need to insert. 339 unsigned NumSpaces = ((i+8)&~7) - (i+1); 340 assert(NumSpaces < 8 && "Invalid computation of space amt"); 341 342 // Insert spaces into the SourceLine. 343 SourceLine.insert(i+1, NumSpaces, ' '); 344 345 // Insert spaces or ~'s into CaretLine. 346 CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' '); 347 } 348 349 // If we are in -fdiagnostics-print-source-range-info mode, we are trying to 350 // produce easily machine parsable output. Add a space before the source line 351 // and the caret to make it trivial to tell the main diagnostic line from what 352 // the user is intended to see. 353 if (PrintRangeInfo) { 354 SourceLine = ' ' + SourceLine; 355 CaretLine = ' ' + CaretLine; 356 } 357 358 std::string FixItInsertionLine; 359 if (NumHints && PrintFixItInfo) { 360 for (const CodeModificationHint *Hint = Hints, *LastHint = Hints + NumHints; 361 Hint != LastHint; ++Hint) { 362 if (Hint->InsertionLoc.isValid()) { 363 // We have an insertion hint. Determine whether the inserted 364 // code is on the same line as the caret. 365 std::pair<FileID, unsigned> HintLocInfo 366 = SM.getDecomposedInstantiationLoc(Hint->InsertionLoc); 367 if (SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) == 368 SM.getLineNumber(FID, FileOffset)) { 369 // Insert the new code into the line just below the code 370 // that the user wrote. 371 unsigned HintColNo 372 = SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second); 373 unsigned LastColumnModified 374 = HintColNo - 1 + Hint->CodeToInsert.size(); 375 if (LastColumnModified > FixItInsertionLine.size()) 376 FixItInsertionLine.resize(LastColumnModified, ' '); 377 std::copy(Hint->CodeToInsert.begin(), Hint->CodeToInsert.end(), 378 FixItInsertionLine.begin() + HintColNo - 1); 379 } else { 380 FixItInsertionLine.clear(); 381 break; 382 } 383 } 384 } 385 } 386 387 // If the source line is too long for our terminal, select only the 388 // "interesting" source region within that line. 389 if (Columns && SourceLine.size() > Columns) 390 SelectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine, 391 CaretEndColNo, Columns); 392 393 // Finally, remove any blank spaces from the end of CaretLine. 394 while (CaretLine[CaretLine.size()-1] == ' ') 395 CaretLine.erase(CaretLine.end()-1); 396 397 // Emit what we have computed. 398 OS << SourceLine << '\n'; 399 OS << CaretLine << '\n'; 400 401 if (!FixItInsertionLine.empty()) { 402 if (PrintRangeInfo) 403 OS << ' '; 404 OS << FixItInsertionLine << '\n'; 405 } 406} 407 408/// \brief Skip over whitespace in the string, starting at the given 409/// index. 410/// 411/// \returns The index of the first non-whitespace character that is 412/// greater than or equal to Idx or, if no such character exists, 413/// returns the end of the string. 414static unsigned skipWhitespace(unsigned Idx, 415 const llvm::SmallVectorImpl<char> &Str, 416 unsigned Length) { 417 while (Idx < Length && isspace(Str[Idx])) 418 ++Idx; 419 return Idx; 420} 421 422/// \brief If the given character is the start of some kind of 423/// balanced punctuation (e.g., quotes or parentheses), return the 424/// character that will terminate the punctuation. 425/// 426/// \returns The ending punctuation character, if any, or the NULL 427/// character if the input character does not start any punctuation. 428static inline char findMatchingPunctuation(char c) { 429 switch (c) { 430 case '\'': return '\''; 431 case '`': return '\''; 432 case '"': return '"'; 433 case '(': return ')'; 434 case '[': return ']'; 435 case '{': return '}'; 436 default: break; 437 } 438 439 return 0; 440} 441 442/// \brief Find the end of the word starting at the given offset 443/// within a string. 444/// 445/// \returns the index pointing one character past the end of the 446/// word. 447unsigned findEndOfWord(unsigned Start, 448 const llvm::SmallVectorImpl<char> &Str, 449 unsigned Length, unsigned Column, 450 unsigned Columns) { 451 unsigned End = Start + 1; 452 453 // Determine if the start of the string is actually opening 454 // punctuation, e.g., a quote or parentheses. 455 char EndPunct = findMatchingPunctuation(Str[Start]); 456 if (!EndPunct) { 457 // This is a normal word. Just find the first space character. 458 while (End < Length && !isspace(Str[End])) 459 ++End; 460 return End; 461 } 462 463 // We have the start of a balanced punctuation sequence (quotes, 464 // parentheses, etc.). Determine the full sequence is. 465 llvm::SmallVector<char, 16> PunctuationEndStack; 466 PunctuationEndStack.push_back(EndPunct); 467 while (End < Length && !PunctuationEndStack.empty()) { 468 if (Str[End] == PunctuationEndStack.back()) 469 PunctuationEndStack.pop_back(); 470 else if (char SubEndPunct = findMatchingPunctuation(Str[End])) 471 PunctuationEndStack.push_back(SubEndPunct); 472 473 ++End; 474 } 475 476 // Find the first space character after the punctuation ended. 477 while (End < Length && !isspace(Str[End])) 478 ++End; 479 480 unsigned PunctWordLength = End - Start; 481 if (// If the word fits on this line 482 Column + PunctWordLength <= Columns || 483 // ... or the word is "short enough" to take up the next line 484 // without too much ugly white space 485 PunctWordLength < Columns/3) 486 return End; // Take the whole thing as a single "word". 487 488 // The whole quoted/parenthesized string is too long to print as a 489 // single "word". Instead, find the "word" that starts just after 490 // the punctuation and use that end-point instead. This will recurse 491 // until it finds something small enough to consider a word. 492 return findEndOfWord(Start + 1, Str, Length, Column + 1, Columns); 493} 494 495/// \brief Print the given string to a stream, word-wrapping it to 496/// some number of columns in the process. 497/// 498/// \brief OS the stream to which the word-wrapping string will be 499/// emitted. 500/// 501/// \brief Str the string to word-wrap and output. 502/// 503/// \brief Columns the number of columns to word-wrap to. 504/// 505/// \brief Column the column number at which the first character of \p 506/// Str will be printed. This will be non-zero when part of the first 507/// line has already been printed. 508/// 509/// \brief Indentation the number of spaces to indent any lines beyond 510/// the first line. 511/// 512/// \returns true if word-wrapping was required, or false if the 513/// string fit on the first line. 514static bool PrintWordWrapped(llvm::raw_ostream &OS, 515 const llvm::SmallVectorImpl<char> &Str, 516 unsigned Columns, 517 unsigned Column = 0, 518 unsigned Indentation = WordWrapIndentation) { 519 unsigned Length = Str.size(); 520 521 // If there is a newline in this message somewhere, find that 522 // newline and split the message into the part before the newline 523 // (which will be word-wrapped) and the part from the newline one 524 // (which will be emitted unchanged). 525 for (unsigned I = 0; I != Length; ++I) 526 if (Str[I] == '\n') { 527 Length = I; 528 break; 529 } 530 531 // The string used to indent each line. 532 llvm::SmallString<16> IndentStr; 533 IndentStr.assign(Indentation, ' '); 534 bool Wrapped = false; 535 for (unsigned WordStart = 0, WordEnd; WordStart < Length; 536 WordStart = WordEnd) { 537 // Find the beginning of the next word. 538 WordStart = skipWhitespace(WordStart, Str, Length); 539 if (WordStart == Length) 540 break; 541 542 // Find the end of this word. 543 WordEnd = findEndOfWord(WordStart, Str, Length, Column, Columns); 544 545 // Does this word fit on the current line? 546 unsigned WordLength = WordEnd - WordStart; 547 if (Column + WordLength < Columns) { 548 // This word fits on the current line; print it there. 549 if (WordStart) { 550 OS << ' '; 551 Column += 1; 552 } 553 OS.write(&Str[WordStart], WordLength); 554 Column += WordLength; 555 continue; 556 } 557 558 // This word does not fit on the current line, so wrap to the next 559 // line. 560 OS << '\n'; 561 OS.write(&IndentStr[0], Indentation); 562 OS.write(&Str[WordStart], WordLength); 563 Column = Indentation + WordLength; 564 Wrapped = true; 565 } 566 567 if (Length == Str.size()) 568 return Wrapped; // We're done. 569 570 // There is a newline in the message, followed by something that 571 // will not be word-wrapped. Print that. 572 OS.write(&Str[Length], Str.size() - Length); 573 return true; 574} 575 576void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level, 577 const DiagnosticInfo &Info) { 578 // Keeps track of the the starting position of the location 579 // information (e.g., "foo.c:10:4:") that precedes the error 580 // message. We use this information to determine how long the 581 // file+line+column number prefix is. 582 uint64_t StartOfLocationInfo = OS.tell(); 583 584 // If the location is specified, print out a file/line/col and include trace 585 // if enabled. 586 if (Info.getLocation().isValid()) { 587 const SourceManager &SM = Info.getLocation().getManager(); 588 PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation()); 589 unsigned LineNo = PLoc.getLine(); 590 591 // First, if this diagnostic is not in the main file, print out the 592 // "included from" lines. 593 if (LastWarningLoc != PLoc.getIncludeLoc()) { 594 LastWarningLoc = PLoc.getIncludeLoc(); 595 PrintIncludeStack(LastWarningLoc, SM); 596 StartOfLocationInfo = OS.tell(); 597 } 598 599 // Compute the column number. 600 if (ShowLocation) { 601 OS << PLoc.getFilename() << ':' << LineNo << ':'; 602 if (ShowColumn) 603 if (unsigned ColNo = PLoc.getColumn()) 604 OS << ColNo << ':'; 605 606 if (PrintRangeInfo && Info.getNumRanges()) { 607 FileID CaretFileID = 608 SM.getFileID(SM.getInstantiationLoc(Info.getLocation())); 609 bool PrintedRange = false; 610 611 for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i) { 612 // Ignore invalid ranges. 613 if (!Info.getRange(i).isValid()) continue; 614 615 SourceLocation B = Info.getRange(i).getBegin(); 616 SourceLocation E = Info.getRange(i).getEnd(); 617 std::pair<FileID, unsigned> BInfo=SM.getDecomposedInstantiationLoc(B); 618 619 E = SM.getInstantiationLoc(E); 620 std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E); 621 622 // If the start or end of the range is in another file, just discard 623 // it. 624 if (BInfo.first != CaretFileID || EInfo.first != CaretFileID) 625 continue; 626 627 // Add in the length of the token, so that we cover multi-char tokens. 628 unsigned TokSize = Lexer::MeasureTokenLength(E, SM, *LangOpts); 629 630 OS << '{' << SM.getLineNumber(BInfo.first, BInfo.second) << ':' 631 << SM.getColumnNumber(BInfo.first, BInfo.second) << '-' 632 << SM.getLineNumber(EInfo.first, EInfo.second) << ':' 633 << (SM.getColumnNumber(EInfo.first, EInfo.second)+TokSize) << '}'; 634 PrintedRange = true; 635 } 636 637 if (PrintedRange) 638 OS << ':'; 639 } 640 OS << ' '; 641 } 642 } 643 644 switch (Level) { 645 case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type"); 646 case Diagnostic::Note: OS << "note: "; break; 647 case Diagnostic::Warning: OS << "warning: "; break; 648 case Diagnostic::Error: OS << "error: "; break; 649 case Diagnostic::Fatal: OS << "fatal error: "; break; 650 } 651 652 llvm::SmallString<100> OutStr; 653 Info.FormatDiagnostic(OutStr); 654 655 if (PrintDiagnosticOption) 656 if (const char *Opt = Diagnostic::getWarningOptionForDiag(Info.getID())) { 657 OutStr += " [-W"; 658 OutStr += Opt; 659 OutStr += ']'; 660 } 661 662 if (MessageLength) { 663 // We will be word-wrapping the error message, so compute the 664 // column number where we currently are (after printing the 665 // location information). 666 unsigned Column = OS.tell() - StartOfLocationInfo; 667 PrintWordWrapped(OS, OutStr, MessageLength, Column); 668 } else { 669 OS.write(OutStr.begin(), OutStr.size()); 670 } 671 OS << '\n'; 672 673 // If caret diagnostics are enabled and we have location, we want to 674 // emit the caret. However, we only do this if the location moved 675 // from the last diagnostic, if the last diagnostic was a note that 676 // was part of a different warning or error diagnostic, or if the 677 // diagnostic has ranges. We don't want to emit the same caret 678 // multiple times if one loc has multiple diagnostics. 679 if (CaretDiagnostics && Info.getLocation().isValid() && 680 ((LastLoc != Info.getLocation()) || Info.getNumRanges() || 681 (LastCaretDiagnosticWasNote && Level != Diagnostic::Note) || 682 Info.getNumCodeModificationHints())) { 683 // Cache the LastLoc, it allows us to omit duplicate source/caret spewage. 684 LastLoc = Info.getLocation(); 685 LastCaretDiagnosticWasNote = (Level == Diagnostic::Note); 686 687 // Get the ranges into a local array we can hack on. 688 SourceRange Ranges[20]; 689 unsigned NumRanges = Info.getNumRanges(); 690 assert(NumRanges < 20 && "Out of space"); 691 for (unsigned i = 0; i != NumRanges; ++i) 692 Ranges[i] = Info.getRange(i); 693 694 unsigned NumHints = Info.getNumCodeModificationHints(); 695 for (unsigned idx = 0; idx < NumHints; ++idx) { 696 const CodeModificationHint &Hint = Info.getCodeModificationHint(idx); 697 if (Hint.RemoveRange.isValid()) { 698 assert(NumRanges < 20 && "Out of space"); 699 Ranges[NumRanges++] = Hint.RemoveRange; 700 } 701 } 702 703 EmitCaretDiagnostic(LastLoc, Ranges, NumRanges, LastLoc.getManager(), 704 Info.getCodeModificationHints(), 705 Info.getNumCodeModificationHints(), 706 MessageLength); 707 } 708 709 OS.flush(); 710} 711