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 | //===----------------------------------------------------------------------===// |
James Dennett | 2f7f5b1 | 2012-07-02 07:01:42 +0000 | [diff] [blame] | 9 | /// |
| 10 | /// \file |
| 11 | /// \brief Defines the clang::SourceLocation class and associated facilities. |
| 12 | /// |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 13 | //===----------------------------------------------------------------------===// |
| 14 | |
| 15 | #ifndef LLVM_CLANG_SOURCELOCATION_H |
| 16 | #define LLVM_CLANG_SOURCELOCATION_H |
| 17 | |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 18 | #include "clang/Basic/LLVM.h" |
Argyrios Kyrtzidis | 0ee7d94 | 2012-02-21 05:04:44 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Compiler.h" |
Chandler Carruth | 30a2e16 | 2012-12-04 09:18:49 +0000 | [diff] [blame] | 20 | #include "llvm/Support/PointerLikeTypeTraits.h" |
Chris Lattner | ae50fa0 | 2009-03-05 00:00:31 +0000 | [diff] [blame] | 21 | #include <cassert> |
Chandler Carruth | 30a2e16 | 2012-12-04 09:18:49 +0000 | [diff] [blame] | 22 | #include <functional> |
Chad Rosier | b328ee5 | 2012-11-09 20:00:52 +0000 | [diff] [blame] | 23 | #include <string> |
Chandler Carruth | 30a2e16 | 2012-12-04 09:18:49 +0000 | [diff] [blame] | 24 | #include <utility> |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 25 | |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 26 | namespace llvm { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 27 | class MemoryBuffer; |
| 28 | template <typename T> struct DenseMapInfo; |
Chris Lattner | 06159e8 | 2009-12-15 07:26:51 +0000 | [diff] [blame] | 29 | template <typename T> struct isPodLike; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 30 | } |
| 31 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | namespace clang { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 33 | |
Ted Kremenek | a9793ed | 2007-12-12 18:16:46 +0000 | [diff] [blame] | 34 | class SourceManager; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 35 | |
James Dennett | 4bd495c | 2012-06-22 05:43:45 +0000 | [diff] [blame] | 36 | /// \brief An opaque identifier used by SourceManager which refers to a |
| 37 | /// source file (MemoryBuffer) along with its \#include path and \#line data. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 38 | /// |
| 39 | class FileID { |
James Dennett | af50aab | 2012-07-02 23:17:34 +0000 | [diff] [blame] | 40 | /// \brief A mostly-opaque identifier, where 0 is "invalid", >0 is |
| 41 | /// this module, and <-1 is something loaded from another module. |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 42 | int ID; |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 43 | public: |
| 44 | FileID() : ID(0) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 45 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 46 | bool isInvalid() const { return ID == 0; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 47 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 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; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 54 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 55 | static FileID getSentinel() { return get(-1); } |
| 56 | unsigned getHashValue() const { return static_cast<unsigned>(ID); } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 57 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 58 | private: |
| 59 | friend class SourceManager; |
Douglas Gregor | 31d375f | 2011-05-06 21:43:30 +0000 | [diff] [blame] | 60 | friend class ASTWriter; |
| 61 | friend class ASTReader; |
| 62 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 63 | static FileID get(int V) { |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 64 | FileID F; |
| 65 | F.ID = V; |
| 66 | return F; |
| 67 | } |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 68 | int getOpaqueValue() const { return ID; } |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 69 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 70 | |
| 71 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 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. |
| 74 | /// |
| 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 |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 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, |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 80 | /// starting at the highest possible offset and growing downwards, contains |
| 81 | /// buffers of loaded modules. |
| 82 | /// |
| 83 | /// In addition, one bit of SourceLocation is used for quick access to the |
Chandler Carruth | 3201f38 | 2011-07-26 05:17:23 +0000 | [diff] [blame] | 84 | /// information whether the location is in a file or a macro expansion. |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 85 | /// |
| 86 | /// It is important that this type remains small. It is currently 32 bits wide. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 87 | class SourceLocation { |
| 88 | unsigned ID; |
Chris Lattner | bcc2a67 | 2009-01-19 06:46:35 +0000 | [diff] [blame] | 89 | friend class SourceManager; |
Argyrios Kyrtzidis | 5a43748 | 2011-09-19 20:40:08 +0000 | [diff] [blame] | 90 | friend class ASTReader; |
| 91 | friend class ASTWriter; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 92 | enum : unsigned { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 93 | MacroIDBit = 1U << 31 |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 94 | }; |
Chris Lattner | 9ebac5e | 2009-01-19 06:57:37 +0000 | [diff] [blame] | 95 | public: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 96 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 97 | SourceLocation() : ID(0) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 98 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 99 | bool isFileID() const { return (ID & MacroIDBit) == 0; } |
| 100 | bool isMacroID() const { return (ID & MacroIDBit) != 0; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 102 | /// \brief Return true if this is a valid SourceLocation object. |
Chris Lattner | b7489d8 | 2007-11-09 23:52:16 +0000 | [diff] [blame] | 103 | /// |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 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 |
| 106 | /// option). |
Chris Lattner | b7489d8 | 2007-11-09 23:52:16 +0000 | [diff] [blame] | 107 | bool isValid() const { return ID != 0; } |
| 108 | bool isInvalid() const { return ID == 0; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 109 | |
Chris Lattner | 6fda54c | 2009-01-19 07:00:35 +0000 | [diff] [blame] | 110 | private: |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 111 | /// \brief Return the offset into the manager's global input view. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 112 | unsigned getOffset() const { |
| 113 | return ID & ~MacroIDBit; |
Chris Lattner | 4d10ef1 | 2009-01-19 06:55:08 +0000 | [diff] [blame] | 114 | } |
Chris Lattner | 6fda54c | 2009-01-19 07:00:35 +0000 | [diff] [blame] | 115 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 116 | static SourceLocation getFileLoc(unsigned ID) { |
| 117 | assert((ID & MacroIDBit) == 0 && "Ran out of source locations!"); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 118 | SourceLocation L; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 119 | L.ID = ID; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 120 | return L; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 121 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 122 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 123 | static SourceLocation getMacroLoc(unsigned ID) { |
| 124 | assert((ID & MacroIDBit) == 0 && "Ran out of source locations!"); |
Chris Lattner | d1623a8 | 2007-07-21 06:41:57 +0000 | [diff] [blame] | 125 | SourceLocation L; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 126 | L.ID = MacroIDBit | ID; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 127 | return L; |
| 128 | } |
Chris Lattner | 4d10ef1 | 2009-01-19 06:55:08 +0000 | [diff] [blame] | 129 | public: |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 130 | |
Argyrios Kyrtzidis | a64ccef | 2011-09-19 20:40:19 +0000 | [diff] [blame] | 131 | /// \brief Return a source location with the specified offset from this |
| 132 | /// SourceLocation. |
| 133 | SourceLocation getLocWithOffset(int Offset) const { |
| 134 | assert(((getOffset()+Offset) & MacroIDBit) == 0 && "offset overflow"); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 135 | SourceLocation L; |
| 136 | L.ID = ID+Offset; |
| 137 | return L; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 138 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 139 | |
James Dennett | 4bd495c | 2012-06-22 05:43:45 +0000 | [diff] [blame] | 140 | /// \brief When a SourceLocation itself cannot be used, this returns |
| 141 | /// an (opaque) 32-bit integer encoding for it. |
| 142 | /// |
| 143 | /// This should only be passed to SourceLocation::getFromRawEncoding, it |
| 144 | /// should not be inspected directly. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 145 | unsigned getRawEncoding() const { return ID; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 146 | |
James Dennett | 4bd495c | 2012-06-22 05:43:45 +0000 | [diff] [blame] | 147 | /// \brief Turn a raw encoding of a SourceLocation object into |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 148 | /// a real SourceLocation. |
James Dennett | 4bd495c | 2012-06-22 05:43:45 +0000 | [diff] [blame] | 149 | /// |
| 150 | /// \see getRawEncoding. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 151 | static SourceLocation getFromRawEncoding(unsigned Encoding) { |
| 152 | SourceLocation X; |
| 153 | X.ID = Encoding; |
| 154 | return X; |
| 155 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 156 | |
James Dennett | 4bd495c | 2012-06-22 05:43:45 +0000 | [diff] [blame] | 157 | /// \brief When a SourceLocation itself cannot be used, this returns |
| 158 | /// an (opaque) pointer encoding for it. |
| 159 | /// |
| 160 | /// This should only be passed to SourceLocation::getFromPtrEncoding, it |
| 161 | /// should not be inspected directly. |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 162 | void* getPtrEncoding() const { |
| 163 | // Double cast to avoid a warning "cast to pointer from integer of different |
| 164 | // size". |
| 165 | return (void*)(uintptr_t)getRawEncoding(); |
| 166 | } |
| 167 | |
James Dennett | 1216015 | 2013-02-16 07:45:22 +0000 | [diff] [blame] | 168 | /// \brief Turn a pointer encoding of a SourceLocation object back |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 169 | /// into a real SourceLocation. |
Jordan Rose | 4a0beb2 | 2012-07-06 22:00:04 +0000 | [diff] [blame] | 170 | static SourceLocation getFromPtrEncoding(const void *Encoding) { |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 171 | return getFromRawEncoding((unsigned)(uintptr_t)Encoding); |
| 172 | } |
| 173 | |
Chris Lattner | 8cc488f | 2011-07-20 07:06:53 +0000 | [diff] [blame] | 174 | void print(raw_ostream &OS, const SourceManager &SM) const; |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 175 | std::string printToString(const SourceManager &SM) const; |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 176 | void dump(const SourceManager &SM) const; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 177 | }; |
| 178 | |
| 179 | inline bool operator==(const SourceLocation &LHS, const SourceLocation &RHS) { |
| 180 | return LHS.getRawEncoding() == RHS.getRawEncoding(); |
| 181 | } |
| 182 | |
| 183 | inline bool operator!=(const SourceLocation &LHS, const SourceLocation &RHS) { |
| 184 | return !(LHS == RHS); |
| 185 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 6fda54c | 2009-01-19 07:00:35 +0000 | [diff] [blame] | 187 | inline bool operator<(const SourceLocation &LHS, const SourceLocation &RHS) { |
| 188 | return LHS.getRawEncoding() < RHS.getRawEncoding(); |
| 189 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 190 | |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 191 | /// \brief A trivial tuple used to represent a source range. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 192 | class SourceRange { |
| 193 | SourceLocation B; |
| 194 | SourceLocation E; |
| 195 | public: |
| 196 | SourceRange(): B(SourceLocation()), E(SourceLocation()) {} |
| 197 | SourceRange(SourceLocation loc) : B(loc), E(loc) {} |
| 198 | SourceRange(SourceLocation begin, SourceLocation end) : B(begin), E(end) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 199 | |
Chris Lattner | 311ff02 | 2007-10-16 22:36:42 +0000 | [diff] [blame] | 200 | SourceLocation getBegin() const { return B; } |
| 201 | SourceLocation getEnd() const { return E; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 202 | |
Chris Lattner | e80a59c | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 203 | void setBegin(SourceLocation b) { B = b; } |
| 204 | void setEnd(SourceLocation e) { E = e; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 205 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 206 | bool isValid() const { return B.isValid() && E.isValid(); } |
Ted Kremenek | 782f2f5 | 2010-01-07 01:20:12 +0000 | [diff] [blame] | 207 | bool isInvalid() const { return !isValid(); } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 208 | |
Ted Kremenek | a898283 | 2009-03-28 17:32:39 +0000 | [diff] [blame] | 209 | bool operator==(const SourceRange &X) const { |
| 210 | return B == X.B && E == X.E; |
| 211 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | |
Ted Kremenek | a898283 | 2009-03-28 17:32:39 +0000 | [diff] [blame] | 213 | bool operator!=(const SourceRange &X) const { |
| 214 | return B != X.B || E != X.E; |
| 215 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 216 | }; |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 217 | |
James Dennett | af50aab | 2012-07-02 23:17:34 +0000 | [diff] [blame] | 218 | /// \brief Represents a character-granular source range. |
| 219 | /// |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 220 | /// The underlying SourceRange can either specify the starting/ending character |
Michael Han | 1f19c76 | 2012-12-01 04:35:48 +0000 | [diff] [blame] | 221 | /// of the range, or it can specify the start of the range and the start of the |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 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 |
| 224 | /// range. |
| 225 | class CharSourceRange { |
| 226 | SourceRange Range; |
| 227 | bool IsTokenRange; |
| 228 | public: |
| 229 | CharSourceRange() : IsTokenRange(false) {} |
Nico Weber | 4a2ef80 | 2013-01-07 01:21:02 +0000 | [diff] [blame] | 230 | CharSourceRange(SourceRange R, bool ITR) : Range(R), IsTokenRange(ITR) {} |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 231 | |
| 232 | static CharSourceRange getTokenRange(SourceRange R) { |
Nico Weber | 4a2ef80 | 2013-01-07 01:21:02 +0000 | [diff] [blame] | 233 | return CharSourceRange(R, true); |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static CharSourceRange getCharRange(SourceRange R) { |
Nico Weber | 4a2ef80 | 2013-01-07 01:21:02 +0000 | [diff] [blame] | 237 | return CharSourceRange(R, false); |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 238 | } |
| 239 | |
| 240 | static CharSourceRange getTokenRange(SourceLocation B, SourceLocation E) { |
| 241 | return getTokenRange(SourceRange(B, E)); |
| 242 | } |
| 243 | static CharSourceRange getCharRange(SourceLocation B, SourceLocation E) { |
| 244 | return getCharRange(SourceRange(B, E)); |
| 245 | } |
| 246 | |
James Dennett | af50aab | 2012-07-02 23:17:34 +0000 | [diff] [blame] | 247 | /// \brief Return true if the end of this range specifies the start of |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 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; } |
Argyrios Kyrtzidis | a83f4d2 | 2012-02-03 05:58:29 +0000 | [diff] [blame] | 251 | bool isCharRange() const { return !IsTokenRange; } |
Chris Lattner | 0a76aae | 2010-06-18 22:45:06 +0000 | [diff] [blame] | 252 | |
| 253 | SourceLocation getBegin() const { return Range.getBegin(); } |
| 254 | SourceLocation getEnd() const { return Range.getEnd(); } |
| 255 | const SourceRange &getAsRange() const { return Range; } |
| 256 | |
| 257 | void setBegin(SourceLocation b) { Range.setBegin(b); } |
| 258 | void setEnd(SourceLocation e) { Range.setEnd(e); } |
| 259 | |
| 260 | bool isValid() const { return Range.isValid(); } |
| 261 | bool isInvalid() const { return !isValid(); } |
| 262 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 263 | |
James Dennett | af50aab | 2012-07-02 23:17:34 +0000 | [diff] [blame] | 264 | /// \brief A SourceLocation and its associated SourceManager. |
| 265 | /// |
| 266 | /// This is useful for argument passing to functions that expect both objects. |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 267 | class FullSourceLoc : public SourceLocation { |
Chris Lattner | 5c5db4e | 2010-04-20 20:49:23 +0000 | [diff] [blame] | 268 | const SourceManager *SrcMgr; |
Ted Kremenek | a9793ed | 2007-12-12 18:16:46 +0000 | [diff] [blame] | 269 | public: |
James Dennett | af50aab | 2012-07-02 23:17:34 +0000 | [diff] [blame] | 270 | /// \brief Creates a FullSourceLoc where isValid() returns \c false. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 271 | explicit FullSourceLoc() : SrcMgr(nullptr) {} |
Ted Kremenek | a9793ed | 2007-12-12 18:16:46 +0000 | [diff] [blame] | 272 | |
Chris Lattner | 5c5db4e | 2010-04-20 20:49:23 +0000 | [diff] [blame] | 273 | explicit FullSourceLoc(SourceLocation Loc, const SourceManager &SM) |
Chris Lattner | a50bd54 | 2009-01-16 23:03:56 +0000 | [diff] [blame] | 274 | : SourceLocation(Loc), SrcMgr(&SM) {} |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 275 | |
James Dennett | af50aab | 2012-07-02 23:17:34 +0000 | [diff] [blame] | 276 | /// \pre This FullSourceLoc has an associated SourceManager. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 277 | const SourceManager &getManager() const { |
| 278 | assert(SrcMgr && "SourceManager is NULL."); |
Ted Kremenek | a9793ed | 2007-12-12 18:16:46 +0000 | [diff] [blame] | 279 | return *SrcMgr; |
| 280 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | |
Chris Lattner | 3b4d5e9 | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 282 | FileID getFileID() const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 283 | |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 284 | FullSourceLoc getExpansionLoc() const; |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 285 | FullSourceLoc getSpellingLoc() const; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 286 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 287 | unsigned getExpansionLineNumber(bool *Invalid = nullptr) const; |
| 288 | unsigned getExpansionColumnNumber(bool *Invalid = nullptr) const; |
Ted Kremenek | 9c728dc | 2007-12-12 22:39:36 +0000 | [diff] [blame] | 289 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 290 | unsigned getSpellingLineNumber(bool *Invalid = nullptr) const; |
| 291 | unsigned getSpellingColumnNumber(bool *Invalid = nullptr) const; |
Chris Lattner | 5c38b63 | 2008-09-29 21:46:13 +0000 | [diff] [blame] | 292 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 293 | const char *getCharacterData(bool *Invalid = nullptr) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 294 | |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 295 | const llvm::MemoryBuffer* getBuffer(bool *Invalid = nullptr) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 296 | |
James Dennett | af50aab | 2012-07-02 23:17:34 +0000 | [diff] [blame] | 297 | /// \brief Return a StringRef to the source buffer data for the |
Benjamin Kramer | ceafc4b | 2010-03-16 14:48:07 +0000 | [diff] [blame] | 298 | /// specified FileID. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 299 | StringRef getBufferData(bool *Invalid = nullptr) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 300 | |
James Dennett | af50aab | 2012-07-02 23:17:34 +0000 | [diff] [blame] | 301 | /// \brief Decompose the specified location into a raw FileID + Offset pair. |
| 302 | /// |
| 303 | /// The first element is the FileID, the second is the offset from the |
| 304 | /// start of the buffer of the location. |
Ted Kremenek | 321abd4 | 2009-03-10 05:13:43 +0000 | [diff] [blame] | 305 | std::pair<FileID, unsigned> getDecomposedLoc() const; |
| 306 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 307 | bool isInSystemHeader() const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 308 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 309 | /// \brief Determines the order of 2 source locations in the translation unit. |
| 310 | /// |
| 311 | /// \returns true if this source location comes before 'Loc', false otherwise. |
| 312 | bool isBeforeInTranslationUnitThan(SourceLocation Loc) const; |
| 313 | |
| 314 | /// \brief Determines the order of 2 source locations in the translation unit. |
| 315 | /// |
| 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); |
| 321 | } |
| 322 | |
Argyrios Kyrtzidis | 8f89652 | 2011-04-07 18:10:07 +0000 | [diff] [blame] | 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); |
| 328 | } |
| 329 | }; |
| 330 | |
James Dennett | af50aab | 2012-07-02 23:17:34 +0000 | [diff] [blame] | 331 | /// \brief Prints information about this FullSourceLoc to stderr. |
| 332 | /// |
| 333 | /// This is useful for debugging. |
Stephen Hines | 651f13c | 2014-04-23 16:59:28 -0700 | [diff] [blame] | 334 | void dump() const; |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 335 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 336 | friend inline bool |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 337 | operator==(const FullSourceLoc &LHS, const FullSourceLoc &RHS) { |
| 338 | return LHS.getRawEncoding() == RHS.getRawEncoding() && |
| 339 | LHS.SrcMgr == RHS.SrcMgr; |
| 340 | } |
| 341 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 342 | friend inline bool |
Douglas Gregor | 0b7a158 | 2009-01-17 00:42:38 +0000 | [diff] [blame] | 343 | operator!=(const FullSourceLoc &LHS, const FullSourceLoc &RHS) { |
| 344 | return !(LHS == RHS); |
| 345 | } |
| 346 | |
Ted Kremenek | a9793ed | 2007-12-12 18:16:46 +0000 | [diff] [blame] | 347 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 348 | |
James Dennett | af50aab | 2012-07-02 23:17:34 +0000 | [diff] [blame] | 349 | /// \brief Represents an unpacked "presumed" location which can be presented |
| 350 | /// to the user. |
| 351 | /// |
| 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. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 354 | /// |
| 355 | /// You can get a PresumedLoc from a SourceLocation with SourceManager. |
| 356 | class PresumedLoc { |
| 357 | const char *Filename; |
| 358 | unsigned Line, Col; |
| 359 | SourceLocation IncludeLoc; |
| 360 | public: |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 361 | PresumedLoc() : Filename(nullptr) {} |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 362 | PresumedLoc(const char *FN, unsigned Ln, unsigned Co, SourceLocation IL) |
| 363 | : Filename(FN), Line(Ln), Col(Co), IncludeLoc(IL) { |
| 364 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 365 | |
James Dennett | af50aab | 2012-07-02 23:17:34 +0000 | [diff] [blame] | 366 | /// \brief Return true if this object is invalid or uninitialized. |
| 367 | /// |
| 368 | /// This occurs when created with invalid source locations or when walking |
| 369 | /// off the top of a \#include stack. |
Stephen Hines | 6bcf27b | 2014-05-29 04:14:42 -0700 | [diff] [blame^] | 370 | bool isInvalid() const { return Filename == nullptr; } |
| 371 | bool isValid() const { return Filename != nullptr; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 372 | |
James Dennett | 4bd495c | 2012-06-22 05:43:45 +0000 | [diff] [blame] | 373 | /// \brief Return the presumed filename of this location. |
| 374 | /// |
| 375 | /// This can be affected by \#line etc. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 376 | const char *getFilename() const { return Filename; } |
| 377 | |
James Dennett | 4bd495c | 2012-06-22 05:43:45 +0000 | [diff] [blame] | 378 | /// \brief Return the presumed line number of this location. |
| 379 | /// |
| 380 | /// This can be affected by \#line etc. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 381 | unsigned getLine() const { return Line; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 382 | |
James Dennett | 4bd495c | 2012-06-22 05:43:45 +0000 | [diff] [blame] | 383 | /// \brief Return the presumed column number of this location. |
| 384 | /// |
| 385 | /// This cannot be affected by \#line, but is packaged here for convenience. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 386 | unsigned getColumn() const { return Col; } |
| 387 | |
James Dennett | 4bd495c | 2012-06-22 05:43:45 +0000 | [diff] [blame] | 388 | /// \brief Return the presumed include location of this location. |
| 389 | /// |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 390 | /// This can be affected by GNU linemarker directives. |
| 391 | SourceLocation getIncludeLoc() const { return IncludeLoc; } |
| 392 | }; |
| 393 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 394 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 395 | } // end namespace clang |
| 396 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 397 | namespace llvm { |
| 398 | /// Define DenseMapInfo so that FileID's can be used as keys in DenseMap and |
| 399 | /// DenseSets. |
| 400 | template <> |
| 401 | struct DenseMapInfo<clang::FileID> { |
| 402 | static inline clang::FileID getEmptyKey() { |
| 403 | return clang::FileID(); |
| 404 | } |
| 405 | static inline clang::FileID getTombstoneKey() { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 406 | return clang::FileID::getSentinel(); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 407 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 408 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 409 | static unsigned getHashValue(clang::FileID S) { |
| 410 | return S.getHashValue(); |
| 411 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 412 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 413 | static bool isEqual(clang::FileID LHS, clang::FileID RHS) { |
| 414 | return LHS == RHS; |
| 415 | } |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 416 | }; |
Chris Lattner | 06159e8 | 2009-12-15 07:26:51 +0000 | [diff] [blame] | 417 | |
| 418 | template <> |
| 419 | struct isPodLike<clang::SourceLocation> { static const bool value = true; }; |
| 420 | template <> |
| 421 | struct isPodLike<clang::FileID> { static const bool value = true; }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 423 | // Teach SmallPtrSet how to handle SourceLocation. |
| 424 | template<> |
| 425 | class PointerLikeTypeTraits<clang::SourceLocation> { |
| 426 | public: |
| 427 | static inline void *getAsVoidPointer(clang::SourceLocation L) { |
Jeffrey Yasskin | dec0984 | 2011-01-18 02:00:16 +0000 | [diff] [blame] | 428 | return L.getPtrEncoding(); |
Argyrios Kyrtzidis | 0827408 | 2010-12-15 18:44:22 +0000 | [diff] [blame] | 429 | } |
| 430 | static inline clang::SourceLocation getFromVoidPointer(void *P) { |
| 431 | return clang::SourceLocation::getFromRawEncoding((unsigned)(uintptr_t)P); |
| 432 | } |
| 433 | enum { NumLowBitsAvailable = 0 }; |
| 434 | }; |
| 435 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 436 | } // end namespace llvm |
| 437 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 438 | #endif |