1 //==--- SourceLocation.cpp - Compact identifier for Source Files -*- C++ -*-==//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines accessor methods for the FullSourceLoc class.
12 //===----------------------------------------------------------------------===//
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"
20 using namespace clang;
22 //===----------------------------------------------------------------------===//
23 // PrettyStackTraceLoc
24 //===----------------------------------------------------------------------===//
26 void PrettyStackTraceLoc::print(raw_ostream &OS) const {
31 OS << Message << '\n';
34 //===----------------------------------------------------------------------===//
36 //===----------------------------------------------------------------------===//
38 void SourceLocation::print(raw_ostream &OS, const SourceManager &SM)const{
40 OS << "<invalid loc>";
45 PresumedLoc PLoc = SM.getPresumedLoc(*this);
47 if (PLoc.isInvalid()) {
51 // The macro expansion and spelling pos is identical for file locs.
52 OS << PLoc.getFilename() << ':' << PLoc.getLine()
53 << ':' << PLoc.getColumn();
57 SM.getExpansionLoc(*this).print(OS, SM);
60 SM.getSpellingLoc(*this).print(OS, SM);
64 std::string SourceLocation::printToString(const SourceManager &SM) const {
66 llvm::raw_string_ostream OS(S);
71 void SourceLocation::dump(const SourceManager &SM) const {
72 print(llvm::errs(), SM);
75 //===----------------------------------------------------------------------===//
77 //===----------------------------------------------------------------------===//
79 FileID FullSourceLoc::getFileID() const {
81 return SrcMgr->getFileID(*this);
85 FullSourceLoc FullSourceLoc::getExpansionLoc() const {
87 return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr);
90 FullSourceLoc FullSourceLoc::getSpellingLoc() const {
92 return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
95 unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const {
97 return SrcMgr->getExpansionLineNumber(*this, Invalid);
100 unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const {
102 return SrcMgr->getExpansionColumnNumber(*this, Invalid);
105 unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const {
107 return SrcMgr->getSpellingLineNumber(*this, Invalid);
110 unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const {
112 return SrcMgr->getSpellingColumnNumber(*this, Invalid);
115 bool FullSourceLoc::isInSystemHeader() const {
117 return SrcMgr->isInSystemHeader(*this);
120 bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const {
122 return SrcMgr->isBeforeInTranslationUnit(*this, Loc);
125 void FullSourceLoc::dump() const {
126 SourceLocation::dump(*SrcMgr);
129 const char *FullSourceLoc::getCharacterData(bool *Invalid) const {
131 return SrcMgr->getCharacterData(*this, Invalid);
134 const llvm::MemoryBuffer* FullSourceLoc::getBuffer(bool *Invalid) const {
136 return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid);
139 StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
140 return getBuffer(Invalid)->getBuffer();
143 std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
144 return SrcMgr->getDecomposedLoc(*this);