|
36 | 36 | #include <cstring> |
37 | 37 | #include <fstream> |
38 | 38 | #include <iomanip> |
39 | | -#include <numeric> |
| 39 | +#include <ios> |
| 40 | +#include <list> |
40 | 41 | #include <sstream> |
41 | 42 | #include <string> |
42 | 43 | #include <unordered_map> |
43 | 44 | #include <utility> |
| 45 | +#include <vector> |
44 | 46 |
|
45 | 47 | #include "xml.h" |
46 | 48 |
|
@@ -638,13 +640,57 @@ std::string ErrorMessage::toXML() const |
638 | 640 | return printer.CStr(); |
639 | 641 | } |
640 | 642 |
|
| 643 | +// Byte offset of the start of each line, indexed by 1-based line number. |
| 644 | +// Building this once per file keeps readCode() cheap. Without it every call |
| 645 | +// rescans the file from the start, which is quadratic for a file that produces |
| 646 | +// many findings - vendor/generated headers routinely produce tens of thousands. |
| 647 | +static const std::vector<std::streamoff>* getLineOffsets(const std::string &file) |
| 648 | +{ |
| 649 | + // thread_local: the thread executor calls this concurrently |
| 650 | + static thread_local std::list<std::pair<std::string, std::vector<std::streamoff>>> cache; |
| 651 | + |
| 652 | + for (auto it = cache.begin(); it != cache.end(); ++it) { |
| 653 | + if (it->first == file) { |
| 654 | + // most recently used goes first |
| 655 | + cache.splice(cache.begin(), cache, it); |
| 656 | + return &cache.front().second; |
| 657 | + } |
| 658 | + } |
| 659 | + |
| 660 | + // Binary mode so the offsets match what the seek in readCode() expects. |
| 661 | + // A trailing '\r' is stripped by the caller. |
| 662 | + std::ifstream fin(file, std::ios::binary); |
| 663 | + if (!fin.is_open()) |
| 664 | + return nullptr; |
| 665 | + |
| 666 | + std::vector<std::streamoff> offsets{0, 0}; // index 0 is unused, line 1 starts at offset 0 |
| 667 | + std::array<char, 64 * 1024> buf; |
| 668 | + std::streamoff pos = 0; |
| 669 | + while (fin.read(buf.data(), buf.size()) || fin.gcount() > 0) { |
| 670 | + const std::streamsize n = fin.gcount(); |
| 671 | + for (std::streamsize i = 0; i < n; ++i) { |
| 672 | + if (buf[i] == '\n') |
| 673 | + offsets.push_back(pos + i + 1); |
| 674 | + } |
| 675 | + pos += n; |
| 676 | + } |
| 677 | + |
| 678 | + constexpr std::size_t maxCachedFiles = 4; |
| 679 | + if (cache.size() >= maxCachedFiles) |
| 680 | + cache.pop_back(); |
| 681 | + cache.emplace_front(file, std::move(offsets)); |
| 682 | + return &cache.front().second; |
| 683 | +} |
| 684 | + |
641 | 685 | // TODO: read info from some shared resource instead? |
642 | 686 | static std::string readCode(const std::string &file, int linenr, int column, const char endl[]) |
643 | 687 | { |
644 | | - std::ifstream fin(file); |
645 | 688 | std::string line; |
646 | | - while (linenr > 0 && std::getline(fin,line)) { |
647 | | - linenr--; |
| 689 | + const std::vector<std::streamoff>* const offsets = getLineOffsets(file); |
| 690 | + if (offsets && linenr > 0 && linenr < static_cast<int>(offsets->size())) { |
| 691 | + std::ifstream fin(file, std::ios::binary); |
| 692 | + fin.seekg((*offsets)[linenr]); |
| 693 | + std::getline(fin, line); |
648 | 694 | } |
649 | 695 | const std::string::size_type endPos = line.find_last_not_of("\r\n\t "); |
650 | 696 | if (endPos + 1 < line.size()) |
|
0 commit comments