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 PresumedLoc FullSourceLoc::getPresumedLoc(bool UseLineDirectives) const {
107  if (!isValid())
108  return PresumedLoc();
109 
110  return SrcMgr->getPresumedLoc(*this, UseLineDirectives);
111 }
112 
113 bool FullSourceLoc::isMacroArgExpansion(FullSourceLoc *StartLoc) const {
114  assert(isValid());
115  return SrcMgr->isMacroArgExpansion(*this, StartLoc);
116 }
117 
118 FullSourceLoc FullSourceLoc::getImmediateMacroCallerLoc() const {
119  assert(isValid());
120  return FullSourceLoc(SrcMgr->getImmediateMacroCallerLoc(*this), *SrcMgr);
121 }
122 
123 std::pair<FullSourceLoc, StringRef> FullSourceLoc::getModuleImportLoc() const {
124  if (!isValid())
125  return std::make_pair(FullSourceLoc(), StringRef());
126 
127  std::pair<SourceLocation, StringRef> ImportLoc =
128  SrcMgr->getModuleImportLoc(*this);
129  return std::make_pair(FullSourceLoc(ImportLoc.first, *SrcMgr),
130  ImportLoc.second);
131 }
132 
133 unsigned FullSourceLoc::getFileOffset() const {
134  assert(isValid());
135  return SrcMgr->getFileOffset(*this);
136 }
137 
138 unsigned FullSourceLoc::getLineNumber(bool *Invalid) const {
139  assert(isValid());
140  return SrcMgr->getLineNumber(getFileID(), getFileOffset(), Invalid);
141 }
142 
143 unsigned FullSourceLoc::getColumnNumber(bool *Invalid) const {
144  assert(isValid());
145  return SrcMgr->getColumnNumber(getFileID(), getFileOffset(), Invalid);
146 }
147 
148 const FileEntry *FullSourceLoc::getFileEntry() const {
149  assert(isValid());
150  return SrcMgr->getFileEntryForID(getFileID());
151 }
152 
153 unsigned FullSourceLoc::getExpansionLineNumber(bool *Invalid) const {
154  assert(isValid());
155  return SrcMgr->getExpansionLineNumber(*this, Invalid);
156 }
157 
158 unsigned FullSourceLoc::getExpansionColumnNumber(bool *Invalid) const {
159  assert(isValid());
160  return SrcMgr->getExpansionColumnNumber(*this, Invalid);
161 }
162 
163 unsigned FullSourceLoc::getSpellingLineNumber(bool *Invalid) const {
164  assert(isValid());
165  return SrcMgr->getSpellingLineNumber(*this, Invalid);
166 }
167 
168 unsigned FullSourceLoc::getSpellingColumnNumber(bool *Invalid) const {
169  assert(isValid());
170  return SrcMgr->getSpellingColumnNumber(*this, Invalid);
171 }
172 
173 bool FullSourceLoc::isInSystemHeader() const {
174  assert(isValid());
175  return SrcMgr->isInSystemHeader(*this);
176 }
177 
178 bool FullSourceLoc::isBeforeInTranslationUnitThan(SourceLocation Loc) const {
179  assert(isValid());
180  return SrcMgr->isBeforeInTranslationUnit(*this, Loc);
181 }
182 
183 LLVM_DUMP_METHOD void FullSourceLoc::dump() const {
184  SourceLocation::dump(*SrcMgr);
185 }
186 
187 const char *FullSourceLoc::getCharacterData(bool *Invalid) const {
188  assert(isValid());
189  return SrcMgr->getCharacterData(*this, Invalid);
190 }
191 
192 StringRef FullSourceLoc::getBufferData(bool *Invalid) const {
193  assert(isValid());
194  return SrcMgr->getBuffer(SrcMgr->getFileID(*this), Invalid)->getBuffer();
195 }
196 
197 std::pair<FileID, unsigned> FullSourceLoc::getDecomposedLoc() const {
198  return SrcMgr->getDecomposedLoc(*this);
199 }
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.