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
17 #include "llvm/Support/PointerLikeTypeTraits.h"
26 template <typename T> struct DenseMapInfo;
27 template <typename T> struct isPodLike;
34 /// FileID - This is an opaque identifier used by SourceManager which refers to
35 /// a source file (MemoryBuffer) along with its #include path and #line data.
38 /// ID - Opaque identifier, 0 is "invalid".
43 bool isInvalid() const { return ID == 0; }
45 bool operator==(const FileID &RHS) const { return ID == RHS.ID; }
46 bool operator<(const FileID &RHS) const { return ID < RHS.ID; }
47 bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; }
48 bool operator!=(const FileID &RHS) const { return !(*this == RHS); }
49 bool operator>(const FileID &RHS) const { return RHS < *this; }
50 bool operator>=(const FileID &RHS) const { return RHS <= *this; }
52 static FileID getSentinel() { return get(~0U); }
53 unsigned getHashValue() const { return ID; }
56 friend class SourceManager;
57 friend class ASTWriter;
58 friend class ASTReader;
60 static FileID get(unsigned V) {
65 unsigned getOpaqueValue() const { return ID; }
69 /// SourceLocation - This is a carefully crafted 32-bit identifier that encodes
70 /// a full include stack, line and column number information for a position in
71 /// an input translation unit.
72 class SourceLocation {
74 friend class SourceManager;
80 SourceLocation() : ID(0) {} // 0 is an invalid FileID.
82 bool isFileID() const { return (ID & MacroIDBit) == 0; }
83 bool isMacroID() const { return (ID & MacroIDBit) != 0; }
85 /// isValid - Return true if this is a valid SourceLocation object. Invalid
86 /// SourceLocations are often used when events have no corresponding location
87 /// in the source (e.g. a diagnostic is required for a command line option).
89 bool isValid() const { return ID != 0; }
90 bool isInvalid() const { return ID == 0; }
93 /// getOffset - Return the index for SourceManager's SLocEntryTable table,
94 /// note that this is not an index *into* it though.
95 unsigned getOffset() const {
96 return ID & ~MacroIDBit;
99 static SourceLocation getFileLoc(unsigned ID) {
100 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
106 static SourceLocation getMacroLoc(unsigned ID) {
107 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
109 L.ID = MacroIDBit | ID;
114 /// getFileLocWithOffset - Return a source location with the specified offset
115 /// from this file SourceLocation.
116 SourceLocation getFileLocWithOffset(int Offset) const {
117 assert(((getOffset()+Offset) & MacroIDBit) == 0 && "invalid location");
123 /// getRawEncoding - When a SourceLocation itself cannot be used, this returns
124 /// an (opaque) 32-bit integer encoding for it. This should only be passed
125 /// to SourceLocation::getFromRawEncoding, it should not be inspected
127 unsigned getRawEncoding() const { return ID; }
129 /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into
130 /// a real SourceLocation.
131 static SourceLocation getFromRawEncoding(unsigned Encoding) {
137 /// getPtrEncoding - When a SourceLocation itself cannot be used, this returns
138 /// an (opaque) pointer encoding for it. This should only be passed
139 /// to SourceLocation::getFromPtrEncoding, it should not be inspected
141 void* getPtrEncoding() const {
142 // Double cast to avoid a warning "cast to pointer from integer of different
144 return (void*)(uintptr_t)getRawEncoding();
147 /// getFromPtrEncoding - Turn a pointer encoding of a SourceLocation object
148 /// into a real SourceLocation.
149 static SourceLocation getFromPtrEncoding(void *Encoding) {
150 return getFromRawEncoding((unsigned)(uintptr_t)Encoding);
153 void print(llvm::raw_ostream &OS, const SourceManager &SM) const;
154 void dump(const SourceManager &SM) const;
157 inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
158 return LHS.getRawEncoding() == RHS.getRawEncoding();
161 inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
162 return !(LHS == RHS);
165 inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
166 return LHS.getRawEncoding() < RHS.getRawEncoding();
169 /// SourceRange - a trival tuple used to represent a source range.
174 SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
175 SourceRange(SourceLocation loc) : B(loc), E(loc) {}
176 SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
178 SourceLocation getBegin() const { return B; }
179 SourceLocation getEnd() const { return E; }
181 void setBegin(SourceLocation b) { B = b; }
182 void setEnd(SourceLocation e) { E = e; }
184 bool isValid() const { return B.isValid() && E.isValid(); }
185 bool isInvalid() const { return !isValid(); }
187 bool operator==(const SourceRange &X) const {
188 return B == X.B && E == X.E;
191 bool operator!=(const SourceRange &X) const {
192 return B != X.B || E != X.E;
196 /// CharSourceRange - This class represents a character granular source range.
197 /// The underlying SourceRange can either specify the starting/ending character
198 /// of the range, or it can specify the start or the range and the start of the
199 /// last token of the range (a "token range"). In the token range case, the
200 /// size of the last token must be measured to determine the actual end of the
202 class CharSourceRange {
206 CharSourceRange() : IsTokenRange(false) {}
207 CharSourceRange(SourceRange R, bool ITR) : Range(R),IsTokenRange(ITR){}
209 static CharSourceRange getTokenRange(SourceRange R) {
210 CharSourceRange Result;
212 Result.IsTokenRange = true;
216 static CharSourceRange getCharRange(SourceRange R) {
217 CharSourceRange Result;
219 Result.IsTokenRange = false;
223 static CharSourceRange getTokenRange(SourceLocation B, SourceLocation E) {
224 return getTokenRange(SourceRange(B, E));
226 static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) {
227 return getCharRange(SourceRange(B, E));
230 /// isTokenRange - Return true if the end of this range specifies the start of
231 /// the last token. Return false if the end of this range specifies the last
232 /// character in the range.
233 bool isTokenRange() const { return IsTokenRange; }
235 SourceLocation getBegin() const { return Range.getBegin(); }
236 SourceLocation getEnd() const { return Range.getEnd(); }
237 const SourceRange &getAsRange() const { return Range; }
239 void setBegin(SourceLocation b) { Range.setBegin(b); }
240 void setEnd(SourceLocation e) { Range.setEnd(e); }
242 bool isValid() const { return Range.isValid(); }
243 bool isInvalid() const { return !isValid(); }
246 /// FullSourceLoc - A SourceLocation and its associated SourceManager. Useful
247 /// for argument passing to functions that expect both objects.
248 class FullSourceLoc : public SourceLocation {
249 const SourceManager *SrcMgr;
251 /// Creates a FullSourceLoc where isValid() returns false.
252 explicit FullSourceLoc() : SrcMgr(0) {}
254 explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM)
255 : SourceLocation(Loc), SrcMgr(&SM) {}
257 const SourceManager &getManager() const {
258 assert(SrcMgr && "SourceManager is NULL.");
262 FileID getFileID() const;
264 FullSourceLoc getInstantiationLoc() const;
265 FullSourceLoc getSpellingLoc() const;
267 unsigned getInstantiationLineNumber(bool *Invalid = 0) const;
268 unsigned getInstantiationColumnNumber(bool *Invalid = 0) const;
270 unsigned getSpellingLineNumber(bool *Invalid = 0) const;
271 unsigned getSpellingColumnNumber(bool *Invalid = 0) const;
273 const char *getCharacterData(bool *Invalid = 0) const;
275 const llvm::MemoryBuffer* getBuffer(bool *Invalid = 0) const;
277 /// getBufferData - Return a StringRef to the source buffer data for the
278 /// specified FileID.
279 llvm::StringRef getBufferData(bool *Invalid = 0) const;
281 /// getDecomposedLoc - Decompose the specified location into a raw FileID +
282 /// Offset pair. The first element is the FileID, the second is the
283 /// offset from the start of the buffer of the location.
284 std::pair<FileID, unsigned> getDecomposedLoc() const;
286 bool isInSystemHeader() const;
288 /// \brief Determines the order of 2 source locations in the translation unit.
290 /// \returns true if this source location comes before 'Loc', false otherwise.
291 bool isBeforeInTranslationUnitThan(SourceLocation Loc) const;
293 /// \brief Determines the order of 2 source locations in the translation unit.
295 /// \returns true if this source location comes before 'Loc', false otherwise.
296 bool isBeforeInTranslationUnitThan(FullSourceLoc Loc) const {
297 assert(Loc.isValid());
298 assert(SrcMgr == Loc.SrcMgr && "Loc comes from another SourceManager!");
299 return isBeforeInTranslationUnitThan((SourceLocation)Loc);
302 /// \brief Comparison function class, useful for sorting FullSourceLocs.
303 struct BeforeThanCompare : public std::binary_function<FullSourceLoc,
304 FullSourceLoc, bool> {
305 bool operator()(const FullSourceLoc& lhs, const FullSourceLoc& rhs) const {
306 return lhs.isBeforeInTranslationUnitThan(rhs);
310 /// Prints information about this FullSourceLoc to stderr. Useful for
312 void dump() const { SourceLocation::dump(*SrcMgr); }
315 operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
316 return LHS.getRawEncoding() == RHS.getRawEncoding() &&
317 LHS.SrcMgr == RHS.SrcMgr;
321 operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
322 return !(LHS == RHS);
327 /// PresumedLoc - This class represents an unpacked "presumed" location which
328 /// can be presented to the user. A 'presumed' location can be modified by
329 /// #line and GNU line marker directives and is always the instantiation point
330 /// of a normal location.
332 /// You can get a PresumedLoc from a SourceLocation with SourceManager.
334 const char *Filename;
336 SourceLocation IncludeLoc;
338 PresumedLoc() : Filename(0) {}
339 PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL)
340 : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) {
343 /// isInvalid - Return true if this object is invalid or uninitialized. This
344 /// occurs when created with invalid source locations or when walking off
345 /// the top of a #include stack.
346 bool isInvalid() const { return Filename == 0; }
347 bool isValid() const { return Filename != 0; }
349 /// getFilename - Return the presumed filename of this location. This can be
350 /// affected by #line etc.
351 const char *getFilename() const { return Filename; }
353 /// getLine - Return the presumed line number of this location. This can be
354 /// affected by #line etc.
355 unsigned getLine() const { return Line; }
357 /// getColumn - Return the presumed column number of this location. This can
358 /// not be affected by #line, but is packaged here for convenience.
359 unsigned getColumn() const { return Col; }
361 /// getIncludeLoc - Return the presumed include location of this location.
362 /// This can be affected by GNU linemarker directives.
363 SourceLocation getIncludeLoc() const { return IncludeLoc; }
367 } // end namespace clang
370 /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
373 struct DenseMapInfo<clang::FileID> {
374 static inline clang::FileID getEmptyKey() {
375 return clang::FileID();
377 static inline clang::FileID getTombstoneKey() {
378 return clang::FileID::getSentinel();
381 static unsigned getHashValue(clang::FileID S) {
382 return S.getHashValue();
385 static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
391 struct isPodLike<clang::SourceLocation> { static const bool value = true; };
393 struct isPodLike<clang::FileID> { static const bool value = true; };
395 // Teach SmallPtrSet how to handle SourceLocation.
397 class PointerLikeTypeTraits<clang::SourceLocation> {
399 static inline void *getAsVoidPointer(clang::SourceLocation L) {
400 return L.getPtrEncoding();
402 static inline clang::SourceLocation getFromVoidPointer(void *P) {
403 return clang::SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)P);
405 enum { NumLowBitsAvailable = 0 };
408 } // end namespace llvm