|
35 | 35 | #include <cstring> |
36 | 36 | #include <fstream> |
37 | 37 | #include <iomanip> |
| 38 | +#include <ios> |
| 39 | +#include <list> |
38 | 40 | #include <sstream> |
39 | 41 | #include <string> |
40 | 42 | #include <unordered_map> |
41 | 43 | #include <utility> |
| 44 | +#include <vector> |
42 | 45 |
|
43 | 46 | #include "xml.h" |
44 | 47 |
|
@@ -544,13 +547,57 @@ std::string ErrorMessage::toXML() const |
544 | 547 | return printer.CStr(); |
545 | 548 | } |
546 | 549 |
|
| 550 | +// Byte offset of the start of each line, indexed by 1-based line number. |
| 551 | +// Building this once per file keeps readCode() cheap. Without it every call |
| 552 | +// rescans the file from the start, which is quadratic for a file that produces |
| 553 | +// many findings - vendor/generated headers routinely produce tens of thousands. |
| 554 | +static const std::vector<std::streamoff>* getLineOffsets(const std::string &file) |
| 555 | +{ |
| 556 | + // thread_local: the thread executor calls this concurrently |
| 557 | + static thread_local std::list<std::pair<std::string, std::vector<std::streamoff>>> cache; |
| 558 | + |
| 559 | + for (auto it = cache.begin(); it != cache.end(); ++it) { |
| 560 | + if (it->first == file) { |
| 561 | + // most recently used goes first |
| 562 | + cache.splice(cache.begin(), cache, it); |
| 563 | + return &cache.front().second; |
| 564 | + } |
| 565 | + } |
| 566 | + |
| 567 | + // Binary mode so the offsets match what the seek in readCode() expects. |
| 568 | + // A trailing '\r' is stripped by the caller. |
| 569 | + std::ifstream fin(file, std::ios::binary); |
| 570 | + if (!fin.is_open()) |
| 571 | + return nullptr; |
| 572 | + |
| 573 | + std::vector<std::streamoff> offsets{0, 0}; // index 0 is unused, line 1 starts at offset 0 |
| 574 | + std::array<char, 64 * 1024> buf; |
| 575 | + std::streamoff pos = 0; |
| 576 | + while (fin.read(buf.data(), buf.size()) || fin.gcount() > 0) { |
| 577 | + const std::streamsize n = fin.gcount(); |
| 578 | + for (std::streamsize i = 0; i < n; ++i) { |
| 579 | + if (buf[i] == '\n') |
| 580 | + offsets.push_back(pos + i + 1); |
| 581 | + } |
| 582 | + pos += n; |
| 583 | + } |
| 584 | + |
| 585 | + constexpr std::size_t maxCachedFiles = 4; |
| 586 | + if (cache.size() >= maxCachedFiles) |
| 587 | + cache.pop_back(); |
| 588 | + cache.emplace_front(file, std::move(offsets)); |
| 589 | + return &cache.front().second; |
| 590 | +} |
| 591 | + |
547 | 592 | // TODO: read info from some shared resource instead? |
548 | 593 | static std::string readCode(const std::string &file, int linenr, int column, const char endl[]) |
549 | 594 | { |
550 | | - std::ifstream fin(file); |
551 | 595 | std::string line; |
552 | | - while (linenr > 0 && std::getline(fin,line)) { |
553 | | - linenr--; |
| 596 | + const std::vector<std::streamoff>* const offsets = getLineOffsets(file); |
| 597 | + if (offsets && linenr > 0 && linenr < static_cast<int>(offsets->size())) { |
| 598 | + std::ifstream fin(file, std::ios::binary); |
| 599 | + fin.seekg((*offsets)[linenr]); |
| 600 | + std::getline(fin, line); |
554 | 601 | } |
555 | 602 | const std::string::size_type endPos = line.find_last_not_of("\r\n\t "); |
556 | 603 | if (endPos + 1 < line.size()) |
|
0 commit comments