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
print(raw_ostream & OS) const26 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
print(raw_ostream & OS,const SourceManager & SM) const38 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
dump(const SourceManager & SM) const64 void SourceLocation::dump(const SourceManager &SM) const {
65 print(llvm::errs(), SM);
66 }
67
68 //===----------------------------------------------------------------------===//
69 // FullSourceLoc
70 //===----------------------------------------------------------------------===//
71
getFileID() const72 FileID FullSourceLoc::getFileID() const {
73 assert(isValid());
74 return SrcMgr->getFileID(*this);
75 }
76
77
getExpansionLoc() const78 FullSourceLoc FullSourceLoc::getExpansionLoc() const {
79 assert(isValid());
80 return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr);
81 }
82
getSpellingLoc() const83 FullSourceLoc FullSourceLoc::getSpellingLoc() const {
84 assert(isValid());
85 return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
86 }
87
getExpansionLineNumber(bool * Invalid) const88 unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const {
89 assert(isValid());
90 return SrcMgr->getExpansionLineNumber(*this, Invalid);
91 }
92
getExpansionColumnNumber(bool * Invalid) const93 unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const {
94 assert(isValid());
95 return SrcMgr->getExpansionColumnNumber(*this, Invalid);
96 }
97
getSpellingLineNumber(bool * Invalid) const98 unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const {
99 assert(isValid());
100 return SrcMgr->getSpellingLineNumber(*this, Invalid);
101 }
102
getSpellingColumnNumber(bool * Invalid) const103 unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const {
104 assert(isValid());
105 return SrcMgr->getSpellingColumnNumber(*this, Invalid);
106 }
107
isInSystemHeader() const108 bool FullSourceLoc::isInSystemHeader() const {
109 assert(isValid());
110 return SrcMgr->isInSystemHeader(*this);
111 }
112
isBeforeInTranslationUnitThan(SourceLocation Loc) const113 bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const {
114 assert(isValid());
115 return SrcMgr->isBeforeInTranslationUnit(*this, Loc);
116 }
117
dump() const118 void FullSourceLoc::dump() const {
119 SourceLocation::dump(*SrcMgr);
120 }
121
getCharacterData(bool * Invalid) const122 const char *FullSourceLoc::getCharacterData(bool *Invalid) const {
123 assert(isValid());
124 return SrcMgr->getCharacterData(*this, Invalid);
125 }
126
getBuffer(bool * Invalid) const127 const llvm::MemoryBuffer* FullSourceLoc::getBuffer(bool *Invalid) const {
128 assert(isValid());
129 return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid);
130 }
131
getBufferData(bool * Invalid) const132 StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
133 return getBuffer(Invalid)->getBuffer();
134 }
135
getDecomposedLoc() const136 std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
137 return SrcMgr->getDecomposedLoc(*this);
138 }
139