blob: 770ac803aee5da941e618c845ed36ba47eb1696f [file] [log] [blame]
Chris Lattner4b009652007-07-25 00:24:17 +00001//===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner959e5be2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Chris Lattner4b009652007-07-25 00:24:17 +00007//
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 Kremenekdd364ea2007-10-30 21:08:08 +000018#include "llvm/Bitcode/SerializationFwd.h"
Chris Lattner4b009652007-07-25 00:24:17 +000019#include <vector>
Ted Kremenekdd364ea2007-10-30 21:08:08 +000020#include <set>
Chris Lattner4b009652007-07-25 00:24:17 +000021#include <list>
22#include <cassert>
23
24namespace llvm {
25class MemoryBuffer;
26}
27
28namespace clang {
29
30class SourceManager;
Ted Kremenek9c856e92007-12-05 00:14:18 +000031class FileManager;
Chris Lattner4b009652007-07-25 00:24:17 +000032class FileEntry;
33class IdentifierTokenInfo;
34
35/// SrcMgr - Private classes that are part of the SourceManager implementation.
36///
37namespace SrcMgr {
Ted Kremenekdd364ea2007-10-30 21:08:08 +000038 /// 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 Lattner4b009652007-07-25 00:24:17 +000046 /// Buffer - The actual buffer containing the characters from the input
Ted Kremenek6a186352007-12-04 18:59:28 +000047 /// file. This is owned by the ContentCache object.
Ted Kremenekdd364ea2007-10-30 21:08:08 +000048 const llvm::MemoryBuffer* Buffer;
Chris Lattner4b009652007-07-25 00:24:17 +000049
50 /// SourceLineCache - A new[]'d array of offsets for each source line. This
Ted Kremenek6a186352007-12-04 18:59:28 +000051 /// is lazily computed. This is owned by the ContentCache object.
Ted Kremenekdd364ea2007-10-30 21:08:08 +000052 unsigned* SourceLineCache;
Chris Lattner4b009652007-07-25 00:24:17 +000053
Ted Kremenek6a186352007-12-04 18:59:28 +000054 /// NumLines - The number of lines in this ContentCache. This is only valid
55 /// if SourceLineCache is non-null.
Chris Lattner4b009652007-07-25 00:24:17 +000056 unsigned NumLines;
Ted Kremenekdd364ea2007-10-30 21:08:08 +000057
58 ContentCache(const FileEntry* e = NULL)
59 : Entry(e), Buffer(NULL), SourceLineCache(NULL), NumLines(0) {}
60
61 ~ContentCache();
Ted Kremenek7670cca2007-10-30 22:57:35 +000062
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 Kremenek0ad06d12007-12-04 19:39:02 +000075 /// Emit - Emit this ContentCache to Bitcode.
Ted Kremenek9c856e92007-12-05 00:14:18 +000076 void Emit(llvm::Serializer& S) const;
Ted Kremenek0ad06d12007-12-04 19:39:02 +000077
Ted Kremenek9c856e92007-12-05 00:14:18 +000078 /// 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 Kremenek7670cca2007-10-30 22:57:35 +000083 private:
84 // Disable assignments.
85 ContentCache& operator=(const ContentCache& RHS);
Ted Kremenekdd364ea2007-10-30 21:08:08 +000086 };
Chris Lattner4b009652007-07-25 00:24:17 +000087
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 Kremenek7670cca2007-10-30 22:57:35 +0000102 /// 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 Lattner4b009652007-07-25 00:24:17 +0000105 ///
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 Weberd2a6ac92008-08-10 19:59:06 +0000115 unsigned ChunkNo:30;
116
117 /// isSystemHeader - Set for system header files.
118 bool isSysHeader:1;
Chris Lattner4b009652007-07-25 00:24:17 +0000119
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000120 /// Content - Information about the source buffer itself.
121 const ContentCache* Content;
Nico Weberd2a6ac92008-08-10 19:59:06 +0000122
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000123 public:
Chris Lattner4b009652007-07-25 00:24:17 +0000124 /// get - Return a FileIDInfo object.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000125 static FileIDInfo get(SourceLocation IL, unsigned CN,
Nico Weberd2a6ac92008-08-10 19:59:06 +0000126 const ContentCache *Con, bool SysHeader) {
Chris Lattner4b009652007-07-25 00:24:17 +0000127 FileIDInfo X;
128 X.IncludeLoc = IL;
129 X.ChunkNo = CN;
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000130 X.Content = Con;
Nico Weberd2a6ac92008-08-10 19:59:06 +0000131 X.isSysHeader = SysHeader;
Chris Lattner4b009652007-07-25 00:24:17 +0000132 return X;
133 }
134
135 SourceLocation getIncludeLoc() const { return IncludeLoc; }
136 unsigned getChunkNo() const { return ChunkNo; }
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000137 const ContentCache* getContentCache() const { return Content; }
Nico Weberd2a6ac92008-08-10 19:59:06 +0000138 bool isSystemHeader() const { return isSysHeader; }
Ted Kremenek9c856e92007-12-05 00:14:18 +0000139
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 Lattner4b009652007-07-25 00:24:17 +0000145 };
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 Lattnerb8a39d92007-11-09 23:59:17 +0000153 SourceLocation VirtualLoc, PhysicalLoc;
Chris Lattner4b009652007-07-25 00:24:17 +0000154 public:
Chris Lattnerb8a39d92007-11-09 23:59:17 +0000155 SourceLocation getVirtualLoc() const { return VirtualLoc; }
Chris Lattner4b009652007-07-25 00:24:17 +0000156 SourceLocation getPhysicalLoc() const { return PhysicalLoc; }
157
Chris Lattnerb8a39d92007-11-09 23:59:17 +0000158 /// 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 Lattner4b009652007-07-25 00:24:17 +0000163 MacroIDInfo X;
Chris Lattnerb8a39d92007-11-09 23:59:17 +0000164 X.VirtualLoc = VL;
Chris Lattner4b009652007-07-25 00:24:17 +0000165 X.PhysicalLoc = PL;
166 return X;
167 }
Ted Kremenek9c856e92007-12-05 00:14:18 +0000168
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 Lattner4b009652007-07-25 00:24:17 +0000174 };
175} // end SrcMgr namespace.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000176} // end clang namespace
Chris Lattner4b009652007-07-25 00:24:17 +0000177
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000178namespace std {
179template <> 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 Lattner4b009652007-07-25 00:24:17 +0000186
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000187namespace clang {
188
Chris Lattner4b009652007-07-25 00:24:17 +0000189/// 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.
201class SourceManager {
202 /// FileInfos - Memoized information about all of the files tracked by this
Ted Kremenek7670cca2007-10-30 22:57:35 +0000203 /// 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 Kremenekdd364ea2007-10-30 21:08:08 +0000206 std::set<SrcMgr::ContentCache> FileInfos;
Chris Lattner4b009652007-07-25 00:24:17 +0000207
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 Kremenek6a186352007-12-04 18:59:28 +0000210 /// the ContentCache objects to be stable. All FileEntry* within the
Ted Kremenek7670cca2007-10-30 22:57:35 +0000211 /// stored ContentCache objects are NULL, as they do not refer to a file.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000212 std::list<SrcMgr::ContentCache> MemBufferInfos;
Chris Lattner4b009652007-07-25 00:24:17 +0000213
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 Kremenekdd364ea2007-10-30 21:08:08 +0000224 SrcMgr::ContentCache *LastLineNoContentCache;
Chris Lattner4b009652007-07-25 00:24:17 +0000225 unsigned LastLineNoFilePos;
226 unsigned LastLineNoResult;
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000227
Ted Kremenek2578dd02007-12-19 22:29:55 +0000228 /// MainFileID - The file ID for the main source file of the translation unit.
229 unsigned MainFileID;
Steve Naroff543c7c62008-02-02 00:10:46 +0000230
231 // SourceManager doesn't support copy construction.
232 explicit SourceManager(const SourceManager&);
233 void operator=(const SourceManager&);
Chris Lattner4b009652007-07-25 00:24:17 +0000234public:
Ted Kremenek2578dd02007-12-19 22:29:55 +0000235 SourceManager() : LastLineNoFileIDQuery(~0U), MainFileID(0) {}
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000236 ~SourceManager() {}
Chris Lattner4b009652007-07-25 00:24:17 +0000237
238 void clearIDTables() {
Ted Kremenek2a4224a2008-06-06 22:42:39 +0000239 MainFileID = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000240 FileIDs.clear();
241 MacroIDs.clear();
242 LastLineNoFileIDQuery = ~0U;
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000243 LastLineNoContentCache = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000244 }
245
Ted Kremenek2578dd02007-12-19 22:29:55 +0000246 /// getMainFileID - Returns the FileID of the main source file.
247 unsigned getMainFileID() const { return MainFileID; }
248
Chris Lattner4b009652007-07-25 00:24:17 +0000249 /// 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 Weberd2a6ac92008-08-10 19:59:06 +0000252 unsigned createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
253 bool isSysHeader = false) {
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000254 const SrcMgr::ContentCache *IR = getContentCache(SourceFile);
Chris Lattner4b009652007-07-25 00:24:17 +0000255 if (IR == 0) return 0; // Error opening file?
Nico Weberd2a6ac92008-08-10 19:59:06 +0000256 return createFileID(IR, IncludePos, isSysHeader);
Chris Lattner4b009652007-07-25 00:24:17 +0000257 }
258
Ted Kremenek6d1d3ac2007-12-19 23:48:45 +0000259 /// 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 Lattner4b009652007-07-25 00:24:17 +0000268 /// 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 Weberd2a6ac92008-08-10 19:59:06 +0000271 unsigned createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
272 bool isSysHeader = false) {
273 return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
274 isSysHeader);
Chris Lattner4b009652007-07-25 00:24:17 +0000275 }
276
Ted Kremenek6d1d3ac2007-12-19 23:48:45 +0000277 /// 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 Lattner73bb7822007-12-20 01:38:17 +0000282 MainFileID = createFileIDForMemBuffer(Buffer);
Ted Kremenek6d1d3ac2007-12-19 23:48:45 +0000283 return MainFileID;
284 }
285
Chris Lattner4b009652007-07-25 00:24:17 +0000286 /// 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 Kremenekdd364ea2007-10-30 21:08:08 +0000294 return getContentCache(FileID)->Buffer;
Chris Lattner4b009652007-07-25 00:24:17 +0000295 }
296
Chris Lattner569faa62007-10-11 18:38:32 +0000297 /// 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 Lattner4b009652007-07-25 00:24:17 +0000301 /// 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 Lattner37f041172007-08-30 05:59:30 +0000343 const char *getSourceName(SourceLocation Loc) const;
Chris Lattner4b009652007-07-25 00:24:17 +0000344
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 Lattnerb8a39d92007-11-09 23:59:17 +0000351 return MacroIDs[Loc.getMacroID()].getVirtualLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000352 }
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 Kremenekdd364ea2007-10-30 21:08:08 +0000364 /// getContentCacheForLoc - Return the ContentCache for the physloc of the
Chris Lattner4b009652007-07-25 00:24:17 +0000365 /// specified SourceLocation, if one exists.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000366 const SrcMgr::ContentCache* getContentCacheForLoc(SourceLocation Loc) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000367 Loc = getPhysicalLoc(Loc);
368 unsigned FileID = Loc.getFileID();
369 assert(FileID-1 < FileIDs.size() && "Invalid FileID!");
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000370 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 Lattner4b009652007-07-25 00:24:17 +0000377 }
378
Ted Kremenekd41db192007-12-20 00:15:17 +0000379 /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
380 const FileEntry* getFileEntryForID(unsigned id) const {
381 return getContentCache(id)->Entry;
382 }
Ted Kremenek81019d72008-04-14 21:04:18 +0000383
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 Kremenekd41db192007-12-20 00:15:17 +0000392
Chris Lattner136df562007-10-12 20:24:19 +0000393 /// 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 Weber2bbdcae2008-08-09 22:13:42 +0000407
408 assert(Loc.getFileID() >= ChunkNo && "Unexpected offset");
Chris Lattner136df562007-10-12 20:24:19 +0000409
Nico Weber2bbdcae2008-08-09 22:13:42 +0000410 return std::make_pair(Loc.getFileID()-ChunkNo, Offset);
Chris Lattner136df562007-10-12 20:24:19 +0000411 }
Ted Kremeneka0bc14d2008-04-08 21:26:35 +0000412
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 Lattner136df562007-10-12 20:24:19 +0000420
Ted Kremenek81019d72008-04-14 21:04:18 +0000421 /// 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 Weberd2a6ac92008-08-10 19:59:06 +0000432
433 /// isInSystemHeader - Returns if a SourceLocation is in a system header.
434 bool isInSystemHeader(SourceLocation Loc) const {
Nico Weber7d3196c2008-08-29 17:02:23 +0000435 return getFIDInfo(getPhysicalLoc(Loc).getFileID())->isSystemHeader();
Nico Weberd2a6ac92008-08-10 19:59:06 +0000436 }
Ted Kremenek81019d72008-04-14 21:04:18 +0000437
Chris Lattner4b009652007-07-25 00:24:17 +0000438 /// PrintStats - Print statistics to stderr.
439 ///
440 void PrintStats() const;
Chris Lattner4b009652007-07-25 00:24:17 +0000441
Ted Kremenek9c856e92007-12-05 00:14:18 +0000442 /// Emit - Emit this SourceManager to Bitcode.
443 void Emit(llvm::Serializer& S) const;
444
445 /// Read - Reconstitute a SourceManager from Bitcode.
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000446 static SourceManager* CreateAndRegister(llvm::Deserializer& S,
447 FileManager &FMgr);
Ted Kremenek9c856e92007-12-05 00:14:18 +0000448
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000449private:
Cédric Venet0009c942008-06-26 12:50:52 +0000450 friend struct SrcMgr::ContentCache; // Used for deserialization.
Ted Kremenek9c856e92007-12-05 00:14:18 +0000451
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000452 /// 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 Weberd2a6ac92008-08-10 19:59:06 +0000456 SourceLocation IncludePos, bool isSysHeader = false);
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000457
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 Lattner4b009652007-07-25 00:24:17 +0000468 assert(FileID-1 < FileIDs.size() && "Invalid FileID!");
469 return &FileIDs[FileID-1];
470 }
471
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000472 const SrcMgr::ContentCache *getContentCache(unsigned FileID) const {
473 return getContentCache(getFIDInfo(FileID));
Chris Lattner4b009652007-07-25 00:24:17 +0000474 }
475
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000476 /// 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 Lattner4b009652007-07-25 00:24:17 +0000482};
483
484
485} // end namespace clang
486
487#endif