[llvm] [llvm-strings] Use small buffer instead of reading whole file (PR #163073)
James Henderson via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 13 00:52:46 PDT 2026
================
@@ -88,41 +90,72 @@ static void parseIntArg(const opt::InputArgList &Args, int ID, T &Value) {
}
}
-static void strings(raw_ostream &OS, StringRef FileName, StringRef Contents) {
- auto print = [&OS, FileName](unsigned Offset, StringRef L) {
- if (L.size() < static_cast<size_t>(MinLength))
- return;
- if (PrintFileName)
- OS << FileName << ": ";
- switch (Radix) {
- case none:
- break;
- case octal:
- OS << format("%7o ", Offset);
- break;
- case hexadecimal:
- OS << format("%7x ", Offset);
- break;
- case decimal:
- OS << format("%7u ", Offset);
- break;
+static void strings(raw_ostream &OS, StringRef FileName,
+ sys::fs::file_t Handle) {
+ SmallString<sys::fs::DefaultReadChunkSize> Buffer;
+ SmallString<DefaultMinLength> Prefix;
+ auto print = [&OS, FileName, &Prefix](unsigned Offset, StringRef L) {
+ if (Prefix.size() + L.size() >= static_cast<size_t>(MinLength)) {
+ if (PrintFileName)
+ OS << FileName << ": ";
+ switch (Radix) {
+ case none:
+ break;
+ case octal:
+ OS << format("%7o ", Offset);
+ break;
+ case hexadecimal:
+ OS << format("%7x ", Offset);
+ break;
+ case decimal:
+ OS << format("%7u ", Offset);
+ break;
+ }
+ OS << Prefix << L << '\n';
}
- OS << L << '\n';
+ Prefix.clear();
};
- const char *B = Contents.begin();
- const char *P = nullptr, *E = nullptr, *S = nullptr;
- for (P = Contents.begin(), E = Contents.end(); P < E; ++P) {
- if (isPrint(*P) || *P == '\t') {
- if (S == nullptr)
- S = P;
- } else if (S) {
- print(S - B, StringRef(S, P - S));
- S = nullptr;
+ Buffer.resize_for_overwrite(sys::fs::DefaultReadChunkSize);
+ // Offset of the start of the current chunk within the file.
+ unsigned Offset = 0;
+ while (true) {
+ /*
+ * llvm-strings should be able to process a very large file on a
+ * memory-budgeted machine. To handle this, we read the file in chunk
+ * instead of allocate a very large memory and copy the whole file to the
+ * memory.
+ */
+ Expected<size_t> ReadBytesOrErr = sys::fs::readNativeFile(
+ Handle, MutableArrayRef(Buffer.data(), Buffer.size()));
+ if (!ReadBytesOrErr) {
+ errs() << FileName << ": "
+ << errorToErrorCode(ReadBytesOrErr.takeError()).message() << '\n';
----------------
jh7370 wrote:
Do we have testing for this error message? (I have no idea how practical it would be to test this specific case, so if it's not sensibly possible, don't worry about it).
https://github.com/llvm/llvm-project/pull/163073
More information about the llvm-commits
mailing list