clang  7.0.0svn
SourceLocation.cpp
Go to the documentation of this file.
1 //===- SourceLocation.cpp - Compact identifier for Source Files -----------===//
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 
15 #include "clang/Basic/LLVM.h"
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/MemoryBuffer.h"
21 #include "llvm/Support/raw_ostream.h"
22 #include <cassert>
23 #include <string>
24 #include <utility>
25 
26 using namespace clang;
27 
28 //===----------------------------------------------------------------------===//
29 // PrettyStackTraceLoc
30 //===----------------------------------------------------------------------===//
31 
32 void PrettyStackTraceLoc::print(raw_ostream &OS) const {
33  if (Loc.isValid()) {
34  Loc.print(OS, SM);
35  OS << ": ";
36  }
37  OS << Message << '\n';
38 }
39 
40 //===----------------------------------------------------------------------===//
41 // SourceLocation
42 //===----------------------------------------------------------------------===//
43 
44 void SourceLocation::print(raw_ostream &OS, const SourceManager &SM)const{
45  if (!isValid()) {
46  OS << "<invalid loc>";
47  return;
48  }
49 
50  if (isFileID()) {
51  PresumedLoc PLoc = SM.getPresumedLoc(*this);
52 
53  if (PLoc.isInvalid()) {
54  OS << "<invalid>";
55  return;
56  }
57  // The macro expansion and spelling pos is identical for file locs.
58  OS << PLoc.getFilename() << ':' << PLoc.getLine()
59  << ':' << PLoc.getColumn();
60  return;
61  }
62 
63  SM.getExpansionLoc(*this).print(OS, SM);
64 
65  OS << " <Spelling=";
66  SM.getSpellingLoc(*this).print(OS, SM);
67  OS << '>';
68 }
69 
70 LLVM_DUMP_METHOD std::string
71 SourceLocation::printToString(const SourceManager &SM) const {
72  std::string S;
73  llvm::raw_string_ostream OS(S);
74  print(OS, SM);
75  return OS.str();
76 }
77 
78 LLVM_DUMP_METHOD void SourceLocation::dump(const SourceManager &SM) const {
79  print(llvm::errs(), SM);
80 }
81 
82 //===----------------------------------------------------------------------===//
83 // FullSourceLoc
84 //===----------------------------------------------------------------------===//
85 
86 FileID FullSourceLoc::getFileID() const {
87  assert(isValid());
88  return SrcMgr->getFileID(*this);
89 }
90 
91 FullSourceLoc FullSourceLoc::getExpansionLoc() const {
92  assert(isValid());
93  return FullSourceLoc(SrcMgr->getExpansionLoc(*this), *SrcMgr);
94 }
95 
96 FullSourceLoc FullSourceLoc::getSpellingLoc() const {
97  assert(isValid());
98  return FullSourceLoc(SrcMgr->getSpellingLoc(*this), *SrcMgr);
99 }
100 
101 FullSourceLoc FullSourceLoc::getFileLoc() const {
102  assert(isValid());
103  return FullSourceLoc(SrcMgr->getFileLoc(*this), *SrcMgr);
104 }
105 
106 std::pair<FullSourceLoc, FullSourceLoc>
107 FullSourceLoc::getImmediateExpansionRange() const {
108  assert(isValid());
109  std::pair<SourceLocation, SourceLocation> Range =
110  SrcMgr->getImmediateExpansionRange(*this);
111  return std::make_pair(FullSourceLoc(Range.first, *SrcMgr),
112  FullSourceLoc(Range.second, *SrcMgr));
113 }
114 
115 PresumedLoc FullSourceLoc::getPresumedLoc(bool UseLineDirectives) const {
116  if (!isValid())
117  return PresumedLoc();
118 
119  return SrcMgr->getPresumedLoc(*this, UseLineDirectives);
120 }
121 
122 bool FullSourceLoc::isMacroArgExpansion(FullSourceLoc *StartLoc) const {
123  assert(isValid());
124  return SrcMgr->isMacroArgExpansion(*this, StartLoc);
125 }
126 
127 FullSourceLoc FullSourceLoc::getImmediateMacroCallerLoc() const {
128  assert(isValid());
129  return FullSourceLoc(SrcMgr->getImmediateMacroCallerLoc(*this), *SrcMgr);
130 }
131 
132 std::pair<FullSourceLoc, StringRef> FullSourceLoc::getModuleImportLoc() const {
133  if (!isValid())
134  return std::make_pair(FullSourceLoc(), StringRef());
135 
136  std::pair<SourceLocation, StringRef> ImportLoc =
137  SrcMgr->getModuleImportLoc(*this);
138  return std::make_pair(FullSourceLoc(ImportLoc.first, *SrcMgr),
139  ImportLoc.second);
140 }
141 
142 unsigned FullSourceLoc::getFileOffset() const {
143  assert(isValid());
144  return SrcMgr->getFileOffset(*this);
145 }
146 
147 unsigned FullSourceLoc::getLineNumber(bool *Invalid) const {
148  assert(isValid());
149  return SrcMgr->getLineNumber(getFileID(), getFileOffset(), Invalid);
150 }
151 
152 unsigned FullSourceLoc::getColumnNumber(bool *Invalid) const {
153  assert(isValid());
154  return SrcMgr->getColumnNumber(getFileID(), getFileOffset(), Invalid);
155 }
156 
157 std::pair<FullSourceLoc, FullSourceLoc>
158 FullSourceLoc::getExpansionRange() const {
159  assert(isValid());
160  std::pair<SourceLocation, SourceLocation> Range =
161  SrcMgr->getExpansionRange(*this);
162  return std::make_pair(FullSourceLoc(Range.first, *SrcMgr),
163  FullSourceLoc(Range.second, *SrcMgr));
164 }
165 
166 const FileEntry *FullSourceLoc::getFileEntry() const {
167  assert(isValid());
168  return SrcMgr->getFileEntryForID(getFileID());
169 }
170 
171 unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const {
172  assert(isValid());
173  return SrcMgr->getExpansionLineNumber(*this, Invalid);
174 }
175 
176 unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const {
177  assert(isValid());
178  return SrcMgr->getExpansionColumnNumber(*this, Invalid);
179 }
180 
181 unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const {
182  assert(isValid());
183  return SrcMgr->getSpellingLineNumber(*this, Invalid);
184 }
185 
186 unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const {
187  assert(isValid());
188  return SrcMgr->getSpellingColumnNumber(*this, Invalid);
189 }
190 
191 bool FullSourceLoc::isInSystemHeader() const {
192  assert(isValid());
193  return SrcMgr->isInSystemHeader(*this);
194 }
195 
196 bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const {
197  assert(isValid());
198  return SrcMgr->isBeforeInTranslationUnit(*this, Loc);
199 }
200 
201 LLVM_DUMP_METHOD void FullSourceLoc::dump() const {
202  SourceLocation::dump(*SrcMgr);
203 }
204 
205 const char *FullSourceLoc::getCharacterData(bool *Invalid) const {
206  assert(isValid());
207  return SrcMgr->getCharacterData(*this, Invalid);
208 }
209 
210 StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
211  assert(isValid());
212  return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid)->getBuffer();
213 }
214 
215 std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
216  return SrcMgr->getDecomposedLoc(*this);
217 }
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
Forward-declares and imports various common LLVM datatypes that clang wants to use unqualified...
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.