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 | |
Daniel Dunbar | e1bd4e6 | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 15 | #include "clang/Basic/SourceManager.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 16 | #include "clang/Lex/Lexer.h" |
Chris Lattner | 037fb7f | 2009-05-05 22:03:18 +0000 | [diff] [blame] | 17 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | a03a5b5 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Douglas Gregor | fffd93f | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 23 | /// \brief Number of spaces to indent when word-wrapping. |
| 24 | const unsigned WordWrapIndentation = 6; |
| 25 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | void TextDiagnosticPrinter:: |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 27 | PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) { |
| 28 | if (Loc.isInvalid()) return; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 29 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 30 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 31 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | // Print out the other include frames first. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 33 | PrintIncludeStack(PLoc.getIncludeLoc(), SM); |
Chris Lattner | 5ce24c8 | 2009-04-21 03:57:54 +0000 | [diff] [blame] | 34 | |
| 35 | if (ShowLocation) |
| 36 | OS << "In file included from " << PLoc.getFilename() |
| 37 | << ':' << PLoc.getLine() << ":\n"; |
| 38 | else |
| 39 | OS << "In included file:\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | } |
| 41 | |
| 42 | /// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s) |
| 43 | /// any characters in LineNo that intersect the SourceRange. |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 44 | void TextDiagnosticPrinter::HighlightRange(const SourceRange &R, |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 45 | const SourceManager &SM, |
Chris Lattner | 3b4d5e9 | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 46 | unsigned LineNo, FileID FID, |
Gordon Henriksen | aad6953 | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 47 | std::string &CaretLine, |
Nuno Lopes | db82568 | 2008-08-05 19:40:20 +0000 | [diff] [blame] | 48 | const std::string &SourceLine) { |
Gordon Henriksen | aad6953 | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 49 | assert(CaretLine.size() == SourceLine.size() && |
| 50 | "Expect a correspondence between source and caret line!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 51 | if (!R.isValid()) return; |
| 52 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 53 | SourceLocation Begin = SM.getInstantiationLoc(R.getBegin()); |
| 54 | SourceLocation End = SM.getInstantiationLoc(R.getEnd()); |
| 55 | |
Chris Lattner | 34837a5 | 2009-02-17 05:19:10 +0000 | [diff] [blame] | 56 | // If the End location and the start location are the same and are a macro |
| 57 | // location, then the range was something that came from a macro expansion |
| 58 | // or _Pragma. If this is an object-like macro, the best we can do is to |
| 59 | // highlight the range. If this is a function-like macro, we'd also like to |
| 60 | // highlight the arguments. |
| 61 | if (Begin == End && R.getEnd().isMacroID()) |
| 62 | End = SM.getInstantiationRange(R.getEnd()).second; |
| 63 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 64 | unsigned StartLineNo = SM.getInstantiationLineNumber(Begin); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 65 | if (StartLineNo > LineNo || SM.getFileID(Begin) != FID) |
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 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 68 | unsigned EndLineNo = SM.getInstantiationLineNumber(End); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 69 | if (EndLineNo < LineNo || SM.getFileID(End) != FID) |
Chris Lattner | e41b7cd | 2008-01-12 06:43:35 +0000 | [diff] [blame] | 70 | return; // No intersection. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 71 | |
| 72 | // Compute the column number of the start. |
| 73 | unsigned StartColNo = 0; |
| 74 | if (StartLineNo == LineNo) { |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 75 | StartColNo = SM.getInstantiationColumnNumber(Begin); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 76 | if (StartColNo) --StartColNo; // Zero base the col #. |
| 77 | } |
| 78 | |
| 79 | // Pick the first non-whitespace column. |
| 80 | while (StartColNo < SourceLine.size() && |
| 81 | (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t')) |
| 82 | ++StartColNo; |
| 83 | |
| 84 | // Compute the column number of the end. |
Gordon Henriksen | aad6953 | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 85 | unsigned EndColNo = CaretLine.size(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 86 | if (EndLineNo == LineNo) { |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 87 | EndColNo = SM.getInstantiationColumnNumber(End); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 88 | if (EndColNo) { |
| 89 | --EndColNo; // Zero base the col #. |
| 90 | |
| 91 | // Add in the length of the token, so that we cover multi-char tokens. |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 92 | EndColNo += Lexer::MeasureTokenLength(End, SM, *LangOpts); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 93 | } else { |
Gordon Henriksen | aad6953 | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 94 | EndColNo = CaretLine.size(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 95 | } |
| 96 | } |
| 97 | |
| 98 | // Pick the last non-whitespace column. |
Nuno Lopes | db82568 | 2008-08-05 19:40:20 +0000 | [diff] [blame] | 99 | if (EndColNo <= SourceLine.size()) |
| 100 | while (EndColNo-1 && |
| 101 | (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t')) |
| 102 | --EndColNo; |
| 103 | else |
| 104 | EndColNo = SourceLine.size(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 105 | |
| 106 | // Fill the range with ~'s. |
| 107 | assert(StartColNo <= EndColNo && "Invalid range!"); |
Nuno Lopes | db82568 | 2008-08-05 19:40:20 +0000 | [diff] [blame] | 108 | for (unsigned i = StartColNo; i < EndColNo; ++i) |
Gordon Henriksen | aad6953 | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 109 | CaretLine[i] = '~'; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | } |
| 111 | |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 112 | /// \brief When the source code line we want to print is too long for |
| 113 | /// the terminal, select the "interesting" region. |
| 114 | static void SelectInterestingSourceRegion(std::string &SourceLine, |
| 115 | std::string &CaretLine, |
| 116 | std::string &FixItInsertionLine, |
Douglas Gregor | cfe1f9d | 2009-05-04 06:27:32 +0000 | [diff] [blame] | 117 | unsigned EndOfCaretToken, |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 118 | unsigned Columns) { |
| 119 | if (CaretLine.size() > SourceLine.size()) |
| 120 | SourceLine.resize(CaretLine.size(), ' '); |
| 121 | |
| 122 | // Find the slice that we need to display the full caret line |
| 123 | // correctly. |
| 124 | unsigned CaretStart = 0, CaretEnd = CaretLine.size(); |
| 125 | for (; CaretStart != CaretEnd; ++CaretStart) |
| 126 | if (!isspace(CaretLine[CaretStart])) |
| 127 | break; |
| 128 | |
| 129 | for (; CaretEnd != CaretStart; --CaretEnd) |
| 130 | if (!isspace(CaretLine[CaretEnd - 1])) |
| 131 | break; |
Douglas Gregor | cfe1f9d | 2009-05-04 06:27:32 +0000 | [diff] [blame] | 132 | |
| 133 | // Make sure we don't chop the string shorter than the caret token |
| 134 | // itself. |
| 135 | if (CaretEnd < EndOfCaretToken) |
| 136 | CaretEnd = EndOfCaretToken; |
| 137 | |
Douglas Gregor | 844da34 | 2009-05-03 04:33:32 +0000 | [diff] [blame] | 138 | // If we have a fix-it line, make sure the slice includes all of the |
| 139 | // fix-it information. |
| 140 | if (!FixItInsertionLine.empty()) { |
| 141 | unsigned FixItStart = 0, FixItEnd = FixItInsertionLine.size(); |
| 142 | for (; FixItStart != FixItEnd; ++FixItStart) |
| 143 | if (!isspace(FixItInsertionLine[FixItStart])) |
| 144 | break; |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 145 | |
Douglas Gregor | 844da34 | 2009-05-03 04:33:32 +0000 | [diff] [blame] | 146 | for (; FixItEnd != FixItStart; --FixItEnd) |
| 147 | if (!isspace(FixItInsertionLine[FixItEnd - 1])) |
| 148 | break; |
| 149 | |
| 150 | if (FixItStart < CaretStart) |
| 151 | CaretStart = FixItStart; |
| 152 | if (FixItEnd > CaretEnd) |
| 153 | CaretEnd = FixItEnd; |
| 154 | } |
| 155 | |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 156 | // CaretLine[CaretStart, CaretEnd) contains all of the interesting |
| 157 | // parts of the caret line. While this slice is smaller than the |
| 158 | // number of columns we have, try to grow the slice to encompass |
| 159 | // more context. |
| 160 | |
| 161 | // If the end of the interesting region comes before we run out of |
| 162 | // space in the terminal, start at the beginning of the line. |
Douglas Gregor | 2167de4 | 2009-05-03 15:24:25 +0000 | [diff] [blame] | 163 | if (CaretEnd < Columns - 3) |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 164 | CaretStart = 0; |
| 165 | |
Douglas Gregor | 7d101f6 | 2009-05-03 04:12:51 +0000 | [diff] [blame] | 166 | unsigned TargetColumns = Columns - 8; // Give us extra room for the ellipses. |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 167 | unsigned SourceLength = SourceLine.size(); |
Douglas Gregor | 2fb3ea3 | 2009-05-04 06:45:38 +0000 | [diff] [blame] | 168 | while ((CaretEnd - CaretStart) < TargetColumns) { |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 169 | bool ExpandedRegion = false; |
| 170 | // Move the start of the interesting region left until we've |
| 171 | // pulled in something else interesting. |
Douglas Gregor | 2fb3ea3 | 2009-05-04 06:45:38 +0000 | [diff] [blame] | 172 | if (CaretStart == 1) |
| 173 | CaretStart = 0; |
| 174 | else if (CaretStart > 1) { |
| 175 | unsigned NewStart = CaretStart - 1; |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 176 | |
Douglas Gregor | 2fb3ea3 | 2009-05-04 06:45:38 +0000 | [diff] [blame] | 177 | // Skip over any whitespace we see here; we're looking for |
| 178 | // another bit of interesting text. |
| 179 | while (NewStart && isspace(SourceLine[NewStart])) |
| 180 | --NewStart; |
| 181 | |
| 182 | // Skip over this bit of "interesting" text. |
| 183 | while (NewStart && !isspace(SourceLine[NewStart])) |
| 184 | --NewStart; |
| 185 | |
| 186 | // Move up to the non-whitespace character we just saw. |
| 187 | if (NewStart) |
| 188 | ++NewStart; |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 189 | |
| 190 | // If we're still within our limit, update the starting |
| 191 | // position within the source/caret line. |
Douglas Gregor | 2fb3ea3 | 2009-05-04 06:45:38 +0000 | [diff] [blame] | 192 | if (CaretEnd - NewStart <= TargetColumns) { |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 193 | CaretStart = NewStart; |
| 194 | ExpandedRegion = true; |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Move the end of the interesting region right until we've |
| 199 | // pulled in something else interesting. |
Daniel Dunbar | 1ef29d2 | 2009-05-03 23:04:40 +0000 | [diff] [blame] | 200 | if (CaretEnd != SourceLength) { |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 201 | unsigned NewEnd = CaretEnd; |
| 202 | |
| 203 | // Skip over any whitespace we see here; we're looking for |
| 204 | // another bit of interesting text. |
| 205 | while (CaretEnd != SourceLength && isspace(SourceLine[NewEnd - 1])) |
| 206 | ++NewEnd; |
| 207 | |
| 208 | // Skip over this bit of "interesting" text. |
| 209 | while (CaretEnd != SourceLength && !isspace(SourceLine[NewEnd - 1])) |
| 210 | ++NewEnd; |
| 211 | |
| 212 | if (NewEnd - CaretStart <= TargetColumns) { |
| 213 | CaretEnd = NewEnd; |
| 214 | ExpandedRegion = true; |
| 215 | } |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 216 | } |
Daniel Dunbar | 1ef29d2 | 2009-05-03 23:04:40 +0000 | [diff] [blame] | 217 | |
| 218 | if (!ExpandedRegion) |
| 219 | break; |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | // [CaretStart, CaretEnd) is the slice we want. Update the various |
| 223 | // output lines to show only this slice, with two-space padding |
| 224 | // before the lines so that it looks nicer. |
Douglas Gregor | 7d101f6 | 2009-05-03 04:12:51 +0000 | [diff] [blame] | 225 | if (CaretEnd < SourceLine.size()) |
| 226 | SourceLine.replace(CaretEnd, std::string::npos, "..."); |
Douglas Gregor | 2167de4 | 2009-05-03 15:24:25 +0000 | [diff] [blame] | 227 | if (CaretEnd < CaretLine.size()) |
| 228 | CaretLine.erase(CaretEnd, std::string::npos); |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 229 | if (FixItInsertionLine.size() > CaretEnd) |
| 230 | FixItInsertionLine.erase(CaretEnd, std::string::npos); |
| 231 | |
| 232 | if (CaretStart > 2) { |
Douglas Gregor | 7d101f6 | 2009-05-03 04:12:51 +0000 | [diff] [blame] | 233 | SourceLine.replace(0, CaretStart, " ..."); |
| 234 | CaretLine.replace(0, CaretStart, " "); |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 235 | if (FixItInsertionLine.size() >= CaretStart) |
Douglas Gregor | 7d101f6 | 2009-05-03 04:12:51 +0000 | [diff] [blame] | 236 | FixItInsertionLine.replace(0, CaretStart, " "); |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 237 | } |
| 238 | } |
| 239 | |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 240 | void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc, |
Chris Lattner | 676f024 | 2009-02-20 00:25:28 +0000 | [diff] [blame] | 241 | SourceRange *Ranges, |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 242 | unsigned NumRanges, |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 243 | SourceManager &SM, |
| 244 | const CodeModificationHint *Hints, |
Douglas Gregor | fffd93f | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 245 | unsigned NumHints, |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 246 | unsigned AvoidColumn, |
| 247 | unsigned Columns) { |
Chris Lattner | 55dcef0 | 2009-02-17 08:44:50 +0000 | [diff] [blame] | 248 | assert(!Loc.isInvalid() && "must have a valid source location here"); |
Chris Lattner | 037fb7f | 2009-05-05 22:03:18 +0000 | [diff] [blame] | 249 | |
| 250 | // If this is a macro ID, first emit information about where this was |
| 251 | // instantiated (recursively) then emit information about where. the token was |
| 252 | // spelled from. |
Chris Lattner | 55dcef0 | 2009-02-17 08:44:50 +0000 | [diff] [blame] | 253 | if (!Loc.isFileID()) { |
Chris Lattner | 609b3ab | 2009-02-18 18:50:45 +0000 | [diff] [blame] | 254 | SourceLocation OneLevelUp = SM.getImmediateInstantiationRange(Loc).first; |
Chris Lattner | 037fb7f | 2009-05-05 22:03:18 +0000 | [diff] [blame] | 255 | // FIXME: Map ranges? |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 256 | EmitCaretDiagnostic(OneLevelUp, Ranges, NumRanges, SM, 0, 0, AvoidColumn, |
| 257 | Columns); |
Chris Lattner | 676f024 | 2009-02-20 00:25:28 +0000 | [diff] [blame] | 258 | |
Chris Lattner | 037fb7f | 2009-05-05 22:03:18 +0000 | [diff] [blame] | 259 | Loc = SM.getImmediateSpellingLoc(Loc); |
| 260 | |
Chris Lattner | 676f024 | 2009-02-20 00:25:28 +0000 | [diff] [blame] | 261 | // Map the ranges. |
| 262 | for (unsigned i = 0; i != NumRanges; ++i) { |
| 263 | SourceLocation S = Ranges[i].getBegin(), E = Ranges[i].getEnd(); |
Chris Lattner | 037fb7f | 2009-05-05 22:03:18 +0000 | [diff] [blame] | 264 | if (S.isMacroID()) S = SM.getImmediateSpellingLoc(S); |
| 265 | if (E.isMacroID()) E = SM.getImmediateSpellingLoc(E); |
Chris Lattner | 676f024 | 2009-02-20 00:25:28 +0000 | [diff] [blame] | 266 | Ranges[i] = SourceRange(S, E); |
| 267 | } |
Chris Lattner | 55dcef0 | 2009-02-17 08:44:50 +0000 | [diff] [blame] | 268 | |
Chris Lattner | 5ce24c8 | 2009-04-21 03:57:54 +0000 | [diff] [blame] | 269 | if (ShowLocation) { |
Chris Lattner | 037fb7f | 2009-05-05 22:03:18 +0000 | [diff] [blame] | 270 | std::pair<FileID, unsigned> IInfo = SM.getDecomposedInstantiationLoc(Loc); |
| 271 | |
Chris Lattner | 5ce24c8 | 2009-04-21 03:57:54 +0000 | [diff] [blame] | 272 | // Emit the file/line/column that this expansion came from. |
Chris Lattner | 037fb7f | 2009-05-05 22:03:18 +0000 | [diff] [blame] | 273 | OS << SM.getBuffer(IInfo.first)->getBufferIdentifier() << ':' |
| 274 | << SM.getLineNumber(IInfo.first, IInfo.second) << ':'; |
Chris Lattner | 5ce24c8 | 2009-04-21 03:57:54 +0000 | [diff] [blame] | 275 | if (ShowColumn) |
Chris Lattner | 037fb7f | 2009-05-05 22:03:18 +0000 | [diff] [blame] | 276 | OS << SM.getColumnNumber(IInfo.first, IInfo.second) << ':'; |
Chris Lattner | 5ce24c8 | 2009-04-21 03:57:54 +0000 | [diff] [blame] | 277 | OS << ' '; |
| 278 | } |
| 279 | OS << "note: instantiated from:\n"; |
Chris Lattner | 037fb7f | 2009-05-05 22:03:18 +0000 | [diff] [blame] | 280 | |
| 281 | EmitCaretDiagnostic(Loc, Ranges, NumRanges, SM, Hints, NumHints, 0,Columns); |
| 282 | return; |
Chris Lattner | 55dcef0 | 2009-02-17 08:44:50 +0000 | [diff] [blame] | 283 | } |
Chris Lattner | b88af81 | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 284 | |
| 285 | // Decompose the location into a FID/Offset pair. |
| 286 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); |
| 287 | FileID FID = LocInfo.first; |
| 288 | unsigned FileOffset = LocInfo.second; |
| 289 | |
| 290 | // Get information about the buffer it points into. |
| 291 | std::pair<const char*, const char*> BufferInfo = SM.getBufferData(FID); |
| 292 | const char *BufStart = BufferInfo.first; |
Chris Lattner | b88af81 | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 293 | |
| 294 | unsigned ColNo = SM.getColumnNumber(FID, FileOffset); |
Douglas Gregor | cfe1f9d | 2009-05-04 06:27:32 +0000 | [diff] [blame] | 295 | unsigned CaretEndColNo |
| 296 | = ColNo + Lexer::MeasureTokenLength(Loc, SM, *LangOpts); |
| 297 | |
Chris Lattner | 94f5578 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 298 | // Rewind from the current position to the start of the line. |
Chris Lattner | b88af81 | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 299 | const char *TokPtr = BufStart+FileOffset; |
| 300 | const char *LineStart = TokPtr-ColNo+1; // Column # is 1-based. |
| 301 | |
Chris Lattner | 94f5578 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 302 | |
| 303 | // Compute the line end. Scan forward from the error position to the end of |
| 304 | // the line. |
Chris Lattner | b88af81 | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 305 | const char *LineEnd = TokPtr; |
Chris Lattner | cd1148b | 2009-03-08 08:11:22 +0000 | [diff] [blame] | 306 | while (*LineEnd != '\n' && *LineEnd != '\r' && *LineEnd != '\0') |
Chris Lattner | 94f5578 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 307 | ++LineEnd; |
| 308 | |
| 309 | // Copy the line of code into an std::string for ease of manipulation. |
| 310 | std::string SourceLine(LineStart, LineEnd); |
| 311 | |
| 312 | // Create a line for the caret that is filled with spaces that is the same |
| 313 | // length as the line of source code. |
| 314 | std::string CaretLine(LineEnd-LineStart, ' '); |
| 315 | |
| 316 | // Highlight all of the characters covered by Ranges with ~ characters. |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 317 | if (NumRanges) { |
Chris Lattner | b88af81 | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 318 | unsigned LineNo = SM.getLineNumber(FID, FileOffset); |
| 319 | |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 320 | for (unsigned i = 0, e = NumRanges; i != e; ++i) |
| 321 | HighlightRange(Ranges[i], SM, LineNo, FID, CaretLine, SourceLine); |
Chris Lattner | b88af81 | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 322 | } |
Chris Lattner | 94f5578 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 323 | |
| 324 | // Next, insert the caret itself. |
| 325 | if (ColNo-1 < CaretLine.size()) |
| 326 | CaretLine[ColNo-1] = '^'; |
| 327 | else |
| 328 | CaretLine.push_back('^'); |
| 329 | |
| 330 | // Scan the source line, looking for tabs. If we find any, manually expand |
| 331 | // them to 8 characters and update the CaretLine to match. |
| 332 | for (unsigned i = 0; i != SourceLine.size(); ++i) { |
| 333 | if (SourceLine[i] != '\t') continue; |
| 334 | |
| 335 | // Replace this tab with at least one space. |
| 336 | SourceLine[i] = ' '; |
| 337 | |
| 338 | // Compute the number of spaces we need to insert. |
| 339 | unsigned NumSpaces = ((i+8)&~7) - (i+1); |
| 340 | assert(NumSpaces < 8 && "Invalid computation of space amt"); |
| 341 | |
| 342 | // Insert spaces into the SourceLine. |
| 343 | SourceLine.insert(i+1, NumSpaces, ' '); |
| 344 | |
| 345 | // Insert spaces or ~'s into CaretLine. |
| 346 | CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' '); |
| 347 | } |
| 348 | |
Chris Lattner | 770dbf0 | 2009-04-28 22:33:16 +0000 | [diff] [blame] | 349 | // If we are in -fdiagnostics-print-source-range-info mode, we are trying to |
| 350 | // produce easily machine parsable output. Add a space before the source line |
| 351 | // and the caret to make it trivial to tell the main diagnostic line from what |
| 352 | // the user is intended to see. |
| 353 | if (PrintRangeInfo) { |
| 354 | SourceLine = ' ' + SourceLine; |
| 355 | CaretLine = ' ' + CaretLine; |
| 356 | } |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 357 | |
| 358 | std::string FixItInsertionLine; |
Chris Lattner | aa5bf2e | 2009-04-19 07:44:08 +0000 | [diff] [blame] | 359 | if (NumHints && PrintFixItInfo) { |
Chris Lattner | aa5bf2e | 2009-04-19 07:44:08 +0000 | [diff] [blame] | 360 | for (const CodeModificationHint *Hint = Hints, *LastHint = Hints + NumHints; |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 361 | Hint != LastHint; ++Hint) { |
| 362 | if (Hint->InsertionLoc.isValid()) { |
| 363 | // We have an insertion hint. Determine whether the inserted |
| 364 | // code is on the same line as the caret. |
| 365 | std::pair<FileID, unsigned> HintLocInfo |
Chris Lattner | 7b5b5b4 | 2009-03-02 20:58:48 +0000 | [diff] [blame] | 366 | = SM.getDecomposedInstantiationLoc(Hint->InsertionLoc); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 367 | if (SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) == |
| 368 | SM.getLineNumber(FID, FileOffset)) { |
| 369 | // Insert the new code into the line just below the code |
| 370 | // that the user wrote. |
| 371 | unsigned HintColNo |
| 372 | = SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second); |
| 373 | unsigned LastColumnModified |
| 374 | = HintColNo - 1 + Hint->CodeToInsert.size(); |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 375 | if (LastColumnModified > FixItInsertionLine.size()) |
| 376 | FixItInsertionLine.resize(LastColumnModified, ' '); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 377 | std::copy(Hint->CodeToInsert.begin(), Hint->CodeToInsert.end(), |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 378 | FixItInsertionLine.begin() + HintColNo - 1); |
Douglas Gregor | 844da34 | 2009-05-03 04:33:32 +0000 | [diff] [blame] | 379 | } else { |
| 380 | FixItInsertionLine.clear(); |
| 381 | break; |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 382 | } |
| 383 | } |
| 384 | } |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 385 | } |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 386 | |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 387 | // If the source line is too long for our terminal, select only the |
| 388 | // "interesting" source region within that line. |
| 389 | if (Columns && SourceLine.size() > Columns) |
| 390 | SelectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine, |
Douglas Gregor | cfe1f9d | 2009-05-04 06:27:32 +0000 | [diff] [blame] | 391 | CaretEndColNo, Columns); |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 392 | |
| 393 | // AvoidColumn tells us which column we should avoid when printing |
| 394 | // the source line. If the source line would start at or near that |
| 395 | // column, add another line of whitespace before printing the source |
| 396 | // line. Otherwise, the source line and the diagnostic text can get |
| 397 | // jumbled together. |
| 398 | unsigned StartCol = 0; |
| 399 | for (unsigned N = SourceLine.size(); StartCol != N; ++StartCol) |
| 400 | if (!isspace(SourceLine[StartCol])) |
| 401 | break; |
| 402 | |
| 403 | if (StartCol != SourceLine.size() && |
| 404 | abs((int)StartCol - (int)AvoidColumn) <= 2) |
| 405 | OS << '\n'; |
| 406 | |
| 407 | // Finally, remove any blank spaces from the end of CaretLine. |
| 408 | while (CaretLine[CaretLine.size()-1] == ' ') |
| 409 | CaretLine.erase(CaretLine.end()-1); |
| 410 | |
| 411 | // Emit what we have computed. |
| 412 | OS << SourceLine << '\n'; |
| 413 | OS << CaretLine << '\n'; |
| 414 | |
| 415 | if (!FixItInsertionLine.empty()) { |
| 416 | if (PrintRangeInfo) |
| 417 | OS << ' '; |
| 418 | OS << FixItInsertionLine << '\n'; |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 419 | } |
Chris Lattner | 94f5578 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 420 | } |
| 421 | |
Douglas Gregor | fffd93f | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 422 | /// \brief Skip over whitespace in the string, starting at the given |
| 423 | /// index. |
| 424 | /// |
| 425 | /// \returns The index of the first non-whitespace character that is |
| 426 | /// greater than or equal to Idx or, if no such character exists, |
| 427 | /// returns the end of the string. |
| 428 | static unsigned skipWhitespace(unsigned Idx, |
| 429 | const llvm::SmallVectorImpl<char> &Str, |
| 430 | unsigned Length) { |
| 431 | while (Idx < Length && isspace(Str[Idx])) |
| 432 | ++Idx; |
| 433 | return Idx; |
| 434 | } |
| 435 | |
| 436 | /// \brief If the given character is the start of some kind of |
| 437 | /// balanced punctuation (e.g., quotes or parentheses), return the |
| 438 | /// character that will terminate the punctuation. |
| 439 | /// |
| 440 | /// \returns The ending punctuation character, if any, or the NULL |
| 441 | /// character if the input character does not start any punctuation. |
| 442 | static inline char findMatchingPunctuation(char c) { |
| 443 | switch (c) { |
| 444 | case '\'': return '\''; |
| 445 | case '`': return '\''; |
| 446 | case '"': return '"'; |
| 447 | case '(': return ')'; |
| 448 | case '[': return ']'; |
| 449 | case '{': return '}'; |
| 450 | default: break; |
| 451 | } |
| 452 | |
| 453 | return 0; |
| 454 | } |
| 455 | |
| 456 | /// \brief Find the end of the word starting at the given offset |
| 457 | /// within a string. |
| 458 | /// |
| 459 | /// \returns the index pointing one character past the end of the |
| 460 | /// word. |
| 461 | unsigned findEndOfWord(unsigned Start, |
| 462 | const llvm::SmallVectorImpl<char> &Str, |
| 463 | unsigned Length, unsigned Column, |
| 464 | unsigned Columns) { |
| 465 | unsigned End = Start + 1; |
| 466 | |
| 467 | // Determine if the start of the string is actually opening |
| 468 | // punctuation, e.g., a quote or parentheses. |
| 469 | char EndPunct = findMatchingPunctuation(Str[Start]); |
| 470 | if (!EndPunct) { |
| 471 | // This is a normal word. Just find the first space character. |
| 472 | while (End < Length && !isspace(Str[End])) |
| 473 | ++End; |
| 474 | return End; |
| 475 | } |
| 476 | |
| 477 | // We have the start of a balanced punctuation sequence (quotes, |
| 478 | // parentheses, etc.). Determine the full sequence is. |
| 479 | llvm::SmallVector<char, 16> PunctuationEndStack; |
| 480 | PunctuationEndStack.push_back(EndPunct); |
| 481 | while (End < Length && !PunctuationEndStack.empty()) { |
| 482 | if (Str[End] == PunctuationEndStack.back()) |
| 483 | PunctuationEndStack.pop_back(); |
| 484 | else if (char SubEndPunct = findMatchingPunctuation(Str[End])) |
| 485 | PunctuationEndStack.push_back(SubEndPunct); |
| 486 | |
| 487 | ++End; |
| 488 | } |
| 489 | |
| 490 | // Find the first space character after the punctuation ended. |
| 491 | while (End < Length && !isspace(Str[End])) |
| 492 | ++End; |
| 493 | |
| 494 | unsigned PunctWordLength = End - Start; |
| 495 | if (// If the word fits on this line |
| 496 | Column + PunctWordLength <= Columns || |
| 497 | // ... or the word is "short enough" to take up the next line |
| 498 | // without too much ugly white space |
| 499 | PunctWordLength < Columns/3) |
| 500 | return End; // Take the whole thing as a single "word". |
| 501 | |
| 502 | // The whole quoted/parenthesized string is too long to print as a |
| 503 | // single "word". Instead, find the "word" that starts just after |
| 504 | // the punctuation and use that end-point instead. This will recurse |
| 505 | // until it finds something small enough to consider a word. |
| 506 | return findEndOfWord(Start + 1, Str, Length, Column + 1, Columns); |
| 507 | } |
| 508 | |
| 509 | /// \brief Print the given string to a stream, word-wrapping it to |
| 510 | /// some number of columns in the process. |
| 511 | /// |
| 512 | /// \brief OS the stream to which the word-wrapping string will be |
| 513 | /// emitted. |
| 514 | /// |
| 515 | /// \brief Str the string to word-wrap and output. |
| 516 | /// |
| 517 | /// \brief Columns the number of columns to word-wrap to. |
| 518 | /// |
| 519 | /// \brief Column the column number at which the first character of \p |
| 520 | /// Str will be printed. This will be non-zero when part of the first |
| 521 | /// line has already been printed. |
| 522 | /// |
| 523 | /// \brief Indentation the number of spaces to indent any lines beyond |
| 524 | /// the first line. |
| 525 | /// |
| 526 | /// \returns true if word-wrapping was required, or false if the |
| 527 | /// string fit on the first line. |
| 528 | static bool PrintWordWrapped(llvm::raw_ostream &OS, |
| 529 | const llvm::SmallVectorImpl<char> &Str, |
| 530 | unsigned Columns, |
| 531 | unsigned Column = 0, |
| 532 | unsigned Indentation = WordWrapIndentation) { |
| 533 | unsigned Length = Str.size(); |
| 534 | |
| 535 | // If there is a newline in this message somewhere, find that |
| 536 | // newline and split the message into the part before the newline |
| 537 | // (which will be word-wrapped) and the part from the newline one |
| 538 | // (which will be emitted unchanged). |
| 539 | for (unsigned I = 0; I != Length; ++I) |
| 540 | if (Str[I] == '\n') { |
| 541 | Length = I; |
| 542 | break; |
| 543 | } |
| 544 | |
| 545 | // The string used to indent each line. |
| 546 | llvm::SmallString<16> IndentStr; |
| 547 | IndentStr.assign(Indentation, ' '); |
| 548 | bool Wrapped = false; |
| 549 | for (unsigned WordStart = 0, WordEnd; WordStart < Length; |
| 550 | WordStart = WordEnd) { |
| 551 | // Find the beginning of the next word. |
| 552 | WordStart = skipWhitespace(WordStart, Str, Length); |
| 553 | if (WordStart == Length) |
| 554 | break; |
| 555 | |
| 556 | // Find the end of this word. |
| 557 | WordEnd = findEndOfWord(WordStart, Str, Length, Column, Columns); |
| 558 | |
| 559 | // Does this word fit on the current line? |
| 560 | unsigned WordLength = WordEnd - WordStart; |
| 561 | if (Column + WordLength < Columns) { |
| 562 | // This word fits on the current line; print it there. |
| 563 | if (WordStart) { |
| 564 | OS << ' '; |
| 565 | Column += 1; |
| 566 | } |
| 567 | OS.write(&Str[WordStart], WordLength); |
| 568 | Column += WordLength; |
| 569 | continue; |
| 570 | } |
| 571 | |
| 572 | // This word does not fit on the current line, so wrap to the next |
| 573 | // line. |
Douglas Gregor | 44cf08e | 2009-05-03 03:52:38 +0000 | [diff] [blame] | 574 | OS << '\n'; |
| 575 | OS.write(&IndentStr[0], Indentation); |
Douglas Gregor | fffd93f | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 576 | OS.write(&Str[WordStart], WordLength); |
| 577 | Column = Indentation + WordLength; |
| 578 | Wrapped = true; |
| 579 | } |
| 580 | |
| 581 | if (Length == Str.size()) |
| 582 | return Wrapped; // We're done. |
| 583 | |
| 584 | // There is a newline in the message, followed by something that |
| 585 | // will not be word-wrapped. Print that. |
| 586 | OS.write(&Str[Length], Str.size() - Length); |
| 587 | return true; |
| 588 | } |
Chris Lattner | 94f5578 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 589 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 590 | void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level, |
| 591 | const DiagnosticInfo &Info) { |
Douglas Gregor | fffd93f | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 592 | // Keeps track of the the starting position of the location |
| 593 | // information (e.g., "foo.c:10:4:") that precedes the error |
| 594 | // message. We use this information to determine how long the |
| 595 | // file+line+column number prefix is. |
| 596 | uint64_t StartOfLocationInfo = OS.tell(); |
| 597 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 598 | // If the location is specified, print out a file/line/col and include trace |
| 599 | // if enabled. |
| 600 | if (Info.getLocation().isValid()) { |
Ted Kremenek | 05f3957 | 2009-01-28 20:47:47 +0000 | [diff] [blame] | 601 | const SourceManager &SM = Info.getLocation().getManager(); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 602 | PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation()); |
| 603 | unsigned LineNo = PLoc.getLine(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 604 | |
| 605 | // First, if this diagnostic is not in the main file, print out the |
| 606 | // "included from" lines. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 607 | if (LastWarningLoc != PLoc.getIncludeLoc()) { |
| 608 | LastWarningLoc = PLoc.getIncludeLoc(); |
| 609 | PrintIncludeStack(LastWarningLoc, SM); |
Douglas Gregor | fffd93f | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 610 | StartOfLocationInfo = OS.tell(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 611 | } |
Douglas Gregor | fffd93f | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 612 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 613 | // Compute the column number. |
Chris Lattner | b8bf65e | 2009-01-30 17:41:53 +0000 | [diff] [blame] | 614 | if (ShowLocation) { |
| 615 | OS << PLoc.getFilename() << ':' << LineNo << ':'; |
Chris Lattner | 8f7b396 | 2009-02-17 07:34:34 +0000 | [diff] [blame] | 616 | if (ShowColumn) |
| 617 | if (unsigned ColNo = PLoc.getColumn()) |
| 618 | OS << ColNo << ':'; |
Chris Lattner | 1fbee5d | 2009-03-13 01:08:23 +0000 | [diff] [blame] | 619 | |
| 620 | if (PrintRangeInfo && Info.getNumRanges()) { |
| 621 | FileID CaretFileID = |
| 622 | SM.getFileID(SM.getInstantiationLoc(Info.getLocation())); |
| 623 | bool PrintedRange = false; |
| 624 | |
| 625 | for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i) { |
Chris Lattner | 74548e6 | 2009-04-19 22:24:10 +0000 | [diff] [blame] | 626 | // Ignore invalid ranges. |
| 627 | if (!Info.getRange(i).isValid()) continue; |
| 628 | |
Chris Lattner | 1fbee5d | 2009-03-13 01:08:23 +0000 | [diff] [blame] | 629 | SourceLocation B = Info.getRange(i).getBegin(); |
| 630 | SourceLocation E = Info.getRange(i).getEnd(); |
| 631 | std::pair<FileID, unsigned> BInfo=SM.getDecomposedInstantiationLoc(B); |
| 632 | |
| 633 | E = SM.getInstantiationLoc(E); |
| 634 | std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E); |
| 635 | |
| 636 | // If the start or end of the range is in another file, just discard |
| 637 | // it. |
| 638 | if (BInfo.first != CaretFileID || EInfo.first != CaretFileID) |
| 639 | continue; |
| 640 | |
| 641 | // Add in the length of the token, so that we cover multi-char tokens. |
Chris Lattner | 2c78b87 | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 642 | unsigned TokSize = Lexer::MeasureTokenLength(E, SM, *LangOpts); |
Chris Lattner | 1fbee5d | 2009-03-13 01:08:23 +0000 | [diff] [blame] | 643 | |
| 644 | OS << '{' << SM.getLineNumber(BInfo.first, BInfo.second) << ':' |
| 645 | << SM.getColumnNumber(BInfo.first, BInfo.second) << '-' |
| 646 | << SM.getLineNumber(EInfo.first, EInfo.second) << ':' |
| 647 | << (SM.getColumnNumber(EInfo.first, EInfo.second)+TokSize) << '}'; |
| 648 | PrintedRange = true; |
| 649 | } |
| 650 | |
| 651 | if (PrintedRange) |
| 652 | OS << ':'; |
| 653 | } |
Chris Lattner | b8bf65e | 2009-01-30 17:41:53 +0000 | [diff] [blame] | 654 | OS << ' '; |
| 655 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | switch (Level) { |
Chris Lattner | 4132758 | 2009-02-06 03:57:44 +0000 | [diff] [blame] | 659 | case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type"); |
Nate Begeman | 165b954 | 2008-04-17 18:06:57 +0000 | [diff] [blame] | 660 | case Diagnostic::Note: OS << "note: "; break; |
| 661 | case Diagnostic::Warning: OS << "warning: "; break; |
| 662 | case Diagnostic::Error: OS << "error: "; break; |
Chris Lattner | 4132758 | 2009-02-06 03:57:44 +0000 | [diff] [blame] | 663 | case Diagnostic::Fatal: OS << "fatal error: "; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 664 | } |
| 665 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 666 | llvm::SmallString<100> OutStr; |
| 667 | Info.FormatDiagnostic(OutStr); |
Chris Lattner | d51d74a | 2009-04-16 05:44:38 +0000 | [diff] [blame] | 668 | |
| 669 | if (PrintDiagnosticOption) |
Douglas Gregor | fffd93f | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 670 | if (const char *Opt = Diagnostic::getWarningOptionForDiag(Info.getID())) { |
| 671 | OutStr += " [-W"; |
| 672 | OutStr += Opt; |
| 673 | OutStr += ']'; |
| 674 | } |
Chris Lattner | d51d74a | 2009-04-16 05:44:38 +0000 | [diff] [blame] | 675 | |
Douglas Gregor | fffd93f | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 676 | bool WordWrapped = false; |
| 677 | if (MessageLength) { |
| 678 | // We will be word-wrapping the error message, so compute the |
| 679 | // column number where we currently are (after printing the |
| 680 | // location information). |
| 681 | unsigned Column = OS.tell() - StartOfLocationInfo; |
| 682 | WordWrapped = PrintWordWrapped(OS, OutStr, MessageLength, Column); |
| 683 | } else { |
| 684 | OS.write(OutStr.begin(), OutStr.size()); |
| 685 | } |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 686 | OS << '\n'; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 687 | |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 688 | // If caret diagnostics are enabled and we have location, we want to |
| 689 | // emit the caret. However, we only do this if the location moved |
| 690 | // from the last diagnostic, if the last diagnostic was a note that |
| 691 | // was part of a different warning or error diagnostic, or if the |
| 692 | // diagnostic has ranges. We don't want to emit the same caret |
| 693 | // multiple times if one loc has multiple diagnostics. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 694 | if (CaretDiagnostics && Info.getLocation().isValid() && |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 695 | ((LastLoc != Info.getLocation()) || Info.getNumRanges() || |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 696 | (LastCaretDiagnosticWasNote && Level != Diagnostic::Note) || |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 697 | Info.getNumCodeModificationHints())) { |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 698 | // Cache the LastLoc, it allows us to omit duplicate source/caret spewage. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 699 | LastLoc = Info.getLocation(); |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 700 | LastCaretDiagnosticWasNote = (Level == Diagnostic::Note); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 701 | |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 702 | // Get the ranges into a local array we can hack on. |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 703 | SourceRange Ranges[20]; |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 704 | unsigned NumRanges = Info.getNumRanges(); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 705 | assert(NumRanges < 20 && "Out of space"); |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 706 | for (unsigned i = 0; i != NumRanges; ++i) |
| 707 | Ranges[i] = Info.getRange(i); |
| 708 | |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 709 | unsigned NumHints = Info.getNumCodeModificationHints(); |
| 710 | for (unsigned idx = 0; idx < NumHints; ++idx) { |
| 711 | const CodeModificationHint &Hint = Info.getCodeModificationHint(idx); |
| 712 | if (Hint.RemoveRange.isValid()) { |
| 713 | assert(NumRanges < 20 && "Out of space"); |
| 714 | Ranges[NumRanges++] = Hint.RemoveRange; |
| 715 | } |
| 716 | } |
| 717 | |
| 718 | EmitCaretDiagnostic(LastLoc, Ranges, NumRanges, LastLoc.getManager(), |
| 719 | Info.getCodeModificationHints(), |
Douglas Gregor | fffd93f | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 720 | Info.getNumCodeModificationHints(), |
Douglas Gregor | 47f7177 | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 721 | WordWrapped? WordWrapIndentation : 0, |
| 722 | MessageLength); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 723 | } |
Chris Lattner | a03a5b5 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 724 | |
| 725 | OS.flush(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 726 | } |