slang::SourceManager class

Contents

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(string_view path) const -> std::string
Convert the given relative path into an absolute path.
void addSystemDirectory(string_view path)
Adds a system include directory.
void addUserDirectory(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 -> string_view
Gets the source file name for a given source location.
auto getRawFileName(BufferID buffer) const -> string_view
auto getColumnNumber(SourceLocation location) const -> size_t
auto getIncludedFrom(BufferID buffer) const -> SourceLocation
auto getMacroName(SourceLocation location) const -> 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 -> 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, string_view macroName) -> SourceLocation
Creates a macro expansion location; used by the preprocessor.
auto assignText(string_view text, SourceLocation includedFrom = SourceLocation()) -> SourceBuffer
Instead of loading source from a file, copy it from text already in memory.
auto assignText(string_view path, string_view text, SourceLocation includedFrom = SourceLocation()) -> SourceBuffer
auto assignBuffer(string_view path, std::vector<char>&& buffer, SourceLocation includedFrom = SourceLocation()) -> SourceBuffer
auto readSource(string_view path) -> SourceBuffer
Read in a source file from disk.
auto readHeader(string_view path, SourceLocation includedFrom, bool isSystemPath) -> SourceBuffer
Read in a header file from disk.
void addLineDirective(SourceLocation location, size_t lineNum, string_view name, uint8_t level)
Adds a line directive at the given location.
void addDiagnosticDirective(SourceLocation location, string_view name, DiagnosticSeverity severity)
Adds a diagnostic directive at the given location.
template<typename Func>
void visitDiagnosticDirectives(Func&& func) const

Function documentation

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.

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.

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

Parameters
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.

Determines whether the

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(string_view path, 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(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.

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.