]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - lib/Basic/SourceLocation.cpp
Vendor import of clang trunk r290819:
[FreeBSD/FreeBSD.git] / lib / Basic / SourceLocation.cpp
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/raw_ostream.h"
18 #include <cstdio>
19 using namespace clang;
20
21 //===----------------------------------------------------------------------===//
22 // PrettyStackTraceLoc
23 //===----------------------------------------------------------------------===//
24
25 void PrettyStackTraceLoc::print(raw_ostream &OS) const {
26   if (Loc.isValid()) {
27     Loc.print(OS, SM);
28     OS << ": ";
29   }
30   OS << Message << '\n';
31 }
32
33 //===----------------------------------------------------------------------===//
34 // SourceLocation
35 //===----------------------------------------------------------------------===//
36
37 void SourceLocation::print(raw_ostream &OS, const SourceManager &SM)const{
38   if (!isValid()) {
39     OS << "<invalid loc>";
40     return;
41   }
42
43   if (isFileID()) {
44     PresumedLoc PLoc = SM.getPresumedLoc(*this);
45     
46     if (PLoc.isInvalid()) {
47       OS << "<invalid>";
48       return;
49     }
50     // The macro expansion and spelling pos is identical for file locs.
51     OS << PLoc.getFilename() << ':' << PLoc.getLine()
52        << ':' << PLoc.getColumn();
53     return;
54   }
55
56   SM.getExpansionLoc(*this).print(OS, SM);
57
58   OS << " <Spelling=";
59   SM.getSpellingLoc(*this).print(OS, SM);
60   OS << '>';
61 }
62
63 LLVM_DUMP_METHOD std::string
64 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 LLVM_DUMP_METHOD 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 LLVM_DUMP_METHOD 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 StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
135   assert(isValid());
136   return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid)->getBuffer();
137 }
138
139 std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
140   return SrcMgr->getDecomposedLoc(*this);
141 }