slang::SourceManager class

SourceManager - Handles loading and tracking source files.

The source manager abstracts away the differences between locations in files and locations generated by macro expansion. See SourceLocation for more details.

The methods in this class are thread safe.

Public types

struct DiagnosticDirectiveInfo

Public functions

auto makeAbsolutePath(std::string_view path) const -> std::string
Convert the given relative path into an absolute path.
void addSystemDirectory(std::string_view path)
Adds a system include directory.
void addUserDirectory(std::string_view path)
Adds a user include directory.
auto getLineNumber(SourceLocation location) const -> size_t
Gets the source line number for a given source location.
auto getFileName(SourceLocation location) const -> std::string_view
Gets the source file name for a given source location.
auto getRawFileName(BufferID buffer) const -> std::string_view
auto getFullPath(BufferID buffer) const -> const std::filesystem::path&
auto getColumnNumber(SourceLocation location) const -> size_t
auto getIncludedFrom(BufferID buffer) const -> SourceLocation
auto getMacroName(SourceLocation location) const -> std::string_view
auto isFileLoc(SourceLocation location) const -> bool
Determines whether the given location exists in a source file.
auto isMacroLoc(SourceLocation location) const -> bool
Determines whether the given location points to a macro expansion.
auto isMacroArgLoc(SourceLocation location) const -> bool
Determines whether the given location points to a macro argument expansion.
auto isIncludedFileLoc(SourceLocation location) const -> bool
Determines whether the given location is inside an include file.
auto isPreprocessedLoc(SourceLocation location) const -> bool
Determines whether the given location is from a macro expansion or an include file.
auto isBeforeInCompilationUnit(SourceLocation left, SourceLocation right) const -> bool
auto getExpansionLoc(SourceLocation location) const -> SourceLocation
Gets the expansion location of a given macro location.
auto getExpansionRange(SourceLocation location) const -> SourceRange
Gets the expansion range of a given macro location.
auto getOriginalLoc(SourceLocation location) const -> SourceLocation
Gets the original source location of a given macro location.
auto getFullyOriginalLoc(SourceLocation location) const -> SourceLocation
auto getFullyExpandedLoc(SourceLocation location) const -> SourceLocation
auto getSourceText(BufferID buffer) const -> std::string_view
Gets the actual source text for a given file buffer.
auto createExpansionLoc(SourceLocation originalLoc, SourceRange expansionRange, bool isMacroArg) -> SourceLocation
Creates a macro expansion location; used by the preprocessor.
auto createExpansionLoc(SourceLocation originalLoc, SourceRange expansionRange, std::string_view macroName) -> SourceLocation
Creates a macro expansion location; used by the preprocessor.
auto assignText(std::string_view text, SourceLocation includedFrom = SourceLocation()) -> SourceBuffer
Instead of loading source from a file, copy it from text already in memory.
auto assignText(std::string_view path, std::string_view text, SourceLocation includedFrom = SourceLocation()) -> SourceBuffer
auto assignBuffer(std::string_view path, std::vector<char>&& buffer, SourceLocation includedFrom = SourceLocation()) -> SourceBuffer
auto readSource(const std::filesystem::path& path) -> SourceBuffer
Read in a source file from disk.
auto readHeader(std::string_view path, SourceLocation includedFrom, bool isSystemPath) -> SourceBuffer
Read in a header file from disk.
auto isCached(const std::filesystem::path& path) const -> bool
Returns true if the given file path is already loaded and cached in the source manager.
void setDisableProximatePaths(bool set)
void addLineDirective(SourceLocation location, size_t lineNum, std::string_view name, uint8_t level)
Adds a line directive at the given location.
void addDiagnosticDirective(SourceLocation location, std::string_view name, DiagnosticSeverity severity)
Adds a diagnostic directive at the given location.
template<typename Func>
void visitDiagnosticDirectives(Func&& func) const
auto getDiagnosticDirectives(BufferID buffer) const -> std::span<const DiagnosticDirectiveInfo>
auto getAllBuffers() const -> std::vector<BufferID>

Function documentation

std::string_view slang::SourceManager::getRawFileName(BufferID buffer) const

Gets the source file name for a given source buffer, not taking into account any `line directives that may be in the file.

const std::filesystem::path& slang::SourceManager::getFullPath(BufferID buffer) const

Gets the full path to the given source buffer. This does not take into account any `line directives. If the buffer is not a file buffer, returns an empty string.

size_t slang::SourceManager::getColumnNumber(SourceLocation location) const

Gets the column line number for a given source location. location must be a file location.

SourceLocation slang::SourceManager::getIncludedFrom(BufferID buffer) const

Gets a location that indicates from where the given buffer was included. location must be a file location.

std::string_view slang::SourceManager::getMacroName(SourceLocation location) const

Attempts to get the name of the macro represented by a macro location. If no macro name can be found, returns an empty string view.

bool slang::SourceManager::isBeforeInCompilationUnit(SourceLocation left, SourceLocation right) const

Determines whether the left location comes before the right location within the "compilation unit space", which is a hypothetical source space where all macros and include files have been expanded out into a flat file.

SourceLocation slang::SourceManager::getFullyOriginalLoc(SourceLocation location) const

Gets the actual original location where source is written, given a location inside a macro. Otherwise just returns the location itself.

SourceLocation slang::SourceManager::getFullyExpandedLoc(SourceLocation location) const

If the given location is a macro location, fully expands it out to its actual file expansion location. Otherwise just returns the location itself.

SourceBuffer slang::SourceManager::assignText(std::string_view path, std::string_view text, SourceLocation includedFrom = SourceLocation())

Instead of loading source from a file, copy it from text already in memory. Pretend it came from a file located at path.

SourceBuffer slang::SourceManager::assignBuffer(std::string_view path, std::vector<char>&& buffer, SourceLocation includedFrom = SourceLocation())

Instead of loading source from a file, move it from text already in memory. Pretend it came from a file located at path.

void slang::SourceManager::setDisableProximatePaths(bool set)

Sets whether filenames should be made "proximate" to the current directory for diagnostic reporting purposes. This is on by default but can be disabled to always use the simple filename.

template<typename Func>
void slang::SourceManager::visitDiagnosticDirectives(Func&& func) const

Visits each buffer that contains diagnostic directives and invokes the provided callback with the first argument being the buffer and the second being an iterable collection of DiagnosticDirectiveInfos.

std::span<const DiagnosticDirectiveInfo> slang::SourceManager::getDiagnosticDirectives(BufferID buffer) const

Gets the diagnostic directives associated with the given buffer, if any. Note that the returned span is not safe to store; the underlying data can be mutated by a call to addDiagnosticDirective and invalidate the span.

std::vector<BufferID> slang::SourceManager::getAllBuffers() const

Returns a list of buffers (files and macros) that have been created in the source manager.