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