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
64 LLVM_DUMP_METHOD std::string
printToString(const SourceManager & SM) const65 SourceLocation::printToString(const SourceManager &SM) const {
66 std::string S;
67 llvm::raw_string_ostream OS(S);
68 print(OS, SM);
69 return OS.str();
70 }
71
dump(const SourceManager & SM) const72 LLVM_DUMP_METHOD void SourceLocation::dump(const SourceManager &SM) const {
73 print(llvm::errs(), SM);
74 }
75
76 //===----------------------------------------------------------------------===//
77 // FullSourceLoc
78 //===----------------------------------------------------------------------===//
79
getFileID() const80 FileID FullSourceLoc::getFileID() const {
81 assert(isValid());
82 return SrcMgr->getFileID(*this);
83 }
84
85
getExpansionLoc() const86 FullSourceLoc FullSourceLoc::getExpansionLoc() const {
87 assert(isValid());
88 return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr);
89 }
90
getSpellingLoc() const91 FullSourceLoc FullSourceLoc::getSpellingLoc() const {
92 assert(isValid());
93 return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
94 }
95
getExpansionLineNumber(bool * Invalid) const96 unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const {
97 assert(isValid());
98 return SrcMgr->getExpansionLineNumber(*this, Invalid);
99 }
100
getExpansionColumnNumber(bool * Invalid) const101 unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const {
102 assert(isValid());
103 return SrcMgr->getExpansionColumnNumber(*this, Invalid);
104 }
105
getSpellingLineNumber(bool * Invalid) const106 unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const {
107 assert(isValid());
108 return SrcMgr->getSpellingLineNumber(*this, Invalid);
109 }
110
getSpellingColumnNumber(bool * Invalid) const111 unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const {
112 assert(isValid());
113 return SrcMgr->getSpellingColumnNumber(*this, Invalid);
114 }
115
isInSystemHeader() const116 bool FullSourceLoc::isInSystemHeader() const {
117 assert(isValid());
118 return SrcMgr->isInSystemHeader(*this);
119 }
120
isBeforeInTranslationUnitThan(SourceLocation Loc) const121 bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const {
122 assert(isValid());
123 return SrcMgr->isBeforeInTranslationUnit(*this, Loc);
124 }
125
dump() const126 LLVM_DUMP_METHOD void FullSourceLoc::dump() const {
127 SourceLocation::dump(*SrcMgr);
128 }
129
getCharacterData(bool * Invalid) const130 const char *FullSourceLoc::getCharacterData(bool *Invalid) const {
131 assert(isValid());
132 return SrcMgr->getCharacterData(*this, Invalid);
133 }
134
getBufferData(bool * Invalid) const135 StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
136 assert(isValid());
137 return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid)->getBuffer();
138 }
139
getDecomposedLoc() const140 std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
141 return SrcMgr->getDecomposedLoc(*this);
142 }
143