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