Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by Bill Wendling and is distributed under the |
| 6 | // University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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" |
| 21 | #include <iostream> |
| 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 | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame^] | 43 | std::cerr << "In file included from " << Pos.getSourceName() |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | << ":" << LineNo << ":\n"; |
| 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, |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame^] | 50 | SourceManager& SourceMgr, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | unsigned LineNo, |
| 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 | |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame^] | 58 | unsigned StartLineNo = SourceMgr.getLogicalLineNumber(R.getBegin()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 59 | if (StartLineNo > LineNo) return; // No intersection. |
| 60 | |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame^] | 61 | unsigned EndLineNo = SourceMgr.getLogicalLineNumber(R.getEnd()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 62 | if (EndLineNo < LineNo) return; // No intersection. |
| 63 | |
| 64 | // Compute the column number of the start. |
| 65 | unsigned StartColNo = 0; |
| 66 | if (StartLineNo == LineNo) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame^] | 67 | StartColNo = SourceMgr.getLogicalColumnNumber(R.getBegin()); |
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. |
| 77 | unsigned EndColNo = CaratLine.size(); |
| 78 | if (EndLineNo == LineNo) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame^] | 79 | EndColNo = SourceMgr.getLogicalColumnNumber(R.getEnd()); |
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. |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame^] | 84 | EndColNo += Lexer::MeasureTokenLength(R.getEnd(), SourceMgr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 85 | } else { |
| 86 | EndColNo = CaratLine.size(); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | // Pick the last non-whitespace column. |
| 91 | while (EndColNo-1 && |
| 92 | (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t')) |
| 93 | --EndColNo; |
| 94 | |
| 95 | // Fill the range with ~'s. |
| 96 | assert(StartColNo <= EndColNo && "Invalid range!"); |
| 97 | for (unsigned i = StartColNo; i != EndColNo; ++i) |
| 98 | CaratLine[i] = '~'; |
| 99 | } |
| 100 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 101 | void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic &Diags, |
| 102 | Diagnostic::Level Level, |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame^] | 103 | FullSourceLoc Pos, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 104 | diag::kind ID, |
| 105 | const std::string *Strs, |
| 106 | unsigned NumStrs, |
| 107 | const SourceRange *Ranges, |
| 108 | unsigned NumRanges) { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 109 | unsigned LineNo = 0, ColNo = 0; |
| 110 | const char *LineStart = 0, *LineEnd = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 111 | |
| 112 | if (Pos.isValid()) { |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame^] | 113 | FullSourceLoc LPos = Pos.getLogicalLoc(); |
| 114 | LineNo = LPos.getLineNumber(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 115 | |
| 116 | // First, if this diagnostic is not in the main file, print out the |
| 117 | // "included from" lines. |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame^] | 118 | if (LastWarningLoc != LPos.getIncludeLoc()) { |
| 119 | LastWarningLoc = LPos.getIncludeLoc(); |
| 120 | PrintIncludeStack(LastWarningLoc); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 121 | } |
| 122 | |
| 123 | // Compute the column number. Rewind from the current position to the start |
| 124 | // of the line. |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame^] | 125 | ColNo = LPos.getColumnNumber(); |
| 126 | const char *TokLogicalPtr = LPos.getCharacterData(); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 127 | LineStart = TokLogicalPtr-ColNo+1; // Column # is 1-based |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 128 | |
| 129 | // Compute the line end. Scan forward from the error position to the end of |
| 130 | // the line. |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame^] | 131 | const llvm::MemoryBuffer *Buffer = LPos.getBuffer(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 132 | const char *BufEnd = Buffer->getBufferEnd(); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 133 | LineEnd = TokLogicalPtr; |
| 134 | while (LineEnd != BufEnd && |
| 135 | *LineEnd != '\n' && *LineEnd != '\r') |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 136 | ++LineEnd; |
| 137 | |
| 138 | std::cerr << Buffer->getBufferIdentifier() |
| 139 | << ":" << LineNo << ":"; |
| 140 | if (ColNo && !NoShowColumn) |
| 141 | std::cerr << ColNo << ":"; |
| 142 | std::cerr << " "; |
| 143 | } |
| 144 | |
| 145 | switch (Level) { |
| 146 | default: assert(0 && "Unknown diagnostic type!"); |
| 147 | case Diagnostic::Note: std::cerr << "note: "; break; |
| 148 | case Diagnostic::Warning: std::cerr << "warning: "; break; |
| 149 | case Diagnostic::Error: std::cerr << "error: "; break; |
| 150 | case Diagnostic::Fatal: std::cerr << "fatal error: "; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 151 | break; |
| 152 | } |
| 153 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 154 | std::cerr << FormatDiagnostic(Diags, Level, ID, Strs, NumStrs) << "\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 155 | |
| 156 | if (!NoCaretDiagnostics && Pos.isValid()) { |
| 157 | // Get the line of the source file. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 158 | std::string SourceLine(LineStart, LineEnd); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 159 | |
| 160 | // Create a line for the carat that is filled with spaces that is the same |
| 161 | // length as the line of source code. |
| 162 | std::string CaratLine(LineEnd-LineStart, ' '); |
| 163 | |
| 164 | // Highlight all of the characters covered by Ranges with ~ characters. |
| 165 | for (unsigned i = 0; i != NumRanges; ++i) |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame^] | 166 | HighlightRange(Ranges[i], Pos.getManager(),LineNo, CaratLine, SourceLine); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 167 | |
| 168 | // Next, insert the carat itself. |
| 169 | if (ColNo-1 < CaratLine.size()) |
| 170 | CaratLine[ColNo-1] = '^'; |
| 171 | else |
| 172 | CaratLine.push_back('^'); |
| 173 | |
| 174 | // Scan the source line, looking for tabs. If we find any, manually expand |
| 175 | // them to 8 characters and update the CaratLine to match. |
| 176 | for (unsigned i = 0; i != SourceLine.size(); ++i) { |
| 177 | if (SourceLine[i] != '\t') continue; |
| 178 | |
| 179 | // Replace this tab with at least one space. |
| 180 | SourceLine[i] = ' '; |
| 181 | |
| 182 | // Compute the number of spaces we need to insert. |
| 183 | unsigned NumSpaces = ((i+8)&~7) - (i+1); |
| 184 | assert(NumSpaces < 8 && "Invalid computation of space amt"); |
| 185 | |
| 186 | // Insert spaces into the SourceLine. |
| 187 | SourceLine.insert(i+1, NumSpaces, ' '); |
| 188 | |
| 189 | // Insert spaces or ~'s into CaratLine. |
| 190 | CaratLine.insert(i+1, NumSpaces, CaratLine[i] == '~' ? '~' : ' '); |
| 191 | } |
| 192 | |
| 193 | // Finally, remove any blank spaces from the end of CaratLine. |
| 194 | while (CaratLine[CaratLine.size()-1] == ' ') |
| 195 | CaratLine.erase(CaratLine.end()-1); |
| 196 | |
| 197 | // Emit what we have computed. |
| 198 | std::cerr << SourceLine << "\n"; |
| 199 | std::cerr << CaratLine << "\n"; |
| 200 | } |
| 201 | } |