Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1 | //===--- SourceManager.h - Track and cache 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 SourceManager interface. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_CLANG_SOURCEMANAGER_H |
| 15 | #define LLVM_CLANG_SOURCEMANAGER_H |
| 16 | |
Chris Lattner | d47d3b0 | 2011-07-23 10:35:09 +0000 | [diff] [blame] | 17 | #include "clang/Basic/LLVM.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 18 | #include "clang/Basic/SourceLocation.h" |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 19 | #include "llvm/Support/Allocator.h" |
Michael J. Spencer | 03013fa | 2010-11-29 18:12:39 +0000 | [diff] [blame] | 20 | #include "llvm/Support/DataTypes.h" |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/PointerIntPair.h" |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/PointerUnion.h" |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 23 | #include "llvm/ADT/IntrusiveRefCntPtr.h" |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 24 | #include "llvm/ADT/DenseMap.h" |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 25 | #include "llvm/Support/MemoryBuffer.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 26 | #include <vector> |
Chris Lattner | 9dc62f0 | 2007-07-12 15:32:57 +0000 | [diff] [blame] | 27 | #include <cassert> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 28 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | namespace clang { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 | |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 31 | class Diagnostic; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | class SourceManager; |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 33 | class FileManager; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | class FileEntry; |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 35 | class LineTableInfo; |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 36 | class LangOptions; |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 37 | |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 38 | /// SrcMgr - Public enums and private classes that are part of the |
| 39 | /// SourceManager implementation. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 40 | /// |
| 41 | namespace SrcMgr { |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 42 | /// CharacteristicKind - This is used to represent whether a file or directory |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 43 | /// holds normal user code, system code, or system code which is implicitly |
| 44 | /// 'extern "C"' in C++ mode. Entire directories can be tagged with this |
| 45 | /// (this is maintained by DirectoryLookup and friends) as can specific |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 46 | /// FileInfos when a #pragma system_header is seen or various other cases. |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 47 | /// |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 48 | enum CharacteristicKind { |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 49 | C_User, C_System, C_ExternCSystem |
| 50 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 51 | |
Dan Gohman | 4710a8e | 2010-08-25 21:59:25 +0000 | [diff] [blame] | 52 | /// ContentCache - One instance of this struct is kept for every file |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 53 | /// loaded or used. This object owns the MemoryBuffer object. |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 54 | class ContentCache { |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 55 | enum CCFlags { |
| 56 | /// \brief Whether the buffer is invalid. |
| 57 | InvalidFlag = 0x01, |
| 58 | /// \brief Whether the buffer should not be freed on destruction. |
| 59 | DoNotFreeFlag = 0x02 |
| 60 | }; |
| 61 | |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 62 | /// Buffer - The actual buffer containing the characters from the input |
| 63 | /// file. This is owned by the ContentCache object. |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 64 | /// The bits indicate indicates whether the buffer is invalid. |
| 65 | mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer; |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 66 | |
| 67 | public: |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 68 | /// Reference to the file entry representing this ContentCache. |
| 69 | /// This reference does not own the FileEntry object. |
| 70 | /// It is possible for this to be NULL if |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 71 | /// the ContentCache encapsulates an imaginary text buffer. |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 72 | const FileEntry *OrigEntry; |
| 73 | |
| 74 | /// \brief References the file which the contents were actually loaded from. |
| 75 | /// Can be different from 'Entry' if we overridden the contents of one file |
| 76 | /// with the contents of another file. |
| 77 | const FileEntry *ContentsEntry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 79 | /// SourceLineCache - A bump pointer allocated array of offsets for each |
| 80 | /// source line. This is lazily computed. This is owned by the |
| 81 | /// SourceManager BumpPointerAllocator object. |
Chris Lattner | 0581659 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 82 | unsigned *SourceLineCache; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 83 | |
Ted Kremenek | b6427f8 | 2007-12-04 18:59:28 +0000 | [diff] [blame] | 84 | /// NumLines - The number of lines in this ContentCache. This is only valid |
| 85 | /// if SourceLineCache is non-null. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 86 | unsigned NumLines; |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 87 | |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 88 | /// getBuffer - Returns the memory buffer for the associated content. |
| 89 | /// |
Jonathan D. Turner | a92d7e7 | 2011-06-16 20:47:21 +0000 | [diff] [blame] | 90 | /// \param Diag Object through which diagnostics will be emitted if the |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 91 | /// buffer cannot be retrieved. |
| 92 | /// |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 93 | /// \param Loc If specified, is the location that invalid file diagnostics |
| 94 | /// will be emitted at. |
| 95 | /// |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 96 | /// \param Invalid If non-NULL, will be set \c true if an error occurred. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 97 | const llvm::MemoryBuffer *getBuffer(Diagnostic &Diag, |
| 98 | const SourceManager &SM, |
| 99 | SourceLocation Loc = SourceLocation(), |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 100 | bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 101 | |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 102 | /// getSize - Returns the size of the content encapsulated by this |
| 103 | /// ContentCache. This can be the size of the source file or the size of an |
| 104 | /// arbitrary scratch buffer. If the ContentCache encapsulates a source |
| 105 | /// file this size is retrieved from the file's FileEntry. |
| 106 | unsigned getSize() const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 108 | /// getSizeBytesMapped - Returns the number of bytes actually mapped for |
| 109 | /// this ContentCache. This can be 0 if the MemBuffer was not actually |
| 110 | /// instantiated. |
| 111 | unsigned getSizeBytesMapped() const; |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 112 | |
| 113 | /// Returns the kind of memory used to back the memory buffer for |
| 114 | /// this content cache. This is used for performance analysis. |
| 115 | llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 116 | |
Chris Lattner | 0581659 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 117 | void setBuffer(const llvm::MemoryBuffer *B) { |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 118 | assert(!Buffer.getPointer() && "MemoryBuffer already set."); |
| 119 | Buffer.setPointer(B); |
| 120 | Buffer.setInt(false); |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 121 | } |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 122 | |
| 123 | /// \brief Get the underlying buffer, returning NULL if the buffer is not |
| 124 | /// yet available. |
| 125 | const llvm::MemoryBuffer *getRawBuffer() const { |
| 126 | return Buffer.getPointer(); |
| 127 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 128 | |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 129 | /// \brief Replace the existing buffer (which will be deleted) |
| 130 | /// with the given buffer. |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 131 | void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 132 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 133 | /// \brief Determine whether the buffer itself is invalid. |
| 134 | bool isBufferInvalid() const { |
| 135 | return Buffer.getInt() & InvalidFlag; |
| 136 | } |
| 137 | |
| 138 | /// \brief Determine whether the buffer should be freed. |
| 139 | bool shouldFreeBuffer() const { |
| 140 | return (Buffer.getInt() & DoNotFreeFlag) == 0; |
| 141 | } |
| 142 | |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 143 | ContentCache(const FileEntry *Ent = 0) |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 144 | : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent), |
| 145 | SourceLineCache(0), NumLines(0) {} |
| 146 | |
| 147 | ContentCache(const FileEntry *Ent, const FileEntry *contentEnt) |
| 148 | : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt), |
| 149 | SourceLineCache(0), NumLines(0) {} |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 150 | |
| 151 | ~ContentCache(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 152 | |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 153 | /// The copy ctor does not allow copies where source object has either |
| 154 | /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory |
Chris Lattner | fc8f0e1 | 2011-04-15 05:22:18 +0000 | [diff] [blame] | 155 | /// is not transferred, so this is a logical error. |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 156 | ContentCache(const ContentCache &RHS) |
| 157 | : Buffer(0, false), SourceLineCache(0) |
| 158 | { |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 159 | OrigEntry = RHS.OrigEntry; |
| 160 | ContentsEntry = RHS.ContentsEntry; |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 161 | |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 162 | assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0 |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 163 | && "Passed ContentCache object cannot own a buffer."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 164 | |
| 165 | NumLines = RHS.NumLines; |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 166 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 167 | |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 168 | private: |
| 169 | // Disable assignments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 170 | ContentCache &operator=(const ContentCache& RHS); |
| 171 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 173 | /// FileInfo - Information about a FileID, basically just the logical file |
| 174 | /// that it represents and include stack information. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 175 | /// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 176 | /// Each FileInfo has include stack information, indicating where it came |
| 177 | /// from. This information encodes the #include chain that a token was |
| 178 | /// instantiated from. The main include file has an invalid IncludeLoc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 179 | /// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 180 | /// FileInfos contain a "ContentCache *", with the contents of the file. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 181 | /// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 182 | class FileInfo { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 183 | /// IncludeLoc - The location of the #include that brought in this file. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 184 | /// This is an invalid SLOC for the main file (top of the #include chain). |
| 185 | unsigned IncludeLoc; // Really a SourceLocation |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 186 | |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 187 | /// Data - This contains the ContentCache* and the bits indicating the |
| 188 | /// characteristic of the file and whether it has #line info, all bitmangled |
| 189 | /// together. |
| 190 | uintptr_t Data; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 191 | public: |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 192 | /// get - Return a FileInfo object. |
| 193 | static FileInfo get(SourceLocation IL, const ContentCache *Con, |
| 194 | CharacteristicKind FileCharacter) { |
| 195 | FileInfo X; |
| 196 | X.IncludeLoc = IL.getRawEncoding(); |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 197 | X.Data = (uintptr_t)Con; |
Chris Lattner | 00282d6 | 2009-02-03 07:41:46 +0000 | [diff] [blame] | 198 | assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned"); |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 199 | assert((unsigned)FileCharacter < 4 && "invalid file character"); |
| 200 | X.Data |= (unsigned)FileCharacter; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 201 | return X; |
| 202 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 203 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 204 | SourceLocation getIncludeLoc() const { |
| 205 | return SourceLocation::getFromRawEncoding(IncludeLoc); |
| 206 | } |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 207 | const ContentCache* getContentCache() const { |
Chris Lattner | 00282d6 | 2009-02-03 07:41:46 +0000 | [diff] [blame] | 208 | return reinterpret_cast<const ContentCache*>(Data & ~7UL); |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 209 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 210 | |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 211 | /// getCharacteristic - Return whether this is a system header or not. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 212 | CharacteristicKind getFileCharacteristic() const { |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 213 | return (CharacteristicKind)(Data & 3); |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 214 | } |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 215 | |
| 216 | /// hasLineDirectives - Return true if this FileID has #line directives in |
| 217 | /// it. |
| 218 | bool hasLineDirectives() const { return (Data & 4) != 0; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 219 | |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 220 | /// setHasLineDirectives - Set the flag that indicates that this FileID has |
| 221 | /// line table entries associated with it. |
| 222 | void setHasLineDirectives() { |
| 223 | Data |= 4; |
| 224 | } |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 225 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 226 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 227 | /// InstantiationInfo - Each InstantiationInfo encodes the Instantiation |
| 228 | /// location - where the token was ultimately instantiated, and the |
| 229 | /// SpellingLoc - where the actual character data for the token came from. |
| 230 | class InstantiationInfo { |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 231 | // Really these are all SourceLocations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 232 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 233 | /// SpellingLoc - Where the spelling for the token can be found. |
| 234 | unsigned SpellingLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 235 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 236 | /// InstantiationLocStart/InstantiationLocEnd - In a macro expansion, these |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 237 | /// indicate the start and end of the instantiation. In object-like macros, |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 238 | /// these will be the same. In a function-like macro instantiation, the |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 239 | /// start will be the identifier and the end will be the ')'. Finally, in |
| 240 | /// macro-argument instantitions, the end will be 'SourceLocation()', an |
| 241 | /// invalid location. |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 242 | unsigned InstantiationLocStart, InstantiationLocEnd; |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 243 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 244 | public: |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 245 | SourceLocation getSpellingLoc() const { |
| 246 | return SourceLocation::getFromRawEncoding(SpellingLoc); |
| 247 | } |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 248 | SourceLocation getInstantiationLocStart() const { |
| 249 | return SourceLocation::getFromRawEncoding(InstantiationLocStart); |
| 250 | } |
| 251 | SourceLocation getInstantiationLocEnd() const { |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 252 | SourceLocation EndLoc = |
| 253 | SourceLocation::getFromRawEncoding(InstantiationLocEnd); |
| 254 | return EndLoc.isInvalid() ? getInstantiationLocStart() : EndLoc; |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 255 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 256 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 257 | std::pair<SourceLocation,SourceLocation> getInstantiationLocRange() const { |
| 258 | return std::make_pair(getInstantiationLocStart(), |
| 259 | getInstantiationLocEnd()); |
| 260 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 261 | |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 262 | bool isMacroArgInstantiation() const { |
| 263 | // Note that this needs to return false for default constructed objects. |
| 264 | return getInstantiationLocStart().isValid() && |
| 265 | SourceLocation::getFromRawEncoding(InstantiationLocEnd).isInvalid(); |
| 266 | } |
| 267 | |
| 268 | /// create - Return a InstantiationInfo for an expansion. ILStart and |
| 269 | /// ILEnd specify the instantiation range (where the macro is expanded), |
| 270 | /// and SL specifies the spelling location (where the characters from the |
| 271 | /// token come from). All three can refer to normal File SLocs or |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 272 | /// instantiation locations. |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 273 | static InstantiationInfo create(SourceLocation SL, |
| 274 | SourceLocation ILStart, |
| 275 | SourceLocation ILEnd) { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 276 | InstantiationInfo X; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 277 | X.SpellingLoc = SL.getRawEncoding(); |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 278 | X.InstantiationLocStart = ILStart.getRawEncoding(); |
| 279 | X.InstantiationLocEnd = ILEnd.getRawEncoding(); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 280 | return X; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 281 | } |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 282 | |
| 283 | /// createForMacroArg - Return a special InstantiationInfo for the |
| 284 | /// expansion of a macro argument into a function-like macro's body. IL |
| 285 | /// specifies the instantiation location (where the macro is expanded). |
| 286 | /// This doesn't need to be a range because a macro is always instantiated |
| 287 | /// at a macro parameter reference, and macro parameters are always exactly |
| 288 | /// one token. SL specifies the spelling location (where the characters |
| 289 | /// from the token come from). IL and SL can both refer to normal File |
| 290 | /// SLocs or instantiation locations. |
| 291 | /// |
| 292 | /// Given the code: |
| 293 | /// \code |
| 294 | /// #define F(x) f(x) |
| 295 | /// F(42); |
| 296 | /// \endcode |
| 297 | /// |
| 298 | /// When expanding '\c F(42)', the '\c x' would call this with an SL |
| 299 | /// pointing at '\c 42' anad an IL pointing at its location in the |
| 300 | /// definition of '\c F'. |
| 301 | static InstantiationInfo createForMacroArg(SourceLocation SL, |
| 302 | SourceLocation IL) { |
| 303 | // We store an intentionally invalid source location for the end of the |
| 304 | // instantiation range to mark that this is a macro argument instantation |
| 305 | // rather than a normal one. |
| 306 | return create(SL, IL, SourceLocation()); |
| 307 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 308 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 309 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 310 | /// SLocEntry - This is a discriminated union of FileInfo and |
| 311 | /// InstantiationInfo. SourceManager keeps an array of these objects, and |
| 312 | /// they are uniquely identified by the FileID datatype. |
| 313 | class SLocEntry { |
| 314 | unsigned Offset; // low bit is set for instantiation info. |
| 315 | union { |
| 316 | FileInfo File; |
| 317 | InstantiationInfo Instantiation; |
| 318 | }; |
| 319 | public: |
| 320 | unsigned getOffset() const { return Offset >> 1; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 321 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 322 | bool isInstantiation() const { return Offset & 1; } |
| 323 | bool isFile() const { return !isInstantiation(); } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 324 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 325 | const FileInfo &getFile() const { |
| 326 | assert(isFile() && "Not a file SLocEntry!"); |
| 327 | return File; |
| 328 | } |
| 329 | |
| 330 | const InstantiationInfo &getInstantiation() const { |
| 331 | assert(isInstantiation() && "Not an instantiation SLocEntry!"); |
| 332 | return Instantiation; |
| 333 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 334 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 335 | static SLocEntry get(unsigned Offset, const FileInfo &FI) { |
| 336 | SLocEntry E; |
| 337 | E.Offset = Offset << 1; |
| 338 | E.File = FI; |
| 339 | return E; |
| 340 | } |
| 341 | |
| 342 | static SLocEntry get(unsigned Offset, const InstantiationInfo &II) { |
| 343 | SLocEntry E; |
| 344 | E.Offset = (Offset << 1) | 1; |
| 345 | E.Instantiation = II; |
| 346 | return E; |
| 347 | } |
| 348 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 349 | } // end SrcMgr namespace. |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 350 | |
| 351 | /// \brief External source of source location entries. |
| 352 | class ExternalSLocEntrySource { |
| 353 | public: |
| 354 | virtual ~ExternalSLocEntrySource(); |
| 355 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 356 | /// \brief Read the source location entry with index ID, which will always be |
| 357 | /// less than -1. |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 358 | /// |
| 359 | /// \returns true if an error occurred that prevented the source-location |
| 360 | /// entry from being loaded. |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 361 | virtual bool ReadSLocEntry(int ID) = 0; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 362 | }; |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 363 | |
| 364 | |
| 365 | /// IsBeforeInTranslationUnitCache - This class holds the cache used by |
| 366 | /// isBeforeInTranslationUnit. The cache structure is complex enough to be |
| 367 | /// worth breaking out of SourceManager. |
| 368 | class IsBeforeInTranslationUnitCache { |
| 369 | /// L/R QueryFID - These are the FID's of the cached query. If these match up |
| 370 | /// with a subsequent query, the result can be reused. |
| 371 | FileID LQueryFID, RQueryFID; |
| 372 | |
| 373 | /// CommonFID - This is the file found in common between the two #include |
| 374 | /// traces. It is the nearest common ancestor of the #include tree. |
| 375 | FileID CommonFID; |
| 376 | |
| 377 | /// L/R CommonOffset - This is the offset of the previous query in CommonFID. |
| 378 | /// Usually, this represents the location of the #include for QueryFID, but if |
| 379 | /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a |
| 380 | /// random token in the parent. |
| 381 | unsigned LCommonOffset, RCommonOffset; |
| 382 | public: |
| 383 | |
| 384 | /// isCacheValid - Return true if the currently cached values match up with |
| 385 | /// the specified LHS/RHS query. If not, we can't use the cache. |
| 386 | bool isCacheValid(FileID LHS, FileID RHS) const { |
| 387 | return LQueryFID == LHS && RQueryFID == RHS; |
| 388 | } |
| 389 | |
| 390 | /// getCachedResult - If the cache is valid, compute the result given the |
| 391 | /// specified offsets in the LHS/RHS FID's. |
| 392 | bool getCachedResult(unsigned LOffset, unsigned ROffset) const { |
| 393 | // If one of the query files is the common file, use the offset. Otherwise, |
| 394 | // use the #include loc in the common file. |
| 395 | if (LQueryFID != CommonFID) LOffset = LCommonOffset; |
| 396 | if (RQueryFID != CommonFID) ROffset = RCommonOffset; |
| 397 | return LOffset < ROffset; |
| 398 | } |
| 399 | |
| 400 | // Set up a new query. |
| 401 | void setQueryFIDs(FileID LHS, FileID RHS) { |
| 402 | LQueryFID = LHS; |
| 403 | RQueryFID = RHS; |
| 404 | } |
| 405 | |
| 406 | void setCommonLoc(FileID commonFID, unsigned lCommonOffset, |
| 407 | unsigned rCommonOffset) { |
| 408 | CommonFID = commonFID; |
| 409 | LCommonOffset = lCommonOffset; |
| 410 | RCommonOffset = rCommonOffset; |
| 411 | } |
| 412 | |
| 413 | }; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 414 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 415 | /// \brief This class handles loading and caching of source files into memory. |
| 416 | /// |
| 417 | /// This object owns the MemoryBuffer objects for all of the loaded |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 418 | /// files and assigns unique FileID's for each unique #include chain. |
| 419 | /// |
| 420 | /// The SourceManager can be queried for information about SourceLocation |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 421 | /// objects, turning them into either spelling or instantiation locations. |
| 422 | /// Spelling locations represent where the bytes corresponding to a token came |
| 423 | /// from and instantiation locations represent where the location is in the |
| 424 | /// user's view. In the case of a macro expansion, for example, the spelling |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 425 | /// location indicates where the expanded token came from and the instantiation |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 426 | /// location specifies where it was expanded. |
Ted Kremenek | 4f32786 | 2011-03-21 18:40:17 +0000 | [diff] [blame] | 427 | class SourceManager : public llvm::RefCountedBase<SourceManager> { |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 428 | /// \brief Diagnostic object. |
| 429 | Diagnostic &Diag; |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 430 | |
| 431 | FileManager &FileMgr; |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 432 | |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 433 | mutable llvm::BumpPtrAllocator ContentCacheAlloc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 434 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 435 | /// FileInfos - Memoized information about all of the files tracked by this |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 436 | /// SourceManager. This set allows us to merge ContentCache entries based |
| 437 | /// on their FileEntry*. All ContentCache objects will thus have unique, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | /// non-null, FileEntry pointers. |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 439 | llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 440 | |
Argyrios Kyrtzidis | 299a4a9 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 441 | /// \brief True if the ContentCache for files that are overriden by other |
| 442 | /// files, should report the original file name. Defaults to true. |
| 443 | bool OverridenFilesKeepOriginalName; |
| 444 | |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 445 | /// \brief Files that have been overriden with the contents from another file. |
| 446 | llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles; |
| 447 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 448 | /// MemBufferInfos - Information about various memory buffers that we have |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 449 | /// read in. All FileEntry* within the stored ContentCache objects are NULL, |
| 450 | /// as they do not refer to a file. |
| 451 | std::vector<SrcMgr::ContentCache*> MemBufferInfos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 452 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 453 | /// \brief The table of SLocEntries that are local to this module. |
| 454 | /// |
| 455 | /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid |
| 456 | /// instantiation. |
| 457 | std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 458 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 459 | /// \brief The table of SLocEntries that are loaded from other modules. |
| 460 | /// |
| 461 | /// Negative FileIDs are indexes into this table. To get from ID to an index, |
| 462 | /// use (-ID - 2). |
| 463 | std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable; |
| 464 | |
| 465 | /// \brief The starting offset of the next local SLocEntry. |
| 466 | /// |
| 467 | /// This is LocalSLocEntryTable.back().Offset + the size of that entry. |
| 468 | unsigned NextLocalOffset; |
| 469 | |
| 470 | /// \brief The starting offset of the latest batch of loaded SLocEntries. |
| 471 | /// |
| 472 | /// This is LoadedSLocEntryTable.back().Offset, except that that entry might |
| 473 | /// not have been loaded, so that value would be unknown. |
| 474 | unsigned CurrentLoadedOffset; |
| 475 | |
| 476 | /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable |
| 477 | /// have already been loaded from the external source. |
| 478 | /// |
| 479 | /// Same indexing as LoadedSLocEntryTable. |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 480 | std::vector<bool> SLocEntryLoaded; |
| 481 | |
| 482 | /// \brief An external source for source location entries. |
| 483 | ExternalSLocEntrySource *ExternalSLocEntries; |
| 484 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 485 | /// LastFileIDLookup - This is a one-entry cache to speed up getFileID. |
| 486 | /// LastFileIDLookup records the last FileID looked up or created, because it |
| 487 | /// is very common to look up many tokens from the same file. |
| 488 | mutable FileID LastFileIDLookup; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 489 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 490 | /// LineTable - This holds information for #line directives. It is referenced |
| 491 | /// by indices from SLocEntryTable. |
| 492 | LineTableInfo *LineTable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 493 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 494 | /// LastLineNo - These ivars serve as a cache used in the getLineNumber |
| 495 | /// method which is used to speedup getLineNumber calls to nearby locations. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 496 | mutable FileID LastLineNoFileIDQuery; |
Chris Lattner | f812a45 | 2008-11-18 06:51:15 +0000 | [diff] [blame] | 497 | mutable SrcMgr::ContentCache *LastLineNoContentCache; |
| 498 | mutable unsigned LastLineNoFilePos; |
| 499 | mutable unsigned LastLineNoResult; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 500 | |
Ted Kremenek | 76edd0e | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 501 | /// MainFileID - The file ID for the main source file of the translation unit. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 502 | FileID MainFileID; |
Steve Naroff | 49c1f4a | 2008-02-02 00:10:46 +0000 | [diff] [blame] | 503 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 504 | // Statistics for -print-stats. |
| 505 | mutable unsigned NumLinearScans, NumBinaryProbes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 506 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 507 | // Cache results for the isBeforeInTranslationUnit method. |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 508 | mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 509 | |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 510 | // Cache for the "fake" buffer used for error-recovery purposes. |
| 511 | mutable llvm::MemoryBuffer *FakeBufferForRecovery; |
| 512 | |
Steve Naroff | 49c1f4a | 2008-02-02 00:10:46 +0000 | [diff] [blame] | 513 | // SourceManager doesn't support copy construction. |
| 514 | explicit SourceManager(const SourceManager&); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 515 | void operator=(const SourceManager&); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 516 | public: |
Chris Lattner | 39b49bc | 2010-11-23 08:35:12 +0000 | [diff] [blame] | 517 | SourceManager(Diagnostic &Diag, FileManager &FileMgr); |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 518 | ~SourceManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 519 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 520 | void clearIDTables(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 521 | |
Argyrios Kyrtzidis | 78a916e | 2010-09-22 14:32:24 +0000 | [diff] [blame] | 522 | Diagnostic &getDiagnostics() const { return Diag; } |
| 523 | |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 524 | FileManager &getFileManager() const { return FileMgr; } |
Argyrios Kyrtzidis | 389db16 | 2010-11-03 22:45:23 +0000 | [diff] [blame] | 525 | |
Argyrios Kyrtzidis | 299a4a9 | 2011-03-08 23:35:24 +0000 | [diff] [blame] | 526 | /// \brief Set true if the SourceManager should report the original file name |
| 527 | /// for contents of files that were overriden by other files.Defaults to true. |
| 528 | void setOverridenFilesKeepOriginalName(bool value) { |
| 529 | OverridenFilesKeepOriginalName = value; |
| 530 | } |
| 531 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 532 | /// createMainFileIDForMembuffer - Create the FileID for a memory buffer |
| 533 | /// that will represent the FileID for the main source. One example |
| 534 | /// of when this would be used is when the main source is read from STDIN. |
| 535 | FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) { |
| 536 | assert(MainFileID.isInvalid() && "MainFileID already set!"); |
| 537 | MainFileID = createFileIDForMemBuffer(Buffer); |
| 538 | return MainFileID; |
| 539 | } |
| 540 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 541 | //===--------------------------------------------------------------------===// |
| 542 | // MainFileID creation and querying methods. |
| 543 | //===--------------------------------------------------------------------===// |
| 544 | |
Ted Kremenek | 76edd0e | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 545 | /// getMainFileID - Returns the FileID of the main source file. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 546 | FileID getMainFileID() const { return MainFileID; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 547 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 548 | /// createMainFileID - Create the FileID for the main source file. |
Dan Gohman | f155dfa | 2010-08-27 15:44:11 +0000 | [diff] [blame] | 549 | FileID createMainFileID(const FileEntry *SourceFile) { |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 550 | assert(MainFileID.isInvalid() && "MainFileID already set!"); |
Dan Gohman | f155dfa | 2010-08-27 15:44:11 +0000 | [diff] [blame] | 551 | MainFileID = createFileID(SourceFile, SourceLocation(), SrcMgr::C_User); |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 552 | return MainFileID; |
| 553 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | |
Douglas Gregor | 414cb64 | 2010-11-30 05:23:00 +0000 | [diff] [blame] | 555 | /// \brief Set the file ID for the precompiled preamble, which is also the |
| 556 | /// main file. |
| 557 | void SetPreambleFileID(FileID Preamble) { |
| 558 | assert(MainFileID.isInvalid() && "MainFileID already set!"); |
| 559 | MainFileID = Preamble; |
| 560 | } |
| 561 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 562 | //===--------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 563 | // Methods to create new FileID's and instantiations. |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 564 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 565 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 566 | /// createFileID - Create a new FileID that represents the specified file |
Peter Collingbourne | d57b7ff | 2011-06-30 16:41:03 +0000 | [diff] [blame] | 567 | /// being #included from the specified IncludePosition. This translates NULL |
| 568 | /// into standard input. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 569 | FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 570 | SrcMgr::CharacteristicKind FileCharacter, |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 571 | int LoadedID = 0, unsigned LoadedOffset = 0) { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 572 | const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile); |
Dan Gohman | 0d06e99 | 2010-10-26 20:47:28 +0000 | [diff] [blame] | 573 | assert(IR && "getOrCreateContentCache() cannot return NULL"); |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 574 | return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 575 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 576 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 577 | /// createFileIDForMemBuffer - Create a new FileID that represents the |
| 578 | /// specified memory buffer. This does no caching of the buffer and takes |
| 579 | /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once. |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 580 | FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer, |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 581 | int LoadedID = 0, unsigned LoadedOffset = 0) { |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 582 | return createFileID(createMemBufferContentCache(Buffer), SourceLocation(), |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 583 | SrcMgr::C_User, LoadedID, LoadedOffset); |
Ted Kremenek | 1036b68 | 2007-12-19 23:48:45 +0000 | [diff] [blame] | 584 | } |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 585 | |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 586 | /// createMacroArgInstantiationLoc - Return a new SourceLocation that encodes |
| 587 | /// the fact that a token from SpellingLoc should actually be referenced from |
| 588 | /// InstantiationLoc, and that it represents the instantiation of a macro |
| 589 | /// argument into the function-like macro body. |
| 590 | SourceLocation createMacroArgInstantiationLoc(SourceLocation Loc, |
| 591 | SourceLocation InstantiationLoc, |
| 592 | unsigned TokLength); |
| 593 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 594 | /// createInstantiationLoc - Return a new SourceLocation that encodes the fact |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 595 | /// that a token from SpellingLoc should actually be referenced from |
| 596 | /// InstantiationLoc. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 597 | SourceLocation createInstantiationLoc(SourceLocation Loc, |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 598 | SourceLocation InstantiationLocStart, |
| 599 | SourceLocation InstantiationLocEnd, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 600 | unsigned TokLength, |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 601 | int LoadedID = 0, |
| 602 | unsigned LoadedOffset = 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 603 | |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 604 | /// \brief Retrieve the memory buffer associated with the given file. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 605 | /// |
| 606 | /// \param Invalid If non-NULL, will be set \c true if an error |
| 607 | /// occurs while retrieving the memory buffer. |
| 608 | const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File, |
| 609 | bool *Invalid = 0); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 610 | |
| 611 | /// \brief Override the contents of the given source file by providing an |
| 612 | /// already-allocated buffer. |
| 613 | /// |
Dan Gohman | afbf5f8 | 2010-08-26 02:27:03 +0000 | [diff] [blame] | 614 | /// \param SourceFile the source file whose contents will be overriden. |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 615 | /// |
| 616 | /// \param Buffer the memory buffer whose contents will be used as the |
| 617 | /// data in the given source file. |
| 618 | /// |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 619 | /// \param DoNotFree If true, then the buffer will not be freed when the |
| 620 | /// source manager is destroyed. |
Dan Gohman | 0d06e99 | 2010-10-26 20:47:28 +0000 | [diff] [blame] | 621 | void overrideFileContents(const FileEntry *SourceFile, |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 622 | const llvm::MemoryBuffer *Buffer, |
| 623 | bool DoNotFree = false); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 624 | |
Argyrios Kyrtzidis | b1c8649 | 2011-03-05 01:03:53 +0000 | [diff] [blame] | 625 | /// \brief Override the the given source file with another one. |
| 626 | /// |
| 627 | /// \param SourceFile the source file which will be overriden. |
| 628 | /// |
| 629 | /// \param NewFile the file whose contents will be used as the |
| 630 | /// data instead of the contents of the given source file. |
| 631 | void overrideFileContents(const FileEntry *SourceFile, |
| 632 | const FileEntry *NewFile); |
| 633 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 634 | //===--------------------------------------------------------------------===// |
| 635 | // FileID manipulation methods. |
| 636 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 637 | |
Daniel Dunbar | 2ffb14f | 2009-12-06 09:19:25 +0000 | [diff] [blame] | 638 | /// getBuffer - Return the buffer for the specified FileID. If there is an |
| 639 | /// error opening this buffer the first time, this manufactures a temporary |
| 640 | /// buffer and returns a non-empty error string. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 641 | const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc, |
| 642 | bool *Invalid = 0) const { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 643 | bool MyInvalid = false; |
| 644 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); |
| 645 | if (MyInvalid || !Entry.isFile()) { |
| 646 | if (Invalid) |
| 647 | *Invalid = true; |
| 648 | |
| 649 | return getFakeBufferForRecovery(); |
| 650 | } |
| 651 | |
| 652 | return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc, |
| 653 | Invalid); |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 654 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 655 | |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 656 | const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 657 | bool MyInvalid = false; |
| 658 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); |
| 659 | if (MyInvalid || !Entry.isFile()) { |
| 660 | if (Invalid) |
| 661 | *Invalid = true; |
| 662 | |
| 663 | return getFakeBufferForRecovery(); |
| 664 | } |
| 665 | |
| 666 | return Entry.getFile().getContentCache()->getBuffer(Diag, *this, |
| 667 | SourceLocation(), |
| 668 | Invalid); |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 669 | } |
| 670 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 671 | /// getFileEntryForID - Returns the FileEntry record for the provided FileID. |
| 672 | const FileEntry *getFileEntryForID(FileID FID) const { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 673 | bool MyInvalid = false; |
| 674 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); |
| 675 | if (MyInvalid || !Entry.isFile()) |
| 676 | return 0; |
| 677 | |
| 678 | return Entry.getFile().getContentCache()->OrigEntry; |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 679 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 680 | |
Ted Kremenek | 9d5a165 | 2011-03-23 02:16:44 +0000 | [diff] [blame] | 681 | /// Returns the FileEntry record for the provided SLocEntry. |
| 682 | const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const |
| 683 | { |
| 684 | return sloc.getFile().getContentCache()->OrigEntry; |
| 685 | } |
| 686 | |
Benjamin Kramer | ceafc4b | 2010-03-16 14:48:07 +0000 | [diff] [blame] | 687 | /// getBufferData - Return a StringRef to the source buffer data for the |
| 688 | /// specified FileID. |
| 689 | /// |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 690 | /// \param FID The file ID whose contents will be returned. |
| 691 | /// \param Invalid If non-NULL, will be set true if an error occurred. |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 692 | StringRef getBufferData(FileID FID, bool *Invalid = 0) const; |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 693 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 694 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 695 | //===--------------------------------------------------------------------===// |
| 696 | // SourceLocation manipulation methods. |
| 697 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 698 | |
Chris Lattner | 668ab1a | 2009-03-13 01:05:57 +0000 | [diff] [blame] | 699 | /// getFileID - Return the FileID for a SourceLocation. This is a very |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 700 | /// hot method that is used for all SourceManager queries that start with a |
| 701 | /// SourceLocation object. It is responsible for finding the entry in |
| 702 | /// SLocEntryTable which contains the specified location. |
| 703 | /// |
| 704 | FileID getFileID(SourceLocation SpellingLoc) const { |
| 705 | unsigned SLocOffset = SpellingLoc.getOffset(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 706 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 707 | // If our one-entry cache covers this offset, just return it. |
| 708 | if (isOffsetInFileID(LastFileIDLookup, SLocOffset)) |
| 709 | return LastFileIDLookup; |
| 710 | |
| 711 | return getFileIDSlow(SLocOffset); |
| 712 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 713 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 714 | /// getLocForStartOfFile - Return the source location corresponding to the |
| 715 | /// first byte of the specified file. |
| 716 | SourceLocation getLocForStartOfFile(FileID FID) const { |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 717 | bool Invalid = false; |
| 718 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); |
| 719 | if (Invalid || !Entry.isFile()) |
| 720 | return SourceLocation(); |
| 721 | |
| 722 | unsigned FileOffset = Entry.getOffset(); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 723 | return SourceLocation::getFileLoc(FileOffset); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 724 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 725 | |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 726 | /// getExpansionLoc - Given a SourceLocation object, return the expansion |
| 727 | /// location referenced by the ID. |
| 728 | SourceLocation getExpansionLoc(SourceLocation Loc) const { |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 729 | // Handle the non-mapped case inline, defer to out of line code to handle |
Chandler Carruth | 4027853 | 2011-07-25 16:49:02 +0000 | [diff] [blame] | 730 | // expansions. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 731 | if (Loc.isFileID()) return Loc; |
Chandler Carruth | f84ef95 | 2011-07-25 20:52:26 +0000 | [diff] [blame] | 732 | return getExpansionLocSlowCase(Loc); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 733 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 734 | |
Chandler Carruth | 999f739 | 2011-07-25 20:52:21 +0000 | [diff] [blame] | 735 | /// getImmediateExpansionRange - Loc is required to be an expansion location. |
| 736 | /// Return the start/end of the expansion information. |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 737 | std::pair<SourceLocation,SourceLocation> |
Chandler Carruth | 999f739 | 2011-07-25 20:52:21 +0000 | [diff] [blame] | 738 | getImmediateExpansionRange(SourceLocation Loc) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 739 | |
Chandler Carruth | edc3dcc | 2011-07-25 16:56:02 +0000 | [diff] [blame] | 740 | /// getExpansionRange - Given a SourceLocation object, return the range of |
| 741 | /// tokens covered by the expansion the ultimate file. |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 742 | std::pair<SourceLocation,SourceLocation> |
Chandler Carruth | edc3dcc | 2011-07-25 16:56:02 +0000 | [diff] [blame] | 743 | getExpansionRange(SourceLocation Loc) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 744 | |
| 745 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 746 | /// getSpellingLoc - Given a SourceLocation object, return the spelling |
| 747 | /// location referenced by the ID. This is the place where the characters |
| 748 | /// that make up the lexed token can be found. |
| 749 | SourceLocation getSpellingLoc(SourceLocation Loc) const { |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 750 | // Handle the non-mapped case inline, defer to out of line code to handle |
| 751 | // instantiations. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 752 | if (Loc.isFileID()) return Loc; |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 753 | return getSpellingLocSlowCase(Loc); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 754 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 755 | |
Chris Lattner | 387616e | 2009-02-17 08:04:48 +0000 | [diff] [blame] | 756 | /// getImmediateSpellingLoc - Given a SourceLocation object, return the |
| 757 | /// spelling location referenced by the ID. This is the first level down |
| 758 | /// towards the place where the characters that make up the lexed token can be |
| 759 | /// found. This should not generally be used by clients. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 760 | SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 761 | |
| 762 | /// getDecomposedLoc - Decompose the specified location into a raw FileID + |
| 763 | /// Offset pair. The first element is the FileID, the second is the |
| 764 | /// offset from the start of the buffer of the location. |
| 765 | std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const { |
| 766 | FileID FID = getFileID(Loc); |
| 767 | return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset()); |
| 768 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 769 | |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 770 | /// getDecomposedExpansionLoc - Decompose the specified location into a |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 771 | /// raw FileID + Offset pair. If the location is an instantiation record, |
| 772 | /// walk through it until we find the final location instantiated. |
| 773 | std::pair<FileID, unsigned> |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 774 | getDecomposedExpansionLoc(SourceLocation Loc) const { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 775 | FileID FID = getFileID(Loc); |
| 776 | const SrcMgr::SLocEntry *E = &getSLocEntry(FID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 777 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 778 | unsigned Offset = Loc.getOffset()-E->getOffset(); |
| 779 | if (Loc.isFileID()) |
| 780 | return std::make_pair(FID, Offset); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 781 | |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 782 | return getDecomposedExpansionLocSlowCase(E); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 783 | } |
| 784 | |
| 785 | /// getDecomposedSpellingLoc - Decompose the specified location into a raw |
| 786 | /// FileID + Offset pair. If the location is an instantiation record, walk |
| 787 | /// through it until we find its spelling record. |
| 788 | std::pair<FileID, unsigned> |
| 789 | getDecomposedSpellingLoc(SourceLocation Loc) const { |
| 790 | FileID FID = getFileID(Loc); |
| 791 | const SrcMgr::SLocEntry *E = &getSLocEntry(FID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 792 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 793 | unsigned Offset = Loc.getOffset()-E->getOffset(); |
| 794 | if (Loc.isFileID()) |
| 795 | return std::make_pair(FID, Offset); |
| 796 | return getDecomposedSpellingLocSlowCase(E, Offset); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 797 | } |
| 798 | |
Chris Lattner | 52c2908 | 2009-01-27 06:27:13 +0000 | [diff] [blame] | 799 | /// getFileOffset - This method returns the offset from the start |
| 800 | /// of the file that the specified SourceLocation represents. This is not very |
| 801 | /// meaningful for a macro ID. |
| 802 | unsigned getFileOffset(SourceLocation SpellingLoc) const { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 803 | return getDecomposedLoc(SpellingLoc).second; |
| 804 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 805 | |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 806 | /// isMacroArgInstantiation - This method tests whether the given source |
| 807 | /// location represents a macro argument's instantiation into the |
| 808 | /// function-like macro definition. Such source locations only appear inside |
| 809 | /// of the instantiation locations representing where a particular |
| 810 | /// function-like macro was expanded. |
| 811 | bool isMacroArgInstantiation(SourceLocation Loc) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 812 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 813 | //===--------------------------------------------------------------------===// |
| 814 | // Queries about the code at a SourceLocation. |
| 815 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 816 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 817 | /// getCharacterData - Return a pointer to the start of the specified location |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 818 | /// in the appropriate spelling MemoryBuffer. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 819 | /// |
| 820 | /// \param Invalid If non-NULL, will be set \c true if an error occurs. |
| 821 | const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 822 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 823 | /// getColumnNumber - Return the column # for the specified file position. |
| 824 | /// This is significantly cheaper to compute than the line number. This |
| 825 | /// returns zero if the column number isn't known. This may only be called on |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 826 | /// a file sloc, so you must choose a spelling or instantiation location |
| 827 | /// before calling this method. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 828 | unsigned getColumnNumber(FileID FID, unsigned FilePos, |
| 829 | bool *Invalid = 0) const; |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 830 | unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const; |
Chandler Carruth | a77c031 | 2011-07-25 20:57:57 +0000 | [diff] [blame^] | 831 | unsigned getExpansionColumnNumber(SourceLocation Loc, |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 832 | bool *Invalid = 0) const; |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 833 | unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | |
| 835 | |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 836 | /// getLineNumber - Given a SourceLocation, return the spelling line number |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 837 | /// for the position indicated. This requires building and caching a table of |
| 838 | /// line offsets for the MemoryBuffer, so this is not cheap: use only when |
| 839 | /// about to emit a diagnostic. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 840 | unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const; |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 841 | unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const; |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 842 | unsigned getInstantiationLineNumber(SourceLocation Loc, |
| 843 | bool *Invalid = 0) const; |
Chandler Carruth | 5ef04ee | 2011-02-23 00:47:48 +0000 | [diff] [blame] | 844 | unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 845 | |
Chris Lattner | bff5c51 | 2009-02-17 08:39:06 +0000 | [diff] [blame] | 846 | /// Return the filename or buffer identifier of the buffer the location is in. |
| 847 | /// Note that this name does not respect #line directives. Use getPresumedLoc |
| 848 | /// for normal clients. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 849 | const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 850 | |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 851 | /// getFileCharacteristic - return the file characteristic of the specified |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 852 | /// source location, indicating whether this is a normal file, a system |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 853 | /// header, or an "implicit extern C" system header. |
| 854 | /// |
| 855 | /// This state can be modified with flags on GNU linemarker directives like: |
| 856 | /// # 4 "foo.h" 3 |
| 857 | /// which changes all source locations in the current file after that to be |
| 858 | /// considered to be from a system header. |
| 859 | SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 860 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 861 | /// getPresumedLoc - This method returns the "presumed" location of a |
| 862 | /// SourceLocation specifies. A "presumed location" can be modified by #line |
| 863 | /// or GNU line marker directives. This provides a view on the data that a |
| 864 | /// user should see in diagnostics, for example. |
| 865 | /// |
| 866 | /// Note that a presumed location is always given as the instantiation point |
| 867 | /// of an instantiation location, not at the spelling location. |
Douglas Gregor | cb7b1e1 | 2010-11-12 07:15:47 +0000 | [diff] [blame] | 868 | /// |
| 869 | /// \returns The presumed location of the specified SourceLocation. If the |
| 870 | /// presumed location cannot be calculate (e.g., because \p Loc is invalid |
| 871 | /// or the file containing \p Loc has changed on disk), returns an invalid |
| 872 | /// presumed location. |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 873 | PresumedLoc getPresumedLoc(SourceLocation Loc) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 874 | |
Ted Kremenek | 9fd87b1 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 875 | /// isFromSameFile - Returns true if both SourceLocations correspond to |
| 876 | /// the same file. |
| 877 | bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const { |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 878 | return getFileID(Loc1) == getFileID(Loc2); |
Ted Kremenek | 9fd87b1 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 879 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 880 | |
Ted Kremenek | 9fd87b1 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 881 | /// isFromMainFile - Returns true if the file of provided SourceLocation is |
| 882 | /// the main file. |
| 883 | bool isFromMainFile(SourceLocation Loc) const { |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 884 | return getFileID(Loc) == getMainFileID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 885 | } |
| 886 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 887 | /// isInSystemHeader - Returns if a SourceLocation is in a system header. |
| 888 | bool isInSystemHeader(SourceLocation Loc) const { |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 889 | return getFileCharacteristic(Loc) != SrcMgr::C_User; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 890 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 891 | |
Chris Lattner | 0d45658 | 2009-06-13 23:31:51 +0000 | [diff] [blame] | 892 | /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C" |
| 893 | /// system header. |
| 894 | bool isInExternCSystemHeader(SourceLocation Loc) const { |
| 895 | return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem; |
| 896 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 897 | |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 898 | /// \brief Given a specific chunk of a FileID (FileID with offset+length), |
| 899 | /// returns true if \arg Loc is inside that chunk and sets relative offset |
| 900 | /// (offset of \arg Loc from beginning of chunk) to \arg relativeOffset. |
| 901 | bool isInFileID(SourceLocation Loc, |
| 902 | FileID FID, unsigned offset, unsigned length, |
| 903 | unsigned *relativeOffset = 0) const { |
| 904 | assert(!FID.isInvalid()); |
| 905 | if (Loc.isInvalid()) |
| 906 | return false; |
| 907 | |
| 908 | unsigned start = getSLocEntry(FID).getOffset() + offset; |
| 909 | unsigned end = start + length; |
| 910 | |
| 911 | #ifndef NDEBUG |
| 912 | // Make sure offset/length describe a chunk inside the given FileID. |
| 913 | unsigned NextOffset; |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 914 | if (FID.ID == -2) |
| 915 | NextOffset = 1U << 31U; |
| 916 | else if (FID.ID+1 == (int)LocalSLocEntryTable.size()) |
| 917 | NextOffset = getNextLocalOffset(); |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 918 | else |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 919 | NextOffset = getSLocEntryByID(FID.ID+1).getOffset(); |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 920 | assert(start < NextOffset); |
| 921 | assert(end < NextOffset); |
| 922 | #endif |
| 923 | |
| 924 | if (Loc.getOffset() >= start && Loc.getOffset() < end) { |
| 925 | if (relativeOffset) |
| 926 | *relativeOffset = Loc.getOffset() - start; |
| 927 | return true; |
| 928 | } |
| 929 | |
| 930 | return false; |
| 931 | } |
Argyrios Kyrtzidis | 469244a | 2011-05-28 03:56:11 +0000 | [diff] [blame] | 932 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 933 | //===--------------------------------------------------------------------===// |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 934 | // Line Table Manipulation Routines |
| 935 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 936 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 937 | /// getLineTableFilenameID - Return the uniqued ID for the specified filename. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 938 | /// |
Chris Lattner | 686775d | 2011-07-20 06:58:45 +0000 | [diff] [blame] | 939 | unsigned getLineTableFilenameID(StringRef Str); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 940 | |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 941 | /// AddLineNote - Add a line note to the line table for the FileID and offset |
| 942 | /// specified by Loc. If FilenameID is -1, it is considered to be |
| 943 | /// unspecified. |
| 944 | void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID); |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 945 | void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 946 | bool IsFileEntry, bool IsFileExit, |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 947 | bool IsSystemHeader, bool IsExternCHeader); |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 948 | |
| 949 | /// \brief Determine if the source manager has a line table. |
| 950 | bool hasLineTable() const { return LineTable != 0; } |
| 951 | |
| 952 | /// \brief Retrieve the stored line table. |
| 953 | LineTableInfo &getLineTable(); |
| 954 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 955 | //===--------------------------------------------------------------------===// |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 956 | // Queries for performance analysis. |
| 957 | //===--------------------------------------------------------------------===// |
| 958 | |
| 959 | /// Return the total amount of physical memory allocated by the |
| 960 | /// ContentCache allocator. |
| 961 | size_t getContentCacheSize() const { |
| 962 | return ContentCacheAlloc.getTotalMemory(); |
| 963 | } |
Ted Kremenek | f61b831 | 2011-04-28 20:36:42 +0000 | [diff] [blame] | 964 | |
| 965 | struct MemoryBufferSizes { |
| 966 | const size_t malloc_bytes; |
| 967 | const size_t mmap_bytes; |
| 968 | |
| 969 | MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes) |
| 970 | : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {} |
| 971 | }; |
| 972 | |
| 973 | /// Return the amount of memory used by memory buffers, breaking down |
| 974 | /// by heap-backed versus mmap'ed memory. |
| 975 | MemoryBufferSizes getMemoryBufferSizes() const; |
Ted Kremenek | 457aaf0 | 2011-04-28 04:10:31 +0000 | [diff] [blame] | 976 | |
| 977 | //===--------------------------------------------------------------------===// |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 978 | // Other miscellaneous methods. |
| 979 | //===--------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 980 | |
| 981 | /// \brief Get the source location for the given file:line:col triplet. |
| 982 | /// |
| 983 | /// If the source file is included multiple times, the source location will |
| 984 | /// be based upon the first inclusion. |
| 985 | SourceLocation getLocation(const FileEntry *SourceFile, |
Douglas Gregor | 86a4d0d | 2011-02-03 17:17:35 +0000 | [diff] [blame] | 986 | unsigned Line, unsigned Col); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 987 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 988 | /// \brief Determines the order of 2 source locations in the translation unit. |
| 989 | /// |
| 990 | /// \returns true if LHS source location comes before RHS, false otherwise. |
| 991 | bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const; |
| 992 | |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 993 | /// \brief Determines the order of 2 source locations in the "source location |
| 994 | /// address space". |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 995 | bool isBeforeInSourceLocationOffset(SourceLocation LHS, |
| 996 | SourceLocation RHS) const { |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 997 | return isBeforeInSourceLocationOffset(LHS, RHS.getOffset()); |
| 998 | } |
| 999 | |
| 1000 | /// \brief Determines the order of a source location and a source location |
| 1001 | /// offset in the "source location address space". |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1002 | /// |
| 1003 | /// Note that we always consider source locations loaded from |
| 1004 | bool isBeforeInSourceLocationOffset(SourceLocation LHS, unsigned RHS) const { |
| 1005 | unsigned LHSOffset = LHS.getOffset(); |
| 1006 | bool LHSLoaded = LHSOffset >= CurrentLoadedOffset; |
| 1007 | bool RHSLoaded = RHS >= CurrentLoadedOffset; |
| 1008 | if (LHSLoaded == RHSLoaded) |
| 1009 | return LHS.getOffset() < RHS; |
| 1010 | |
| 1011 | return LHSLoaded; |
Argyrios Kyrtzidis | b73377e | 2011-07-07 03:40:34 +0000 | [diff] [blame] | 1012 | } |
| 1013 | |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 1014 | // Iterators over FileInfos. |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 1015 | typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> |
| 1016 | ::const_iterator fileinfo_iterator; |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 1017 | fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); } |
| 1018 | fileinfo_iterator fileinfo_end() const { return FileInfos.end(); } |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 1019 | bool hasFileInfo(const FileEntry *File) const { |
| 1020 | return FileInfos.find(File) != FileInfos.end(); |
| 1021 | } |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 1022 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1023 | /// PrintStats - Print statistics to stderr. |
| 1024 | /// |
| 1025 | void PrintStats() const; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1026 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1027 | /// \brief Get the number of local SLocEntries we have. |
| 1028 | unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); } |
| 1029 | |
| 1030 | /// \brief Get a local SLocEntry. This is exposed for indexing. |
| 1031 | const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index, |
| 1032 | bool *Invalid = 0) const { |
| 1033 | assert(Index < LocalSLocEntryTable.size() && "Invalid index"); |
| 1034 | return LocalSLocEntryTable[Index]; |
Douglas Gregor | bdfe48a | 2009-10-16 22:46:09 +0000 | [diff] [blame] | 1035 | } |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1036 | |
| 1037 | /// \brief Get the number of loaded SLocEntries we have. |
| 1038 | unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();} |
| 1039 | |
| 1040 | /// \brief Get a loaded SLocEntry. This is exposed for indexing. |
| 1041 | const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index, bool *Invalid=0) const { |
| 1042 | assert(Index < LoadedSLocEntryTable.size() && "Invalid index"); |
| 1043 | if (!SLocEntryLoaded[Index]) |
| 1044 | ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2)); |
| 1045 | return LoadedSLocEntryTable[Index]; |
| 1046 | } |
| 1047 | |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1048 | const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const { |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1049 | return getSLocEntryByID(FID.ID); |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 1050 | } |
| 1051 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1052 | unsigned getNextLocalOffset() const { return NextLocalOffset; } |
| 1053 | |
| 1054 | void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) { |
| 1055 | assert(LoadedSLocEntryTable.empty() && |
| 1056 | "Invalidating existing loaded entries"); |
| 1057 | ExternalSLocEntries = Source; |
| 1058 | } |
| 1059 | |
| 1060 | /// \brief Allocate a number of loaded SLocEntries, which will be actually |
| 1061 | /// loaded on demand from the external source. |
| 1062 | /// |
| 1063 | /// NumSLocEntries will be allocated, which occupy a total of TotalSize space |
| 1064 | /// in the global source view. The lowest ID and the base offset of the |
| 1065 | /// entries will be returned. |
| 1066 | std::pair<int, unsigned> |
| 1067 | AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize); |
| 1068 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1069 | private: |
Douglas Gregor | e23ac65 | 2011-04-20 00:21:03 +0000 | [diff] [blame] | 1070 | const llvm::MemoryBuffer *getFakeBufferForRecovery() const; |
| 1071 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1072 | /// \brief Get the entry with the given unwrapped FileID. |
| 1073 | const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const { |
| 1074 | assert(ID != -1 && "Using FileID sentinel value"); |
| 1075 | if (ID < 0) |
| 1076 | return getLoadedSLocEntryByID(ID); |
| 1077 | return getLocalSLocEntry(static_cast<unsigned>(ID)); |
| 1078 | } |
| 1079 | |
| 1080 | const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID) const { |
| 1081 | return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2)); |
| 1082 | } |
| 1083 | |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 1084 | /// createInstantiationLoc - Implements the common elements of storing an |
| 1085 | /// instantiation info struct into the SLocEntry table and producing a source |
| 1086 | /// location that refers to it. |
| 1087 | SourceLocation createInstantiationLocImpl(const SrcMgr::InstantiationInfo &II, |
| 1088 | unsigned TokLength, |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1089 | int LoadedID = 0, |
| 1090 | unsigned LoadedOffset = 0); |
Chandler Carruth | c8d1ecc | 2011-07-07 23:56:36 +0000 | [diff] [blame] | 1091 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1092 | /// isOffsetInFileID - Return true if the specified FileID contains the |
| 1093 | /// specified SourceLocation offset. This is a very hot method. |
| 1094 | inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const { |
| 1095 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID); |
| 1096 | // If the entry is after the offset, it can't contain it. |
| 1097 | if (SLocOffset < Entry.getOffset()) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1098 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1099 | // If this is the very last entry then it does. |
| 1100 | if (FID.ID == -2) |
| 1101 | return true; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1102 | |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1103 | // If it is the last local entry, then it does if the location is local. |
| 1104 | if (static_cast<unsigned>(FID.ID+1) == LocalSLocEntryTable.size()) { |
| 1105 | return SLocOffset < NextLocalOffset; |
| 1106 | } |
| 1107 | |
| 1108 | // Otherwise, the entry after it has to not include it. This works for both |
| 1109 | // local and loaded entries. |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1110 | return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset(); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1111 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1112 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1113 | /// createFileID - Create a new fileID for the specified ContentCache and |
| 1114 | /// include position. This works regardless of whether the ContentCache |
| 1115 | /// corresponds to a file or some other input source. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 1116 | FileID createFileID(const SrcMgr::ContentCache* File, |
| 1117 | SourceLocation IncludePos, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 1118 | SrcMgr::CharacteristicKind DirCharacter, |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1119 | int LoadedID, unsigned LoadedOffset); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1120 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1121 | const SrcMgr::ContentCache * |
| 1122 | getOrCreateContentCache(const FileEntry *SourceFile); |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 1123 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 1124 | /// createMemBufferContentCache - Create a new ContentCache for the specified |
| 1125 | /// memory buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1126 | const SrcMgr::ContentCache* |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 1127 | createMemBufferContentCache(const llvm::MemoryBuffer *Buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 1128 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1129 | FileID getFileIDSlow(unsigned SLocOffset) const; |
Douglas Gregor | f62d43d | 2011-07-19 16:10:42 +0000 | [diff] [blame] | 1130 | FileID getFileIDLocal(unsigned SLocOffset) const; |
| 1131 | FileID getFileIDLoaded(unsigned SLocOffset) const; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1132 | |
Chandler Carruth | f84ef95 | 2011-07-25 20:52:26 +0000 | [diff] [blame] | 1133 | SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const; |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 1134 | SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const; |
| 1135 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1136 | std::pair<FileID, unsigned> |
Chandler Carruth | e7b2b6e | 2011-07-25 20:52:32 +0000 | [diff] [blame] | 1137 | getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 1138 | std::pair<FileID, unsigned> |
| 1139 | getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, |
| 1140 | unsigned Offset) const; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 1141 | }; |
| 1142 | |
| 1143 | |
| 1144 | } // end namespace clang |
| 1145 | |
| 1146 | #endif |