Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 1 | //===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 959e5be | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +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" |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 18 | #include "llvm/Bitcode/SerializationFwd.h" |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 19 | #include <vector> |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 20 | #include <set> |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 21 | #include <list> |
| 22 | #include <cassert> |
| 23 | |
| 24 | namespace llvm { |
| 25 | class MemoryBuffer; |
| 26 | } |
| 27 | |
| 28 | namespace clang { |
| 29 | |
| 30 | class SourceManager; |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 31 | class FileManager; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 32 | class FileEntry; |
| 33 | class IdentifierTokenInfo; |
| 34 | |
Chris Lattner | 6f04406 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 35 | /// SrcMgr - Public enums and private classes that are part of the |
| 36 | /// SourceManager implementation. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 37 | /// |
| 38 | namespace SrcMgr { |
Chris Lattner | 7a4864e | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 39 | /// CharacteristicKind - This is used to represent whether a file or directory |
Chris Lattner | 6f04406 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 40 | /// holds normal user code, system code, or system code which is implicitly |
| 41 | /// 'extern "C"' in C++ mode. Entire directories can be tagged with this |
| 42 | /// (this is maintained by DirectoryLookup and friends) as can specific |
| 43 | /// FileIDInfos when a #pragma system_header is seen or various other cases. |
| 44 | /// |
Chris Lattner | 7a4864e | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 45 | enum CharacteristicKind { |
Chris Lattner | 6f04406 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 46 | C_User, C_System, C_ExternCSystem |
| 47 | }; |
| 48 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 49 | /// ContentCache - Once instance of this struct is kept for every file |
Chris Lattner | d2e0017 | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 50 | /// loaded or used. This object owns the MemoryBuffer object. |
Ted Kremenek | aa7dac1 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 51 | class ContentCache { |
| 52 | /// Buffer - The actual buffer containing the characters from the input |
| 53 | /// file. This is owned by the ContentCache object. |
Chris Lattner | ac49bb4 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 54 | mutable const llvm::MemoryBuffer *Buffer; |
Ted Kremenek | aa7dac1 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 55 | |
| 56 | public: |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 57 | /// Reference to the file entry. This reference does not own |
| 58 | /// the FileEntry object. It is possible for this to be NULL if |
| 59 | /// the ContentCache encapsulates an imaginary text buffer. |
Chris Lattner | ac49bb4 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 60 | const FileEntry *Entry; |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 62 | /// SourceLineCache - A new[]'d array of offsets for each source line. This |
Ted Kremenek | 6a18635 | 2007-12-04 18:59:28 +0000 | [diff] [blame] | 63 | /// is lazily computed. This is owned by the ContentCache object. |
Chris Lattner | ac49bb4 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 64 | unsigned *SourceLineCache; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 65 | |
Ted Kremenek | 6a18635 | 2007-12-04 18:59:28 +0000 | [diff] [blame] | 66 | /// NumLines - The number of lines in this ContentCache. This is only valid |
| 67 | /// if SourceLineCache is non-null. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 68 | unsigned NumLines; |
Ted Kremenek | aa7dac1 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 69 | |
| 70 | /// getBuffer - Returns the memory buffer for the associated content. |
Chris Lattner | ac49bb4 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 71 | const llvm::MemoryBuffer *getBuffer() const; |
Ted Kremenek | aa7dac1 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 72 | |
| 73 | /// getSize - Returns the size of the content encapsulated by this |
| 74 | /// ContentCache. This can be the size of the source file or the size of an |
| 75 | /// arbitrary scratch buffer. If the ContentCache encapsulates a source |
| 76 | /// file this size is retrieved from the file's FileEntry. |
| 77 | unsigned getSize() const; |
| 78 | |
| 79 | /// getSizeBytesMapped - Returns the number of bytes actually mapped for |
| 80 | /// this ContentCache. This can be 0 if the MemBuffer was not actually |
| 81 | /// instantiated. |
| 82 | unsigned getSizeBytesMapped() const; |
| 83 | |
Chris Lattner | ac49bb4 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 84 | void setBuffer(const llvm::MemoryBuffer *B) { |
Ted Kremenek | aa7dac1 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 85 | assert(!Buffer && "MemoryBuffer already set."); |
| 86 | Buffer = B; |
| 87 | } |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 88 | |
Chris Lattner | ac49bb4 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 89 | ContentCache(const FileEntry *e = NULL) |
Ted Kremenek | aa7dac1 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 90 | : Buffer(NULL), Entry(e), SourceLineCache(NULL), NumLines(0) {} |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 91 | |
| 92 | ~ContentCache(); |
Ted Kremenek | 7670cca | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 93 | |
| 94 | /// The copy ctor does not allow copies where source object has either |
| 95 | /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory |
| 96 | /// is not transfered, so this is a logical error. |
Chris Lattner | ac49bb4 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 97 | ContentCache(const ContentCache &RHS) : Buffer(NULL),SourceLineCache(NULL) { |
Ted Kremenek | 7670cca | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 98 | Entry = RHS.Entry; |
| 99 | |
| 100 | assert (RHS.Buffer == NULL && RHS.SourceLineCache == NULL |
| 101 | && "Passed ContentCache object cannot own a buffer."); |
| 102 | |
| 103 | NumLines = RHS.NumLines; |
| 104 | } |
| 105 | |
Ted Kremenek | 0ad06d1 | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 106 | /// Emit - Emit this ContentCache to Bitcode. |
Chris Lattner | ac49bb4 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 107 | void Emit(llvm::Serializer &S) const; |
Ted Kremenek | 0ad06d1 | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 108 | |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 109 | /// ReadToSourceManager - Reconstitute a ContentCache from Bitcode |
| 110 | // and store it in the specified SourceManager. |
Chris Lattner | ac49bb4 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 111 | static void ReadToSourceManager(llvm::Deserializer &D, SourceManager &SM, |
| 112 | FileManager *FMgr, std::vector<char> &Buf); |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 113 | |
Ted Kremenek | 7670cca | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 114 | private: |
| 115 | // Disable assignments. |
Chris Lattner | ac49bb4 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 116 | ContentCache &operator=(const ContentCache& RHS); |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 117 | }; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 118 | |
| 119 | /// FileIDInfo - Information about a FileID, basically just the logical file |
| 120 | /// that it represents and include stack information. A File SourceLocation |
| 121 | /// is a byte offset from the start of this. |
| 122 | /// |
| 123 | /// FileID's are used to compute the location of a character in memory as well |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 124 | /// as the instantiation source location, which can be differ from the |
| 125 | /// spelling location. It is different when #line's are active or when macros |
| 126 | /// have been expanded. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 127 | /// |
| 128 | /// Each FileID has include stack information, indicating where it came from. |
| 129 | /// For the primary translation unit, it comes from SourceLocation() aka 0. |
| 130 | /// This information encodes the #include chain that a token was instantiated |
| 131 | /// from. |
| 132 | /// |
Ted Kremenek | 7670cca | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 133 | /// FileIDInfos contain a "ContentCache *", describing the source file, |
| 134 | /// and a Chunk number, which allows a SourceLocation to index into very |
| 135 | /// large files (those which there are not enough FilePosBits to address). |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 136 | /// |
| 137 | struct FileIDInfo { |
| 138 | private: |
| 139 | /// IncludeLoc - The location of the #include that brought in this file. |
| 140 | /// This SourceLocation object has an invalid SLOC for the main file. |
| 141 | SourceLocation IncludeLoc; |
| 142 | |
| 143 | /// ChunkNo - Really large buffers are broken up into chunks that are |
| 144 | /// each (1 << SourceLocation::FilePosBits) in size. This specifies the |
| 145 | /// chunk number of this FileID. |
Chris Lattner | 7257ce2 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 146 | unsigned ChunkNo : 30; |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 147 | |
Chris Lattner | 7a4864e | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 148 | /// FileCharacteristic - This is an instance of CharacteristicKind, |
Chris Lattner | 7257ce2 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 149 | /// indicating whether this is a system header dir or not. |
Chris Lattner | 6f04406 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 150 | unsigned FileCharacteristic : 2; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 151 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 152 | /// Content - Information about the source buffer itself. |
Chris Lattner | ac49bb4 | 2009-01-17 03:54:16 +0000 | [diff] [blame] | 153 | const ContentCache *Content; |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 154 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 155 | public: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 156 | /// get - Return a FileIDInfo object. |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 157 | static FileIDInfo get(SourceLocation IL, unsigned CN, |
Chris Lattner | 6f04406 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 158 | const ContentCache *Con, |
Chris Lattner | 7a4864e | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 159 | CharacteristicKind FileCharacter) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 160 | FileIDInfo X; |
| 161 | X.IncludeLoc = IL; |
| 162 | X.ChunkNo = CN; |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 163 | X.Content = Con; |
Chris Lattner | 6f04406 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 164 | X.FileCharacteristic = FileCharacter; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 165 | return X; |
| 166 | } |
| 167 | |
| 168 | SourceLocation getIncludeLoc() const { return IncludeLoc; } |
| 169 | unsigned getChunkNo() const { return ChunkNo; } |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 170 | const ContentCache* getContentCache() const { return Content; } |
Chris Lattner | 7257ce2 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 6f04406 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 172 | /// getCharacteristic - Return whether this is a system header or not. |
Chris Lattner | 7a4864e | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 173 | CharacteristicKind getFileCharacteristic() const { |
| 174 | return (CharacteristicKind)FileCharacteristic; |
Chris Lattner | 6f04406 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 175 | } |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 176 | |
| 177 | /// Emit - Emit this FileIDInfo to Bitcode. |
| 178 | void Emit(llvm::Serializer& S) const; |
| 179 | |
| 180 | /// ReadVal - Reconstitute a FileIDInfo from Bitcode. |
| 181 | static FileIDInfo ReadVal(llvm::Deserializer& S); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 182 | }; |
| 183 | |
| 184 | /// MacroIDInfo - Macro SourceLocations refer to these records by their ID. |
| 185 | /// Each MacroIDInfo encodes the Instantiation location - where the macro was |
Chris Lattner | cdf600e | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 186 | /// instantiated, and the SpellingLoc - where the actual character data for |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 187 | /// the token came from. An actual macro SourceLocation stores deltas from |
| 188 | /// these positions. |
| 189 | class MacroIDInfo { |
Chris Lattner | 74f6701 | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 190 | SourceLocation InstantiationLoc, SpellingLoc; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 191 | public: |
Chris Lattner | 74f6701 | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 192 | SourceLocation getInstantiationLoc() const { return InstantiationLoc; } |
Chris Lattner | cdf600e | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 193 | SourceLocation getSpellingLoc() const { return SpellingLoc; } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 194 | |
Chris Lattner | b8a39d9 | 2007-11-09 23:59:17 +0000 | [diff] [blame] | 195 | /// get - Return a MacroID for a macro expansion. VL specifies |
Chris Lattner | cdf600e | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 196 | /// the instantiation location (where the macro is expanded), and SL |
| 197 | /// specifies the spelling location (where the characters from the token |
Chris Lattner | b8a39d9 | 2007-11-09 23:59:17 +0000 | [diff] [blame] | 198 | /// come from). Both VL and PL refer to normal File SLocs. |
Chris Lattner | cdf600e | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 199 | static MacroIDInfo get(SourceLocation VL, SourceLocation SL) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 200 | MacroIDInfo X; |
Chris Lattner | 74f6701 | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 201 | X.InstantiationLoc = VL; |
Chris Lattner | cdf600e | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 202 | X.SpellingLoc = SL; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 203 | return X; |
| 204 | } |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 205 | |
| 206 | /// Emit - Emit this MacroIDInfo to Bitcode. |
| 207 | void Emit(llvm::Serializer& S) const; |
| 208 | |
| 209 | /// ReadVal - Reconstitute a MacroIDInfo from Bitcode. |
| 210 | static MacroIDInfo ReadVal(llvm::Deserializer& S); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 211 | }; |
| 212 | } // end SrcMgr namespace. |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 213 | } // end clang namespace |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 214 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 215 | namespace std { |
| 216 | template <> struct less<clang::SrcMgr::ContentCache> { |
| 217 | inline bool operator()(const clang::SrcMgr::ContentCache& L, |
| 218 | const clang::SrcMgr::ContentCache& R) const { |
| 219 | return L.Entry < R.Entry; |
| 220 | } |
| 221 | }; |
| 222 | } // end std namespace |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 223 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 224 | namespace clang { |
| 225 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 226 | /// SourceManager - This file handles loading and caching of source files into |
| 227 | /// memory. This object owns the MemoryBuffer objects for all of the loaded |
| 228 | /// files and assigns unique FileID's for each unique #include chain. |
| 229 | /// |
| 230 | /// The SourceManager can be queried for information about SourceLocation |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 231 | /// objects, turning them into either spelling or instantiation locations. |
| 232 | /// Spelling locations represent where the bytes corresponding to a token came |
| 233 | /// from and instantiation locations represent where the location is in the |
| 234 | /// user's view. In the case of a macro expansion, for example, the spelling |
| 235 | /// location indicates where the expanded token came from and the instantiation |
| 236 | /// location specifies where it was expanded. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 237 | class SourceManager { |
| 238 | /// FileInfos - Memoized information about all of the files tracked by this |
Ted Kremenek | 7670cca | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 239 | /// SourceManager. This set allows us to merge ContentCache entries based |
| 240 | /// on their FileEntry*. All ContentCache objects will thus have unique, |
| 241 | /// non-null, FileEntry pointers. |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 242 | std::set<SrcMgr::ContentCache> FileInfos; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 243 | |
| 244 | /// MemBufferInfos - Information about various memory buffers that we have |
| 245 | /// read in. This is a list, instead of a vector, because we need pointers to |
Ted Kremenek | 6a18635 | 2007-12-04 18:59:28 +0000 | [diff] [blame] | 246 | /// the ContentCache objects to be stable. All FileEntry* within the |
Ted Kremenek | 7670cca | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 247 | /// stored ContentCache objects are NULL, as they do not refer to a file. |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 248 | std::list<SrcMgr::ContentCache> MemBufferInfos; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 249 | |
| 250 | /// FileIDs - Information about each FileID. FileID #0 is not valid, so all |
| 251 | /// entries are off by one. |
| 252 | std::vector<SrcMgr::FileIDInfo> FileIDs; |
| 253 | |
| 254 | /// MacroIDs - Information about each MacroID. |
| 255 | std::vector<SrcMgr::MacroIDInfo> MacroIDs; |
| 256 | |
| 257 | /// LastLineNo - These ivars serve as a cache used in the getLineNumber |
| 258 | /// method which is used to speedup getLineNumber calls to nearby locations. |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 259 | mutable FileID LastLineNoFileIDQuery; |
Chris Lattner | e9bf3e3 | 2008-11-18 06:51:15 +0000 | [diff] [blame] | 260 | mutable SrcMgr::ContentCache *LastLineNoContentCache; |
| 261 | mutable unsigned LastLineNoFilePos; |
| 262 | mutable unsigned LastLineNoResult; |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 263 | |
Ted Kremenek | 2578dd0 | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 264 | /// MainFileID - The file ID for the main source file of the translation unit. |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 265 | FileID MainFileID; |
Steve Naroff | 543c7c6 | 2008-02-02 00:10:46 +0000 | [diff] [blame] | 266 | |
| 267 | // SourceManager doesn't support copy construction. |
| 268 | explicit SourceManager(const SourceManager&); |
| 269 | void operator=(const SourceManager&); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 270 | public: |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 271 | SourceManager() {} |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 272 | ~SourceManager() {} |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 273 | |
| 274 | void clearIDTables() { |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 275 | MainFileID = FileID(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 276 | FileIDs.clear(); |
| 277 | MacroIDs.clear(); |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 278 | LastLineNoFileIDQuery = FileID(); |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 279 | LastLineNoContentCache = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 280 | } |
Chris Lattner | d2e0017 | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 281 | |
| 282 | //===--------------------------------------------------------------------===// |
| 283 | // MainFileID creation and querying methods. |
| 284 | //===--------------------------------------------------------------------===// |
| 285 | |
Ted Kremenek | 2578dd0 | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 286 | /// getMainFileID - Returns the FileID of the main source file. |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 287 | FileID getMainFileID() const { return MainFileID; } |
Ted Kremenek | 2578dd0 | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 288 | |
Chris Lattner | d2e0017 | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 289 | /// createMainFileID - Create the FileID for the main source file. |
| 290 | FileID createMainFileID(const FileEntry *SourceFile, |
| 291 | SourceLocation IncludePos) { |
| 292 | assert(MainFileID.isInvalid() && "MainFileID already set!"); |
| 293 | MainFileID = createFileID(SourceFile, IncludePos, SrcMgr::C_User); |
| 294 | return MainFileID; |
| 295 | } |
| 296 | |
| 297 | //===--------------------------------------------------------------------===// |
| 298 | // Methods to create new FileID's. |
| 299 | //===--------------------------------------------------------------------===// |
| 300 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 301 | /// createFileID - Create a new FileID that represents the specified file |
| 302 | /// being #included from the specified IncludePosition. This returns 0 on |
| 303 | /// error and translates NULL into standard input. |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 304 | FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, |
Chris Lattner | 10ea72b | 2009-01-19 07:30:29 +0000 | [diff] [blame] | 305 | SrcMgr::CharacteristicKind FileCharacter) { |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 306 | const SrcMgr::ContentCache *IR = getContentCache(SourceFile); |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 307 | if (IR == 0) return FileID(); // Error opening file? |
Chris Lattner | 6f04406 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 308 | return createFileID(IR, IncludePos, FileCharacter); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 309 | } |
| 310 | |
| 311 | /// createFileIDForMemBuffer - Create a new FileID that represents the |
| 312 | /// specified memory buffer. This does no caching of the buffer and takes |
| 313 | /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once. |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 314 | FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) { |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 315 | return createFileID(createMemBufferContentCache(Buffer), SourceLocation(), |
Chris Lattner | 6f04406 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 316 | SrcMgr::C_User); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 317 | } |
| 318 | |
Ted Kremenek | 6d1d3ac | 2007-12-19 23:48:45 +0000 | [diff] [blame] | 319 | /// createMainFileIDForMembuffer - Create the FileID for a memory buffer |
| 320 | /// that will represent the FileID for the main source. One example |
| 321 | /// of when this would be used is when the main source is read from STDIN. |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 322 | FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) { |
| 323 | assert(MainFileID.isInvalid() && "MainFileID already set!"); |
Chris Lattner | 73bb782 | 2007-12-20 01:38:17 +0000 | [diff] [blame] | 324 | MainFileID = createFileIDForMemBuffer(Buffer); |
Ted Kremenek | 6d1d3ac | 2007-12-19 23:48:45 +0000 | [diff] [blame] | 325 | return MainFileID; |
| 326 | } |
Chris Lattner | d2e0017 | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 327 | |
| 328 | //===--------------------------------------------------------------------===// |
| 329 | // FileID manipulation methods. |
| 330 | //===--------------------------------------------------------------------===// |
| 331 | |
| 332 | /// getBuffer - Return the buffer for the specified FileID. |
| 333 | /// |
| 334 | const llvm::MemoryBuffer *getBuffer(FileID FID) const { |
| 335 | return getContentCache(FID)->getBuffer(); |
| 336 | } |
| 337 | |
| 338 | /// getFileEntryForID - Returns the FileEntry record for the provided FileID. |
| 339 | const FileEntry *getFileEntryForID(FileID FID) const { |
| 340 | return getContentCache(FID)->Entry; |
| 341 | } |
| 342 | |
| 343 | /// getBufferData - Return a pointer to the start and end of the source buffer |
| 344 | /// data for the specified FileID. |
| 345 | std::pair<const char*, const char*> getBufferData(FileID FID) const; |
| 346 | |
| 347 | |
| 348 | //===--------------------------------------------------------------------===// |
| 349 | // SourceLocation manipulation methods. |
| 350 | //===--------------------------------------------------------------------===// |
Ted Kremenek | 6d1d3ac | 2007-12-19 23:48:45 +0000 | [diff] [blame] | 351 | |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 352 | /// getLocForStartOfFile - Return the source location corresponding to the |
| 353 | /// first byte of the specified file. |
| 354 | SourceLocation getLocForStartOfFile(FileID FID) const { |
| 355 | return SourceLocation::getFileLoc(FID.ID, 0); |
| 356 | } |
| 357 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 358 | /// getInstantiationLoc - Return a new SourceLocation that encodes the fact |
| 359 | /// that a token at Loc should actually be referenced from InstantiationLoc. |
| 360 | SourceLocation getInstantiationLoc(SourceLocation Loc, |
| 361 | SourceLocation InstantiationLoc); |
| 362 | |
Chris Lattner | d2e0017 | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 363 | /// getIncludeLoc - Return the location of the #include for the specified |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 364 | /// SourceLocation. If this is a macro expansion, this transparently figures |
| 365 | /// out which file includes the file being expanded into. |
| 366 | SourceLocation getIncludeLoc(SourceLocation ID) const { |
Chris Lattner | 10aaf53 | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 367 | return getFIDInfo(getInstantiationLoc(ID).getChunkID())->getIncludeLoc(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 368 | } |
| 369 | |
| 370 | /// getCharacterData - Return a pointer to the start of the specified location |
| 371 | /// in the appropriate MemoryBuffer. |
| 372 | const char *getCharacterData(SourceLocation SL) const; |
| 373 | |
| 374 | /// getColumnNumber - Return the column # for the specified file position. |
| 375 | /// This is significantly cheaper to compute than the line number. This |
| 376 | /// returns zero if the column number isn't known. This may only be called on |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 377 | /// a file sloc, so you must choose a spelling or instantiation location |
| 378 | /// before calling this method. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 379 | unsigned getColumnNumber(SourceLocation Loc) const; |
| 380 | |
Chris Lattner | cdf600e | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 381 | unsigned getSpellingColumnNumber(SourceLocation Loc) const { |
| 382 | return getColumnNumber(getSpellingLoc(Loc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 383 | } |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 384 | unsigned getInstantiationColumnNumber(SourceLocation Loc) const { |
| 385 | return getColumnNumber(getInstantiationLoc(Loc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 386 | } |
| 387 | |
| 388 | |
Chris Lattner | cdf600e | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 389 | /// getLineNumber - Given a SourceLocation, return the spelling line number |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 390 | /// for the position indicated. This requires building and caching a table of |
| 391 | /// line offsets for the MemoryBuffer, so this is not cheap: use only when |
| 392 | /// about to emit a diagnostic. |
Chris Lattner | e9bf3e3 | 2008-11-18 06:51:15 +0000 | [diff] [blame] | 393 | unsigned getLineNumber(SourceLocation Loc) const; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 394 | |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 395 | unsigned getInstantiationLineNumber(SourceLocation Loc) const { |
| 396 | return getLineNumber(getInstantiationLoc(Loc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 397 | } |
Chris Lattner | cdf600e | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 398 | unsigned getSpellingLineNumber(SourceLocation Loc) const { |
| 399 | return getLineNumber(getSpellingLoc(Loc)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | /// getSourceName - This method returns the name of the file or buffer that |
| 403 | /// the SourceLocation specifies. This can be modified with #line directives, |
| 404 | /// etc. |
Chris Lattner | 37f04117 | 2007-08-30 05:59:30 +0000 | [diff] [blame] | 405 | const char *getSourceName(SourceLocation Loc) const; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 406 | |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 407 | /// Given a SourceLocation object, return the instantiation location |
| 408 | /// referenced by the ID. |
| 409 | SourceLocation getInstantiationLoc(SourceLocation Loc) const { |
Chris Lattner | cdf600e | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 410 | // File locations work. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 411 | if (Loc.isFileID()) return Loc; |
Chris Lattner | cdf600e | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 412 | |
Chris Lattner | 74f6701 | 2009-01-16 07:15:35 +0000 | [diff] [blame] | 413 | return MacroIDs[Loc.getMacroID()].getInstantiationLoc(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 414 | } |
| 415 | |
Chris Lattner | cdf600e | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 416 | /// getSpellingLoc - Given a SourceLocation object, return the spelling |
| 417 | /// location referenced by the ID. This is the place where the characters |
| 418 | /// that make up the lexed token can be found. |
| 419 | SourceLocation getSpellingLoc(SourceLocation Loc) const { |
| 420 | // File locations work! |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 421 | if (Loc.isFileID()) return Loc; |
| 422 | |
Chris Lattner | cdf600e | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 423 | // Look up the macro token's spelling location. |
| 424 | SourceLocation PLoc = MacroIDs[Loc.getMacroID()].getSpellingLoc(); |
| 425 | return PLoc.getFileLocWithOffset(Loc.getMacroSpellingOffs()); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 426 | } |
| 427 | |
Chris Lattner | 136df56 | 2007-10-12 20:24:19 +0000 | [diff] [blame] | 428 | /// getDecomposedFileLoc - Decompose the specified file location into a raw |
| 429 | /// FileID + Offset pair. The first element is the FileID, the second is the |
| 430 | /// offset from the start of the buffer of the location. |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 431 | std::pair<FileID, unsigned> getDecomposedFileLoc(SourceLocation Loc) const { |
Chris Lattner | 136df56 | 2007-10-12 20:24:19 +0000 | [diff] [blame] | 432 | assert(Loc.isFileID() && "Isn't a File SourceLocation"); |
| 433 | |
| 434 | // TODO: Add a flag "is first chunk" to SLOC. |
Chris Lattner | 10aaf53 | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 435 | const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(Loc.getChunkID()); |
Chris Lattner | 136df56 | 2007-10-12 20:24:19 +0000 | [diff] [blame] | 436 | |
| 437 | // If this file has been split up into chunks, factor in the chunk number |
| 438 | // that the FileID references. |
| 439 | unsigned ChunkNo = FIDInfo->getChunkNo(); |
| 440 | unsigned Offset = Loc.getRawFilePos(); |
| 441 | Offset += (ChunkNo << SourceLocation::FilePosBits); |
Nico Weber | 2bbdcae | 2008-08-09 22:13:42 +0000 | [diff] [blame] | 442 | |
Chris Lattner | 10aaf53 | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 443 | assert(Loc.getChunkID() >= ChunkNo && "Unexpected offset"); |
Chris Lattner | 136df56 | 2007-10-12 20:24:19 +0000 | [diff] [blame] | 444 | |
Chris Lattner | 10aaf53 | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 445 | return std::make_pair(FileID::Create(Loc.getChunkID()-ChunkNo), Offset); |
Chris Lattner | 136df56 | 2007-10-12 20:24:19 +0000 | [diff] [blame] | 446 | } |
Chris Lattner | 1737bd5 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 447 | |
| 448 | /// getFileID - Return the FileID for a SourceLocation. |
| 449 | /// |
| 450 | FileID getFileID(SourceLocation SpellingLoc) const { |
| 451 | return getDecomposedFileLoc(SpellingLoc).first; |
| 452 | } |
| 453 | |
Ted Kremenek | a0bc14d | 2008-04-08 21:26:35 +0000 | [diff] [blame] | 454 | /// getFullFilePos - This (efficient) method returns the offset from the start |
Chris Lattner | cdf600e | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 455 | /// of the file that the specified spelling SourceLocation represents. This |
Chris Lattner | 18c8dc0 | 2009-01-16 07:36:28 +0000 | [diff] [blame] | 456 | /// returns the location of the actual character data, not the instantiation |
Ted Kremenek | a0bc14d | 2008-04-08 21:26:35 +0000 | [diff] [blame] | 457 | /// position. |
Chris Lattner | cdf600e | 2009-01-16 07:00:02 +0000 | [diff] [blame] | 458 | unsigned getFullFilePos(SourceLocation SpellingLoc) const { |
| 459 | return getDecomposedFileLoc(SpellingLoc).second; |
Ted Kremenek | a0bc14d | 2008-04-08 21:26:35 +0000 | [diff] [blame] | 460 | } |
Chris Lattner | 136df56 | 2007-10-12 20:24:19 +0000 | [diff] [blame] | 461 | |
Ted Kremenek | 81019d7 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 462 | /// isFromSameFile - Returns true if both SourceLocations correspond to |
| 463 | /// the same file. |
| 464 | bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const { |
Chris Lattner | 1737bd5 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 465 | return getFileID(Loc1) == getFileID(Loc2); |
Ted Kremenek | 81019d7 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 466 | } |
| 467 | |
| 468 | /// isFromMainFile - Returns true if the file of provided SourceLocation is |
| 469 | /// the main file. |
| 470 | bool isFromMainFile(SourceLocation Loc) const { |
Chris Lattner | 1737bd5 | 2009-01-19 07:46:45 +0000 | [diff] [blame] | 471 | return getFileID(Loc) == getMainFileID(); |
Ted Kremenek | 81019d7 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 472 | } |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 473 | |
| 474 | /// isInSystemHeader - Returns if a SourceLocation is in a system header. |
| 475 | bool isInSystemHeader(SourceLocation Loc) const { |
Chris Lattner | 6f04406 | 2008-09-26 21:18:42 +0000 | [diff] [blame] | 476 | return getFileCharacteristic(Loc) != SrcMgr::C_User; |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 477 | } |
Chris Lattner | 7a4864e | 2008-10-27 01:19:25 +0000 | [diff] [blame] | 478 | SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const { |
Chris Lattner | 10aaf53 | 2009-01-17 08:45:21 +0000 | [diff] [blame] | 479 | return getFIDInfo(getSpellingLoc(Loc).getChunkID()) |
| 480 | ->getFileCharacteristic(); |
Chris Lattner | 7257ce2 | 2008-09-26 20:12:23 +0000 | [diff] [blame] | 481 | } |
Chris Lattner | d2e0017 | 2009-01-19 08:02:45 +0000 | [diff] [blame] | 482 | |
| 483 | //===--------------------------------------------------------------------===// |
| 484 | // Other miscellaneous methods. |
| 485 | //===--------------------------------------------------------------------===// |
Ted Kremenek | d111a25 | 2008-11-19 22:41:46 +0000 | [diff] [blame] | 486 | |
Chris Lattner | ef63fd5 | 2009-01-17 03:48:08 +0000 | [diff] [blame] | 487 | // Iterators over FileInfos. |
| 488 | typedef std::set<SrcMgr::ContentCache>::const_iterator fileinfo_iterator; |
| 489 | fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); } |
| 490 | fileinfo_iterator fileinfo_end() const { return FileInfos.end(); } |
| 491 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 492 | /// PrintStats - Print statistics to stderr. |
| 493 | /// |
| 494 | void PrintStats() const; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 495 | |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 496 | /// Emit - Emit this SourceManager to Bitcode. |
| 497 | void Emit(llvm::Serializer& S) const; |
| 498 | |
| 499 | /// Read - Reconstitute a SourceManager from Bitcode. |
Ted Kremenek | bc54abf | 2007-12-05 00:19:51 +0000 | [diff] [blame] | 500 | static SourceManager* CreateAndRegister(llvm::Deserializer& S, |
| 501 | FileManager &FMgr); |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 502 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 503 | private: |
Cédric Venet | 0009c94 | 2008-06-26 12:50:52 +0000 | [diff] [blame] | 504 | friend struct SrcMgr::ContentCache; // Used for deserialization. |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 505 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 506 | /// createFileID - Create a new fileID for the specified ContentCache and |
| 507 | /// include position. This works regardless of whether the ContentCache |
| 508 | /// corresponds to a file or some other input source. |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 509 | FileID createFileID(const SrcMgr::ContentCache* File, |
| 510 | SourceLocation IncludePos, |
| 511 | SrcMgr::CharacteristicKind DirCharacter); |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 512 | |
| 513 | /// getContentCache - Create or return a cached ContentCache for the specified |
| 514 | /// file. This returns null on failure. |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 515 | const SrcMgr::ContentCache* getContentCache(const FileEntry *SourceFile); |
Ted Kremenek | aa7dac1 | 2009-01-06 01:55:26 +0000 | [diff] [blame] | 516 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 517 | /// createMemBufferContentCache - Create a new ContentCache for the specified |
| 518 | /// memory buffer. |
| 519 | const SrcMgr::ContentCache* |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 520 | createMemBufferContentCache(const llvm::MemoryBuffer *Buf); |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 521 | |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 522 | const SrcMgr::FileIDInfo *getFIDInfo(unsigned FID) const { |
| 523 | assert(FID-1 < FileIDs.size() && "Invalid FileID!"); |
| 524 | return &FileIDs[FID-1]; |
| 525 | } |
| 526 | const SrcMgr::FileIDInfo *getFIDInfo(FileID FID) const { |
| 527 | return getFIDInfo(FID.ID); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 528 | } |
| 529 | |
Chris Lattner | f4f776a | 2009-01-17 06:22:33 +0000 | [diff] [blame] | 530 | const SrcMgr::ContentCache *getContentCache(FileID FID) const { |
| 531 | return getContentCache(getFIDInfo(FID.ID)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 532 | } |
| 533 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 534 | /// Return the ContentCache structure for the specified FileID. |
| 535 | /// This is always the physical reference for the ID. |
| 536 | const SrcMgr::ContentCache* |
| 537 | getContentCache(const SrcMgr::FileIDInfo* FIDInfo) const { |
| 538 | return FIDInfo->getContentCache(); |
| 539 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 540 | }; |
| 541 | |
| 542 | |
| 543 | } // end namespace clang |
| 544 | |
| 545 | #endif |