LLVM 20.0.0git
|
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling. More...
#include "llvm/Support/SourceMgr.h"
Public Types | |
enum | DiagKind { DK_Error , DK_Warning , DK_Remark , DK_Note } |
using | DiagHandlerTy = void(*)(const SMDiagnostic &, void *Context) |
Clients that want to handle their own diagnostics in a custom way can register a function pointer+context as a diagnostic handler. | |
Public Member Functions | |
SourceMgr ()=default | |
SourceMgr (const SourceMgr &)=delete | |
SourceMgr & | operator= (const SourceMgr &)=delete |
SourceMgr (SourceMgr &&)=default | |
SourceMgr & | operator= (SourceMgr &&)=default |
~SourceMgr ()=default | |
ArrayRef< std::string > | getIncludeDirs () const |
Return the include directories of this source manager. | |
void | setIncludeDirs (const std::vector< std::string > &Dirs) |
void | setDiagHandler (DiagHandlerTy DH, void *Ctx=nullptr) |
Specify a diagnostic handler to be invoked every time PrintMessage is called. | |
DiagHandlerTy | getDiagHandler () const |
void * | getDiagContext () const |
const SrcBuffer & | getBufferInfo (unsigned i) const |
const MemoryBuffer * | getMemoryBuffer (unsigned i) const |
unsigned | getNumBuffers () const |
unsigned | getMainFileID () const |
SMLoc | getParentIncludeLoc (unsigned i) const |
unsigned | AddNewSourceBuffer (std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc) |
Add a new source buffer to this source manager. | |
void | takeSourceBuffersFrom (SourceMgr &SrcMgr, SMLoc MainBufferIncludeLoc=SMLoc()) |
Takes the source buffers from the given source manager and append them to the current manager. | |
unsigned | AddIncludeFile (const std::string &Filename, SMLoc IncludeLoc, std::string &IncludedFile) |
Search for a file with the specified name in the current directory or in one of the IncludeDirs. | |
ErrorOr< std::unique_ptr< MemoryBuffer > > | OpenIncludeFile (const std::string &Filename, std::string &IncludedFile) |
Search for a file with the specified name in the current directory or in one of the IncludeDirs, and try to open it without adding to the SourceMgr. | |
unsigned | FindBufferContainingLoc (SMLoc Loc) const |
Return the ID of the buffer containing the specified location. | |
unsigned | FindLineNumber (SMLoc Loc, unsigned BufferID=0) const |
Find the line number for the specified location in the specified file. | |
std::pair< unsigned, unsigned > | getLineAndColumn (SMLoc Loc, unsigned BufferID=0) const |
Find the line and column number for the specified location in the specified file. | |
std::string | getFormattedLocationNoOffset (SMLoc Loc, bool IncludePath=false) const |
Get a string with the SMLoc filename and line number formatted in the standard style. | |
SMLoc | FindLocForLineAndColumn (unsigned BufferID, unsigned LineNo, unsigned ColNo) |
Given a line and column number in a mapped buffer, turn it into an SMLoc. | |
void | PrintMessage (raw_ostream &OS, SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges={}, ArrayRef< SMFixIt > FixIts={}, bool ShowColors=true) const |
Emit a message about the specified location with the specified string. | |
void | PrintMessage (SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges={}, ArrayRef< SMFixIt > FixIts={}, bool ShowColors=true) const |
Emits a diagnostic to llvm::errs(). | |
void | PrintMessage (raw_ostream &OS, const SMDiagnostic &Diagnostic, bool ShowColors=true) const |
Emits a manually-constructed diagnostic to the given output stream. | |
SMDiagnostic | GetMessage (SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges={}, ArrayRef< SMFixIt > FixIts={}) const |
Return an SMDiagnostic at the specified location with the specified string. | |
void | PrintIncludeStack (SMLoc IncludeLoc, raw_ostream &OS) const |
Prints the names of included files and the line of the file they were included from. | |
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition at line 31 of file SourceMgr.h.
using llvm::SourceMgr::DiagHandlerTy = void (*)(const SMDiagnostic &, void *Context) |
Clients that want to handle their own diagnostics in a custom way can register a function pointer+context as a diagnostic handler.
It gets called each time PrintMessage is invoked.
Definition at line 43 of file SourceMgr.h.
Enumerator | |
---|---|
DK_Error | |
DK_Warning | |
DK_Remark | |
DK_Note |
Definition at line 33 of file SourceMgr.h.
|
default |
|
default |
|
default |
unsigned SourceMgr::AddIncludeFile | ( | const std::string & | Filename, |
SMLoc | IncludeLoc, | ||
std::string & | IncludedFile | ||
) |
Search for a file with the specified name in the current directory or in one of the IncludeDirs.
If no file is found, this returns 0, otherwise it returns the buffer ID of the stacked file. The full path to the included file can be found in IncludedFile
.
Definition at line 41 of file SourceMgr.cpp.
References AddNewSourceBuffer(), and OpenIncludeFile().
|
inline |
Add a new source buffer to this source manager.
This takes ownership of the memory buffer.
Definition at line 144 of file SourceMgr.h.
References F.
Referenced by AddIncludeFile(), llvm::FileCheckPatternContext::defineCmdlineVariables(), llvm::MIRParserImpl::initializeMachineFunction(), initializeRecordStreamer(), parseAssemblyInto(), llvm::parseConstantValue(), llvm::parseDIExpressionBodyAtBeginning(), parseSummaryIndexAssemblyInto(), llvm::parseType(), llvm::parseTypeAtBeginning(), llvm::FileCheck::readCheckFile(), and llvm::TableGenMain().
Return the ID of the buffer containing the specified location.
0 is returned if the buffer is not found.
Definition at line 73 of file SourceMgr.cpp.
References llvm::SMLoc::getPointer().
Referenced by getFormattedLocationNoOffset(), getLineAndColumn(), getLocCookie(), GetMessage(), llvm::MCGenDwarfLabelEntry::Make(), PrintIncludeStack(), and PrintMessage().
Find the line number for the specified location in the specified file.
This is not a fast method.
Definition at line 196 of file SourceMgr.h.
References getLineAndColumn().
Referenced by getFormattedLocationNoOffset(), llvm::MCGenDwarfLabelEntry::Make(), and PrintIncludeStack().
Given a line and column number in a mapped buffer, turn it into an SMLoc.
This will return a null SMLoc if the line/column location is invalid.
Definition at line 234 of file SourceMgr.cpp.
References getBufferInfo(), llvm::SMLoc::getFromPointer(), llvm::StringRef::npos, and Ptr.
Definition at line 120 of file SourceMgr.h.
References assert().
Referenced by FindLocForLineAndColumn(), getFormattedLocationNoOffset(), getLineAndColumn(), PrintIncludeStack(), and PrintMessage().
|
inline |
Definition at line 118 of file SourceMgr.h.
Referenced by llvm::TableGenParseFile(), and llvm::remarks::YAMLParseError::YAMLParseError().
|
inline |
Definition at line 117 of file SourceMgr.h.
Referenced by llvm::TableGenParseFile(), and llvm::remarks::YAMLParseError::YAMLParseError().
Get a string with the SMLoc
filename and line number formatted in the standard style.
Get a string with the source location formatted in the standard style, but without the line offset.
If IncludePath
is true, the path is included. If false, only the file name and extension are included.
Definition at line 216 of file SourceMgr.cpp.
References assert(), FindBufferContainingLoc(), FindLineNumber(), getBufferInfo(), and I.
|
inline |
Return the include directories of this source manager.
Definition at line 104 of file SourceMgr.h.
Referenced by llvm::TableGenParseFile().
std::pair< unsigned, unsigned > SourceMgr::getLineAndColumn | ( | SMLoc | Loc, |
unsigned | BufferID = 0 |
||
) | const |
Find the line and column number for the specified location in the specified file.
This is not a fast method.
Definition at line 192 of file SourceMgr.cpp.
References assert(), llvm::StringRef::find_last_of(), FindBufferContainingLoc(), getBufferInfo(), llvm::SMLoc::getPointer(), llvm::StringRef::npos, and Ptr.
Referenced by llvm::FileCheckDiag::FileCheckDiag(), FindLineNumber(), and GetMessage().
|
inline |
Definition at line 132 of file SourceMgr.h.
References assert(), and getNumBuffers().
Referenced by llvm::SIMachineFunctionInfo::initializeBaseYamlFields(), llvm::MCContext::MCContext(), llvm::GCNTargetMachine::parseMachineFunctionInfo(), llvm::TableGenParseFile(), and llvm::TGLexer::TGLexer().
|
inline |
Definition at line 125 of file SourceMgr.h.
References assert().
Referenced by GetMessage(), llvm::SIMachineFunctionInfo::initializeBaseYamlFields(), llvm::MCContext::MCContext(), llvm::GCNTargetMachine::parseMachineFunctionInfo(), llvm::TableGenParseFile(), and llvm::TGLexer::TGLexer().
SMDiagnostic SourceMgr::GetMessage | ( | SMLoc | Loc, |
SourceMgr::DiagKind | Kind, | ||
const Twine & | Msg, | ||
ArrayRef< SMRange > | Ranges = {} , |
||
ArrayRef< SMFixIt > | FixIts = {} |
||
) | const |
Return an SMDiagnostic at the specified location with the specified string.
Msg | If non-null, the kind of message (e.g., "error") which is prefixed to the message. |
Definition at line 274 of file SourceMgr.cpp.
References assert(), FindBufferContainingLoc(), llvm::MemoryBuffer::getBufferEnd(), llvm::MemoryBuffer::getBufferIdentifier(), llvm::MemoryBuffer::getBufferStart(), llvm::SMLoc::getFromPointer(), getLineAndColumn(), getMemoryBuffer(), llvm::SMLoc::getPointer(), llvm::SMLoc::isValid(), llvm::SmallVectorTemplateBase< T, bool >::push_back(), and llvm::Twine::str().
Referenced by llvm::MIRParserImpl::error(), llvm::ErrorDiagnostic::get(), llvm::parseType(), PrintMessage(), llvm::MCContext::reportError(), and llvm::MCContext::reportWarning().
|
inline |
Definition at line 130 of file SourceMgr.h.
Referenced by getMainFileID(), llvm::MCContext::MCContext(), and takeSourceBuffersFrom().
Definition at line 137 of file SourceMgr.h.
References assert().
ErrorOr< std::unique_ptr< MemoryBuffer > > SourceMgr::OpenIncludeFile | ( | const std::string & | Filename, |
std::string & | IncludedFile | ||
) |
Search for a file with the specified name in the current directory or in one of the IncludeDirs, and try to open it without adding to the SourceMgr.
If the opened file is intended to be added to the source manager, prefer AddIncludeFile
instead.
If no file is found, this returns an Error, otherwise it returns the buffer of the stacked file. The full path to the included file can be found in IncludedFile
.
Definition at line 53 of file SourceMgr.cpp.
References llvm::sys::path::append(), and llvm::MemoryBuffer::getFile().
Referenced by AddIncludeFile().
void SourceMgr::PrintIncludeStack | ( | SMLoc | IncludeLoc, |
raw_ostream & | OS | ||
) | const |
Prints the names of included files and the line of the file they were included from.
A diagnostic handler can use this before printing its custom formatted message.
IncludeLoc | The location of the include. |
OS | the raw_ostream to print on. |
Definition at line 261 of file SourceMgr.cpp.
References assert(), FindBufferContainingLoc(), FindLineNumber(), getBufferInfo(), OS, and PrintIncludeStack().
Referenced by PrintIncludeStack(), and PrintMessage().
void SourceMgr::PrintMessage | ( | raw_ostream & | OS, |
const SMDiagnostic & | Diagnostic, | ||
bool | ShowColors = true |
||
) | const |
Emits a manually-constructed diagnostic to the given output stream.
ShowColors | Display colored messages if output is a terminal and the default error handler is used. |
Definition at line 335 of file SourceMgr.cpp.
References assert(), FindBufferContainingLoc(), getBufferInfo(), llvm::SMDiagnostic::getLoc(), llvm::SMLoc::isValid(), OS, llvm::SMDiagnostic::print(), and PrintIncludeStack().
void SourceMgr::PrintMessage | ( | raw_ostream & | OS, |
SMLoc | Loc, | ||
SourceMgr::DiagKind | Kind, | ||
const Twine & | Msg, | ||
ArrayRef< SMRange > | Ranges = {} , |
||
ArrayRef< SMFixIt > | FixIts = {} , |
||
bool | ShowColors = true |
||
) | const |
Emit a message about the specified location with the specified string.
ShowColors | Display colored messages if output is a terminal and the default error handler is used. |
Definition at line 352 of file SourceMgr.cpp.
References GetMessage(), OS, and PrintMessage().
Referenced by llvm::FileCheckString::CheckDag(), llvm::FileCheckString::CheckNext(), llvm::FileCheckString::CheckSame(), llvm::vfs::RedirectingFileSystem::create(), llvm::MCObjectStreamer::emitFill(), llvm::Pattern::parsePattern(), llvm::PrintError(), llvm::Pattern::printFuzzyMatch(), printMatch(), llvm::PrintMessage(), PrintMessage(), printNoMatch(), llvm::Pattern::printSubstitutions(), llvm::Pattern::printVariableDefs(), llvm::PrintWarning(), llvm::FileCheck::readCheckFile(), llvm::HexagonShuffler::reportError(), llvm::HexagonMCChecker::reportNote(), and llvm::LLLexer::Warning().
void SourceMgr::PrintMessage | ( | SMLoc | Loc, |
SourceMgr::DiagKind | Kind, | ||
const Twine & | Msg, | ||
ArrayRef< SMRange > | Ranges = {} , |
||
ArrayRef< SMFixIt > | FixIts = {} , |
||
bool | ShowColors = true |
||
) | const |
Emits a diagnostic to llvm::errs().
Definition at line 359 of file SourceMgr.cpp.
References llvm::errs(), and PrintMessage().
|
inline |
Specify a diagnostic handler to be invoked every time PrintMessage is called.
Ctx
is passed into the handler when it is invoked.
Definition at line 112 of file SourceMgr.h.
Referenced by llvm::vfs::RedirectingFileSystem::create(), setupSM(), llvm::TableGenParseFile(), and llvm::remarks::YAMLParseError::YAMLParseError().
|
inline |
Definition at line 106 of file SourceMgr.h.
Referenced by llvm::TableGenMain(), and llvm::TableGenParseFile().
|
inline |
Takes the source buffers from the given source manager and append them to the current manager.
MainBufferIncludeLoc
is an optional include location to attach to the main buffer of SrcMgr
after it gets moved to the current manager.
Definition at line 157 of file SourceMgr.h.
References getNumBuffers(), and llvm::SrcMgr.
Referenced by llvm::TableGenParseFile().