| 1 | |
| 2 | |
| 3 | |
| 4 | |
| 5 | |
| 6 | |
| 7 | |
| 8 | |
| 9 | #include "clang/Frontend/TextDiagnostic.h" |
| 10 | #include "clang/Basic/CharInfo.h" |
| 11 | #include "clang/Basic/DiagnosticOptions.h" |
| 12 | #include "clang/Basic/FileManager.h" |
| 13 | #include "clang/Basic/SourceManager.h" |
| 14 | #include "clang/Lex/Lexer.h" |
| 15 | #include "llvm/ADT/SmallString.h" |
| 16 | #include "llvm/ADT/StringExtras.h" |
| 17 | #include "llvm/Support/ConvertUTF.h" |
| 18 | #include "llvm/Support/ErrorHandling.h" |
| 19 | #include "llvm/Support/Locale.h" |
| 20 | #include "llvm/Support/Path.h" |
| 21 | #include "llvm/Support/raw_ostream.h" |
| 22 | #include <algorithm> |
| 23 | |
| 24 | using namespace clang; |
| 25 | |
| 26 | static const enum raw_ostream::Colors noteColor = |
| 27 | raw_ostream::BLACK; |
| 28 | static const enum raw_ostream::Colors = |
| 29 | raw_ostream::BLUE; |
| 30 | static const enum raw_ostream::Colors fixitColor = |
| 31 | raw_ostream::GREEN; |
| 32 | static const enum raw_ostream::Colors caretColor = |
| 33 | raw_ostream::GREEN; |
| 34 | static const enum raw_ostream::Colors warningColor = |
| 35 | raw_ostream::MAGENTA; |
| 36 | static const enum raw_ostream::Colors templateColor = |
| 37 | raw_ostream::CYAN; |
| 38 | static const enum raw_ostream::Colors errorColor = raw_ostream::RED; |
| 39 | static const enum raw_ostream::Colors fatalColor = raw_ostream::RED; |
| 40 | |
| 41 | static const enum raw_ostream::Colors savedColor = |
| 42 | raw_ostream::SAVEDCOLOR; |
| 43 | |
| 44 | |
| 45 | static void applyTemplateHighlighting(raw_ostream &OS, StringRef Str, |
| 46 | bool &Normal, bool Bold) { |
| 47 | while (1) { |
| 48 | size_t Pos = Str.find(ToggleHighlight); |
| 49 | OS << Str.slice(0, Pos); |
| 50 | if (Pos == StringRef::npos) |
| 51 | break; |
| 52 | |
| 53 | Str = Str.substr(Pos + 1); |
| 54 | if (Normal) |
| 55 | OS.changeColor(templateColor, true); |
| 56 | else { |
| 57 | OS.resetColor(); |
| 58 | if (Bold) |
| 59 | OS.changeColor(savedColor, true); |
| 60 | } |
| 61 | Normal = !Normal; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | |
| 66 | const unsigned WordWrapIndentation = 6; |
| 67 | |
| 68 | static int bytesSincePreviousTabOrLineBegin(StringRef SourceLine, size_t i) { |
| 69 | int bytes = 0; |
| 70 | while (0<i) { |
| 71 | if (SourceLine[--i]=='\t') |
| 72 | break; |
| 73 | ++bytes; |
| 74 | } |
| 75 | return bytes; |
| 76 | } |
| 77 | |
| 78 | |
| 79 | |
| 80 | |
| 81 | |
| 82 | |
| 83 | |
| 84 | |
| 85 | |
| 86 | |
| 87 | |
| 88 | |
| 89 | |
| 90 | |
| 91 | |
| 92 | |
| 93 | |
| 94 | |
| 95 | |
| 96 | |
| 97 | static std::pair<SmallString<16>, bool> |
| 98 | printableTextForNextCharacter(StringRef SourceLine, size_t *i, |
| 99 | unsigned TabStop) { |
| 100 | (0) . __assert_fail ("i && \"i must not be null\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 100, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(i && "i must not be null"); |
| 101 | (0) . __assert_fail ("*iassert(*i<SourceLine.size() && "must point to a valid index"); |
| 102 | |
| 103 | if (SourceLine[*i]=='\t') { |
| 104 | (0) . __assert_fail ("0 < TabStop && TabStop <= DiagnosticOptions..MaxTabStop && \"Invalid -ftabstop value\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 105, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(0 < TabStop && TabStop <= DiagnosticOptions::MaxTabStop && |
| 105 | (0) . __assert_fail ("0 < TabStop && TabStop <= DiagnosticOptions..MaxTabStop && \"Invalid -ftabstop value\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 105, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "Invalid -ftabstop value"); |
| 106 | unsigned col = bytesSincePreviousTabOrLineBegin(SourceLine, *i); |
| 107 | unsigned NumSpaces = TabStop - col%TabStop; |
| 108 | (0) . __assert_fail ("0 < NumSpaces && NumSpaces <= TabStop && \"Invalid computation of space amt\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 109, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(0 < NumSpaces && NumSpaces <= TabStop |
| 109 | (0) . __assert_fail ("0 < NumSpaces && NumSpaces <= TabStop && \"Invalid computation of space amt\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 109, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> && "Invalid computation of space amt"); |
| 110 | ++(*i); |
| 111 | |
| 112 | SmallString<16> expandedTab; |
| 113 | expandedTab.assign(NumSpaces, ' '); |
| 114 | return std::make_pair(expandedTab, true); |
| 115 | } |
| 116 | |
| 117 | unsigned char const *begin, *end; |
| 118 | begin = reinterpret_cast<unsigned char const *>(&*(SourceLine.begin() + *i)); |
| 119 | end = begin + (SourceLine.size() - *i); |
| 120 | |
| 121 | if (llvm::isLegalUTF8Sequence(begin, end)) { |
| 122 | llvm::UTF32 c; |
| 123 | llvm::UTF32 *cptr = &c; |
| 124 | unsigned char const *original_begin = begin; |
| 125 | unsigned char const *cp_end = |
| 126 | begin + llvm::getNumBytesForUTF8(SourceLine[*i]); |
| 127 | |
| 128 | llvm::ConversionResult res = llvm::ConvertUTF8toUTF32( |
| 129 | &begin, cp_end, &cptr, cptr + 1, llvm::strictConversion); |
| 130 | (void)res; |
| 131 | assert(llvm::conversionOK == res); |
| 132 | (0) . __assert_fail ("0 < begin-original_begin && \"we must be further along in the string now\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 133, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(0 < begin-original_begin |
| 133 | (0) . __assert_fail ("0 < begin-original_begin && \"we must be further along in the string now\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 133, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> && "we must be further along in the string now"); |
| 134 | *i += begin-original_begin; |
| 135 | |
| 136 | if (!llvm::sys::locale::isPrint(c)) { |
| 137 | |
| 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 | |
| 149 | return std::make_pair(SmallString<16>(original_begin, cp_end), true); |
| 150 | |
| 151 | } |
| 152 | |
| 153 | |
| 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 | |
| 162 | static void expandTabs(std::string &SourceLine, unsigned TabStop) { |
| 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 | |
| 176 | |
| 177 | |
| 178 | |
| 179 | |
| 180 | |
| 181 | |
| 182 | |
| 183 | |
| 184 | |
| 185 | |
| 186 | |
| 187 | |
| 188 | |
| 189 | |
| 190 | |
| 191 | |
| 192 | |
| 193 | |
| 194 | |
| 195 | |
| 196 | |
| 197 | static void byteToColumn(StringRef SourceLine, unsigned TabStop, |
| 198 | SmallVectorImpl<int> &out) { |
| 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 | |
| 220 | |
| 221 | |
| 222 | |
| 223 | |
| 224 | |
| 225 | |
| 226 | |
| 227 | |
| 228 | |
| 229 | |
| 230 | |
| 231 | static void columnToByte(StringRef SourceLine, unsigned TabStop, |
| 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 | |
| 253 | namespace { |
| 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 | (m_columnToByte.back()+1)", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 264, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(m_byteToColumn.size() |
| 264 | (m_columnToByte.back()+1)", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 264, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> == static_cast<unsigned>(m_columnToByte.back()+1)); |
| 265 | (m_byteToColumn.back()+1) == m_columnToByte.size()", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 266, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(static_cast<unsigned>(m_byteToColumn.back()+1) |
| 266 | (m_byteToColumn.back()+1) == m_columnToByte.size()", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 266, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> == m_columnToByte.size()); |
| 267 | } |
| 268 | int columns() const { return m_byteToColumn.back(); } |
| 269 | int bytes() const { return m_columnToByte.back(); } |
| 270 | |
| 271 | |
| 272 | |
| 273 | int byteToColumn(int n) const { |
| 274 | (m_byteToColumn.size())", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 274, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(0<=n && n<static_cast<int>(m_byteToColumn.size())); |
| 275 | return m_byteToColumn[n]; |
| 276 | } |
| 277 | |
| 278 | |
| 279 | int byteToContainingColumn(int N) const { |
| 280 | (m_byteToColumn.size())", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 280, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">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 | |
| 287 | |
| 288 | |
| 289 | int columnToByte(int n) const { |
| 290 | (m_columnToByte.size())", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 290, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(0<=n && n<static_cast<int>(m_columnToByte.size())); |
| 291 | return m_columnToByte[n]; |
| 292 | } |
| 293 | |
| 294 | |
| 295 | int startOfNextColumn(int N) const { |
| 296 | (m_byteToColumn.size() - 1)", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 296, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(0 <= N && N < static_cast<int>(m_byteToColumn.size() - 1)); |
| 297 | while (byteToColumn(++N) == -1) {} |
| 298 | return N; |
| 299 | } |
| 300 | |
| 301 | |
| 302 | int startOfPreviousColumn(int N) const { |
| 303 | (m_byteToColumn.size())", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 303, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(0 < N && N < static_cast<int>(m_byteToColumn.size())); |
| 304 | while (byteToColumn(--N) == -1) {} |
| 305 | return N; |
| 306 | } |
| 307 | |
| 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 | }; |
| 317 | } |
| 318 | |
| 319 | |
| 320 | |
| 321 | static void selectInterestingSourceRegion(std::string &SourceLine, |
| 322 | std::string &CaretLine, |
| 323 | std::string &FixItInsertionLine, |
| 324 | unsigned Columns, |
| 325 | const SourceColumnMap &map) { |
| 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)); |
| 330 | |
| 331 | if (MaxColumns <= Columns) |
| 332 | return; |
| 333 | |
| 334 | |
| 335 | assert(CaretLine.end() == |
| 336 | std::find_if(CaretLine.begin(), CaretLine.end(), |
| 337 | [](char c) { return c < ' ' || '~' < c; })); |
| 338 | |
| 339 | |
| 340 | |
| 341 | unsigned CaretStart = 0, CaretEnd = CaretLine.size(); |
| 342 | for (; CaretStart != CaretEnd; ++CaretStart) |
| 343 | if (!isWhitespace(CaretLine[CaretStart])) |
| 344 | break; |
| 345 | |
| 346 | for (; CaretEnd != CaretStart; --CaretEnd) |
| 347 | if (!isWhitespace(CaretLine[CaretEnd - 1])) |
| 348 | break; |
| 349 | |
| 350 | |
| 351 | |
| 352 | |
| 353 | |
| 354 | |
| 355 | if (!FixItInsertionLine.empty()) { |
| 356 | unsigned FixItStart = 0, FixItEnd = FixItInsertionLine.size(); |
| 357 | for (; FixItStart != FixItEnd; ++FixItStart) |
| 358 | if (!isWhitespace(FixItInsertionLine[FixItStart])) |
| 359 | break; |
| 360 | |
| 361 | for (; FixItEnd != FixItStart; --FixItEnd) |
| 362 | if (!isWhitespace(FixItInsertionLine[FixItEnd - 1])) |
| 363 | break; |
| 364 | |
| 365 | |
| 366 | |
| 367 | |
| 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); |
| 374 | } |
| 375 | |
| 376 | |
| 377 | |
| 378 | |
| 379 | while (static_cast<int>(CaretEnd) < map.columns() && |
| 380 | -1 == map.columnToByte(CaretEnd)) |
| 381 | ++CaretEnd; |
| 382 | |
| 383 | (0) . __assert_fail ("(static_cast(CaretStart) > map.columns() || -1!=map.columnToByte(CaretStart)) && \"CaretStart must not point to a column in the middle of a source\" \" line character\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 386, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((static_cast<int>(CaretStart) > map.columns() || |
| 384 | (0) . __assert_fail ("(static_cast(CaretStart) > map.columns() || -1!=map.columnToByte(CaretStart)) && \"CaretStart must not point to a column in the middle of a source\" \" line character\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 386, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> -1!=map.columnToByte(CaretStart)) && |
| 385 | (0) . __assert_fail ("(static_cast(CaretStart) > map.columns() || -1!=map.columnToByte(CaretStart)) && \"CaretStart must not point to a column in the middle of a source\" \" line character\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 386, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "CaretStart must not point to a column in the middle of a source" |
| 386 | (0) . __assert_fail ("(static_cast(CaretStart) > map.columns() || -1!=map.columnToByte(CaretStart)) && \"CaretStart must not point to a column in the middle of a source\" \" line character\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 386, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> " line character"); |
| 387 | (0) . __assert_fail ("(static_cast(CaretEnd) > map.columns() || -1!=map.columnToByte(CaretEnd)) && \"CaretEnd must not point to a column in the middle of a source line\" \" character\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 390, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert((static_cast<int>(CaretEnd) > map.columns() || |
| 388 | (0) . __assert_fail ("(static_cast(CaretEnd) > map.columns() || -1!=map.columnToByte(CaretEnd)) && \"CaretEnd must not point to a column in the middle of a source line\" \" character\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 390, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> -1!=map.columnToByte(CaretEnd)) && |
| 389 | (0) . __assert_fail ("(static_cast(CaretEnd) > map.columns() || -1!=map.columnToByte(CaretEnd)) && \"CaretEnd must not point to a column in the middle of a source line\" \" character\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 390, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> "CaretEnd must not point to a column in the middle of a source line" |
| 390 | (0) . __assert_fail ("(static_cast(CaretEnd) > map.columns() || -1!=map.columnToByte(CaretEnd)) && \"CaretEnd must not point to a column in the middle of a source line\" \" character\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 390, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true"> " character"); |
| 391 | |
| 392 | |
| 393 | |
| 394 | |
| 395 | |
| 396 | |
| 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); |
| 409 | |
| 410 | unsigned TargetColumns = Columns; |
| 411 | |
| 412 | |
| 413 | if (TargetColumns > ellipses_space+CaretColumnsOutsideSource) |
| 414 | TargetColumns -= ellipses_space+CaretColumnsOutsideSource; |
| 415 | |
| 416 | while (SourceStart>0 || SourceEnd<SourceLine.size()) { |
| 417 | bool ExpandedRegion = false; |
| 418 | |
| 419 | if (SourceStart>0) { |
| 420 | unsigned NewStart = map.startOfPreviousColumn(SourceStart); |
| 421 | |
| 422 | |
| 423 | |
| 424 | |
| 425 | while (NewStart && isWhitespace(SourceLine[NewStart])) |
| 426 | NewStart = map.startOfPreviousColumn(NewStart); |
| 427 | |
| 428 | |
| 429 | while (NewStart) { |
| 430 | unsigned Prev = map.startOfPreviousColumn(NewStart); |
| 431 | if (isWhitespace(SourceLine[Prev])) |
| 432 | break; |
| 433 | NewStart = Prev; |
| 434 | } |
| 435 | |
| 436 | assert(map.byteToColumn(NewStart) != -1); |
| 437 | unsigned NewColumns = map.byteToColumn(SourceEnd) - |
| 438 | map.byteToColumn(NewStart); |
| 439 | if (NewColumns <= TargetColumns) { |
| 440 | SourceStart = NewStart; |
| 441 | ExpandedRegion = true; |
| 442 | } |
| 443 | } |
| 444 | |
| 445 | if (SourceEnd<SourceLine.size()) { |
| 446 | unsigned NewEnd = map.startOfNextColumn(SourceEnd); |
| 447 | |
| 448 | |
| 449 | |
| 450 | |
| 451 | while (NewEnd < SourceLine.size() && isWhitespace(SourceLine[NewEnd])) |
| 452 | NewEnd = map.startOfNextColumn(NewEnd); |
| 453 | |
| 454 | |
| 455 | while (NewEnd < SourceLine.size() && isWhitespace(SourceLine[NewEnd])) |
| 456 | NewEnd = map.startOfNextColumn(NewEnd); |
| 457 | |
| 458 | assert(map.byteToColumn(NewEnd) != -1); |
| 459 | unsigned NewColumns = map.byteToColumn(NewEnd) - |
| 460 | map.byteToColumn(SourceStart); |
| 461 | if (NewColumns <= TargetColumns) { |
| 462 | SourceEnd = NewEnd; |
| 463 | ExpandedRegion = true; |
| 464 | } |
| 465 | } |
| 466 | |
| 467 | if (!ExpandedRegion) |
| 468 | break; |
| 469 | } |
| 470 | |
| 471 | CaretStart = map.byteToColumn(SourceStart); |
| 472 | CaretEnd = map.byteToColumn(SourceEnd) + CaretColumnsOutsideSource; |
| 473 | |
| 474 | |
| 475 | |
| 476 | |
| 477 | |
| 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 | |
| 489 | Columns", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 489, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(FrontColumnsRemoved+ColumnsKept+BackColumnsRemoved > Columns); |
| 490 | |
| 491 | |
| 492 | |
| 493 | if (BackColumnsRemoved > strlen(back_ellipse)) |
| 494 | SourceLine.replace(SourceEnd, std::string::npos, back_ellipse); |
| 495 | |
| 496 | |
| 497 | if (FrontColumnsRemoved+ColumnsKept <= Columns) |
| 498 | return; |
| 499 | |
| 500 | |
| 501 | if (FrontColumnsRemoved > strlen(front_ellipse)) { |
| 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); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | |
| 510 | |
| 511 | |
| 512 | |
| 513 | |
| 514 | |
| 515 | static unsigned skipWhitespace(unsigned Idx, StringRef Str, unsigned Length) { |
| 516 | while (Idx < Length && isWhitespace(Str[Idx])) |
| 517 | ++Idx; |
| 518 | return Idx; |
| 519 | } |
| 520 | |
| 521 | |
| 522 | |
| 523 | |
| 524 | |
| 525 | |
| 526 | |
| 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 | |
| 542 | |
| 543 | |
| 544 | |
| 545 | |
| 546 | static unsigned findEndOfWord(unsigned Start, StringRef Str, |
| 547 | unsigned Length, unsigned Column, |
| 548 | unsigned Columns) { |
| 549 | (0) . __assert_fail ("Start < Str.size() && \"Invalid start position!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 549, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Start < Str.size() && "Invalid start position!"); |
| 550 | unsigned End = Start + 1; |
| 551 | |
| 552 | |
| 553 | if (End == Str.size()) |
| 554 | return End; |
| 555 | |
| 556 | |
| 557 | |
| 558 | char EndPunct = findMatchingPunctuation(Str[Start]); |
| 559 | if (!EndPunct) { |
| 560 | |
| 561 | while (End < Length && !isWhitespace(Str[End])) |
| 562 | ++End; |
| 563 | return End; |
| 564 | } |
| 565 | |
| 566 | |
| 567 | |
| 568 | SmallString<16> PunctuationEndStack; |
| 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 | |
| 580 | while (End < Length && !isWhitespace(Str[End])) |
| 581 | ++End; |
| 582 | |
| 583 | unsigned PunctWordLength = End - Start; |
| 584 | if ( |
| 585 | Column + PunctWordLength <= Columns || |
| 586 | |
| 587 | |
| 588 | PunctWordLength < Columns/3) |
| 589 | return End; |
| 590 | |
| 591 | |
| 592 | |
| 593 | |
| 594 | |
| 595 | return findEndOfWord(Start + 1, Str, Length, Column + 1, Columns); |
| 596 | } |
| 597 | |
| 598 | |
| 599 | |
| 600 | |
| 601 | |
| 602 | |
| 603 | |
| 604 | |
| 605 | |
| 606 | |
| 607 | |
| 608 | |
| 609 | |
| 610 | |
| 611 | |
| 612 | |
| 613 | static bool printWordWrapped(raw_ostream &OS, StringRef Str, |
| 614 | unsigned Columns, |
| 615 | unsigned Column = 0, |
| 616 | bool Bold = false, |
| 617 | unsigned Indentation = WordWrapIndentation) { |
| 618 | const unsigned Length = std::min(Str.find('\n'), Str.size()); |
| 619 | bool TextNormal = true; |
| 620 | |
| 621 | |
| 622 | SmallString<16> IndentStr; |
| 623 | IndentStr.assign(Indentation, ' '); |
| 624 | bool Wrapped = false; |
| 625 | for (unsigned WordStart = 0, WordEnd; WordStart < Length; |
| 626 | WordStart = WordEnd) { |
| 627 | |
| 628 | WordStart = skipWhitespace(WordStart, Str, Length); |
| 629 | if (WordStart == Length) |
| 630 | break; |
| 631 | |
| 632 | |
| 633 | WordEnd = findEndOfWord(WordStart, Str, Length, Column, Columns); |
| 634 | |
| 635 | |
| 636 | unsigned WordLength = WordEnd - WordStart; |
| 637 | if (Column + WordLength < Columns) { |
| 638 | |
| 639 | if (WordStart) { |
| 640 | OS << ' '; |
| 641 | Column += 1; |
| 642 | } |
| 643 | applyTemplateHighlighting(OS, Str.substr(WordStart, WordLength), |
| 644 | TextNormal, Bold); |
| 645 | Column += WordLength; |
| 646 | continue; |
| 647 | } |
| 648 | |
| 649 | |
| 650 | |
| 651 | OS << '\n'; |
| 652 | OS.write(&IndentStr[0], Indentation); |
| 653 | applyTemplateHighlighting(OS, Str.substr(WordStart, WordLength), |
| 654 | TextNormal, Bold); |
| 655 | Column = Indentation + WordLength; |
| 656 | Wrapped = true; |
| 657 | } |
| 658 | |
| 659 | |
| 660 | applyTemplateHighlighting(OS, Str.substr(Length), TextNormal, Bold); |
| 661 | |
| 662 | (0) . __assert_fail ("TextNormal && \"Text highlighted at end of diagnostic message.\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 662, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(TextNormal && "Text highlighted at end of diagnostic message."); |
| 663 | |
| 664 | return Wrapped; |
| 665 | } |
| 666 | |
| 667 | TextDiagnostic::TextDiagnostic(raw_ostream &OS, |
| 668 | const LangOptions &LangOpts, |
| 669 | DiagnosticOptions *DiagOpts) |
| 670 | : DiagnosticRenderer(LangOpts, DiagOpts), OS(OS) {} |
| 671 | |
| 672 | TextDiagnostic::~TextDiagnostic() {} |
| 673 | |
| 674 | void TextDiagnostic::emitDiagnosticMessage( |
| 675 | FullSourceLoc Loc, PresumedLoc PLoc, DiagnosticsEngine::Level Level, |
| 676 | StringRef Message, ArrayRef<clang::CharSourceRange> Ranges, |
| 677 | DiagOrStoredDiag D) { |
| 678 | uint64_t StartOfLocationInfo = OS.tell(); |
| 679 | |
| 680 | |
| 681 | if (Loc.isValid()) |
| 682 | emitDiagnosticLoc(Loc, PLoc, Level, Ranges); |
| 683 | |
| 684 | if (DiagOpts->ShowColors) |
| 685 | OS.resetColor(); |
| 686 | |
| 687 | printDiagnosticLevel(OS, Level, DiagOpts->ShowColors, |
| 688 | DiagOpts->CLFallbackMode); |
| 689 | printDiagnosticMessage(OS, |
| 690 | Level == DiagnosticsEngine::Note, |
| 691 | Message, OS.tell() - StartOfLocationInfo, |
| 692 | DiagOpts->MessageLength, DiagOpts->ShowColors); |
| 693 | } |
| 694 | |
| 695 | void |
| 696 | TextDiagnostic::printDiagnosticLevel(raw_ostream &OS, |
| 697 | DiagnosticsEngine::Level Level, |
| 698 | bool ShowColors, |
| 699 | bool CLFallbackMode) { |
| 700 | if (ShowColors) { |
| 701 | |
| 702 | switch (Level) { |
| 703 | case DiagnosticsEngine::Ignored: |
| 704 | llvm_unreachable("Invalid diagnostic type"); |
| 705 | case DiagnosticsEngine::Note: OS.changeColor(noteColor, true); break; |
| 706 | case DiagnosticsEngine::Remark: OS.changeColor(remarkColor, true); break; |
| 707 | case DiagnosticsEngine::Warning: OS.changeColor(warningColor, true); break; |
| 708 | case DiagnosticsEngine::Error: OS.changeColor(errorColor, true); break; |
| 709 | case DiagnosticsEngine::Fatal: OS.changeColor(fatalColor, true); break; |
| 710 | } |
| 711 | } |
| 712 | |
| 713 | switch (Level) { |
| 714 | case DiagnosticsEngine::Ignored: |
| 715 | llvm_unreachable("Invalid diagnostic type"); |
| 716 | case DiagnosticsEngine::Note: OS << "note"; break; |
| 717 | case DiagnosticsEngine::Remark: OS << "remark"; break; |
| 718 | case DiagnosticsEngine::Warning: OS << "warning"; break; |
| 719 | case DiagnosticsEngine::Error: OS << "error"; break; |
| 720 | case DiagnosticsEngine::Fatal: OS << "fatal error"; break; |
| 721 | } |
| 722 | |
| 723 | |
| 724 | |
| 725 | |
| 726 | |
| 727 | if (CLFallbackMode) |
| 728 | OS << "(clang)"; |
| 729 | |
| 730 | OS << ": "; |
| 731 | |
| 732 | if (ShowColors) |
| 733 | OS.resetColor(); |
| 734 | } |
| 735 | |
| 736 | |
| 737 | void TextDiagnostic::printDiagnosticMessage(raw_ostream &OS, |
| 738 | bool IsSupplemental, |
| 739 | StringRef Message, |
| 740 | unsigned CurrentColumn, |
| 741 | unsigned Columns, bool ShowColors) { |
| 742 | bool Bold = false; |
| 743 | if (ShowColors && !IsSupplemental) { |
| 744 | |
| 745 | |
| 746 | OS.changeColor(savedColor, true); |
| 747 | Bold = true; |
| 748 | } |
| 749 | |
| 750 | if (Columns) |
| 751 | printWordWrapped(OS, Message, Columns, CurrentColumn, Bold); |
| 752 | else { |
| 753 | bool Normal = true; |
| 754 | applyTemplateHighlighting(OS, Message, Normal, Bold); |
| 755 | (0) . __assert_fail ("Normal && \"Formatting should have returned to normal\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 755, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Normal && "Formatting should have returned to normal"); |
| 756 | } |
| 757 | |
| 758 | if (ShowColors) |
| 759 | OS.resetColor(); |
| 760 | OS << '\n'; |
| 761 | } |
| 762 | |
| 763 | void TextDiagnostic::emitFilename(StringRef Filename, const SourceManager &SM) { |
| 764 | SmallVector<char, 128> AbsoluteFilename; |
| 765 | if (DiagOpts->AbsolutePath) { |
| 766 | const DirectoryEntry *Dir = SM.getFileManager().getDirectory( |
| 767 | llvm::sys::path::parent_path(Filename)); |
| 768 | if (Dir) { |
| 769 | StringRef DirName = SM.getFileManager().getCanonicalName(Dir); |
| 770 | llvm::sys::path::append(AbsoluteFilename, DirName, |
| 771 | llvm::sys::path::filename(Filename)); |
| 772 | Filename = StringRef(AbsoluteFilename.data(), AbsoluteFilename.size()); |
| 773 | } |
| 774 | } |
| 775 | |
| 776 | OS << Filename; |
| 777 | } |
| 778 | |
| 779 | |
| 780 | |
| 781 | |
| 782 | |
| 783 | |
| 784 | |
| 785 | void TextDiagnostic::emitDiagnosticLoc(FullSourceLoc Loc, PresumedLoc PLoc, |
| 786 | DiagnosticsEngine::Level Level, |
| 787 | ArrayRef<CharSourceRange> Ranges) { |
| 788 | if (PLoc.isInvalid()) { |
| 789 | |
| 790 | FileID FID = Loc.getFileID(); |
| 791 | if (FID.isValid()) { |
| 792 | const FileEntry *FE = Loc.getFileEntry(); |
| 793 | if (FE && FE->isValid()) { |
| 794 | emitFilename(FE->getName(), Loc.getManager()); |
| 795 | OS << ": "; |
| 796 | } |
| 797 | } |
| 798 | return; |
| 799 | } |
| 800 | unsigned LineNo = PLoc.getLine(); |
| 801 | |
| 802 | if (!DiagOpts->ShowLocation) |
| 803 | return; |
| 804 | |
| 805 | if (DiagOpts->ShowColors) |
| 806 | OS.changeColor(savedColor, true); |
| 807 | |
| 808 | emitFilename(PLoc.getFilename(), Loc.getManager()); |
| 809 | switch (DiagOpts->getFormat()) { |
| 810 | case DiagnosticOptions::Clang: OS << ':' << LineNo; break; |
| 811 | case DiagnosticOptions::MSVC: OS << '(' << LineNo; break; |
| 812 | case DiagnosticOptions::Vi: OS << " +" << LineNo; break; |
| 813 | } |
| 814 | |
| 815 | if (DiagOpts->ShowColumn) |
| 816 | |
| 817 | if (unsigned ColNo = PLoc.getColumn()) { |
| 818 | if (DiagOpts->getFormat() == DiagnosticOptions::MSVC) { |
| 819 | OS << ','; |
| 820 | |
| 821 | if (LangOpts.MSCompatibilityVersion && |
| 822 | !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2012)) |
| 823 | ColNo--; |
| 824 | } else |
| 825 | OS << ':'; |
| 826 | OS << ColNo; |
| 827 | } |
| 828 | switch (DiagOpts->getFormat()) { |
| 829 | case DiagnosticOptions::Clang: |
| 830 | case DiagnosticOptions::Vi: OS << ':'; break; |
| 831 | case DiagnosticOptions::MSVC: |
| 832 | |
| 833 | |
| 834 | OS << ')'; |
| 835 | if (LangOpts.MSCompatibilityVersion && |
| 836 | !LangOpts.isCompatibleWithMSVC(LangOptions::MSVC2015)) |
| 837 | OS << ' '; |
| 838 | OS << ':'; |
| 839 | break; |
| 840 | } |
| 841 | |
| 842 | if (DiagOpts->ShowSourceRanges && !Ranges.empty()) { |
| 843 | FileID CaretFileID = Loc.getExpansionLoc().getFileID(); |
| 844 | bool PrintedRange = false; |
| 845 | |
| 846 | for (ArrayRef<CharSourceRange>::const_iterator RI = Ranges.begin(), |
| 847 | RE = Ranges.end(); |
| 848 | RI != RE; ++RI) { |
| 849 | |
| 850 | if (!RI->isValid()) continue; |
| 851 | |
| 852 | auto &SM = Loc.getManager(); |
| 853 | SourceLocation B = SM.getExpansionLoc(RI->getBegin()); |
| 854 | CharSourceRange ERange = SM.getExpansionRange(RI->getEnd()); |
| 855 | SourceLocation E = ERange.getEnd(); |
| 856 | bool IsTokenRange = ERange.isTokenRange(); |
| 857 | |
| 858 | std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(B); |
| 859 | std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(E); |
| 860 | |
| 861 | |
| 862 | |
| 863 | if (BInfo.first != CaretFileID || EInfo.first != CaretFileID) |
| 864 | continue; |
| 865 | |
| 866 | |
| 867 | |
| 868 | unsigned TokSize = 0; |
| 869 | if (IsTokenRange) |
| 870 | TokSize = Lexer::MeasureTokenLength(E, SM, LangOpts); |
| 871 | |
| 872 | FullSourceLoc BF(B, SM), EF(E, SM); |
| 873 | OS << '{' |
| 874 | << BF.getLineNumber() << ':' << BF.getColumnNumber() << '-' |
| 875 | << EF.getLineNumber() << ':' << (EF.getColumnNumber() + TokSize) |
| 876 | << '}'; |
| 877 | PrintedRange = true; |
| 878 | } |
| 879 | |
| 880 | if (PrintedRange) |
| 881 | OS << ':'; |
| 882 | } |
| 883 | OS << ' '; |
| 884 | } |
| 885 | |
| 886 | void TextDiagnostic::emitIncludeLocation(FullSourceLoc Loc, PresumedLoc PLoc) { |
| 887 | if (DiagOpts->ShowLocation && PLoc.isValid()) |
| 888 | OS << "In file included from " << PLoc.getFilename() << ':' |
| 889 | << PLoc.getLine() << ":\n"; |
| 890 | else |
| 891 | OS << "In included file:\n"; |
| 892 | } |
| 893 | |
| 894 | void TextDiagnostic::emitImportLocation(FullSourceLoc Loc, PresumedLoc PLoc, |
| 895 | StringRef ModuleName) { |
| 896 | if (DiagOpts->ShowLocation && PLoc.isValid()) |
| 897 | OS << "In module '" << ModuleName << "' imported from " |
| 898 | << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n"; |
| 899 | else |
| 900 | OS << "In module '" << ModuleName << "':\n"; |
| 901 | } |
| 902 | |
| 903 | void TextDiagnostic::emitBuildingModuleLocation(FullSourceLoc Loc, |
| 904 | PresumedLoc PLoc, |
| 905 | StringRef ModuleName) { |
| 906 | if (DiagOpts->ShowLocation && PLoc.isValid()) |
| 907 | OS << "While building module '" << ModuleName << "' imported from " |
| 908 | << PLoc.getFilename() << ':' << PLoc.getLine() << ":\n"; |
| 909 | else |
| 910 | OS << "While building module '" << ModuleName << "':\n"; |
| 911 | } |
| 912 | |
| 913 | |
| 914 | static llvm::Optional<std::pair<unsigned, unsigned>> |
| 915 | findLinesForRange(const CharSourceRange &R, FileID FID, |
| 916 | const SourceManager &SM) { |
| 917 | if (!R.isValid()) return None; |
| 918 | |
| 919 | SourceLocation Begin = R.getBegin(); |
| 920 | SourceLocation End = R.getEnd(); |
| 921 | if (SM.getFileID(Begin) != FID || SM.getFileID(End) != FID) |
| 922 | return None; |
| 923 | |
| 924 | return std::make_pair(SM.getExpansionLineNumber(Begin), |
| 925 | SM.getExpansionLineNumber(End)); |
| 926 | } |
| 927 | |
| 928 | |
| 929 | |
| 930 | static std::pair<unsigned, unsigned> |
| 931 | maybeAddRange(std::pair<unsigned, unsigned> A, std::pair<unsigned, unsigned> B, |
| 932 | unsigned MaxRange) { |
| 933 | |
| 934 | unsigned Slack = MaxRange - (A.second - A.first + 1); |
| 935 | if (Slack == 0) |
| 936 | return A; |
| 937 | |
| 938 | |
| 939 | unsigned Min = std::min(A.first, B.first); |
| 940 | unsigned Max = std::max(A.second, B.second); |
| 941 | if (Max - Min + 1 <= MaxRange) |
| 942 | return {Min, Max}; |
| 943 | |
| 944 | |
| 945 | |
| 946 | if ((B.first > A.first && B.first - A.first + 1 > MaxRange) || |
| 947 | (B.second < A.second && A.second - B.second + 1 > MaxRange)) |
| 948 | return A; |
| 949 | |
| 950 | |
| 951 | |
| 952 | |
| 953 | |
| 954 | |
| 955 | |
| 956 | A.second = std::min(A.second + (Slack + 1) / 2, Max); |
| 957 | Slack = MaxRange - (A.second - A.first + 1); |
| 958 | A.first = std::max(Min + Slack, A.first) - Slack; |
| 959 | A.second = std::min(A.first + MaxRange - 1, Max); |
| 960 | return A; |
| 961 | } |
| 962 | |
| 963 | |
| 964 | static void highlightRange(const CharSourceRange &R, |
| 965 | unsigned LineNo, FileID FID, |
| 966 | const SourceColumnMap &map, |
| 967 | std::string &CaretLine, |
| 968 | const SourceManager &SM, |
| 969 | const LangOptions &LangOpts) { |
| 970 | if (!R.isValid()) return; |
| 971 | |
| 972 | SourceLocation Begin = R.getBegin(); |
| 973 | SourceLocation End = R.getEnd(); |
| 974 | |
| 975 | unsigned StartLineNo = SM.getExpansionLineNumber(Begin); |
| 976 | if (StartLineNo > LineNo || SM.getFileID(Begin) != FID) |
| 977 | return; |
| 978 | |
| 979 | unsigned EndLineNo = SM.getExpansionLineNumber(End); |
| 980 | if (EndLineNo < LineNo || SM.getFileID(End) != FID) |
| 981 | return; |
| 982 | |
| 983 | |
| 984 | unsigned StartColNo = 0; |
| 985 | if (StartLineNo == LineNo) { |
| 986 | StartColNo = SM.getExpansionColumnNumber(Begin); |
| 987 | if (StartColNo) --StartColNo; |
| 988 | } |
| 989 | |
| 990 | |
| 991 | unsigned EndColNo = map.getSourceLine().size(); |
| 992 | if (EndLineNo == LineNo) { |
| 993 | EndColNo = SM.getExpansionColumnNumber(End); |
| 994 | if (EndColNo) { |
| 995 | --EndColNo; |
| 996 | |
| 997 | |
| 998 | |
| 999 | if (R.isTokenRange()) |
| 1000 | EndColNo += Lexer::MeasureTokenLength(End, SM, LangOpts); |
| 1001 | } else { |
| 1002 | EndColNo = CaretLine.size(); |
| 1003 | } |
| 1004 | } |
| 1005 | |
| 1006 | (0) . __assert_fail ("StartColNo <= EndColNo && \"Invalid range!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 1006, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(StartColNo <= EndColNo && "Invalid range!"); |
| 1007 | |
| 1008 | |
| 1009 | if (R.isTokenRange()) { |
| 1010 | |
| 1011 | while (StartColNo < map.getSourceLine().size() && |
| 1012 | (map.getSourceLine()[StartColNo] == ' ' || |
| 1013 | map.getSourceLine()[StartColNo] == '\t')) |
| 1014 | StartColNo = map.startOfNextColumn(StartColNo); |
| 1015 | |
| 1016 | |
| 1017 | if (EndColNo > map.getSourceLine().size()) |
| 1018 | EndColNo = map.getSourceLine().size(); |
| 1019 | while (EndColNo && |
| 1020 | (map.getSourceLine()[EndColNo-1] == ' ' || |
| 1021 | map.getSourceLine()[EndColNo-1] == '\t')) |
| 1022 | EndColNo = map.startOfPreviousColumn(EndColNo); |
| 1023 | |
| 1024 | |
| 1025 | |
| 1026 | |
| 1027 | if (StartColNo > EndColNo) { |
| 1028 | (0) . __assert_fail ("StartLineNo != EndLineNo && \"trying to highlight whitespace\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 1028, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(StartLineNo != EndLineNo && "trying to highlight whitespace"); |
| 1029 | StartColNo = EndColNo; |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | (0) . __assert_fail ("StartColNo <= map.getSourceLine().size() && \"Invalid range!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 1033, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(StartColNo <= map.getSourceLine().size() && "Invalid range!"); |
| 1034 | (0) . __assert_fail ("EndColNo <= map.getSourceLine().size() && \"Invalid range!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 1034, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(EndColNo <= map.getSourceLine().size() && "Invalid range!"); |
| 1035 | |
| 1036 | |
| 1037 | StartColNo = map.byteToContainingColumn(StartColNo); |
| 1038 | EndColNo = map.byteToContainingColumn(EndColNo); |
| 1039 | |
| 1040 | (0) . __assert_fail ("StartColNo <= EndColNo && \"Invalid range!\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 1040, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(StartColNo <= EndColNo && "Invalid range!"); |
| 1041 | if (CaretLine.size() < EndColNo) |
| 1042 | CaretLine.resize(EndColNo,' '); |
| 1043 | std::fill(CaretLine.begin()+StartColNo,CaretLine.begin()+EndColNo,'~'); |
| 1044 | } |
| 1045 | |
| 1046 | static std::string buildFixItInsertionLine(FileID FID, |
| 1047 | unsigned LineNo, |
| 1048 | const SourceColumnMap &map, |
| 1049 | ArrayRef<FixItHint> Hints, |
| 1050 | const SourceManager &SM, |
| 1051 | const DiagnosticOptions *DiagOpts) { |
| 1052 | std::string FixItInsertionLine; |
| 1053 | if (Hints.empty() || !DiagOpts->ShowFixits) |
| 1054 | return FixItInsertionLine; |
| 1055 | unsigned PrevHintEndCol = 0; |
| 1056 | |
| 1057 | for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end(); |
| 1058 | I != E; ++I) { |
| 1059 | if (!I->CodeToInsert.empty()) { |
| 1060 | |
| 1061 | |
| 1062 | std::pair<FileID, unsigned> HintLocInfo |
| 1063 | = SM.getDecomposedExpansionLoc(I->RemoveRange.getBegin()); |
| 1064 | if (FID == HintLocInfo.first && |
| 1065 | LineNo == SM.getLineNumber(HintLocInfo.first, HintLocInfo.second) && |
| 1066 | StringRef(I->CodeToInsert).find_first_of("\n\r") == StringRef::npos) { |
| 1067 | |
| 1068 | |
| 1069 | |
| 1070 | |
| 1071 | |
| 1072 | unsigned HintByteOffset |
| 1073 | = SM.getColumnNumber(HintLocInfo.first, HintLocInfo.second) - 1; |
| 1074 | |
| 1075 | |
| 1076 | (map.bytes())+1", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 1076, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(HintByteOffset < static_cast<unsigned>(map.bytes())+1); |
| 1077 | unsigned HintCol = map.byteToContainingColumn(HintByteOffset); |
| 1078 | |
| 1079 | |
| 1080 | |
| 1081 | |
| 1082 | |
| 1083 | |
| 1084 | |
| 1085 | |
| 1086 | if (HintCol < PrevHintEndCol) |
| 1087 | HintCol = PrevHintEndCol + 1; |
| 1088 | |
| 1089 | |
| 1090 | |
| 1091 | unsigned NewFixItLineSize = FixItInsertionLine.size() + |
| 1092 | (HintCol - PrevHintEndCol) + I->CodeToInsert.size(); |
| 1093 | if (NewFixItLineSize > FixItInsertionLine.size()) |
| 1094 | FixItInsertionLine.resize(NewFixItLineSize, ' '); |
| 1095 | |
| 1096 | std::copy(I->CodeToInsert.begin(), I->CodeToInsert.end(), |
| 1097 | FixItInsertionLine.end() - I->CodeToInsert.size()); |
| 1098 | |
| 1099 | PrevHintEndCol = |
| 1100 | HintCol + llvm::sys::locale::columnWidth(I->CodeToInsert); |
| 1101 | } |
| 1102 | } |
| 1103 | } |
| 1104 | |
| 1105 | expandTabs(FixItInsertionLine, DiagOpts->TabStop); |
| 1106 | |
| 1107 | return FixItInsertionLine; |
| 1108 | } |
| 1109 | |
| 1110 | |
| 1111 | |
| 1112 | |
| 1113 | |
| 1114 | |
| 1115 | |
| 1116 | |
| 1117 | void TextDiagnostic::emitSnippetAndCaret( |
| 1118 | FullSourceLoc Loc, DiagnosticsEngine::Level Level, |
| 1119 | SmallVectorImpl<CharSourceRange> &Ranges, ArrayRef<FixItHint> Hints) { |
| 1120 | (0) . __assert_fail ("Loc.isValid() && \"must have a valid source location here\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 1120, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Loc.isValid() && "must have a valid source location here"); |
| 1121 | (0) . __assert_fail ("Loc.isFileID() && \"must have a file location here\"", "/home/seafit/code_projects/clang_source/clang/lib/Frontend/TextDiagnostic.cpp", 1121, __PRETTY_FUNCTION__))" file_link="../../../include/assert.h.html#88" macro="true">assert(Loc.isFileID() && "must have a file location here"); |
| 1122 | |
| 1123 | |
| 1124 | |
| 1125 | |
| 1126 | |
| 1127 | |
| 1128 | |
| 1129 | if (!DiagOpts->ShowCarets) |
| 1130 | return; |
| 1131 | if (Loc == LastLoc && Ranges.empty() && Hints.empty() && |
| 1132 | (LastLevel != DiagnosticsEngine::Note || Level == LastLevel)) |
| 1133 | return; |
| 1134 | |
| 1135 | |
| 1136 | std::pair<FileID, unsigned> LocInfo = Loc.getDecomposedLoc(); |
| 1137 | FileID FID = LocInfo.first; |
| 1138 | const SourceManager &SM = Loc.getManager(); |
| 1139 | |
| 1140 | |
| 1141 | bool Invalid = false; |
| 1142 | StringRef BufData = Loc.getBufferData(&Invalid); |
| 1143 | if (Invalid) |
| 1144 | return; |
| 1145 | |
| 1146 | unsigned CaretLineNo = Loc.getLineNumber(); |
| 1147 | unsigned CaretColNo = Loc.getColumnNumber(); |
| 1148 | |
| 1149 | |
| 1150 | static const size_t MaxLineLengthToPrint = 4096; |
| 1151 | if (CaretColNo > MaxLineLengthToPrint) |
| 1152 | return; |
| 1153 | |
| 1154 | |
| 1155 | const unsigned MaxLines = DiagOpts->SnippetLineLimit; |
| 1156 | std::pair<unsigned, unsigned> Lines = {CaretLineNo, CaretLineNo}; |
| 1157 | for (SmallVectorImpl<CharSourceRange>::iterator I = Ranges.begin(), |
| 1158 | E = Ranges.end(); |
| 1159 | I != E; ++I) |
| 1160 | if (auto OptionalRange = findLinesForRange(*I, FID, SM)) |
| 1161 | Lines = maybeAddRange(Lines, *OptionalRange, MaxLines); |
| 1162 | |
| 1163 | for (unsigned LineNo = Lines.first; LineNo != Lines.second + 1; ++LineNo) { |
| 1164 | const char *BufStart = BufData.data(); |
| 1165 | const char *BufEnd = BufStart + BufData.size(); |
| 1166 | |
| 1167 | |
| 1168 | const char *LineStart = |
| 1169 | BufStart + |
| 1170 | SM.getDecomposedLoc(SM.translateLineCol(FID, LineNo, 1)).second; |
| 1171 | if (LineStart == BufEnd) |
| 1172 | break; |
| 1173 | |
| 1174 | |
| 1175 | const char *LineEnd = LineStart; |
| 1176 | while (*LineEnd != '\n' && *LineEnd != '\r' && LineEnd != BufEnd) |
| 1177 | ++LineEnd; |
| 1178 | |
| 1179 | |
| 1180 | |
| 1181 | if (size_t(LineEnd - LineStart) > MaxLineLengthToPrint) |
| 1182 | return; |
| 1183 | |
| 1184 | |
| 1185 | StringRef Line(LineStart, LineEnd - LineStart); |
| 1186 | while (!Line.empty() && Line.back() == '\0' && |
| 1187 | (LineNo != CaretLineNo || Line.size() > CaretColNo)) |
| 1188 | Line = Line.drop_back(); |
| 1189 | |
| 1190 | |
| 1191 | std::string SourceLine(Line.begin(), Line.end()); |
| 1192 | |
| 1193 | |
| 1194 | const SourceColumnMap sourceColMap(SourceLine, DiagOpts->TabStop); |
| 1195 | |
| 1196 | |
| 1197 | |
| 1198 | std::string CaretLine(sourceColMap.columns(), ' '); |
| 1199 | |
| 1200 | |
| 1201 | for (SmallVectorImpl<CharSourceRange>::iterator I = Ranges.begin(), |
| 1202 | E = Ranges.end(); |
| 1203 | I != E; ++I) |
| 1204 | highlightRange(*I, LineNo, FID, sourceColMap, CaretLine, SM, LangOpts); |
| 1205 | |
| 1206 | |
| 1207 | if (CaretLineNo == LineNo) { |
| 1208 | CaretColNo = sourceColMap.byteToContainingColumn(CaretColNo - 1); |
| 1209 | if (CaretLine.size() < CaretColNo + 1) |
| 1210 | CaretLine.resize(CaretColNo + 1, ' '); |
| 1211 | CaretLine[CaretColNo] = '^'; |
| 1212 | } |
| 1213 | |
| 1214 | std::string FixItInsertionLine = buildFixItInsertionLine( |
| 1215 | FID, LineNo, sourceColMap, Hints, SM, DiagOpts.get()); |
| 1216 | |
| 1217 | |
| 1218 | |
| 1219 | unsigned Columns = DiagOpts->MessageLength; |
| 1220 | if (Columns) |
| 1221 | selectInterestingSourceRegion(SourceLine, CaretLine, FixItInsertionLine, |
| 1222 | Columns, sourceColMap); |
| 1223 | |
| 1224 | |
| 1225 | |
| 1226 | |
| 1227 | |
| 1228 | if (DiagOpts->ShowSourceRanges) { |
| 1229 | SourceLine = ' ' + SourceLine; |
| 1230 | CaretLine = ' ' + CaretLine; |
| 1231 | } |
| 1232 | |
| 1233 | |
| 1234 | while (!CaretLine.empty() && CaretLine[CaretLine.size() - 1] == ' ') |
| 1235 | CaretLine.erase(CaretLine.end() - 1); |
| 1236 | |
| 1237 | |
| 1238 | emitSnippet(SourceLine); |
| 1239 | |
| 1240 | if (!CaretLine.empty()) { |
| 1241 | if (DiagOpts->ShowColors) |
| 1242 | OS.changeColor(caretColor, true); |
| 1243 | OS << CaretLine << '\n'; |
| 1244 | if (DiagOpts->ShowColors) |
| 1245 | OS.resetColor(); |
| 1246 | } |
| 1247 | |
| 1248 | if (!FixItInsertionLine.empty()) { |
| 1249 | if (DiagOpts->ShowColors) |
| 1250 | |
| 1251 | OS.changeColor(fixitColor, false); |
| 1252 | if (DiagOpts->ShowSourceRanges) |
| 1253 | OS << ' '; |
| 1254 | OS << FixItInsertionLine << '\n'; |
| 1255 | if (DiagOpts->ShowColors) |
| 1256 | OS.resetColor(); |
| 1257 | } |
| 1258 | } |
| 1259 | |
| 1260 | |
| 1261 | emitParseableFixits(Hints, SM); |
| 1262 | } |
| 1263 | |
| 1264 | void TextDiagnostic::emitSnippet(StringRef line) { |
| 1265 | if (line.empty()) |
| 1266 | return; |
| 1267 | |
| 1268 | size_t i = 0; |
| 1269 | |
| 1270 | std::string to_print; |
| 1271 | bool print_reversed = false; |
| 1272 | |
| 1273 | while (i<line.size()) { |
| 1274 | std::pair<SmallString<16>,bool> res |
| 1275 | = printableTextForNextCharacter(line, &i, DiagOpts->TabStop); |
| 1276 | bool was_printable = res.second; |
| 1277 | |
| 1278 | if (DiagOpts->ShowColors && was_printable == print_reversed) { |
| 1279 | if (print_reversed) |
| 1280 | OS.reverseColor(); |
| 1281 | OS << to_print; |
| 1282 | to_print.clear(); |
| 1283 | if (DiagOpts->ShowColors) |
| 1284 | OS.resetColor(); |
| 1285 | } |
| 1286 | |
| 1287 | print_reversed = !was_printable; |
| 1288 | to_print += res.first.str(); |
| 1289 | } |
| 1290 | |
| 1291 | if (print_reversed && DiagOpts->ShowColors) |
| 1292 | OS.reverseColor(); |
| 1293 | OS << to_print; |
| 1294 | if (print_reversed && DiagOpts->ShowColors) |
| 1295 | OS.resetColor(); |
| 1296 | |
| 1297 | OS << '\n'; |
| 1298 | } |
| 1299 | |
| 1300 | void TextDiagnostic::emitParseableFixits(ArrayRef<FixItHint> Hints, |
| 1301 | const SourceManager &SM) { |
| 1302 | if (!DiagOpts->ShowParseableFixits) |
| 1303 | return; |
| 1304 | |
| 1305 | |
| 1306 | |
| 1307 | for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end(); |
| 1308 | I != E; ++I) { |
| 1309 | if (I->RemoveRange.isInvalid() || |
| 1310 | I->RemoveRange.getBegin().isMacroID() || |
| 1311 | I->RemoveRange.getEnd().isMacroID()) |
| 1312 | return; |
| 1313 | } |
| 1314 | |
| 1315 | for (ArrayRef<FixItHint>::iterator I = Hints.begin(), E = Hints.end(); |
| 1316 | I != E; ++I) { |
| 1317 | SourceLocation BLoc = I->RemoveRange.getBegin(); |
| 1318 | SourceLocation ELoc = I->RemoveRange.getEnd(); |
| 1319 | |
| 1320 | std::pair<FileID, unsigned> BInfo = SM.getDecomposedLoc(BLoc); |
| 1321 | std::pair<FileID, unsigned> EInfo = SM.getDecomposedLoc(ELoc); |
| 1322 | |
| 1323 | |
| 1324 | if (I->RemoveRange.isTokenRange()) |
| 1325 | EInfo.second += Lexer::MeasureTokenLength(ELoc, SM, LangOpts); |
| 1326 | |
| 1327 | |
| 1328 | |
| 1329 | PresumedLoc PLoc = SM.getPresumedLoc(BLoc); |
| 1330 | if (PLoc.isInvalid()) |
| 1331 | break; |
| 1332 | |
| 1333 | OS << "fix-it:\""; |
| 1334 | OS.write_escaped(PLoc.getFilename()); |
| 1335 | OS << "\":{" << SM.getLineNumber(BInfo.first, BInfo.second) |
| 1336 | << ':' << SM.getColumnNumber(BInfo.first, BInfo.second) |
| 1337 | << '-' << SM.getLineNumber(EInfo.first, EInfo.second) |
| 1338 | << ':' << SM.getColumnNumber(EInfo.first, EInfo.second) |
| 1339 | << "}:\""; |
| 1340 | OS.write_escaped(I->CodeToInsert); |
| 1341 | OS << "\"\n"; |
| 1342 | } |
| 1343 | } |
| 1344 | |