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 | // |
| 5 | // This file was developed by Chris Lattner and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 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 | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 18 | #include "llvm/Bitcode/SerializationFwd.h" |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 19 | #include <vector> |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 20 | #include <set> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 21 | #include <list> |
Chris Lattner | 9dc62f0 | 2007-07-12 15:32:57 +0000 | [diff] [blame] | 22 | #include <cassert> |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 23 | |
| 24 | namespace llvm { |
| 25 | class MemoryBuffer; |
| 26 | } |
| 27 | |
| 28 | namespace clang { |
| 29 | |
| 30 | class SourceManager; |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 31 | class FileManager; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 32 | class FileEntry; |
| 33 | class IdentifierTokenInfo; |
| 34 | |
| 35 | /// SrcMgr - Private classes that are part of the SourceManager implementation. |
| 36 | /// |
| 37 | namespace SrcMgr { |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 38 | /// ContentCache - Once instance of this struct is kept for every file |
| 39 | /// loaded or used. This object owns the MemoryBuffer object. |
| 40 | struct ContentCache { |
| 41 | /// Reference to the file entry. This reference does not own |
| 42 | /// the FileEntry object. It is possible for this to be NULL if |
| 43 | /// the ContentCache encapsulates an imaginary text buffer. |
| 44 | const FileEntry* Entry; |
| 45 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 46 | /// Buffer - The actual buffer containing the characters from the input |
Ted Kremenek | b6427f8 | 2007-12-04 18:59:28 +0000 | [diff] [blame] | 47 | /// file. This is owned by the ContentCache object. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 48 | const llvm::MemoryBuffer* Buffer; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 49 | |
| 50 | /// SourceLineCache - A new[]'d array of offsets for each source line. This |
Ted Kremenek | b6427f8 | 2007-12-04 18:59:28 +0000 | [diff] [blame] | 51 | /// is lazily computed. This is owned by the ContentCache object. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 52 | unsigned* SourceLineCache; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 53 | |
Ted Kremenek | b6427f8 | 2007-12-04 18:59:28 +0000 | [diff] [blame] | 54 | /// NumLines - The number of lines in this ContentCache. This is only valid |
| 55 | /// if SourceLineCache is non-null. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 56 | unsigned NumLines; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 57 | |
| 58 | ContentCache(const FileEntry* e = NULL) |
| 59 | : Entry(e), Buffer(NULL), SourceLineCache(NULL), NumLines(0) {} |
| 60 | |
| 61 | ~ContentCache(); |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 62 | |
| 63 | /// The copy ctor does not allow copies where source object has either |
| 64 | /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory |
| 65 | /// is not transfered, so this is a logical error. |
| 66 | ContentCache(const ContentCache& RHS) : Buffer(NULL),SourceLineCache(NULL) { |
| 67 | Entry = RHS.Entry; |
| 68 | |
| 69 | assert (RHS.Buffer == NULL && RHS.SourceLineCache == NULL |
| 70 | && "Passed ContentCache object cannot own a buffer."); |
| 71 | |
| 72 | NumLines = RHS.NumLines; |
| 73 | } |
| 74 | |
Ted Kremenek | e21272f | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 75 | /// Emit - Emit this ContentCache to Bitcode. |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 76 | void Emit(llvm::Serializer& S) const; |
Ted Kremenek | e21272f | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 78 | /// ReadToSourceManager - Reconstitute a ContentCache from Bitcode |
| 79 | // and store it in the specified SourceManager. |
| 80 | static void ReadToSourceManager(llvm::Deserializer& D, SourceManager& SMgr, |
| 81 | FileManager* FMgr, std::vector<char>& Buf); |
| 82 | |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 83 | private: |
| 84 | // Disable assignments. |
| 85 | ContentCache& operator=(const ContentCache& RHS); |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 86 | }; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 87 | |
| 88 | /// FileIDInfo - Information about a FileID, basically just the logical file |
Chris Lattner | 31bb8be | 2007-07-20 18:00:12 +0000 | [diff] [blame] | 89 | /// that it represents and include stack information. A File SourceLocation |
| 90 | /// is a byte offset from the start of this. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 91 | /// |
| 92 | /// FileID's are used to compute the location of a character in memory as well |
| 93 | /// as the logical source location, which can be differ from the physical |
| 94 | /// location. It is different when #line's are active or when macros have |
| 95 | /// been expanded. |
| 96 | /// |
| 97 | /// Each FileID has include stack information, indicating where it came from. |
| 98 | /// For the primary translation unit, it comes from SourceLocation() aka 0. |
Chris Lattner | 31bb8be | 2007-07-20 18:00:12 +0000 | [diff] [blame] | 99 | /// This information encodes the #include chain that a token was instantiated |
| 100 | /// from. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 101 | /// |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 102 | /// FileIDInfos contain a "ContentCache *", describing the source file, |
| 103 | /// and a Chunk number, which allows a SourceLocation to index into very |
| 104 | /// large files (those which there are not enough FilePosBits to address). |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 105 | /// |
| 106 | struct FileIDInfo { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 107 | private: |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 108 | /// IncludeLoc - The location of the #include that brought in this file. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 109 | /// This SourceLocation object has an invalid SLOC for the main file. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 110 | SourceLocation IncludeLoc; |
| 111 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 112 | /// ChunkNo - Really large buffers are broken up into chunks that are |
| 113 | /// each (1 << SourceLocation::FilePosBits) in size. This specifies the |
| 114 | /// chunk number of this FileID. |
| 115 | unsigned ChunkNo; |
| 116 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 117 | /// Content - Information about the source buffer itself. |
| 118 | const ContentCache* Content; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 119 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 120 | public: |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 121 | /// get - Return a FileIDInfo object. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 122 | static FileIDInfo get(SourceLocation IL, unsigned CN, |
| 123 | const ContentCache *Con) { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 124 | FileIDInfo X; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 125 | X.IncludeLoc = IL; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 126 | X.ChunkNo = CN; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 127 | X.Content = Con; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 128 | return X; |
| 129 | } |
| 130 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 131 | SourceLocation getIncludeLoc() const { return IncludeLoc; } |
| 132 | unsigned getChunkNo() const { return ChunkNo; } |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 133 | const ContentCache* getContentCache() const { return Content; } |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 134 | |
| 135 | /// Emit - Emit this FileIDInfo to Bitcode. |
| 136 | void Emit(llvm::Serializer& S) const; |
| 137 | |
| 138 | /// ReadVal - Reconstitute a FileIDInfo from Bitcode. |
| 139 | static FileIDInfo ReadVal(llvm::Deserializer& S); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 140 | }; |
| 141 | |
Chris Lattner | 31bb8be | 2007-07-20 18:00:12 +0000 | [diff] [blame] | 142 | /// MacroIDInfo - Macro SourceLocations refer to these records by their ID. |
| 143 | /// Each MacroIDInfo encodes the Instantiation location - where the macro was |
| 144 | /// instantiated, and the PhysicalLoc - where the actual character data for |
| 145 | /// the token came from. An actual macro SourceLocation stores deltas from |
| 146 | /// these positions. |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 147 | class MacroIDInfo { |
Chris Lattner | 18807d2 | 2007-11-09 23:59:17 +0000 | [diff] [blame] | 148 | SourceLocation VirtualLoc, PhysicalLoc; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 149 | public: |
Chris Lattner | 18807d2 | 2007-11-09 23:59:17 +0000 | [diff] [blame] | 150 | SourceLocation getVirtualLoc() const { return VirtualLoc; } |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 151 | SourceLocation getPhysicalLoc() const { return PhysicalLoc; } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 152 | |
Chris Lattner | 18807d2 | 2007-11-09 23:59:17 +0000 | [diff] [blame] | 153 | /// get - Return a MacroID for a macro expansion. VL specifies |
| 154 | /// the instantiation location (where the macro is expanded), and PL |
| 155 | /// specifies the physical location (where the characters from the token |
| 156 | /// come from). Both VL and PL refer to normal File SLocs. |
| 157 | static MacroIDInfo get(SourceLocation VL, SourceLocation PL) { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 158 | MacroIDInfo X; |
Chris Lattner | 18807d2 | 2007-11-09 23:59:17 +0000 | [diff] [blame] | 159 | X.VirtualLoc = VL; |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 160 | X.PhysicalLoc = PL; |
| 161 | return X; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 162 | } |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 163 | |
| 164 | /// Emit - Emit this MacroIDInfo to Bitcode. |
| 165 | void Emit(llvm::Serializer& S) const; |
| 166 | |
| 167 | /// ReadVal - Reconstitute a MacroIDInfo from Bitcode. |
| 168 | static MacroIDInfo ReadVal(llvm::Deserializer& S); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 169 | }; |
| 170 | } // end SrcMgr namespace. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 171 | } // end clang namespace |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 172 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 173 | namespace std { |
| 174 | template <> struct less<clang::SrcMgr::ContentCache> { |
| 175 | inline bool operator()(const clang::SrcMgr::ContentCache& L, |
| 176 | const clang::SrcMgr::ContentCache& R) const { |
| 177 | return L.Entry < R.Entry; |
| 178 | } |
| 179 | }; |
| 180 | } // end std namespace |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 181 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 182 | namespace clang { |
| 183 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 184 | /// SourceManager - This file handles loading and caching of source files into |
| 185 | /// memory. This object owns the MemoryBuffer objects for all of the loaded |
| 186 | /// files and assigns unique FileID's for each unique #include chain. |
| 187 | /// |
| 188 | /// The SourceManager can be queried for information about SourceLocation |
| 189 | /// objects, turning them into either physical or logical locations. Physical |
| 190 | /// locations represent where the bytes corresponding to a token came from and |
| 191 | /// logical locations represent where the location is in the user's view. In |
| 192 | /// the case of a macro expansion, for example, the physical location indicates |
| 193 | /// where the expanded token came from and the logical location specifies where |
| 194 | /// it was expanded. Logical locations are also influenced by #line directives, |
| 195 | /// etc. |
| 196 | class SourceManager { |
| 197 | /// FileInfos - Memoized information about all of the files tracked by this |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 198 | /// SourceManager. This set allows us to merge ContentCache entries based |
| 199 | /// on their FileEntry*. All ContentCache objects will thus have unique, |
| 200 | /// non-null, FileEntry pointers. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 201 | std::set<SrcMgr::ContentCache> FileInfos; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 202 | |
| 203 | /// MemBufferInfos - Information about various memory buffers that we have |
| 204 | /// read in. This is a list, instead of a vector, because we need pointers to |
Ted Kremenek | b6427f8 | 2007-12-04 18:59:28 +0000 | [diff] [blame] | 205 | /// the ContentCache objects to be stable. All FileEntry* within the |
Ted Kremenek | 0d892d8 | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 206 | /// stored ContentCache objects are NULL, as they do not refer to a file. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 207 | std::list<SrcMgr::ContentCache> MemBufferInfos; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 208 | |
| 209 | /// FileIDs - Information about each FileID. FileID #0 is not valid, so all |
| 210 | /// entries are off by one. |
| 211 | std::vector<SrcMgr::FileIDInfo> FileIDs; |
| 212 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 213 | /// MacroIDs - Information about each MacroID. |
| 214 | std::vector<SrcMgr::MacroIDInfo> MacroIDs; |
| 215 | |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 216 | /// LastLineNo - These ivars serve as a cache used in the getLineNumber |
| 217 | /// method which is used to speedup getLineNumber calls to nearby locations. |
| 218 | unsigned LastLineNoFileIDQuery; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 219 | SrcMgr::ContentCache *LastLineNoContentCache; |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 220 | unsigned LastLineNoFilePos; |
| 221 | unsigned LastLineNoResult; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 222 | |
Ted Kremenek | 76edd0e | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 223 | /// MainFileID - The file ID for the main source file of the translation unit. |
| 224 | unsigned MainFileID; |
| 225 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 226 | public: |
Ted Kremenek | 76edd0e | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 227 | SourceManager() : LastLineNoFileIDQuery(~0U), MainFileID(0) {} |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 228 | ~SourceManager() {} |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 229 | |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 230 | void clearIDTables() { |
| 231 | FileIDs.clear(); |
| 232 | MacroIDs.clear(); |
Chris Lattner | 5e36a7a | 2007-07-24 05:57:19 +0000 | [diff] [blame] | 233 | LastLineNoFileIDQuery = ~0U; |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 234 | LastLineNoContentCache = 0; |
Chris Lattner | bd24776 | 2007-07-22 06:05:44 +0000 | [diff] [blame] | 235 | } |
| 236 | |
Ted Kremenek | 76edd0e | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 237 | /// getMainFileID - Returns the FileID of the main source file. |
| 238 | unsigned getMainFileID() const { return MainFileID; } |
| 239 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 240 | /// createFileID - Create a new FileID that represents the specified file |
| 241 | /// being #included from the specified IncludePosition. This returns 0 on |
| 242 | /// error and translates NULL into standard input. |
| 243 | unsigned createFileID(const FileEntry *SourceFile, SourceLocation IncludePos){ |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 244 | const SrcMgr::ContentCache *IR = getContentCache(SourceFile); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 245 | if (IR == 0) return 0; // Error opening file? |
| 246 | return createFileID(IR, IncludePos); |
| 247 | } |
| 248 | |
Ted Kremenek | 1036b68 | 2007-12-19 23:48:45 +0000 | [diff] [blame^] | 249 | /// createMainFileID - Create the FileID for the main source file. |
| 250 | unsigned createMainFileID(const FileEntry *SourceFile, |
| 251 | SourceLocation IncludePos) { |
| 252 | |
| 253 | assert (MainFileID == 0 && "MainFileID already set!"); |
| 254 | MainFileID = createFileID(SourceFile,IncludePos); |
| 255 | return MainFileID; |
| 256 | } |
| 257 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 258 | /// createFileIDForMemBuffer - Create a new FileID that represents the |
| 259 | /// specified memory buffer. This does no caching of the buffer and takes |
| 260 | /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once. |
| 261 | unsigned createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) { |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 262 | return createFileID(createMemBufferContentCache(Buffer), SourceLocation()); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Ted Kremenek | 1036b68 | 2007-12-19 23:48:45 +0000 | [diff] [blame^] | 265 | /// createMainFileIDForMembuffer - Create the FileID for a memory buffer |
| 266 | /// that will represent the FileID for the main source. One example |
| 267 | /// of when this would be used is when the main source is read from STDIN. |
| 268 | unsigned createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) { |
| 269 | assert (MainFileID == 0 && "MainFileID already set!"); |
| 270 | MainFileID = createMainFileIDForMemBuffer(Buffer); |
| 271 | return MainFileID; |
| 272 | } |
| 273 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 274 | /// getInstantiationLoc - Return a new SourceLocation that encodes the fact |
Chris Lattner | abca2bb | 2007-07-15 06:35:27 +0000 | [diff] [blame] | 275 | /// that a token at Loc should actually be referenced from InstantiationLoc. |
| 276 | SourceLocation getInstantiationLoc(SourceLocation Loc, |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 277 | SourceLocation InstantiationLoc); |
| 278 | |
| 279 | /// getBuffer - Return the buffer for the specified FileID. |
| 280 | /// |
| 281 | const llvm::MemoryBuffer *getBuffer(unsigned FileID) const { |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 282 | return getContentCache(FileID)->Buffer; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 283 | } |
| 284 | |
Chris Lattner | 8a12c27 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 285 | /// getBufferData - Return a pointer to the start and end of the character |
| 286 | /// data for the specified FileID. |
| 287 | std::pair<const char*, const char*> getBufferData(unsigned FileID) const; |
| 288 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 289 | /// getIncludeLoc - Return the location of the #include for the specified |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 290 | /// SourceLocation. If this is a macro expansion, this transparently figures |
| 291 | /// out which file includes the file being expanded into. |
| 292 | SourceLocation getIncludeLoc(SourceLocation ID) const { |
| 293 | return getFIDInfo(getLogicalLoc(ID).getFileID())->getIncludeLoc(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | /// getCharacterData - Return a pointer to the start of the specified location |
| 297 | /// in the appropriate MemoryBuffer. |
| 298 | const char *getCharacterData(SourceLocation SL) const; |
| 299 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 300 | /// getColumnNumber - Return the column # for the specified file position. |
| 301 | /// This is significantly cheaper to compute than the line number. This |
| 302 | /// returns zero if the column number isn't known. This may only be called on |
| 303 | /// a file sloc, so you must choose a physical or logical location before |
| 304 | /// calling this method. |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 305 | unsigned getColumnNumber(SourceLocation Loc) const; |
| 306 | |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 307 | unsigned getPhysicalColumnNumber(SourceLocation Loc) const { |
| 308 | return getColumnNumber(getPhysicalLoc(Loc)); |
| 309 | } |
| 310 | unsigned getLogicalColumnNumber(SourceLocation Loc) const { |
| 311 | return getColumnNumber(getLogicalLoc(Loc)); |
| 312 | } |
| 313 | |
| 314 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 315 | /// getLineNumber - Given a SourceLocation, return the physical line number |
| 316 | /// for the position indicated. This requires building and caching a table of |
| 317 | /// line offsets for the MemoryBuffer, so this is not cheap: use only when |
| 318 | /// about to emit a diagnostic. |
| 319 | unsigned getLineNumber(SourceLocation Loc); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 320 | |
| 321 | unsigned getLogicalLineNumber(SourceLocation Loc) { |
| 322 | return getLineNumber(getLogicalLoc(Loc)); |
| 323 | } |
| 324 | unsigned getPhysicalLineNumber(SourceLocation Loc) { |
| 325 | return getLineNumber(getPhysicalLoc(Loc)); |
| 326 | } |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 327 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 328 | /// getSourceName - This method returns the name of the file or buffer that |
| 329 | /// the SourceLocation specifies. This can be modified with #line directives, |
| 330 | /// etc. |
Chris Lattner | 8b6ca88 | 2007-08-30 05:59:30 +0000 | [diff] [blame] | 331 | const char *getSourceName(SourceLocation Loc) const; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 332 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 333 | /// Given a SourceLocation object, return the logical location referenced by |
| 334 | /// the ID. This logical location is subject to #line directives, etc. |
| 335 | SourceLocation getLogicalLoc(SourceLocation Loc) const { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 336 | // File locations are both physical and logical. |
| 337 | if (Loc.isFileID()) return Loc; |
| 338 | |
Chris Lattner | 18807d2 | 2007-11-09 23:59:17 +0000 | [diff] [blame] | 339 | return MacroIDs[Loc.getMacroID()].getVirtualLoc(); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | /// getPhysicalLoc - Given a SourceLocation object, return the physical |
| 343 | /// location referenced by the ID. |
| 344 | SourceLocation getPhysicalLoc(SourceLocation Loc) const { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 345 | // File locations are both physical and logical. |
| 346 | if (Loc.isFileID()) return Loc; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 347 | |
Chris Lattner | 31bb8be | 2007-07-20 18:00:12 +0000 | [diff] [blame] | 348 | SourceLocation PLoc = MacroIDs[Loc.getMacroID()].getPhysicalLoc(); |
| 349 | return PLoc.getFileLocWithOffset(Loc.getMacroPhysOffs()); |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 350 | } |
| 351 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 352 | /// getContentCacheForLoc - Return the ContentCache for the physloc of the |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 353 | /// specified SourceLocation, if one exists. |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 354 | const SrcMgr::ContentCache* getContentCacheForLoc(SourceLocation Loc) const { |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 355 | Loc = getPhysicalLoc(Loc); |
| 356 | unsigned FileID = Loc.getFileID(); |
| 357 | assert(FileID-1 < FileIDs.size() && "Invalid FileID!"); |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 358 | return FileIDs[FileID-1].getContentCache(); |
| 359 | } |
| 360 | |
| 361 | /// getFileEntryForLoc - Return the FileEntry record for the physloc of the |
| 362 | /// specified SourceLocation, if one exists. |
| 363 | const FileEntry* getFileEntryForLoc(SourceLocation Loc) const { |
| 364 | return getContentCacheForLoc(Loc)->Entry; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 365 | } |
| 366 | |
Chris Lattner | 3457e8c | 2007-10-12 20:24:19 +0000 | [diff] [blame] | 367 | /// getDecomposedFileLoc - Decompose the specified file location into a raw |
| 368 | /// FileID + Offset pair. The first element is the FileID, the second is the |
| 369 | /// offset from the start of the buffer of the location. |
| 370 | std::pair<unsigned, unsigned> getDecomposedFileLoc(SourceLocation Loc) const { |
| 371 | assert(Loc.isFileID() && "Isn't a File SourceLocation"); |
| 372 | |
| 373 | // TODO: Add a flag "is first chunk" to SLOC. |
| 374 | const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(Loc.getFileID()); |
| 375 | |
| 376 | // If this file has been split up into chunks, factor in the chunk number |
| 377 | // that the FileID references. |
| 378 | unsigned ChunkNo = FIDInfo->getChunkNo(); |
| 379 | unsigned Offset = Loc.getRawFilePos(); |
| 380 | Offset += (ChunkNo << SourceLocation::FilePosBits); |
| 381 | |
| 382 | return std::pair<unsigned,unsigned>(Loc.getFileID()-ChunkNo, Offset); |
| 383 | } |
| 384 | |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 385 | /// PrintStats - Print statistics to stderr. |
| 386 | /// |
| 387 | void PrintStats() const; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 388 | |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 389 | /// Emit - Emit this SourceManager to Bitcode. |
| 390 | void Emit(llvm::Serializer& S) const; |
| 391 | |
| 392 | /// Read - Reconstitute a SourceManager from Bitcode. |
Ted Kremenek | 1f94100 | 2007-12-05 00:19:51 +0000 | [diff] [blame] | 393 | static SourceManager* CreateAndRegister(llvm::Deserializer& S, |
| 394 | FileManager &FMgr); |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 395 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 396 | private: |
Ted Kremenek | 099b474 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 397 | friend class SrcMgr::ContentCache; // Used for deserialization. |
| 398 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 399 | /// createFileID - Create a new fileID for the specified ContentCache and |
| 400 | /// include position. This works regardless of whether the ContentCache |
| 401 | /// corresponds to a file or some other input source. |
| 402 | unsigned createFileID(const SrcMgr::ContentCache* File, |
| 403 | SourceLocation IncludePos); |
| 404 | |
| 405 | /// getContentCache - Create or return a cached ContentCache for the specified |
| 406 | /// file. This returns null on failure. |
| 407 | const SrcMgr::ContentCache* getContentCache(const FileEntry* SourceFile); |
| 408 | |
| 409 | /// createMemBufferContentCache - Create a new ContentCache for the specified |
| 410 | /// memory buffer. |
| 411 | const SrcMgr::ContentCache* |
| 412 | createMemBufferContentCache(const llvm::MemoryBuffer* Buf); |
| 413 | |
| 414 | const SrcMgr::FileIDInfo* getFIDInfo(unsigned FileID) const { |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 415 | assert(FileID-1 < FileIDs.size() && "Invalid FileID!"); |
| 416 | return &FileIDs[FileID-1]; |
| 417 | } |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 418 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 419 | const SrcMgr::ContentCache *getContentCache(unsigned FileID) const { |
| 420 | return getContentCache(getFIDInfo(FileID)); |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 421 | } |
| 422 | |
Ted Kremenek | 78d85f5 | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 423 | /// Return the ContentCache structure for the specified FileID. |
| 424 | /// This is always the physical reference for the ID. |
| 425 | const SrcMgr::ContentCache* |
| 426 | getContentCache(const SrcMgr::FileIDInfo* FIDInfo) const { |
| 427 | return FIDInfo->getContentCache(); |
| 428 | } |
Chris Lattner | 9dc1f53 | 2007-07-20 16:37:10 +0000 | [diff] [blame] | 429 | |
| 430 | /// getFullFilePos - This (efficient) method returns the offset from the start |
| 431 | /// of the file that the specified physical SourceLocation represents. This |
| 432 | /// returns the location of the physical character data, not the logical file |
| 433 | /// position. |
| 434 | unsigned getFullFilePos(SourceLocation PhysLoc) const { |
Chris Lattner | 3457e8c | 2007-10-12 20:24:19 +0000 | [diff] [blame] | 435 | return getDecomposedFileLoc(PhysLoc).second; |
Reid Spencer | 5f016e2 | 2007-07-11 17:01:13 +0000 | [diff] [blame] | 436 | } |
| 437 | }; |
| 438 | |
| 439 | |
| 440 | } // end namespace clang |
| 441 | |
| 442 | #endif |