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 | |
| 35 | /// SrcMgr - Private classes that are part of the SourceManager implementation. |
| 36 | /// |
| 37 | namespace SrcMgr { |
Ted Kremenek | dd364ea | 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 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 46 | /// Buffer - The actual buffer containing the characters from the input |
Ted Kremenek | 6a18635 | 2007-12-04 18:59:28 +0000 | [diff] [blame] | 47 | /// file. This is owned by the ContentCache object. |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 48 | const llvm::MemoryBuffer* Buffer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 49 | |
| 50 | /// SourceLineCache - A new[]'d array of offsets for each source line. This |
Ted Kremenek | 6a18635 | 2007-12-04 18:59:28 +0000 | [diff] [blame] | 51 | /// is lazily computed. This is owned by the ContentCache object. |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 52 | unsigned* SourceLineCache; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 53 | |
Ted Kremenek | 6a18635 | 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. |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 56 | unsigned NumLines; |
Ted Kremenek | dd364ea | 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 | 7670cca | 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 | 0ad06d1 | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 75 | /// Emit - Emit this ContentCache to Bitcode. |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 76 | void Emit(llvm::Serializer& S) const; |
Ted Kremenek | 0ad06d1 | 2007-12-04 19:39:02 +0000 | [diff] [blame] | 77 | |
Ted Kremenek | 9c856e9 | 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 | 7670cca | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 83 | private: |
| 84 | // Disable assignments. |
| 85 | ContentCache& operator=(const ContentCache& RHS); |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 86 | }; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 87 | |
| 88 | /// FileIDInfo - Information about a FileID, basically just the logical file |
| 89 | /// that it represents and include stack information. A File SourceLocation |
| 90 | /// is a byte offset from the start of this. |
| 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. |
| 99 | /// This information encodes the #include chain that a token was instantiated |
| 100 | /// from. |
| 101 | /// |
Ted Kremenek | 7670cca | 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). |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 105 | /// |
| 106 | struct FileIDInfo { |
| 107 | private: |
| 108 | /// IncludeLoc - The location of the #include that brought in this file. |
| 109 | /// This SourceLocation object has an invalid SLOC for the main file. |
| 110 | SourceLocation IncludeLoc; |
| 111 | |
| 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. |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 115 | unsigned ChunkNo:30; |
| 116 | |
| 117 | /// isSystemHeader - Set for system header files. |
| 118 | bool isSysHeader:1; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 119 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 120 | /// Content - Information about the source buffer itself. |
| 121 | const ContentCache* Content; |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 122 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 123 | public: |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 124 | /// get - Return a FileIDInfo object. |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 125 | static FileIDInfo get(SourceLocation IL, unsigned CN, |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 126 | const ContentCache *Con, bool SysHeader) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 127 | FileIDInfo X; |
| 128 | X.IncludeLoc = IL; |
| 129 | X.ChunkNo = CN; |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 130 | X.Content = Con; |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 131 | X.isSysHeader = SysHeader; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 132 | return X; |
| 133 | } |
| 134 | |
| 135 | SourceLocation getIncludeLoc() const { return IncludeLoc; } |
| 136 | unsigned getChunkNo() const { return ChunkNo; } |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 137 | const ContentCache* getContentCache() const { return Content; } |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 138 | bool isSystemHeader() const { return isSysHeader; } |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 139 | |
| 140 | /// Emit - Emit this FileIDInfo to Bitcode. |
| 141 | void Emit(llvm::Serializer& S) const; |
| 142 | |
| 143 | /// ReadVal - Reconstitute a FileIDInfo from Bitcode. |
| 144 | static FileIDInfo ReadVal(llvm::Deserializer& S); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | /// MacroIDInfo - Macro SourceLocations refer to these records by their ID. |
| 148 | /// Each MacroIDInfo encodes the Instantiation location - where the macro was |
| 149 | /// instantiated, and the PhysicalLoc - where the actual character data for |
| 150 | /// the token came from. An actual macro SourceLocation stores deltas from |
| 151 | /// these positions. |
| 152 | class MacroIDInfo { |
Chris Lattner | b8a39d9 | 2007-11-09 23:59:17 +0000 | [diff] [blame] | 153 | SourceLocation VirtualLoc, PhysicalLoc; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 154 | public: |
Chris Lattner | b8a39d9 | 2007-11-09 23:59:17 +0000 | [diff] [blame] | 155 | SourceLocation getVirtualLoc() const { return VirtualLoc; } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 156 | SourceLocation getPhysicalLoc() const { return PhysicalLoc; } |
| 157 | |
Chris Lattner | b8a39d9 | 2007-11-09 23:59:17 +0000 | [diff] [blame] | 158 | /// get - Return a MacroID for a macro expansion. VL specifies |
| 159 | /// the instantiation location (where the macro is expanded), and PL |
| 160 | /// specifies the physical location (where the characters from the token |
| 161 | /// come from). Both VL and PL refer to normal File SLocs. |
| 162 | static MacroIDInfo get(SourceLocation VL, SourceLocation PL) { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 163 | MacroIDInfo X; |
Chris Lattner | b8a39d9 | 2007-11-09 23:59:17 +0000 | [diff] [blame] | 164 | X.VirtualLoc = VL; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 165 | X.PhysicalLoc = PL; |
| 166 | return X; |
| 167 | } |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 168 | |
| 169 | /// Emit - Emit this MacroIDInfo to Bitcode. |
| 170 | void Emit(llvm::Serializer& S) const; |
| 171 | |
| 172 | /// ReadVal - Reconstitute a MacroIDInfo from Bitcode. |
| 173 | static MacroIDInfo ReadVal(llvm::Deserializer& S); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 174 | }; |
| 175 | } // end SrcMgr namespace. |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 176 | } // end clang namespace |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 177 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 178 | namespace std { |
| 179 | template <> struct less<clang::SrcMgr::ContentCache> { |
| 180 | inline bool operator()(const clang::SrcMgr::ContentCache& L, |
| 181 | const clang::SrcMgr::ContentCache& R) const { |
| 182 | return L.Entry < R.Entry; |
| 183 | } |
| 184 | }; |
| 185 | } // end std namespace |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 186 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 187 | namespace clang { |
| 188 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 189 | /// SourceManager - This file handles loading and caching of source files into |
| 190 | /// memory. This object owns the MemoryBuffer objects for all of the loaded |
| 191 | /// files and assigns unique FileID's for each unique #include chain. |
| 192 | /// |
| 193 | /// The SourceManager can be queried for information about SourceLocation |
| 194 | /// objects, turning them into either physical or logical locations. Physical |
| 195 | /// locations represent where the bytes corresponding to a token came from and |
| 196 | /// logical locations represent where the location is in the user's view. In |
| 197 | /// the case of a macro expansion, for example, the physical location indicates |
| 198 | /// where the expanded token came from and the logical location specifies where |
| 199 | /// it was expanded. Logical locations are also influenced by #line directives, |
| 200 | /// etc. |
| 201 | class SourceManager { |
| 202 | /// FileInfos - Memoized information about all of the files tracked by this |
Ted Kremenek | 7670cca | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 203 | /// SourceManager. This set allows us to merge ContentCache entries based |
| 204 | /// on their FileEntry*. All ContentCache objects will thus have unique, |
| 205 | /// non-null, FileEntry pointers. |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 206 | std::set<SrcMgr::ContentCache> FileInfos; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 207 | |
| 208 | /// MemBufferInfos - Information about various memory buffers that we have |
| 209 | /// 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] | 210 | /// the ContentCache objects to be stable. All FileEntry* within the |
Ted Kremenek | 7670cca | 2007-10-30 22:57:35 +0000 | [diff] [blame] | 211 | /// 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] | 212 | std::list<SrcMgr::ContentCache> MemBufferInfos; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 213 | |
| 214 | /// FileIDs - Information about each FileID. FileID #0 is not valid, so all |
| 215 | /// entries are off by one. |
| 216 | std::vector<SrcMgr::FileIDInfo> FileIDs; |
| 217 | |
| 218 | /// MacroIDs - Information about each MacroID. |
| 219 | std::vector<SrcMgr::MacroIDInfo> MacroIDs; |
| 220 | |
| 221 | /// LastLineNo - These ivars serve as a cache used in the getLineNumber |
| 222 | /// method which is used to speedup getLineNumber calls to nearby locations. |
| 223 | unsigned LastLineNoFileIDQuery; |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 224 | SrcMgr::ContentCache *LastLineNoContentCache; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 225 | unsigned LastLineNoFilePos; |
| 226 | unsigned LastLineNoResult; |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 227 | |
Ted Kremenek | 2578dd0 | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 228 | /// MainFileID - The file ID for the main source file of the translation unit. |
| 229 | unsigned MainFileID; |
Steve Naroff | 543c7c6 | 2008-02-02 00:10:46 +0000 | [diff] [blame] | 230 | |
| 231 | // SourceManager doesn't support copy construction. |
| 232 | explicit SourceManager(const SourceManager&); |
| 233 | void operator=(const SourceManager&); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 234 | public: |
Ted Kremenek | 2578dd0 | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 235 | SourceManager() : LastLineNoFileIDQuery(~0U), MainFileID(0) {} |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 236 | ~SourceManager() {} |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 237 | |
| 238 | void clearIDTables() { |
Ted Kremenek | 2a4224a | 2008-06-06 22:42:39 +0000 | [diff] [blame] | 239 | MainFileID = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 240 | FileIDs.clear(); |
| 241 | MacroIDs.clear(); |
| 242 | LastLineNoFileIDQuery = ~0U; |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 243 | LastLineNoContentCache = 0; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Ted Kremenek | 2578dd0 | 2007-12-19 22:29:55 +0000 | [diff] [blame] | 246 | /// getMainFileID - Returns the FileID of the main source file. |
| 247 | unsigned getMainFileID() const { return MainFileID; } |
| 248 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 249 | /// createFileID - Create a new FileID that represents the specified file |
| 250 | /// being #included from the specified IncludePosition. This returns 0 on |
| 251 | /// error and translates NULL into standard input. |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 252 | unsigned createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, |
| 253 | bool isSysHeader = false) { |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 254 | const SrcMgr::ContentCache *IR = getContentCache(SourceFile); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 255 | if (IR == 0) return 0; // Error opening file? |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 256 | return createFileID(IR, IncludePos, isSysHeader); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 257 | } |
| 258 | |
Ted Kremenek | 6d1d3ac | 2007-12-19 23:48:45 +0000 | [diff] [blame] | 259 | /// createMainFileID - Create the FileID for the main source file. |
| 260 | unsigned createMainFileID(const FileEntry *SourceFile, |
| 261 | SourceLocation IncludePos) { |
| 262 | |
| 263 | assert (MainFileID == 0 && "MainFileID already set!"); |
| 264 | MainFileID = createFileID(SourceFile,IncludePos); |
| 265 | return MainFileID; |
| 266 | } |
| 267 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 268 | /// createFileIDForMemBuffer - Create a new FileID that represents the |
| 269 | /// specified memory buffer. This does no caching of the buffer and takes |
| 270 | /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once. |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 271 | unsigned createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer, |
| 272 | bool isSysHeader = false) { |
| 273 | return createFileID(createMemBufferContentCache(Buffer), SourceLocation(), |
| 274 | isSysHeader); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 275 | } |
| 276 | |
Ted Kremenek | 6d1d3ac | 2007-12-19 23:48:45 +0000 | [diff] [blame] | 277 | /// createMainFileIDForMembuffer - Create the FileID for a memory buffer |
| 278 | /// that will represent the FileID for the main source. One example |
| 279 | /// of when this would be used is when the main source is read from STDIN. |
| 280 | unsigned createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) { |
| 281 | assert (MainFileID == 0 && "MainFileID already set!"); |
Chris Lattner | 73bb782 | 2007-12-20 01:38:17 +0000 | [diff] [blame] | 282 | MainFileID = createFileIDForMemBuffer(Buffer); |
Ted Kremenek | 6d1d3ac | 2007-12-19 23:48:45 +0000 | [diff] [blame] | 283 | return MainFileID; |
| 284 | } |
| 285 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 286 | /// getInstantiationLoc - Return a new SourceLocation that encodes the fact |
| 287 | /// that a token at Loc should actually be referenced from InstantiationLoc. |
| 288 | SourceLocation getInstantiationLoc(SourceLocation Loc, |
| 289 | SourceLocation InstantiationLoc); |
| 290 | |
| 291 | /// getBuffer - Return the buffer for the specified FileID. |
| 292 | /// |
| 293 | const llvm::MemoryBuffer *getBuffer(unsigned FileID) const { |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 294 | return getContentCache(FileID)->Buffer; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 295 | } |
| 296 | |
Chris Lattner | 569faa6 | 2007-10-11 18:38:32 +0000 | [diff] [blame] | 297 | /// getBufferData - Return a pointer to the start and end of the character |
| 298 | /// data for the specified FileID. |
| 299 | std::pair<const char*, const char*> getBufferData(unsigned FileID) const; |
| 300 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 301 | /// getIncludeLoc - Return the location of the #include for the specified |
| 302 | /// SourceLocation. If this is a macro expansion, this transparently figures |
| 303 | /// out which file includes the file being expanded into. |
| 304 | SourceLocation getIncludeLoc(SourceLocation ID) const { |
| 305 | return getFIDInfo(getLogicalLoc(ID).getFileID())->getIncludeLoc(); |
| 306 | } |
| 307 | |
| 308 | /// getCharacterData - Return a pointer to the start of the specified location |
| 309 | /// in the appropriate MemoryBuffer. |
| 310 | const char *getCharacterData(SourceLocation SL) const; |
| 311 | |
| 312 | /// getColumnNumber - Return the column # for the specified file position. |
| 313 | /// This is significantly cheaper to compute than the line number. This |
| 314 | /// returns zero if the column number isn't known. This may only be called on |
| 315 | /// a file sloc, so you must choose a physical or logical location before |
| 316 | /// calling this method. |
| 317 | unsigned getColumnNumber(SourceLocation Loc) const; |
| 318 | |
| 319 | unsigned getPhysicalColumnNumber(SourceLocation Loc) const { |
| 320 | return getColumnNumber(getPhysicalLoc(Loc)); |
| 321 | } |
| 322 | unsigned getLogicalColumnNumber(SourceLocation Loc) const { |
| 323 | return getColumnNumber(getLogicalLoc(Loc)); |
| 324 | } |
| 325 | |
| 326 | |
| 327 | /// getLineNumber - Given a SourceLocation, return the physical line number |
| 328 | /// for the position indicated. This requires building and caching a table of |
| 329 | /// line offsets for the MemoryBuffer, so this is not cheap: use only when |
| 330 | /// about to emit a diagnostic. |
| 331 | unsigned getLineNumber(SourceLocation Loc); |
| 332 | |
| 333 | unsigned getLogicalLineNumber(SourceLocation Loc) { |
| 334 | return getLineNumber(getLogicalLoc(Loc)); |
| 335 | } |
| 336 | unsigned getPhysicalLineNumber(SourceLocation Loc) { |
| 337 | return getLineNumber(getPhysicalLoc(Loc)); |
| 338 | } |
| 339 | |
| 340 | /// getSourceName - This method returns the name of the file or buffer that |
| 341 | /// the SourceLocation specifies. This can be modified with #line directives, |
| 342 | /// etc. |
Chris Lattner | 37f04117 | 2007-08-30 05:59:30 +0000 | [diff] [blame] | 343 | const char *getSourceName(SourceLocation Loc) const; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 344 | |
| 345 | /// Given a SourceLocation object, return the logical location referenced by |
| 346 | /// the ID. This logical location is subject to #line directives, etc. |
| 347 | SourceLocation getLogicalLoc(SourceLocation Loc) const { |
| 348 | // File locations are both physical and logical. |
| 349 | if (Loc.isFileID()) return Loc; |
| 350 | |
Chris Lattner | b8a39d9 | 2007-11-09 23:59:17 +0000 | [diff] [blame] | 351 | return MacroIDs[Loc.getMacroID()].getVirtualLoc(); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | /// getPhysicalLoc - Given a SourceLocation object, return the physical |
| 355 | /// location referenced by the ID. |
| 356 | SourceLocation getPhysicalLoc(SourceLocation Loc) const { |
| 357 | // File locations are both physical and logical. |
| 358 | if (Loc.isFileID()) return Loc; |
| 359 | |
| 360 | SourceLocation PLoc = MacroIDs[Loc.getMacroID()].getPhysicalLoc(); |
| 361 | return PLoc.getFileLocWithOffset(Loc.getMacroPhysOffs()); |
| 362 | } |
| 363 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 364 | /// getContentCacheForLoc - Return the ContentCache for the physloc of the |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 365 | /// specified SourceLocation, if one exists. |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 366 | const SrcMgr::ContentCache* getContentCacheForLoc(SourceLocation Loc) const { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 367 | Loc = getPhysicalLoc(Loc); |
| 368 | unsigned FileID = Loc.getFileID(); |
| 369 | assert(FileID-1 < FileIDs.size() && "Invalid FileID!"); |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 370 | return FileIDs[FileID-1].getContentCache(); |
| 371 | } |
| 372 | |
| 373 | /// getFileEntryForLoc - Return the FileEntry record for the physloc of the |
| 374 | /// specified SourceLocation, if one exists. |
| 375 | const FileEntry* getFileEntryForLoc(SourceLocation Loc) const { |
| 376 | return getContentCacheForLoc(Loc)->Entry; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 377 | } |
| 378 | |
Ted Kremenek | d41db19 | 2007-12-20 00:15:17 +0000 | [diff] [blame] | 379 | /// getFileEntryForID - Returns the FileEntry record for the provided FileID. |
| 380 | const FileEntry* getFileEntryForID(unsigned id) const { |
| 381 | return getContentCache(id)->Entry; |
| 382 | } |
Ted Kremenek | 81019d7 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 383 | |
| 384 | /// getCanonicalFileID - Return the canonical FileID for a SourceLocation. |
| 385 | /// A file can have multiple FileIDs if it is large enough to be broken |
| 386 | /// into multiple chunks. This method returns the unique FileID without |
| 387 | /// chunk information for a given SourceLocation. Use this method when |
| 388 | /// you want to compare FileIDs across SourceLocations. |
| 389 | unsigned getCanonicalFileID(SourceLocation PhysLoc) const { |
| 390 | return getDecomposedFileLoc(PhysLoc).first; |
| 391 | } |
Ted Kremenek | d41db19 | 2007-12-20 00:15:17 +0000 | [diff] [blame] | 392 | |
Chris Lattner | 136df56 | 2007-10-12 20:24:19 +0000 | [diff] [blame] | 393 | /// getDecomposedFileLoc - Decompose the specified file location into a raw |
| 394 | /// FileID + Offset pair. The first element is the FileID, the second is the |
| 395 | /// offset from the start of the buffer of the location. |
| 396 | std::pair<unsigned, unsigned> getDecomposedFileLoc(SourceLocation Loc) const { |
| 397 | assert(Loc.isFileID() && "Isn't a File SourceLocation"); |
| 398 | |
| 399 | // TODO: Add a flag "is first chunk" to SLOC. |
| 400 | const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(Loc.getFileID()); |
| 401 | |
| 402 | // If this file has been split up into chunks, factor in the chunk number |
| 403 | // that the FileID references. |
| 404 | unsigned ChunkNo = FIDInfo->getChunkNo(); |
| 405 | unsigned Offset = Loc.getRawFilePos(); |
| 406 | Offset += (ChunkNo << SourceLocation::FilePosBits); |
Nico Weber | 2bbdcae | 2008-08-09 22:13:42 +0000 | [diff] [blame] | 407 | |
| 408 | assert(Loc.getFileID() >= ChunkNo && "Unexpected offset"); |
Chris Lattner | 136df56 | 2007-10-12 20:24:19 +0000 | [diff] [blame] | 409 | |
Nico Weber | 2bbdcae | 2008-08-09 22:13:42 +0000 | [diff] [blame] | 410 | return std::make_pair(Loc.getFileID()-ChunkNo, Offset); |
Chris Lattner | 136df56 | 2007-10-12 20:24:19 +0000 | [diff] [blame] | 411 | } |
Ted Kremenek | a0bc14d | 2008-04-08 21:26:35 +0000 | [diff] [blame] | 412 | |
| 413 | /// getFullFilePos - This (efficient) method returns the offset from the start |
| 414 | /// of the file that the specified physical SourceLocation represents. This |
| 415 | /// returns the location of the physical character data, not the logical file |
| 416 | /// position. |
| 417 | unsigned getFullFilePos(SourceLocation PhysLoc) const { |
| 418 | return getDecomposedFileLoc(PhysLoc).second; |
| 419 | } |
Chris Lattner | 136df56 | 2007-10-12 20:24:19 +0000 | [diff] [blame] | 420 | |
Ted Kremenek | 81019d7 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 421 | /// isFromSameFile - Returns true if both SourceLocations correspond to |
| 422 | /// the same file. |
| 423 | bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const { |
| 424 | return getCanonicalFileID(Loc1) == getCanonicalFileID(Loc2); |
| 425 | } |
| 426 | |
| 427 | /// isFromMainFile - Returns true if the file of provided SourceLocation is |
| 428 | /// the main file. |
| 429 | bool isFromMainFile(SourceLocation Loc) const { |
| 430 | return getCanonicalFileID(Loc) == getMainFileID(); |
| 431 | } |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 432 | |
| 433 | /// isInSystemHeader - Returns if a SourceLocation is in a system header. |
| 434 | bool isInSystemHeader(SourceLocation Loc) const { |
Nico Weber | 7d3196c | 2008-08-29 17:02:23 +0000 | [diff] [blame] | 435 | return getFIDInfo(getPhysicalLoc(Loc).getFileID())->isSystemHeader(); |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 436 | } |
Ted Kremenek | 81019d7 | 2008-04-14 21:04:18 +0000 | [diff] [blame] | 437 | |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 438 | /// PrintStats - Print statistics to stderr. |
| 439 | /// |
| 440 | void PrintStats() const; |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 441 | |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 442 | /// Emit - Emit this SourceManager to Bitcode. |
| 443 | void Emit(llvm::Serializer& S) const; |
| 444 | |
| 445 | /// Read - Reconstitute a SourceManager from Bitcode. |
Ted Kremenek | bc54abf | 2007-12-05 00:19:51 +0000 | [diff] [blame] | 446 | static SourceManager* CreateAndRegister(llvm::Deserializer& S, |
| 447 | FileManager &FMgr); |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 448 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 449 | private: |
Cédric Venet | 0009c94 | 2008-06-26 12:50:52 +0000 | [diff] [blame] | 450 | friend struct SrcMgr::ContentCache; // Used for deserialization. |
Ted Kremenek | 9c856e9 | 2007-12-05 00:14:18 +0000 | [diff] [blame] | 451 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 452 | /// createFileID - Create a new fileID for the specified ContentCache and |
| 453 | /// include position. This works regardless of whether the ContentCache |
| 454 | /// corresponds to a file or some other input source. |
| 455 | unsigned createFileID(const SrcMgr::ContentCache* File, |
Nico Weber | d2a6ac9 | 2008-08-10 19:59:06 +0000 | [diff] [blame] | 456 | SourceLocation IncludePos, bool isSysHeader = false); |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 457 | |
| 458 | /// getContentCache - Create or return a cached ContentCache for the specified |
| 459 | /// file. This returns null on failure. |
| 460 | const SrcMgr::ContentCache* getContentCache(const FileEntry* SourceFile); |
| 461 | |
| 462 | /// createMemBufferContentCache - Create a new ContentCache for the specified |
| 463 | /// memory buffer. |
| 464 | const SrcMgr::ContentCache* |
| 465 | createMemBufferContentCache(const llvm::MemoryBuffer* Buf); |
| 466 | |
| 467 | const SrcMgr::FileIDInfo* getFIDInfo(unsigned FileID) const { |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 468 | assert(FileID-1 < FileIDs.size() && "Invalid FileID!"); |
| 469 | return &FileIDs[FileID-1]; |
| 470 | } |
| 471 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 472 | const SrcMgr::ContentCache *getContentCache(unsigned FileID) const { |
| 473 | return getContentCache(getFIDInfo(FileID)); |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 474 | } |
| 475 | |
Ted Kremenek | dd364ea | 2007-10-30 21:08:08 +0000 | [diff] [blame] | 476 | /// Return the ContentCache structure for the specified FileID. |
| 477 | /// This is always the physical reference for the ID. |
| 478 | const SrcMgr::ContentCache* |
| 479 | getContentCache(const SrcMgr::FileIDInfo* FIDInfo) const { |
| 480 | return FIDInfo->getContentCache(); |
| 481 | } |
Chris Lattner | 4b00965 | 2007-07-25 00:24:17 +0000 | [diff] [blame] | 482 | }; |
| 483 | |
| 484 | |
| 485 | } // end namespace clang |
| 486 | |
| 487 | #endif |