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 | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 18 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | be8e5a4 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 22 | void TextDiagnosticPrinter:: |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 23 | PrintIncludeStack(FullSourceLoc Pos) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 24 | if (Pos.isInvalid()) return; |
| 25 | |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 26 | Pos = Pos.getInstantiationLoc(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 27 | |
| 28 | // Print out the other include frames first. |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 29 | PrintIncludeStack(Pos.getIncludeLoc()); |
| 30 | unsigned LineNo = Pos.getLineNumber(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 31 | |
Nate Begeman | 01d7427 | 2008-04-17 18:06:57 +0000 | [diff] [blame] | 32 | OS << "In file included from " << Pos.getSourceName() |
Chris Lattner | 92a3353 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 33 | << ':' << LineNo << ":\n"; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 34 | } |
| 35 | |
| 36 | /// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s) |
| 37 | /// any characters in LineNo that intersect the SourceRange. |
Ted Kremenek | b3ee193 | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 38 | void TextDiagnosticPrinter::HighlightRange(const SourceRange &R, |
Chris Lattner | 10aaf53 | 2009-01-17 08:45:21 +0000 | [diff] [blame^] | 39 | const SourceManager &SourceMgr, |
| 40 | unsigned LineNo, FileID FID, |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 41 | std::string &CaretLine, |
Nuno Lopes | d0e162c | 2008-08-05 19:40:20 +0000 | [diff] [blame] | 42 | const std::string &SourceLine) { |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 43 | assert(CaretLine.size() == SourceLine.size() && |
| 44 | "Expect a correspondence between source and caret line!"); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 45 | if (!R.isValid()) return; |
| 46 | |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 47 | SourceLocation InstantiationStart = |
| 48 | SourceMgr.getInstantiationLoc(R.getBegin()); |
| 49 | unsigned StartLineNo = SourceMgr.getLineNumber(InstantiationStart); |
Chris Lattner | 10aaf53 | 2009-01-17 08:45:21 +0000 | [diff] [blame^] | 50 | if (StartLineNo > LineNo || |
| 51 | SourceMgr.getCanonicalFileID(InstantiationStart) != FID) |
Chris Lattner | a0030d2 | 2008-01-12 06:43:35 +0000 | [diff] [blame] | 52 | return; // No intersection. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 54 | SourceLocation InstantiationEnd = SourceMgr.getInstantiationLoc(R.getEnd()); |
| 55 | unsigned EndLineNo = SourceMgr.getLineNumber(InstantiationEnd); |
Chris Lattner | 10aaf53 | 2009-01-17 08:45:21 +0000 | [diff] [blame^] | 56 | if (EndLineNo < LineNo || |
| 57 | SourceMgr.getCanonicalFileID(InstantiationEnd) != FID) |
Chris Lattner | a0030d2 | 2008-01-12 06:43:35 +0000 | [diff] [blame] | 58 | return; // No intersection. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 59 | |
| 60 | // Compute the column number of the start. |
| 61 | unsigned StartColNo = 0; |
| 62 | if (StartLineNo == LineNo) { |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 63 | StartColNo = SourceMgr.getInstantiationColumnNumber(R.getBegin()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 64 | if (StartColNo) --StartColNo; // Zero base the col #. |
| 65 | } |
| 66 | |
| 67 | // Pick the first non-whitespace column. |
| 68 | while (StartColNo < SourceLine.size() && |
| 69 | (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t')) |
| 70 | ++StartColNo; |
| 71 | |
| 72 | // Compute the column number of the end. |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 73 | unsigned EndColNo = CaretLine.size(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 74 | if (EndLineNo == LineNo) { |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 75 | EndColNo = SourceMgr.getInstantiationColumnNumber(R.getEnd()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 76 | if (EndColNo) { |
| 77 | --EndColNo; // Zero base the col #. |
| 78 | |
| 79 | // Add in the length of the token, so that we cover multi-char tokens. |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 80 | EndColNo += Lexer::MeasureTokenLength(R.getEnd(), SourceMgr); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 81 | } else { |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 82 | EndColNo = CaretLine.size(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
| 85 | |
| 86 | // Pick the last non-whitespace column. |
Nuno Lopes | d0e162c | 2008-08-05 19:40:20 +0000 | [diff] [blame] | 87 | if (EndColNo <= SourceLine.size()) |
| 88 | while (EndColNo-1 && |
| 89 | (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t')) |
| 90 | --EndColNo; |
| 91 | else |
| 92 | EndColNo = SourceLine.size(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 93 | |
| 94 | // Fill the range with ~'s. |
| 95 | assert(StartColNo <= EndColNo && "Invalid range!"); |
Nuno Lopes | d0e162c | 2008-08-05 19:40:20 +0000 | [diff] [blame] | 96 | for (unsigned i = StartColNo; i < EndColNo; ++i) |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 97 | CaretLine[i] = '~'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 98 | } |
| 99 | |
Chris Lattner | 6948ae6 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 100 | void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level, |
| 101 | const DiagnosticInfo &Info) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 102 | unsigned LineNo = 0, ColNo = 0; |
Chris Lattner | 10aaf53 | 2009-01-17 08:45:21 +0000 | [diff] [blame^] | 103 | FileID FID; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 104 | const char *LineStart = 0, *LineEnd = 0; |
Chris Lattner | 6948ae6 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 105 | const FullSourceLoc &Pos = Info.getLocation(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 106 | |
| 107 | if (Pos.isValid()) { |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 108 | FullSourceLoc LPos = Pos.getInstantiationLoc(); |
Chris Lattner | 10aaf53 | 2009-01-17 08:45:21 +0000 | [diff] [blame^] | 109 | FID = LPos.getFileID(); |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 110 | LineNo = LPos.getLineNumber(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 111 | |
| 112 | // First, if this diagnostic is not in the main file, print out the |
| 113 | // "included from" lines. |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 114 | if (LastWarningLoc != LPos.getIncludeLoc()) { |
| 115 | LastWarningLoc = LPos.getIncludeLoc(); |
| 116 | PrintIncludeStack(LastWarningLoc); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 117 | } |
| 118 | |
| 119 | // Compute the column number. Rewind from the current position to the start |
| 120 | // of the line. |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 121 | ColNo = LPos.getColumnNumber(); |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 122 | const char *TokInstantiationPtr = LPos.getCharacterData(); |
| 123 | LineStart = TokInstantiationPtr-ColNo+1; // Column # is 1-based |
Chris Lattner | 8334334 | 2008-01-25 00:01:10 +0000 | [diff] [blame] | 124 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 125 | // Compute the line end. Scan forward from the error position to the end of |
| 126 | // the line. |
Ted Kremenek | d7f64cd | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 127 | const llvm::MemoryBuffer *Buffer = LPos.getBuffer(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 128 | const char *BufEnd = Buffer->getBufferEnd(); |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 129 | LineEnd = TokInstantiationPtr; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 130 | while (LineEnd != BufEnd && |
| 131 | *LineEnd != '\n' && *LineEnd != '\r') |
| 132 | ++LineEnd; |
| 133 | |
Chris Lattner | 92a3353 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 134 | OS << Buffer->getBufferIdentifier() << ':' << LineNo << ':'; |
Nico Weber | 0e13eaa | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 135 | if (ColNo && ShowColumn) |
Chris Lattner | 92a3353 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 136 | OS << ColNo << ':'; |
| 137 | OS << ' '; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | switch (Level) { |
| 141 | default: assert(0 && "Unknown diagnostic type!"); |
Nate Begeman | 01d7427 | 2008-04-17 18:06:57 +0000 | [diff] [blame] | 142 | case Diagnostic::Note: OS << "note: "; break; |
| 143 | case Diagnostic::Warning: OS << "warning: "; break; |
| 144 | case Diagnostic::Error: OS << "error: "; break; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Chris Lattner | be8e5a4 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 147 | llvm::SmallString<100> OutStr; |
| 148 | Info.FormatDiagnostic(OutStr); |
| 149 | OS.write(OutStr.begin(), OutStr.size()); |
| 150 | OS << '\n'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 151 | |
Chris Lattner | be8e5a4 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 152 | if (CaretDiagnostics && Pos.isValid() && |
| 153 | ((LastLoc != Pos) || Info.getNumRanges())) { |
Steve Naroff | b268d2a | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 154 | // Cache the LastLoc, it allows us to omit duplicate source/caret spewage. |
| 155 | LastLoc = Pos; |
| 156 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 157 | // Get the line of the source file. |
| 158 | std::string SourceLine(LineStart, LineEnd); |
| 159 | |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 160 | // 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] | 161 | // length as the line of source code. |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 162 | std::string CaretLine(LineEnd-LineStart, ' '); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 163 | |
| 164 | // Highlight all of the characters covered by Ranges with ~ characters. |
Chris Lattner | 6948ae6 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 165 | for (unsigned i = 0; i != Info.getNumRanges(); ++i) |
Chris Lattner | 10aaf53 | 2009-01-17 08:45:21 +0000 | [diff] [blame^] | 166 | HighlightRange(Info.getRange(i), Pos.getManager(), LineNo, FID, |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 167 | CaretLine, SourceLine); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 168 | |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 169 | // Next, insert the caret itself. |
| 170 | if (ColNo-1 < CaretLine.size()) |
| 171 | CaretLine[ColNo-1] = '^'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 172 | else |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 173 | CaretLine.push_back('^'); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 174 | |
| 175 | // 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] | 176 | // them to 8 characters and update the CaretLine to match. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 177 | for (unsigned i = 0; i != SourceLine.size(); ++i) { |
| 178 | if (SourceLine[i] != '\t') continue; |
| 179 | |
| 180 | // Replace this tab with at least one space. |
| 181 | SourceLine[i] = ' '; |
| 182 | |
| 183 | // Compute the number of spaces we need to insert. |
| 184 | unsigned NumSpaces = ((i+8)&~7) - (i+1); |
| 185 | assert(NumSpaces < 8 && "Invalid computation of space amt"); |
| 186 | |
| 187 | // Insert spaces into the SourceLine. |
| 188 | SourceLine.insert(i+1, NumSpaces, ' '); |
| 189 | |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 190 | // Insert spaces or ~'s into CaretLine. |
| 191 | CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' '); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 192 | } |
| 193 | |
Gordon Henriksen | f0a835c | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 194 | // Finally, remove any blank spaces from the end of CaretLine. |
| 195 | while (CaretLine[CaretLine.size()-1] == ' ') |
| 196 | CaretLine.erase(CaretLine.end()-1); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 197 | |
| 198 | // Emit what we have computed. |
Chris Lattner | 92a3353 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 199 | OS << SourceLine << '\n'; |
| 200 | OS << CaretLine << '\n'; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 201 | } |
Chris Lattner | 92a3353 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 202 | |
| 203 | OS.flush(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 204 | } |