blob: 1eac4f15ded09a1964b39f88b2359a24f9c0ba00 [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
Chris Lattner6f044062008-09-26 21:18:42 +000035/// SrcMgr - Public enums and private classes that are part of the
36/// SourceManager implementation.
Chris Lattner4b009652007-07-25 00:24:17 +000037///
38namespace SrcMgr {
Chris Lattner7a4864e2008-10-27 01:19:25 +000039 /// CharacteristicKind - This is used to represent whether a file or directory
Chris Lattner6f044062008-09-26 21:18:42 +000040 /// 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 Lattner7a4864e2008-10-27 01:19:25 +000045 enum CharacteristicKind {
Chris Lattner6f044062008-09-26 21:18:42 +000046 C_User, C_System, C_ExternCSystem
47 };
48
Ted Kremenekdd364ea2007-10-30 21:08:08 +000049 /// ContentCache - Once instance of this struct is kept for every file
50 /// loaded or used. This object owns the MemoryBuffer object.
Ted Kremenekaa7dac12009-01-06 01:55:26 +000051 class ContentCache {
52 /// Buffer - The actual buffer containing the characters from the input
53 /// file. This is owned by the ContentCache object.
54 const llvm::MemoryBuffer* Buffer;
55
56 public:
Ted Kremenekdd364ea2007-10-30 21:08:08 +000057 /// 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.
60 const FileEntry* Entry;
61
Chris Lattner4b009652007-07-25 00:24:17 +000062 /// SourceLineCache - A new[]'d array of offsets for each source line. This
Ted Kremenek6a186352007-12-04 18:59:28 +000063 /// is lazily computed. This is owned by the ContentCache object.
Ted Kremenekdd364ea2007-10-30 21:08:08 +000064 unsigned* SourceLineCache;
Chris Lattner4b009652007-07-25 00:24:17 +000065
Ted Kremenek6a186352007-12-04 18:59:28 +000066 /// NumLines - The number of lines in this ContentCache. This is only valid
67 /// if SourceLineCache is non-null.
Chris Lattner4b009652007-07-25 00:24:17 +000068 unsigned NumLines;
Ted Kremenekaa7dac12009-01-06 01:55:26 +000069
70 /// getBuffer - Returns the memory buffer for the associated content.
71 const llvm::MemoryBuffer* getBuffer() const;
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
84 void setBuffer(const llvm::MemoryBuffer* B) {
85 assert(!Buffer && "MemoryBuffer already set.");
86 Buffer = B;
87 }
Ted Kremenekdd364ea2007-10-30 21:08:08 +000088
89 ContentCache(const FileEntry* e = NULL)
Ted Kremenekaa7dac12009-01-06 01:55:26 +000090 : Buffer(NULL), Entry(e), SourceLineCache(NULL), NumLines(0) {}
Ted Kremenekdd364ea2007-10-30 21:08:08 +000091
92 ~ContentCache();
Ted Kremenek7670cca2007-10-30 22:57:35 +000093
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.
97 ContentCache(const ContentCache& RHS) : Buffer(NULL),SourceLineCache(NULL) {
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 Kremenek0ad06d12007-12-04 19:39:02 +0000106 /// Emit - Emit this ContentCache to Bitcode.
Ted Kremenek9c856e92007-12-05 00:14:18 +0000107 void Emit(llvm::Serializer& S) const;
Ted Kremenek0ad06d12007-12-04 19:39:02 +0000108
Ted Kremenek9c856e92007-12-05 00:14:18 +0000109 /// ReadToSourceManager - Reconstitute a ContentCache from Bitcode
110 // and store it in the specified SourceManager.
111 static void ReadToSourceManager(llvm::Deserializer& D, SourceManager& SMgr,
112 FileManager* FMgr, std::vector<char>& Buf);
113
Ted Kremenek7670cca2007-10-30 22:57:35 +0000114 private:
115 // Disable assignments.
116 ContentCache& operator=(const ContentCache& RHS);
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000117 };
Chris Lattner4b009652007-07-25 00:24:17 +0000118
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 Lattner18c8dc02009-01-16 07:36:28 +0000124 /// 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 Lattner4b009652007-07-25 00:24:17 +0000127 ///
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 Kremenek7670cca2007-10-30 22:57:35 +0000133 /// 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 Lattner4b009652007-07-25 00:24:17 +0000136 ///
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 Lattner7257ce22008-09-26 20:12:23 +0000146 unsigned ChunkNo : 30;
Nico Weberd2a6ac92008-08-10 19:59:06 +0000147
Chris Lattner7a4864e2008-10-27 01:19:25 +0000148 /// FileCharacteristic - This is an instance of CharacteristicKind,
Chris Lattner7257ce22008-09-26 20:12:23 +0000149 /// indicating whether this is a system header dir or not.
Chris Lattner6f044062008-09-26 21:18:42 +0000150 unsigned FileCharacteristic : 2;
Chris Lattner4b009652007-07-25 00:24:17 +0000151
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000152 /// Content - Information about the source buffer itself.
153 const ContentCache* Content;
Nico Weberd2a6ac92008-08-10 19:59:06 +0000154
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000155 public:
Chris Lattner4b009652007-07-25 00:24:17 +0000156 /// get - Return a FileIDInfo object.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000157 static FileIDInfo get(SourceLocation IL, unsigned CN,
Chris Lattner6f044062008-09-26 21:18:42 +0000158 const ContentCache *Con,
Chris Lattner7a4864e2008-10-27 01:19:25 +0000159 CharacteristicKind FileCharacter) {
Chris Lattner4b009652007-07-25 00:24:17 +0000160 FileIDInfo X;
161 X.IncludeLoc = IL;
162 X.ChunkNo = CN;
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000163 X.Content = Con;
Chris Lattner6f044062008-09-26 21:18:42 +0000164 X.FileCharacteristic = FileCharacter;
Chris Lattner4b009652007-07-25 00:24:17 +0000165 return X;
166 }
167
168 SourceLocation getIncludeLoc() const { return IncludeLoc; }
169 unsigned getChunkNo() const { return ChunkNo; }
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000170 const ContentCache* getContentCache() const { return Content; }
Chris Lattner7257ce22008-09-26 20:12:23 +0000171
Chris Lattner6f044062008-09-26 21:18:42 +0000172 /// getCharacteristic - Return whether this is a system header or not.
Chris Lattner7a4864e2008-10-27 01:19:25 +0000173 CharacteristicKind getFileCharacteristic() const {
174 return (CharacteristicKind)FileCharacteristic;
Chris Lattner6f044062008-09-26 21:18:42 +0000175 }
Ted Kremenek9c856e92007-12-05 00:14:18 +0000176
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 Lattner4b009652007-07-25 00:24:17 +0000182 };
183
184 /// MacroIDInfo - Macro SourceLocations refer to these records by their ID.
185 /// Each MacroIDInfo encodes the Instantiation location - where the macro was
Chris Lattnercdf600e2009-01-16 07:00:02 +0000186 /// instantiated, and the SpellingLoc - where the actual character data for
Chris Lattner4b009652007-07-25 00:24:17 +0000187 /// the token came from. An actual macro SourceLocation stores deltas from
188 /// these positions.
189 class MacroIDInfo {
Chris Lattner74f67012009-01-16 07:15:35 +0000190 SourceLocation InstantiationLoc, SpellingLoc;
Chris Lattner4b009652007-07-25 00:24:17 +0000191 public:
Chris Lattner74f67012009-01-16 07:15:35 +0000192 SourceLocation getInstantiationLoc() const { return InstantiationLoc; }
Chris Lattnercdf600e2009-01-16 07:00:02 +0000193 SourceLocation getSpellingLoc() const { return SpellingLoc; }
Chris Lattner4b009652007-07-25 00:24:17 +0000194
Chris Lattnerb8a39d92007-11-09 23:59:17 +0000195 /// get - Return a MacroID for a macro expansion. VL specifies
Chris Lattnercdf600e2009-01-16 07:00:02 +0000196 /// the instantiation location (where the macro is expanded), and SL
197 /// specifies the spelling location (where the characters from the token
Chris Lattnerb8a39d92007-11-09 23:59:17 +0000198 /// come from). Both VL and PL refer to normal File SLocs.
Chris Lattnercdf600e2009-01-16 07:00:02 +0000199 static MacroIDInfo get(SourceLocation VL, SourceLocation SL) {
Chris Lattner4b009652007-07-25 00:24:17 +0000200 MacroIDInfo X;
Chris Lattner74f67012009-01-16 07:15:35 +0000201 X.InstantiationLoc = VL;
Chris Lattnercdf600e2009-01-16 07:00:02 +0000202 X.SpellingLoc = SL;
Chris Lattner4b009652007-07-25 00:24:17 +0000203 return X;
204 }
Ted Kremenek9c856e92007-12-05 00:14:18 +0000205
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 Lattner4b009652007-07-25 00:24:17 +0000211 };
212} // end SrcMgr namespace.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000213} // end clang namespace
Chris Lattner4b009652007-07-25 00:24:17 +0000214
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000215namespace std {
216template <> 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 Lattner4b009652007-07-25 00:24:17 +0000223
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000224namespace clang {
225
Chris Lattner4b009652007-07-25 00:24:17 +0000226/// 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 Lattner18c8dc02009-01-16 07:36:28 +0000231/// 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 Lattner4b009652007-07-25 00:24:17 +0000237class SourceManager {
238 /// FileInfos - Memoized information about all of the files tracked by this
Ted Kremenek7670cca2007-10-30 22:57:35 +0000239 /// 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 Kremenekdd364ea2007-10-30 21:08:08 +0000242 std::set<SrcMgr::ContentCache> FileInfos;
Chris Lattner4b009652007-07-25 00:24:17 +0000243
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 Kremenek6a186352007-12-04 18:59:28 +0000246 /// the ContentCache objects to be stable. All FileEntry* within the
Ted Kremenek7670cca2007-10-30 22:57:35 +0000247 /// stored ContentCache objects are NULL, as they do not refer to a file.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000248 std::list<SrcMgr::ContentCache> MemBufferInfos;
Chris Lattner4b009652007-07-25 00:24:17 +0000249
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 Lattnere9bf3e32008-11-18 06:51:15 +0000259 mutable unsigned LastLineNoFileIDQuery;
260 mutable SrcMgr::ContentCache *LastLineNoContentCache;
261 mutable unsigned LastLineNoFilePos;
262 mutable unsigned LastLineNoResult;
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000263
Ted Kremenek2578dd02007-12-19 22:29:55 +0000264 /// MainFileID - The file ID for the main source file of the translation unit.
265 unsigned MainFileID;
Steve Naroff543c7c62008-02-02 00:10:46 +0000266
267 // SourceManager doesn't support copy construction.
268 explicit SourceManager(const SourceManager&);
269 void operator=(const SourceManager&);
Chris Lattner4b009652007-07-25 00:24:17 +0000270public:
Ted Kremenek2578dd02007-12-19 22:29:55 +0000271 SourceManager() : LastLineNoFileIDQuery(~0U), MainFileID(0) {}
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000272 ~SourceManager() {}
Chris Lattner4b009652007-07-25 00:24:17 +0000273
274 void clearIDTables() {
Ted Kremenek2a4224a2008-06-06 22:42:39 +0000275 MainFileID = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000276 FileIDs.clear();
277 MacroIDs.clear();
278 LastLineNoFileIDQuery = ~0U;
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000279 LastLineNoContentCache = 0;
Chris Lattner4b009652007-07-25 00:24:17 +0000280 }
281
Ted Kremenek2578dd02007-12-19 22:29:55 +0000282 /// getMainFileID - Returns the FileID of the main source file.
283 unsigned getMainFileID() const { return MainFileID; }
284
Chris Lattner4b009652007-07-25 00:24:17 +0000285 /// createFileID - Create a new FileID that represents the specified file
286 /// being #included from the specified IncludePosition. This returns 0 on
287 /// error and translates NULL into standard input.
Nico Weberd2a6ac92008-08-10 19:59:06 +0000288 unsigned createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
Chris Lattner7a4864e2008-10-27 01:19:25 +0000289 SrcMgr::CharacteristicKind FileCharacter) {
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000290 const SrcMgr::ContentCache *IR = getContentCache(SourceFile);
Chris Lattner4b009652007-07-25 00:24:17 +0000291 if (IR == 0) return 0; // Error opening file?
Chris Lattner6f044062008-09-26 21:18:42 +0000292 return createFileID(IR, IncludePos, FileCharacter);
Chris Lattner4b009652007-07-25 00:24:17 +0000293 }
294
Ted Kremenek6d1d3ac2007-12-19 23:48:45 +0000295 /// createMainFileID - Create the FileID for the main source file.
296 unsigned createMainFileID(const FileEntry *SourceFile,
297 SourceLocation IncludePos) {
298
299 assert (MainFileID == 0 && "MainFileID already set!");
Chris Lattner6f044062008-09-26 21:18:42 +0000300 MainFileID = createFileID(SourceFile, IncludePos, SrcMgr::C_User);
Ted Kremenek6d1d3ac2007-12-19 23:48:45 +0000301 return MainFileID;
302 }
303
Chris Lattner4b009652007-07-25 00:24:17 +0000304 /// createFileIDForMemBuffer - Create a new FileID that represents the
305 /// specified memory buffer. This does no caching of the buffer and takes
306 /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
Chris Lattner7257ce22008-09-26 20:12:23 +0000307 unsigned createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
Nico Weberd2a6ac92008-08-10 19:59:06 +0000308 return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
Chris Lattner6f044062008-09-26 21:18:42 +0000309 SrcMgr::C_User);
Chris Lattner4b009652007-07-25 00:24:17 +0000310 }
311
Ted Kremenek6d1d3ac2007-12-19 23:48:45 +0000312 /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
313 /// that will represent the FileID for the main source. One example
314 /// of when this would be used is when the main source is read from STDIN.
315 unsigned createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
316 assert (MainFileID == 0 && "MainFileID already set!");
Chris Lattner73bb7822007-12-20 01:38:17 +0000317 MainFileID = createFileIDForMemBuffer(Buffer);
Ted Kremenek6d1d3ac2007-12-19 23:48:45 +0000318 return MainFileID;
319 }
320
Chris Lattner4b009652007-07-25 00:24:17 +0000321 /// getInstantiationLoc - Return a new SourceLocation that encodes the fact
322 /// that a token at Loc should actually be referenced from InstantiationLoc.
323 SourceLocation getInstantiationLoc(SourceLocation Loc,
324 SourceLocation InstantiationLoc);
325
326 /// getBuffer - Return the buffer for the specified FileID.
327 ///
328 const llvm::MemoryBuffer *getBuffer(unsigned FileID) const {
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000329 return getContentCache(FileID)->getBuffer();
Chris Lattner4b009652007-07-25 00:24:17 +0000330 }
331
Chris Lattner569faa62007-10-11 18:38:32 +0000332 /// getBufferData - Return a pointer to the start and end of the character
333 /// data for the specified FileID.
334 std::pair<const char*, const char*> getBufferData(unsigned FileID) const;
335
Chris Lattner4b009652007-07-25 00:24:17 +0000336 /// getIncludeLoc - Return the location of the #include for the specified
337 /// SourceLocation. If this is a macro expansion, this transparently figures
338 /// out which file includes the file being expanded into.
339 SourceLocation getIncludeLoc(SourceLocation ID) const {
Chris Lattner18c8dc02009-01-16 07:36:28 +0000340 return getFIDInfo(getInstantiationLoc(ID).getFileID())->getIncludeLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000341 }
342
343 /// getCharacterData - Return a pointer to the start of the specified location
344 /// in the appropriate MemoryBuffer.
345 const char *getCharacterData(SourceLocation SL) const;
346
347 /// getColumnNumber - Return the column # for the specified file position.
348 /// This is significantly cheaper to compute than the line number. This
349 /// returns zero if the column number isn't known. This may only be called on
Chris Lattner18c8dc02009-01-16 07:36:28 +0000350 /// a file sloc, so you must choose a spelling or instantiation location
351 /// before calling this method.
Chris Lattner4b009652007-07-25 00:24:17 +0000352 unsigned getColumnNumber(SourceLocation Loc) const;
353
Chris Lattnercdf600e2009-01-16 07:00:02 +0000354 unsigned getSpellingColumnNumber(SourceLocation Loc) const {
355 return getColumnNumber(getSpellingLoc(Loc));
Chris Lattner4b009652007-07-25 00:24:17 +0000356 }
Chris Lattner18c8dc02009-01-16 07:36:28 +0000357 unsigned getInstantiationColumnNumber(SourceLocation Loc) const {
358 return getColumnNumber(getInstantiationLoc(Loc));
Chris Lattner4b009652007-07-25 00:24:17 +0000359 }
360
361
Chris Lattnercdf600e2009-01-16 07:00:02 +0000362 /// getLineNumber - Given a SourceLocation, return the spelling line number
Chris Lattner4b009652007-07-25 00:24:17 +0000363 /// for the position indicated. This requires building and caching a table of
364 /// line offsets for the MemoryBuffer, so this is not cheap: use only when
365 /// about to emit a diagnostic.
Chris Lattnere9bf3e32008-11-18 06:51:15 +0000366 unsigned getLineNumber(SourceLocation Loc) const;
Chris Lattner4b009652007-07-25 00:24:17 +0000367
Chris Lattner18c8dc02009-01-16 07:36:28 +0000368 unsigned getInstantiationLineNumber(SourceLocation Loc) const {
369 return getLineNumber(getInstantiationLoc(Loc));
Chris Lattner4b009652007-07-25 00:24:17 +0000370 }
Chris Lattnercdf600e2009-01-16 07:00:02 +0000371 unsigned getSpellingLineNumber(SourceLocation Loc) const {
372 return getLineNumber(getSpellingLoc(Loc));
Chris Lattner4b009652007-07-25 00:24:17 +0000373 }
374
375 /// getSourceName - This method returns the name of the file or buffer that
376 /// the SourceLocation specifies. This can be modified with #line directives,
377 /// etc.
Chris Lattner37f041172007-08-30 05:59:30 +0000378 const char *getSourceName(SourceLocation Loc) const;
Chris Lattner4b009652007-07-25 00:24:17 +0000379
Chris Lattner18c8dc02009-01-16 07:36:28 +0000380 /// Given a SourceLocation object, return the instantiation location
381 /// referenced by the ID.
382 SourceLocation getInstantiationLoc(SourceLocation Loc) const {
Chris Lattnercdf600e2009-01-16 07:00:02 +0000383 // File locations work.
Chris Lattner4b009652007-07-25 00:24:17 +0000384 if (Loc.isFileID()) return Loc;
Chris Lattnercdf600e2009-01-16 07:00:02 +0000385
Chris Lattner74f67012009-01-16 07:15:35 +0000386 return MacroIDs[Loc.getMacroID()].getInstantiationLoc();
Chris Lattner4b009652007-07-25 00:24:17 +0000387 }
388
Chris Lattnercdf600e2009-01-16 07:00:02 +0000389 /// getSpellingLoc - Given a SourceLocation object, return the spelling
390 /// location referenced by the ID. This is the place where the characters
391 /// that make up the lexed token can be found.
392 SourceLocation getSpellingLoc(SourceLocation Loc) const {
393 // File locations work!
Chris Lattner4b009652007-07-25 00:24:17 +0000394 if (Loc.isFileID()) return Loc;
395
Chris Lattnercdf600e2009-01-16 07:00:02 +0000396 // Look up the macro token's spelling location.
397 SourceLocation PLoc = MacroIDs[Loc.getMacroID()].getSpellingLoc();
398 return PLoc.getFileLocWithOffset(Loc.getMacroSpellingOffs());
Chris Lattner4b009652007-07-25 00:24:17 +0000399 }
400
Chris Lattnercdf600e2009-01-16 07:00:02 +0000401 /// getContentCacheForLoc - Return the ContentCache for the spelling loc of
402 /// the specified SourceLocation, if one exists.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000403 const SrcMgr::ContentCache* getContentCacheForLoc(SourceLocation Loc) const {
Chris Lattnercdf600e2009-01-16 07:00:02 +0000404 Loc = getSpellingLoc(Loc);
Chris Lattner4b009652007-07-25 00:24:17 +0000405 unsigned FileID = Loc.getFileID();
406 assert(FileID-1 < FileIDs.size() && "Invalid FileID!");
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000407 return FileIDs[FileID-1].getContentCache();
408 }
409
Chris Lattnercdf600e2009-01-16 07:00:02 +0000410 /// getFileEntryForLoc - Return the FileEntry record for the spelling loc of
411 /// the specified SourceLocation, if one exists.
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000412 const FileEntry* getFileEntryForLoc(SourceLocation Loc) const {
413 return getContentCacheForLoc(Loc)->Entry;
Chris Lattner4b009652007-07-25 00:24:17 +0000414 }
415
Ted Kremenekd41db192007-12-20 00:15:17 +0000416 /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
417 const FileEntry* getFileEntryForID(unsigned id) const {
418 return getContentCache(id)->Entry;
419 }
Ted Kremenek81019d72008-04-14 21:04:18 +0000420
421 /// getCanonicalFileID - Return the canonical FileID for a SourceLocation.
422 /// A file can have multiple FileIDs if it is large enough to be broken
423 /// into multiple chunks. This method returns the unique FileID without
424 /// chunk information for a given SourceLocation. Use this method when
425 /// you want to compare FileIDs across SourceLocations.
Chris Lattnercdf600e2009-01-16 07:00:02 +0000426 unsigned getCanonicalFileID(SourceLocation SpellingLoc) const {
427 return getDecomposedFileLoc(SpellingLoc).first;
Ted Kremenek81019d72008-04-14 21:04:18 +0000428 }
Ted Kremenekd41db192007-12-20 00:15:17 +0000429
Chris Lattner136df562007-10-12 20:24:19 +0000430 /// getDecomposedFileLoc - Decompose the specified file location into a raw
431 /// FileID + Offset pair. The first element is the FileID, the second is the
432 /// offset from the start of the buffer of the location.
433 std::pair<unsigned, unsigned> getDecomposedFileLoc(SourceLocation Loc) const {
434 assert(Loc.isFileID() && "Isn't a File SourceLocation");
435
436 // TODO: Add a flag "is first chunk" to SLOC.
437 const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(Loc.getFileID());
438
439 // If this file has been split up into chunks, factor in the chunk number
440 // that the FileID references.
441 unsigned ChunkNo = FIDInfo->getChunkNo();
442 unsigned Offset = Loc.getRawFilePos();
443 Offset += (ChunkNo << SourceLocation::FilePosBits);
Nico Weber2bbdcae2008-08-09 22:13:42 +0000444
445 assert(Loc.getFileID() >= ChunkNo && "Unexpected offset");
Chris Lattner136df562007-10-12 20:24:19 +0000446
Nico Weber2bbdcae2008-08-09 22:13:42 +0000447 return std::make_pair(Loc.getFileID()-ChunkNo, Offset);
Chris Lattner136df562007-10-12 20:24:19 +0000448 }
Ted Kremeneka0bc14d2008-04-08 21:26:35 +0000449
450 /// getFullFilePos - This (efficient) method returns the offset from the start
Chris Lattnercdf600e2009-01-16 07:00:02 +0000451 /// of the file that the specified spelling SourceLocation represents. This
Chris Lattner18c8dc02009-01-16 07:36:28 +0000452 /// returns the location of the actual character data, not the instantiation
Ted Kremeneka0bc14d2008-04-08 21:26:35 +0000453 /// position.
Chris Lattnercdf600e2009-01-16 07:00:02 +0000454 unsigned getFullFilePos(SourceLocation SpellingLoc) const {
455 return getDecomposedFileLoc(SpellingLoc).second;
Ted Kremeneka0bc14d2008-04-08 21:26:35 +0000456 }
Chris Lattner136df562007-10-12 20:24:19 +0000457
Ted Kremenek81019d72008-04-14 21:04:18 +0000458 /// isFromSameFile - Returns true if both SourceLocations correspond to
459 /// the same file.
460 bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
461 return getCanonicalFileID(Loc1) == getCanonicalFileID(Loc2);
462 }
463
464 /// isFromMainFile - Returns true if the file of provided SourceLocation is
465 /// the main file.
466 bool isFromMainFile(SourceLocation Loc) const {
467 return getCanonicalFileID(Loc) == getMainFileID();
468 }
Nico Weberd2a6ac92008-08-10 19:59:06 +0000469
470 /// isInSystemHeader - Returns if a SourceLocation is in a system header.
471 bool isInSystemHeader(SourceLocation Loc) const {
Chris Lattner6f044062008-09-26 21:18:42 +0000472 return getFileCharacteristic(Loc) != SrcMgr::C_User;
Nico Weberd2a6ac92008-08-10 19:59:06 +0000473 }
Chris Lattner7a4864e2008-10-27 01:19:25 +0000474 SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const {
Chris Lattnercdf600e2009-01-16 07:00:02 +0000475 return getFIDInfo(getSpellingLoc(Loc).getFileID())->getFileCharacteristic();
Chris Lattner7257ce22008-09-26 20:12:23 +0000476 }
Ted Kremenekd111a252008-11-19 22:41:46 +0000477 SrcMgr::CharacteristicKind getFileCharacteristic(unsigned FileID) const {
478 return getFIDInfo(FileID)->getFileCharacteristic();
479 }
480
Ted Kremenek431f35b2008-10-21 03:32:32 +0000481 // Iterators over FileIDs.
482
483 class fileid_iterator {
484 std::vector<SrcMgr::FileIDInfo>::iterator I;
485 unsigned fid;
486 public:
487 fileid_iterator(std::vector<SrcMgr::FileIDInfo>::iterator i, unsigned f)
488 : I(i), fid(f) {}
489
490 bool operator==(const fileid_iterator& X) const { return X.fid == fid; }
491 bool operator!=(const fileid_iterator& X) const { return X.fid != fid; }
492 fileid_iterator& operator++() { ++fid; ++I; return *this; }
493
494 unsigned getFileID() const { return fid; }
495 SrcMgr::FileIDInfo& getFileIDInfo() { return *I; }
496 };
497
498 fileid_iterator fileid_begin() {
499 return fileid_iterator(FileIDs.begin(), 1);
500 }
501
502 fileid_iterator fileid_end() {
503 return fileid_iterator(FileIDs.end(), FileIDs.size()+1);
504 }
Ted Kremenek81019d72008-04-14 21:04:18 +0000505
Chris Lattner4b009652007-07-25 00:24:17 +0000506 /// PrintStats - Print statistics to stderr.
507 ///
508 void PrintStats() const;
Chris Lattner4b009652007-07-25 00:24:17 +0000509
Ted Kremenek9c856e92007-12-05 00:14:18 +0000510 /// Emit - Emit this SourceManager to Bitcode.
511 void Emit(llvm::Serializer& S) const;
512
513 /// Read - Reconstitute a SourceManager from Bitcode.
Ted Kremenekbc54abf2007-12-05 00:19:51 +0000514 static SourceManager* CreateAndRegister(llvm::Deserializer& S,
515 FileManager &FMgr);
Ted Kremenek9c856e92007-12-05 00:14:18 +0000516
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000517private:
Cédric Venet0009c942008-06-26 12:50:52 +0000518 friend struct SrcMgr::ContentCache; // Used for deserialization.
Ted Kremenek9c856e92007-12-05 00:14:18 +0000519
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000520 /// createFileID - Create a new fileID for the specified ContentCache and
521 /// include position. This works regardless of whether the ContentCache
522 /// corresponds to a file or some other input source.
523 unsigned createFileID(const SrcMgr::ContentCache* File,
Chris Lattner6f044062008-09-26 21:18:42 +0000524 SourceLocation IncludePos,
Chris Lattner7a4864e2008-10-27 01:19:25 +0000525 SrcMgr::CharacteristicKind DirCharacter);
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000526
527 /// getContentCache - Create or return a cached ContentCache for the specified
528 /// file. This returns null on failure.
529 const SrcMgr::ContentCache* getContentCache(const FileEntry* SourceFile);
Ted Kremenekaa7dac12009-01-06 01:55:26 +0000530
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000531 /// createMemBufferContentCache - Create a new ContentCache for the specified
532 /// memory buffer.
533 const SrcMgr::ContentCache*
534 createMemBufferContentCache(const llvm::MemoryBuffer* Buf);
535
536 const SrcMgr::FileIDInfo* getFIDInfo(unsigned FileID) const {
Chris Lattner4b009652007-07-25 00:24:17 +0000537 assert(FileID-1 < FileIDs.size() && "Invalid FileID!");
538 return &FileIDs[FileID-1];
539 }
540
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000541 const SrcMgr::ContentCache *getContentCache(unsigned FileID) const {
542 return getContentCache(getFIDInfo(FileID));
Chris Lattner4b009652007-07-25 00:24:17 +0000543 }
544
Ted Kremenekdd364ea2007-10-30 21:08:08 +0000545 /// Return the ContentCache structure for the specified FileID.
546 /// This is always the physical reference for the ID.
547 const SrcMgr::ContentCache*
548 getContentCache(const SrcMgr::FileIDInfo* FIDInfo) const {
549 return FIDInfo->getContentCache();
550 }
Chris Lattner4b009652007-07-25 00:24:17 +0000551};
552
553
554} // end namespace clang
555
556#endif