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