Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This diagnostic client prints out their diagnostic messages. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Nico Weber | 0e13eaa | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 14 | #include "clang/Driver/TextDiagnosticPrinter.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 15 | #include "clang/Basic/SourceManager.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 16 | #include "clang/Lex/Lexer.h" |
Chris Lattner | 92a3353 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | be8e5a4 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 21 | void TextDiagnosticPrinter:: |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 22 | PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) { |
| 23 | if (Loc.isInvalid()) return; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 24 | |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 25 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 26 | |
| 27 | // Print out the other include frames first. |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 28 | PrintIncludeStack(PLoc.getIncludeLoc(), SM); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 29 | |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 30 | OS << "In file included from " << PLoc.getFilename() |
| 31 | << ':' << PLoc.getLine() << ":\n"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 32 | } |
| 33 | |
| 34 | /// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s) |
| 35 | /// any characters in LineNo that intersect the SourceRange. |
Ted Kremenek | b3ee193 | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 36 | void TextDiagnosticPrinter::HighlightRange(const SourceRange &R, |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 37 | const SourceManager &SM, |
Chris Lattner | 10aaf53 | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 38 | unsigned LineNo, FileID FID, |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 39 | std::string &CaretLine, |
Nuno Lopes | d0e162c | 2008-08-05 19:40:20 +0000 | [diff] [blame] | 40 | const std::string &SourceLine) { |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 41 | assert(CaretLine.size() == SourceLine.size() && |
| 42 | "Expect a correspondence between source and caret line!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 43 | if (!R.isValid()) return; |
| 44 | |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 45 | SourceLocation Begin = SM.getInstantiationLoc(R.getBegin()); |
| 46 | SourceLocation End = SM.getInstantiationLoc(R.getEnd()); |
| 47 | |
Chris Lattner | 2d89c56 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 48 | unsigned StartLineNo = SM.getInstantiationLineNumber(Begin); |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 49 | if (StartLineNo > LineNo || SM.getFileID(Begin) != FID) |
Chris Lattner | a0030d2 | 2008-01-12 06:43:35 +0000 | [diff] [blame] | 50 | return; // No intersection. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 51 | |
Chris Lattner | 2d89c56 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 52 | unsigned EndLineNo = SM.getInstantiationLineNumber(End); |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 53 | if (EndLineNo < LineNo || SM.getFileID(End) != FID) |
Chris Lattner | a0030d2 | 2008-01-12 06:43:35 +0000 | [diff] [blame] | 54 | return; // No intersection. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 55 | |
| 56 | // Compute the column number of the start. |
| 57 | unsigned StartColNo = 0; |
| 58 | if (StartLineNo == LineNo) { |
Chris Lattner | e79fc85 | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 59 | StartColNo = SM.getInstantiationColumnNumber(Begin); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 60 | if (StartColNo) --StartColNo; // Zero base the col #. |
| 61 | } |
| 62 | |
| 63 | // Pick the first non-whitespace column. |
| 64 | while (StartColNo < SourceLine.size() && |
| 65 | (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t')) |
| 66 | ++StartColNo; |
| 67 | |
| 68 | // Compute the column number of the end. |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 69 | unsigned EndColNo = CaretLine.size(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 70 | if (EndLineNo == LineNo) { |
Chris Lattner | e79fc85 | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 71 | EndColNo = SM.getInstantiationColumnNumber(End); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 72 | if (EndColNo) { |
| 73 | --EndColNo; // Zero base the col #. |
| 74 | |
| 75 | // Add in the length of the token, so that we cover multi-char tokens. |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 76 | EndColNo += Lexer::MeasureTokenLength(End, SM); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 77 | } else { |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 78 | EndColNo = CaretLine.size(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | // Pick the last non-whitespace column. |
Nuno Lopes | d0e162c | 2008-08-05 19:40:20 +0000 | [diff] [blame] | 83 | if (EndColNo <= SourceLine.size()) |
| 84 | while (EndColNo-1 && |
| 85 | (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t')) |
| 86 | --EndColNo; |
| 87 | else |
| 88 | EndColNo = SourceLine.size(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 89 | |
| 90 | // Fill the range with ~'s. |
| 91 | assert(StartColNo <= EndColNo && "Invalid range!"); |
Nuno Lopes | d0e162c | 2008-08-05 19:40:20 +0000 | [diff] [blame] | 92 | for (unsigned i = StartColNo; i < EndColNo; ++i) |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 93 | CaretLine[i] = '~'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 94 | } |
| 95 | |
Chris Lattner | 6948ae6 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 96 | void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level, |
| 97 | const DiagnosticInfo &Info) { |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 98 | unsigned ColNo = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 99 | |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 100 | // If the location is specified, print out a file/line/col and include trace |
| 101 | // if enabled. |
| 102 | if (Info.getLocation().isValid()) { |
Ted Kremenek | dd62ea6 | 2009-01-28 20:47:47 +0000 | [diff] [blame] | 103 | const SourceManager &SM = Info.getLocation().getManager(); |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 104 | PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation()); |
| 105 | unsigned LineNo = PLoc.getLine(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 106 | |
| 107 | // First, if this diagnostic is not in the main file, print out the |
| 108 | // "included from" lines. |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 109 | if (LastWarningLoc != PLoc.getIncludeLoc()) { |
| 110 | LastWarningLoc = PLoc.getIncludeLoc(); |
| 111 | PrintIncludeStack(LastWarningLoc, SM); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 112 | } |
| 113 | |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 114 | // Compute the column number. |
| 115 | ColNo = PLoc.getColumn(); |
Chris Lattner | 68c1e19 | 2009-01-30 17:41:53 +0000 | [diff] [blame] | 116 | if (ShowLocation) { |
| 117 | OS << PLoc.getFilename() << ':' << LineNo << ':'; |
| 118 | if (ColNo && ShowColumn) |
| 119 | OS << ColNo << ':'; |
| 120 | OS << ' '; |
| 121 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | switch (Level) { |
| 125 | default: assert(0 && "Unknown diagnostic type!"); |
Nate Begeman | 01d7427 | 2008-04-17 18:06:57 +0000 | [diff] [blame] | 126 | case Diagnostic::Note: OS << "note: "; break; |
| 127 | case Diagnostic::Warning: OS << "warning: "; break; |
| 128 | case Diagnostic::Error: OS << "error: "; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 129 | } |
| 130 | |
Chris Lattner | be8e5a4 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 131 | llvm::SmallString<100> OutStr; |
| 132 | Info.FormatDiagnostic(OutStr); |
| 133 | OS.write(OutStr.begin(), OutStr.size()); |
| 134 | OS << '\n'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 135 | |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 136 | // If caret diagnostics are enabled and we have location, we want to emit the |
| 137 | // caret. However, we only do this if the location moved from the last |
| 138 | // diagnostic, or if the diagnostic has ranges. We don't want to emit the |
| 139 | // same caret multiple times if one loc has multiple diagnostics. |
| 140 | if (CaretDiagnostics && Info.getLocation().isValid() && |
| 141 | ((LastLoc != Info.getLocation()) || Info.getNumRanges())) { |
Steve Naroff | b268d2a | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 142 | // Cache the LastLoc, it allows us to omit duplicate source/caret spewage. |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 143 | LastLoc = Info.getLocation(); |
| 144 | |
| 145 | // Inspect the actual instantiation point of the diagnostic, we don't care |
| 146 | // about presumed locations anymore. |
Ted Kremenek | dd62ea6 | 2009-01-28 20:47:47 +0000 | [diff] [blame] | 147 | FullSourceLoc ILoc = Info.getLocation().getInstantiationLoc(); |
| 148 | |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 149 | // Rewind from the current position to the start of the line. |
Ted Kremenek | dd62ea6 | 2009-01-28 20:47:47 +0000 | [diff] [blame] | 150 | const char *TokInstantiationPtr = ILoc.getCharacterData(); |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 151 | const char *LineStart = TokInstantiationPtr-ColNo+1; // Column # is 1-based. |
| 152 | |
| 153 | // Compute the line end. Scan forward from the error position to the end of |
| 154 | // the line. |
Ted Kremenek | dd62ea6 | 2009-01-28 20:47:47 +0000 | [diff] [blame] | 155 | const char *BufEnd = ILoc.getBufferData().second; |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 156 | const char *LineEnd = TokInstantiationPtr; |
| 157 | while (LineEnd != BufEnd && |
| 158 | *LineEnd != '\n' && *LineEnd != '\r') |
| 159 | ++LineEnd; |
| 160 | |
| 161 | // Copy the line of code into an std::string for ease of manipulation. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 162 | std::string SourceLine(LineStart, LineEnd); |
| 163 | |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 164 | // Create a line for the caret that is filled with spaces that is the same |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 165 | // length as the line of source code. |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 166 | std::string CaretLine(LineEnd-LineStart, ' '); |
Chris Lattner | 836774b | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 167 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 168 | // Highlight all of the characters covered by Ranges with ~ characters. |
Chris Lattner | 6948ae6 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 169 | for (unsigned i = 0; i != Info.getNumRanges(); ++i) |
Chris Lattner | 2d89c56 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 170 | HighlightRange(Info.getRange(i), ILoc.getManager(), |
| 171 | ILoc.getInstantiationLineNumber(), |
Ted Kremenek | dd62ea6 | 2009-01-28 20:47:47 +0000 | [diff] [blame] | 172 | ILoc.getFileID(), CaretLine, SourceLine); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 173 | |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 174 | // Next, insert the caret itself. |
| 175 | if (ColNo-1 < CaretLine.size()) |
| 176 | CaretLine[ColNo-1] = '^'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 177 | else |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 178 | CaretLine.push_back('^'); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 179 | |
| 180 | // Scan the source line, looking for tabs. If we find any, manually expand |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 181 | // them to 8 characters and update the CaretLine to match. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 182 | for (unsigned i = 0; i != SourceLine.size(); ++i) { |
| 183 | if (SourceLine[i] != '\t') continue; |
| 184 | |
| 185 | // Replace this tab with at least one space. |
| 186 | SourceLine[i] = ' '; |
| 187 | |
| 188 | // Compute the number of spaces we need to insert. |
| 189 | unsigned NumSpaces = ((i+8)&~7) - (i+1); |
| 190 | assert(NumSpaces < 8 && "Invalid computation of space amt"); |
| 191 | |
| 192 | // Insert spaces into the SourceLine. |
| 193 | SourceLine.insert(i+1, NumSpaces, ' '); |
| 194 | |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 195 | // Insert spaces or ~'s into CaretLine. |
| 196 | CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' '); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 197 | } |
| 198 | |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 199 | // Finally, remove any blank spaces from the end of CaretLine. |
| 200 | while (CaretLine[CaretLine.size()-1] == ' ') |
| 201 | CaretLine.erase(CaretLine.end()-1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 202 | |
| 203 | // Emit what we have computed. |
Chris Lattner | 92a3353 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 204 | OS << SourceLine << '\n'; |
| 205 | OS << CaretLine << '\n'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 206 | } |
Chris Lattner | 92a3353 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 207 | |
| 208 | OS.flush(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 209 | } |