Skip to content

Commit

Permalink
Support pre-loaded buffers in SourceLoader
Browse files Browse the repository at this point in the history
  • Loading branch information
povik committed Oct 22, 2024
1 parent f4e8ec4 commit 52781f5
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 3 deletions.
13 changes: 12 additions & 1 deletion include/slang/driver/SourceLoader.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
#include <span>
#include <vector>

#include "slang/text/SourceLocation.h"
#include "slang/syntax/SyntaxFwd.h"
#include "slang/text/Glob.h"
#include "slang/util/Hash.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;

// 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),
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

0 comments on commit 52781f5

Please sign in to comment.