Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 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. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This diagnostic client prints out their diagnostic messages. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Nico Weber | fd54ebc | 2008-08-05 23:33:20 +0000 | [diff] [blame] | 14 | #include "clang/Driver/TextDiagnosticPrinter.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 15 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/Lex/Lexer.h" |
Chris Lattner | a03a5b5 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | using namespace clang; |
| 20 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | void TextDiagnosticPrinter:: |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 22 | PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) { |
| 23 | if (Loc.isInvalid()) return; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 24 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 25 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 26 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 27 | // Print out the other include frames first. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 28 | PrintIncludeStack(PLoc.getIncludeLoc(), SM); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 30 | OS << "In file included from " << PLoc.getFilename() |
| 31 | << ':' << PLoc.getLine() << ":\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +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 | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 36 | void TextDiagnosticPrinter::HighlightRange(const SourceRange &R, |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 37 | const SourceManager &SM, |
Chris Lattner | 3b4d5e9 | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 38 | unsigned LineNo, FileID FID, |
Gordon Henriksen | aad6953 | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 39 | std::string &CaretLine, |
Nuno Lopes | db82568 | 2008-08-05 19:40:20 +0000 | [diff] [blame] | 40 | const std::string &SourceLine) { |
Gordon Henriksen | aad6953 | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 41 | assert(CaretLine.size() == SourceLine.size() && |
| 42 | "Expect a correspondence between source and caret line!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 43 | if (!R.isValid()) return; |
| 44 | |
Chris Lattner | b9c3f96 | 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 | 34837a5 | 2009-02-17 05:19:10 +0000 | [diff] [blame] | 48 | // If the End location and the start location are the same and are a macro |
| 49 | // location, then the range was something that came from a macro expansion |
| 50 | // or _Pragma. If this is an object-like macro, the best we can do is to |
| 51 | // highlight the range. If this is a function-like macro, we'd also like to |
| 52 | // highlight the arguments. |
| 53 | if (Begin == End && R.getEnd().isMacroID()) |
| 54 | End = SM.getInstantiationRange(R.getEnd()).second; |
| 55 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 56 | unsigned StartLineNo = SM.getInstantiationLineNumber(Begin); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 57 | if (StartLineNo > LineNo || SM.getFileID(Begin) != FID) |
Chris Lattner | e41b7cd | 2008-01-12 06:43:35 +0000 | [diff] [blame] | 58 | return; // No intersection. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 60 | unsigned EndLineNo = SM.getInstantiationLineNumber(End); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 61 | if (EndLineNo < LineNo || SM.getFileID(End) != FID) |
Chris Lattner | e41b7cd | 2008-01-12 06:43:35 +0000 | [diff] [blame] | 62 | return; // No intersection. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 63 | |
| 64 | // Compute the column number of the start. |
| 65 | unsigned StartColNo = 0; |
| 66 | if (StartLineNo == LineNo) { |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 67 | StartColNo = SM.getInstantiationColumnNumber(Begin); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 68 | if (StartColNo) --StartColNo; // Zero base the col #. |
| 69 | } |
| 70 | |
| 71 | // Pick the first non-whitespace column. |
| 72 | while (StartColNo < SourceLine.size() && |
| 73 | (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t')) |
| 74 | ++StartColNo; |
| 75 | |
| 76 | // Compute the column number of the end. |
Gordon Henriksen | aad6953 | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 77 | unsigned EndColNo = CaretLine.size(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 78 | if (EndLineNo == LineNo) { |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 79 | EndColNo = SM.getInstantiationColumnNumber(End); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 80 | if (EndColNo) { |
| 81 | --EndColNo; // Zero base the col #. |
| 82 | |
| 83 | // Add in the length of the token, so that we cover multi-char tokens. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 84 | EndColNo += Lexer::MeasureTokenLength(End, SM); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 85 | } else { |
Gordon Henriksen | aad6953 | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 86 | EndColNo = CaretLine.size(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 87 | } |
| 88 | } |
| 89 | |
| 90 | // Pick the last non-whitespace column. |
Nuno Lopes | db82568 | 2008-08-05 19:40:20 +0000 | [diff] [blame] | 91 | if (EndColNo <= SourceLine.size()) |
| 92 | while (EndColNo-1 && |
| 93 | (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t')) |
| 94 | --EndColNo; |
| 95 | else |
| 96 | EndColNo = SourceLine.size(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 97 | |
| 98 | // Fill the range with ~'s. |
| 99 | assert(StartColNo <= EndColNo && "Invalid range!"); |
Nuno Lopes | db82568 | 2008-08-05 19:40:20 +0000 | [diff] [blame] | 100 | for (unsigned i = StartColNo; i < EndColNo; ++i) |
Gordon Henriksen | aad6953 | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 101 | CaretLine[i] = '~'; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame^] | 104 | void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc, |
| 105 | const SourceRange *Ranges, |
| 106 | unsigned NumRanges, |
Chris Lattner | b88af81 | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 107 | SourceManager &SM) { |
Chris Lattner | 55dcef0 | 2009-02-17 08:44:50 +0000 | [diff] [blame] | 108 | assert(!Loc.isInvalid() && "must have a valid source location here"); |
| 109 | |
Chris Lattner | 49a48eb | 2009-02-17 07:54:55 +0000 | [diff] [blame] | 110 | // We always emit diagnostics about the instantiation points, not the spelling |
| 111 | // points. This more closely correlates to what the user writes. |
Chris Lattner | 55dcef0 | 2009-02-17 08:44:50 +0000 | [diff] [blame] | 112 | if (!Loc.isFileID()) { |
Chris Lattner | 609b3ab | 2009-02-18 18:50:45 +0000 | [diff] [blame] | 113 | SourceLocation OneLevelUp = SM.getImmediateInstantiationRange(Loc).first; |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame^] | 114 | EmitCaretDiagnostic(OneLevelUp, Ranges, NumRanges, SM); |
Chris Lattner | 55dcef0 | 2009-02-17 08:44:50 +0000 | [diff] [blame] | 115 | |
| 116 | Loc = SM.getInstantiationLoc(SM.getImmediateSpellingLoc(Loc)); |
| 117 | |
| 118 | // Emit the file/line/column that this expansion came from. |
| 119 | OS << SM.getBufferName(Loc) << ':' << SM.getInstantiationLineNumber(Loc) |
| 120 | << ':'; |
| 121 | if (ShowColumn) |
| 122 | OS << SM.getInstantiationColumnNumber(Loc) << ':'; |
| 123 | OS << " note: instantiated from:\n"; |
| 124 | } |
Chris Lattner | b88af81 | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 125 | |
| 126 | // Decompose the location into a FID/Offset pair. |
| 127 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); |
| 128 | FileID FID = LocInfo.first; |
| 129 | unsigned FileOffset = LocInfo.second; |
| 130 | |
| 131 | // Get information about the buffer it points into. |
| 132 | std::pair<const char*, const char*> BufferInfo = SM.getBufferData(FID); |
| 133 | const char *BufStart = BufferInfo.first; |
| 134 | const char *BufEnd = BufferInfo.second; |
| 135 | |
| 136 | unsigned ColNo = SM.getColumnNumber(FID, FileOffset); |
Chris Lattner | 94f5578 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 137 | |
| 138 | // Rewind from the current position to the start of the line. |
Chris Lattner | b88af81 | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 139 | const char *TokPtr = BufStart+FileOffset; |
| 140 | const char *LineStart = TokPtr-ColNo+1; // Column # is 1-based. |
| 141 | |
Chris Lattner | 94f5578 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 142 | |
| 143 | // Compute the line end. Scan forward from the error position to the end of |
| 144 | // the line. |
Chris Lattner | b88af81 | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 145 | const char *LineEnd = TokPtr; |
Chris Lattner | 94f5578 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 146 | while (LineEnd != BufEnd && |
| 147 | *LineEnd != '\n' && *LineEnd != '\r') |
| 148 | ++LineEnd; |
| 149 | |
| 150 | // Copy the line of code into an std::string for ease of manipulation. |
| 151 | std::string SourceLine(LineStart, LineEnd); |
| 152 | |
| 153 | // Create a line for the caret that is filled with spaces that is the same |
| 154 | // length as the line of source code. |
| 155 | std::string CaretLine(LineEnd-LineStart, ' '); |
| 156 | |
| 157 | // Highlight all of the characters covered by Ranges with ~ characters. |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame^] | 158 | if (NumRanges) { |
Chris Lattner | b88af81 | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 159 | unsigned LineNo = SM.getLineNumber(FID, FileOffset); |
| 160 | |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame^] | 161 | for (unsigned i = 0, e = NumRanges; i != e; ++i) |
| 162 | HighlightRange(Ranges[i], SM, LineNo, FID, CaretLine, SourceLine); |
Chris Lattner | b88af81 | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 163 | } |
Chris Lattner | 94f5578 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 164 | |
| 165 | // Next, insert the caret itself. |
| 166 | if (ColNo-1 < CaretLine.size()) |
| 167 | CaretLine[ColNo-1] = '^'; |
| 168 | else |
| 169 | CaretLine.push_back('^'); |
| 170 | |
| 171 | // Scan the source line, looking for tabs. If we find any, manually expand |
| 172 | // them to 8 characters and update the CaretLine to match. |
| 173 | for (unsigned i = 0; i != SourceLine.size(); ++i) { |
| 174 | if (SourceLine[i] != '\t') continue; |
| 175 | |
| 176 | // Replace this tab with at least one space. |
| 177 | SourceLine[i] = ' '; |
| 178 | |
| 179 | // Compute the number of spaces we need to insert. |
| 180 | unsigned NumSpaces = ((i+8)&~7) - (i+1); |
| 181 | assert(NumSpaces < 8 && "Invalid computation of space amt"); |
| 182 | |
| 183 | // Insert spaces into the SourceLine. |
| 184 | SourceLine.insert(i+1, NumSpaces, ' '); |
| 185 | |
| 186 | // Insert spaces or ~'s into CaretLine. |
| 187 | CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' '); |
| 188 | } |
| 189 | |
| 190 | // Finally, remove any blank spaces from the end of CaretLine. |
| 191 | while (CaretLine[CaretLine.size()-1] == ' ') |
| 192 | CaretLine.erase(CaretLine.end()-1); |
| 193 | |
| 194 | // Emit what we have computed. |
| 195 | OS << SourceLine << '\n'; |
| 196 | OS << CaretLine << '\n'; |
| 197 | } |
| 198 | |
| 199 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 200 | void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level, |
| 201 | const DiagnosticInfo &Info) { |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 202 | // If the location is specified, print out a file/line/col and include trace |
| 203 | // if enabled. |
| 204 | if (Info.getLocation().isValid()) { |
Ted Kremenek | 05f3957 | 2009-01-28 20:47:47 +0000 | [diff] [blame] | 205 | const SourceManager &SM = Info.getLocation().getManager(); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 206 | PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation()); |
| 207 | unsigned LineNo = PLoc.getLine(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 208 | |
| 209 | // First, if this diagnostic is not in the main file, print out the |
| 210 | // "included from" lines. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 211 | if (LastWarningLoc != PLoc.getIncludeLoc()) { |
| 212 | LastWarningLoc = PLoc.getIncludeLoc(); |
| 213 | PrintIncludeStack(LastWarningLoc, SM); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 216 | // Compute the column number. |
Chris Lattner | b8bf65e | 2009-01-30 17:41:53 +0000 | [diff] [blame] | 217 | if (ShowLocation) { |
| 218 | OS << PLoc.getFilename() << ':' << LineNo << ':'; |
Chris Lattner | 8f7b396 | 2009-02-17 07:34:34 +0000 | [diff] [blame] | 219 | if (ShowColumn) |
| 220 | if (unsigned ColNo = PLoc.getColumn()) |
| 221 | OS << ColNo << ':'; |
Chris Lattner | b8bf65e | 2009-01-30 17:41:53 +0000 | [diff] [blame] | 222 | OS << ' '; |
| 223 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 224 | } |
| 225 | |
| 226 | switch (Level) { |
Chris Lattner | 4132758 | 2009-02-06 03:57:44 +0000 | [diff] [blame] | 227 | case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type"); |
Nate Begeman | 165b954 | 2008-04-17 18:06:57 +0000 | [diff] [blame] | 228 | case Diagnostic::Note: OS << "note: "; break; |
| 229 | case Diagnostic::Warning: OS << "warning: "; break; |
| 230 | case Diagnostic::Error: OS << "error: "; break; |
Chris Lattner | 4132758 | 2009-02-06 03:57:44 +0000 | [diff] [blame] | 231 | case Diagnostic::Fatal: OS << "fatal error: "; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 234 | llvm::SmallString<100> OutStr; |
| 235 | Info.FormatDiagnostic(OutStr); |
| 236 | OS.write(OutStr.begin(), OutStr.size()); |
| 237 | OS << '\n'; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 238 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 239 | // If caret diagnostics are enabled and we have location, we want to emit the |
| 240 | // caret. However, we only do this if the location moved from the last |
| 241 | // diagnostic, or if the diagnostic has ranges. We don't want to emit the |
| 242 | // same caret multiple times if one loc has multiple diagnostics. |
| 243 | if (CaretDiagnostics && Info.getLocation().isValid() && |
| 244 | ((LastLoc != Info.getLocation()) || Info.getNumRanges())) { |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 245 | // Cache the LastLoc, it allows us to omit duplicate source/caret spewage. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 246 | LastLoc = Info.getLocation(); |
| 247 | |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame^] | 248 | // Get the ranges into a local array we can hack on. |
| 249 | SourceRange Ranges[10]; |
| 250 | unsigned NumRanges = Info.getNumRanges(); |
| 251 | assert(NumRanges < 10 && "Out of space"); |
| 252 | for (unsigned i = 0; i != NumRanges; ++i) |
| 253 | Ranges[i] = Info.getRange(i); |
| 254 | |
| 255 | EmitCaretDiagnostic(LastLoc, Ranges, NumRanges, LastLoc.getManager()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 256 | } |
Chris Lattner | a03a5b5 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 257 | |
| 258 | OS.flush(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 259 | } |