clang  6.0.0svn
SourceLocation.cpp
Go to the documentation of this file.
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 
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 FullSourceLoc FullSourceLoc::getFileLoc() const {
96  assert(isValid());
97  return FullSourceLoc(SrcMgr->getFileLoc(*this), *SrcMgr);
98 }
99 
100 std::pair<FullSourceLoc, FullSourceLoc>
101 FullSourceLoc::getImmediateExpansionRange() const {
102  assert(isValid());
103  std::pair<SourceLocation, SourceLocation> Range =
104  SrcMgr->getImmediateExpansionRange(*this);
105  return std::make_pair(FullSourceLoc(Range.first, *SrcMgr),
106  FullSourceLoc(Range.second, *SrcMgr));
107 }
108 
109 PresumedLoc FullSourceLoc::getPresumedLoc(bool UseLineDirectives) const {
110  if (!isValid())
111  return PresumedLoc();
112 
113  return SrcMgr->getPresumedLoc(*this, UseLineDirectives);
114 }
115 
116 bool FullSourceLoc::isMacroArgExpansion(FullSourceLoc *StartLoc) const {
117  assert(isValid());
118  return SrcMgr->isMacroArgExpansion(*this, StartLoc);
119 }
120 
121 FullSourceLoc FullSourceLoc::getImmediateMacroCallerLoc() const {
122  assert(isValid());
123  return FullSourceLoc(SrcMgr->getImmediateMacroCallerLoc(*this), *SrcMgr);
124 }
125 
126 std::pair<FullSourceLoc, StringRef> FullSourceLoc::getModuleImportLoc() const {
127  if (!isValid())
128  return std::make_pair(FullSourceLoc(), StringRef());
129 
130  std::pair<SourceLocation, StringRef> ImportLoc =
131  SrcMgr->getModuleImportLoc(*this);
132  return std::make_pair(FullSourceLoc(ImportLoc.first, *SrcMgr),
133  ImportLoc.second);
134 }
135 
136 unsigned FullSourceLoc::getFileOffset() const {
137  assert(isValid());
138  return SrcMgr->getFileOffset(*this);
139 }
140 
141 unsigned FullSourceLoc::getLineNumber(bool *Invalid) const {
142  assert(isValid());
143  return SrcMgr->getLineNumber(getFileID(), getFileOffset(), Invalid);
144 }
145 
146 unsigned FullSourceLoc::getColumnNumber(bool *Invalid) const {
147  assert(isValid());
148  return SrcMgr->getColumnNumber(getFileID(), getFileOffset(), Invalid);
149 }
150 
151 std::pair<FullSourceLoc, FullSourceLoc>
152 FullSourceLoc::getExpansionRange() const {
153  assert(isValid());
154  std::pair<SourceLocation, SourceLocation> Range =
155  SrcMgr->getExpansionRange(*this);
156  return std::make_pair(FullSourceLoc(Range.first, *SrcMgr),
157  FullSourceLoc(Range.second, *SrcMgr));
158 }
159 
160 const FileEntry *FullSourceLoc::getFileEntry() const {
161  assert(isValid());
162  return SrcMgr->getFileEntryForID(getFileID());
163 }
164 
165 unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const {
166  assert(isValid());
167  return SrcMgr->getExpansionLineNumber(*this, Invalid);
168 }
169 
170 unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const {
171  assert(isValid());
172  return SrcMgr->getExpansionColumnNumber(*this, Invalid);
173 }
174 
175 unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const {
176  assert(isValid());
177  return SrcMgr->getSpellingLineNumber(*this, Invalid);
178 }
179 
180 unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const {
181  assert(isValid());
182  return SrcMgr->getSpellingColumnNumber(*this, Invalid);
183 }
184 
185 bool FullSourceLoc::isInSystemHeader() const {
186  assert(isValid());
187  return SrcMgr->isInSystemHeader(*this);
188 }
189 
190 bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const {
191  assert(isValid());
192  return SrcMgr->isBeforeInTranslationUnit(*this, Loc);
193 }
194 
195 LLVM_DUMP_METHOD void FullSourceLoc::dump() const {
196  SourceLocation::dump(*SrcMgr);
197 }
198 
199 const char *FullSourceLoc::getCharacterData(bool *Invalid) const {
200  assert(isValid());
201  return SrcMgr->getCharacterData(*this, Invalid);
202 }
203 
204 StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
205  assert(isValid());
206  return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid)->getBuffer();
207 }
208 
209 std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
210  return SrcMgr->getDecomposedLoc(*this);
211 }
Defines the SourceManager interface.
Defines the PrettyStackTraceEntry class, which is used to make crashes give more contextual informati...
void print(raw_ostream &OS) const override
void print(raw_ostream &OS, const SourceManager &SM) const
SourceLocation getExpansionLoc(SourceLocation Loc) const
Given a SourceLocation object Loc, return the expansion location referenced by the ID...
SourceLocation getSpellingLoc(SourceLocation Loc) const
Given a SourceLocation object, return the spelling location referenced by the ID. ...
bool isInvalid() const
Return true if this object is invalid or uninitialized.
unsigned getLine() const
Return the presumed line number of this location.
Represents an unpacked "presumed" location which can be presented to the user.
const char * getFilename() const
Return the presumed filename of this location.
unsigned getColumn() const
Return the presumed column number of this location.
Dataflow Directional Tag Classes.
bool isValid() const
Return true if this is a valid SourceLocation object.
PresumedLoc getPresumedLoc(SourceLocation Loc, bool UseLineDirectives=true) const
Returns the "presumed" location of a SourceLocation specifies.
Defines the clang::SourceLocation class and associated facilities.
This class handles loading and caching of source files into memory.