1 //===--- SourceLocation.h - Compact identifier for Source Files -*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the SourceLocation class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_SOURCELOCATION_H
15 #define LLVM_CLANG_SOURCELOCATION_H
23 template <typename T> struct DenseMapInfo;
31 /// FileID - This is an opaque identifier used by SourceManager which refers to
32 /// a source file (MemoryBuffer) along with its #include path and #line data.
35 /// ID - Opaque identifier, 0 is "invalid".
40 bool isInvalid() const { return ID == 0; }
42 bool operator==(const FileID &RHS) const { return ID == RHS.ID; }
43 bool operator<(const FileID &RHS) const { return ID < RHS.ID; }
44 bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; }
45 bool operator!=(const FileID &RHS) const { return !(*this == RHS); }
46 bool operator>(const FileID &RHS) const { return RHS < *this; }
47 bool operator>=(const FileID &RHS) const { return RHS <= *this; }
49 static FileID getSentinel() { return get(~0U); }
50 unsigned getHashValue() const { return ID; }
53 friend class SourceManager;
54 static FileID get(unsigned V) {
59 unsigned getOpaqueValue() const { return ID; }
63 /// SourceLocation - This is a carefully crafted 32-bit identifier that encodes
64 /// a full include stack, line and column number information for a position in
65 /// an input translation unit.
66 class SourceLocation {
68 friend class SourceManager;
74 SourceLocation() : ID(0) {} // 0 is an invalid FileID.
76 bool isFileID() const { return (ID & MacroIDBit) == 0; }
77 bool isMacroID() const { return (ID & MacroIDBit) != 0; }
79 /// isValid - Return true if this is a valid SourceLocation object. Invalid
80 /// SourceLocations are often used when events have no corresponding location
81 /// in the source (e.g. a diagnostic is required for a command line option).
83 bool isValid() const { return ID != 0; }
84 bool isInvalid() const { return ID == 0; }
87 /// getOffset - Return the index for SourceManager's SLocEntryTable table,
88 /// note that this is not an index *into* it though.
89 unsigned getOffset() const {
90 return ID & ~MacroIDBit;
93 static SourceLocation getFileLoc(unsigned ID) {
94 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
100 static SourceLocation getMacroLoc(unsigned ID) {
101 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
103 L.ID = MacroIDBit | ID;
108 /// getFileLocWithOffset - Return a source location with the specified offset
109 /// from this file SourceLocation.
110 SourceLocation getFileLocWithOffset(int Offset) const {
111 assert(((getOffset()+Offset) & MacroIDBit) == 0 && "invalid location");
117 /// getRawEncoding - When a SourceLocation itself cannot be used, this returns
118 /// an (opaque) 32-bit integer encoding for it. This should only be passed
119 /// to SourceLocation::getFromRawEncoding, it should not be inspected
121 unsigned getRawEncoding() const { return ID; }
124 /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into
125 /// a real SourceLocation.
126 static SourceLocation getFromRawEncoding(unsigned Encoding) {
132 void print(llvm::raw_ostream &OS, const SourceManager &SM) const;
133 void dump(const SourceManager &SM) const;
136 inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
137 return LHS.getRawEncoding() == RHS.getRawEncoding();
140 inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
141 return !(LHS == RHS);
144 inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
145 return LHS.getRawEncoding() < RHS.getRawEncoding();
148 inline bool operator<=(const SourceLocation &LHS, const SourceLocation &RHS) {
149 return LHS.getRawEncoding() <= RHS.getRawEncoding();
152 /// SourceRange - a trival tuple used to represent a source range.
157 SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
158 SourceRange(SourceLocation loc) : B(loc), E(loc) {}
159 SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
161 SourceLocation getBegin() const { return B; }
162 SourceLocation getEnd() const { return E; }
164 void setBegin(SourceLocation b) { B = b; }
165 void setEnd(SourceLocation e) { E = e; }
167 bool isValid() const { return B.isValid() && E.isValid(); }
169 bool operator==(const SourceRange &X) const {
170 return B == X.B && E == X.E;
173 bool operator!=(const SourceRange &X) const {
174 return B != X.B || E != X.E;
178 /// FullSourceLoc - A SourceLocation and its associated SourceManager. Useful
179 /// for argument passing to functions that expect both objects.
180 class FullSourceLoc : public SourceLocation {
181 SourceManager* SrcMgr;
183 /// Creates a FullSourceLoc where isValid() returns false.
184 explicit FullSourceLoc() : SrcMgr((SourceManager*) 0) {}
186 explicit FullSourceLoc(SourceLocation Loc, SourceManager &SM)
187 : SourceLocation(Loc), SrcMgr(&SM) {}
189 SourceManager &getManager() {
190 assert(SrcMgr && "SourceManager is NULL.");
194 const SourceManager &getManager() const {
195 assert(SrcMgr && "SourceManager is NULL.");
199 FileID getFileID() const;
201 FullSourceLoc getInstantiationLoc() const;
202 FullSourceLoc getSpellingLoc() const;
204 unsigned getInstantiationLineNumber() const;
205 unsigned getInstantiationColumnNumber() const;
207 unsigned getSpellingLineNumber() const;
208 unsigned getSpellingColumnNumber() const;
210 const char *getCharacterData() const;
212 const llvm::MemoryBuffer* getBuffer() const;
214 /// getBufferData - Return a pointer to the start and end of the source buffer
215 /// data for the specified FileID.
216 std::pair<const char*, const char*> getBufferData() const;
218 /// getDecomposedLoc - Decompose the specified location into a raw FileID +
219 /// Offset pair. The first element is the FileID, the second is the
220 /// offset from the start of the buffer of the location.
221 std::pair<FileID, unsigned> getDecomposedLoc() const;
223 bool isInSystemHeader() const;
225 /// Prints information about this FullSourceLoc to stderr. Useful for
227 void dump() const { SourceLocation::dump(*SrcMgr); }
230 operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
231 return LHS.getRawEncoding() == RHS.getRawEncoding() &&
232 LHS.SrcMgr == RHS.SrcMgr;
236 operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
237 return !(LHS == RHS);
242 /// PresumedLoc - This class represents an unpacked "presumed" location which
243 /// can be presented to the user. A 'presumed' location can be modified by
244 /// #line and GNU line marker directives and is always the instantiation point
245 /// of a normal location.
247 /// You can get a PresumedLoc from a SourceLocation with SourceManager.
249 const char *Filename;
251 SourceLocation IncludeLoc;
253 PresumedLoc() : Filename(0) {}
254 PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL)
255 : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) {
258 /// isInvalid - Return true if this object is invalid or uninitialized. This
259 /// occurs when created with invalid source locations or when walking off
260 /// the top of a #include stack.
261 bool isInvalid() const { return Filename == 0; }
262 bool isValid() const { return Filename != 0; }
264 /// getFilename - Return the presumed filename of this location. This can be
265 /// affected by #line etc.
266 const char *getFilename() const { return Filename; }
268 /// getLine - Return the presumed line number of this location. This can be
269 /// affected by #line etc.
270 unsigned getLine() const { return Line; }
272 /// getColumn - Return the presumed column number of this location. This can
273 /// not be affected by #line, but is packaged here for convenience.
274 unsigned getColumn() const { return Col; }
276 /// getIncludeLoc - Return the presumed include location of this location.
277 /// This can be affected by GNU linemarker directives.
278 SourceLocation getIncludeLoc() const { return IncludeLoc; }
282 } // end namespace clang
285 /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
288 struct DenseMapInfo<clang::FileID> {
289 static inline clang::FileID getEmptyKey() {
290 return clang::FileID();
292 static inline clang::FileID getTombstoneKey() {
293 return clang::FileID::getSentinel();
296 static unsigned getHashValue(clang::FileID S) {
297 return S.getHashValue();
300 static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
304 static bool isPod() { return true; }
307 } // end namespace llvm