1 //==--- SourceLocation.cpp - Compact identifier for Source Files -*- C++ -*-==// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines accessor methods for the FullSourceLoc class. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Basic/SourceLocation.h" 15 #include "clang/Basic/PrettyStackTrace.h" 16 #include "clang/Basic/SourceManager.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 #include "llvm/Support/raw_ostream.h" 19 #include <cstdio> 20 using namespace clang; 21 22 //===----------------------------------------------------------------------===// 23 // PrettyStackTraceLoc 24 //===----------------------------------------------------------------------===// 25 26 void PrettyStackTraceLoc::print(raw_ostream &OS) const { 27 if (Loc.isValid()) { 28 Loc.print(OS, SM); 29 OS << ": "; 30 } 31 OS << Message << '\n'; 32 } 33 34 //===----------------------------------------------------------------------===// 35 // SourceLocation 36 //===----------------------------------------------------------------------===// 37 38 void SourceLocation::print(raw_ostream &OS, const SourceManager &SM)const{ 39 if (!isValid()) { 40 OS << "<invalid loc>"; 41 return; 42 } 43 44 if (isFileID()) { 45 PresumedLoc PLoc = SM.getPresumedLoc(*this); 46 47 if (PLoc.isInvalid()) { 48 OS << "<invalid>"; 49 return; 50 } 51 // The macro expansion and spelling pos is identical for file locs. 52 OS << PLoc.getFilename() << ':' << PLoc.getLine() 53 << ':' << PLoc.getColumn(); 54 return; 55 } 56 57 SM.getExpansionLoc(*this).print(OS, SM); 58 59 OS << " <Spelling="; 60 SM.getSpellingLoc(*this).print(OS, SM); 61 OS << '>'; 62 } 63 64 std::string SourceLocation::printToString(const SourceManager &SM) const { 65 std::string S; 66 llvm::raw_string_ostream OS(S); 67 print(OS, SM); 68 return OS.str(); 69 } 70 71 void SourceLocation::dump(const SourceManager &SM) const { 72 print(llvm::errs(), SM); 73 } 74 75 //===----------------------------------------------------------------------===// 76 // FullSourceLoc 77 //===----------------------------------------------------------------------===// 78 79 FileID FullSourceLoc::getFileID() const { 80 assert(isValid()); 81 return SrcMgr->getFileID(*this); 82 } 83 84 85 FullSourceLoc FullSourceLoc::getExpansionLoc() const { 86 assert(isValid()); 87 return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr); 88 } 89 90 FullSourceLoc FullSourceLoc::getSpellingLoc() const { 91 assert(isValid()); 92 return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr); 93 } 94 95 unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const { 96 assert(isValid()); 97 return SrcMgr->getExpansionLineNumber(*this, Invalid); 98 } 99 100 unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const { 101 assert(isValid()); 102 return SrcMgr->getExpansionColumnNumber(*this, Invalid); 103 } 104 105 unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const { 106 assert(isValid()); 107 return SrcMgr->getSpellingLineNumber(*this, Invalid); 108 } 109 110 unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const { 111 assert(isValid()); 112 return SrcMgr->getSpellingColumnNumber(*this, Invalid); 113 } 114 115 bool FullSourceLoc::isInSystemHeader() const { 116 assert(isValid()); 117 return SrcMgr->isInSystemHeader(*this); 118 } 119 120 bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const { 121 assert(isValid()); 122 return SrcMgr->isBeforeInTranslationUnit(*this, Loc); 123 } 124 125 void FullSourceLoc::dump() const { 126 SourceLocation::dump(*SrcMgr); 127 } 128 129 const char *FullSourceLoc::getCharacterData(bool *Invalid) const { 130 assert(isValid()); 131 return SrcMgr->getCharacterData(*this, Invalid); 132 } 133 134 const llvm::MemoryBuffer* FullSourceLoc::getBuffer(bool *Invalid) const { 135 assert(isValid()); 136 return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid); 137 } 138 139 StringRef FullSourceLoc::getBufferData(bool *Invalid) const { 140 return getBuffer(Invalid)->getBuffer(); 141 } 142 143 std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const { 144 return SrcMgr->getDecomposedLoc(*this); 145 } 146