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