Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 1 | //===--- TextDiagnosticPrinter.cpp - Diagnostic Printer -------------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 5b12ab8 | 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. |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This diagnostic client prints out their diagnostic messages. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Daniel Dunbar | 51adf58 | 2009-03-02 06:16:29 +0000 | [diff] [blame] | 14 | #include "clang/Frontend/TextDiagnosticPrinter.h" |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 15 | #include "clang/Basic/SourceManager.h" |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 16 | #include "clang/Lex/Lexer.h" |
Chris Lattner | c45529b | 2009-05-05 22:03:18 +0000 | [diff] [blame] | 17 | #include "llvm/Support/MemoryBuffer.h" |
Chris Lattner | 327984f | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Chris Lattner | 23be067 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 19 | #include "llvm/ADT/SmallString.h" |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 20 | #include <algorithm> |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 21 | using namespace clang; |
| 22 | |
Torok Edwin | c91b6e0 | 2009-06-04 07:18:23 +0000 | [diff] [blame] | 23 | static const enum llvm::raw_ostream::Colors noteColor = |
| 24 | llvm::raw_ostream::BLACK; |
| 25 | static const enum llvm::raw_ostream::Colors fixitColor = |
| 26 | llvm::raw_ostream::GREEN; |
| 27 | static const enum llvm::raw_ostream::Colors caretColor = |
| 28 | llvm::raw_ostream::GREEN; |
| 29 | static const enum llvm::raw_ostream::Colors warningColor = |
| 30 | llvm::raw_ostream::MAGENTA; |
| 31 | static const enum llvm::raw_ostream::Colors errorColor = llvm::raw_ostream::RED; |
| 32 | static const enum llvm::raw_ostream::Colors fatalColor = llvm::raw_ostream::RED; |
| 33 | // used for changing only the bold attribute |
| 34 | static const enum llvm::raw_ostream::Colors savedColor = |
| 35 | llvm::raw_ostream::SAVEDCOLOR; |
| 36 | |
Douglas Gregor | 4818553 | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 37 | /// \brief Number of spaces to indent when word-wrapping. |
| 38 | const unsigned WordWrapIndentation = 6; |
| 39 | |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 40 | void TextDiagnosticPrinter:: |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 41 | PrintIncludeStack(SourceLocation Loc, const SourceManager &SM) { |
| 42 | if (Loc.isInvalid()) return; |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 43 | |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 44 | PresumedLoc PLoc = SM.getPresumedLoc(Loc); |
Chris Lattner | dc5c055 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 45 | |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 46 | // Print out the other include frames first. |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 47 | PrintIncludeStack(PLoc.getIncludeLoc(), SM); |
Chris Lattner | 92b29b2f | 2009-04-21 03:57:54 +0000 | [diff] [blame] | 48 | |
| 49 | if (ShowLocation) |
| 50 | OS << "In file included from " << PLoc.getFilename() |
| 51 | << ':' << PLoc.getLine() << ":\n"; |
| 52 | else |
| 53 | OS << "In included file:\n"; |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 54 | } |
| 55 | |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 56 | /// HighlightRange - Given a SourceRange and a line number, highlight (with ~'s) |
| 57 | /// any characters in LineNo that intersect the SourceRange. |
Ted Kremenek | d4e5fba | 2007-12-11 21:27:55 +0000 | [diff] [blame] | 58 | void TextDiagnosticPrinter::HighlightRange(const SourceRange &R, |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 59 | const SourceManager &SM, |
Chris Lattner | 71dc14b | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 60 | unsigned LineNo, FileID FID, |
Gordon Henriksen | 04c50bd | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 61 | std::string &CaretLine, |
Nuno Lopes | d86aa93 | 2008-08-05 19:40:20 +0000 | [diff] [blame] | 62 | const std::string &SourceLine) { |
Gordon Henriksen | 04c50bd | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 63 | assert(CaretLine.size() == SourceLine.size() && |
| 64 | "Expect a correspondence between source and caret line!"); |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 65 | if (!R.isValid()) return; |
| 66 | |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 67 | SourceLocation Begin = SM.getInstantiationLoc(R.getBegin()); |
| 68 | SourceLocation End = SM.getInstantiationLoc(R.getEnd()); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 69 | |
Chris Lattner | b42700f | 2009-02-17 05:19:10 +0000 | [diff] [blame] | 70 | // If the End location and the start location are the same and are a macro |
| 71 | // location, then the range was something that came from a macro expansion |
| 72 | // or _Pragma. If this is an object-like macro, the best we can do is to |
| 73 | // highlight the range. If this is a function-like macro, we'd also like to |
| 74 | // highlight the arguments. |
| 75 | if (Begin == End && R.getEnd().isMacroID()) |
| 76 | End = SM.getInstantiationRange(R.getEnd()).second; |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 77 | |
Chris Lattner | 88ea93e | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 78 | unsigned StartLineNo = SM.getInstantiationLineNumber(Begin); |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 79 | if (StartLineNo > LineNo || SM.getFileID(Begin) != FID) |
Chris Lattner | 177977ac | 2008-01-12 06:43:35 +0000 | [diff] [blame] | 80 | return; // No intersection. |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 81 | |
Chris Lattner | 88ea93e | 2009-02-04 01:06:56 +0000 | [diff] [blame] | 82 | unsigned EndLineNo = SM.getInstantiationLineNumber(End); |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 83 | if (EndLineNo < LineNo || SM.getFileID(End) != FID) |
Chris Lattner | 177977ac | 2008-01-12 06:43:35 +0000 | [diff] [blame] | 84 | return; // No intersection. |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 85 | |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 86 | // Compute the column number of the start. |
| 87 | unsigned StartColNo = 0; |
| 88 | if (StartLineNo == LineNo) { |
Chris Lattner | e4ad417 | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 89 | StartColNo = SM.getInstantiationColumnNumber(Begin); |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 90 | if (StartColNo) --StartColNo; // Zero base the col #. |
| 91 | } |
| 92 | |
| 93 | // Pick the first non-whitespace column. |
| 94 | while (StartColNo < SourceLine.size() && |
| 95 | (SourceLine[StartColNo] == ' ' || SourceLine[StartColNo] == '\t')) |
| 96 | ++StartColNo; |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 97 | |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 98 | // Compute the column number of the end. |
Gordon Henriksen | 04c50bd | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 99 | unsigned EndColNo = CaretLine.size(); |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 100 | if (EndLineNo == LineNo) { |
Chris Lattner | e4ad417 | 2009-02-04 00:55:58 +0000 | [diff] [blame] | 101 | EndColNo = SM.getInstantiationColumnNumber(End); |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 102 | if (EndColNo) { |
| 103 | --EndColNo; // Zero base the col #. |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 104 | |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 105 | // Add in the length of the token, so that we cover multi-char tokens. |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 106 | EndColNo += Lexer::MeasureTokenLength(End, SM, *LangOpts); |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 107 | } else { |
Gordon Henriksen | 04c50bd | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 108 | EndColNo = CaretLine.size(); |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 109 | } |
| 110 | } |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 111 | |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 112 | // Pick the last non-whitespace column. |
Nuno Lopes | d86aa93 | 2008-08-05 19:40:20 +0000 | [diff] [blame] | 113 | if (EndColNo <= SourceLine.size()) |
| 114 | while (EndColNo-1 && |
| 115 | (SourceLine[EndColNo-1] == ' ' || SourceLine[EndColNo-1] == '\t')) |
| 116 | --EndColNo; |
| 117 | else |
| 118 | EndColNo = SourceLine.size(); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 119 | |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 120 | // Fill the range with ~'s. |
| 121 | assert(StartColNo <= EndColNo && "Invalid range!"); |
Nuno Lopes | d86aa93 | 2008-08-05 19:40:20 +0000 | [diff] [blame] | 122 | for (unsigned i = StartColNo; i < EndColNo; ++i) |
Gordon Henriksen | 04c50bd | 2008-08-09 19:58:22 +0000 | [diff] [blame] | 123 | CaretLine[i] = '~'; |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 124 | } |
| 125 | |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 126 | /// \brief When the source code line we want to print is too long for |
| 127 | /// the terminal, select the "interesting" region. |
| 128 | static void SelectInterestingSourceRegion(std::string &SourceLine, |
| 129 | std::string &CaretLine, |
| 130 | std::string &FixItInsertionLine, |
Douglas Gregor | 2d69cd2 | 2009-05-04 06:27:32 +0000 | [diff] [blame] | 131 | unsigned EndOfCaretToken, |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 132 | unsigned Columns) { |
| 133 | if (CaretLine.size() > SourceLine.size()) |
| 134 | SourceLine.resize(CaretLine.size(), ' '); |
| 135 | |
| 136 | // Find the slice that we need to display the full caret line |
| 137 | // correctly. |
| 138 | unsigned CaretStart = 0, CaretEnd = CaretLine.size(); |
| 139 | for (; CaretStart != CaretEnd; ++CaretStart) |
| 140 | if (!isspace(CaretLine[CaretStart])) |
| 141 | break; |
| 142 | |
| 143 | for (; CaretEnd != CaretStart; --CaretEnd) |
| 144 | if (!isspace(CaretLine[CaretEnd - 1])) |
| 145 | break; |
Douglas Gregor | 2d69cd2 | 2009-05-04 06:27:32 +0000 | [diff] [blame] | 146 | |
| 147 | // Make sure we don't chop the string shorter than the caret token |
| 148 | // itself. |
| 149 | if (CaretEnd < EndOfCaretToken) |
| 150 | CaretEnd = EndOfCaretToken; |
| 151 | |
Douglas Gregor | b8c6d5d | 2009-05-03 04:33:32 +0000 | [diff] [blame] | 152 | // If we have a fix-it line, make sure the slice includes all of the |
| 153 | // fix-it information. |
| 154 | if (!FixItInsertionLine.empty()) { |
| 155 | unsigned FixItStart = 0, FixItEnd = FixItInsertionLine.size(); |
| 156 | for (; FixItStart != FixItEnd; ++FixItStart) |
| 157 | if (!isspace(FixItInsertionLine[FixItStart])) |
| 158 | break; |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 159 | |
Douglas Gregor | b8c6d5d | 2009-05-03 04:33:32 +0000 | [diff] [blame] | 160 | for (; FixItEnd != FixItStart; --FixItEnd) |
| 161 | if (!isspace(FixItInsertionLine[FixItEnd - 1])) |
| 162 | break; |
| 163 | |
| 164 | if (FixItStart < CaretStart) |
| 165 | CaretStart = FixItStart; |
| 166 | if (FixItEnd > CaretEnd) |
| 167 | CaretEnd = FixItEnd; |
| 168 | } |
| 169 | |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 170 | // CaretLine[CaretStart, CaretEnd) contains all of the interesting |
| 171 | // parts of the caret line. While this slice is smaller than the |
| 172 | // number of columns we have, try to grow the slice to encompass |
| 173 | // more context. |
| 174 | |
| 175 | // If the end of the interesting region comes before we run out of |
| 176 | // space in the terminal, start at the beginning of the line. |
Douglas Gregor | 12c3a5c | 2009-05-15 18:05:24 +0000 | [diff] [blame] | 177 | if (Columns > 3 && CaretEnd < Columns - 3) |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 178 | CaretStart = 0; |
| 179 | |
Douglas Gregor | 12c3a5c | 2009-05-15 18:05:24 +0000 | [diff] [blame] | 180 | unsigned TargetColumns = Columns; |
| 181 | if (TargetColumns > 8) |
| 182 | TargetColumns -= 8; // Give us extra room for the ellipses. |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 183 | unsigned SourceLength = SourceLine.size(); |
Douglas Gregor | 006fd38 | 2009-05-04 06:45:38 +0000 | [diff] [blame] | 184 | while ((CaretEnd - CaretStart) < TargetColumns) { |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 185 | bool ExpandedRegion = false; |
| 186 | // Move the start of the interesting region left until we've |
| 187 | // pulled in something else interesting. |
Douglas Gregor | 006fd38 | 2009-05-04 06:45:38 +0000 | [diff] [blame] | 188 | if (CaretStart == 1) |
| 189 | CaretStart = 0; |
| 190 | else if (CaretStart > 1) { |
| 191 | unsigned NewStart = CaretStart - 1; |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 192 | |
Douglas Gregor | 006fd38 | 2009-05-04 06:45:38 +0000 | [diff] [blame] | 193 | // Skip over any whitespace we see here; we're looking for |
| 194 | // another bit of interesting text. |
| 195 | while (NewStart && isspace(SourceLine[NewStart])) |
| 196 | --NewStart; |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 197 | |
Douglas Gregor | 006fd38 | 2009-05-04 06:45:38 +0000 | [diff] [blame] | 198 | // Skip over this bit of "interesting" text. |
| 199 | while (NewStart && !isspace(SourceLine[NewStart])) |
| 200 | --NewStart; |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 201 | |
Douglas Gregor | 006fd38 | 2009-05-04 06:45:38 +0000 | [diff] [blame] | 202 | // Move up to the non-whitespace character we just saw. |
| 203 | if (NewStart) |
| 204 | ++NewStart; |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 205 | |
| 206 | // If we're still within our limit, update the starting |
| 207 | // position within the source/caret line. |
Douglas Gregor | 006fd38 | 2009-05-04 06:45:38 +0000 | [diff] [blame] | 208 | if (CaretEnd - NewStart <= TargetColumns) { |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 209 | CaretStart = NewStart; |
| 210 | ExpandedRegion = true; |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | // Move the end of the interesting region right until we've |
| 215 | // pulled in something else interesting. |
Daniel Dunbar | 6f94991 | 2009-05-03 23:04:40 +0000 | [diff] [blame] | 216 | if (CaretEnd != SourceLength) { |
Daniel Dunbar | 28a24fd | 2009-10-19 09:11:21 +0000 | [diff] [blame^] | 217 | assert(CaretEnd < SourceLength && "Unexpected caret position!"); |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 218 | unsigned NewEnd = CaretEnd; |
| 219 | |
| 220 | // Skip over any whitespace we see here; we're looking for |
| 221 | // another bit of interesting text. |
Douglas Gregor | 8e35e2d | 2009-05-18 22:09:16 +0000 | [diff] [blame] | 222 | while (NewEnd != SourceLength && isspace(SourceLine[NewEnd - 1])) |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 223 | ++NewEnd; |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 224 | |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 225 | // Skip over this bit of "interesting" text. |
Douglas Gregor | 8e35e2d | 2009-05-18 22:09:16 +0000 | [diff] [blame] | 226 | while (NewEnd != SourceLength && !isspace(SourceLine[NewEnd - 1])) |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 227 | ++NewEnd; |
| 228 | |
| 229 | if (NewEnd - CaretStart <= TargetColumns) { |
| 230 | CaretEnd = NewEnd; |
| 231 | ExpandedRegion = true; |
| 232 | } |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 233 | } |
Daniel Dunbar | 6f94991 | 2009-05-03 23:04:40 +0000 | [diff] [blame] | 234 | |
| 235 | if (!ExpandedRegion) |
| 236 | break; |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | // [CaretStart, CaretEnd) is the slice we want. Update the various |
| 240 | // output lines to show only this slice, with two-space padding |
| 241 | // before the lines so that it looks nicer. |
Douglas Gregor | 54c6a3b | 2009-05-03 04:12:51 +0000 | [diff] [blame] | 242 | if (CaretEnd < SourceLine.size()) |
| 243 | SourceLine.replace(CaretEnd, std::string::npos, "..."); |
Douglas Gregor | cb51662 | 2009-05-03 15:24:25 +0000 | [diff] [blame] | 244 | if (CaretEnd < CaretLine.size()) |
| 245 | CaretLine.erase(CaretEnd, std::string::npos); |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 246 | if (FixItInsertionLine.size() > CaretEnd) |
| 247 | FixItInsertionLine.erase(CaretEnd, std::string::npos); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 248 | |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 249 | if (CaretStart > 2) { |
Douglas Gregor | 54c6a3b | 2009-05-03 04:12:51 +0000 | [diff] [blame] | 250 | SourceLine.replace(0, CaretStart, " ..."); |
| 251 | CaretLine.replace(0, CaretStart, " "); |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 252 | if (FixItInsertionLine.size() >= CaretStart) |
Douglas Gregor | 54c6a3b | 2009-05-03 04:12:51 +0000 | [diff] [blame] | 253 | FixItInsertionLine.replace(0, CaretStart, " "); |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Chris Lattner | 1973d84 | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 257 | void TextDiagnosticPrinter::EmitCaretDiagnostic(SourceLocation Loc, |
Chris Lattner | 464ceb4 | 2009-02-20 00:25:28 +0000 | [diff] [blame] | 258 | SourceRange *Ranges, |
Chris Lattner | 1973d84 | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 259 | unsigned NumRanges, |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 260 | SourceManager &SM, |
| 261 | const CodeModificationHint *Hints, |
Douglas Gregor | 4818553 | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 262 | unsigned NumHints, |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 263 | unsigned Columns) { |
Chris Lattner | 7b60a16 | 2009-02-17 08:44:50 +0000 | [diff] [blame] | 264 | assert(!Loc.isInvalid() && "must have a valid source location here"); |
Chris Lattner | c45529b | 2009-05-05 22:03:18 +0000 | [diff] [blame] | 265 | |
| 266 | // If this is a macro ID, first emit information about where this was |
| 267 | // instantiated (recursively) then emit information about where. the token was |
| 268 | // spelled from. |
Chris Lattner | 7b60a16 | 2009-02-17 08:44:50 +0000 | [diff] [blame] | 269 | if (!Loc.isFileID()) { |
Chris Lattner | 24eb28b | 2009-02-18 18:50:45 +0000 | [diff] [blame] | 270 | SourceLocation OneLevelUp = SM.getImmediateInstantiationRange(Loc).first; |
Chris Lattner | c45529b | 2009-05-05 22:03:18 +0000 | [diff] [blame] | 271 | // FIXME: Map ranges? |
Douglas Gregor | 080fe61 | 2009-05-06 04:43:47 +0000 | [diff] [blame] | 272 | EmitCaretDiagnostic(OneLevelUp, Ranges, NumRanges, SM, 0, 0, Columns); |
Chris Lattner | 464ceb4 | 2009-02-20 00:25:28 +0000 | [diff] [blame] | 273 | |
Chris Lattner | c45529b | 2009-05-05 22:03:18 +0000 | [diff] [blame] | 274 | Loc = SM.getImmediateSpellingLoc(Loc); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 275 | |
Chris Lattner | 464ceb4 | 2009-02-20 00:25:28 +0000 | [diff] [blame] | 276 | // Map the ranges. |
| 277 | for (unsigned i = 0; i != NumRanges; ++i) { |
| 278 | SourceLocation S = Ranges[i].getBegin(), E = Ranges[i].getEnd(); |
Chris Lattner | c45529b | 2009-05-05 22:03:18 +0000 | [diff] [blame] | 279 | if (S.isMacroID()) S = SM.getImmediateSpellingLoc(S); |
| 280 | if (E.isMacroID()) E = SM.getImmediateSpellingLoc(E); |
Chris Lattner | 464ceb4 | 2009-02-20 00:25:28 +0000 | [diff] [blame] | 281 | Ranges[i] = SourceRange(S, E); |
| 282 | } |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 283 | |
Chris Lattner | 92b29b2f | 2009-04-21 03:57:54 +0000 | [diff] [blame] | 284 | if (ShowLocation) { |
Chris Lattner | c45529b | 2009-05-05 22:03:18 +0000 | [diff] [blame] | 285 | std::pair<FileID, unsigned> IInfo = SM.getDecomposedInstantiationLoc(Loc); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 286 | |
Chris Lattner | 92b29b2f | 2009-04-21 03:57:54 +0000 | [diff] [blame] | 287 | // Emit the file/line/column that this expansion came from. |
Chris Lattner | c45529b | 2009-05-05 22:03:18 +0000 | [diff] [blame] | 288 | OS << SM.getBuffer(IInfo.first)->getBufferIdentifier() << ':' |
| 289 | << SM.getLineNumber(IInfo.first, IInfo.second) << ':'; |
Chris Lattner | 92b29b2f | 2009-04-21 03:57:54 +0000 | [diff] [blame] | 290 | if (ShowColumn) |
Chris Lattner | c45529b | 2009-05-05 22:03:18 +0000 | [diff] [blame] | 291 | OS << SM.getColumnNumber(IInfo.first, IInfo.second) << ':'; |
Chris Lattner | 92b29b2f | 2009-04-21 03:57:54 +0000 | [diff] [blame] | 292 | OS << ' '; |
| 293 | } |
| 294 | OS << "note: instantiated from:\n"; |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 295 | |
Douglas Gregor | 080fe61 | 2009-05-06 04:43:47 +0000 | [diff] [blame] | 296 | EmitCaretDiagnostic(Loc, Ranges, NumRanges, SM, Hints, NumHints, Columns); |
Chris Lattner | c45529b | 2009-05-05 22:03:18 +0000 | [diff] [blame] | 297 | return; |
Chris Lattner | 7b60a16 | 2009-02-17 08:44:50 +0000 | [diff] [blame] | 298 | } |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 299 | |
Chris Lattner | d22603c | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 300 | // Decompose the location into a FID/Offset pair. |
| 301 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); |
| 302 | FileID FID = LocInfo.first; |
| 303 | unsigned FileOffset = LocInfo.second; |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 304 | |
Chris Lattner | d22603c | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 305 | // Get information about the buffer it points into. |
| 306 | std::pair<const char*, const char*> BufferInfo = SM.getBufferData(FID); |
| 307 | const char *BufStart = BufferInfo.first; |
Chris Lattner | d22603c | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 308 | |
| 309 | unsigned ColNo = SM.getColumnNumber(FID, FileOffset); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 310 | unsigned CaretEndColNo |
Douglas Gregor | 2d69cd2 | 2009-05-04 06:27:32 +0000 | [diff] [blame] | 311 | = ColNo + Lexer::MeasureTokenLength(Loc, SM, *LangOpts); |
| 312 | |
Chris Lattner | 2cf68c1 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 313 | // Rewind from the current position to the start of the line. |
Chris Lattner | d22603c | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 314 | const char *TokPtr = BufStart+FileOffset; |
| 315 | const char *LineStart = TokPtr-ColNo+1; // Column # is 1-based. |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 316 | |
| 317 | |
Chris Lattner | 2cf68c1 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 318 | // Compute the line end. Scan forward from the error position to the end of |
| 319 | // the line. |
Chris Lattner | d22603c | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 320 | const char *LineEnd = TokPtr; |
Chris Lattner | 139d87b | 2009-03-08 08:11:22 +0000 | [diff] [blame] | 321 | while (*LineEnd != '\n' && *LineEnd != '\r' && *LineEnd != '\0') |
Chris Lattner | 2cf68c1 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 322 | ++LineEnd; |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 323 | |
Daniel Dunbar | 28a24fd | 2009-10-19 09:11:21 +0000 | [diff] [blame^] | 324 | // FIXME: This shouldn't be necessary, but the CaretEndColNo can extend past |
| 325 | // the source line length as currently being computed. See |
| 326 | // test/Misc/message-length.c. |
| 327 | CaretEndColNo = std::min(CaretEndColNo, unsigned(LineEnd - LineStart)); |
| 328 | |
Chris Lattner | 2cf68c1 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 329 | // Copy the line of code into an std::string for ease of manipulation. |
| 330 | std::string SourceLine(LineStart, LineEnd); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 331 | |
Chris Lattner | 2cf68c1 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 332 | // Create a line for the caret that is filled with spaces that is the same |
| 333 | // length as the line of source code. |
| 334 | std::string CaretLine(LineEnd-LineStart, ' '); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 335 | |
Chris Lattner | 2cf68c1 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 336 | // Highlight all of the characters covered by Ranges with ~ characters. |
Chris Lattner | 1973d84 | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 337 | if (NumRanges) { |
Chris Lattner | d22603c | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 338 | unsigned LineNo = SM.getLineNumber(FID, FileOffset); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 339 | |
Chris Lattner | 1973d84 | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 340 | for (unsigned i = 0, e = NumRanges; i != e; ++i) |
| 341 | HighlightRange(Ranges[i], SM, LineNo, FID, CaretLine, SourceLine); |
Chris Lattner | d22603c | 2009-02-17 07:51:53 +0000 | [diff] [blame] | 342 | } |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 343 | |
Chris Lattner | 2cf68c1 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 344 | // Next, insert the caret itself. |
| 345 | if (ColNo-1 < CaretLine.size()) |
| 346 | CaretLine[ColNo-1] = '^'; |
| 347 | else |
| 348 | CaretLine.push_back('^'); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 349 | |
Chris Lattner | 2cf68c1 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 350 | // Scan the source line, looking for tabs. If we find any, manually expand |
| 351 | // them to 8 characters and update the CaretLine to match. |
| 352 | for (unsigned i = 0; i != SourceLine.size(); ++i) { |
| 353 | if (SourceLine[i] != '\t') continue; |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 354 | |
Chris Lattner | 2cf68c1 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 355 | // Replace this tab with at least one space. |
| 356 | SourceLine[i] = ' '; |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 357 | |
Chris Lattner | 2cf68c1 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 358 | // Compute the number of spaces we need to insert. |
| 359 | unsigned NumSpaces = ((i+8)&~7) - (i+1); |
| 360 | assert(NumSpaces < 8 && "Invalid computation of space amt"); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 361 | |
Chris Lattner | 2cf68c1 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 362 | // Insert spaces into the SourceLine. |
| 363 | SourceLine.insert(i+1, NumSpaces, ' '); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 364 | |
Chris Lattner | 2cf68c1 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 365 | // Insert spaces or ~'s into CaretLine. |
| 366 | CaretLine.insert(i+1, NumSpaces, CaretLine[i] == '~' ? '~' : ' '); |
| 367 | } |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 368 | |
Chris Lattner | af6094e | 2009-04-28 22:33:16 +0000 | [diff] [blame] | 369 | // If we are in -fdiagnostics-print-source-range-info mode, we are trying to |
| 370 | // produce easily machine parsable output. Add a space before the source line |
| 371 | // and the caret to make it trivial to tell the main diagnostic line from what |
| 372 | // the user is intended to see. |
| 373 | if (PrintRangeInfo) { |
| 374 | SourceLine = ' ' + SourceLine; |
| 375 | CaretLine = ' ' + CaretLine; |
| 376 | } |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 377 | |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 378 | std::string FixItInsertionLine; |
Chris Lattner | 29d34ca | 2009-04-19 07:44:08 +0000 | [diff] [blame] | 379 | if (NumHints && PrintFixItInfo) { |
Chris Lattner | 29d34ca | 2009-04-19 07:44:08 +0000 | [diff] [blame] | 380 | for (const CodeModificationHint *Hint = Hints, *LastHint = Hints + NumHints; |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 381 | Hint != LastHint; ++Hint) { |
| 382 | if (Hint->InsertionLoc.isValid()) { |
| 383 | // We have an insertion hint. Determine whether the inserted |
| 384 | // code is on the same line as the caret. |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 385 | std::pair<FileID, unsigned> HintLocInfo |
Chris Lattner | e29509a | 2009-03-02 20:58:48 +0000 | [diff] [blame] | 386 | = SM.getDecomposedInstantiationLoc(Hint->InsertionLoc); |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 387 | if (SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) == |
| 388 | SM.getLineNumber(FID, FileOffset)) { |
| 389 | // Insert the new code into the line just below the code |
| 390 | // that the user wrote. |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 391 | unsigned HintColNo |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 392 | = SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 393 | unsigned LastColumnModified |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 394 | = HintColNo - 1 + Hint->CodeToInsert.size(); |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 395 | if (LastColumnModified > FixItInsertionLine.size()) |
| 396 | FixItInsertionLine.resize(LastColumnModified, ' '); |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 397 | std::copy(Hint->CodeToInsert.begin(), Hint->CodeToInsert.end(), |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 398 | FixItInsertionLine.begin() + HintColNo - 1); |
Douglas Gregor | b8c6d5d | 2009-05-03 04:33:32 +0000 | [diff] [blame] | 399 | } else { |
| 400 | FixItInsertionLine.clear(); |
| 401 | break; |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 402 | } |
| 403 | } |
| 404 | } |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 405 | } |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 406 | |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 407 | // If the source line is too long for our terminal, select only the |
| 408 | // "interesting" source region within that line. |
| 409 | if (Columns && SourceLine.size() > Columns) |
| 410 | SelectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine, |
Douglas Gregor | 2d69cd2 | 2009-05-04 06:27:32 +0000 | [diff] [blame] | 411 | CaretEndColNo, Columns); |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 412 | |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 413 | // Finally, remove any blank spaces from the end of CaretLine. |
| 414 | while (CaretLine[CaretLine.size()-1] == ' ') |
| 415 | CaretLine.erase(CaretLine.end()-1); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 416 | |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 417 | // Emit what we have computed. |
| 418 | OS << SourceLine << '\n'; |
Torok Edwin | c91b6e0 | 2009-06-04 07:18:23 +0000 | [diff] [blame] | 419 | |
| 420 | if (UseColors) |
| 421 | OS.changeColor(caretColor, true); |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 422 | OS << CaretLine << '\n'; |
Torok Edwin | c91b6e0 | 2009-06-04 07:18:23 +0000 | [diff] [blame] | 423 | if (UseColors) |
| 424 | OS.resetColor(); |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 425 | |
| 426 | if (!FixItInsertionLine.empty()) { |
Torok Edwin | c91b6e0 | 2009-06-04 07:18:23 +0000 | [diff] [blame] | 427 | if (UseColors) |
| 428 | // Print fixit line in color |
| 429 | OS.changeColor(fixitColor, false); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 430 | if (PrintRangeInfo) |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 431 | OS << ' '; |
| 432 | OS << FixItInsertionLine << '\n'; |
Torok Edwin | c91b6e0 | 2009-06-04 07:18:23 +0000 | [diff] [blame] | 433 | if (UseColors) |
| 434 | OS.resetColor(); |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 435 | } |
Chris Lattner | 2cf68c1 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 436 | } |
| 437 | |
Douglas Gregor | 4818553 | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 438 | /// \brief Skip over whitespace in the string, starting at the given |
| 439 | /// index. |
| 440 | /// |
| 441 | /// \returns The index of the first non-whitespace character that is |
| 442 | /// greater than or equal to Idx or, if no such character exists, |
| 443 | /// returns the end of the string. |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 444 | static unsigned skipWhitespace(unsigned Idx, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | const llvm::SmallVectorImpl<char> &Str, |
Douglas Gregor | 4818553 | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 446 | unsigned Length) { |
| 447 | while (Idx < Length && isspace(Str[Idx])) |
| 448 | ++Idx; |
| 449 | return Idx; |
| 450 | } |
| 451 | |
| 452 | /// \brief If the given character is the start of some kind of |
| 453 | /// balanced punctuation (e.g., quotes or parentheses), return the |
| 454 | /// character that will terminate the punctuation. |
| 455 | /// |
| 456 | /// \returns The ending punctuation character, if any, or the NULL |
| 457 | /// character if the input character does not start any punctuation. |
| 458 | static inline char findMatchingPunctuation(char c) { |
| 459 | switch (c) { |
| 460 | case '\'': return '\''; |
| 461 | case '`': return '\''; |
| 462 | case '"': return '"'; |
| 463 | case '(': return ')'; |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 464 | case '[': return ']'; |
Douglas Gregor | 4818553 | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 465 | case '{': return '}'; |
| 466 | default: break; |
| 467 | } |
| 468 | |
| 469 | return 0; |
| 470 | } |
| 471 | |
| 472 | /// \brief Find the end of the word starting at the given offset |
| 473 | /// within a string. |
| 474 | /// |
| 475 | /// \returns the index pointing one character past the end of the |
| 476 | /// word. |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 477 | unsigned findEndOfWord(unsigned Start, |
Douglas Gregor | 4818553 | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 478 | const llvm::SmallVectorImpl<char> &Str, |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 479 | unsigned Length, unsigned Column, |
Douglas Gregor | 4818553 | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 480 | unsigned Columns) { |
| 481 | unsigned End = Start + 1; |
| 482 | |
| 483 | // Determine if the start of the string is actually opening |
| 484 | // punctuation, e.g., a quote or parentheses. |
| 485 | char EndPunct = findMatchingPunctuation(Str[Start]); |
| 486 | if (!EndPunct) { |
| 487 | // This is a normal word. Just find the first space character. |
| 488 | while (End < Length && !isspace(Str[End])) |
| 489 | ++End; |
| 490 | return End; |
| 491 | } |
| 492 | |
| 493 | // We have the start of a balanced punctuation sequence (quotes, |
| 494 | // parentheses, etc.). Determine the full sequence is. |
| 495 | llvm::SmallVector<char, 16> PunctuationEndStack; |
| 496 | PunctuationEndStack.push_back(EndPunct); |
| 497 | while (End < Length && !PunctuationEndStack.empty()) { |
| 498 | if (Str[End] == PunctuationEndStack.back()) |
| 499 | PunctuationEndStack.pop_back(); |
| 500 | else if (char SubEndPunct = findMatchingPunctuation(Str[End])) |
| 501 | PunctuationEndStack.push_back(SubEndPunct); |
| 502 | |
| 503 | ++End; |
| 504 | } |
| 505 | |
| 506 | // Find the first space character after the punctuation ended. |
| 507 | while (End < Length && !isspace(Str[End])) |
| 508 | ++End; |
| 509 | |
| 510 | unsigned PunctWordLength = End - Start; |
| 511 | if (// If the word fits on this line |
| 512 | Column + PunctWordLength <= Columns || |
| 513 | // ... or the word is "short enough" to take up the next line |
| 514 | // without too much ugly white space |
| 515 | PunctWordLength < Columns/3) |
| 516 | return End; // Take the whole thing as a single "word". |
| 517 | |
| 518 | // The whole quoted/parenthesized string is too long to print as a |
| 519 | // single "word". Instead, find the "word" that starts just after |
| 520 | // the punctuation and use that end-point instead. This will recurse |
| 521 | // until it finds something small enough to consider a word. |
| 522 | return findEndOfWord(Start + 1, Str, Length, Column + 1, Columns); |
| 523 | } |
| 524 | |
| 525 | /// \brief Print the given string to a stream, word-wrapping it to |
| 526 | /// some number of columns in the process. |
| 527 | /// |
| 528 | /// \brief OS the stream to which the word-wrapping string will be |
| 529 | /// emitted. |
| 530 | /// |
| 531 | /// \brief Str the string to word-wrap and output. |
| 532 | /// |
| 533 | /// \brief Columns the number of columns to word-wrap to. |
| 534 | /// |
| 535 | /// \brief Column the column number at which the first character of \p |
| 536 | /// Str will be printed. This will be non-zero when part of the first |
| 537 | /// line has already been printed. |
| 538 | /// |
| 539 | /// \brief Indentation the number of spaces to indent any lines beyond |
| 540 | /// the first line. |
| 541 | /// |
| 542 | /// \returns true if word-wrapping was required, or false if the |
| 543 | /// string fit on the first line. |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 544 | static bool PrintWordWrapped(llvm::raw_ostream &OS, |
Mike Stump | 11289f4 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 545 | const llvm::SmallVectorImpl<char> &Str, |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 546 | unsigned Columns, |
Douglas Gregor | 4818553 | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 547 | unsigned Column = 0, |
| 548 | unsigned Indentation = WordWrapIndentation) { |
| 549 | unsigned Length = Str.size(); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 550 | |
Douglas Gregor | 4818553 | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 551 | // If there is a newline in this message somewhere, find that |
| 552 | // newline and split the message into the part before the newline |
| 553 | // (which will be word-wrapped) and the part from the newline one |
| 554 | // (which will be emitted unchanged). |
| 555 | for (unsigned I = 0; I != Length; ++I) |
| 556 | if (Str[I] == '\n') { |
| 557 | Length = I; |
| 558 | break; |
| 559 | } |
| 560 | |
| 561 | // The string used to indent each line. |
| 562 | llvm::SmallString<16> IndentStr; |
| 563 | IndentStr.assign(Indentation, ' '); |
| 564 | bool Wrapped = false; |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 565 | for (unsigned WordStart = 0, WordEnd; WordStart < Length; |
Douglas Gregor | 4818553 | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 566 | WordStart = WordEnd) { |
| 567 | // Find the beginning of the next word. |
| 568 | WordStart = skipWhitespace(WordStart, Str, Length); |
| 569 | if (WordStart == Length) |
| 570 | break; |
| 571 | |
| 572 | // Find the end of this word. |
| 573 | WordEnd = findEndOfWord(WordStart, Str, Length, Column, Columns); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 574 | |
Douglas Gregor | 4818553 | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 575 | // Does this word fit on the current line? |
| 576 | unsigned WordLength = WordEnd - WordStart; |
| 577 | if (Column + WordLength < Columns) { |
| 578 | // This word fits on the current line; print it there. |
| 579 | if (WordStart) { |
| 580 | OS << ' '; |
| 581 | Column += 1; |
| 582 | } |
| 583 | OS.write(&Str[WordStart], WordLength); |
| 584 | Column += WordLength; |
| 585 | continue; |
| 586 | } |
| 587 | |
| 588 | // This word does not fit on the current line, so wrap to the next |
| 589 | // line. |
Douglas Gregor | 6f5a38b | 2009-05-03 03:52:38 +0000 | [diff] [blame] | 590 | OS << '\n'; |
| 591 | OS.write(&IndentStr[0], Indentation); |
Douglas Gregor | 4818553 | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 592 | OS.write(&Str[WordStart], WordLength); |
| 593 | Column = Indentation + WordLength; |
| 594 | Wrapped = true; |
| 595 | } |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 596 | |
Douglas Gregor | 4818553 | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 597 | if (Length == Str.size()) |
| 598 | return Wrapped; // We're done. |
| 599 | |
| 600 | // There is a newline in the message, followed by something that |
| 601 | // will not be word-wrapped. Print that. |
| 602 | OS.write(&Str[Length], Str.size() - Length); |
| 603 | return true; |
| 604 | } |
Chris Lattner | 2cf68c1 | 2009-02-17 07:38:37 +0000 | [diff] [blame] | 605 | |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 606 | void TextDiagnosticPrinter::HandleDiagnostic(Diagnostic::Level Level, |
Chris Lattner | 8488c82 | 2008-11-18 07:04:44 +0000 | [diff] [blame] | 607 | const DiagnosticInfo &Info) { |
Douglas Gregor | 4818553 | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 608 | // Keeps track of the the starting position of the location |
| 609 | // information (e.g., "foo.c:10:4:") that precedes the error |
| 610 | // message. We use this information to determine how long the |
| 611 | // file+line+column number prefix is. |
| 612 | uint64_t StartOfLocationInfo = OS.tell(); |
| 613 | |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 614 | // If the location is specified, print out a file/line/col and include trace |
| 615 | // if enabled. |
| 616 | if (Info.getLocation().isValid()) { |
Ted Kremenek | 5241832 | 2009-01-28 20:47:47 +0000 | [diff] [blame] | 617 | const SourceManager &SM = Info.getLocation().getManager(); |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 618 | PresumedLoc PLoc = SM.getPresumedLoc(Info.getLocation()); |
| 619 | unsigned LineNo = PLoc.getLine(); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 620 | |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 621 | // First, if this diagnostic is not in the main file, print out the |
| 622 | // "included from" lines. |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 623 | if (LastWarningLoc != PLoc.getIncludeLoc()) { |
| 624 | LastWarningLoc = PLoc.getIncludeLoc(); |
| 625 | PrintIncludeStack(LastWarningLoc, SM); |
Douglas Gregor | 4818553 | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 626 | StartOfLocationInfo = OS.tell(); |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 627 | } |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 628 | |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 629 | // Compute the column number. |
Chris Lattner | 1958677 | 2009-01-30 17:41:53 +0000 | [diff] [blame] | 630 | if (ShowLocation) { |
Torok Edwin | c91b6e0 | 2009-06-04 07:18:23 +0000 | [diff] [blame] | 631 | if (UseColors) |
| 632 | OS.changeColor(savedColor, true); |
Chris Lattner | 1958677 | 2009-01-30 17:41:53 +0000 | [diff] [blame] | 633 | OS << PLoc.getFilename() << ':' << LineNo << ':'; |
Chris Lattner | 11a2a43 | 2009-02-17 07:34:34 +0000 | [diff] [blame] | 634 | if (ShowColumn) |
| 635 | if (unsigned ColNo = PLoc.getColumn()) |
| 636 | OS << ColNo << ':'; |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 637 | |
Chris Lattner | 44219f3 | 2009-03-13 01:08:23 +0000 | [diff] [blame] | 638 | if (PrintRangeInfo && Info.getNumRanges()) { |
| 639 | FileID CaretFileID = |
| 640 | SM.getFileID(SM.getInstantiationLoc(Info.getLocation())); |
| 641 | bool PrintedRange = false; |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 642 | |
Chris Lattner | 44219f3 | 2009-03-13 01:08:23 +0000 | [diff] [blame] | 643 | for (unsigned i = 0, e = Info.getNumRanges(); i != e; ++i) { |
Chris Lattner | 3251e3c | 2009-04-19 22:24:10 +0000 | [diff] [blame] | 644 | // Ignore invalid ranges. |
| 645 | if (!Info.getRange(i).isValid()) continue; |
| 646 | |
Chris Lattner | 44219f3 | 2009-03-13 01:08:23 +0000 | [diff] [blame] | 647 | SourceLocation B = Info.getRange(i).getBegin(); |
| 648 | SourceLocation E = Info.getRange(i).getEnd(); |
Chris Lattner | 6ed7d59 | 2009-06-15 05:18:27 +0000 | [diff] [blame] | 649 | B = SM.getInstantiationLoc(B); |
Chris Lattner | 44219f3 | 2009-03-13 01:08:23 +0000 | [diff] [blame] | 650 | E = SM.getInstantiationLoc(E); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 651 | |
Chris Lattner | 6ed7d59 | 2009-06-15 05:18:27 +0000 | [diff] [blame] | 652 | // If the End location and the start location are the same and are a |
| 653 | // macro location, then the range was something that came from a macro |
| 654 | // expansion or _Pragma. If this is an object-like macro, the best we |
| 655 | // can do is to highlight the range. If this is a function-like |
| 656 | // macro, we'd also like to highlight the arguments. |
| 657 | if (B == E && Info.getRange(i).getEnd().isMacroID()) |
| 658 | E = SM.getInstantiationRange(Info.getRange(i).getEnd()).second; |
| 659 | |
| 660 | std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(B); |
Chris Lattner | 44219f3 | 2009-03-13 01:08:23 +0000 | [diff] [blame] | 661 | std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 662 | |
Chris Lattner | 44219f3 | 2009-03-13 01:08:23 +0000 | [diff] [blame] | 663 | // If the start or end of the range is in another file, just discard |
| 664 | // it. |
| 665 | if (BInfo.first != CaretFileID || EInfo.first != CaretFileID) |
| 666 | continue; |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 667 | |
Chris Lattner | 44219f3 | 2009-03-13 01:08:23 +0000 | [diff] [blame] | 668 | // Add in the length of the token, so that we cover multi-char tokens. |
Chris Lattner | 184e65d | 2009-04-14 23:22:57 +0000 | [diff] [blame] | 669 | unsigned TokSize = Lexer::MeasureTokenLength(E, SM, *LangOpts); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 670 | |
Chris Lattner | 44219f3 | 2009-03-13 01:08:23 +0000 | [diff] [blame] | 671 | OS << '{' << SM.getLineNumber(BInfo.first, BInfo.second) << ':' |
| 672 | << SM.getColumnNumber(BInfo.first, BInfo.second) << '-' |
| 673 | << SM.getLineNumber(EInfo.first, EInfo.second) << ':' |
| 674 | << (SM.getColumnNumber(EInfo.first, EInfo.second)+TokSize) << '}'; |
| 675 | PrintedRange = true; |
| 676 | } |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 677 | |
Chris Lattner | 44219f3 | 2009-03-13 01:08:23 +0000 | [diff] [blame] | 678 | if (PrintedRange) |
| 679 | OS << ':'; |
| 680 | } |
Chris Lattner | 1958677 | 2009-01-30 17:41:53 +0000 | [diff] [blame] | 681 | OS << ' '; |
Torok Edwin | c91b6e0 | 2009-06-04 07:18:23 +0000 | [diff] [blame] | 682 | if (UseColors) |
| 683 | OS.resetColor(); |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | if (UseColors) { |
| 688 | // Print diagnostic category in bold and color |
| 689 | switch (Level) { |
| 690 | case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type"); |
| 691 | case Diagnostic::Note: OS.changeColor(noteColor, true); break; |
| 692 | case Diagnostic::Warning: OS.changeColor(warningColor, true); break; |
| 693 | case Diagnostic::Error: OS.changeColor(errorColor, true); break; |
| 694 | case Diagnostic::Fatal: OS.changeColor(fatalColor, true); break; |
Chris Lattner | 1958677 | 2009-01-30 17:41:53 +0000 | [diff] [blame] | 695 | } |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 696 | } |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 697 | |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 698 | switch (Level) { |
Chris Lattner | b05f49e | 2009-02-06 03:57:44 +0000 | [diff] [blame] | 699 | case Diagnostic::Ignored: assert(0 && "Invalid diagnostic type"); |
Nate Begeman | 36c4927 | 2008-04-17 18:06:57 +0000 | [diff] [blame] | 700 | case Diagnostic::Note: OS << "note: "; break; |
| 701 | case Diagnostic::Warning: OS << "warning: "; break; |
| 702 | case Diagnostic::Error: OS << "error: "; break; |
Chris Lattner | b05f49e | 2009-02-06 03:57:44 +0000 | [diff] [blame] | 703 | case Diagnostic::Fatal: OS << "fatal error: "; break; |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 704 | } |
Torok Edwin | c91b6e0 | 2009-06-04 07:18:23 +0000 | [diff] [blame] | 705 | |
| 706 | if (UseColors) |
| 707 | OS.resetColor(); |
| 708 | |
Chris Lattner | 23be067 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 709 | llvm::SmallString<100> OutStr; |
| 710 | Info.FormatDiagnostic(OutStr); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 711 | |
Chris Lattner | 22cb818 | 2009-04-16 05:44:38 +0000 | [diff] [blame] | 712 | if (PrintDiagnosticOption) |
Douglas Gregor | 4818553 | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 713 | if (const char *Opt = Diagnostic::getWarningOptionForDiag(Info.getID())) { |
| 714 | OutStr += " [-W"; |
| 715 | OutStr += Opt; |
| 716 | OutStr += ']'; |
| 717 | } |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 718 | |
Torok Edwin | c91b6e0 | 2009-06-04 07:18:23 +0000 | [diff] [blame] | 719 | if (UseColors) { |
| 720 | // Print warnings, errors and fatal errors in bold, no color |
| 721 | switch (Level) { |
| 722 | case Diagnostic::Warning: OS.changeColor(savedColor, true); break; |
| 723 | case Diagnostic::Error: OS.changeColor(savedColor, true); break; |
| 724 | case Diagnostic::Fatal: OS.changeColor(savedColor, true); break; |
| 725 | default: break; //don't bold notes |
| 726 | } |
| 727 | } |
| 728 | |
Douglas Gregor | 4818553 | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 729 | if (MessageLength) { |
| 730 | // We will be word-wrapping the error message, so compute the |
| 731 | // column number where we currently are (after printing the |
| 732 | // location information). |
| 733 | unsigned Column = OS.tell() - StartOfLocationInfo; |
Douglas Gregor | 080fe61 | 2009-05-06 04:43:47 +0000 | [diff] [blame] | 734 | PrintWordWrapped(OS, OutStr, MessageLength, Column); |
Douglas Gregor | 4818553 | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 735 | } else { |
| 736 | OS.write(OutStr.begin(), OutStr.size()); |
| 737 | } |
Chris Lattner | 23be067 | 2008-11-19 06:51:40 +0000 | [diff] [blame] | 738 | OS << '\n'; |
Torok Edwin | c91b6e0 | 2009-06-04 07:18:23 +0000 | [diff] [blame] | 739 | if (UseColors) |
| 740 | OS.resetColor(); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 741 | |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 742 | // If caret diagnostics are enabled and we have location, we want to |
| 743 | // emit the caret. However, we only do this if the location moved |
| 744 | // from the last diagnostic, if the last diagnostic was a note that |
| 745 | // was part of a different warning or error diagnostic, or if the |
| 746 | // diagnostic has ranges. We don't want to emit the same caret |
| 747 | // multiple times if one loc has multiple diagnostics. |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 748 | if (CaretDiagnostics && Info.getLocation().isValid() && |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 749 | ((LastLoc != Info.getLocation()) || Info.getNumRanges() || |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 750 | (LastCaretDiagnosticWasNote && Level != Diagnostic::Note) || |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 751 | Info.getNumCodeModificationHints())) { |
Steve Naroff | a3699224 | 2008-02-08 22:06:17 +0000 | [diff] [blame] | 752 | // Cache the LastLoc, it allows us to omit duplicate source/caret spewage. |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 753 | LastLoc = Info.getLocation(); |
Douglas Gregor | 79cf603 | 2009-03-10 20:44:00 +0000 | [diff] [blame] | 754 | LastCaretDiagnosticWasNote = (Level == Diagnostic::Note); |
Chris Lattner | f1ca7d3 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 755 | |
Chris Lattner | 1973d84 | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 756 | // Get the ranges into a local array we can hack on. |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 757 | SourceRange Ranges[20]; |
Chris Lattner | 1973d84 | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 758 | unsigned NumRanges = Info.getNumRanges(); |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 759 | assert(NumRanges < 20 && "Out of space"); |
Chris Lattner | 1973d84 | 2009-02-20 00:18:51 +0000 | [diff] [blame] | 760 | for (unsigned i = 0; i != NumRanges; ++i) |
| 761 | Ranges[i] = Info.getRange(i); |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 762 | |
Douglas Gregor | 87f95b0 | 2009-02-26 21:00:50 +0000 | [diff] [blame] | 763 | unsigned NumHints = Info.getNumCodeModificationHints(); |
| 764 | for (unsigned idx = 0; idx < NumHints; ++idx) { |
| 765 | const CodeModificationHint &Hint = Info.getCodeModificationHint(idx); |
| 766 | if (Hint.RemoveRange.isValid()) { |
| 767 | assert(NumRanges < 20 && "Out of space"); |
| 768 | Ranges[NumRanges++] = Hint.RemoveRange; |
| 769 | } |
| 770 | } |
| 771 | |
| 772 | EmitCaretDiagnostic(LastLoc, Ranges, NumRanges, LastLoc.getManager(), |
| 773 | Info.getCodeModificationHints(), |
Douglas Gregor | 4818553 | 2009-05-01 21:53:04 +0000 | [diff] [blame] | 774 | Info.getNumCodeModificationHints(), |
Douglas Gregor | cf7b2af | 2009-05-01 23:32:58 +0000 | [diff] [blame] | 775 | MessageLength); |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 776 | } |
Daniel Dunbar | 49f0e80 | 2009-09-07 23:07:56 +0000 | [diff] [blame] | 777 | |
Chris Lattner | 327984f | 2008-11-19 06:56:25 +0000 | [diff] [blame] | 778 | OS.flush(); |
Bill Wendling | 37b1dde | 2007-06-07 09:34:54 +0000 | [diff] [blame] | 779 | } |