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