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
24 template <typename T> struct DenseMapInfo;
25 template <typename T> struct isPodLike;
32 /// FileID - This is an opaque identifier used by SourceManager which refers to
33 /// a source file (MemoryBuffer) along with its #include path and #line data.
36 /// ID - Opaque identifier, 0 is "invalid".
41 bool isInvalid() const { return ID == 0; }
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 ID <= RHS.ID; }
46 bool operator!=(const FileID &RHS) const { return !(*this == RHS); }
47 bool operator>(const FileID &RHS) const { return RHS < *this; }
48 bool operator>=(const FileID &RHS) const { return RHS <= *this; }
50 static FileID getSentinel() { return get(~0U); }
51 unsigned getHashValue() const { return ID; }
54 friend class SourceManager;
55 static FileID get(unsigned V) {
60 unsigned getOpaqueValue() const { return ID; }
64 /// SourceLocation - This is a carefully crafted 32-bit identifier that encodes
65 /// a full include stack, line and column number information for a position in
66 /// an input translation unit.
67 class SourceLocation {
69 friend class SourceManager;
75 SourceLocation() : ID(0) {} // 0 is an invalid FileID.
77 bool isFileID() const { return (ID & MacroIDBit) == 0; }
78 bool isMacroID() const { return (ID & MacroIDBit) != 0; }
80 /// isValid - Return true if this is a valid SourceLocation object. Invalid
81 /// SourceLocations are often used when events have no corresponding location
82 /// in the source (e.g. a diagnostic is required for a command line option).
84 bool isValid() const { return ID != 0; }
85 bool isInvalid() const { return ID == 0; }
88 /// getOffset - Return the index for SourceManager's SLocEntryTable table,
89 /// note that this is not an index *into* it though.
90 unsigned getOffset() const {
91 return ID & ~MacroIDBit;
94 static SourceLocation getFileLoc(unsigned ID) {
95 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
101 static SourceLocation getMacroLoc(unsigned ID) {
102 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
104 L.ID = MacroIDBit | ID;
109 /// getFileLocWithOffset - Return a source location with the specified offset
110 /// from this file SourceLocation.
111 SourceLocation getFileLocWithOffset(int Offset) const {
112 assert(((getOffset()+Offset) & MacroIDBit) == 0 && "invalid location");
118 /// getRawEncoding - When a SourceLocation itself cannot be used, this returns
119 /// an (opaque) 32-bit integer encoding for it. This should only be passed
120 /// to SourceLocation::getFromRawEncoding, it should not be inspected
122 unsigned getRawEncoding() const { return ID; }
125 /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into
126 /// a real SourceLocation.
127 static SourceLocation getFromRawEncoding(unsigned Encoding) {
133 void print(llvm::raw_ostream &OS, const SourceManager &SM) const;
134 void dump(const SourceManager &SM) const;
137 inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
138 return LHS.getRawEncoding() == RHS.getRawEncoding();
141 inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
142 return !(LHS == RHS);
145 inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
146 return LHS.getRawEncoding() < RHS.getRawEncoding();
149 /// SourceRange - a trival tuple used to represent a source range.
154 SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
155 SourceRange(SourceLocation loc) : B(loc), E(loc) {}
156 SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
158 SourceLocation getBegin() const { return B; }
159 SourceLocation getEnd() const { return E; }
161 void setBegin(SourceLocation b) { B = b; }
162 void setEnd(SourceLocation e) { E = e; }
164 bool isValid() const { return B.isValid() && E.isValid(); }
165 bool isInvalid() const { return !isValid(); }
167 bool operator==(const SourceRange &X) const {
168 return B == X.B && E == X.E;
171 bool operator!=(const SourceRange &X) const {
172 return B != X.B || E != X.E;
176 /// CharSourceRange - This class represents a character granular source range.
177 /// The underlying SourceRange can either specify the starting/ending character
178 /// of the range, or it can specify the start or the range and the start of the
179 /// last token of the range (a "token range"). In the token range case, the
180 /// size of the last token must be measured to determine the actual end of the
182 class CharSourceRange {
186 CharSourceRange() : IsTokenRange(false) {}
187 CharSourceRange(SourceRange R, bool ITR) : Range(R),IsTokenRange(ITR){}
189 static CharSourceRange getTokenRange(SourceRange R) {
190 CharSourceRange Result;
192 Result.IsTokenRange = true;
196 static CharSourceRange getCharRange(SourceRange R) {
197 CharSourceRange Result;
199 Result.IsTokenRange = false;
203 static CharSourceRange getTokenRange(SourceLocation B, SourceLocation E) {
204 return getTokenRange(SourceRange(B, E));
206 static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) {
207 return getCharRange(SourceRange(B, E));
210 /// isTokenRange - Return true if the end of this range specifies the start of
211 /// the last token. Return false if the end of this range specifies the last
212 /// character in the range.
213 bool isTokenRange() const { return IsTokenRange; }
215 SourceLocation getBegin() const { return Range.getBegin(); }
216 SourceLocation getEnd() const { return Range.getEnd(); }
217 const SourceRange &getAsRange() const { return Range; }
219 void setBegin(SourceLocation b) { Range.setBegin(b); }
220 void setEnd(SourceLocation e) { Range.setEnd(e); }
222 bool isValid() const { return Range.isValid(); }
223 bool isInvalid() const { return !isValid(); }
226 /// FullSourceLoc - A SourceLocation and its associated SourceManager. Useful
227 /// for argument passing to functions that expect both objects.
228 class FullSourceLoc : public SourceLocation {
229 const SourceManager *SrcMgr;
231 /// Creates a FullSourceLoc where isValid() returns false.
232 explicit FullSourceLoc() : SrcMgr(0) {}
234 explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM)
235 : SourceLocation(Loc), SrcMgr(&SM) {}
237 const SourceManager &getManager() const {
238 assert(SrcMgr && "SourceManager is NULL.");
242 FileID getFileID() const;
244 FullSourceLoc getInstantiationLoc() const;
245 FullSourceLoc getSpellingLoc() const;
247 unsigned getInstantiationLineNumber(bool *Invalid = 0) const;
248 unsigned getInstantiationColumnNumber(bool *Invalid = 0) const;
250 unsigned getSpellingLineNumber(bool *Invalid = 0) const;
251 unsigned getSpellingColumnNumber(bool *Invalid = 0) const;
253 const char *getCharacterData(bool *Invalid = 0) const;
255 const llvm::MemoryBuffer* getBuffer(bool *Invalid = 0) const;
257 /// getBufferData - Return a StringRef to the source buffer data for the
258 /// specified FileID.
259 llvm::StringRef getBufferData(bool *Invalid = 0) const;
261 /// getDecomposedLoc - Decompose the specified location into a raw FileID +
262 /// Offset pair. The first element is the FileID, the second is the
263 /// offset from the start of the buffer of the location.
264 std::pair<FileID, unsigned> getDecomposedLoc() const;
266 bool isInSystemHeader() const;
268 /// Prints information about this FullSourceLoc to stderr. Useful for
270 void dump() const { SourceLocation::dump(*SrcMgr); }
273 operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
274 return LHS.getRawEncoding() == RHS.getRawEncoding() &&
275 LHS.SrcMgr == RHS.SrcMgr;
279 operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
280 return !(LHS == RHS);
285 /// PresumedLoc - This class represents an unpacked "presumed" location which
286 /// can be presented to the user. A 'presumed' location can be modified by
287 /// #line and GNU line marker directives and is always the instantiation point
288 /// of a normal location.
290 /// You can get a PresumedLoc from a SourceLocation with SourceManager.
292 const char *Filename;
294 SourceLocation IncludeLoc;
296 PresumedLoc() : Filename(0) {}
297 PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL)
298 : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) {
301 /// isInvalid - Return true if this object is invalid or uninitialized. This
302 /// occurs when created with invalid source locations or when walking off
303 /// the top of a #include stack.
304 bool isInvalid() const { return Filename == 0; }
305 bool isValid() const { return Filename != 0; }
307 /// getFilename - Return the presumed filename of this location. This can be
308 /// affected by #line etc.
309 const char *getFilename() const { return Filename; }
311 /// getLine - Return the presumed line number of this location. This can be
312 /// affected by #line etc.
313 unsigned getLine() const { return Line; }
315 /// getColumn - Return the presumed column number of this location. This can
316 /// not be affected by #line, but is packaged here for convenience.
317 unsigned getColumn() const { return Col; }
319 /// getIncludeLoc - Return the presumed include location of this location.
320 /// This can be affected by GNU linemarker directives.
321 SourceLocation getIncludeLoc() const { return IncludeLoc; }
325 } // end namespace clang
328 /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
331 struct DenseMapInfo<clang::FileID> {
332 static inline clang::FileID getEmptyKey() {
333 return clang::FileID();
335 static inline clang::FileID getTombstoneKey() {
336 return clang::FileID::getSentinel();
339 static unsigned getHashValue(clang::FileID S) {
340 return S.getHashValue();
343 static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
349 struct isPodLike<clang::SourceLocation> { static const bool value = true; };
351 struct isPodLike<clang::FileID> { static const bool value = true; };
353 } // end namespace llvm