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