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 | a03a5b5 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 17 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 18 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 19 | #include <algorithm> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 20 | using namespace clang; |
| 21 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 22 | void TextDiagnosticPrinter:: |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 23 | PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) { |
| 24 | if (Loc.isInvalid()) return; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 25 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 26 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 27 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | // Print out the other include frames first. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 29 | PrintIncludeStack(PLoc.getIncludeLoc(), SM); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 31 | OS << "In file included from " << PLoc.getFilename() |
| 32 | << ':' << PLoc.getLine() << ":\n"; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | /// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s) |
| 36 | /// any characters in LineNo that intersect the SourceRange. |
Ted Kremenek | 7a9d49f | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 37 | void TextDiagnosticPrinter::HighlightRange(const SourceRange &R, |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 38 | const SourceManager &SM, |
Chris Lattner | 3b4d5e9 | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 39 | unsigned LineNo, FileID FID, |
Gordon Henriksen | aad6953 | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 40 | std::string &CaretLine, |
Nuno Lopes | db82568 | 2008-08-05 19:40:20 +0000 | [diff] [blame] | 41 | const std::string &SourceLine) { |
Gordon Henriksen | aad6953 | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 42 | assert(CaretLine.size() == SourceLine.size() && |
| 43 | "Expect a correspondence between source and caret line!"); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 44 | if (!R.isValid()) return; |
| 45 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 46 | SourceLocation Begin = SM.getInstantiationLoc(R.getBegin()); |
| 47 | SourceLocation End = SM.getInstantiationLoc(R.getEnd()); |
| 48 | |
Chris Lattner | 34837a5 | 2009-02-17 05:19:10 +0000 | [diff] [blame] | 49 | // If the End location and the start location are the same and are a macro |
| 50 | // location, then the range was something that came from a macro expansion |
| 51 | // or _Pragma. If this is an object-like macro, the best we can do is to |
| 52 | // highlight the range. If this is a function-like macro, we'd also like to |
| 53 | // highlight the arguments. |
| 54 | if (Begin == End && R.getEnd().isMacroID()) |
| 55 | End = SM.getInstantiationRange(R.getEnd()).second; |
| 56 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 57 | unsigned StartLineNo = SM.getInstantiationLineNumber(Begin); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 58 | if (StartLineNo > LineNo || SM.getFileID(Begin) != FID) |
Chris Lattner | e41b7cd | 2008-01-12 06:43:35 +0000 | [diff] [blame] | 59 | return; // No intersection. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 60 | |
Chris Lattner | 30fc933 | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 61 | unsigned EndLineNo = SM.getInstantiationLineNumber(End); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 62 | if (EndLineNo < LineNo || SM.getFileID(End) != FID) |
Chris Lattner | e41b7cd | 2008-01-12 06:43:35 +0000 | [diff] [blame] | 63 | return; // No intersection. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 64 | |
| 65 | // Compute the column number of the start. |
| 66 | unsigned StartColNo = 0; |
| 67 | if (StartLineNo == LineNo) { |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 68 | StartColNo = SM.getInstantiationColumnNumber(Begin); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 69 | if (StartColNo) --StartColNo; // Zero base the col #. |
| 70 | } |
| 71 | |
| 72 | // Pick the first non-whitespace column. |
| 73 | while (StartColNo < SourceLine.size() && |
| 74 | (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t')) |
| 75 | ++StartColNo; |
| 76 | |
| 77 | // Compute the column number of the end. |
Gordon Henriksen | aad6953 | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 78 | unsigned EndColNo = CaretLine.size(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 79 | if (EndLineNo == LineNo) { |
Chris Lattner | 7da5aea | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 80 | EndColNo = SM.getInstantiationColumnNumber(End); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | if (EndColNo) { |
| 82 | --EndColNo; // Zero base the col #. |
| 83 | |
| 84 | // Add in the length of the token, so that we cover multi-char tokens. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 85 | EndColNo += Lexer::MeasureTokenLength(End, SM); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 86 | } else { |
Gordon Henriksen | aad6953 | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 87 | EndColNo = CaretLine.size(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
| 91 | // Pick the last non-whitespace column. |
Nuno Lopes | db82568 | 2008-08-05 19:40:20 +0000 | [diff] [blame] | 92 | if (EndColNo <= SourceLine.size()) |
| 93 | while (EndColNo-1 && |
| 94 | (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t')) |
| 95 | --EndColNo; |
| 96 | else |
| 97 | EndColNo = SourceLine.size(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 98 | |
| 99 | // Fill the range with ~'s. |
| 100 | assert(StartColNo <= EndColNo && "Invalid range!"); |
Nuno Lopes | db82568 | 2008-08-05 19:40:20 +0000 | [diff] [blame] | 101 | for (unsigned i = StartColNo; i < EndColNo; ++i) |
Gordon Henriksen | aad6953 | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 102 | CaretLine[i] = '~'; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 103 | } |
| 104 | |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 105 | void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc, |
Chris Lattner | 676f024 | 2009-02-20 00:25:28 +0000 | [diff] [blame] | 106 | SourceRange *Ranges, |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 107 | unsigned NumRanges, |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 108 | SourceManager &SM, |
| 109 | const CodeModificationHint *Hints, |
| 110 | unsigned NumHints) { |
Chris Lattner | 55dcef0 | 2009-02-17 08:44:50 +0000 | [diff] [blame] | 111 | assert(!Loc.isInvalid() && "must have a valid source location here"); |
| 112 | |
Chris Lattner | 49a48eb | 2009-02-17 07:54:55 +0000 | [diff] [blame] | 113 | // We always emit diagnostics about the instantiation points, not the spelling |
| 114 | // points. This more closely correlates to what the user writes. |
Chris Lattner | 55dcef0 | 2009-02-17 08:44:50 +0000 | [diff] [blame] | 115 | if (!Loc.isFileID()) { |
Chris Lattner | 609b3ab | 2009-02-18 18:50:45 +0000 | [diff] [blame] | 116 | SourceLocation OneLevelUp = SM.getImmediateInstantiationRange(Loc).first; |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 117 | EmitCaretDiagnostic(OneLevelUp, Ranges, NumRanges, SM); |
Chris Lattner | 55dcef0 | 2009-02-17 08:44:50 +0000 | [diff] [blame] | 118 | |
Chris Lattner | 676f024 | 2009-02-20 00:25:28 +0000 | [diff] [blame] | 119 | // Map the location through the macro. |
Chris Lattner | 55dcef0 | 2009-02-17 08:44:50 +0000 | [diff] [blame] | 120 | Loc = SM.getInstantiationLoc(SM.getImmediateSpellingLoc(Loc)); |
Chris Lattner | 676f024 | 2009-02-20 00:25:28 +0000 | [diff] [blame] | 121 | |
| 122 | // Map the ranges. |
| 123 | for (unsigned i = 0; i != NumRanges; ++i) { |
| 124 | SourceLocation S = Ranges[i].getBegin(), E = Ranges[i].getEnd(); |
| 125 | if (S.isMacroID()) |
| 126 | S = SM.getInstantiationLoc(SM.getImmediateSpellingLoc(S)); |
| 127 | if (E.isMacroID()) |
| 128 | E = SM.getInstantiationLoc(SM.getImmediateSpellingLoc(E)); |
| 129 | Ranges[i] = SourceRange(S, E); |
| 130 | } |
Chris Lattner | 55dcef0 | 2009-02-17 08:44:50 +0000 | [diff] [blame] | 131 | |
| 132 | // Emit the file/line/column that this expansion came from. |
| 133 | OS << SM.getBufferName(Loc) << ':' << SM.getInstantiationLineNumber(Loc) |
| 134 | << ':'; |
| 135 | if (ShowColumn) |
| 136 | OS << SM.getInstantiationColumnNumber(Loc) << ':'; |
| 137 | OS << " note: instantiated from:\n"; |
| 138 | } |
Chris Lattner | b88af81 | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 139 | |
| 140 | // Decompose the location into a FID/Offset pair. |
| 141 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); |
| 142 | FileID FID = LocInfo.first; |
| 143 | unsigned FileOffset = LocInfo.second; |
| 144 | |
| 145 | // Get information about the buffer it points into. |
| 146 | std::pair<const char*, const char*> BufferInfo = SM.getBufferData(FID); |
| 147 | const char *BufStart = BufferInfo.first; |
Chris Lattner | b88af81 | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 148 | |
| 149 | unsigned ColNo = SM.getColumnNumber(FID, FileOffset); |
Chris Lattner | 94f5578 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 150 | |
| 151 | // Rewind from the current position to the start of the line. |
Chris Lattner | b88af81 | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 152 | const char *TokPtr = BufStart+FileOffset; |
| 153 | const char *LineStart = TokPtr-ColNo+1; // Column # is 1-based. |
| 154 | |
Chris Lattner | 94f5578 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 155 | |
| 156 | // Compute the line end. Scan forward from the error position to the end of |
| 157 | // the line. |
Chris Lattner | b88af81 | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 158 | const char *LineEnd = TokPtr; |
Chris Lattner | cd1148b | 2009-03-08 08:11:22 +0000 | [diff] [blame] | 159 | while (*LineEnd != '\n' && *LineEnd != '\r' && *LineEnd != '\0') |
Chris Lattner | 94f5578 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 160 | ++LineEnd; |
| 161 | |
| 162 | // Copy the line of code into an std::string for ease of manipulation. |
| 163 | std::string SourceLine(LineStart, LineEnd); |
| 164 | |
| 165 | // Create a line for the caret that is filled with spaces that is the same |
| 166 | // length as the line of source code. |
| 167 | std::string CaretLine(LineEnd-LineStart, ' '); |
| 168 | |
| 169 | // Highlight all of the characters covered by Ranges with ~ characters. |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 170 | if (NumRanges) { |
Chris Lattner | b88af81 | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 171 | unsigned LineNo = SM.getLineNumber(FID, FileOffset); |
| 172 | |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 173 | for (unsigned i = 0, e = NumRanges; i != e; ++i) |
| 174 | HighlightRange(Ranges[i], SM, LineNo, FID, CaretLine, SourceLine); |
Chris Lattner | b88af81 | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 175 | } |
Chris Lattner | 94f5578 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 176 | |
| 177 | // Next, insert the caret itself. |
| 178 | if (ColNo-1 < CaretLine.size()) |
| 179 | CaretLine[ColNo-1] = '^'; |
| 180 | else |
| 181 | CaretLine.push_back('^'); |
| 182 | |
| 183 | // Scan the source line, looking for tabs. If we find any, manually expand |
| 184 | // them to 8 characters and update the CaretLine to match. |
| 185 | for (unsigned i = 0; i != SourceLine.size(); ++i) { |
| 186 | if (SourceLine[i] != '\t') continue; |
| 187 | |
| 188 | // Replace this tab with at least one space. |
| 189 | SourceLine[i] = ' '; |
| 190 | |
| 191 | // Compute the number of spaces we need to insert. |
| 192 | unsigned NumSpaces = ((i+8)&~7) - (i+1); |
| 193 | assert(NumSpaces < 8 && "Invalid computation of space amt"); |
| 194 | |
| 195 | // Insert spaces into the SourceLine. |
| 196 | SourceLine.insert(i+1, NumSpaces, ' '); |
| 197 | |
| 198 | // Insert spaces or ~'s into CaretLine. |
| 199 | CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' '); |
| 200 | } |
| 201 | |
| 202 | // Finally, remove any blank spaces from the end of CaretLine. |
| 203 | while (CaretLine[CaretLine.size()-1] == ' ') |
| 204 | CaretLine.erase(CaretLine.end()-1); |
| 205 | |
| 206 | // Emit what we have computed. |
| 207 | OS << SourceLine << '\n'; |
| 208 | OS << CaretLine << '\n'; |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 209 | |
| 210 | if (NumHints) { |
| 211 | std::string InsertionLine; |
| 212 | for (const CodeModificationHint *Hint = Hints, |
| 213 | *LastHint = Hints + NumHints; |
| 214 | Hint != LastHint; ++Hint) { |
| 215 | if (Hint->InsertionLoc.isValid()) { |
| 216 | // We have an insertion hint. Determine whether the inserted |
| 217 | // code is on the same line as the caret. |
| 218 | std::pair<FileID, unsigned> HintLocInfo |
Chris Lattner | 7b5b5b4 | 2009-03-02 20:58:48 +0000 | [diff] [blame] | 219 | = SM.getDecomposedInstantiationLoc(Hint->InsertionLoc); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 220 | if (SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) == |
| 221 | SM.getLineNumber(FID, FileOffset)) { |
| 222 | // Insert the new code into the line just below the code |
| 223 | // that the user wrote. |
| 224 | unsigned HintColNo |
| 225 | = SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second); |
| 226 | unsigned LastColumnModified |
| 227 | = HintColNo - 1 + Hint->CodeToInsert.size(); |
| 228 | if (LastColumnModified > InsertionLine.size()) |
| 229 | InsertionLine.resize(LastColumnModified, ' '); |
| 230 | std::copy(Hint->CodeToInsert.begin(), Hint->CodeToInsert.end(), |
| 231 | InsertionLine.begin() + HintColNo - 1); |
| 232 | } |
| 233 | } |
| 234 | } |
| 235 | |
| 236 | if (!InsertionLine.empty()) |
| 237 | OS << InsertionLine << '\n'; |
| 238 | } |
Chris Lattner | 94f5578 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | |
Chris Lattner | 0a14eee | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 242 | void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level, |
| 243 | const DiagnosticInfo &Info) { |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 244 | // If the location is specified, print out a file/line/col and include trace |
| 245 | // if enabled. |
| 246 | if (Info.getLocation().isValid()) { |
Ted Kremenek | 05f3957 | 2009-01-28 20:47:47 +0000 | [diff] [blame] | 247 | const SourceManager &SM = Info.getLocation().getManager(); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 248 | PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation()); |
| 249 | unsigned LineNo = PLoc.getLine(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 250 | |
| 251 | // First, if this diagnostic is not in the main file, print out the |
| 252 | // "included from" lines. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 253 | if (LastWarningLoc != PLoc.getIncludeLoc()) { |
| 254 | LastWarningLoc = PLoc.getIncludeLoc(); |
| 255 | PrintIncludeStack(LastWarningLoc, SM); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 256 | } |
| 257 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 258 | // Compute the column number. |
Chris Lattner | b8bf65e | 2009-01-30 17:41:53 +0000 | [diff] [blame] | 259 | if (ShowLocation) { |
| 260 | OS << PLoc.getFilename() << ':' << LineNo << ':'; |
Chris Lattner | 8f7b396 | 2009-02-17 07:34:34 +0000 | [diff] [blame] | 261 | if (ShowColumn) |
| 262 | if (unsigned ColNo = PLoc.getColumn()) |
| 263 | OS << ColNo << ':'; |
Chris Lattner | b8bf65e | 2009-01-30 17:41:53 +0000 | [diff] [blame] | 264 | OS << ' '; |
| 265 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 266 | } |
| 267 | |
| 268 | switch (Level) { |
Chris Lattner | 4132758 | 2009-02-06 03:57:44 +0000 | [diff] [blame] | 269 | case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type"); |
Nate Begeman | 165b954 | 2008-04-17 18:06:57 +0000 | [diff] [blame] | 270 | case Diagnostic::Note: OS << "note: "; break; |
| 271 | case Diagnostic::Warning: OS << "warning: "; break; |
| 272 | case Diagnostic::Error: OS << "error: "; break; |
Chris Lattner | 4132758 | 2009-02-06 03:57:44 +0000 | [diff] [blame] | 273 | case Diagnostic::Fatal: OS << "fatal error: "; break; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 274 | } |
| 275 | |
Chris Lattner | f4c8396 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 276 | llvm::SmallString<100> OutStr; |
| 277 | Info.FormatDiagnostic(OutStr); |
| 278 | OS.write(OutStr.begin(), OutStr.size()); |
| 279 | OS << '\n'; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 280 | |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 281 | // If caret diagnostics are enabled and we have location, we want to |
| 282 | // emit the caret. However, we only do this if the location moved |
| 283 | // from the last diagnostic, if the last diagnostic was a note that |
| 284 | // was part of a different warning or error diagnostic, or if the |
| 285 | // diagnostic has ranges. We don't want to emit the same caret |
| 286 | // multiple times if one loc has multiple diagnostics. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 287 | if (CaretDiagnostics && Info.getLocation().isValid() && |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 288 | ((LastLoc != Info.getLocation()) || Info.getNumRanges() || |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 289 | (LastCaretDiagnosticWasNote && Level != Diagnostic::Note) || |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 290 | Info.getNumCodeModificationHints())) { |
Steve Naroff | efe7f36 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 291 | // Cache the LastLoc, it allows us to omit duplicate source/caret spewage. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 292 | LastLoc = Info.getLocation(); |
Douglas Gregor | df667e7 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 293 | LastCaretDiagnosticWasNote = (Level == Diagnostic::Note); |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 294 | |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 295 | // Get the ranges into a local array we can hack on. |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 296 | SourceRange Ranges[20]; |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 297 | unsigned NumRanges = Info.getNumRanges(); |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 298 | assert(NumRanges < 20 && "Out of space"); |
Chris Lattner | ebbbb1b | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 299 | for (unsigned i = 0; i != NumRanges; ++i) |
| 300 | Ranges[i] = Info.getRange(i); |
| 301 | |
Douglas Gregor | 4b2d3f7 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 302 | unsigned NumHints = Info.getNumCodeModificationHints(); |
| 303 | for (unsigned idx = 0; idx < NumHints; ++idx) { |
| 304 | const CodeModificationHint &Hint = Info.getCodeModificationHint(idx); |
| 305 | if (Hint.RemoveRange.isValid()) { |
| 306 | assert(NumRanges < 20 && "Out of space"); |
| 307 | Ranges[NumRanges++] = Hint.RemoveRange; |
| 308 | } |
| 309 | } |
| 310 | |
| 311 | EmitCaretDiagnostic(LastLoc, Ranges, NumRanges, LastLoc.getManager(), |
| 312 | Info.getCodeModificationHints(), |
| 313 | Info.getNumCodeModificationHints()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 314 | } |
Chris Lattner | a03a5b5 | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 315 | |
| 316 | OS.flush(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 317 | } |