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