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