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