From 52781f5f8fd514a7155f76042508559ff83191e1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Povi=C5=A1er?= Date: Tue, 22 Oct 2024 18:38:41 +0200 Subject: [PATCH 1/3] Support pre-loaded buffers in SourceLoader --- include/slang/driver/SourceLoader.h | 13 ++++++++++++- source/driver/SourceLoader.cpp | 18 ++++++++++++++++-- 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/include/slang/driver/SourceLoader.h b/include/slang/driver/SourceLoader.h index f84868bb3..486a4927f 100644 --- a/include/slang/driver/SourceLoader.h +++ b/include/slang/driver/SourceLoader.h @@ -15,6 +15,7 @@ #include #include +#include "slang/text/SourceLocation.h" #include "slang/syntax/SyntaxFwd.h" #include "slang/text/Glob.h" #include "slang/util/Hash.h" @@ -66,6 +67,9 @@ class SLANG_EXPORT SourceLoader { SourceLoader(const SourceLoader& other) = delete; SourceLoader(SourceLoader&& other) = default; + /// @brief Adds a pre-loaded buffer + void addBuffer(SourceBuffer buffer); + /// @brief Adds files to be loaded, specified via the given @a pattern. /// /// All of the files that match the pattern will be added for loading. @@ -159,6 +163,10 @@ class SLANG_EXPORT SourceLoader { // The filesystem path (as specified by the user). std::filesystem::path path; + // An optional pre-loaded buffer for when the source doesn't originate + // from the filesystem + const SourceBuffer preloadedBuffer; + // The library to which the file belongs, if any. const SourceLibrary* library = nullptr; @@ -186,8 +194,11 @@ class SLANG_EXPORT SourceLoader { FileEntry(std::filesystem::path&& path, bool isLibraryFile, const SourceLibrary* library, const UnitEntry* unit, GlobRank libraryRank) : - path(std::move(path)), library(library), unit(unit), libraryRank(libraryRank), + path(std::move(path)), preloadedBuffer(), library(library), unit(unit), libraryRank(libraryRank), isLibraryFile(isLibraryFile) {} + + FileEntry(SourceBuffer buffer) + : preloadedBuffer(buffer), libraryRank(GlobRank::ExactPath) {} }; // The result of a loadAndParse call. diff --git a/source/driver/SourceLoader.cpp b/source/driver/SourceLoader.cpp index a2fd7506b..a006b6d88 100644 --- a/source/driver/SourceLoader.cpp +++ b/source/driver/SourceLoader.cpp @@ -31,6 +31,10 @@ SourceLoader::SourceLoader(SourceManager& sourceManager) : sourceManager(sourceM searchExtensions.emplace_back(ext); } +void SourceLoader::addBuffer(SourceBuffer buffer) { + fileEntries.emplace_back(buffer); +} + void SourceLoader::addFiles(std::string_view pattern) { addFilesInternal(pattern, {}, /* isLibraryFile */ false, /* library */ nullptr, /* unit */ nullptr, @@ -157,7 +161,12 @@ std::vector SourceLoader::loadSources() { results.reserve(fileEntries.size()); for (auto& entry : fileEntries) { - auto buffer = sourceManager.readSource(entry.path, entry.library); + SourceManager::BufferOrError buffer; + if (!entry.preloadedBuffer) + buffer = sourceManager.readSource(entry.path, entry.library); + else + buffer = entry.preloadedBuffer; + if (!buffer) addError(entry.path, buffer.error()); else @@ -527,7 +536,12 @@ SourceLoader::LoadResult SourceLoader::loadAndParse(const FileEntry& entry, cons uint64_t fileSortKey) { // TODO: error if secondLib is set - auto buffer = sourceManager.readSource(entry.path, entry.library, fileSortKey); + SourceManager::BufferOrError buffer; + if (entry.preloadedBuffer) + buffer = entry.preloadedBuffer; + else + buffer = sourceManager.readSource(entry.path, entry.library, fileSortKey); + if (!buffer) return std::pair{&entry, buffer.error()}; From 520ba5f503dabeb6d36bc87e9f7b49a42c535eac Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 22 Oct 2024 16:47:07 +0000 Subject: [PATCH 2/3] style: pre-commit fixes --- include/slang/driver/SourceLoader.h | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/include/slang/driver/SourceLoader.h b/include/slang/driver/SourceLoader.h index 486a4927f..8d58e7ed5 100644 --- a/include/slang/driver/SourceLoader.h +++ b/include/slang/driver/SourceLoader.h @@ -15,9 +15,9 @@ #include #include -#include "slang/text/SourceLocation.h" #include "slang/syntax/SyntaxFwd.h" #include "slang/text/Glob.h" +#include "slang/text/SourceLocation.h" #include "slang/util/Hash.h" #include "slang/util/Util.h" @@ -194,11 +194,11 @@ class SLANG_EXPORT SourceLoader { FileEntry(std::filesystem::path&& path, bool isLibraryFile, const SourceLibrary* library, const UnitEntry* unit, GlobRank libraryRank) : - path(std::move(path)), preloadedBuffer(), library(library), unit(unit), libraryRank(libraryRank), - isLibraryFile(isLibraryFile) {} + path(std::move(path)), preloadedBuffer(), library(library), unit(unit), + libraryRank(libraryRank), isLibraryFile(isLibraryFile) {} - FileEntry(SourceBuffer buffer) - : preloadedBuffer(buffer), libraryRank(GlobRank::ExactPath) {} + FileEntry(SourceBuffer buffer) : + preloadedBuffer(buffer), libraryRank(GlobRank::ExactPath) {} }; // The result of a loadAndParse call. From f19a57fbbf51432a705397a35f2f1bef45f5af6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20Povi=C5=A1er?= Date: Fri, 25 Oct 2024 17:52:40 +0200 Subject: [PATCH 3/3] Rm const qualifier --- include/slang/driver/SourceLoader.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/slang/driver/SourceLoader.h b/include/slang/driver/SourceLoader.h index 8d58e7ed5..c80964add 100644 --- a/include/slang/driver/SourceLoader.h +++ b/include/slang/driver/SourceLoader.h @@ -165,7 +165,7 @@ class SLANG_EXPORT SourceLoader { // An optional pre-loaded buffer for when the source doesn't originate // from the filesystem - const SourceBuffer preloadedBuffer; + SourceBuffer preloadedBuffer; // The library to which the file belongs, if any. const SourceLibrary* library = nullptr;