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 | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 34 | PrintIncludeStack(SourceLocation Pos, SourceManager& SourceMgr) { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 35 | if (Pos.isInvalid()) return; |
| 36 | |
| 37 | Pos = SourceMgr.getLogicalLoc(Pos); |
| 38 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 39 | // Print out the other include frames first. |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 40 | PrintIncludeStack(SourceMgr.getIncludeLoc(Pos),SourceMgr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | unsigned LineNo = SourceMgr.getLineNumber(Pos); |
| 42 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 43 | std::cerr << "In file included from " << SourceMgr.getSourceName(Pos) |
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, |
| 50 | SourceManager& SourceMgr, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | unsigned LineNo, |
| 52 | std::string &CaratLine, |
| 53 | const std::string &SourceLine) { |
| 54 | assert(CaratLine.size() == SourceLine.size() && |
| 55 | "Expect a correspondence between source and carat line!"); |
| 56 | if (!R.isValid()) return; |
| 57 | |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +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 | |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +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) { |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +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) { |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +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. |
Chris Lattner | 9a61194 | 2007-10-17 21:18:47 +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, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | SourceLocation Pos, |
| 104 | diag::kind ID, |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 105 | SourceManager& SourceMgr, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 106 | const std::string *Strs, |
| 107 | unsigned NumStrs, |
| 108 | const SourceRange *Ranges, |
| 109 | unsigned NumRanges) { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 110 | unsigned LineNo = 0, ColNo = 0; |
| 111 | const char *LineStart = 0, *LineEnd = 0; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 112 | |
| 113 | if (Pos.isValid()) { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 114 | SourceLocation LPos = SourceMgr.getLogicalLoc(Pos); |
| 115 | LineNo = SourceMgr.getLineNumber(LPos); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 116 | |
| 117 | // First, if this diagnostic is not in the main file, print out the |
| 118 | // "included from" lines. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 119 | if (LastWarningLoc != SourceMgr.getIncludeLoc(LPos)) { |
| 120 | LastWarningLoc = SourceMgr.getIncludeLoc(LPos); |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 121 | PrintIncludeStack(LastWarningLoc,SourceMgr); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | // Compute the column number. Rewind from the current position to the start |
| 125 | // of the line. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 126 | ColNo = SourceMgr.getColumnNumber(LPos); |
| 127 | const char *TokLogicalPtr = SourceMgr.getCharacterData(LPos); |
| 128 | LineStart = TokLogicalPtr-ColNo+1; // Column # is 1-based |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | |
| 130 | // Compute the line end. Scan forward from the error position to the end of |
| 131 | // the line. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 132 | const llvm::MemoryBuffer *Buffer = SourceMgr.getBuffer(LPos.getFileID()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 133 | const char *BufEnd = Buffer->getBufferEnd(); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 134 | LineEnd = TokLogicalPtr; |
| 135 | while (LineEnd != BufEnd && |
| 136 | *LineEnd != '\n' && *LineEnd != '\r') |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 137 | ++LineEnd; |
| 138 | |
| 139 | std::cerr << Buffer->getBufferIdentifier() |
| 140 | << ":" << LineNo << ":"; |
| 141 | if (ColNo && !NoShowColumn) |
| 142 | std::cerr << ColNo << ":"; |
| 143 | std::cerr << " "; |
| 144 | } |
| 145 | |
| 146 | switch (Level) { |
| 147 | default: assert(0 && "Unknown diagnostic type!"); |
| 148 | case Diagnostic::Note: std::cerr << "note: "; break; |
| 149 | case Diagnostic::Warning: std::cerr << "warning: "; break; |
| 150 | case Diagnostic::Error: std::cerr << "error: "; break; |
| 151 | case Diagnostic::Fatal: std::cerr << "fatal error: "; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 152 | break; |
| 153 | } |
| 154 | |
Chris Lattner | 0750618 | 2007-11-30 22:53:43 +0000 | [diff] [blame] | 155 | std::cerr << FormatDiagnostic(Diags, Level, ID, Strs, NumStrs) << "\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 156 | |
| 157 | if (!NoCaretDiagnostics && Pos.isValid()) { |
| 158 | // Get the line of the source file. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 159 | std::string SourceLine(LineStart, LineEnd); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 160 | |
| 161 | // Create a line for the carat that is filled with spaces that is the same |
| 162 | // length as the line of source code. |
| 163 | std::string CaratLine(LineEnd-LineStart, ' '); |
| 164 | |
| 165 | // Highlight all of the characters covered by Ranges with ~ characters. |
| 166 | for (unsigned i = 0; i != NumRanges; ++i) |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 167 | HighlightRange(Ranges[i], SourceMgr, LineNo, CaratLine, SourceLine); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | |
| 169 | // Next, insert the carat itself. |
| 170 | if (ColNo-1 < CaratLine.size()) |
| 171 | CaratLine[ColNo-1] = '^'; |
| 172 | else |
| 173 | CaratLine.push_back('^'); |
| 174 | |
| 175 | // Scan the source line, looking for tabs. If we find any, manually expand |
| 176 | // them to 8 characters and update the CaratLine to match. |
| 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 | |
| 190 | // Insert spaces or ~'s into CaratLine. |
| 191 | CaratLine.insert(i+1, NumSpaces, CaratLine[i] == '~' ? '~' : ' '); |
| 192 | } |
| 193 | |
| 194 | // Finally, remove any blank spaces from the end of CaratLine. |
| 195 | while (CaratLine[CaratLine.size()-1] == ' ') |
| 196 | CaratLine.erase(CaratLine.end()-1); |
| 197 | |
| 198 | // Emit what we have computed. |
| 199 | std::cerr << SourceLine << "\n"; |
| 200 | std::cerr << CaratLine << "\n"; |
| 201 | } |
| 202 | } |