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 | |
| 17 | #include "clang/Basic/SourceLocation.h" |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 18 | #include "llvm/Support/Allocator.h" |
Chandler Carruth | 9f8eb20 | 2009-10-26 01:37:10 +0000 | [diff] [blame] | 19 | #include "llvm/System/DataTypes.h" |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 20 | #include "llvm/ADT/PointerIntPair.h" |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 21 | #include "llvm/ADT/PointerUnion.h" |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 22 | #include "llvm/ADT/DenseMap.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | #include <vector> |
Chris Lattner | 9dc62f0 | 2007-07-12 15:32:57 +0000 | [diff] [blame] | 24 | #include <cassert> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 25 | |
| 26 | namespace llvm { |
| 27 | class MemoryBuffer; |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 28 | class StringRef; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 29 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 30 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 31 | namespace clang { |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 32 | |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 33 | class Diagnostic; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 34 | class SourceManager; |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 35 | class FileManager; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 36 | class FileEntry; |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 37 | class LineTableInfo; |
Douglas Gregor | aea67db | 2010-03-15 22:54:52 +0000 | [diff] [blame] | 38 | |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 39 | /// SrcMgr - Public enums and private classes that are part of the |
| 40 | /// SourceManager implementation. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 41 | /// |
| 42 | namespace SrcMgr { |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 43 | /// CharacteristicKind - This is used to represent whether a file or directory |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 44 | /// holds normal user code, system code, or system code which is implicitly |
| 45 | /// 'extern "C"' in C++ mode. Entire directories can be tagged with this |
| 46 | /// (this is maintained by DirectoryLookup and friends) as can specific |
| 47 | /// FileIDInfos when a #pragma system_header is seen or various other cases. |
| 48 | /// |
Chris Lattner | 9d72851 | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 49 | enum CharacteristicKind { |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 50 | C_User, C_System, C_ExternCSystem |
| 51 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 52 | |
Dan Gohman | 4710a8e | 2010-08-25 21:59:25 +0000 | [diff] [blame] | 53 | /// ContentCache - One instance of this struct is kept for every file |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 54 | /// loaded or used. This object owns the MemoryBuffer object. |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 55 | class ContentCache { |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 56 | enum CCFlags { |
| 57 | /// \brief Whether the buffer is invalid. |
| 58 | InvalidFlag = 0x01, |
| 59 | /// \brief Whether the buffer should not be freed on destruction. |
| 60 | DoNotFreeFlag = 0x02 |
| 61 | }; |
| 62 | |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 63 | /// Buffer - The actual buffer containing the characters from the input |
| 64 | /// file. This is owned by the ContentCache object. |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 65 | /// The bits indicate indicates whether the buffer is invalid. |
| 66 | mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer; |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 67 | |
| 68 | public: |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 69 | /// Reference to the file entry. This reference does not own |
| 70 | /// the FileEntry object. It is possible for this to be NULL if |
| 71 | /// the ContentCache encapsulates an imaginary text buffer. |
Chris Lattner | 0581659 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 72 | const FileEntry *Entry; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 73 | |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 74 | /// SourceLineCache - A bump pointer allocated array of offsets for each |
| 75 | /// source line. This is lazily computed. This is owned by the |
| 76 | /// SourceManager BumpPointerAllocator object. |
Chris Lattner | 0581659 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 77 | unsigned *SourceLineCache; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 78 | |
Ted Kremenek | b6427f8 | 2007-12-04 18:59:28 +0000 | [diff] [blame] | 79 | /// NumLines - The number of lines in this ContentCache. This is only valid |
| 80 | /// if SourceLineCache is non-null. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 81 | unsigned NumLines; |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 82 | |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 83 | /// getBuffer - Returns the memory buffer for the associated content. |
| 84 | /// |
| 85 | /// \param Diag Object through which diagnostics will be emitted it the |
| 86 | /// buffer cannot be retrieved. |
| 87 | /// |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 88 | /// \param Loc If specified, is the location that invalid file diagnostics |
| 89 | /// will be emitted at. |
| 90 | /// |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 91 | /// \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] | 92 | const llvm::MemoryBuffer *getBuffer(Diagnostic &Diag, |
| 93 | const SourceManager &SM, |
| 94 | SourceLocation Loc = SourceLocation(), |
Douglas Gregor | 36c35ba | 2010-03-16 00:35:39 +0000 | [diff] [blame] | 95 | bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 96 | |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 97 | /// getSize - Returns the size of the content encapsulated by this |
| 98 | /// ContentCache. This can be the size of the source file or the size of an |
| 99 | /// arbitrary scratch buffer. If the ContentCache encapsulates a source |
| 100 | /// file this size is retrieved from the file's FileEntry. |
| 101 | unsigned getSize() const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 102 | |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 103 | /// getSizeBytesMapped - Returns the number of bytes actually mapped for |
| 104 | /// this ContentCache. This can be 0 if the MemBuffer was not actually |
| 105 | /// instantiated. |
| 106 | unsigned getSizeBytesMapped() const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 107 | |
Chris Lattner | 0581659 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 108 | void setBuffer(const llvm::MemoryBuffer *B) { |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 109 | assert(!Buffer.getPointer() && "MemoryBuffer already set."); |
| 110 | Buffer.setPointer(B); |
| 111 | Buffer.setInt(false); |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 112 | } |
Douglas Gregor | cc5888d | 2010-07-31 00:40:00 +0000 | [diff] [blame] | 113 | |
| 114 | /// \brief Get the underlying buffer, returning NULL if the buffer is not |
| 115 | /// yet available. |
| 116 | const llvm::MemoryBuffer *getRawBuffer() const { |
| 117 | return Buffer.getPointer(); |
| 118 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 119 | |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 120 | /// \brief Replace the existing buffer (which will be deleted) |
| 121 | /// with the given buffer. |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 122 | void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 123 | |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 124 | /// \brief Determine whether the buffer itself is invalid. |
| 125 | bool isBufferInvalid() const { |
| 126 | return Buffer.getInt() & InvalidFlag; |
| 127 | } |
| 128 | |
| 129 | /// \brief Determine whether the buffer should be freed. |
| 130 | bool shouldFreeBuffer() const { |
| 131 | return (Buffer.getInt() & DoNotFreeFlag) == 0; |
| 132 | } |
| 133 | |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 134 | ContentCache(const FileEntry *Ent = 0) |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 135 | : Buffer(0, false), Entry(Ent), SourceLineCache(0), NumLines(0) {} |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 136 | |
| 137 | ~ContentCache(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 138 | |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 139 | /// The copy ctor does not allow copies where source object has either |
| 140 | /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory |
| 141 | /// is not transfered, so this is a logical error. |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 142 | ContentCache(const ContentCache &RHS) |
| 143 | : Buffer(0, false), SourceLineCache(0) |
| 144 | { |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 145 | Entry = RHS.Entry; |
| 146 | |
Douglas Gregor | c815108 | 2010-03-16 22:53:51 +0000 | [diff] [blame] | 147 | assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0 |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 148 | && "Passed ContentCache object cannot own a buffer."); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 149 | |
| 150 | NumLines = RHS.NumLines; |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 151 | } |
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 | private: |
| 154 | // Disable assignments. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 155 | ContentCache &operator=(const ContentCache& RHS); |
| 156 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 157 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 158 | /// FileInfo - Information about a FileID, basically just the logical file |
| 159 | /// that it represents and include stack information. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 160 | /// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 161 | /// Each FileInfo has include stack information, indicating where it came |
| 162 | /// from. This information encodes the #include chain that a token was |
| 163 | /// instantiated from. The main include file has an invalid IncludeLoc. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 164 | /// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 165 | /// FileInfos contain a "ContentCache *", with the contents of the file. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 166 | /// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 167 | class FileInfo { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 168 | /// IncludeLoc - The location of the #include that brought in this file. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 169 | /// This is an invalid SLOC for the main file (top of the #include chain). |
| 170 | unsigned IncludeLoc; // Really a SourceLocation |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 172 | /// Data - This contains the ContentCache* and the bits indicating the |
| 173 | /// characteristic of the file and whether it has #line info, all bitmangled |
| 174 | /// together. |
| 175 | uintptr_t Data; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 176 | public: |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 177 | /// get - Return a FileInfo object. |
| 178 | static FileInfo get(SourceLocation IL, const ContentCache *Con, |
| 179 | CharacteristicKind FileCharacter) { |
| 180 | FileInfo X; |
| 181 | X.IncludeLoc = IL.getRawEncoding(); |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 182 | X.Data = (uintptr_t)Con; |
Chris Lattner | 00282d6 | 2009-02-03 07:41:46 +0000 | [diff] [blame] | 183 | assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned"); |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 184 | assert((unsigned)FileCharacter < 4 && "invalid file character"); |
| 185 | X.Data |= (unsigned)FileCharacter; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 186 | return X; |
| 187 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 188 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 189 | SourceLocation getIncludeLoc() const { |
| 190 | return SourceLocation::getFromRawEncoding(IncludeLoc); |
| 191 | } |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 192 | const ContentCache* getContentCache() const { |
Chris Lattner | 00282d6 | 2009-02-03 07:41:46 +0000 | [diff] [blame] | 193 | return reinterpret_cast<const ContentCache*>(Data & ~7UL); |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 194 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 195 | |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 196 | /// getCharacteristic - Return whether this is a system header or not. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 197 | CharacteristicKind getFileCharacteristic() const { |
Chris Lattner | 6e1aff2 | 2009-01-26 06:49:09 +0000 | [diff] [blame] | 198 | return (CharacteristicKind)(Data & 3); |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 199 | } |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 200 | |
| 201 | /// hasLineDirectives - Return true if this FileID has #line directives in |
| 202 | /// it. |
| 203 | bool hasLineDirectives() const { return (Data & 4) != 0; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 204 | |
Chris Lattner | ac50e34 | 2009-02-03 22:13:05 +0000 | [diff] [blame] | 205 | /// setHasLineDirectives - Set the flag that indicates that this FileID has |
| 206 | /// line table entries associated with it. |
| 207 | void setHasLineDirectives() { |
| 208 | Data |= 4; |
| 209 | } |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 210 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 211 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 212 | /// InstantiationInfo - Each InstantiationInfo encodes the Instantiation |
| 213 | /// location - where the token was ultimately instantiated, and the |
| 214 | /// SpellingLoc - where the actual character data for the token came from. |
| 215 | class InstantiationInfo { |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 216 | // Really these are all SourceLocations. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 217 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 218 | /// SpellingLoc - Where the spelling for the token can be found. |
| 219 | unsigned SpellingLoc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 220 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 221 | /// InstantiationLocStart/InstantiationLocEnd - In a macro expansion, these |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 222 | /// indicate the start and end of the instantiation. In object-like macros, |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 223 | /// these will be the same. In a function-like macro instantiation, the |
| 224 | /// start will be the identifier and the end will be the ')'. |
| 225 | unsigned InstantiationLocStart, InstantiationLocEnd; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 226 | public: |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 227 | SourceLocation getSpellingLoc() const { |
| 228 | return SourceLocation::getFromRawEncoding(SpellingLoc); |
| 229 | } |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 230 | SourceLocation getInstantiationLocStart() const { |
| 231 | return SourceLocation::getFromRawEncoding(InstantiationLocStart); |
| 232 | } |
| 233 | SourceLocation getInstantiationLocEnd() const { |
| 234 | return SourceLocation::getFromRawEncoding(InstantiationLocEnd); |
| 235 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 236 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 237 | std::pair<SourceLocation,SourceLocation> getInstantiationLocRange() const { |
| 238 | return std::make_pair(getInstantiationLocStart(), |
| 239 | getInstantiationLocEnd()); |
| 240 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 241 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 242 | /// get - Return a InstantiationInfo for an expansion. IL specifies |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 243 | /// the instantiation location (where the macro is expanded), and SL |
| 244 | /// specifies the spelling location (where the characters from the token |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 245 | /// come from). IL and PL can both refer to normal File SLocs or |
| 246 | /// instantiation locations. |
| 247 | static InstantiationInfo get(SourceLocation ILStart, SourceLocation ILEnd, |
| 248 | SourceLocation SL) { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 249 | InstantiationInfo X; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 250 | X.SpellingLoc = SL.getRawEncoding(); |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 251 | X.InstantiationLocStart = ILStart.getRawEncoding(); |
| 252 | X.InstantiationLocEnd = ILEnd.getRawEncoding(); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 253 | return X; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 254 | } |
| 255 | }; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 256 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 257 | /// SLocEntry - This is a discriminated union of FileInfo and |
| 258 | /// InstantiationInfo. SourceManager keeps an array of these objects, and |
| 259 | /// they are uniquely identified by the FileID datatype. |
| 260 | class SLocEntry { |
| 261 | unsigned Offset; // low bit is set for instantiation info. |
| 262 | union { |
| 263 | FileInfo File; |
| 264 | InstantiationInfo Instantiation; |
| 265 | }; |
| 266 | public: |
| 267 | unsigned getOffset() const { return Offset >> 1; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 268 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 269 | bool isInstantiation() const { return Offset & 1; } |
| 270 | bool isFile() const { return !isInstantiation(); } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 271 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 272 | const FileInfo &getFile() const { |
| 273 | assert(isFile() && "Not a file SLocEntry!"); |
| 274 | return File; |
| 275 | } |
| 276 | |
| 277 | const InstantiationInfo &getInstantiation() const { |
| 278 | assert(isInstantiation() && "Not an instantiation SLocEntry!"); |
| 279 | return Instantiation; |
| 280 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 281 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 282 | static SLocEntry get(unsigned Offset, const FileInfo &FI) { |
| 283 | SLocEntry E; |
| 284 | E.Offset = Offset << 1; |
| 285 | E.File = FI; |
| 286 | return E; |
| 287 | } |
| 288 | |
| 289 | static SLocEntry get(unsigned Offset, const InstantiationInfo &II) { |
| 290 | SLocEntry E; |
| 291 | E.Offset = (Offset << 1) | 1; |
| 292 | E.Instantiation = II; |
| 293 | return E; |
| 294 | } |
| 295 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 296 | } // end SrcMgr namespace. |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 297 | |
| 298 | /// \brief External source of source location entries. |
| 299 | class ExternalSLocEntrySource { |
| 300 | public: |
| 301 | virtual ~ExternalSLocEntrySource(); |
| 302 | |
| 303 | /// \brief Read the source location entry with index ID. |
| 304 | virtual void ReadSLocEntry(unsigned ID) = 0; |
| 305 | }; |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 306 | |
| 307 | |
| 308 | /// IsBeforeInTranslationUnitCache - This class holds the cache used by |
| 309 | /// isBeforeInTranslationUnit. The cache structure is complex enough to be |
| 310 | /// worth breaking out of SourceManager. |
| 311 | class IsBeforeInTranslationUnitCache { |
| 312 | /// L/R QueryFID - These are the FID's of the cached query. If these match up |
| 313 | /// with a subsequent query, the result can be reused. |
| 314 | FileID LQueryFID, RQueryFID; |
| 315 | |
| 316 | /// CommonFID - This is the file found in common between the two #include |
| 317 | /// traces. It is the nearest common ancestor of the #include tree. |
| 318 | FileID CommonFID; |
| 319 | |
| 320 | /// L/R CommonOffset - This is the offset of the previous query in CommonFID. |
| 321 | /// Usually, this represents the location of the #include for QueryFID, but if |
| 322 | /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a |
| 323 | /// random token in the parent. |
| 324 | unsigned LCommonOffset, RCommonOffset; |
| 325 | public: |
| 326 | |
| 327 | /// isCacheValid - Return true if the currently cached values match up with |
| 328 | /// the specified LHS/RHS query. If not, we can't use the cache. |
| 329 | bool isCacheValid(FileID LHS, FileID RHS) const { |
| 330 | return LQueryFID == LHS && RQueryFID == RHS; |
| 331 | } |
| 332 | |
| 333 | /// getCachedResult - If the cache is valid, compute the result given the |
| 334 | /// specified offsets in the LHS/RHS FID's. |
| 335 | bool getCachedResult(unsigned LOffset, unsigned ROffset) const { |
| 336 | // If one of the query files is the common file, use the offset. Otherwise, |
| 337 | // use the #include loc in the common file. |
| 338 | if (LQueryFID != CommonFID) LOffset = LCommonOffset; |
| 339 | if (RQueryFID != CommonFID) ROffset = RCommonOffset; |
| 340 | return LOffset < ROffset; |
| 341 | } |
| 342 | |
| 343 | // Set up a new query. |
| 344 | void setQueryFIDs(FileID LHS, FileID RHS) { |
| 345 | LQueryFID = LHS; |
| 346 | RQueryFID = RHS; |
| 347 | } |
| 348 | |
| 349 | void setCommonLoc(FileID commonFID, unsigned lCommonOffset, |
| 350 | unsigned rCommonOffset) { |
| 351 | CommonFID = commonFID; |
| 352 | LCommonOffset = lCommonOffset; |
| 353 | RCommonOffset = rCommonOffset; |
| 354 | } |
| 355 | |
| 356 | }; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 357 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 358 | /// SourceManager - This file handles loading and caching of source files into |
| 359 | /// memory. This object owns the MemoryBuffer objects for all of the loaded |
| 360 | /// files and assigns unique FileID's for each unique #include chain. |
| 361 | /// |
| 362 | /// The SourceManager can be queried for information about SourceLocation |
Chris Lattner | f7cf85b | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 363 | /// objects, turning them into either spelling or instantiation locations. |
| 364 | /// Spelling locations represent where the bytes corresponding to a token came |
| 365 | /// from and instantiation locations represent where the location is in the |
| 366 | /// user's view. In the case of a macro expansion, for example, the spelling |
| 367 | /// location indicates where the expanded token came from and the instantiation |
| 368 | /// location specifies where it was expanded. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 369 | class SourceManager { |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 370 | /// \brief Diagnostic object. |
| 371 | Diagnostic &Diag; |
| 372 | |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 373 | mutable llvm::BumpPtrAllocator ContentCacheAlloc; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 374 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 375 | /// FileInfos - Memoized information about all of the files tracked by this |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 376 | /// SourceManager. This set allows us to merge ContentCache entries based |
| 377 | /// on their FileEntry*. All ContentCache objects will thus have unique, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 378 | /// non-null, FileEntry pointers. |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 379 | llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 380 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 381 | /// MemBufferInfos - Information about various memory buffers that we have |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 382 | /// read in. All FileEntry* within the stored ContentCache objects are NULL, |
| 383 | /// as they do not refer to a file. |
| 384 | std::vector<SrcMgr::ContentCache*> MemBufferInfos; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 385 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 386 | /// SLocEntryTable - This is an array of SLocEntry's that we have created. |
| 387 | /// FileID is an index into this vector. This array is sorted by the offset. |
| 388 | std::vector<SrcMgr::SLocEntry> SLocEntryTable; |
| 389 | /// NextOffset - This is the next available offset that a new SLocEntry can |
| 390 | /// start at. It is SLocEntryTable.back().getOffset()+size of back() entry. |
| 391 | unsigned NextOffset; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 392 | |
| 393 | /// \brief If source location entries are being lazily loaded from |
| 394 | /// an external source, this vector indicates whether the Ith source |
| 395 | /// location entry has already been loaded from the external storage. |
| 396 | std::vector<bool> SLocEntryLoaded; |
| 397 | |
| 398 | /// \brief An external source for source location entries. |
| 399 | ExternalSLocEntrySource *ExternalSLocEntries; |
| 400 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 401 | /// LastFileIDLookup - This is a one-entry cache to speed up getFileID. |
| 402 | /// LastFileIDLookup records the last FileID looked up or created, because it |
| 403 | /// is very common to look up many tokens from the same file. |
| 404 | mutable FileID LastFileIDLookup; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 405 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 406 | /// LineTable - This holds information for #line directives. It is referenced |
| 407 | /// by indices from SLocEntryTable. |
| 408 | LineTableInfo *LineTable; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 409 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 410 | /// LastLineNo - These ivars serve as a cache used in the getLineNumber |
| 411 | /// method which is used to speedup getLineNumber calls to nearby locations. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 412 | mutable FileID LastLineNoFileIDQuery; |
Chris Lattner | f812a45 | 2008-11-18 06:51:15 +0000 | [diff] [blame] | 413 | mutable SrcMgr::ContentCache *LastLineNoContentCache; |
| 414 | mutable unsigned LastLineNoFilePos; |
| 415 | mutable unsigned LastLineNoResult; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 416 | |
Ted Kremenek | 76edd0e | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 417 | /// 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] | 418 | FileID MainFileID; |
Steve Naroff | 49c1f4a | 2008-02-02 00:10:46 +0000 | [diff] [blame] | 419 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 420 | // Statistics for -print-stats. |
| 421 | mutable unsigned NumLinearScans, NumBinaryProbes; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 422 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 423 | // Cache results for the isBeforeInTranslationUnit method. |
Chris Lattner | dcb1d68 | 2010-05-07 01:17:07 +0000 | [diff] [blame] | 424 | mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 425 | |
Steve Naroff | 49c1f4a | 2008-02-02 00:10:46 +0000 | [diff] [blame] | 426 | // SourceManager doesn't support copy construction. |
| 427 | explicit SourceManager(const SourceManager&); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 428 | void operator=(const SourceManager&); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 429 | public: |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 430 | SourceManager(Diagnostic &Diag) |
| 431 | : Diag(Diag), ExternalSLocEntries(0), LineTable(0), NumLinearScans(0), |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 432 | NumBinaryProbes(0) { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 433 | clearIDTables(); |
| 434 | } |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 435 | ~SourceManager(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 436 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 437 | void clearIDTables(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 438 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 439 | //===--------------------------------------------------------------------===// |
| 440 | // MainFileID creation and querying methods. |
| 441 | //===--------------------------------------------------------------------===// |
| 442 | |
Ted Kremenek | 76edd0e | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 443 | /// getMainFileID - Returns the FileID of the main source file. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 444 | FileID getMainFileID() const { return MainFileID; } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 445 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 446 | /// createMainFileID - Create the FileID for the main source file. |
| 447 | FileID createMainFileID(const FileEntry *SourceFile, |
| 448 | SourceLocation IncludePos) { |
| 449 | assert(MainFileID.isInvalid() && "MainFileID already set!"); |
| 450 | MainFileID = createFileID(SourceFile, IncludePos, SrcMgr::C_User); |
| 451 | return MainFileID; |
| 452 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 453 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 454 | //===--------------------------------------------------------------------===// |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 455 | // Methods to create new FileID's and instantiations. |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 456 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 457 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 458 | /// createFileID - Create a new FileID that represents the specified file |
| 459 | /// being #included from the specified IncludePosition. This returns 0 on |
| 460 | /// error and translates NULL into standard input. |
Sebastian Redl | 190faf7 | 2010-07-20 21:50:20 +0000 | [diff] [blame] | 461 | /// PreallocateID should be non-zero to specify which pre-allocated, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 462 | /// lazily computed source location is being filled in by this operation. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 463 | FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 464 | SrcMgr::CharacteristicKind FileCharacter, |
| 465 | unsigned PreallocatedID = 0, |
| 466 | unsigned Offset = 0) { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 467 | const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 468 | if (IR == 0) return FileID(); // Error opening file? |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 469 | return createFileID(IR, IncludePos, FileCharacter, PreallocatedID, Offset); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 470 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 471 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 472 | /// createFileIDForMemBuffer - Create a new FileID that represents the |
| 473 | /// specified memory buffer. This does no caching of the buffer and takes |
| 474 | /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once. |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 475 | FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer, |
| 476 | unsigned PreallocatedID = 0, |
| 477 | unsigned Offset = 0) { |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 478 | return createFileID(createMemBufferContentCache(Buffer), SourceLocation(), |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 479 | SrcMgr::C_User, PreallocatedID, Offset); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 480 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 481 | |
Ted Kremenek | 1036b68 | 2007-12-19 23:48:45 +0000 | [diff] [blame] | 482 | /// createMainFileIDForMembuffer - Create the FileID for a memory buffer |
| 483 | /// that will represent the FileID for the main source. One example |
| 484 | /// of when this would be used is when the main source is read from STDIN. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 485 | FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) { |
| 486 | assert(MainFileID.isInvalid() && "MainFileID already set!"); |
Chris Lattner | 7697b5c | 2007-12-20 01:38:17 +0000 | [diff] [blame] | 487 | MainFileID = createFileIDForMemBuffer(Buffer); |
Ted Kremenek | 1036b68 | 2007-12-19 23:48:45 +0000 | [diff] [blame] | 488 | return MainFileID; |
| 489 | } |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 490 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 491 | /// createInstantiationLoc - Return a new SourceLocation that encodes the fact |
| 492 | /// that a token at Loc should actually be referenced from InstantiationLoc. |
| 493 | /// TokLength is the length of the token being instantiated. |
| 494 | SourceLocation createInstantiationLoc(SourceLocation Loc, |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 495 | SourceLocation InstantiationLocStart, |
| 496 | SourceLocation InstantiationLocEnd, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 497 | unsigned TokLength, |
| 498 | unsigned PreallocatedID = 0, |
| 499 | unsigned Offset = 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 500 | |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 501 | /// \brief Retrieve the memory buffer associated with the given file. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 502 | /// |
| 503 | /// \param Invalid If non-NULL, will be set \c true if an error |
| 504 | /// occurs while retrieving the memory buffer. |
| 505 | const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File, |
| 506 | bool *Invalid = 0); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 507 | |
| 508 | /// \brief Override the contents of the given source file by providing an |
| 509 | /// already-allocated buffer. |
| 510 | /// |
Dan Gohman | afbf5f8 | 2010-08-26 02:27:03 +0000 | [diff] [blame] | 511 | /// \param SourceFile the source file whose contents will be overriden. |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 512 | /// |
| 513 | /// \param Buffer the memory buffer whose contents will be used as the |
| 514 | /// data in the given source file. |
| 515 | /// |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 516 | /// \param DoNotFree If true, then the buffer will not be freed when the |
| 517 | /// source manager is destroyed. |
| 518 | /// |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 519 | /// \returns true if an error occurred, false otherwise. |
| 520 | bool overrideFileContents(const FileEntry *SourceFile, |
Douglas Gregor | f4f6c9d | 2010-07-26 21:36:20 +0000 | [diff] [blame] | 521 | const llvm::MemoryBuffer *Buffer, |
| 522 | bool DoNotFree = false); |
Douglas Gregor | 2968442 | 2009-12-02 06:49:09 +0000 | [diff] [blame] | 523 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 524 | //===--------------------------------------------------------------------===// |
| 525 | // FileID manipulation methods. |
| 526 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 527 | |
Daniel Dunbar | 2ffb14f | 2009-12-06 09:19:25 +0000 | [diff] [blame] | 528 | /// getBuffer - Return the buffer for the specified FileID. If there is an |
| 529 | /// error opening this buffer the first time, this manufactures a temporary |
| 530 | /// buffer and returns a non-empty error string. |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 531 | const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc, |
| 532 | bool *Invalid = 0) const { |
| 533 | return getSLocEntry(FID).getFile().getContentCache() |
| 534 | ->getBuffer(Diag, *this, Loc, Invalid); |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 535 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 536 | |
Chris Lattner | e127a0d | 2010-04-20 20:35:58 +0000 | [diff] [blame] | 537 | const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const { |
| 538 | return getSLocEntry(FID).getFile().getContentCache() |
| 539 | ->getBuffer(Diag, *this, SourceLocation(), Invalid); |
| 540 | } |
| 541 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 542 | /// getFileEntryForID - Returns the FileEntry record for the provided FileID. |
| 543 | const FileEntry *getFileEntryForID(FileID FID) const { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 544 | return getSLocEntry(FID).getFile().getContentCache()->Entry; |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 545 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 546 | |
Benjamin Kramer | ceafc4b | 2010-03-16 14:48:07 +0000 | [diff] [blame] | 547 | /// getBufferData - Return a StringRef to the source buffer data for the |
| 548 | /// specified FileID. |
| 549 | /// |
Douglas Gregor | f715ca1 | 2010-03-16 00:06:06 +0000 | [diff] [blame] | 550 | /// \param FID The file ID whose contents will be returned. |
| 551 | /// \param Invalid If non-NULL, will be set true if an error occurred. |
Benjamin Kramer | f6ac97b | 2010-03-16 14:14:31 +0000 | [diff] [blame] | 552 | llvm::StringRef getBufferData(FileID FID, bool *Invalid = 0) const; |
| 553 | |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 554 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 555 | //===--------------------------------------------------------------------===// |
| 556 | // SourceLocation manipulation methods. |
| 557 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 558 | |
Chris Lattner | 668ab1a | 2009-03-13 01:05:57 +0000 | [diff] [blame] | 559 | /// getFileID - Return the FileID for a SourceLocation. This is a very |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 560 | /// hot method that is used for all SourceManager queries that start with a |
| 561 | /// SourceLocation object. It is responsible for finding the entry in |
| 562 | /// SLocEntryTable which contains the specified location. |
| 563 | /// |
| 564 | FileID getFileID(SourceLocation SpellingLoc) const { |
| 565 | unsigned SLocOffset = SpellingLoc.getOffset(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 566 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 567 | // If our one-entry cache covers this offset, just return it. |
| 568 | if (isOffsetInFileID(LastFileIDLookup, SLocOffset)) |
| 569 | return LastFileIDLookup; |
| 570 | |
| 571 | return getFileIDSlow(SLocOffset); |
| 572 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 573 | |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 574 | /// getLocForStartOfFile - Return the source location corresponding to the |
| 575 | /// first byte of the specified file. |
| 576 | SourceLocation getLocForStartOfFile(FileID FID) const { |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 577 | assert(FID.ID < SLocEntryTable.size() && "FileID out of range"); |
| 578 | assert(getSLocEntry(FID).isFile() && "FileID is not a file"); |
| 579 | unsigned FileOffset = getSLocEntry(FID).getOffset(); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 580 | return SourceLocation::getFileLoc(FileOffset); |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 581 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 582 | |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 583 | /// getInstantiationLoc - Given a SourceLocation object, return the |
| 584 | /// instantiation location referenced by the ID. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 585 | SourceLocation getInstantiationLoc(SourceLocation Loc) const { |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 586 | // Handle the non-mapped case inline, defer to out of line code to handle |
| 587 | // instantiations. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 588 | if (Loc.isFileID()) return Loc; |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 589 | return getInstantiationLocSlowCase(Loc); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 590 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 591 | |
Chris Lattner | e7fb484 | 2009-02-15 20:52:18 +0000 | [diff] [blame] | 592 | /// getImmediateInstantiationRange - Loc is required to be an instantiation |
| 593 | /// location. Return the start/end of the instantiation information. |
| 594 | std::pair<SourceLocation,SourceLocation> |
| 595 | getImmediateInstantiationRange(SourceLocation Loc) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 596 | |
Chris Lattner | 6678133 | 2009-02-15 21:26:50 +0000 | [diff] [blame] | 597 | /// getInstantiationRange - Given a SourceLocation object, return the |
| 598 | /// range of tokens covered by the instantiation in the ultimate file. |
| 599 | std::pair<SourceLocation,SourceLocation> |
| 600 | getInstantiationRange(SourceLocation Loc) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 601 | |
| 602 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 603 | /// getSpellingLoc - Given a SourceLocation object, return the spelling |
| 604 | /// location referenced by the ID. This is the place where the characters |
| 605 | /// that make up the lexed token can be found. |
| 606 | SourceLocation getSpellingLoc(SourceLocation Loc) const { |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 607 | // Handle the non-mapped case inline, defer to out of line code to handle |
| 608 | // instantiations. |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 609 | if (Loc.isFileID()) return Loc; |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 610 | return getSpellingLocSlowCase(Loc); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 611 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 612 | |
Chris Lattner | 387616e | 2009-02-17 08:04:48 +0000 | [diff] [blame] | 613 | /// getImmediateSpellingLoc - Given a SourceLocation object, return the |
| 614 | /// spelling location referenced by the ID. This is the first level down |
| 615 | /// towards the place where the characters that make up the lexed token can be |
| 616 | /// found. This should not generally be used by clients. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 617 | SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 618 | |
| 619 | /// getDecomposedLoc - Decompose the specified location into a raw FileID + |
| 620 | /// Offset pair. The first element is the FileID, the second is the |
| 621 | /// offset from the start of the buffer of the location. |
| 622 | std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const { |
| 623 | FileID FID = getFileID(Loc); |
| 624 | return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset()); |
| 625 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 626 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 627 | /// getDecomposedInstantiationLoc - Decompose the specified location into a |
| 628 | /// raw FileID + Offset pair. If the location is an instantiation record, |
| 629 | /// walk through it until we find the final location instantiated. |
| 630 | std::pair<FileID, unsigned> |
| 631 | getDecomposedInstantiationLoc(SourceLocation Loc) const { |
| 632 | FileID FID = getFileID(Loc); |
| 633 | const SrcMgr::SLocEntry *E = &getSLocEntry(FID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 634 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 635 | unsigned Offset = Loc.getOffset()-E->getOffset(); |
| 636 | if (Loc.isFileID()) |
| 637 | return std::make_pair(FID, Offset); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 638 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 639 | return getDecomposedInstantiationLocSlowCase(E, Offset); |
| 640 | } |
| 641 | |
| 642 | /// getDecomposedSpellingLoc - Decompose the specified location into a raw |
| 643 | /// FileID + Offset pair. If the location is an instantiation record, walk |
| 644 | /// through it until we find its spelling record. |
| 645 | std::pair<FileID, unsigned> |
| 646 | getDecomposedSpellingLoc(SourceLocation Loc) const { |
| 647 | FileID FID = getFileID(Loc); |
| 648 | const SrcMgr::SLocEntry *E = &getSLocEntry(FID); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 649 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 650 | unsigned Offset = Loc.getOffset()-E->getOffset(); |
| 651 | if (Loc.isFileID()) |
| 652 | return std::make_pair(FID, Offset); |
| 653 | return getDecomposedSpellingLocSlowCase(E, Offset); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 654 | } |
| 655 | |
Chris Lattner | 52c2908 | 2009-01-27 06:27:13 +0000 | [diff] [blame] | 656 | /// getFileOffset - This method returns the offset from the start |
| 657 | /// of the file that the specified SourceLocation represents. This is not very |
| 658 | /// meaningful for a macro ID. |
| 659 | unsigned getFileOffset(SourceLocation SpellingLoc) const { |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 660 | return getDecomposedLoc(SpellingLoc).second; |
| 661 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 662 | |
| 663 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 664 | //===--------------------------------------------------------------------===// |
| 665 | // Queries about the code at a SourceLocation. |
| 666 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 667 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 668 | /// getCharacterData - Return a pointer to the start of the specified location |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 669 | /// in the appropriate spelling MemoryBuffer. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 670 | /// |
| 671 | /// \param Invalid If non-NULL, will be set \c true if an error occurs. |
| 672 | const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 673 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 674 | /// getColumnNumber - Return the column # for the specified file position. |
| 675 | /// This is significantly cheaper to compute than the line number. This |
| 676 | /// 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] | 677 | /// a file sloc, so you must choose a spelling or instantiation location |
| 678 | /// before calling this method. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 679 | unsigned getColumnNumber(FileID FID, unsigned FilePos, |
| 680 | bool *Invalid = 0) const; |
| 681 | unsigned getSpellingColumnNumber(SourceLocation Loc, |
| 682 | bool *Invalid = 0) const; |
| 683 | unsigned getInstantiationColumnNumber(SourceLocation Loc, |
| 684 | bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 685 | |
| 686 | |
Chris Lattner | df7c17a | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 687 | /// getLineNumber - Given a SourceLocation, return the spelling line number |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 688 | /// for the position indicated. This requires building and caching a table of |
| 689 | /// line offsets for the MemoryBuffer, so this is not cheap: use only when |
| 690 | /// about to emit a diagnostic. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 691 | unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 692 | |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 693 | unsigned getInstantiationLineNumber(SourceLocation Loc, |
| 694 | bool *Invalid = 0) const; |
| 695 | unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 696 | |
Chris Lattner | bff5c51 | 2009-02-17 08:39:06 +0000 | [diff] [blame] | 697 | /// Return the filename or buffer identifier of the buffer the location is in. |
| 698 | /// Note that this name does not respect #line directives. Use getPresumedLoc |
| 699 | /// for normal clients. |
Douglas Gregor | 50f6af7 | 2010-03-16 05:20:39 +0000 | [diff] [blame] | 700 | const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 701 | |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 702 | /// getFileCharacteristic - return the file characteristic of the specified |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 703 | /// source location, indicating whether this is a normal file, a system |
Chris Lattner | 6b30667 | 2009-02-04 05:33:01 +0000 | [diff] [blame] | 704 | /// header, or an "implicit extern C" system header. |
| 705 | /// |
| 706 | /// This state can be modified with flags on GNU linemarker directives like: |
| 707 | /// # 4 "foo.h" 3 |
| 708 | /// which changes all source locations in the current file after that to be |
| 709 | /// considered to be from a system header. |
| 710 | SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 711 | |
Chris Lattner | b9c3f96 | 2009-01-27 07:57:44 +0000 | [diff] [blame] | 712 | /// getPresumedLoc - This method returns the "presumed" location of a |
| 713 | /// SourceLocation specifies. A "presumed location" can be modified by #line |
| 714 | /// or GNU line marker directives. This provides a view on the data that a |
| 715 | /// user should see in diagnostics, for example. |
| 716 | /// |
| 717 | /// Note that a presumed location is always given as the instantiation point |
| 718 | /// of an instantiation location, not at the spelling location. |
| 719 | PresumedLoc getPresumedLoc(SourceLocation Loc) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 720 | |
Ted Kremenek | 9fd87b1 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 721 | /// isFromSameFile - Returns true if both SourceLocations correspond to |
| 722 | /// the same file. |
| 723 | bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const { |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 724 | return getFileID(Loc1) == getFileID(Loc2); |
Ted Kremenek | 9fd87b1 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 725 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 726 | |
Ted Kremenek | 9fd87b1 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 727 | /// isFromMainFile - Returns true if the file of provided SourceLocation is |
| 728 | /// the main file. |
| 729 | bool isFromMainFile(SourceLocation Loc) const { |
Chris Lattner | a11d617 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 730 | return getFileID(Loc) == getMainFileID(); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 731 | } |
| 732 | |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 733 | /// isInSystemHeader - Returns if a SourceLocation is in a system header. |
| 734 | bool isInSystemHeader(SourceLocation Loc) const { |
Chris Lattner | 0b9e736 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 735 | return getFileCharacteristic(Loc) != SrcMgr::C_User; |
Nico Weber | 7bfaaae | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 736 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 737 | |
Chris Lattner | 0d45658 | 2009-06-13 23:31:51 +0000 | [diff] [blame] | 738 | /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C" |
| 739 | /// system header. |
| 740 | bool isInExternCSystemHeader(SourceLocation Loc) const { |
| 741 | return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem; |
| 742 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 743 | |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 744 | //===--------------------------------------------------------------------===// |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 745 | // Line Table Manipulation Routines |
| 746 | //===--------------------------------------------------------------------===// |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 747 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 748 | /// getLineTableFilenameID - Return the uniqued ID for the specified filename. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 749 | /// |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 750 | unsigned getLineTableFilenameID(const char *Ptr, unsigned Len); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 751 | |
Chris Lattner | 4c4ea17 | 2009-02-03 21:52:55 +0000 | [diff] [blame] | 752 | /// AddLineNote - Add a line note to the line table for the FileID and offset |
| 753 | /// specified by Loc. If FilenameID is -1, it is considered to be |
| 754 | /// unspecified. |
| 755 | void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID); |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 756 | void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 757 | bool IsFileEntry, bool IsFileExit, |
Chris Lattner | 9d79eba | 2009-02-04 05:21:58 +0000 | [diff] [blame] | 758 | bool IsSystemHeader, bool IsExternCHeader); |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 759 | |
| 760 | /// \brief Determine if the source manager has a line table. |
| 761 | bool hasLineTable() const { return LineTable != 0; } |
| 762 | |
| 763 | /// \brief Retrieve the stored line table. |
| 764 | LineTableInfo &getLineTable(); |
| 765 | |
Chris Lattner | 5b9a504 | 2009-01-26 07:57:50 +0000 | [diff] [blame] | 766 | //===--------------------------------------------------------------------===// |
Chris Lattner | 06a062d | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 767 | // Other miscellaneous methods. |
| 768 | //===--------------------------------------------------------------------===// |
Argyrios Kyrtzidis | 10b46d2 | 2009-06-20 08:09:57 +0000 | [diff] [blame] | 769 | |
| 770 | /// \brief Get the source location for the given file:line:col triplet. |
| 771 | /// |
| 772 | /// If the source file is included multiple times, the source location will |
| 773 | /// be based upon the first inclusion. |
| 774 | SourceLocation getLocation(const FileEntry *SourceFile, |
| 775 | unsigned Line, unsigned Col) const; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 776 | |
Argyrios Kyrtzidis | 2aa03d5 | 2009-06-23 22:01:48 +0000 | [diff] [blame] | 777 | /// \brief Determines the order of 2 source locations in the translation unit. |
| 778 | /// |
| 779 | /// \returns true if LHS source location comes before RHS, false otherwise. |
| 780 | bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const; |
| 781 | |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 782 | // Iterators over FileInfos. |
Chris Lattner | 0d0bf8c | 2009-02-03 07:30:45 +0000 | [diff] [blame] | 783 | typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> |
| 784 | ::const_iterator fileinfo_iterator; |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 785 | fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); } |
| 786 | fileinfo_iterator fileinfo_end() const { return FileInfos.end(); } |
Douglas Gregor | d93256e | 2010-01-28 06:00:51 +0000 | [diff] [blame] | 787 | bool hasFileInfo(const FileEntry *File) const { |
| 788 | return FileInfos.find(File) != FileInfos.end(); |
| 789 | } |
Chris Lattner | c6fe32a | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 790 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 791 | /// PrintStats - Print statistics to stderr. |
| 792 | /// |
| 793 | void PrintStats() const; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 794 | |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 795 | unsigned sloc_entry_size() const { return SLocEntryTable.size(); } |
Kovarththanan Rajaratnam | 2d684c2 | 2010-03-15 17:31:29 +0000 | [diff] [blame] | 796 | |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 797 | // FIXME: Exposing this is a little gross; what we want is a good way |
Sebastian Redl | 3c7f413 | 2010-08-18 23:57:06 +0000 | [diff] [blame] | 798 | // to iterate the entries that were not defined in an AST file (or |
Ted Kremenek | 16b55a7 | 2010-01-26 19:31:51 +0000 | [diff] [blame] | 799 | // any other external source). |
| 800 | unsigned sloc_loaded_entry_size() const { return SLocEntryLoaded.size(); } |
Douglas Gregor | 14f7900 | 2009-04-10 03:52:48 +0000 | [diff] [blame] | 801 | |
Douglas Gregor | bdfe48a | 2009-10-16 22:46:09 +0000 | [diff] [blame] | 802 | const SrcMgr::SLocEntry &getSLocEntry(unsigned ID) const { |
| 803 | assert(ID < SLocEntryTable.size() && "Invalid id"); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 804 | if (ExternalSLocEntries && |
Douglas Gregor | bdfe48a | 2009-10-16 22:46:09 +0000 | [diff] [blame] | 805 | ID < SLocEntryLoaded.size() && |
| 806 | !SLocEntryLoaded[ID]) |
| 807 | ExternalSLocEntries->ReadSLocEntry(ID); |
| 808 | return SLocEntryTable[ID]; |
| 809 | } |
Kovarththanan Rajaratnam | 2d684c2 | 2010-03-15 17:31:29 +0000 | [diff] [blame] | 810 | |
| 811 | const SrcMgr::SLocEntry &getSLocEntry(FileID FID) const { |
Douglas Gregor | bdfe48a | 2009-10-16 22:46:09 +0000 | [diff] [blame] | 812 | return getSLocEntry(FID.ID); |
Douglas Gregor | bd94500 | 2009-04-13 16:31:14 +0000 | [diff] [blame] | 813 | } |
| 814 | |
Douglas Gregor | f60e991 | 2009-04-15 18:05:10 +0000 | [diff] [blame] | 815 | unsigned getNextOffset() const { return NextOffset; } |
| 816 | |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 817 | /// \brief Preallocate some number of source location entries, which |
| 818 | /// will be loaded as needed from the given external source. |
| 819 | void PreallocateSLocEntries(ExternalSLocEntrySource *Source, |
| 820 | unsigned NumSLocEntries, |
| 821 | unsigned NextOffset); |
| 822 | |
Douglas Gregor | 2bf1eb0 | 2009-04-27 21:28:04 +0000 | [diff] [blame] | 823 | /// \brief Clear out any preallocated source location entries that |
| 824 | /// haven't already been loaded. |
| 825 | void ClearPreallocatedSLocEntries(); |
| 826 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 827 | private: |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 828 | /// isOffsetInFileID - Return true if the specified FileID contains the |
| 829 | /// specified SourceLocation offset. This is a very hot method. |
| 830 | inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const { |
| 831 | const SrcMgr::SLocEntry &Entry = getSLocEntry(FID); |
| 832 | // If the entry is after the offset, it can't contain it. |
| 833 | if (SLocOffset < Entry.getOffset()) return false; |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 834 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 835 | // If this is the last entry than it does. Otherwise, the entry after it |
| 836 | // has to not include it. |
| 837 | if (FID.ID+1 == SLocEntryTable.size()) return true; |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 838 | |
| 839 | return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset(); |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 840 | } |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 841 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 842 | /// createFileID - Create a new fileID for the specified ContentCache and |
| 843 | /// include position. This works regardless of whether the ContentCache |
| 844 | /// corresponds to a file or some other input source. |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 845 | FileID createFileID(const SrcMgr::ContentCache* File, |
| 846 | SourceLocation IncludePos, |
Douglas Gregor | 7f94b0b | 2009-04-27 06:38:32 +0000 | [diff] [blame] | 847 | SrcMgr::CharacteristicKind DirCharacter, |
| 848 | unsigned PreallocatedID = 0, |
| 849 | unsigned Offset = 0); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 850 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 851 | const SrcMgr::ContentCache * |
| 852 | getOrCreateContentCache(const FileEntry *SourceFile); |
Ted Kremenek | c16c208 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 853 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 854 | /// createMemBufferContentCache - Create a new ContentCache for the specified |
| 855 | /// memory buffer. |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 856 | const SrcMgr::ContentCache* |
Chris Lattner | 2b2453a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 857 | createMemBufferContentCache(const llvm::MemoryBuffer *Buf); |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 858 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 859 | FileID getFileIDSlow(unsigned SLocOffset) const; |
| 860 | |
Chris Lattner | addb797 | 2009-01-26 20:04:19 +0000 | [diff] [blame] | 861 | SourceLocation getInstantiationLocSlowCase(SourceLocation Loc) const; |
| 862 | SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const; |
| 863 | |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 864 | std::pair<FileID, unsigned> |
| 865 | getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E, |
Mike Stump | 1eb4433 | 2009-09-09 15:08:12 +0000 | [diff] [blame] | 866 | unsigned Offset) const; |
Chris Lattner | de7aeef | 2009-01-26 00:43:02 +0000 | [diff] [blame] | 867 | std::pair<FileID, unsigned> |
| 868 | getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, |
| 869 | unsigned Offset) const; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 870 | }; |
| 871 | |
| 872 | |
| 873 | } // end namespace clang |
| 874 | |
| 875 | #endif |