Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support pre-loaded buffers in SourceLoader #1164

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions include/slang/driver/SourceLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

#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"

Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
povik marked this conversation as resolved.
Show resolved Hide resolved

// The library to which the file belongs, if any.
const SourceLibrary* library = nullptr;

Expand Down Expand Up @@ -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),
isLibraryFile(isLibraryFile) {}
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.
Expand Down
18 changes: 16 additions & 2 deletions source/driver/SourceLoader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -157,7 +161,12 @@ std::vector<SourceBuffer> 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
Expand Down Expand Up @@ -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()};

Expand Down