diff options
Diffstat (limited to 'llvm/lib/Support/SourceMgr.cpp')
-rw-r--r-- | llvm/lib/Support/SourceMgr.cpp | 26 |
1 files changed, 25 insertions, 1 deletions
diff --git a/llvm/lib/Support/SourceMgr.cpp b/llvm/lib/Support/SourceMgr.cpp index 9cc6973..e50cf5b 100644 --- a/llvm/lib/Support/SourceMgr.cpp +++ b/llvm/lib/Support/SourceMgr.cpp @@ -180,7 +180,7 @@ std::pair<unsigned, unsigned> SourceMgr::getLineAndColumn(SMLoc Loc, unsigned BufferID) const { if (!BufferID) BufferID = FindBufferContainingLoc(Loc); - assert(BufferID && "Invalid Location!"); + assert(BufferID && "Invalid location!"); auto &SB = getBufferInfo(BufferID); const char *Ptr = Loc.getPointer(); @@ -193,6 +193,30 @@ SourceMgr::getLineAndColumn(SMLoc Loc, unsigned BufferID) const { return std::make_pair(LineNo, Ptr - BufStart - NewlineOffs); } +// FIXME: Note that the formatting of source locations is spread between +// multiple functions, some in SourceMgr and some in SMDiagnostic. A better +// solution would be a general-purpose source location formatter +// in one of those two classes, or possibly in SMLoc. + +/// Get a string with the source location formatted in the standard +/// style, but without the line offset. If \p IncludePath is true, the path +/// is included. If false, only the file name and extension are included. +std::string SourceMgr::getFormattedLocationNoOffset(SMLoc Loc, + bool IncludePath) const { + auto BufferID = FindBufferContainingLoc(Loc); + assert(BufferID && "Invalid location!"); + auto FileSpec = getBufferInfo(BufferID).Buffer->getBufferIdentifier(); + + if (IncludePath) { + return FileSpec.str() + ":" + std::to_string(FindLineNumber(Loc, BufferID)); + } else { + auto I = FileSpec.find_last_of("/\\"); + I = (I == FileSpec.size()) ? 0 : (I + 1); + return FileSpec.substr(I).str() + ":" + + std::to_string(FindLineNumber(Loc, BufferID)); + } +} + /// 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. SMLoc SourceMgr::FindLocForLineAndColumn(unsigned BufferID, unsigned LineNo, |