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 "clang/Basic/LLVM.h"
18 #include "llvm/Support/PointerLikeTypeTraits.h"
19 #include "llvm/Support/Compiler.h"
26 template <typename T> struct DenseMapInfo;
27 template <typename T> struct isPodLike;
34 /// \brief An opaque identifier used by SourceManager which refers to a
35 /// source file (MemoryBuffer) along with its \#include path and \#line data.
38 /// ID - Opaque identifier, 0 is "invalid". >0 is this module, <-1 is
39 /// something loaded from another module.
44 bool isInvalid() const { return ID == 0; }
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 ID <= RHS.ID; }
49 bool operator!=(const FileID &RHS) const { return !(*this == RHS); }
50 bool operator>(const FileID &RHS) const { return RHS < *this; }
51 bool operator>=(const FileID &RHS) const { return RHS <= *this; }
53 static FileID getSentinel() { return get(-1); }
54 unsigned getHashValue() const { return static_cast<unsigned>(ID); }
57 friend class SourceManager;
58 friend class ASTWriter;
59 friend class ASTReader;
61 static FileID get(int V) {
66 int getOpaqueValue() const { return ID; }
70 /// \brief Encodes a location in the source. The SourceManager can decode this
71 /// to get at the full include stack, line and column information.
73 /// Technically, a source location is simply an offset into the manager's view
74 /// of the input source, which is all input buffers (including macro
75 /// expansions) concatenated in an effectively arbitrary order. The manager
76 /// actually maintains two blocks of input buffers. One, starting at offset
77 /// 0 and growing upwards, contains all buffers from this module. The other,
78 /// starting at the highest possible offset and growing downwards, contains
79 /// buffers of loaded modules.
81 /// In addition, one bit of SourceLocation is used for quick access to the
82 /// information whether the location is in a file or a macro expansion.
84 /// It is important that this type remains small. It is currently 32 bits wide.
85 class SourceLocation {
87 friend class SourceManager;
88 friend class ASTReader;
89 friend class ASTWriter;
95 SourceLocation() : ID(0) {}
97 bool isFileID() const { return (ID & MacroIDBit) == 0; }
98 bool isMacroID() const { return (ID & MacroIDBit) != 0; }
100 /// \brief Return true if this is a valid SourceLocation object.
102 /// Invalid SourceLocations are often used when events have no corresponding
103 /// location in the source (e.g. a diagnostic is required for a command line
105 bool isValid() const { return ID != 0; }
106 bool isInvalid() const { return ID == 0; }
109 /// \brief Return the offset into the manager's global input view.
110 unsigned getOffset() const {
111 return ID & ~MacroIDBit;
114 static SourceLocation getFileLoc(unsigned ID) {
115 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
121 static SourceLocation getMacroLoc(unsigned ID) {
122 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
124 L.ID = MacroIDBit | ID;
129 /// \brief Return a source location with the specified offset from this
131 SourceLocation getLocWithOffset(int Offset) const {
132 assert(((getOffset()+Offset) & MacroIDBit) == 0 && "offset overflow");
138 /// \brief When a SourceLocation itself cannot be used, this returns
139 /// an (opaque) 32-bit integer encoding for it.
141 /// This should only be passed to SourceLocation::getFromRawEncoding, it
142 /// should not be inspected directly.
143 unsigned getRawEncoding() const { return ID; }
145 /// \brief Turn a raw encoding of a SourceLocation object into
146 /// a real SourceLocation.
148 /// \see getRawEncoding.
149 static SourceLocation getFromRawEncoding(unsigned Encoding) {
155 /// \brief When a SourceLocation itself cannot be used, this returns
156 /// an (opaque) pointer encoding for it.
158 /// This should only be passed to SourceLocation::getFromPtrEncoding, it
159 /// should not be inspected directly.
160 void* getPtrEncoding() const {
161 // Double cast to avoid a warning "cast to pointer from integer of different
163 return (void*)(uintptr_t)getRawEncoding();
166 /// getFromPtrEncoding - Turn a pointer encoding of a SourceLocation object
167 /// into a real SourceLocation.
168 static SourceLocation getFromPtrEncoding(void *Encoding) {
169 return getFromRawEncoding((unsigned)(uintptr_t)Encoding);
172 void print(raw_ostream &OS, const SourceManager &SM) const;
173 void dump(const SourceManager &SM) const;
176 inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
177 return LHS.getRawEncoding() == RHS.getRawEncoding();
180 inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
181 return !(LHS == RHS);
184 inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
185 return LHS.getRawEncoding() < RHS.getRawEncoding();
188 /// SourceRange - a trival tuple used to represent a source range.
193 SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
194 SourceRange(SourceLocation loc) : B(loc), E(loc) {}
195 SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
197 SourceLocation getBegin() const { return B; }
198 SourceLocation getEnd() const { return E; }
200 void setBegin(SourceLocation b) { B = b; }
201 void setEnd(SourceLocation e) { E = e; }
203 bool isValid() const { return B.isValid() && E.isValid(); }
204 bool isInvalid() const { return !isValid(); }
206 bool operator==(const SourceRange &X) const {
207 return B == X.B && E == X.E;
210 bool operator!=(const SourceRange &X) const {
211 return B != X.B || E != X.E;
215 /// CharSourceRange - This class represents a character granular source range.
216 /// The underlying SourceRange can either specify the starting/ending character
217 /// of the range, or it can specify the start or the range and the start of the
218 /// last token of the range (a "token range"). In the token range case, the
219 /// size of the last token must be measured to determine the actual end of the
221 class CharSourceRange {
225 CharSourceRange() : IsTokenRange(false) {}
226 CharSourceRange(SourceRange R, bool ITR) : Range(R),IsTokenRange(ITR){}
228 static CharSourceRange getTokenRange(SourceRange R) {
229 CharSourceRange Result;
231 Result.IsTokenRange = true;
235 static CharSourceRange getCharRange(SourceRange R) {
236 CharSourceRange Result;
238 Result.IsTokenRange = false;
242 static CharSourceRange getTokenRange(SourceLocation B, SourceLocation E) {
243 return getTokenRange(SourceRange(B, E));
245 static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) {
246 return getCharRange(SourceRange(B, E));
249 /// isTokenRange - Return true if the end of this range specifies the start of
250 /// the last token. Return false if the end of this range specifies the last
251 /// character in the range.
252 bool isTokenRange() const { return IsTokenRange; }
253 bool isCharRange() const { return !IsTokenRange; }
255 SourceLocation getBegin() const { return Range.getBegin(); }
256 SourceLocation getEnd() const { return Range.getEnd(); }
257 const SourceRange &getAsRange() const { return Range; }
259 void setBegin(SourceLocation b) { Range.setBegin(b); }
260 void setEnd(SourceLocation e) { Range.setEnd(e); }
262 bool isValid() const { return Range.isValid(); }
263 bool isInvalid() const { return !isValid(); }
266 /// FullSourceLoc - A SourceLocation and its associated SourceManager. Useful
267 /// for argument passing to functions that expect both objects.
268 class FullSourceLoc : public SourceLocation {
269 const SourceManager *SrcMgr;
271 /// Creates a FullSourceLoc where isValid() returns false.
272 explicit FullSourceLoc() : SrcMgr(0) {}
274 explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM)
275 : SourceLocation(Loc), SrcMgr(&SM) {}
277 const SourceManager &getManager() const {
278 assert(SrcMgr && "SourceManager is NULL.");
282 FileID getFileID() const;
284 FullSourceLoc getExpansionLoc() const;
285 FullSourceLoc getSpellingLoc() const;
287 unsigned getExpansionLineNumber(bool *Invalid = 0) const;
288 unsigned getExpansionColumnNumber(bool *Invalid = 0) const;
290 unsigned getSpellingLineNumber(bool *Invalid = 0) const;
291 unsigned getSpellingColumnNumber(bool *Invalid = 0) const;
293 const char *getCharacterData(bool *Invalid = 0) const;
295 const llvm::MemoryBuffer* getBuffer(bool *Invalid = 0) const;
297 /// getBufferData - Return a StringRef to the source buffer data for the
298 /// specified FileID.
299 StringRef getBufferData(bool *Invalid = 0) const;
301 /// getDecomposedLoc - Decompose the specified location into a raw FileID +
302 /// Offset pair. The first element is the FileID, the second is the
303 /// offset from the start of the buffer of the location.
304 std::pair<FileID, unsigned> getDecomposedLoc() const;
306 bool isInSystemHeader() const;
308 /// \brief Determines the order of 2 source locations in the translation unit.
310 /// \returns true if this source location comes before 'Loc', false otherwise.
311 bool isBeforeInTranslationUnitThan(SourceLocation Loc) const;
313 /// \brief Determines the order of 2 source locations in the translation unit.
315 /// \returns true if this source location comes before 'Loc', false otherwise.
316 bool isBeforeInTranslationUnitThan(FullSourceLoc Loc) const {
317 assert(Loc.isValid());
318 assert(SrcMgr == Loc.SrcMgr && "Loc comes from another SourceManager!");
319 return isBeforeInTranslationUnitThan((SourceLocation)Loc);
322 /// \brief Comparison function class, useful for sorting FullSourceLocs.
323 struct BeforeThanCompare : public std::binary_function<FullSourceLoc,
324 FullSourceLoc, bool> {
325 bool operator()(const FullSourceLoc& lhs, const FullSourceLoc& rhs) const {
326 return lhs.isBeforeInTranslationUnitThan(rhs);
330 /// Prints information about this FullSourceLoc to stderr. Useful for
332 LLVM_ATTRIBUTE_USED void dump() const;
335 operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
336 return LHS.getRawEncoding() == RHS.getRawEncoding() &&
337 LHS.SrcMgr == RHS.SrcMgr;
341 operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
342 return !(LHS == RHS);
347 /// PresumedLoc - This class represents an unpacked "presumed" location which
348 /// can be presented to the user. A 'presumed' location can be modified by
349 /// \#line and GNU line marker directives and is always the expansion point of
350 /// a normal location.
352 /// You can get a PresumedLoc from a SourceLocation with SourceManager.
354 const char *Filename;
356 SourceLocation IncludeLoc;
358 PresumedLoc() : Filename(0) {}
359 PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL)
360 : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) {
363 /// isInvalid - Return true if this object is invalid or uninitialized. This
364 /// occurs when created with invalid source locations or when walking off
365 /// the top of a \#include stack.
366 bool isInvalid() const { return Filename == 0; }
367 bool isValid() const { return Filename != 0; }
369 /// \brief Return the presumed filename of this location.
371 /// This can be affected by \#line etc.
372 const char *getFilename() const { return Filename; }
374 /// \brief Return the presumed line number of this location.
376 /// This can be affected by \#line etc.
377 unsigned getLine() const { return Line; }
379 /// \brief Return the presumed column number of this location.
381 /// This cannot be affected by \#line, but is packaged here for convenience.
382 unsigned getColumn() const { return Col; }
384 /// \brief Return the presumed include location of this location.
386 /// This can be affected by GNU linemarker directives.
387 SourceLocation getIncludeLoc() const { return IncludeLoc; }
391 } // end namespace clang
394 /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
397 struct DenseMapInfo<clang::FileID> {
398 static inline clang::FileID getEmptyKey() {
399 return clang::FileID();
401 static inline clang::FileID getTombstoneKey() {
402 return clang::FileID::getSentinel();
405 static unsigned getHashValue(clang::FileID S) {
406 return S.getHashValue();
409 static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
415 struct isPodLike<clang::SourceLocation> { static const bool value = true; };
417 struct isPodLike<clang::FileID> { static const bool value = true; };
419 // Teach SmallPtrSet how to handle SourceLocation.
421 class PointerLikeTypeTraits<clang::SourceLocation> {
423 static inline void *getAsVoidPointer(clang::SourceLocation L) {
424 return L.getPtrEncoding();
426 static inline clang::SourceLocation getFromVoidPointer(void *P) {
427 return clang::SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)P);
429 enum { NumLowBitsAvailable = 0 };
432 } // end namespace llvm