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