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 unless otherwise noted.

Public types

struct DiagnosticDirectiveInfo
Stores information specified in a `pragma diagnostic directive, which alters the currently active set of diagnostic mappings.

Constructors, destructors, conversion operators

SourceManager()
Default constructor.

Public functions

auto addSystemDirectories(std::string_view pattern) -> std::error_code
Adds one or more system include directories that match the given pattern.
auto addUserDirectories(std::string_view pattern) -> std::error_code
Adds one or more user include directories that match the given pattern.
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
Gets the source file name for a given source buffer, not taking into account any `line directives that may be in the file.
auto getFullPath(BufferID buffer) const -> const std::filesystem::path&
Gets the full path to the given source buffer.
auto getColumnNumber(SourceLocation location) const -> size_t
Gets the column line number for a given source location.
auto getIncludedFrom(BufferID buffer) const -> SourceLocation
Gets a location that indicates from where the given buffer was included.
auto getLibraryFor(BufferID buffer) const -> const SourceLibrary*
Gets the source library of which the given buffer is a part, or nullptr if it's not explicitly part of any library.
auto getMacroName(SourceLocation location) const -> std::string_view
Attempts to get the name of the macro represented by a macro location.
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 -> std::optional<bool>
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.
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
Gets the actual original location where source is written, given a location inside a macro.
auto getFullyOriginalRange(SourceRange range) const -> SourceRange
Build the original location range where source is written.
auto getFullyExpandedLoc(SourceLocation location) const -> SourceLocation
If the given location is a macro location, fully expands it out to its actual file expansion location.
auto getSourceText(BufferID buffer) const -> std::string_view
Gets the actual source text for a given file buffer.
auto getSortKey(BufferID buffer) const -> uint64_t
Gets a value that can be used to sort a given buffer when comparing to other buffers.
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(), const SourceLibrary* library = nullptr) -> 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(), const SourceLibrary* library = nullptr) -> SourceBuffer
Instead of loading source from a file, copy it from text already in memory.
auto assignBuffer(std::string_view path, SmallVector<char>&& buffer, SourceLocation includedFrom = SourceLocation(), const SourceLibrary* library = nullptr) -> SourceBuffer
Instead of loading source from a file, move it from text already in memory.
auto readSource(const std::filesystem::path& path, const SourceLibrary* library, uint64_t sortKey = UINT64_MAX) -> BufferOrError
Read in a source file from disk.
auto readHeader(std::string_view path, SourceLocation includedFrom, const SourceLibrary* library, bool isSystemPath, std::span<std::filesystem::path const> additionalIncludePaths) -> BufferOrError
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)
Sets whether filenames should be made "proximate" to the current directory for diagnostic reporting purposes.
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
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.
auto getDiagnosticDirectives(BufferID buffer) const -> std::span<const DiagnosticDirectiveInfo>
Gets the diagnostic directives associated with the given buffer, if any.
auto getAllBuffers() const -> std::vector<BufferID>
Returns a list of buffers (files and macros) that have been created in the source manager.

Function documentation

std::error_code slang::SourceManager::addSystemDirectories(std::string_view pattern)

Adds one or more system include directories that match the given pattern.

Returns An error code if the given pattern is for an exact path and that path does not exist or is not a directory.

std::error_code slang::SourceManager::addUserDirectories(std::string_view pattern)

Adds one or more user include directories that match the given pattern.

Returns An error code if the given pattern is for an exact path and that path does not exist or is not a directory.

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.

std::optional<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.

Returns std::nullopt if the locations are in unrelated compilation units.

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(), const SourceLibrary* library = nullptr)

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, SmallVector<char>&& buffer, SourceLocation includedFrom = SourceLocation(), const SourceLibrary* library = nullptr)

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.

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

Gets the diagnostic directives associated with the given buffer, if any.