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