Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SourceLocation.h - Compact identifier for Source Files -*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 0bc735f | 2007-12-29 19:59:25 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file defines the SourceLocation class. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_CLANG_SOURCELOCATION_H |
| 15 | #define LLVM_CLANG_SOURCELOCATION_H |
| 16 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 17 | #include "llvm/Support/PointerLikeTypeTraits.h" |
Ted Kremenek | 3632a35 | 2009-01-28 20:46:26 +0000 | [diff] [blame] | 18 | #include <utility> |
Argyrios Kyrtzidis | 8f89652 | 2011-04-07 18:10:07 +0000 | [diff] [blame] | 19 | #include <functional> |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 20 | #include <cassert> |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 21 | |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 22 | namespace llvm { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 23 | class MemoryBuffer; |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 24 | class raw_ostream; |
Benjamin Kramer | ceafc4b | 2010-03-16 14:48:07 +0000 | [diff] [blame] | 25 | class StringRef; |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 26 | template <typename T> struct DenseMapInfo; |
Chris Lattner | 06159e8 | 2009-12-15 07:26:51 +0000 | [diff] [blame] | 27 | template <typename T> struct isPodLike; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 28 | } |
| 29 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 30 | namespace clang { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 31 | |
Ted Kremenek | a9793ed | 2007-12-12 18:16:46 +0000 | [diff] [blame] | 32 | class SourceManager; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 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. |
| 36 | /// |
| 37 | class FileID { |
| 38 | /// ID - Opaque identifier, 0 is "invalid". |
| 39 | unsigned ID; |
| 40 | public: |
| 41 | FileID() : ID(0) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 42 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 43 | bool isInvalid() const { return ID == 0; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 44 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 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; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 52 | static FileID getSentinel() { return get(~0U); } |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 53 | unsigned getHashValue() const { return ID; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 55 | private: |
| 56 | friend class SourceManager; |
Douglas Gregor | 31d375f | 2011-05-06 21:43:30 +0000 | [diff] [blame] | 57 | friend class ASTWriter; |
| 58 | friend class ASTReader; |
| 59 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 60 | static FileID get(unsigned V) { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 61 | FileID F; |
| 62 | F.ID = V; |
| 63 | return F; |
| 64 | } |
| 65 | unsigned getOpaqueValue() const { return ID; } |
| 66 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 67 | |
| 68 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 { |
| 73 | unsigned ID; |
Chris Lattner | bcc2a67 | 2009-01-19 06:46:35 +0000 | [diff] [blame] | 74 | friend class SourceManager; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 75 | enum { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 76 | MacroIDBit = 1U << 31 |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 77 | }; |
Chris Lattner | 9ebac5e | 2009-01-19 06:57:37 +0000 | [diff] [blame] | 78 | public: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 79 | |
| 80 | SourceLocation() : ID(0) {} // 0 is an invalid FileID. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 81 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 82 | bool isFileID() const { return (ID & MacroIDBit) == 0; } |
| 83 | bool isMacroID() const { return (ID & MacroIDBit) != 0; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 84 | |
Chris Lattner | b7489d8 | 2007-11-09 23:52:16 +0000 | [diff] [blame] | 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). |
| 88 | /// |
| 89 | bool isValid() const { return ID != 0; } |
| 90 | bool isInvalid() const { return ID == 0; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 91 | |
Chris Lattner | 6fda54c | 2009-01-19 07:00:35 +0000 | [diff] [blame] | 92 | private: |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 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; |
Chris Lattner | 4d10ef1 | 2009-01-19 06:55:08 +0000 | [diff] [blame] | 97 | } |
Chris Lattner | 6fda54c | 2009-01-19 07:00:35 +0000 | [diff] [blame] | 98 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 99 | static SourceLocation getFileLoc(unsigned ID) { |
| 100 | assert((ID & MacroIDBit) == 0 && "Ran out of source locations!"); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 101 | SourceLocation L; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 102 | L.ID = ID; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 103 | return L; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 104 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 105 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 106 | static SourceLocation getMacroLoc(unsigned ID) { |
| 107 | assert((ID & MacroIDBit) == 0 && "Ran out of source locations!"); |
Chris Lattner | d1623a8 | 2007-07-21 06:41:57 +0000 | [diff] [blame] | 108 | SourceLocation L; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 109 | L.ID = MacroIDBit | ID; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 110 | return L; |
| 111 | } |
Chris Lattner | 4d10ef1 | 2009-01-19 06:55:08 +0000 | [diff] [blame] | 112 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 113 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 114 | /// getFileLocWithOffset - Return a source location with the specified offset |
| 115 | /// from this file SourceLocation. |
Chris Lattner | d1623a8 | 2007-07-21 06:41:57 +0000 | [diff] [blame] | 116 | SourceLocation getFileLocWithOffset(int Offset) const { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 117 | assert(((getOffset()+Offset) & MacroIDBit) == 0 && "invalid location"); |
| 118 | SourceLocation L; |
| 119 | L.ID = ID+Offset; |
| 120 | return L; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 121 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 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 |
| 126 | /// directly. |
| 127 | unsigned getRawEncoding() const { return ID; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 129 | /// getFromRawEncoding - Turn a raw encoding of a SourceLocation object into |
| 130 | /// a real SourceLocation. |
| 131 | static SourceLocation getFromRawEncoding(unsigned Encoding) { |
| 132 | SourceLocation X; |
| 133 | X.ID = Encoding; |
| 134 | return X; |
| 135 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 136 | |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 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 |
| 140 | /// directly. |
| 141 | void* getPtrEncoding() const { |
| 142 | // Double cast to avoid a warning "cast to pointer from integer of different |
| 143 | // size". |
| 144 | return (void*)(uintptr_t)getRawEncoding(); |
| 145 | } |
| 146 | |
| 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); |
| 151 | } |
| 152 | |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 153 | void print(llvm::raw_ostream &OS, const SourceManager &SM) const; |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 154 | void dump(const SourceManager &SM) const; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 155 | }; |
| 156 | |
| 157 | inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) { |
| 158 | return LHS.getRawEncoding() == RHS.getRawEncoding(); |
| 159 | } |
| 160 | |
| 161 | inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) { |
| 162 | return !(LHS == RHS); |
| 163 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | |
Chris Lattner | 6fda54c | 2009-01-19 07:00:35 +0000 | [diff] [blame] | 165 | inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) { |
| 166 | return LHS.getRawEncoding() < RHS.getRawEncoding(); |
| 167 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | |
| 169 | /// SourceRange - a trival tuple used to represent a source range. |
| 170 | class SourceRange { |
| 171 | SourceLocation B; |
| 172 | SourceLocation E; |
| 173 | public: |
| 174 | SourceRange(): B(SourceLocation()), E(SourceLocation()) {} |
| 175 | SourceRange(SourceLocation loc) : B(loc), E(loc) {} |
| 176 | SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 177 | |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 178 | SourceLocation getBegin() const { return B; } |
| 179 | SourceLocation getEnd() const { return E; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 180 | |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 181 | void setBegin(SourceLocation b) { B = b; } |
| 182 | void setEnd(SourceLocation e) { E = e; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 183 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 184 | bool isValid() const { return B.isValid() && E.isValid(); } |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 185 | bool isInvalid() const { return !isValid(); } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Ted Kremenek | a898283 | 2009-03-28 17:32:39 +0000 | [diff] [blame] | 187 | bool operator==(const SourceRange &X) const { |
| 188 | return B == X.B && E == X.E; |
| 189 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 190 | |
Ted Kremenek | a898283 | 2009-03-28 17:32:39 +0000 | [diff] [blame] | 191 | bool operator!=(const SourceRange &X) const { |
| 192 | return B != X.B || E != X.E; |
| 193 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 194 | }; |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 195 | |
| 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 |
| 201 | /// range. |
| 202 | class CharSourceRange { |
| 203 | SourceRange Range; |
| 204 | bool IsTokenRange; |
| 205 | public: |
| 206 | CharSourceRange() : IsTokenRange(false) {} |
| 207 | CharSourceRange(SourceRange R, bool ITR) : Range(R),IsTokenRange(ITR){} |
| 208 | |
| 209 | static CharSourceRange getTokenRange(SourceRange R) { |
| 210 | CharSourceRange Result; |
| 211 | Result.Range = R; |
| 212 | Result.IsTokenRange = true; |
| 213 | return Result; |
| 214 | } |
| 215 | |
| 216 | static CharSourceRange getCharRange(SourceRange R) { |
| 217 | CharSourceRange Result; |
| 218 | Result.Range = R; |
| 219 | Result.IsTokenRange = false; |
| 220 | return Result; |
| 221 | } |
| 222 | |
| 223 | static CharSourceRange getTokenRange(SourceLocation B, SourceLocation E) { |
| 224 | return getTokenRange(SourceRange(B, E)); |
| 225 | } |
| 226 | static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) { |
| 227 | return getCharRange(SourceRange(B, E)); |
| 228 | } |
| 229 | |
| 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; } |
| 234 | |
| 235 | SourceLocation getBegin() const { return Range.getBegin(); } |
| 236 | SourceLocation getEnd() const { return Range.getEnd(); } |
| 237 | const SourceRange &getAsRange() const { return Range; } |
| 238 | |
| 239 | void setBegin(SourceLocation b) { Range.setBegin(b); } |
| 240 | void setEnd(SourceLocation e) { Range.setEnd(e); } |
| 241 | |
| 242 | bool isValid() const { return Range.isValid(); } |
| 243 | bool isInvalid() const { return !isValid(); } |
| 244 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 245 | |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 246 | /// FullSourceLoc - A SourceLocation and its associated SourceManager. Useful |
| 247 | /// for argument passing to functions that expect both objects. |
| 248 | class FullSourceLoc : public SourceLocation { |
Chris Lattner | 5c5db4e | 2010-04-20 20:49:23 +0000 | [diff] [blame] | 249 | const SourceManager *SrcMgr; |
Ted Kremenek | a9793ed | 2007-12-12 18:16:46 +0000 | [diff] [blame] | 250 | public: |
Ted Kremenek | 3632a35 | 2009-01-28 20:46:26 +0000 | [diff] [blame] | 251 | /// Creates a FullSourceLoc where isValid() returns false. |
Chris Lattner | 5c5db4e | 2010-04-20 20:49:23 +0000 | [diff] [blame] | 252 | explicit FullSourceLoc() : SrcMgr(0) {} |
Ted Kremenek | a9793ed | 2007-12-12 18:16:46 +0000 | [diff] [blame] | 253 | |
Chris Lattner | 5c5db4e | 2010-04-20 20:49:23 +0000 | [diff] [blame] | 254 | explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM) |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 255 | : SourceLocation(Loc), SrcMgr(&SM) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 256 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 257 | const SourceManager &getManager() const { |
| 258 | assert(SrcMgr && "SourceManager is NULL."); |
Ted Kremenek | a9793ed | 2007-12-12 18:16:46 +0000 | [diff] [blame] | 259 | return *SrcMgr; |
| 260 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | |
Chris Lattner | 3b4d5e9 | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 262 | FileID getFileID() const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 264 | FullSourceLoc getInstantiationLoc() const; |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 265 | FullSourceLoc getSpellingLoc() const; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 266 | |
Douglas Gregor | 64e462d | 2010-03-16 20:53:17 +0000 | [diff] [blame] | 267 | unsigned getInstantiationLineNumber(bool *Invalid = 0) const; |
| 268 | unsigned getInstantiationColumnNumber(bool *Invalid = 0) const; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 269 | |
Douglas Gregor | 64e462d | 2010-03-16 20:53:17 +0000 | [diff] [blame] | 270 | unsigned getSpellingLineNumber(bool *Invalid = 0) const; |
| 271 | unsigned getSpellingColumnNumber(bool *Invalid = 0) const; |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 272 | |
Douglas Gregor | a543016 | 2010-03-16 20:46:42 +0000 | [diff] [blame] | 273 | const char *getCharacterData(bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 274 | |
Douglas Gregor | aae58b0 | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 275 | const llvm::MemoryBuffer* getBuffer(bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 276 | |
Benjamin Kramer | ceafc4b | 2010-03-16 14:48:07 +0000 | [diff] [blame] | 277 | /// getBufferData - Return a StringRef to the source buffer data for the |
| 278 | /// specified FileID. |
Douglas Gregor | aae58b0 | 2010-03-16 20:01:30 +0000 | [diff] [blame] | 279 | llvm::StringRef getBufferData(bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 280 | |
Ted Kremenek | 321abd4 | 2009-03-10 05:13:43 +0000 | [diff] [blame] | 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; |
| 285 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 286 | bool isInSystemHeader() const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 287 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 288 | /// \brief Determines the order of 2 source locations in the translation unit. |
| 289 | /// |
| 290 | /// \returns true if this source location comes before 'Loc', false otherwise. |
| 291 | bool isBeforeInTranslationUnitThan(SourceLocation Loc) const; |
| 292 | |
| 293 | /// \brief Determines the order of 2 source locations in the translation unit. |
| 294 | /// |
| 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); |
| 300 | } |
| 301 | |
Argyrios Kyrtzidis | 8f89652 | 2011-04-07 18:10:07 +0000 | [diff] [blame] | 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); |
| 307 | } |
| 308 | }; |
| 309 | |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 310 | /// Prints information about this FullSourceLoc to stderr. Useful for |
| 311 | /// debugging. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 312 | void dump() const { SourceLocation::dump(*SrcMgr); } |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 313 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 314 | friend inline bool |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 315 | operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) { |
| 316 | return LHS.getRawEncoding() == RHS.getRawEncoding() && |
| 317 | LHS.SrcMgr == RHS.SrcMgr; |
| 318 | } |
| 319 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 320 | friend inline bool |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 321 | operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) { |
| 322 | return !(LHS == RHS); |
| 323 | } |
| 324 | |
Ted Kremenek | a9793ed | 2007-12-12 18:16:46 +0000 | [diff] [blame] | 325 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 326 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 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. |
| 331 | /// |
| 332 | /// You can get a PresumedLoc from a SourceLocation with SourceManager. |
| 333 | class PresumedLoc { |
| 334 | const char *Filename; |
| 335 | unsigned Line, Col; |
| 336 | SourceLocation IncludeLoc; |
| 337 | public: |
| 338 | PresumedLoc() : Filename(0) {} |
| 339 | PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL) |
| 340 | : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) { |
| 341 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 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; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 349 | /// getFilename - Return the presumed filename of this location. This can be |
| 350 | /// affected by #line etc. |
| 351 | const char *getFilename() const { return Filename; } |
| 352 | |
| 353 | /// getLine - Return the presumed line number of this location. This can be |
| 354 | /// affected by #line etc. |
| 355 | unsigned getLine() const { return Line; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 356 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 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; } |
| 360 | |
| 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; } |
| 364 | }; |
| 365 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 366 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 367 | } // end namespace clang |
| 368 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 369 | namespace llvm { |
| 370 | /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and |
| 371 | /// DenseSets. |
| 372 | template <> |
| 373 | struct DenseMapInfo<clang::FileID> { |
| 374 | static inline clang::FileID getEmptyKey() { |
| 375 | return clang::FileID(); |
| 376 | } |
| 377 | static inline clang::FileID getTombstoneKey() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | return clang::FileID::getSentinel(); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 379 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 381 | static unsigned getHashValue(clang::FileID S) { |
| 382 | return S.getHashValue(); |
| 383 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 384 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 385 | static bool isEqual(clang::FileID LHS, clang::FileID RHS) { |
| 386 | return LHS == RHS; |
| 387 | } |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 388 | }; |
Chris Lattner | 06159e8 | 2009-12-15 07:26:51 +0000 | [diff] [blame] | 389 | |
| 390 | template <> |
| 391 | struct isPodLike<clang::SourceLocation> { static const bool value = true; }; |
| 392 | template <> |
| 393 | struct isPodLike<clang::FileID> { static const bool value = true; }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 395 | // Teach SmallPtrSet how to handle SourceLocation. |
| 396 | template<> |
| 397 | class PointerLikeTypeTraits<clang::SourceLocation> { |
| 398 | public: |
| 399 | static inline void *getAsVoidPointer(clang::SourceLocation L) { |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 400 | return L.getPtrEncoding(); |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 401 | } |
| 402 | static inline clang::SourceLocation getFromVoidPointer(void *P) { |
| 403 | return clang::SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)P); |
| 404 | } |
| 405 | enum { NumLowBitsAvailable = 0 }; |
| 406 | }; |
| 407 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 408 | } // end namespace llvm |
| 409 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 410 | #endif |