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 //===----------------------------------------------------------------------===//
11 /// \brief Defines the clang::SourceLocation class and associated facilities.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_SOURCELOCATION_H
16 #define LLVM_CLANG_SOURCELOCATION_H
18 #include "clang/Basic/LLVM.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/PointerLikeTypeTraits.h"
28 template <typename T> struct DenseMapInfo;
29 template <typename T> struct isPodLike;
36 /// \brief An opaque identifier used by SourceManager which refers to a
37 /// source file (MemoryBuffer) along with its \#include path and \#line data.
40 /// \brief A mostly-opaque identifier, where 0 is "invalid", >0 is
41 /// this module, and <-1 is something loaded from another module.
46 bool isInvalid() const { return ID == 0; }
48 bool operator==(const FileID &RHS) const { return ID == RHS.ID; }
49 bool operator<(const FileID &RHS) const { return ID < RHS.ID; }
50 bool operator<=(const FileID &RHS) const { return ID <= RHS.ID; }
51 bool operator!=(const FileID &RHS) const { return !(*this == RHS); }
52 bool operator>(const FileID &RHS) const { return RHS < *this; }
53 bool operator>=(const FileID &RHS) const { return RHS <= *this; }
55 static FileID getSentinel() { return get(-1); }
56 unsigned getHashValue() const { return static_cast<unsigned>(ID); }
59 friend class SourceManager;
60 friend class ASTWriter;
61 friend class ASTReader;
63 static FileID get(int V) {
68 int getOpaqueValue() const { return ID; }
72 /// \brief Encodes a location in the source. The SourceManager can decode this
73 /// to get at the full include stack, line and column information.
75 /// Technically, a source location is simply an offset into the manager's view
76 /// of the input source, which is all input buffers (including macro
77 /// expansions) concatenated in an effectively arbitrary order. The manager
78 /// actually maintains two blocks of input buffers. One, starting at offset
79 /// 0 and growing upwards, contains all buffers from this module. The other,
80 /// starting at the highest possible offset and growing downwards, contains
81 /// buffers of loaded modules.
83 /// In addition, one bit of SourceLocation is used for quick access to the
84 /// information whether the location is in a file or a macro expansion.
86 /// It is important that this type remains small. It is currently 32 bits wide.
87 class SourceLocation {
89 friend class SourceManager;
90 friend class ASTReader;
91 friend class ASTWriter;
97 SourceLocation() : ID(0) {}
99 bool isFileID() const { return (ID & MacroIDBit) == 0; }
100 bool isMacroID() const { return (ID & MacroIDBit) != 0; }
102 /// \brief Return true if this is a valid SourceLocation object.
104 /// Invalid SourceLocations are often used when events have no corresponding
105 /// location in the source (e.g. a diagnostic is required for a command line
107 bool isValid() const { return ID != 0; }
108 bool isInvalid() const { return ID == 0; }
111 /// \brief Return the offset into the manager's global input view.
112 unsigned getOffset() const {
113 return ID & ~MacroIDBit;
116 static SourceLocation getFileLoc(unsigned ID) {
117 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
123 static SourceLocation getMacroLoc(unsigned ID) {
124 assert((ID & MacroIDBit) == 0 && "Ran out of source locations!");
126 L.ID = MacroIDBit | ID;
131 /// \brief Return a source location with the specified offset from this
133 SourceLocation getLocWithOffset(int Offset) const {
134 assert(((getOffset()+Offset) & MacroIDBit) == 0 && "offset overflow");
140 /// \brief When a SourceLocation itself cannot be used, this returns
141 /// an (opaque) 32-bit integer encoding for it.
143 /// This should only be passed to SourceLocation::getFromRawEncoding, it
144 /// should not be inspected directly.
145 unsigned getRawEncoding() const { return ID; }
147 /// \brief Turn a raw encoding of a SourceLocation object into
148 /// a real SourceLocation.
150 /// \see getRawEncoding.
151 static SourceLocation getFromRawEncoding(unsigned Encoding) {
157 /// \brief When a SourceLocation itself cannot be used, this returns
158 /// an (opaque) pointer encoding for it.
160 /// This should only be passed to SourceLocation::getFromPtrEncoding, it
161 /// should not be inspected directly.
162 void* getPtrEncoding() const {
163 // Double cast to avoid a warning "cast to pointer from integer of different
165 return (void*)(uintptr_t)getRawEncoding();
168 /// \brief Turn a pointer encoding of a SourceLocation object back
169 /// into a real SourceLocation.
170 static SourceLocation getFromPtrEncoding(const void *Encoding) {
171 return getFromRawEncoding((unsigned)(uintptr_t)Encoding);
174 void print(raw_ostream &OS, const SourceManager &SM) const;
175 LLVM_ATTRIBUTE_USED std::string printToString(const SourceManager &SM) const;
176 void dump(const SourceManager &SM) const;
179 inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) {
180 return LHS.getRawEncoding() == RHS.getRawEncoding();
183 inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) {
184 return !(LHS == RHS);
187 inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) {
188 return LHS.getRawEncoding() < RHS.getRawEncoding();
191 /// \brief A trival tuple used to represent a source range.
196 SourceRange(): B(SourceLocation()), E(SourceLocation()) {}
197 SourceRange(SourceLocation loc) : B(loc), E(loc) {}
198 SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {}
200 SourceLocation getBegin() const { return B; }
201 SourceLocation getEnd() const { return E; }
203 void setBegin(SourceLocation b) { B = b; }
204 void setEnd(SourceLocation e) { E = e; }
206 bool isValid() const { return B.isValid() && E.isValid(); }
207 bool isInvalid() const { return !isValid(); }
209 bool operator==(const SourceRange &X) const {
210 return B == X.B && E == X.E;
213 bool operator!=(const SourceRange &X) const {
214 return B != X.B || E != X.E;
218 /// \brief Represents a character-granular source range.
220 /// The underlying SourceRange can either specify the starting/ending character
221 /// of the range, or it can specify the start of the range and the start of the
222 /// last token of the range (a "token range"). In the token range case, the
223 /// size of the last token must be measured to determine the actual end of the
225 class CharSourceRange {
229 CharSourceRange() : IsTokenRange(false) {}
230 CharSourceRange(SourceRange R, bool ITR) : Range(R), IsTokenRange(ITR) {}
232 static CharSourceRange getTokenRange(SourceRange R) {
233 return CharSourceRange(R, true);
236 static CharSourceRange getCharRange(SourceRange R) {
237 return CharSourceRange(R, false);
240 static CharSourceRange getTokenRange(SourceLocation B, SourceLocation E) {
241 return getTokenRange(SourceRange(B, E));
243 static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) {
244 return getCharRange(SourceRange(B, E));
247 /// \brief Return true if the end of this range specifies the start of
248 /// the last token. Return false if the end of this range specifies the last
249 /// character in the range.
250 bool isTokenRange() const { return IsTokenRange; }
251 bool isCharRange() const { return !IsTokenRange; }
253 SourceLocation getBegin() const { return Range.getBegin(); }
254 SourceLocation getEnd() const { return Range.getEnd(); }
255 const SourceRange &getAsRange() const { return Range; }
257 void setBegin(SourceLocation b) { Range.setBegin(b); }
258 void setEnd(SourceLocation e) { Range.setEnd(e); }
260 bool isValid() const { return Range.isValid(); }
261 bool isInvalid() const { return !isValid(); }
264 /// \brief A SourceLocation and its associated SourceManager.
266 /// This is useful for argument passing to functions that expect both objects.
267 class FullSourceLoc : public SourceLocation {
268 const SourceManager *SrcMgr;
270 /// \brief Creates a FullSourceLoc where isValid() returns \c false.
271 explicit FullSourceLoc() : SrcMgr(0) {}
273 explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM)
274 : SourceLocation(Loc), SrcMgr(&SM) {}
276 /// \pre This FullSourceLoc has an associated SourceManager.
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 /// \brief Return a StringRef to the source buffer data for the
298 /// specified FileID.
299 StringRef getBufferData(bool *Invalid = 0) const;
301 /// \brief Decompose the specified location into a raw FileID + Offset pair.
303 /// The first element is the FileID, the second is the offset from the
304 /// start of the buffer of the location.
305 std::pair<FileID, unsigned> getDecomposedLoc() const;
307 bool isInSystemHeader() const;
309 /// \brief Determines the order of 2 source locations in the translation unit.
311 /// \returns true if this source location comes before 'Loc', false otherwise.
312 bool isBeforeInTranslationUnitThan(SourceLocation Loc) const;
314 /// \brief Determines the order of 2 source locations in the translation unit.
316 /// \returns true if this source location comes before 'Loc', false otherwise.
317 bool isBeforeInTranslationUnitThan(FullSourceLoc Loc) const {
318 assert(Loc.isValid());
319 assert(SrcMgr == Loc.SrcMgr && "Loc comes from another SourceManager!");
320 return isBeforeInTranslationUnitThan((SourceLocation)Loc);
323 /// \brief Comparison function class, useful for sorting FullSourceLocs.
324 struct BeforeThanCompare : public std::binary_function<FullSourceLoc,
325 FullSourceLoc, bool> {
326 bool operator()(const FullSourceLoc& lhs, const FullSourceLoc& rhs) const {
327 return lhs.isBeforeInTranslationUnitThan(rhs);
331 /// \brief Prints information about this FullSourceLoc to stderr.
333 /// This is useful for debugging.
334 LLVM_ATTRIBUTE_USED void dump() const;
337 operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
338 return LHS.getRawEncoding() == RHS.getRawEncoding() &&
339 LHS.SrcMgr == RHS.SrcMgr;
343 operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) {
344 return !(LHS == RHS);
349 /// \brief Represents an unpacked "presumed" location which can be presented
352 /// A 'presumed' location can be modified by \#line and GNU line marker
353 /// directives and is always the expansion point of a normal location.
355 /// You can get a PresumedLoc from a SourceLocation with SourceManager.
357 const char *Filename;
359 SourceLocation IncludeLoc;
361 PresumedLoc() : Filename(0) {}
362 PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL)
363 : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) {
366 /// \brief Return true if this object is invalid or uninitialized.
368 /// This occurs when created with invalid source locations or when walking
369 /// off the top of a \#include stack.
370 bool isInvalid() const { return Filename == 0; }
371 bool isValid() const { return Filename != 0; }
373 /// \brief Return the presumed filename of this location.
375 /// This can be affected by \#line etc.
376 const char *getFilename() const { return Filename; }
378 /// \brief Return the presumed line number of this location.
380 /// This can be affected by \#line etc.
381 unsigned getLine() const { return Line; }
383 /// \brief Return the presumed column number of this location.
385 /// This cannot be affected by \#line, but is packaged here for convenience.
386 unsigned getColumn() const { return Col; }
388 /// \brief Return the presumed include location of this location.
390 /// This can be affected by GNU linemarker directives.
391 SourceLocation getIncludeLoc() const { return IncludeLoc; }
395 } // end namespace clang
398 /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and
401 struct DenseMapInfo<clang::FileID> {
402 static inline clang::FileID getEmptyKey() {
403 return clang::FileID();
405 static inline clang::FileID getTombstoneKey() {
406 return clang::FileID::getSentinel();
409 static unsigned getHashValue(clang::FileID S) {
410 return S.getHashValue();
413 static bool isEqual(clang::FileID LHS, clang::FileID RHS) {
419 struct isPodLike<clang::SourceLocation> { static const bool value = true; };
421 struct isPodLike<clang::FileID> { static const bool value = true; };
423 // Teach SmallPtrSet how to handle SourceLocation.
425 class PointerLikeTypeTraits<clang::SourceLocation> {
427 static inline void *getAsVoidPointer(clang::SourceLocation L) {
428 return L.getPtrEncoding();
430 static inline clang::SourceLocation getFromVoidPointer(void *P) {
431 return clang::SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)P);
433 enum { NumLowBitsAvailable = 0 };
436 } // end namespace llvm