blob: a4aee0af38faabed7773acaacec8905db9e4de58 [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +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"
Chris Lattner0d0bf8c2009-02-03 07:30:45 +000018#include "llvm/Support/Allocator.h"
Chandler Carruth9f8eb202009-10-26 01:37:10 +000019#include "llvm/System/DataTypes.h"
Douglas Gregorc8151082010-03-16 22:53:51 +000020#include "llvm/ADT/PointerIntPair.h"
Douglas Gregoraea67db2010-03-15 22:54:52 +000021#include "llvm/ADT/PointerUnion.h"
Chris Lattner0d0bf8c2009-02-03 07:30:45 +000022#include "llvm/ADT/DenseMap.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000023#include <vector>
Chris Lattner9dc62f02007-07-12 15:32:57 +000024#include <cassert>
Reid Spencer5f016e22007-07-11 17:01:13 +000025
26namespace llvm {
27class MemoryBuffer;
Douglas Gregoraea67db2010-03-15 22:54:52 +000028class StringRef;
Reid Spencer5f016e22007-07-11 17:01:13 +000029}
Mike Stump1eb44332009-09-09 15:08:12 +000030
Reid Spencer5f016e22007-07-11 17:01:13 +000031namespace clang {
Mike Stump1eb44332009-09-09 15:08:12 +000032
Douglas Gregoraea67db2010-03-15 22:54:52 +000033class Diagnostic;
Reid Spencer5f016e22007-07-11 17:01:13 +000034class SourceManager;
Ted Kremenek099b4742007-12-05 00:14:18 +000035class FileManager;
Reid Spencer5f016e22007-07-11 17:01:13 +000036class FileEntry;
Chris Lattner5b9a5042009-01-26 07:57:50 +000037class LineTableInfo;
Douglas Gregoraea67db2010-03-15 22:54:52 +000038
Chris Lattner0b9e7362008-09-26 21:18:42 +000039/// SrcMgr - Public enums and private classes that are part of the
40/// SourceManager implementation.
Reid Spencer5f016e22007-07-11 17:01:13 +000041///
42namespace SrcMgr {
Chris Lattner9d728512008-10-27 01:19:25 +000043 /// CharacteristicKind - This is used to represent whether a file or directory
Chris Lattner0b9e7362008-09-26 21:18:42 +000044 /// holds normal user code, system code, or system code which is implicitly
45 /// 'extern "C"' in C++ mode. Entire directories can be tagged with this
46 /// (this is maintained by DirectoryLookup and friends) as can specific
47 /// FileIDInfos when a #pragma system_header is seen or various other cases.
48 ///
Chris Lattner9d728512008-10-27 01:19:25 +000049 enum CharacteristicKind {
Chris Lattner0b9e7362008-09-26 21:18:42 +000050 C_User, C_System, C_ExternCSystem
51 };
Mike Stump1eb44332009-09-09 15:08:12 +000052
Dan Gohman4710a8e2010-08-25 21:59:25 +000053 /// ContentCache - One instance of this struct is kept for every file
Chris Lattner06a062d2009-01-19 08:02:45 +000054 /// loaded or used. This object owns the MemoryBuffer object.
Ted Kremenekc16c2082009-01-06 01:55:26 +000055 class ContentCache {
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +000056 enum CCFlags {
57 /// \brief Whether the buffer is invalid.
58 InvalidFlag = 0x01,
59 /// \brief Whether the buffer should not be freed on destruction.
60 DoNotFreeFlag = 0x02
61 };
62
Ted Kremenekc16c2082009-01-06 01:55:26 +000063 /// Buffer - The actual buffer containing the characters from the input
64 /// file. This is owned by the ContentCache object.
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +000065 /// The bits indicate indicates whether the buffer is invalid.
66 mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
Ted Kremenekc16c2082009-01-06 01:55:26 +000067
68 public:
Ted Kremenek78d85f52007-10-30 21:08:08 +000069 /// Reference to the file entry. This reference does not own
70 /// the FileEntry object. It is possible for this to be NULL if
71 /// the ContentCache encapsulates an imaginary text buffer.
Chris Lattner05816592009-01-17 03:54:16 +000072 const FileEntry *Entry;
Mike Stump1eb44332009-09-09 15:08:12 +000073
Chris Lattner0d0bf8c2009-02-03 07:30:45 +000074 /// SourceLineCache - A bump pointer allocated array of offsets for each
75 /// source line. This is lazily computed. This is owned by the
76 /// SourceManager BumpPointerAllocator object.
Chris Lattner05816592009-01-17 03:54:16 +000077 unsigned *SourceLineCache;
Mike Stump1eb44332009-09-09 15:08:12 +000078
Ted Kremenekb6427f82007-12-04 18:59:28 +000079 /// NumLines - The number of lines in this ContentCache. This is only valid
80 /// if SourceLineCache is non-null.
Reid Spencer5f016e22007-07-11 17:01:13 +000081 unsigned NumLines;
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +000082
Douglas Gregor36c35ba2010-03-16 00:35:39 +000083 /// getBuffer - Returns the memory buffer for the associated content.
84 ///
85 /// \param Diag Object through which diagnostics will be emitted it the
86 /// buffer cannot be retrieved.
87 ///
Chris Lattnere127a0d2010-04-20 20:35:58 +000088 /// \param Loc If specified, is the location that invalid file diagnostics
89 /// will be emitted at.
90 ///
Douglas Gregor36c35ba2010-03-16 00:35:39 +000091 /// \param Invalid If non-NULL, will be set \c true if an error occurred.
Chris Lattnere127a0d2010-04-20 20:35:58 +000092 const llvm::MemoryBuffer *getBuffer(Diagnostic &Diag,
93 const SourceManager &SM,
94 SourceLocation Loc = SourceLocation(),
Douglas Gregor36c35ba2010-03-16 00:35:39 +000095 bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +000096
Ted Kremenekc16c2082009-01-06 01:55:26 +000097 /// getSize - Returns the size of the content encapsulated by this
98 /// ContentCache. This can be the size of the source file or the size of an
99 /// arbitrary scratch buffer. If the ContentCache encapsulates a source
100 /// file this size is retrieved from the file's FileEntry.
101 unsigned getSize() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenekc16c2082009-01-06 01:55:26 +0000103 /// getSizeBytesMapped - Returns the number of bytes actually mapped for
104 /// this ContentCache. This can be 0 if the MemBuffer was not actually
105 /// instantiated.
106 unsigned getSizeBytesMapped() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Chris Lattner05816592009-01-17 03:54:16 +0000108 void setBuffer(const llvm::MemoryBuffer *B) {
Douglas Gregorc8151082010-03-16 22:53:51 +0000109 assert(!Buffer.getPointer() && "MemoryBuffer already set.");
110 Buffer.setPointer(B);
111 Buffer.setInt(false);
Ted Kremenekc16c2082009-01-06 01:55:26 +0000112 }
Douglas Gregorcc5888d2010-07-31 00:40:00 +0000113
114 /// \brief Get the underlying buffer, returning NULL if the buffer is not
115 /// yet available.
116 const llvm::MemoryBuffer *getRawBuffer() const {
117 return Buffer.getPointer();
118 }
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Douglas Gregor29684422009-12-02 06:49:09 +0000120 /// \brief Replace the existing buffer (which will be deleted)
121 /// with the given buffer.
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000122 void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
Douglas Gregor29684422009-12-02 06:49:09 +0000123
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000124 /// \brief Determine whether the buffer itself is invalid.
125 bool isBufferInvalid() const {
126 return Buffer.getInt() & InvalidFlag;
127 }
128
129 /// \brief Determine whether the buffer should be freed.
130 bool shouldFreeBuffer() const {
131 return (Buffer.getInt() & DoNotFreeFlag) == 0;
132 }
133
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000134 ContentCache(const FileEntry *Ent = 0)
Douglas Gregorc8151082010-03-16 22:53:51 +0000135 : Buffer(0, false), Entry(Ent), SourceLineCache(0), NumLines(0) {}
Ted Kremenek78d85f52007-10-30 21:08:08 +0000136
137 ~ContentCache();
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Ted Kremenek0d892d82007-10-30 22:57:35 +0000139 /// The copy ctor does not allow copies where source object has either
140 /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory
141 /// is not transfered, so this is a logical error.
Douglas Gregorc8151082010-03-16 22:53:51 +0000142 ContentCache(const ContentCache &RHS)
143 : Buffer(0, false), SourceLineCache(0)
144 {
Ted Kremenek0d892d82007-10-30 22:57:35 +0000145 Entry = RHS.Entry;
146
Douglas Gregorc8151082010-03-16 22:53:51 +0000147 assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0
Ted Kremenek0d892d82007-10-30 22:57:35 +0000148 && "Passed ContentCache object cannot own a buffer.");
Mike Stump1eb44332009-09-09 15:08:12 +0000149
150 NumLines = RHS.NumLines;
Ted Kremenek0d892d82007-10-30 22:57:35 +0000151 }
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Ted Kremenek0d892d82007-10-30 22:57:35 +0000153 private:
154 // Disable assignments.
Mike Stump1eb44332009-09-09 15:08:12 +0000155 ContentCache &operator=(const ContentCache& RHS);
156 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000157
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000158 /// FileInfo - Information about a FileID, basically just the logical file
159 /// that it represents and include stack information.
Reid Spencer5f016e22007-07-11 17:01:13 +0000160 ///
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000161 /// Each FileInfo has include stack information, indicating where it came
162 /// from. This information encodes the #include chain that a token was
163 /// instantiated from. The main include file has an invalid IncludeLoc.
Reid Spencer5f016e22007-07-11 17:01:13 +0000164 ///
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000165 /// FileInfos contain a "ContentCache *", with the contents of the file.
Reid Spencer5f016e22007-07-11 17:01:13 +0000166 ///
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000167 class FileInfo {
Reid Spencer5f016e22007-07-11 17:01:13 +0000168 /// IncludeLoc - The location of the #include that brought in this file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000169 /// This is an invalid SLOC for the main file (top of the #include chain).
170 unsigned IncludeLoc; // Really a SourceLocation
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Chris Lattner6e1aff22009-01-26 06:49:09 +0000172 /// Data - This contains the ContentCache* and the bits indicating the
173 /// characteristic of the file and whether it has #line info, all bitmangled
174 /// together.
175 uintptr_t Data;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000176 public:
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000177 /// get - Return a FileInfo object.
178 static FileInfo get(SourceLocation IL, const ContentCache *Con,
179 CharacteristicKind FileCharacter) {
180 FileInfo X;
181 X.IncludeLoc = IL.getRawEncoding();
Chris Lattner6e1aff22009-01-26 06:49:09 +0000182 X.Data = (uintptr_t)Con;
Chris Lattner00282d62009-02-03 07:41:46 +0000183 assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
Chris Lattner6e1aff22009-01-26 06:49:09 +0000184 assert((unsigned)FileCharacter < 4 && "invalid file character");
185 X.Data |= (unsigned)FileCharacter;
Reid Spencer5f016e22007-07-11 17:01:13 +0000186 return X;
187 }
Mike Stump1eb44332009-09-09 15:08:12 +0000188
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000189 SourceLocation getIncludeLoc() const {
190 return SourceLocation::getFromRawEncoding(IncludeLoc);
191 }
Chris Lattner6e1aff22009-01-26 06:49:09 +0000192 const ContentCache* getContentCache() const {
Chris Lattner00282d62009-02-03 07:41:46 +0000193 return reinterpret_cast<const ContentCache*>(Data & ~7UL);
Chris Lattner6e1aff22009-01-26 06:49:09 +0000194 }
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Chris Lattner0b9e7362008-09-26 21:18:42 +0000196 /// getCharacteristic - Return whether this is a system header or not.
Mike Stump1eb44332009-09-09 15:08:12 +0000197 CharacteristicKind getFileCharacteristic() const {
Chris Lattner6e1aff22009-01-26 06:49:09 +0000198 return (CharacteristicKind)(Data & 3);
Chris Lattner0b9e7362008-09-26 21:18:42 +0000199 }
Chris Lattnerac50e342009-02-03 22:13:05 +0000200
201 /// hasLineDirectives - Return true if this FileID has #line directives in
202 /// it.
203 bool hasLineDirectives() const { return (Data & 4) != 0; }
Mike Stump1eb44332009-09-09 15:08:12 +0000204
Chris Lattnerac50e342009-02-03 22:13:05 +0000205 /// setHasLineDirectives - Set the flag that indicates that this FileID has
206 /// line table entries associated with it.
207 void setHasLineDirectives() {
208 Data |= 4;
209 }
Chris Lattner9dc1f532007-07-20 16:37:10 +0000210 };
Mike Stump1eb44332009-09-09 15:08:12 +0000211
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000212 /// InstantiationInfo - Each InstantiationInfo encodes the Instantiation
213 /// location - where the token was ultimately instantiated, and the
214 /// SpellingLoc - where the actual character data for the token came from.
215 class InstantiationInfo {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000216 // Really these are all SourceLocations.
Mike Stump1eb44332009-09-09 15:08:12 +0000217
Chris Lattnere7fb4842009-02-15 20:52:18 +0000218 /// SpellingLoc - Where the spelling for the token can be found.
219 unsigned SpellingLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000220
Chris Lattnere7fb4842009-02-15 20:52:18 +0000221 /// InstantiationLocStart/InstantiationLocEnd - In a macro expansion, these
Douglas Gregor14f79002009-04-10 03:52:48 +0000222 /// indicate the start and end of the instantiation. In object-like macros,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000223 /// these will be the same. In a function-like macro instantiation, the
224 /// start will be the identifier and the end will be the ')'.
225 unsigned InstantiationLocStart, InstantiationLocEnd;
Chris Lattner9dc1f532007-07-20 16:37:10 +0000226 public:
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000227 SourceLocation getSpellingLoc() const {
228 return SourceLocation::getFromRawEncoding(SpellingLoc);
229 }
Chris Lattnere7fb4842009-02-15 20:52:18 +0000230 SourceLocation getInstantiationLocStart() const {
231 return SourceLocation::getFromRawEncoding(InstantiationLocStart);
232 }
233 SourceLocation getInstantiationLocEnd() const {
234 return SourceLocation::getFromRawEncoding(InstantiationLocEnd);
235 }
Mike Stump1eb44332009-09-09 15:08:12 +0000236
Chris Lattnere7fb4842009-02-15 20:52:18 +0000237 std::pair<SourceLocation,SourceLocation> getInstantiationLocRange() const {
238 return std::make_pair(getInstantiationLocStart(),
239 getInstantiationLocEnd());
240 }
Mike Stump1eb44332009-09-09 15:08:12 +0000241
Chris Lattnere7fb4842009-02-15 20:52:18 +0000242 /// get - Return a InstantiationInfo for an expansion. IL specifies
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000243 /// the instantiation location (where the macro is expanded), and SL
244 /// specifies the spelling location (where the characters from the token
Chris Lattnere7fb4842009-02-15 20:52:18 +0000245 /// come from). IL and PL can both refer to normal File SLocs or
246 /// instantiation locations.
247 static InstantiationInfo get(SourceLocation ILStart, SourceLocation ILEnd,
248 SourceLocation SL) {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000249 InstantiationInfo X;
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000250 X.SpellingLoc = SL.getRawEncoding();
Chris Lattnere7fb4842009-02-15 20:52:18 +0000251 X.InstantiationLocStart = ILStart.getRawEncoding();
252 X.InstantiationLocEnd = ILEnd.getRawEncoding();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000253 return X;
Reid Spencer5f016e22007-07-11 17:01:13 +0000254 }
255 };
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000257 /// SLocEntry - This is a discriminated union of FileInfo and
258 /// InstantiationInfo. SourceManager keeps an array of these objects, and
259 /// they are uniquely identified by the FileID datatype.
260 class SLocEntry {
261 unsigned Offset; // low bit is set for instantiation info.
262 union {
263 FileInfo File;
264 InstantiationInfo Instantiation;
265 };
266 public:
267 unsigned getOffset() const { return Offset >> 1; }
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000269 bool isInstantiation() const { return Offset & 1; }
270 bool isFile() const { return !isInstantiation(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000272 const FileInfo &getFile() const {
273 assert(isFile() && "Not a file SLocEntry!");
274 return File;
275 }
276
277 const InstantiationInfo &getInstantiation() const {
278 assert(isInstantiation() && "Not an instantiation SLocEntry!");
279 return Instantiation;
280 }
Mike Stump1eb44332009-09-09 15:08:12 +0000281
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000282 static SLocEntry get(unsigned Offset, const FileInfo &FI) {
283 SLocEntry E;
284 E.Offset = Offset << 1;
285 E.File = FI;
286 return E;
287 }
288
289 static SLocEntry get(unsigned Offset, const InstantiationInfo &II) {
290 SLocEntry E;
291 E.Offset = (Offset << 1) | 1;
292 E.Instantiation = II;
293 return E;
294 }
295 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000296} // end SrcMgr namespace.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000297
298/// \brief External source of source location entries.
299class ExternalSLocEntrySource {
300public:
301 virtual ~ExternalSLocEntrySource();
302
303 /// \brief Read the source location entry with index ID.
304 virtual void ReadSLocEntry(unsigned ID) = 0;
305};
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000306
307
308/// IsBeforeInTranslationUnitCache - This class holds the cache used by
309/// isBeforeInTranslationUnit. The cache structure is complex enough to be
310/// worth breaking out of SourceManager.
311class IsBeforeInTranslationUnitCache {
312 /// L/R QueryFID - These are the FID's of the cached query. If these match up
313 /// with a subsequent query, the result can be reused.
314 FileID LQueryFID, RQueryFID;
315
316 /// CommonFID - This is the file found in common between the two #include
317 /// traces. It is the nearest common ancestor of the #include tree.
318 FileID CommonFID;
319
320 /// L/R CommonOffset - This is the offset of the previous query in CommonFID.
321 /// Usually, this represents the location of the #include for QueryFID, but if
322 /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a
323 /// random token in the parent.
324 unsigned LCommonOffset, RCommonOffset;
325public:
326
327 /// isCacheValid - Return true if the currently cached values match up with
328 /// the specified LHS/RHS query. If not, we can't use the cache.
329 bool isCacheValid(FileID LHS, FileID RHS) const {
330 return LQueryFID == LHS && RQueryFID == RHS;
331 }
332
333 /// getCachedResult - If the cache is valid, compute the result given the
334 /// specified offsets in the LHS/RHS FID's.
335 bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
336 // If one of the query files is the common file, use the offset. Otherwise,
337 // use the #include loc in the common file.
338 if (LQueryFID != CommonFID) LOffset = LCommonOffset;
339 if (RQueryFID != CommonFID) ROffset = RCommonOffset;
340 return LOffset < ROffset;
341 }
342
343 // Set up a new query.
344 void setQueryFIDs(FileID LHS, FileID RHS) {
345 LQueryFID = LHS;
346 RQueryFID = RHS;
347 }
348
349 void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
350 unsigned rCommonOffset) {
351 CommonFID = commonFID;
352 LCommonOffset = lCommonOffset;
353 RCommonOffset = rCommonOffset;
354 }
355
356};
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000357
Reid Spencer5f016e22007-07-11 17:01:13 +0000358/// SourceManager - This file handles loading and caching of source files into
359/// memory. This object owns the MemoryBuffer objects for all of the loaded
360/// files and assigns unique FileID's for each unique #include chain.
361///
362/// The SourceManager can be queried for information about SourceLocation
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000363/// objects, turning them into either spelling or instantiation locations.
364/// Spelling locations represent where the bytes corresponding to a token came
365/// from and instantiation locations represent where the location is in the
366/// user's view. In the case of a macro expansion, for example, the spelling
367/// location indicates where the expanded token came from and the instantiation
368/// location specifies where it was expanded.
Reid Spencer5f016e22007-07-11 17:01:13 +0000369class SourceManager {
Douglas Gregorf715ca12010-03-16 00:06:06 +0000370 /// \brief Diagnostic object.
371 Diagnostic &Diag;
372
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000373 mutable llvm::BumpPtrAllocator ContentCacheAlloc;
Mike Stump1eb44332009-09-09 15:08:12 +0000374
Reid Spencer5f016e22007-07-11 17:01:13 +0000375 /// FileInfos - Memoized information about all of the files tracked by this
Ted Kremenek0d892d82007-10-30 22:57:35 +0000376 /// SourceManager. This set allows us to merge ContentCache entries based
377 /// on their FileEntry*. All ContentCache objects will thus have unique,
Mike Stump1eb44332009-09-09 15:08:12 +0000378 /// non-null, FileEntry pointers.
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000379 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000380
Reid Spencer5f016e22007-07-11 17:01:13 +0000381 /// MemBufferInfos - Information about various memory buffers that we have
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000382 /// read in. All FileEntry* within the stored ContentCache objects are NULL,
383 /// as they do not refer to a file.
384 std::vector<SrcMgr::ContentCache*> MemBufferInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000385
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000386 /// SLocEntryTable - This is an array of SLocEntry's that we have created.
387 /// FileID is an index into this vector. This array is sorted by the offset.
388 std::vector<SrcMgr::SLocEntry> SLocEntryTable;
389 /// NextOffset - This is the next available offset that a new SLocEntry can
390 /// start at. It is SLocEntryTable.back().getOffset()+size of back() entry.
391 unsigned NextOffset;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000392
393 /// \brief If source location entries are being lazily loaded from
394 /// an external source, this vector indicates whether the Ith source
395 /// location entry has already been loaded from the external storage.
396 std::vector<bool> SLocEntryLoaded;
397
398 /// \brief An external source for source location entries.
399 ExternalSLocEntrySource *ExternalSLocEntries;
400
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000401 /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
402 /// LastFileIDLookup records the last FileID looked up or created, because it
403 /// is very common to look up many tokens from the same file.
404 mutable FileID LastFileIDLookup;
Mike Stump1eb44332009-09-09 15:08:12 +0000405
Chris Lattner5b9a5042009-01-26 07:57:50 +0000406 /// LineTable - This holds information for #line directives. It is referenced
407 /// by indices from SLocEntryTable.
408 LineTableInfo *LineTable;
Mike Stump1eb44332009-09-09 15:08:12 +0000409
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000410 /// LastLineNo - These ivars serve as a cache used in the getLineNumber
411 /// method which is used to speedup getLineNumber calls to nearby locations.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000412 mutable FileID LastLineNoFileIDQuery;
Chris Lattnerf812a452008-11-18 06:51:15 +0000413 mutable SrcMgr::ContentCache *LastLineNoContentCache;
414 mutable unsigned LastLineNoFilePos;
415 mutable unsigned LastLineNoResult;
Mike Stump1eb44332009-09-09 15:08:12 +0000416
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000417 /// MainFileID - The file ID for the main source file of the translation unit.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000418 FileID MainFileID;
Steve Naroff49c1f4a2008-02-02 00:10:46 +0000419
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000420 // Statistics for -print-stats.
421 mutable unsigned NumLinearScans, NumBinaryProbes;
Mike Stump1eb44332009-09-09 15:08:12 +0000422
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +0000423 // Cache results for the isBeforeInTranslationUnit method.
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000424 mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
Mike Stump1eb44332009-09-09 15:08:12 +0000425
Steve Naroff49c1f4a2008-02-02 00:10:46 +0000426 // SourceManager doesn't support copy construction.
427 explicit SourceManager(const SourceManager&);
Mike Stump1eb44332009-09-09 15:08:12 +0000428 void operator=(const SourceManager&);
Reid Spencer5f016e22007-07-11 17:01:13 +0000429public:
Douglas Gregorf715ca12010-03-16 00:06:06 +0000430 SourceManager(Diagnostic &Diag)
431 : Diag(Diag), ExternalSLocEntries(0), LineTable(0), NumLinearScans(0),
Douglas Gregor29684422009-12-02 06:49:09 +0000432 NumBinaryProbes(0) {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000433 clearIDTables();
434 }
Chris Lattner5b9a5042009-01-26 07:57:50 +0000435 ~SourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000436
Chris Lattner5b9a5042009-01-26 07:57:50 +0000437 void clearIDTables();
Mike Stump1eb44332009-09-09 15:08:12 +0000438
Chris Lattner06a062d2009-01-19 08:02:45 +0000439 //===--------------------------------------------------------------------===//
440 // MainFileID creation and querying methods.
441 //===--------------------------------------------------------------------===//
442
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000443 /// getMainFileID - Returns the FileID of the main source file.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000444 FileID getMainFileID() const { return MainFileID; }
Mike Stump1eb44332009-09-09 15:08:12 +0000445
Chris Lattner06a062d2009-01-19 08:02:45 +0000446 /// createMainFileID - Create the FileID for the main source file.
447 FileID createMainFileID(const FileEntry *SourceFile,
448 SourceLocation IncludePos) {
449 assert(MainFileID.isInvalid() && "MainFileID already set!");
450 MainFileID = createFileID(SourceFile, IncludePos, SrcMgr::C_User);
451 return MainFileID;
452 }
Mike Stump1eb44332009-09-09 15:08:12 +0000453
Chris Lattner06a062d2009-01-19 08:02:45 +0000454 //===--------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000455 // Methods to create new FileID's and instantiations.
Chris Lattner06a062d2009-01-19 08:02:45 +0000456 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000457
Reid Spencer5f016e22007-07-11 17:01:13 +0000458 /// createFileID - Create a new FileID that represents the specified file
459 /// being #included from the specified IncludePosition. This returns 0 on
460 /// error and translates NULL into standard input.
Sebastian Redl190faf72010-07-20 21:50:20 +0000461 /// PreallocateID should be non-zero to specify which pre-allocated,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000462 /// lazily computed source location is being filled in by this operation.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000463 FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000464 SrcMgr::CharacteristicKind FileCharacter,
465 unsigned PreallocatedID = 0,
466 unsigned Offset = 0) {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000467 const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000468 if (IR == 0) return FileID(); // Error opening file?
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000469 return createFileID(IR, IncludePos, FileCharacter, PreallocatedID, Offset);
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 }
Mike Stump1eb44332009-09-09 15:08:12 +0000471
Reid Spencer5f016e22007-07-11 17:01:13 +0000472 /// createFileIDForMemBuffer - Create a new FileID that represents the
473 /// specified memory buffer. This does no caching of the buffer and takes
474 /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000475 FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
476 unsigned PreallocatedID = 0,
477 unsigned Offset = 0) {
Nico Weber7bfaaae2008-08-10 19:59:06 +0000478 return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000479 SrcMgr::C_User, PreallocatedID, Offset);
Reid Spencer5f016e22007-07-11 17:01:13 +0000480 }
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Ted Kremenek1036b682007-12-19 23:48:45 +0000482 /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
483 /// that will represent the FileID for the main source. One example
484 /// of when this would be used is when the main source is read from STDIN.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000485 FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
486 assert(MainFileID.isInvalid() && "MainFileID already set!");
Chris Lattner7697b5c2007-12-20 01:38:17 +0000487 MainFileID = createFileIDForMemBuffer(Buffer);
Ted Kremenek1036b682007-12-19 23:48:45 +0000488 return MainFileID;
489 }
Chris Lattner06a062d2009-01-19 08:02:45 +0000490
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000491 /// createInstantiationLoc - Return a new SourceLocation that encodes the fact
492 /// that a token at Loc should actually be referenced from InstantiationLoc.
493 /// TokLength is the length of the token being instantiated.
494 SourceLocation createInstantiationLoc(SourceLocation Loc,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000495 SourceLocation InstantiationLocStart,
496 SourceLocation InstantiationLocEnd,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000497 unsigned TokLength,
498 unsigned PreallocatedID = 0,
499 unsigned Offset = 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Douglas Gregor29684422009-12-02 06:49:09 +0000501 /// \brief Retrieve the memory buffer associated with the given file.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000502 ///
503 /// \param Invalid If non-NULL, will be set \c true if an error
504 /// occurs while retrieving the memory buffer.
505 const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
506 bool *Invalid = 0);
Douglas Gregor29684422009-12-02 06:49:09 +0000507
508 /// \brief Override the contents of the given source file by providing an
509 /// already-allocated buffer.
510 ///
Dan Gohmanafbf5f82010-08-26 02:27:03 +0000511 /// \param SourceFile the source file whose contents will be overriden.
Douglas Gregor29684422009-12-02 06:49:09 +0000512 ///
513 /// \param Buffer the memory buffer whose contents will be used as the
514 /// data in the given source file.
515 ///
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000516 /// \param DoNotFree If true, then the buffer will not be freed when the
517 /// source manager is destroyed.
518 ///
Douglas Gregor29684422009-12-02 06:49:09 +0000519 /// \returns true if an error occurred, false otherwise.
520 bool overrideFileContents(const FileEntry *SourceFile,
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000521 const llvm::MemoryBuffer *Buffer,
522 bool DoNotFree = false);
Douglas Gregor29684422009-12-02 06:49:09 +0000523
Chris Lattner06a062d2009-01-19 08:02:45 +0000524 //===--------------------------------------------------------------------===//
525 // FileID manipulation methods.
526 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000527
Daniel Dunbar2ffb14f2009-12-06 09:19:25 +0000528 /// getBuffer - Return the buffer for the specified FileID. If there is an
529 /// error opening this buffer the first time, this manufactures a temporary
530 /// buffer and returns a non-empty error string.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000531 const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
532 bool *Invalid = 0) const {
533 return getSLocEntry(FID).getFile().getContentCache()
534 ->getBuffer(Diag, *this, Loc, Invalid);
Chris Lattner06a062d2009-01-19 08:02:45 +0000535 }
Mike Stump1eb44332009-09-09 15:08:12 +0000536
Chris Lattnere127a0d2010-04-20 20:35:58 +0000537 const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
538 return getSLocEntry(FID).getFile().getContentCache()
539 ->getBuffer(Diag, *this, SourceLocation(), Invalid);
540 }
541
Chris Lattner06a062d2009-01-19 08:02:45 +0000542 /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
543 const FileEntry *getFileEntryForID(FileID FID) const {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000544 return getSLocEntry(FID).getFile().getContentCache()->Entry;
Chris Lattner06a062d2009-01-19 08:02:45 +0000545 }
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Benjamin Kramerceafc4b2010-03-16 14:48:07 +0000547 /// getBufferData - Return a StringRef to the source buffer data for the
548 /// specified FileID.
549 ///
Douglas Gregorf715ca12010-03-16 00:06:06 +0000550 /// \param FID The file ID whose contents will be returned.
551 /// \param Invalid If non-NULL, will be set true if an error occurred.
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +0000552 llvm::StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
553
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Chris Lattner06a062d2009-01-19 08:02:45 +0000555 //===--------------------------------------------------------------------===//
556 // SourceLocation manipulation methods.
557 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Chris Lattner668ab1a2009-03-13 01:05:57 +0000559 /// getFileID - Return the FileID for a SourceLocation. This is a very
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000560 /// hot method that is used for all SourceManager queries that start with a
561 /// SourceLocation object. It is responsible for finding the entry in
562 /// SLocEntryTable which contains the specified location.
563 ///
564 FileID getFileID(SourceLocation SpellingLoc) const {
565 unsigned SLocOffset = SpellingLoc.getOffset();
Mike Stump1eb44332009-09-09 15:08:12 +0000566
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000567 // If our one-entry cache covers this offset, just return it.
568 if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
569 return LastFileIDLookup;
570
571 return getFileIDSlow(SLocOffset);
572 }
Mike Stump1eb44332009-09-09 15:08:12 +0000573
Chris Lattner2b2453a2009-01-17 06:22:33 +0000574 /// getLocForStartOfFile - Return the source location corresponding to the
575 /// first byte of the specified file.
576 SourceLocation getLocForStartOfFile(FileID FID) const {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000577 assert(FID.ID < SLocEntryTable.size() && "FileID out of range");
578 assert(getSLocEntry(FID).isFile() && "FileID is not a file");
579 unsigned FileOffset = getSLocEntry(FID).getOffset();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000580 return SourceLocation::getFileLoc(FileOffset);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000581 }
Mike Stump1eb44332009-09-09 15:08:12 +0000582
Chris Lattner66781332009-02-15 21:26:50 +0000583 /// getInstantiationLoc - Given a SourceLocation object, return the
584 /// instantiation location referenced by the ID.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000585 SourceLocation getInstantiationLoc(SourceLocation Loc) const {
Chris Lattneraddb7972009-01-26 20:04:19 +0000586 // Handle the non-mapped case inline, defer to out of line code to handle
587 // instantiations.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000588 if (Loc.isFileID()) return Loc;
Chris Lattneraddb7972009-01-26 20:04:19 +0000589 return getInstantiationLocSlowCase(Loc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000590 }
Mike Stump1eb44332009-09-09 15:08:12 +0000591
Chris Lattnere7fb4842009-02-15 20:52:18 +0000592 /// getImmediateInstantiationRange - Loc is required to be an instantiation
593 /// location. Return the start/end of the instantiation information.
594 std::pair<SourceLocation,SourceLocation>
595 getImmediateInstantiationRange(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000596
Chris Lattner66781332009-02-15 21:26:50 +0000597 /// getInstantiationRange - Given a SourceLocation object, return the
598 /// range of tokens covered by the instantiation in the ultimate file.
599 std::pair<SourceLocation,SourceLocation>
600 getInstantiationRange(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000601
602
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000603 /// getSpellingLoc - Given a SourceLocation object, return the spelling
604 /// location referenced by the ID. This is the place where the characters
605 /// that make up the lexed token can be found.
606 SourceLocation getSpellingLoc(SourceLocation Loc) const {
Chris Lattneraddb7972009-01-26 20:04:19 +0000607 // Handle the non-mapped case inline, defer to out of line code to handle
608 // instantiations.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000609 if (Loc.isFileID()) return Loc;
Chris Lattneraddb7972009-01-26 20:04:19 +0000610 return getSpellingLocSlowCase(Loc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000611 }
Mike Stump1eb44332009-09-09 15:08:12 +0000612
Chris Lattner387616e2009-02-17 08:04:48 +0000613 /// getImmediateSpellingLoc - Given a SourceLocation object, return the
614 /// spelling location referenced by the ID. This is the first level down
615 /// towards the place where the characters that make up the lexed token can be
616 /// found. This should not generally be used by clients.
Mike Stump1eb44332009-09-09 15:08:12 +0000617 SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000618
619 /// getDecomposedLoc - Decompose the specified location into a raw FileID +
620 /// Offset pair. The first element is the FileID, the second is the
621 /// offset from the start of the buffer of the location.
622 std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
623 FileID FID = getFileID(Loc);
624 return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset());
625 }
Mike Stump1eb44332009-09-09 15:08:12 +0000626
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000627 /// getDecomposedInstantiationLoc - Decompose the specified location into a
628 /// raw FileID + Offset pair. If the location is an instantiation record,
629 /// walk through it until we find the final location instantiated.
630 std::pair<FileID, unsigned>
631 getDecomposedInstantiationLoc(SourceLocation Loc) const {
632 FileID FID = getFileID(Loc);
633 const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000635 unsigned Offset = Loc.getOffset()-E->getOffset();
636 if (Loc.isFileID())
637 return std::make_pair(FID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000638
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000639 return getDecomposedInstantiationLocSlowCase(E, Offset);
640 }
641
642 /// getDecomposedSpellingLoc - Decompose the specified location into a raw
643 /// FileID + Offset pair. If the location is an instantiation record, walk
644 /// through it until we find its spelling record.
645 std::pair<FileID, unsigned>
646 getDecomposedSpellingLoc(SourceLocation Loc) const {
647 FileID FID = getFileID(Loc);
648 const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000649
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000650 unsigned Offset = Loc.getOffset()-E->getOffset();
651 if (Loc.isFileID())
652 return std::make_pair(FID, Offset);
653 return getDecomposedSpellingLocSlowCase(E, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000654 }
655
Chris Lattner52c29082009-01-27 06:27:13 +0000656 /// getFileOffset - This method returns the offset from the start
657 /// of the file that the specified SourceLocation represents. This is not very
658 /// meaningful for a macro ID.
659 unsigned getFileOffset(SourceLocation SpellingLoc) const {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000660 return getDecomposedLoc(SpellingLoc).second;
661 }
Mike Stump1eb44332009-09-09 15:08:12 +0000662
663
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000664 //===--------------------------------------------------------------------===//
665 // Queries about the code at a SourceLocation.
666 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000667
Reid Spencer5f016e22007-07-11 17:01:13 +0000668 /// getCharacterData - Return a pointer to the start of the specified location
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000669 /// in the appropriate spelling MemoryBuffer.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000670 ///
671 /// \param Invalid If non-NULL, will be set \c true if an error occurs.
672 const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Chris Lattner9dc1f532007-07-20 16:37:10 +0000674 /// getColumnNumber - Return the column # for the specified file position.
675 /// This is significantly cheaper to compute than the line number. This
676 /// returns zero if the column number isn't known. This may only be called on
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000677 /// a file sloc, so you must choose a spelling or instantiation location
678 /// before calling this method.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000679 unsigned getColumnNumber(FileID FID, unsigned FilePos,
680 bool *Invalid = 0) const;
681 unsigned getSpellingColumnNumber(SourceLocation Loc,
682 bool *Invalid = 0) const;
683 unsigned getInstantiationColumnNumber(SourceLocation Loc,
684 bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000685
686
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000687 /// getLineNumber - Given a SourceLocation, return the spelling line number
Reid Spencer5f016e22007-07-11 17:01:13 +0000688 /// for the position indicated. This requires building and caching a table of
689 /// line offsets for the MemoryBuffer, so this is not cheap: use only when
690 /// about to emit a diagnostic.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000691 unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000692
Douglas Gregor50f6af72010-03-16 05:20:39 +0000693 unsigned getInstantiationLineNumber(SourceLocation Loc,
694 bool *Invalid = 0) const;
695 unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000696
Chris Lattnerbff5c512009-02-17 08:39:06 +0000697 /// Return the filename or buffer identifier of the buffer the location is in.
698 /// Note that this name does not respect #line directives. Use getPresumedLoc
699 /// for normal clients.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000700 const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000701
Chris Lattner6b306672009-02-04 05:33:01 +0000702 /// getFileCharacteristic - return the file characteristic of the specified
Mike Stump1eb44332009-09-09 15:08:12 +0000703 /// source location, indicating whether this is a normal file, a system
Chris Lattner6b306672009-02-04 05:33:01 +0000704 /// header, or an "implicit extern C" system header.
705 ///
706 /// This state can be modified with flags on GNU linemarker directives like:
707 /// # 4 "foo.h" 3
708 /// which changes all source locations in the current file after that to be
709 /// considered to be from a system header.
710 SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000711
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000712 /// getPresumedLoc - This method returns the "presumed" location of a
713 /// SourceLocation specifies. A "presumed location" can be modified by #line
714 /// or GNU line marker directives. This provides a view on the data that a
715 /// user should see in diagnostics, for example.
716 ///
717 /// Note that a presumed location is always given as the instantiation point
718 /// of an instantiation location, not at the spelling location.
719 PresumedLoc getPresumedLoc(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000720
Ted Kremenek9fd87b12008-04-14 21:04:18 +0000721 /// isFromSameFile - Returns true if both SourceLocations correspond to
722 /// the same file.
723 bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
Chris Lattnera11d6172009-01-19 07:46:45 +0000724 return getFileID(Loc1) == getFileID(Loc2);
Ted Kremenek9fd87b12008-04-14 21:04:18 +0000725 }
Mike Stump1eb44332009-09-09 15:08:12 +0000726
Ted Kremenek9fd87b12008-04-14 21:04:18 +0000727 /// isFromMainFile - Returns true if the file of provided SourceLocation is
728 /// the main file.
729 bool isFromMainFile(SourceLocation Loc) const {
Chris Lattnera11d6172009-01-19 07:46:45 +0000730 return getFileID(Loc) == getMainFileID();
Mike Stump1eb44332009-09-09 15:08:12 +0000731 }
732
Nico Weber7bfaaae2008-08-10 19:59:06 +0000733 /// isInSystemHeader - Returns if a SourceLocation is in a system header.
734 bool isInSystemHeader(SourceLocation Loc) const {
Chris Lattner0b9e7362008-09-26 21:18:42 +0000735 return getFileCharacteristic(Loc) != SrcMgr::C_User;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000736 }
Mike Stump1eb44332009-09-09 15:08:12 +0000737
Chris Lattner0d456582009-06-13 23:31:51 +0000738 /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
739 /// system header.
740 bool isInExternCSystemHeader(SourceLocation Loc) const {
741 return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
742 }
Mike Stump1eb44332009-09-09 15:08:12 +0000743
Chris Lattner06a062d2009-01-19 08:02:45 +0000744 //===--------------------------------------------------------------------===//
Chris Lattner5b9a5042009-01-26 07:57:50 +0000745 // Line Table Manipulation Routines
746 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000747
Chris Lattner5b9a5042009-01-26 07:57:50 +0000748 /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
Mike Stump1eb44332009-09-09 15:08:12 +0000749 ///
Chris Lattner5b9a5042009-01-26 07:57:50 +0000750 unsigned getLineTableFilenameID(const char *Ptr, unsigned Len);
Mike Stump1eb44332009-09-09 15:08:12 +0000751
Chris Lattner4c4ea172009-02-03 21:52:55 +0000752 /// AddLineNote - Add a line note to the line table for the FileID and offset
753 /// specified by Loc. If FilenameID is -1, it is considered to be
754 /// unspecified.
755 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
Chris Lattner9d79eba2009-02-04 05:21:58 +0000756 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
Mike Stump1eb44332009-09-09 15:08:12 +0000757 bool IsFileEntry, bool IsFileExit,
Chris Lattner9d79eba2009-02-04 05:21:58 +0000758 bool IsSystemHeader, bool IsExternCHeader);
Douglas Gregorbd945002009-04-13 16:31:14 +0000759
760 /// \brief Determine if the source manager has a line table.
761 bool hasLineTable() const { return LineTable != 0; }
762
763 /// \brief Retrieve the stored line table.
764 LineTableInfo &getLineTable();
765
Chris Lattner5b9a5042009-01-26 07:57:50 +0000766 //===--------------------------------------------------------------------===//
Chris Lattner06a062d2009-01-19 08:02:45 +0000767 // Other miscellaneous methods.
768 //===--------------------------------------------------------------------===//
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +0000769
770 /// \brief Get the source location for the given file:line:col triplet.
771 ///
772 /// If the source file is included multiple times, the source location will
773 /// be based upon the first inclusion.
774 SourceLocation getLocation(const FileEntry *SourceFile,
775 unsigned Line, unsigned Col) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000776
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +0000777 /// \brief Determines the order of 2 source locations in the translation unit.
778 ///
779 /// \returns true if LHS source location comes before RHS, false otherwise.
780 bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
781
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000782 // Iterators over FileInfos.
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000783 typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
784 ::const_iterator fileinfo_iterator;
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000785 fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
786 fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
Douglas Gregord93256e2010-01-28 06:00:51 +0000787 bool hasFileInfo(const FileEntry *File) const {
788 return FileInfos.find(File) != FileInfos.end();
789 }
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000790
Reid Spencer5f016e22007-07-11 17:01:13 +0000791 /// PrintStats - Print statistics to stderr.
792 ///
793 void PrintStats() const;
Reid Spencer5f016e22007-07-11 17:01:13 +0000794
Douglas Gregor14f79002009-04-10 03:52:48 +0000795 unsigned sloc_entry_size() const { return SLocEntryTable.size(); }
Kovarththanan Rajaratnam2d684c22010-03-15 17:31:29 +0000796
Ted Kremenek16b55a72010-01-26 19:31:51 +0000797 // FIXME: Exposing this is a little gross; what we want is a good way
Sebastian Redl3c7f4132010-08-18 23:57:06 +0000798 // to iterate the entries that were not defined in an AST file (or
Ted Kremenek16b55a72010-01-26 19:31:51 +0000799 // any other external source).
800 unsigned sloc_loaded_entry_size() const { return SLocEntryLoaded.size(); }
Douglas Gregor14f79002009-04-10 03:52:48 +0000801
Douglas Gregorbdfe48a2009-10-16 22:46:09 +0000802 const SrcMgr::SLocEntry &getSLocEntry(unsigned ID) const {
803 assert(ID < SLocEntryTable.size() && "Invalid id");
Mike Stump1eb44332009-09-09 15:08:12 +0000804 if (ExternalSLocEntries &&
Douglas Gregorbdfe48a2009-10-16 22:46:09 +0000805 ID < SLocEntryLoaded.size() &&
806 !SLocEntryLoaded[ID])
807 ExternalSLocEntries->ReadSLocEntry(ID);
808 return SLocEntryTable[ID];
809 }
Kovarththanan Rajaratnam2d684c22010-03-15 17:31:29 +0000810
811 const SrcMgr::SLocEntry &getSLocEntry(FileID FID) const {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +0000812 return getSLocEntry(FID.ID);
Douglas Gregorbd945002009-04-13 16:31:14 +0000813 }
814
Douglas Gregorf60e9912009-04-15 18:05:10 +0000815 unsigned getNextOffset() const { return NextOffset; }
816
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000817 /// \brief Preallocate some number of source location entries, which
818 /// will be loaded as needed from the given external source.
819 void PreallocateSLocEntries(ExternalSLocEntrySource *Source,
820 unsigned NumSLocEntries,
821 unsigned NextOffset);
822
Douglas Gregor2bf1eb02009-04-27 21:28:04 +0000823 /// \brief Clear out any preallocated source location entries that
824 /// haven't already been loaded.
825 void ClearPreallocatedSLocEntries();
826
Ted Kremenek78d85f52007-10-30 21:08:08 +0000827private:
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000828 /// isOffsetInFileID - Return true if the specified FileID contains the
829 /// specified SourceLocation offset. This is a very hot method.
830 inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
831 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
832 // If the entry is after the offset, it can't contain it.
833 if (SLocOffset < Entry.getOffset()) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000834
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000835 // If this is the last entry than it does. Otherwise, the entry after it
836 // has to not include it.
837 if (FID.ID+1 == SLocEntryTable.size()) return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000838
839 return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000840 }
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Ted Kremenek78d85f52007-10-30 21:08:08 +0000842 /// createFileID - Create a new fileID for the specified ContentCache and
843 /// include position. This works regardless of whether the ContentCache
844 /// corresponds to a file or some other input source.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000845 FileID createFileID(const SrcMgr::ContentCache* File,
846 SourceLocation IncludePos,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000847 SrcMgr::CharacteristicKind DirCharacter,
848 unsigned PreallocatedID = 0,
849 unsigned Offset = 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000850
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000851 const SrcMgr::ContentCache *
852 getOrCreateContentCache(const FileEntry *SourceFile);
Ted Kremenekc16c2082009-01-06 01:55:26 +0000853
Ted Kremenek78d85f52007-10-30 21:08:08 +0000854 /// createMemBufferContentCache - Create a new ContentCache for the specified
855 /// memory buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000856 const SrcMgr::ContentCache*
Chris Lattner2b2453a2009-01-17 06:22:33 +0000857 createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
Mike Stump1eb44332009-09-09 15:08:12 +0000858
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000859 FileID getFileIDSlow(unsigned SLocOffset) const;
860
Chris Lattneraddb7972009-01-26 20:04:19 +0000861 SourceLocation getInstantiationLocSlowCase(SourceLocation Loc) const;
862 SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
863
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000864 std::pair<FileID, unsigned>
865 getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
Mike Stump1eb44332009-09-09 15:08:12 +0000866 unsigned Offset) const;
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000867 std::pair<FileID, unsigned>
868 getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
869 unsigned Offset) const;
Reid Spencer5f016e22007-07-11 17:01:13 +0000870};
871
872
873} // end namespace clang
874
875#endif