diff options
author | Stephen Kelly <steveire@gmail.com> | 2018-08-30 23:10:52 +0000 |
---|---|---|
committer | Stephen Kelly <steveire@gmail.com> | 2018-08-30 23:10:52 +0000 |
commit | b8e08860065a64963f49da64742350a8231b1727 (patch) | |
tree | b5f13c580d78cb591c39b702be6b7f2e2e217f9f /clang/lib/Basic/SourceLocation.cpp | |
parent | c7998937875e0e7ad08d16ef35fa3bf4c6e0a155 (diff) | |
download | bcm5719-llvm-b8e08860065a64963f49da64742350a8231b1727.tar.gz bcm5719-llvm-b8e08860065a64963f49da64742350a8231b1727.zip |
Add dump() method for SourceRange
Subscribers: cfe-commits
Differential Revision: https://reviews.llvm.org/D50662
llvm-svn: 341140
Diffstat (limited to 'clang/lib/Basic/SourceLocation.cpp')
-rw-r--r-- | clang/lib/Basic/SourceLocation.cpp | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/clang/lib/Basic/SourceLocation.cpp b/clang/lib/Basic/SourceLocation.cpp index 27eb1191fdd..aa844f2cd26 100644 --- a/clang/lib/Basic/SourceLocation.cpp +++ b/clang/lib/Basic/SourceLocation.cpp @@ -80,6 +80,60 @@ LLVM_DUMP_METHOD void SourceLocation::dump(const SourceManager &SM) const { llvm::errs() << '\n'; } +LLVM_DUMP_METHOD void SourceRange::dump(const SourceManager &SM) const { + print(llvm::errs(), SM); + llvm::errs() << '\n'; +} + +static PresumedLoc PrintDifference(raw_ostream &OS, const SourceManager &SM, + SourceLocation Loc, PresumedLoc Previous) { + if (Loc.isFileID()) { + + PresumedLoc PLoc = SM.getPresumedLoc(Loc); + + if (PLoc.isInvalid()) { + OS << "<invalid sloc>"; + return Previous; + } + + if (Previous.isInvalid() || + strcmp(PLoc.getFilename(), Previous.getFilename()) != 0) { + OS << PLoc.getFilename() << ':' << PLoc.getLine() << ':' + << PLoc.getColumn(); + } else if (Previous.isInvalid() || PLoc.getLine() != Previous.getLine()) { + OS << "line" << ':' << PLoc.getLine() << ':' << PLoc.getColumn(); + } else { + OS << "col" << ':' << PLoc.getColumn(); + } + return PLoc; + } + auto PrintedLoc = PrintDifference(OS, SM, SM.getExpansionLoc(Loc), Previous); + + OS << " <Spelling="; + PrintedLoc = PrintDifference(OS, SM, SM.getSpellingLoc(Loc), PrintedLoc); + OS << '>'; + return PrintedLoc; +} + +void SourceRange::print(raw_ostream &OS, const SourceManager &SM) const { + + OS << '<'; + auto PrintedLoc = PrintDifference(OS, SM, B, {}); + if (B != E) { + OS << ", "; + PrintDifference(OS, SM, E, PrintedLoc); + } + OS << '>'; +} + +LLVM_DUMP_METHOD std::string +SourceRange::printToString(const SourceManager &SM) const { + std::string S; + llvm::raw_string_ostream OS(S); + print(OS, SM); + return OS.str(); +} + //===----------------------------------------------------------------------===// // FullSourceLoc //===----------------------------------------------------------------------===// |