From b8e08860065a64963f49da64742350a8231b1727 Mon Sep 17 00:00:00 2001 From: Stephen Kelly Date: Thu, 30 Aug 2018 23:10:52 +0000 Subject: Add dump() method for SourceRange Subscribers: cfe-commits Differential Revision: https://reviews.llvm.org/D50662 llvm-svn: 341140 --- clang/lib/Basic/SourceLocation.cpp | 54 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) (limited to 'clang/lib/Basic/SourceLocation.cpp') 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 << ""; + 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 << " '; + 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 //===----------------------------------------------------------------------===// -- cgit v1.2.3