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