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