Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1 | //===--- TextDiagnostic.cpp - Text Diagnostic Pretty-Printing -------------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | |
| 10 | #include "clang/Frontend/TextDiagnostic.h" |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 11 | #include "clang/Basic/DiagnosticOptions.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 12 | #include "clang/Basic/FileManager.h" |
| 13 | #include "clang/Basic/SourceManager.h" |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 14 | #include "clang/Lex/Lexer.h" |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 15 | #include "llvm/ADT/SmallString.h" |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/StringExtras.h" |
Dmitri Gribenko | cb5620c | 2013-01-30 12:06:08 +0000 | [diff] [blame] | 17 | #include "llvm/Support/ConvertUTF.h" |
Chandler Carruth | 55fc873 | 2012-12-04 09:13:33 +0000 | [diff] [blame] | 18 | #include "llvm/Support/ErrorHandling.h" |
| 19 | #include "llvm/Support/Locale.h" |
| 20 | #include "llvm/Support/MemoryBuffer.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 22 | #include <algorithm> |
Joerg Sonnenberger | 7094dee | 2012-08-10 10:58:18 +0000 | [diff] [blame] | 23 | #include <cctype> |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 24 | |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 25 | using namespace clang; |
| 26 | |
| 27 | static const enum raw_ostream::Colors noteColor = |
| 28 | raw_ostream::BLACK; |
| 29 | static const enum raw_ostream::Colors fixitColor = |
| 30 | raw_ostream::GREEN; |
| 31 | static const enum raw_ostream::Colors caretColor = |
| 32 | raw_ostream::GREEN; |
| 33 | static const enum raw_ostream::Colors warningColor = |
| 34 | raw_ostream::MAGENTA; |
Richard Trieu | 246b6aa | 2012-06-26 18:18:47 +0000 | [diff] [blame] | 35 | static const enum raw_ostream::Colors templateColor = |
| 36 | raw_ostream::CYAN; |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 37 | static const enum raw_ostream::Colors errorColor = raw_ostream::RED; |
| 38 | static const enum raw_ostream::Colors fatalColor = raw_ostream::RED; |
| 39 | // Used for changing only the bold attribute. |
| 40 | static const enum raw_ostream::Colors savedColor = |
| 41 | raw_ostream::SAVEDCOLOR; |
| 42 | |
Richard Trieu | 246b6aa | 2012-06-26 18:18:47 +0000 | [diff] [blame] | 43 | /// \brief Add highlights to differences in template strings. |
| 44 | static void applyTemplateHighlighting(raw_ostream &OS, StringRef Str, |
Richard Trieu | b956e5a | 2012-06-28 22:39:03 +0000 | [diff] [blame] | 45 | bool &Normal, bool Bold) { |
Benjamin Kramer | 4ca3abd | 2012-10-18 20:09:54 +0000 | [diff] [blame] | 46 | while (1) { |
| 47 | size_t Pos = Str.find(ToggleHighlight); |
| 48 | OS << Str.slice(0, Pos); |
| 49 | if (Pos == StringRef::npos) |
| 50 | break; |
| 51 | |
| 52 | Str = Str.substr(Pos + 1); |
| 53 | if (Normal) |
| 54 | OS.changeColor(templateColor, true); |
| 55 | else { |
| 56 | OS.resetColor(); |
| 57 | if (Bold) |
| 58 | OS.changeColor(savedColor, true); |
Richard Trieu | 246b6aa | 2012-06-26 18:18:47 +0000 | [diff] [blame] | 59 | } |
Benjamin Kramer | 4ca3abd | 2012-10-18 20:09:54 +0000 | [diff] [blame] | 60 | Normal = !Normal; |
| 61 | } |
Richard Trieu | 246b6aa | 2012-06-26 18:18:47 +0000 | [diff] [blame] | 62 | } |
| 63 | |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 64 | /// \brief Number of spaces to indent when word-wrapping. |
| 65 | const unsigned WordWrapIndentation = 6; |
| 66 | |
Benjamin Kramer | d1fda03 | 2012-05-01 14:34:11 +0000 | [diff] [blame] | 67 | static int bytesSincePreviousTabOrLineBegin(StringRef SourceLine, size_t i) { |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 68 | int bytes = 0; |
| 69 | while (0<i) { |
| 70 | if (SourceLine[--i]=='\t') |
| 71 | break; |
| 72 | ++bytes; |
| 73 | } |
| 74 | return bytes; |
| 75 | } |
| 76 | |
| 77 | /// \brief returns a printable representation of first item from input range |
| 78 | /// |
| 79 | /// This function returns a printable representation of the next item in a line |
| 80 | /// of source. If the next byte begins a valid and printable character, that |
| 81 | /// character is returned along with 'true'. |
| 82 | /// |
| 83 | /// Otherwise, if the next byte begins a valid, but unprintable character, a |
| 84 | /// printable, escaped representation of the character is returned, along with |
| 85 | /// 'false'. Otherwise a printable, escaped representation of the next byte |
| 86 | /// is returned along with 'false'. |
| 87 | /// |
| 88 | /// \note The index is updated to be used with a subsequent call to |
| 89 | /// printableTextForNextCharacter. |
| 90 | /// |
| 91 | /// \param SourceLine The line of source |
| 92 | /// \param i Pointer to byte index, |
| 93 | /// \param TabStop used to expand tabs |
Sylvestre Ledru | f3477c1 | 2012-09-27 10:16:10 +0000 | [diff] [blame] | 94 | /// \return pair(printable text, 'true' iff original text was printable) |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 95 | /// |
Benjamin Kramer | d1fda03 | 2012-05-01 14:34:11 +0000 | [diff] [blame] | 96 | static std::pair<SmallString<16>, bool> |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 97 | printableTextForNextCharacter(StringRef SourceLine, size_t *i, |
| 98 | unsigned TabStop) { |
| 99 | assert(i && "i must not be null"); |
| 100 | assert(*i<SourceLine.size() && "must point to a valid index"); |
| 101 | |
| 102 | if (SourceLine[*i]=='\t') { |
| 103 | assert(0 < TabStop && TabStop <= DiagnosticOptions::MaxTabStop && |
| 104 | "Invalid -ftabstop value"); |
| 105 | unsigned col = bytesSincePreviousTabOrLineBegin(SourceLine, *i); |
| 106 | unsigned NumSpaces = TabStop - col%TabStop; |
| 107 | assert(0 < NumSpaces && NumSpaces <= TabStop |
| 108 | && "Invalid computation of space amt"); |
| 109 | ++(*i); |
| 110 | |
| 111 | SmallString<16> expandedTab; |
| 112 | expandedTab.assign(NumSpaces, ' '); |
| 113 | return std::make_pair(expandedTab, true); |
| 114 | } |
| 115 | |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 116 | unsigned char const *begin, *end; |
| 117 | begin = reinterpret_cast<unsigned char const *>(&*(SourceLine.begin() + *i)); |
Seth Cantrell | c6a2f6e | 2012-10-30 06:13:50 +0000 | [diff] [blame] | 118 | end = begin + (SourceLine.size() - *i); |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 119 | |
| 120 | if (isLegalUTF8Sequence(begin, end)) { |
| 121 | UTF32 c; |
| 122 | UTF32 *cptr = &c; |
| 123 | unsigned char const *original_begin = begin; |
Seth Cantrell | 0d1e645 | 2012-10-30 06:13:52 +0000 | [diff] [blame] | 124 | unsigned char const *cp_end = begin+getNumBytesForUTF8(SourceLine[*i]); |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 125 | |
| 126 | ConversionResult res = ConvertUTF8toUTF32(&begin, cp_end, &cptr, cptr+1, |
| 127 | strictConversion); |
Matt Beaumont-Gay | 0ddb097 | 2012-04-18 17:25:16 +0000 | [diff] [blame] | 128 | (void)res; |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 129 | assert(conversionOK==res); |
| 130 | assert(0 < begin-original_begin |
| 131 | && "we must be further along in the string now"); |
| 132 | *i += begin-original_begin; |
| 133 | |
| 134 | if (!llvm::sys::locale::isPrint(c)) { |
| 135 | // If next character is valid UTF-8, but not printable |
| 136 | SmallString<16> expandedCP("<U+>"); |
| 137 | while (c) { |
| 138 | expandedCP.insert(expandedCP.begin()+3, llvm::hexdigit(c%16)); |
| 139 | c/=16; |
| 140 | } |
| 141 | while (expandedCP.size() < 8) |
| 142 | expandedCP.insert(expandedCP.begin()+3, llvm::hexdigit(0)); |
| 143 | return std::make_pair(expandedCP, false); |
| 144 | } |
| 145 | |
| 146 | // If next character is valid UTF-8, and printable |
| 147 | return std::make_pair(SmallString<16>(original_begin, cp_end), true); |
| 148 | |
| 149 | } |
| 150 | |
| 151 | // If next byte is not valid UTF-8 (and therefore not printable) |
| 152 | SmallString<16> expandedByte("<XX>"); |
| 153 | unsigned char byte = SourceLine[*i]; |
| 154 | expandedByte[1] = llvm::hexdigit(byte / 16); |
| 155 | expandedByte[2] = llvm::hexdigit(byte % 16); |
| 156 | ++(*i); |
| 157 | return std::make_pair(expandedByte, false); |
| 158 | } |
| 159 | |
Benjamin Kramer | d1fda03 | 2012-05-01 14:34:11 +0000 | [diff] [blame] | 160 | static void expandTabs(std::string &SourceLine, unsigned TabStop) { |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 161 | size_t i = SourceLine.size(); |
| 162 | while (i>0) { |
| 163 | i--; |
| 164 | if (SourceLine[i]!='\t') |
| 165 | continue; |
| 166 | size_t tmp_i = i; |
| 167 | std::pair<SmallString<16>,bool> res |
| 168 | = printableTextForNextCharacter(SourceLine, &tmp_i, TabStop); |
| 169 | SourceLine.replace(i, 1, res.first.c_str()); |
| 170 | } |
| 171 | } |
| 172 | |
| 173 | /// This function takes a raw source line and produces a mapping from the bytes |
| 174 | /// of the printable representation of the line to the columns those printable |
| 175 | /// characters will appear at (numbering the first column as 0). |
| 176 | /// |
| 177 | /// If a byte 'i' corresponds to muliple columns (e.g. the byte contains a tab |
Sylvestre Ledru | bed28ac | 2012-07-23 08:59:39 +0000 | [diff] [blame] | 178 | /// character) then the array will map that byte to the first column the |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 179 | /// tab appears at and the next value in the map will have been incremented |
| 180 | /// more than once. |
| 181 | /// |
| 182 | /// If a byte is the first in a sequence of bytes that together map to a single |
| 183 | /// entity in the output, then the array will map that byte to the appropriate |
| 184 | /// column while the subsequent bytes will be -1. |
| 185 | /// |
| 186 | /// The last element in the array does not correspond to any byte in the input |
| 187 | /// and instead is the number of columns needed to display the source |
| 188 | /// |
| 189 | /// example: (given a tabstop of 8) |
| 190 | /// |
| 191 | /// "a \t \u3042" -> {0,1,2,8,9,-1,-1,11} |
| 192 | /// |
James Dennett | 6b4f506 | 2012-06-22 05:33:23 +0000 | [diff] [blame] | 193 | /// (\\u3042 is represented in UTF-8 by three bytes and takes two columns to |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 194 | /// display) |
Benjamin Kramer | d1fda03 | 2012-05-01 14:34:11 +0000 | [diff] [blame] | 195 | static void byteToColumn(StringRef SourceLine, unsigned TabStop, |
| 196 | SmallVectorImpl<int> &out) { |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 197 | out.clear(); |
| 198 | |
| 199 | if (SourceLine.empty()) { |
| 200 | out.resize(1u,0); |
| 201 | return; |
| 202 | } |
| 203 | |
| 204 | out.resize(SourceLine.size()+1, -1); |
| 205 | |
| 206 | int columns = 0; |
| 207 | size_t i = 0; |
| 208 | while (i<SourceLine.size()) { |
| 209 | out[i] = columns; |
| 210 | std::pair<SmallString<16>,bool> res |
| 211 | = printableTextForNextCharacter(SourceLine, &i, TabStop); |
| 212 | columns += llvm::sys::locale::columnWidth(res.first); |
| 213 | } |
| 214 | out.back() = columns; |
| 215 | } |
| 216 | |
| 217 | /// This function takes a raw source line and produces a mapping from columns |
| 218 | /// to the byte of the source line that produced the character displaying at |
| 219 | /// that column. This is the inverse of the mapping produced by byteToColumn() |
| 220 | /// |
| 221 | /// The last element in the array is the number of bytes in the source string |
| 222 | /// |
| 223 | /// example: (given a tabstop of 8) |
| 224 | /// |
| 225 | /// "a \t \u3042" -> {0,1,2,-1,-1,-1,-1,-1,3,4,-1,7} |
| 226 | /// |
James Dennett | 6b4f506 | 2012-06-22 05:33:23 +0000 | [diff] [blame] | 227 | /// (\\u3042 is represented in UTF-8 by three bytes and takes two columns to |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 228 | /// display) |
Benjamin Kramer | d1fda03 | 2012-05-01 14:34:11 +0000 | [diff] [blame] | 229 | static void columnToByte(StringRef SourceLine, unsigned TabStop, |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 230 | SmallVectorImpl<int> &out) { |
| 231 | out.clear(); |
| 232 | |
| 233 | if (SourceLine.empty()) { |
| 234 | out.resize(1u, 0); |
| 235 | return; |
| 236 | } |
| 237 | |
| 238 | int columns = 0; |
| 239 | size_t i = 0; |
| 240 | while (i<SourceLine.size()) { |
| 241 | out.resize(columns+1, -1); |
| 242 | out.back() = i; |
| 243 | std::pair<SmallString<16>,bool> res |
| 244 | = printableTextForNextCharacter(SourceLine, &i, TabStop); |
| 245 | columns += llvm::sys::locale::columnWidth(res.first); |
| 246 | } |
| 247 | out.resize(columns+1, -1); |
| 248 | out.back() = i; |
| 249 | } |
| 250 | |
Benjamin Kramer | c2b914f | 2012-12-01 20:58:01 +0000 | [diff] [blame] | 251 | namespace { |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 252 | struct SourceColumnMap { |
| 253 | SourceColumnMap(StringRef SourceLine, unsigned TabStop) |
| 254 | : m_SourceLine(SourceLine) { |
| 255 | |
| 256 | ::byteToColumn(SourceLine, TabStop, m_byteToColumn); |
| 257 | ::columnToByte(SourceLine, TabStop, m_columnToByte); |
| 258 | |
| 259 | assert(m_byteToColumn.size()==SourceLine.size()+1); |
| 260 | assert(0 < m_byteToColumn.size() && 0 < m_columnToByte.size()); |
| 261 | assert(m_byteToColumn.size() |
| 262 | == static_cast<unsigned>(m_columnToByte.back()+1)); |
| 263 | assert(static_cast<unsigned>(m_byteToColumn.back()+1) |
| 264 | == m_columnToByte.size()); |
| 265 | } |
| 266 | int columns() const { return m_byteToColumn.back(); } |
| 267 | int bytes() const { return m_columnToByte.back(); } |
Richard Smith | c7bb384 | 2012-09-13 18:37:50 +0000 | [diff] [blame] | 268 | |
| 269 | /// \brief Map a byte to the column which it is at the start of, or return -1 |
| 270 | /// if it is not at the start of a column (for a UTF-8 trailing byte). |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 271 | int byteToColumn(int n) const { |
| 272 | assert(0<=n && n<static_cast<int>(m_byteToColumn.size())); |
| 273 | return m_byteToColumn[n]; |
| 274 | } |
Richard Smith | c7bb384 | 2012-09-13 18:37:50 +0000 | [diff] [blame] | 275 | |
| 276 | /// \brief Map a byte to the first column which contains it. |
| 277 | int byteToContainingColumn(int N) const { |
| 278 | assert(0 <= N && N < static_cast<int>(m_byteToColumn.size())); |
| 279 | while (m_byteToColumn[N] == -1) |
| 280 | --N; |
| 281 | return m_byteToColumn[N]; |
| 282 | } |
| 283 | |
| 284 | /// \brief Map a column to the byte which starts the column, or return -1 if |
| 285 | /// the column the second or subsequent column of an expanded tab or similar |
| 286 | /// multi-column entity. |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 287 | int columnToByte(int n) const { |
| 288 | assert(0<=n && n<static_cast<int>(m_columnToByte.size())); |
| 289 | return m_columnToByte[n]; |
| 290 | } |
Richard Smith | c7bb384 | 2012-09-13 18:37:50 +0000 | [diff] [blame] | 291 | |
| 292 | /// \brief Map from a byte index to the next byte which starts a column. |
| 293 | int startOfNextColumn(int N) const { |
| 294 | assert(0 <= N && N < static_cast<int>(m_columnToByte.size() - 1)); |
| 295 | while (byteToColumn(++N) == -1) {} |
| 296 | return N; |
| 297 | } |
| 298 | |
| 299 | /// \brief Map from a byte index to the previous byte which starts a column. |
| 300 | int startOfPreviousColumn(int N) const { |
| 301 | assert(0 < N && N < static_cast<int>(m_columnToByte.size())); |
Seth Cantrell | eaa5a2b | 2012-11-03 21:21:14 +0000 | [diff] [blame] | 302 | while (byteToColumn(--N) == -1) {} |
Richard Smith | c7bb384 | 2012-09-13 18:37:50 +0000 | [diff] [blame] | 303 | return N; |
| 304 | } |
| 305 | |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 306 | StringRef getSourceLine() const { |
| 307 | return m_SourceLine; |
| 308 | } |
| 309 | |
| 310 | private: |
| 311 | const std::string m_SourceLine; |
| 312 | SmallVector<int,200> m_byteToColumn; |
| 313 | SmallVector<int,200> m_columnToByte; |
| 314 | }; |
| 315 | |
| 316 | // used in assert in selectInterestingSourceRegion() |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 317 | struct char_out_of_range { |
| 318 | const char lower,upper; |
| 319 | char_out_of_range(char lower, char upper) : |
| 320 | lower(lower), upper(upper) {} |
| 321 | bool operator()(char c) { return c < lower || upper < c; } |
| 322 | }; |
Benjamin Kramer | c2b914f | 2012-12-01 20:58:01 +0000 | [diff] [blame] | 323 | } // end anonymous namespace |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 324 | |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 325 | /// \brief When the source code line we want to print is too long for |
| 326 | /// the terminal, select the "interesting" region. |
Chandler Carruth | 7531f57 | 2011-10-15 23:54:09 +0000 | [diff] [blame] | 327 | static void selectInterestingSourceRegion(std::string &SourceLine, |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 328 | std::string &CaretLine, |
| 329 | std::string &FixItInsertionLine, |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 330 | unsigned Columns, |
| 331 | const SourceColumnMap &map) { |
| 332 | unsigned MaxColumns = std::max<unsigned>(map.columns(), |
| 333 | std::max(CaretLine.size(), |
| 334 | FixItInsertionLine.size())); |
| 335 | // if the number of columns is less than the desired number we're done |
| 336 | if (MaxColumns <= Columns) |
| 337 | return; |
| 338 | |
| 339 | // no special characters allowed in CaretLine or FixItInsertionLine |
| 340 | assert(CaretLine.end() == |
| 341 | std::find_if(CaretLine.begin(), CaretLine.end(), |
| 342 | char_out_of_range(' ','~'))); |
| 343 | assert(FixItInsertionLine.end() == |
| 344 | std::find_if(FixItInsertionLine.begin(), FixItInsertionLine.end(), |
| 345 | char_out_of_range(' ','~'))); |
| 346 | |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 347 | // Find the slice that we need to display the full caret line |
| 348 | // correctly. |
| 349 | unsigned CaretStart = 0, CaretEnd = CaretLine.size(); |
| 350 | for (; CaretStart != CaretEnd; ++CaretStart) |
Seth Cantrell | 4031a37 | 2012-05-25 00:03:29 +0000 | [diff] [blame] | 351 | if (!isspace(static_cast<unsigned char>(CaretLine[CaretStart]))) |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 352 | break; |
| 353 | |
| 354 | for (; CaretEnd != CaretStart; --CaretEnd) |
Seth Cantrell | 4031a37 | 2012-05-25 00:03:29 +0000 | [diff] [blame] | 355 | if (!isspace(static_cast<unsigned char>(CaretLine[CaretEnd - 1]))) |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 356 | break; |
| 357 | |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 358 | // caret has already been inserted into CaretLine so the above whitespace |
| 359 | // check is guaranteed to include the caret |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 360 | |
| 361 | // If we have a fix-it line, make sure the slice includes all of the |
| 362 | // fix-it information. |
| 363 | if (!FixItInsertionLine.empty()) { |
| 364 | unsigned FixItStart = 0, FixItEnd = FixItInsertionLine.size(); |
| 365 | for (; FixItStart != FixItEnd; ++FixItStart) |
Seth Cantrell | 4031a37 | 2012-05-25 00:03:29 +0000 | [diff] [blame] | 366 | if (!isspace(static_cast<unsigned char>(FixItInsertionLine[FixItStart]))) |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 367 | break; |
| 368 | |
| 369 | for (; FixItEnd != FixItStart; --FixItEnd) |
Seth Cantrell | 4031a37 | 2012-05-25 00:03:29 +0000 | [diff] [blame] | 370 | if (!isspace(static_cast<unsigned char>(FixItInsertionLine[FixItEnd - 1]))) |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 371 | break; |
| 372 | |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 373 | CaretStart = std::min(FixItStart, CaretStart); |
| 374 | CaretEnd = std::max(FixItEnd, CaretEnd); |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 375 | } |
| 376 | |
Seth Cantrell | c5493d7 | 2012-05-24 05:14:44 +0000 | [diff] [blame] | 377 | // CaretEnd may have been set at the middle of a character |
| 378 | // If it's not at a character's first column then advance it past the current |
| 379 | // character. |
| 380 | while (static_cast<int>(CaretEnd) < map.columns() && |
| 381 | -1 == map.columnToByte(CaretEnd)) |
| 382 | ++CaretEnd; |
| 383 | |
| 384 | assert((static_cast<int>(CaretStart) > map.columns() || |
| 385 | -1!=map.columnToByte(CaretStart)) && |
| 386 | "CaretStart must not point to a column in the middle of a source" |
| 387 | " line character"); |
| 388 | assert((static_cast<int>(CaretEnd) > map.columns() || |
| 389 | -1!=map.columnToByte(CaretEnd)) && |
| 390 | "CaretEnd must not point to a column in the middle of a source line" |
| 391 | " character"); |
| 392 | |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 393 | // CaretLine[CaretStart, CaretEnd) contains all of the interesting |
| 394 | // parts of the caret line. While this slice is smaller than the |
| 395 | // number of columns we have, try to grow the slice to encompass |
| 396 | // more context. |
| 397 | |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 398 | unsigned SourceStart = map.columnToByte(std::min<unsigned>(CaretStart, |
| 399 | map.columns())); |
| 400 | unsigned SourceEnd = map.columnToByte(std::min<unsigned>(CaretEnd, |
| 401 | map.columns())); |
| 402 | |
| 403 | unsigned CaretColumnsOutsideSource = CaretEnd-CaretStart |
| 404 | - (map.byteToColumn(SourceEnd)-map.byteToColumn(SourceStart)); |
| 405 | |
| 406 | char const *front_ellipse = " ..."; |
| 407 | char const *front_space = " "; |
| 408 | char const *back_ellipse = "..."; |
| 409 | unsigned ellipses_space = strlen(front_ellipse) + strlen(back_ellipse); |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 410 | |
| 411 | unsigned TargetColumns = Columns; |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 412 | // Give us extra room for the ellipses |
| 413 | // and any of the caret line that extends past the source |
| 414 | if (TargetColumns > ellipses_space+CaretColumnsOutsideSource) |
| 415 | TargetColumns -= ellipses_space+CaretColumnsOutsideSource; |
| 416 | |
| 417 | while (SourceStart>0 || SourceEnd<SourceLine.size()) { |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 418 | bool ExpandedRegion = false; |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 419 | |
| 420 | if (SourceStart>0) { |
Seth Cantrell | 9cffb4a | 2012-11-03 21:21:17 +0000 | [diff] [blame] | 421 | unsigned NewStart = map.startOfPreviousColumn(SourceStart); |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 422 | |
| 423 | // Skip over any whitespace we see here; we're looking for |
| 424 | // another bit of interesting text. |
Richard Smith | c7bb384 | 2012-09-13 18:37:50 +0000 | [diff] [blame] | 425 | // FIXME: Detect non-ASCII whitespace characters too. |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 426 | while (NewStart && |
Richard Smith | c7bb384 | 2012-09-13 18:37:50 +0000 | [diff] [blame] | 427 | isspace(static_cast<unsigned char>(SourceLine[NewStart]))) |
| 428 | NewStart = map.startOfPreviousColumn(NewStart); |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 429 | |
| 430 | // Skip over this bit of "interesting" text. |
Richard Smith | c7bb384 | 2012-09-13 18:37:50 +0000 | [diff] [blame] | 431 | while (NewStart) { |
| 432 | unsigned Prev = map.startOfPreviousColumn(NewStart); |
| 433 | if (isspace(static_cast<unsigned char>(SourceLine[Prev]))) |
| 434 | break; |
| 435 | NewStart = Prev; |
| 436 | } |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 437 | |
Richard Smith | c7bb384 | 2012-09-13 18:37:50 +0000 | [diff] [blame] | 438 | assert(map.byteToColumn(NewStart) != -1); |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 439 | unsigned NewColumns = map.byteToColumn(SourceEnd) - |
| 440 | map.byteToColumn(NewStart); |
| 441 | if (NewColumns <= TargetColumns) { |
| 442 | SourceStart = NewStart; |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 443 | ExpandedRegion = true; |
| 444 | } |
| 445 | } |
| 446 | |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 447 | if (SourceEnd<SourceLine.size()) { |
Seth Cantrell | 9cffb4a | 2012-11-03 21:21:17 +0000 | [diff] [blame] | 448 | unsigned NewEnd = map.startOfNextColumn(SourceEnd); |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 449 | |
| 450 | // Skip over any whitespace we see here; we're looking for |
| 451 | // another bit of interesting text. |
Richard Smith | c7bb384 | 2012-09-13 18:37:50 +0000 | [diff] [blame] | 452 | // FIXME: Detect non-ASCII whitespace characters too. |
| 453 | while (NewEnd < SourceLine.size() && |
| 454 | isspace(static_cast<unsigned char>(SourceLine[NewEnd]))) |
| 455 | NewEnd = map.startOfNextColumn(NewEnd); |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 456 | |
| 457 | // Skip over this bit of "interesting" text. |
Richard Smith | c7bb384 | 2012-09-13 18:37:50 +0000 | [diff] [blame] | 458 | while (NewEnd < SourceLine.size() && |
| 459 | !isspace(static_cast<unsigned char>(SourceLine[NewEnd]))) |
| 460 | NewEnd = map.startOfNextColumn(NewEnd); |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 461 | |
Richard Smith | c7bb384 | 2012-09-13 18:37:50 +0000 | [diff] [blame] | 462 | assert(map.byteToColumn(NewEnd) != -1); |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 463 | unsigned NewColumns = map.byteToColumn(NewEnd) - |
| 464 | map.byteToColumn(SourceStart); |
| 465 | if (NewColumns <= TargetColumns) { |
| 466 | SourceEnd = NewEnd; |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 467 | ExpandedRegion = true; |
| 468 | } |
| 469 | } |
| 470 | |
| 471 | if (!ExpandedRegion) |
| 472 | break; |
| 473 | } |
| 474 | |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 475 | CaretStart = map.byteToColumn(SourceStart); |
| 476 | CaretEnd = map.byteToColumn(SourceEnd) + CaretColumnsOutsideSource; |
| 477 | |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 478 | // [CaretStart, CaretEnd) is the slice we want. Update the various |
| 479 | // output lines to show only this slice, with two-space padding |
| 480 | // before the lines so that it looks nicer. |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 481 | |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 482 | assert(CaretStart!=(unsigned)-1 && CaretEnd!=(unsigned)-1 && |
| 483 | SourceStart!=(unsigned)-1 && SourceEnd!=(unsigned)-1); |
| 484 | assert(SourceStart <= SourceEnd); |
| 485 | assert(CaretStart <= CaretEnd); |
| 486 | |
| 487 | unsigned BackColumnsRemoved |
| 488 | = map.byteToColumn(SourceLine.size())-map.byteToColumn(SourceEnd); |
| 489 | unsigned FrontColumnsRemoved = CaretStart; |
| 490 | unsigned ColumnsKept = CaretEnd-CaretStart; |
| 491 | |
| 492 | // We checked up front that the line needed truncation |
| 493 | assert(FrontColumnsRemoved+ColumnsKept+BackColumnsRemoved > Columns); |
| 494 | |
| 495 | // The line needs some trunctiona, and we'd prefer to keep the front |
| 496 | // if possible, so remove the back |
Seth Cantrell | 191db6d | 2012-11-03 23:56:43 +0000 | [diff] [blame] | 497 | if (BackColumnsRemoved > strlen(back_ellipse)) |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 498 | SourceLine.replace(SourceEnd, std::string::npos, back_ellipse); |
| 499 | |
| 500 | // If that's enough then we're done |
| 501 | if (FrontColumnsRemoved+ColumnsKept <= Columns) |
| 502 | return; |
| 503 | |
| 504 | // Otherwise remove the front as well |
Seth Cantrell | 191db6d | 2012-11-03 23:56:43 +0000 | [diff] [blame] | 505 | if (FrontColumnsRemoved > strlen(front_ellipse)) { |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 506 | SourceLine.replace(0, SourceStart, front_ellipse); |
| 507 | CaretLine.replace(0, CaretStart, front_space); |
| 508 | if (!FixItInsertionLine.empty()) |
| 509 | FixItInsertionLine.replace(0, CaretStart, front_space); |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 510 | } |
| 511 | } |
| 512 | |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 513 | /// \brief Skip over whitespace in the string, starting at the given |
| 514 | /// index. |
| 515 | /// |
| 516 | /// \returns The index of the first non-whitespace character that is |
| 517 | /// greater than or equal to Idx or, if no such character exists, |
| 518 | /// returns the end of the string. |
| 519 | static unsigned skipWhitespace(unsigned Idx, StringRef Str, unsigned Length) { |
| 520 | while (Idx < Length && isspace(Str[Idx])) |
| 521 | ++Idx; |
| 522 | return Idx; |
| 523 | } |
| 524 | |
| 525 | /// \brief If the given character is the start of some kind of |
| 526 | /// balanced punctuation (e.g., quotes or parentheses), return the |
| 527 | /// character that will terminate the punctuation. |
| 528 | /// |
| 529 | /// \returns The ending punctuation character, if any, or the NULL |
| 530 | /// character if the input character does not start any punctuation. |
| 531 | static inline char findMatchingPunctuation(char c) { |
| 532 | switch (c) { |
| 533 | case '\'': return '\''; |
| 534 | case '`': return '\''; |
| 535 | case '"': return '"'; |
| 536 | case '(': return ')'; |
| 537 | case '[': return ']'; |
| 538 | case '{': return '}'; |
| 539 | default: break; |
| 540 | } |
| 541 | |
| 542 | return 0; |
| 543 | } |
| 544 | |
| 545 | /// \brief Find the end of the word starting at the given offset |
| 546 | /// within a string. |
| 547 | /// |
| 548 | /// \returns the index pointing one character past the end of the |
| 549 | /// word. |
| 550 | static unsigned findEndOfWord(unsigned Start, StringRef Str, |
| 551 | unsigned Length, unsigned Column, |
| 552 | unsigned Columns) { |
| 553 | assert(Start < Str.size() && "Invalid start position!"); |
| 554 | unsigned End = Start + 1; |
| 555 | |
| 556 | // If we are already at the end of the string, take that as the word. |
| 557 | if (End == Str.size()) |
| 558 | return End; |
| 559 | |
| 560 | // Determine if the start of the string is actually opening |
| 561 | // punctuation, e.g., a quote or parentheses. |
| 562 | char EndPunct = findMatchingPunctuation(Str[Start]); |
| 563 | if (!EndPunct) { |
| 564 | // This is a normal word. Just find the first space character. |
| 565 | while (End < Length && !isspace(Str[End])) |
| 566 | ++End; |
| 567 | return End; |
| 568 | } |
| 569 | |
| 570 | // We have the start of a balanced punctuation sequence (quotes, |
| 571 | // parentheses, etc.). Determine the full sequence is. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 572 | SmallString<16> PunctuationEndStack; |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 573 | PunctuationEndStack.push_back(EndPunct); |
| 574 | while (End < Length && !PunctuationEndStack.empty()) { |
| 575 | if (Str[End] == PunctuationEndStack.back()) |
| 576 | PunctuationEndStack.pop_back(); |
| 577 | else if (char SubEndPunct = findMatchingPunctuation(Str[End])) |
| 578 | PunctuationEndStack.push_back(SubEndPunct); |
| 579 | |
| 580 | ++End; |
| 581 | } |
| 582 | |
| 583 | // Find the first space character after the punctuation ended. |
| 584 | while (End < Length && !isspace(Str[End])) |
| 585 | ++End; |
| 586 | |
| 587 | unsigned PunctWordLength = End - Start; |
| 588 | if (// If the word fits on this line |
| 589 | Column + PunctWordLength <= Columns || |
| 590 | // ... or the word is "short enough" to take up the next line |
| 591 | // without too much ugly white space |
| 592 | PunctWordLength < Columns/3) |
| 593 | return End; // Take the whole thing as a single "word". |
| 594 | |
| 595 | // The whole quoted/parenthesized string is too long to print as a |
| 596 | // single "word". Instead, find the "word" that starts just after |
| 597 | // the punctuation and use that end-point instead. This will recurse |
| 598 | // until it finds something small enough to consider a word. |
| 599 | return findEndOfWord(Start + 1, Str, Length, Column + 1, Columns); |
| 600 | } |
| 601 | |
| 602 | /// \brief Print the given string to a stream, word-wrapping it to |
| 603 | /// some number of columns in the process. |
| 604 | /// |
| 605 | /// \param OS the stream to which the word-wrapping string will be |
| 606 | /// emitted. |
| 607 | /// \param Str the string to word-wrap and output. |
| 608 | /// \param Columns the number of columns to word-wrap to. |
| 609 | /// \param Column the column number at which the first character of \p |
| 610 | /// Str will be printed. This will be non-zero when part of the first |
| 611 | /// line has already been printed. |
Richard Trieu | b956e5a | 2012-06-28 22:39:03 +0000 | [diff] [blame] | 612 | /// \param Bold if the current text should be bold |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 613 | /// \param Indentation the number of spaces to indent any lines beyond |
| 614 | /// the first line. |
| 615 | /// \returns true if word-wrapping was required, or false if the |
| 616 | /// string fit on the first line. |
| 617 | static bool printWordWrapped(raw_ostream &OS, StringRef Str, |
| 618 | unsigned Columns, |
| 619 | unsigned Column = 0, |
Richard Trieu | b956e5a | 2012-06-28 22:39:03 +0000 | [diff] [blame] | 620 | bool Bold = false, |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 621 | unsigned Indentation = WordWrapIndentation) { |
| 622 | const unsigned Length = std::min(Str.find('\n'), Str.size()); |
Richard Trieu | 246b6aa | 2012-06-26 18:18:47 +0000 | [diff] [blame] | 623 | bool TextNormal = true; |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 624 | |
| 625 | // The string used to indent each line. |
Dylan Noblesmith | f7ccbad | 2012-02-05 02:13:05 +0000 | [diff] [blame] | 626 | SmallString<16> IndentStr; |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 627 | IndentStr.assign(Indentation, ' '); |
| 628 | bool Wrapped = false; |
| 629 | for (unsigned WordStart = 0, WordEnd; WordStart < Length; |
| 630 | WordStart = WordEnd) { |
| 631 | // Find the beginning of the next word. |
| 632 | WordStart = skipWhitespace(WordStart, Str, Length); |
| 633 | if (WordStart == Length) |
| 634 | break; |
| 635 | |
| 636 | // Find the end of this word. |
| 637 | WordEnd = findEndOfWord(WordStart, Str, Length, Column, Columns); |
| 638 | |
| 639 | // Does this word fit on the current line? |
| 640 | unsigned WordLength = WordEnd - WordStart; |
| 641 | if (Column + WordLength < Columns) { |
| 642 | // This word fits on the current line; print it there. |
| 643 | if (WordStart) { |
| 644 | OS << ' '; |
| 645 | Column += 1; |
| 646 | } |
Richard Trieu | 246b6aa | 2012-06-26 18:18:47 +0000 | [diff] [blame] | 647 | applyTemplateHighlighting(OS, Str.substr(WordStart, WordLength), |
Richard Trieu | b956e5a | 2012-06-28 22:39:03 +0000 | [diff] [blame] | 648 | TextNormal, Bold); |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 649 | Column += WordLength; |
| 650 | continue; |
| 651 | } |
| 652 | |
| 653 | // This word does not fit on the current line, so wrap to the next |
| 654 | // line. |
| 655 | OS << '\n'; |
| 656 | OS.write(&IndentStr[0], Indentation); |
Richard Trieu | 246b6aa | 2012-06-26 18:18:47 +0000 | [diff] [blame] | 657 | applyTemplateHighlighting(OS, Str.substr(WordStart, WordLength), |
Richard Trieu | b956e5a | 2012-06-28 22:39:03 +0000 | [diff] [blame] | 658 | TextNormal, Bold); |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 659 | Column = Indentation + WordLength; |
| 660 | Wrapped = true; |
| 661 | } |
| 662 | |
| 663 | // Append any remaning text from the message with its existing formatting. |
Richard Trieu | b956e5a | 2012-06-28 22:39:03 +0000 | [diff] [blame] | 664 | applyTemplateHighlighting(OS, Str.substr(Length), TextNormal, Bold); |
Richard Trieu | 246b6aa | 2012-06-26 18:18:47 +0000 | [diff] [blame] | 665 | |
| 666 | assert(TextNormal && "Text highlighted at end of diagnostic message."); |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 667 | |
| 668 | return Wrapped; |
| 669 | } |
| 670 | |
| 671 | TextDiagnostic::TextDiagnostic(raw_ostream &OS, |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 672 | const LangOptions &LangOpts, |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 673 | DiagnosticOptions *DiagOpts) |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 674 | : DiagnosticRenderer(LangOpts, DiagOpts), OS(OS) {} |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 675 | |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 676 | TextDiagnostic::~TextDiagnostic() {} |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 677 | |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 678 | void |
| 679 | TextDiagnostic::emitDiagnosticMessage(SourceLocation Loc, |
| 680 | PresumedLoc PLoc, |
| 681 | DiagnosticsEngine::Level Level, |
| 682 | StringRef Message, |
| 683 | ArrayRef<clang::CharSourceRange> Ranges, |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 684 | const SourceManager *SM, |
Ted Kremenek | 8be51ea | 2012-02-14 02:46:00 +0000 | [diff] [blame] | 685 | DiagOrStoredDiag D) { |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 686 | uint64_t StartOfLocationInfo = OS.tell(); |
| 687 | |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 688 | // Emit the location of this particular diagnostic. |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 689 | if (Loc.isValid()) |
| 690 | emitDiagnosticLoc(Loc, PLoc, Level, Ranges, *SM); |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 691 | |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 692 | if (DiagOpts->ShowColors) |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 693 | OS.resetColor(); |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 694 | |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 695 | printDiagnosticLevel(OS, Level, DiagOpts->ShowColors); |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 696 | printDiagnosticMessage(OS, Level, Message, |
| 697 | OS.tell() - StartOfLocationInfo, |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 698 | DiagOpts->MessageLength, DiagOpts->ShowColors); |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Chandler Carruth | 6ddd887 | 2011-10-15 23:48:02 +0000 | [diff] [blame] | 701 | /*static*/ void |
| 702 | TextDiagnostic::printDiagnosticLevel(raw_ostream &OS, |
| 703 | DiagnosticsEngine::Level Level, |
| 704 | bool ShowColors) { |
| 705 | if (ShowColors) { |
| 706 | // Print diagnostic category in bold and color |
| 707 | switch (Level) { |
| 708 | case DiagnosticsEngine::Ignored: |
| 709 | llvm_unreachable("Invalid diagnostic type"); |
| 710 | case DiagnosticsEngine::Note: OS.changeColor(noteColor, true); break; |
| 711 | case DiagnosticsEngine::Warning: OS.changeColor(warningColor, true); break; |
| 712 | case DiagnosticsEngine::Error: OS.changeColor(errorColor, true); break; |
| 713 | case DiagnosticsEngine::Fatal: OS.changeColor(fatalColor, true); break; |
| 714 | } |
| 715 | } |
| 716 | |
| 717 | switch (Level) { |
| 718 | case DiagnosticsEngine::Ignored: |
| 719 | llvm_unreachable("Invalid diagnostic type"); |
| 720 | case DiagnosticsEngine::Note: OS << "note: "; break; |
| 721 | case DiagnosticsEngine::Warning: OS << "warning: "; break; |
| 722 | case DiagnosticsEngine::Error: OS << "error: "; break; |
| 723 | case DiagnosticsEngine::Fatal: OS << "fatal error: "; break; |
| 724 | } |
| 725 | |
| 726 | if (ShowColors) |
| 727 | OS.resetColor(); |
| 728 | } |
| 729 | |
| 730 | /*static*/ void |
| 731 | TextDiagnostic::printDiagnosticMessage(raw_ostream &OS, |
| 732 | DiagnosticsEngine::Level Level, |
| 733 | StringRef Message, |
| 734 | unsigned CurrentColumn, unsigned Columns, |
| 735 | bool ShowColors) { |
Richard Trieu | b956e5a | 2012-06-28 22:39:03 +0000 | [diff] [blame] | 736 | bool Bold = false; |
Chandler Carruth | 6ddd887 | 2011-10-15 23:48:02 +0000 | [diff] [blame] | 737 | if (ShowColors) { |
| 738 | // Print warnings, errors and fatal errors in bold, no color |
| 739 | switch (Level) { |
Richard Trieu | b956e5a | 2012-06-28 22:39:03 +0000 | [diff] [blame] | 740 | case DiagnosticsEngine::Warning: |
| 741 | case DiagnosticsEngine::Error: |
| 742 | case DiagnosticsEngine::Fatal: |
| 743 | OS.changeColor(savedColor, true); |
| 744 | Bold = true; |
| 745 | break; |
Chandler Carruth | 6ddd887 | 2011-10-15 23:48:02 +0000 | [diff] [blame] | 746 | default: break; //don't bold notes |
| 747 | } |
| 748 | } |
| 749 | |
| 750 | if (Columns) |
Richard Trieu | b956e5a | 2012-06-28 22:39:03 +0000 | [diff] [blame] | 751 | printWordWrapped(OS, Message, Columns, CurrentColumn, Bold); |
David Blaikie | 50badd5 | 2012-06-28 21:46:07 +0000 | [diff] [blame] | 752 | else { |
| 753 | bool Normal = true; |
Richard Trieu | b956e5a | 2012-06-28 22:39:03 +0000 | [diff] [blame] | 754 | applyTemplateHighlighting(OS, Message, Normal, Bold); |
David Blaikie | 50badd5 | 2012-06-28 21:46:07 +0000 | [diff] [blame] | 755 | assert(Normal && "Formatting should have returned to normal"); |
| 756 | } |
Chandler Carruth | 6ddd887 | 2011-10-15 23:48:02 +0000 | [diff] [blame] | 757 | |
| 758 | if (ShowColors) |
| 759 | OS.resetColor(); |
| 760 | OS << '\n'; |
| 761 | } |
| 762 | |
Chandler Carruth | 6ddd887 | 2011-10-15 23:48:02 +0000 | [diff] [blame] | 763 | /// \brief Print out the file/line/column information and include trace. |
| 764 | /// |
| 765 | /// This method handlen the emission of the diagnostic location information. |
| 766 | /// This includes extracting as much location information as is present for |
| 767 | /// the diagnostic and printing it, as well as any include stack or source |
| 768 | /// ranges necessary. |
Chandler Carruth | 7531f57 | 2011-10-15 23:54:09 +0000 | [diff] [blame] | 769 | void TextDiagnostic::emitDiagnosticLoc(SourceLocation Loc, PresumedLoc PLoc, |
Chandler Carruth | 6ddd887 | 2011-10-15 23:48:02 +0000 | [diff] [blame] | 770 | DiagnosticsEngine::Level Level, |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 771 | ArrayRef<CharSourceRange> Ranges, |
| 772 | const SourceManager &SM) { |
Chandler Carruth | 6ddd887 | 2011-10-15 23:48:02 +0000 | [diff] [blame] | 773 | if (PLoc.isInvalid()) { |
| 774 | // At least print the file name if available: |
| 775 | FileID FID = SM.getFileID(Loc); |
| 776 | if (!FID.isInvalid()) { |
| 777 | const FileEntry* FE = SM.getFileEntryForID(FID); |
| 778 | if (FE && FE->getName()) { |
| 779 | OS << FE->getName(); |
| 780 | if (FE->getDevice() == 0 && FE->getInode() == 0 |
| 781 | && FE->getFileMode() == 0) { |
| 782 | // in PCH is a guess, but a good one: |
| 783 | OS << " (in PCH)"; |
| 784 | } |
| 785 | OS << ": "; |
| 786 | } |
| 787 | } |
| 788 | return; |
| 789 | } |
| 790 | unsigned LineNo = PLoc.getLine(); |
| 791 | |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 792 | if (!DiagOpts->ShowLocation) |
Chandler Carruth | 6ddd887 | 2011-10-15 23:48:02 +0000 | [diff] [blame] | 793 | return; |
| 794 | |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 795 | if (DiagOpts->ShowColors) |
Chandler Carruth | 6ddd887 | 2011-10-15 23:48:02 +0000 | [diff] [blame] | 796 | OS.changeColor(savedColor, true); |
| 797 | |
| 798 | OS << PLoc.getFilename(); |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 799 | switch (DiagOpts->getFormat()) { |
Chandler Carruth | 6ddd887 | 2011-10-15 23:48:02 +0000 | [diff] [blame] | 800 | case DiagnosticOptions::Clang: OS << ':' << LineNo; break; |
| 801 | case DiagnosticOptions::Msvc: OS << '(' << LineNo; break; |
| 802 | case DiagnosticOptions::Vi: OS << " +" << LineNo; break; |
| 803 | } |
| 804 | |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 805 | if (DiagOpts->ShowColumn) |
Chandler Carruth | 6ddd887 | 2011-10-15 23:48:02 +0000 | [diff] [blame] | 806 | // Compute the column number. |
| 807 | if (unsigned ColNo = PLoc.getColumn()) { |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 808 | if (DiagOpts->getFormat() == DiagnosticOptions::Msvc) { |
Chandler Carruth | 6ddd887 | 2011-10-15 23:48:02 +0000 | [diff] [blame] | 809 | OS << ','; |
| 810 | ColNo--; |
| 811 | } else |
| 812 | OS << ':'; |
| 813 | OS << ColNo; |
| 814 | } |
Douglas Gregor | dc7b641 | 2012-10-23 23:11:23 +0000 | [diff] [blame] | 815 | switch (DiagOpts->getFormat()) { |
Chandler Carruth | 6ddd887 | 2011-10-15 23:48:02 +0000 | [diff] [blame] | 816 | case DiagnosticOptions::Clang: |
| 817 | case DiagnosticOptions::Vi: OS << ':'; break; |
| 818 | case DiagnosticOptions::Msvc: OS << ") : "; break; |
| 819 | } |
| 820 | |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 821 | if (DiagOpts->ShowSourceRanges && !Ranges.empty()) { |
Chandler Carruth | 6ddd887 | 2011-10-15 23:48:02 +0000 | [diff] [blame] | 822 | FileID CaretFileID = |
| 823 | SM.getFileID(SM.getExpansionLoc(Loc)); |
| 824 | bool PrintedRange = false; |
| 825 | |
| 826 | for (ArrayRef<CharSourceRange>::const_iterator RI = Ranges.begin(), |
| 827 | RE = Ranges.end(); |
| 828 | RI != RE; ++RI) { |
| 829 | // Ignore invalid ranges. |
| 830 | if (!RI->isValid()) continue; |
| 831 | |
| 832 | SourceLocation B = SM.getExpansionLoc(RI->getBegin()); |
| 833 | SourceLocation E = SM.getExpansionLoc(RI->getEnd()); |
| 834 | |
| 835 | // If the End location and the start location are the same and are a |
| 836 | // macro location, then the range was something that came from a |
| 837 | // macro expansion or _Pragma. If this is an object-like macro, the |
| 838 | // best we can do is to highlight the range. If this is a |
| 839 | // function-like macro, we'd also like to highlight the arguments. |
| 840 | if (B == E && RI->getEnd().isMacroID()) |
| 841 | E = SM.getExpansionRange(RI->getEnd()).second; |
| 842 | |
| 843 | std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(B); |
| 844 | std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E); |
| 845 | |
| 846 | // If the start or end of the range is in another file, just discard |
| 847 | // it. |
| 848 | if (BInfo.first != CaretFileID || EInfo.first != CaretFileID) |
| 849 | continue; |
| 850 | |
| 851 | // Add in the length of the token, so that we cover multi-char |
| 852 | // tokens. |
| 853 | unsigned TokSize = 0; |
| 854 | if (RI->isTokenRange()) |
| 855 | TokSize = Lexer::MeasureTokenLength(E, SM, LangOpts); |
| 856 | |
| 857 | OS << '{' << SM.getLineNumber(BInfo.first, BInfo.second) << ':' |
| 858 | << SM.getColumnNumber(BInfo.first, BInfo.second) << '-' |
| 859 | << SM.getLineNumber(EInfo.first, EInfo.second) << ':' |
| 860 | << (SM.getColumnNumber(EInfo.first, EInfo.second)+TokSize) |
| 861 | << '}'; |
| 862 | PrintedRange = true; |
| 863 | } |
| 864 | |
| 865 | if (PrintedRange) |
| 866 | OS << ':'; |
| 867 | } |
| 868 | OS << ' '; |
| 869 | } |
| 870 | |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 871 | void TextDiagnostic::emitBasicNote(StringRef Message) { |
| 872 | // FIXME: Emit this as a real note diagnostic. |
| 873 | // FIXME: Format an actual diagnostic rather than a hard coded string. |
| 874 | OS << "note: " << Message << "\n"; |
| 875 | } |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 876 | |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 877 | void TextDiagnostic::emitIncludeLocation(SourceLocation Loc, |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 878 | PresumedLoc PLoc, |
| 879 | const SourceManager &SM) { |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 880 | if (DiagOpts->ShowLocation) |
Ted Kremenek | 2898d4f | 2011-12-17 05:26:04 +0000 | [diff] [blame] | 881 | OS << "In file included from " << PLoc.getFilename() << ':' |
| 882 | << PLoc.getLine() << ":\n"; |
| 883 | else |
| 884 | OS << "In included file:\n"; |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Douglas Gregor | 6c32543 | 2012-11-30 21:58:49 +0000 | [diff] [blame] | 887 | void TextDiagnostic::emitImportLocation(SourceLocation Loc, PresumedLoc PLoc, |
| 888 | StringRef ModuleName, |
| 889 | const SourceManager &SM) { |
| 890 | if (DiagOpts->ShowLocation) |
| 891 | OS << "In module '" << ModuleName << "' imported from " |
| 892 | << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n"; |
| 893 | else |
| 894 | OS << "In module " << ModuleName << "':\n"; |
| 895 | } |
| 896 | |
Douglas Gregor | 830ea5b | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 897 | void TextDiagnostic::emitBuildingModuleLocation(SourceLocation Loc, |
| 898 | PresumedLoc PLoc, |
| 899 | StringRef ModuleName, |
| 900 | const SourceManager &SM) { |
Douglas Gregor | 813bc7f | 2012-12-18 23:02:07 +0000 | [diff] [blame] | 901 | if (DiagOpts->ShowLocation && PLoc.getFilename()) |
Douglas Gregor | 830ea5b | 2012-11-30 18:38:50 +0000 | [diff] [blame] | 902 | OS << "While building module '" << ModuleName << "' imported from " |
| 903 | << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n"; |
| 904 | else |
| 905 | OS << "While building module '" << ModuleName << "':\n"; |
| 906 | } |
| 907 | |
Benjamin Kramer | c2b914f | 2012-12-01 20:58:01 +0000 | [diff] [blame] | 908 | /// \brief Highlight a SourceRange (with ~'s) for any characters on LineNo. |
| 909 | static void highlightRange(const CharSourceRange &R, |
| 910 | unsigned LineNo, FileID FID, |
| 911 | const SourceColumnMap &map, |
| 912 | std::string &CaretLine, |
| 913 | const SourceManager &SM, |
| 914 | const LangOptions &LangOpts) { |
| 915 | if (!R.isValid()) return; |
| 916 | |
| 917 | SourceLocation Begin = R.getBegin(); |
| 918 | SourceLocation End = R.getEnd(); |
| 919 | |
| 920 | unsigned StartLineNo = SM.getExpansionLineNumber(Begin); |
| 921 | if (StartLineNo > LineNo || SM.getFileID(Begin) != FID) |
| 922 | return; // No intersection. |
| 923 | |
| 924 | unsigned EndLineNo = SM.getExpansionLineNumber(End); |
| 925 | if (EndLineNo < LineNo || SM.getFileID(End) != FID) |
| 926 | return; // No intersection. |
| 927 | |
| 928 | // Compute the column number of the start. |
| 929 | unsigned StartColNo = 0; |
| 930 | if (StartLineNo == LineNo) { |
| 931 | StartColNo = SM.getExpansionColumnNumber(Begin); |
| 932 | if (StartColNo) --StartColNo; // Zero base the col #. |
| 933 | } |
| 934 | |
| 935 | // Compute the column number of the end. |
| 936 | unsigned EndColNo = map.getSourceLine().size(); |
| 937 | if (EndLineNo == LineNo) { |
| 938 | EndColNo = SM.getExpansionColumnNumber(End); |
| 939 | if (EndColNo) { |
| 940 | --EndColNo; // Zero base the col #. |
| 941 | |
| 942 | // Add in the length of the token, so that we cover multi-char tokens if |
| 943 | // this is a token range. |
| 944 | if (R.isTokenRange()) |
| 945 | EndColNo += Lexer::MeasureTokenLength(End, SM, LangOpts); |
| 946 | } else { |
| 947 | EndColNo = CaretLine.size(); |
| 948 | } |
| 949 | } |
| 950 | |
| 951 | assert(StartColNo <= EndColNo && "Invalid range!"); |
| 952 | |
| 953 | // Check that a token range does not highlight only whitespace. |
| 954 | if (R.isTokenRange()) { |
| 955 | // Pick the first non-whitespace column. |
| 956 | while (StartColNo < map.getSourceLine().size() && |
| 957 | (map.getSourceLine()[StartColNo] == ' ' || |
| 958 | map.getSourceLine()[StartColNo] == '\t')) |
| 959 | StartColNo = map.startOfNextColumn(StartColNo); |
| 960 | |
| 961 | // Pick the last non-whitespace column. |
| 962 | if (EndColNo > map.getSourceLine().size()) |
| 963 | EndColNo = map.getSourceLine().size(); |
| 964 | while (EndColNo-1 && |
| 965 | (map.getSourceLine()[EndColNo-1] == ' ' || |
| 966 | map.getSourceLine()[EndColNo-1] == '\t')) |
| 967 | EndColNo = map.startOfPreviousColumn(EndColNo); |
| 968 | |
| 969 | // If the start/end passed each other, then we are trying to highlight a |
| 970 | // range that just exists in whitespace, which must be some sort of other |
| 971 | // bug. |
| 972 | assert(StartColNo <= EndColNo && "Trying to highlight whitespace??"); |
| 973 | } |
| 974 | |
| 975 | assert(StartColNo <= map.getSourceLine().size() && "Invalid range!"); |
| 976 | assert(EndColNo <= map.getSourceLine().size() && "Invalid range!"); |
| 977 | |
| 978 | // Fill the range with ~'s. |
| 979 | StartColNo = map.byteToContainingColumn(StartColNo); |
| 980 | EndColNo = map.byteToContainingColumn(EndColNo); |
| 981 | |
| 982 | assert(StartColNo <= EndColNo && "Invalid range!"); |
| 983 | if (CaretLine.size() < EndColNo) |
| 984 | CaretLine.resize(EndColNo,' '); |
| 985 | std::fill(CaretLine.begin()+StartColNo,CaretLine.begin()+EndColNo,'~'); |
| 986 | } |
| 987 | |
| 988 | static std::string buildFixItInsertionLine(unsigned LineNo, |
| 989 | const SourceColumnMap &map, |
| 990 | ArrayRef<FixItHint> Hints, |
| 991 | const SourceManager &SM, |
| 992 | const DiagnosticOptions *DiagOpts) { |
| 993 | std::string FixItInsertionLine; |
| 994 | if (Hints.empty() || !DiagOpts->ShowFixits) |
| 995 | return FixItInsertionLine; |
| 996 | unsigned PrevHintEndCol = 0; |
| 997 | |
| 998 | for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end(); |
| 999 | I != E; ++I) { |
| 1000 | if (!I->CodeToInsert.empty()) { |
| 1001 | // We have an insertion hint. Determine whether the inserted |
| 1002 | // code contains no newlines and is on the same line as the caret. |
| 1003 | std::pair<FileID, unsigned> HintLocInfo |
| 1004 | = SM.getDecomposedExpansionLoc(I->RemoveRange.getBegin()); |
| 1005 | if (LineNo == SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) && |
| 1006 | StringRef(I->CodeToInsert).find_first_of("\n\r") == StringRef::npos) { |
| 1007 | // Insert the new code into the line just below the code |
| 1008 | // that the user wrote. |
| 1009 | // Note: When modifying this function, be very careful about what is a |
| 1010 | // "column" (printed width, platform-dependent) and what is a |
| 1011 | // "byte offset" (SourceManager "column"). |
| 1012 | unsigned HintByteOffset |
| 1013 | = SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second) - 1; |
| 1014 | |
| 1015 | // The hint must start inside the source or right at the end |
| 1016 | assert(HintByteOffset < static_cast<unsigned>(map.bytes())+1); |
| 1017 | unsigned HintCol = map.byteToContainingColumn(HintByteOffset); |
| 1018 | |
| 1019 | // If we inserted a long previous hint, push this one forwards, and add |
| 1020 | // an extra space to show that this is not part of the previous |
| 1021 | // completion. This is sort of the best we can do when two hints appear |
| 1022 | // to overlap. |
| 1023 | // |
| 1024 | // Note that if this hint is located immediately after the previous |
| 1025 | // hint, no space will be added, since the location is more important. |
| 1026 | if (HintCol < PrevHintEndCol) |
| 1027 | HintCol = PrevHintEndCol + 1; |
| 1028 | |
| 1029 | // FIXME: This function handles multibyte characters in the source, but |
| 1030 | // not in the fixits. This assertion is intended to catch unintended |
| 1031 | // use of multibyte characters in fixits. If we decide to do this, we'll |
| 1032 | // have to track separate byte widths for the source and fixit lines. |
| 1033 | assert((size_t)llvm::sys::locale::columnWidth(I->CodeToInsert) == |
| 1034 | I->CodeToInsert.size()); |
| 1035 | |
| 1036 | // This relies on one byte per column in our fixit hints. |
| 1037 | // This should NOT use HintByteOffset, because the source might have |
| 1038 | // Unicode characters in earlier columns. |
| 1039 | unsigned LastColumnModified = HintCol + I->CodeToInsert.size(); |
| 1040 | if (LastColumnModified > FixItInsertionLine.size()) |
| 1041 | FixItInsertionLine.resize(LastColumnModified, ' '); |
| 1042 | |
| 1043 | std::copy(I->CodeToInsert.begin(), I->CodeToInsert.end(), |
| 1044 | FixItInsertionLine.begin() + HintCol); |
| 1045 | |
| 1046 | PrevHintEndCol = LastColumnModified; |
| 1047 | } else { |
| 1048 | FixItInsertionLine.clear(); |
| 1049 | break; |
| 1050 | } |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | expandTabs(FixItInsertionLine, DiagOpts->TabStop); |
| 1055 | |
| 1056 | return FixItInsertionLine; |
| 1057 | } |
| 1058 | |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1059 | /// \brief Emit a code snippet and caret line. |
| 1060 | /// |
| 1061 | /// This routine emits a single line's code snippet and caret line.. |
| 1062 | /// |
| 1063 | /// \param Loc The location for the caret. |
| 1064 | /// \param Ranges The underlined ranges for this code snippet. |
| 1065 | /// \param Hints The FixIt hints active for this diagnostic. |
Chandler Carruth | 7531f57 | 2011-10-15 23:54:09 +0000 | [diff] [blame] | 1066 | void TextDiagnostic::emitSnippetAndCaret( |
Chandler Carruth | 4ba5565 | 2011-10-16 07:20:28 +0000 | [diff] [blame] | 1067 | SourceLocation Loc, DiagnosticsEngine::Level Level, |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1068 | SmallVectorImpl<CharSourceRange>& Ranges, |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 1069 | ArrayRef<FixItHint> Hints, |
| 1070 | const SourceManager &SM) { |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1071 | assert(!Loc.isInvalid() && "must have a valid source location here"); |
| 1072 | assert(Loc.isFileID() && "must have a file location here"); |
| 1073 | |
Chandler Carruth | 4ba5565 | 2011-10-16 07:20:28 +0000 | [diff] [blame] | 1074 | // If caret diagnostics are enabled and we have location, we want to |
| 1075 | // emit the caret. However, we only do this if the location moved |
| 1076 | // from the last diagnostic, if the last diagnostic was a note that |
| 1077 | // was part of a different warning or error diagnostic, or if the |
| 1078 | // diagnostic has ranges. We don't want to emit the same caret |
| 1079 | // multiple times if one loc has multiple diagnostics. |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 1080 | if (!DiagOpts->ShowCarets) |
Chandler Carruth | 4ba5565 | 2011-10-16 07:20:28 +0000 | [diff] [blame] | 1081 | return; |
| 1082 | if (Loc == LastLoc && Ranges.empty() && Hints.empty() && |
| 1083 | (LastLevel != DiagnosticsEngine::Note || Level == LastLevel)) |
| 1084 | return; |
| 1085 | |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1086 | // Decompose the location into a FID/Offset pair. |
| 1087 | std::pair<FileID, unsigned> LocInfo = SM.getDecomposedLoc(Loc); |
| 1088 | FileID FID = LocInfo.first; |
| 1089 | unsigned FileOffset = LocInfo.second; |
| 1090 | |
| 1091 | // Get information about the buffer it points into. |
| 1092 | bool Invalid = false; |
Nico Weber | 40d8e97 | 2012-04-26 21:39:46 +0000 | [diff] [blame] | 1093 | const char *BufStart = SM.getBufferData(FID, &Invalid).data(); |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1094 | if (Invalid) |
| 1095 | return; |
| 1096 | |
| 1097 | unsigned LineNo = SM.getLineNumber(FID, FileOffset); |
| 1098 | unsigned ColNo = SM.getColumnNumber(FID, FileOffset); |
Jordan Rose | f6e74a3 | 2013-01-30 21:41:07 +0000 | [diff] [blame^] | 1099 | |
| 1100 | // Arbitrarily stop showing snippets when the line is too long. |
| 1101 | static const unsigned MaxLineLength = 4096; |
| 1102 | if (ColNo > MaxLineLength) |
| 1103 | return; |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1104 | |
| 1105 | // Rewind from the current position to the start of the line. |
| 1106 | const char *TokPtr = BufStart+FileOffset; |
| 1107 | const char *LineStart = TokPtr-ColNo+1; // Column # is 1-based. |
| 1108 | |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1109 | // Compute the line end. Scan forward from the error position to the end of |
| 1110 | // the line. |
| 1111 | const char *LineEnd = TokPtr; |
Nico Weber | 40d8e97 | 2012-04-26 21:39:46 +0000 | [diff] [blame] | 1112 | while (*LineEnd != '\n' && *LineEnd != '\r' && *LineEnd != '\0') |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1113 | ++LineEnd; |
| 1114 | |
Jordan Rose | f6e74a3 | 2013-01-30 21:41:07 +0000 | [diff] [blame^] | 1115 | // Arbitrarily stop showing snippets when the line is too long. |
| 1116 | if (LineEnd - LineStart > MaxLineLength) |
| 1117 | return; |
| 1118 | |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1119 | // Copy the line of code into an std::string for ease of manipulation. |
| 1120 | std::string SourceLine(LineStart, LineEnd); |
| 1121 | |
| 1122 | // Create a line for the caret that is filled with spaces that is the same |
| 1123 | // length as the line of source code. |
| 1124 | std::string CaretLine(LineEnd-LineStart, ' '); |
| 1125 | |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 1126 | const SourceColumnMap sourceColMap(SourceLine, DiagOpts->TabStop); |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 1127 | |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1128 | // Highlight all of the characters covered by Ranges with ~ characters. |
| 1129 | for (SmallVectorImpl<CharSourceRange>::iterator I = Ranges.begin(), |
| 1130 | E = Ranges.end(); |
| 1131 | I != E; ++I) |
Benjamin Kramer | c2b914f | 2012-12-01 20:58:01 +0000 | [diff] [blame] | 1132 | highlightRange(*I, LineNo, FID, sourceColMap, CaretLine, SM, LangOpts); |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1133 | |
| 1134 | // Next, insert the caret itself. |
Richard Smith | c7bb384 | 2012-09-13 18:37:50 +0000 | [diff] [blame] | 1135 | ColNo = sourceColMap.byteToContainingColumn(ColNo-1); |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 1136 | if (CaretLine.size()<ColNo+1) |
| 1137 | CaretLine.resize(ColNo+1, ' '); |
| 1138 | CaretLine[ColNo] = '^'; |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1139 | |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 1140 | std::string FixItInsertionLine = buildFixItInsertionLine(LineNo, |
| 1141 | sourceColMap, |
Benjamin Kramer | c2b914f | 2012-12-01 20:58:01 +0000 | [diff] [blame] | 1142 | Hints, SM, |
| 1143 | DiagOpts.getPtr()); |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 1144 | |
| 1145 | // If the source line is too long for our terminal, select only the |
| 1146 | // "interesting" source region within that line. |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 1147 | unsigned Columns = DiagOpts->MessageLength; |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 1148 | if (Columns) |
| 1149 | selectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine, |
| 1150 | Columns, sourceColMap); |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1151 | |
| 1152 | // If we are in -fdiagnostics-print-source-range-info mode, we are trying |
| 1153 | // to produce easily machine parsable output. Add a space before the |
| 1154 | // source line and the caret to make it trivial to tell the main diagnostic |
| 1155 | // line from what the user is intended to see. |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 1156 | if (DiagOpts->ShowSourceRanges) { |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1157 | SourceLine = ' ' + SourceLine; |
| 1158 | CaretLine = ' ' + CaretLine; |
| 1159 | } |
| 1160 | |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1161 | // Finally, remove any blank spaces from the end of CaretLine. |
| 1162 | while (CaretLine[CaretLine.size()-1] == ' ') |
| 1163 | CaretLine.erase(CaretLine.end()-1); |
| 1164 | |
| 1165 | // Emit what we have computed. |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 1166 | emitSnippet(SourceLine); |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1167 | |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 1168 | if (DiagOpts->ShowColors) |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1169 | OS.changeColor(caretColor, true); |
| 1170 | OS << CaretLine << '\n'; |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 1171 | if (DiagOpts->ShowColors) |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1172 | OS.resetColor(); |
| 1173 | |
| 1174 | if (!FixItInsertionLine.empty()) { |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 1175 | if (DiagOpts->ShowColors) |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1176 | // Print fixit line in color |
| 1177 | OS.changeColor(fixitColor, false); |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 1178 | if (DiagOpts->ShowSourceRanges) |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1179 | OS << ' '; |
| 1180 | OS << FixItInsertionLine << '\n'; |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 1181 | if (DiagOpts->ShowColors) |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1182 | OS.resetColor(); |
| 1183 | } |
| 1184 | |
| 1185 | // Print out any parseable fixit information requested by the options. |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 1186 | emitParseableFixits(Hints, SM); |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1187 | } |
| 1188 | |
Benjamin Kramer | d1fda03 | 2012-05-01 14:34:11 +0000 | [diff] [blame] | 1189 | void TextDiagnostic::emitSnippet(StringRef line) { |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 1190 | if (line.empty()) |
| 1191 | return; |
| 1192 | |
| 1193 | size_t i = 0; |
| 1194 | |
| 1195 | std::string to_print; |
| 1196 | bool print_reversed = false; |
| 1197 | |
| 1198 | while (i<line.size()) { |
| 1199 | std::pair<SmallString<16>,bool> res |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 1200 | = printableTextForNextCharacter(line, &i, DiagOpts->TabStop); |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 1201 | bool was_printable = res.second; |
| 1202 | |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 1203 | if (DiagOpts->ShowColors && was_printable == print_reversed) { |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 1204 | if (print_reversed) |
| 1205 | OS.reverseColor(); |
| 1206 | OS << to_print; |
| 1207 | to_print.clear(); |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 1208 | if (DiagOpts->ShowColors) |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 1209 | OS.resetColor(); |
| 1210 | } |
| 1211 | |
| 1212 | print_reversed = !was_printable; |
| 1213 | to_print += res.first.str(); |
| 1214 | } |
| 1215 | |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 1216 | if (print_reversed && DiagOpts->ShowColors) |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 1217 | OS.reverseColor(); |
| 1218 | OS << to_print; |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 1219 | if (print_reversed && DiagOpts->ShowColors) |
Seth Cantrell | 6749dd5 | 2012-04-18 02:44:46 +0000 | [diff] [blame] | 1220 | OS.resetColor(); |
| 1221 | |
| 1222 | OS << '\n'; |
| 1223 | } |
| 1224 | |
Argyrios Kyrtzidis | 16afdf7 | 2012-05-10 05:03:45 +0000 | [diff] [blame] | 1225 | void TextDiagnostic::emitParseableFixits(ArrayRef<FixItHint> Hints, |
| 1226 | const SourceManager &SM) { |
Douglas Gregor | 02c23eb | 2012-10-23 22:26:28 +0000 | [diff] [blame] | 1227 | if (!DiagOpts->ShowParseableFixits) |
Chandler Carruth | db463bb | 2011-10-15 23:43:53 +0000 | [diff] [blame] | 1228 | return; |
| 1229 | |
| 1230 | // We follow FixItRewriter's example in not (yet) handling |
| 1231 | // fix-its in macros. |
| 1232 | for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end(); |
| 1233 | I != E; ++I) { |
| 1234 | if (I->RemoveRange.isInvalid() || |
| 1235 | I->RemoveRange.getBegin().isMacroID() || |
| 1236 | I->RemoveRange.getEnd().isMacroID()) |
| 1237 | return; |
| 1238 | } |
| 1239 | |
| 1240 | for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end(); |
| 1241 | I != E; ++I) { |
| 1242 | SourceLocation BLoc = I->RemoveRange.getBegin(); |
| 1243 | SourceLocation ELoc = I->RemoveRange.getEnd(); |
| 1244 | |
| 1245 | std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(BLoc); |
| 1246 | std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(ELoc); |
| 1247 | |
| 1248 | // Adjust for token ranges. |
| 1249 | if (I->RemoveRange.isTokenRange()) |
| 1250 | EInfo.second += Lexer::MeasureTokenLength(ELoc, SM, LangOpts); |
| 1251 | |
| 1252 | // We specifically do not do word-wrapping or tab-expansion here, |
| 1253 | // because this is supposed to be easy to parse. |
| 1254 | PresumedLoc PLoc = SM.getPresumedLoc(BLoc); |
| 1255 | if (PLoc.isInvalid()) |
| 1256 | break; |
| 1257 | |
| 1258 | OS << "fix-it:\""; |
| 1259 | OS.write_escaped(PLoc.getFilename()); |
| 1260 | OS << "\":{" << SM.getLineNumber(BInfo.first, BInfo.second) |
| 1261 | << ':' << SM.getColumnNumber(BInfo.first, BInfo.second) |
| 1262 | << '-' << SM.getLineNumber(EInfo.first, EInfo.second) |
| 1263 | << ':' << SM.getColumnNumber(EInfo.first, EInfo.second) |
| 1264 | << "}:\""; |
| 1265 | OS.write_escaped(I->CodeToInsert); |
| 1266 | OS << "\"\n"; |
| 1267 | } |
| 1268 | } |