blob: 74736017366f3323e755bb53a08beb7560c3f128 [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
Chris Lattnerd47d3b02011-07-23 10:35:09 +000017#include "clang/Basic/LLVM.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000018#include "clang/Basic/SourceLocation.h"
Chris Lattner0d0bf8c2009-02-03 07:30:45 +000019#include "llvm/Support/Allocator.h"
Michael J. Spencer03013fa2010-11-29 18:12:39 +000020#include "llvm/Support/DataTypes.h"
Douglas Gregorc8151082010-03-16 22:53:51 +000021#include "llvm/ADT/PointerIntPair.h"
Douglas Gregoraea67db2010-03-15 22:54:52 +000022#include "llvm/ADT/PointerUnion.h"
Ted Kremenek4f327862011-03-21 18:40:17 +000023#include "llvm/ADT/IntrusiveRefCntPtr.h"
Chris Lattner0d0bf8c2009-02-03 07:30:45 +000024#include "llvm/ADT/DenseMap.h"
Ted Kremenekf61b8312011-04-28 20:36:42 +000025#include "llvm/Support/MemoryBuffer.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000026#include <vector>
Chris Lattner9dc62f02007-07-12 15:32:57 +000027#include <cassert>
Reid Spencer5f016e22007-07-11 17:01:13 +000028
Reid Spencer5f016e22007-07-11 17:01:13 +000029namespace clang {
Mike Stump1eb44332009-09-09 15:08:12 +000030
Douglas Gregoraea67db2010-03-15 22:54:52 +000031class Diagnostic;
Reid Spencer5f016e22007-07-11 17:01:13 +000032class SourceManager;
Ted Kremenek099b4742007-12-05 00:14:18 +000033class FileManager;
Reid Spencer5f016e22007-07-11 17:01:13 +000034class FileEntry;
Chris Lattner5b9a5042009-01-26 07:57:50 +000035class LineTableInfo;
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000036class LangOptions;
Douglas Gregoraea67db2010-03-15 22:54:52 +000037
Chris Lattner0b9e7362008-09-26 21:18:42 +000038/// SrcMgr - Public enums and private classes that are part of the
39/// SourceManager implementation.
Reid Spencer5f016e22007-07-11 17:01:13 +000040///
41namespace SrcMgr {
Chris Lattner9d728512008-10-27 01:19:25 +000042 /// CharacteristicKind - This is used to represent whether a file or directory
Chris Lattner0b9e7362008-09-26 21:18:42 +000043 /// holds normal user code, system code, or system code which is implicitly
44 /// 'extern "C"' in C++ mode. Entire directories can be tagged with this
45 /// (this is maintained by DirectoryLookup and friends) as can specific
Douglas Gregorf62d43d2011-07-19 16:10:42 +000046 /// FileInfos when a #pragma system_header is seen or various other cases.
Chris Lattner0b9e7362008-09-26 21:18:42 +000047 ///
Chris Lattner9d728512008-10-27 01:19:25 +000048 enum CharacteristicKind {
Chris Lattner0b9e7362008-09-26 21:18:42 +000049 C_User, C_System, C_ExternCSystem
50 };
Mike Stump1eb44332009-09-09 15:08:12 +000051
Dan Gohman4710a8e2010-08-25 21:59:25 +000052 /// ContentCache - One instance of this struct is kept for every file
Chris Lattner06a062d2009-01-19 08:02:45 +000053 /// loaded or used. This object owns the MemoryBuffer object.
Ted Kremenekc16c2082009-01-06 01:55:26 +000054 class ContentCache {
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +000055 enum CCFlags {
56 /// \brief Whether the buffer is invalid.
57 InvalidFlag = 0x01,
58 /// \brief Whether the buffer should not be freed on destruction.
59 DoNotFreeFlag = 0x02
60 };
61
Ted Kremenekc16c2082009-01-06 01:55:26 +000062 /// Buffer - The actual buffer containing the characters from the input
63 /// file. This is owned by the ContentCache object.
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +000064 /// The bits indicate indicates whether the buffer is invalid.
65 mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
Ted Kremenekc16c2082009-01-06 01:55:26 +000066
67 public:
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +000068 /// Reference to the file entry representing this ContentCache.
69 /// This reference does not own the FileEntry object.
70 /// It is possible for this to be NULL if
Ted Kremenek78d85f52007-10-30 21:08:08 +000071 /// the ContentCache encapsulates an imaginary text buffer.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +000072 const FileEntry *OrigEntry;
73
74 /// \brief References the file which the contents were actually loaded from.
75 /// Can be different from 'Entry' if we overridden the contents of one file
76 /// with the contents of another file.
77 const FileEntry *ContentsEntry;
Mike Stump1eb44332009-09-09 15:08:12 +000078
Chris Lattner0d0bf8c2009-02-03 07:30:45 +000079 /// SourceLineCache - A bump pointer allocated array of offsets for each
80 /// source line. This is lazily computed. This is owned by the
81 /// SourceManager BumpPointerAllocator object.
Chris Lattner05816592009-01-17 03:54:16 +000082 unsigned *SourceLineCache;
Mike Stump1eb44332009-09-09 15:08:12 +000083
Ted Kremenekb6427f82007-12-04 18:59:28 +000084 /// NumLines - The number of lines in this ContentCache. This is only valid
85 /// if SourceLineCache is non-null.
Reid Spencer5f016e22007-07-11 17:01:13 +000086 unsigned NumLines;
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +000087
Douglas Gregor36c35ba2010-03-16 00:35:39 +000088 /// getBuffer - Returns the memory buffer for the associated content.
89 ///
Jonathan D. Turnera92d7e72011-06-16 20:47:21 +000090 /// \param Diag Object through which diagnostics will be emitted if the
Douglas Gregor36c35ba2010-03-16 00:35:39 +000091 /// buffer cannot be retrieved.
92 ///
Chris Lattnere127a0d2010-04-20 20:35:58 +000093 /// \param Loc If specified, is the location that invalid file diagnostics
94 /// will be emitted at.
95 ///
Douglas Gregor36c35ba2010-03-16 00:35:39 +000096 /// \param Invalid If non-NULL, will be set \c true if an error occurred.
Chris Lattnere127a0d2010-04-20 20:35:58 +000097 const llvm::MemoryBuffer *getBuffer(Diagnostic &Diag,
98 const SourceManager &SM,
99 SourceLocation Loc = SourceLocation(),
Douglas Gregor36c35ba2010-03-16 00:35:39 +0000100 bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000101
Ted Kremenekc16c2082009-01-06 01:55:26 +0000102 /// getSize - Returns the size of the content encapsulated by this
103 /// ContentCache. This can be the size of the source file or the size of an
104 /// arbitrary scratch buffer. If the ContentCache encapsulates a source
105 /// file this size is retrieved from the file's FileEntry.
106 unsigned getSize() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Ted Kremenekc16c2082009-01-06 01:55:26 +0000108 /// getSizeBytesMapped - Returns the number of bytes actually mapped for
109 /// this ContentCache. This can be 0 if the MemBuffer was not actually
110 /// instantiated.
111 unsigned getSizeBytesMapped() const;
Ted Kremenekf61b8312011-04-28 20:36:42 +0000112
113 /// Returns the kind of memory used to back the memory buffer for
114 /// this content cache. This is used for performance analysis.
115 llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000116
Chris Lattner05816592009-01-17 03:54:16 +0000117 void setBuffer(const llvm::MemoryBuffer *B) {
Douglas Gregorc8151082010-03-16 22:53:51 +0000118 assert(!Buffer.getPointer() && "MemoryBuffer already set.");
119 Buffer.setPointer(B);
120 Buffer.setInt(false);
Ted Kremenekc16c2082009-01-06 01:55:26 +0000121 }
Douglas Gregorcc5888d2010-07-31 00:40:00 +0000122
123 /// \brief Get the underlying buffer, returning NULL if the buffer is not
124 /// yet available.
125 const llvm::MemoryBuffer *getRawBuffer() const {
126 return Buffer.getPointer();
127 }
Mike Stump1eb44332009-09-09 15:08:12 +0000128
Douglas Gregor29684422009-12-02 06:49:09 +0000129 /// \brief Replace the existing buffer (which will be deleted)
130 /// with the given buffer.
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000131 void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
Douglas Gregor29684422009-12-02 06:49:09 +0000132
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000133 /// \brief Determine whether the buffer itself is invalid.
134 bool isBufferInvalid() const {
135 return Buffer.getInt() & InvalidFlag;
136 }
137
138 /// \brief Determine whether the buffer should be freed.
139 bool shouldFreeBuffer() const {
140 return (Buffer.getInt() & DoNotFreeFlag) == 0;
141 }
142
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000143 ContentCache(const FileEntry *Ent = 0)
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000144 : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent),
145 SourceLineCache(0), NumLines(0) {}
146
147 ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
148 : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt),
149 SourceLineCache(0), NumLines(0) {}
Ted Kremenek78d85f52007-10-30 21:08:08 +0000150
151 ~ContentCache();
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Ted Kremenek0d892d82007-10-30 22:57:35 +0000153 /// The copy ctor does not allow copies where source object has either
154 /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000155 /// is not transferred, so this is a logical error.
Douglas Gregorc8151082010-03-16 22:53:51 +0000156 ContentCache(const ContentCache &RHS)
157 : Buffer(0, false), SourceLineCache(0)
158 {
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000159 OrigEntry = RHS.OrigEntry;
160 ContentsEntry = RHS.ContentsEntry;
Ted Kremenek0d892d82007-10-30 22:57:35 +0000161
Douglas Gregorc8151082010-03-16 22:53:51 +0000162 assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0
Ted Kremenek0d892d82007-10-30 22:57:35 +0000163 && "Passed ContentCache object cannot own a buffer.");
Mike Stump1eb44332009-09-09 15:08:12 +0000164
165 NumLines = RHS.NumLines;
Ted Kremenek0d892d82007-10-30 22:57:35 +0000166 }
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Ted Kremenek0d892d82007-10-30 22:57:35 +0000168 private:
169 // Disable assignments.
Mike Stump1eb44332009-09-09 15:08:12 +0000170 ContentCache &operator=(const ContentCache& RHS);
171 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000172
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000173 /// FileInfo - Information about a FileID, basically just the logical file
174 /// that it represents and include stack information.
Reid Spencer5f016e22007-07-11 17:01:13 +0000175 ///
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000176 /// Each FileInfo has include stack information, indicating where it came
177 /// from. This information encodes the #include chain that a token was
178 /// instantiated from. The main include file has an invalid IncludeLoc.
Reid Spencer5f016e22007-07-11 17:01:13 +0000179 ///
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000180 /// FileInfos contain a "ContentCache *", with the contents of the file.
Reid Spencer5f016e22007-07-11 17:01:13 +0000181 ///
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000182 class FileInfo {
Reid Spencer5f016e22007-07-11 17:01:13 +0000183 /// IncludeLoc - The location of the #include that brought in this file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000184 /// This is an invalid SLOC for the main file (top of the #include chain).
185 unsigned IncludeLoc; // Really a SourceLocation
Mike Stump1eb44332009-09-09 15:08:12 +0000186
Chris Lattner6e1aff22009-01-26 06:49:09 +0000187 /// Data - This contains the ContentCache* and the bits indicating the
188 /// characteristic of the file and whether it has #line info, all bitmangled
189 /// together.
190 uintptr_t Data;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000191 public:
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000192 /// get - Return a FileInfo object.
193 static FileInfo get(SourceLocation IL, const ContentCache *Con,
194 CharacteristicKind FileCharacter) {
195 FileInfo X;
196 X.IncludeLoc = IL.getRawEncoding();
Chris Lattner6e1aff22009-01-26 06:49:09 +0000197 X.Data = (uintptr_t)Con;
Chris Lattner00282d62009-02-03 07:41:46 +0000198 assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
Chris Lattner6e1aff22009-01-26 06:49:09 +0000199 assert((unsigned)FileCharacter < 4 && "invalid file character");
200 X.Data |= (unsigned)FileCharacter;
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 return X;
202 }
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000204 SourceLocation getIncludeLoc() const {
205 return SourceLocation::getFromRawEncoding(IncludeLoc);
206 }
Chris Lattner6e1aff22009-01-26 06:49:09 +0000207 const ContentCache* getContentCache() const {
Chris Lattner00282d62009-02-03 07:41:46 +0000208 return reinterpret_cast<const ContentCache*>(Data & ~7UL);
Chris Lattner6e1aff22009-01-26 06:49:09 +0000209 }
Mike Stump1eb44332009-09-09 15:08:12 +0000210
Chris Lattner0b9e7362008-09-26 21:18:42 +0000211 /// getCharacteristic - Return whether this is a system header or not.
Mike Stump1eb44332009-09-09 15:08:12 +0000212 CharacteristicKind getFileCharacteristic() const {
Chris Lattner6e1aff22009-01-26 06:49:09 +0000213 return (CharacteristicKind)(Data & 3);
Chris Lattner0b9e7362008-09-26 21:18:42 +0000214 }
Chris Lattnerac50e342009-02-03 22:13:05 +0000215
216 /// hasLineDirectives - Return true if this FileID has #line directives in
217 /// it.
218 bool hasLineDirectives() const { return (Data & 4) != 0; }
Mike Stump1eb44332009-09-09 15:08:12 +0000219
Chris Lattnerac50e342009-02-03 22:13:05 +0000220 /// setHasLineDirectives - Set the flag that indicates that this FileID has
221 /// line table entries associated with it.
222 void setHasLineDirectives() {
223 Data |= 4;
224 }
Chris Lattner9dc1f532007-07-20 16:37:10 +0000225 };
Mike Stump1eb44332009-09-09 15:08:12 +0000226
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000227 /// InstantiationInfo - Each InstantiationInfo encodes the Instantiation
228 /// location - where the token was ultimately instantiated, and the
229 /// SpellingLoc - where the actual character data for the token came from.
230 class InstantiationInfo {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000231 // Really these are all SourceLocations.
Mike Stump1eb44332009-09-09 15:08:12 +0000232
Chris Lattnere7fb4842009-02-15 20:52:18 +0000233 /// SpellingLoc - Where the spelling for the token can be found.
234 unsigned SpellingLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Chris Lattnere7fb4842009-02-15 20:52:18 +0000236 /// InstantiationLocStart/InstantiationLocEnd - In a macro expansion, these
Douglas Gregor14f79002009-04-10 03:52:48 +0000237 /// indicate the start and end of the instantiation. In object-like macros,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000238 /// these will be the same. In a function-like macro instantiation, the
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000239 /// start will be the identifier and the end will be the ')'. Finally, in
240 /// macro-argument instantitions, the end will be 'SourceLocation()', an
241 /// invalid location.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000242 unsigned InstantiationLocStart, InstantiationLocEnd;
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000243
Chris Lattner9dc1f532007-07-20 16:37:10 +0000244 public:
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000245 SourceLocation getSpellingLoc() const {
246 return SourceLocation::getFromRawEncoding(SpellingLoc);
247 }
Chris Lattnere7fb4842009-02-15 20:52:18 +0000248 SourceLocation getInstantiationLocStart() const {
249 return SourceLocation::getFromRawEncoding(InstantiationLocStart);
250 }
251 SourceLocation getInstantiationLocEnd() const {
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000252 SourceLocation EndLoc =
253 SourceLocation::getFromRawEncoding(InstantiationLocEnd);
254 return EndLoc.isInvalid() ? getInstantiationLocStart() : EndLoc;
Chris Lattnere7fb4842009-02-15 20:52:18 +0000255 }
Mike Stump1eb44332009-09-09 15:08:12 +0000256
Chris Lattnere7fb4842009-02-15 20:52:18 +0000257 std::pair<SourceLocation,SourceLocation> getInstantiationLocRange() const {
258 return std::make_pair(getInstantiationLocStart(),
259 getInstantiationLocEnd());
260 }
Mike Stump1eb44332009-09-09 15:08:12 +0000261
Chandler Carruth96d35892011-07-26 03:03:00 +0000262 bool isMacroArgExpansion() const {
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000263 // Note that this needs to return false for default constructed objects.
264 return getInstantiationLocStart().isValid() &&
265 SourceLocation::getFromRawEncoding(InstantiationLocEnd).isInvalid();
266 }
267
268 /// create - Return a InstantiationInfo for an expansion. ILStart and
269 /// ILEnd specify the instantiation range (where the macro is expanded),
270 /// and SL specifies the spelling location (where the characters from the
271 /// token come from). All three can refer to normal File SLocs or
Chris Lattnere7fb4842009-02-15 20:52:18 +0000272 /// instantiation locations.
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000273 static InstantiationInfo create(SourceLocation SL,
274 SourceLocation ILStart,
275 SourceLocation ILEnd) {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000276 InstantiationInfo X;
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000277 X.SpellingLoc = SL.getRawEncoding();
Chris Lattnere7fb4842009-02-15 20:52:18 +0000278 X.InstantiationLocStart = ILStart.getRawEncoding();
279 X.InstantiationLocEnd = ILEnd.getRawEncoding();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000280 return X;
Reid Spencer5f016e22007-07-11 17:01:13 +0000281 }
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000282
283 /// createForMacroArg - Return a special InstantiationInfo for the
284 /// expansion of a macro argument into a function-like macro's body. IL
285 /// specifies the instantiation location (where the macro is expanded).
286 /// This doesn't need to be a range because a macro is always instantiated
287 /// at a macro parameter reference, and macro parameters are always exactly
288 /// one token. SL specifies the spelling location (where the characters
289 /// from the token come from). IL and SL can both refer to normal File
290 /// SLocs or instantiation locations.
291 ///
292 /// Given the code:
293 /// \code
294 /// #define F(x) f(x)
295 /// F(42);
296 /// \endcode
297 ///
298 /// When expanding '\c F(42)', the '\c x' would call this with an SL
299 /// pointing at '\c 42' anad an IL pointing at its location in the
300 /// definition of '\c F'.
301 static InstantiationInfo createForMacroArg(SourceLocation SL,
302 SourceLocation IL) {
303 // We store an intentionally invalid source location for the end of the
304 // instantiation range to mark that this is a macro argument instantation
305 // rather than a normal one.
306 return create(SL, IL, SourceLocation());
307 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000308 };
Mike Stump1eb44332009-09-09 15:08:12 +0000309
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000310 /// SLocEntry - This is a discriminated union of FileInfo and
311 /// InstantiationInfo. SourceManager keeps an array of these objects, and
312 /// they are uniquely identified by the FileID datatype.
313 class SLocEntry {
314 unsigned Offset; // low bit is set for instantiation info.
315 union {
316 FileInfo File;
317 InstantiationInfo Instantiation;
318 };
319 public:
320 unsigned getOffset() const { return Offset >> 1; }
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000322 bool isInstantiation() const { return Offset & 1; }
323 bool isFile() const { return !isInstantiation(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000324
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000325 const FileInfo &getFile() const {
326 assert(isFile() && "Not a file SLocEntry!");
327 return File;
328 }
329
330 const InstantiationInfo &getInstantiation() const {
331 assert(isInstantiation() && "Not an instantiation SLocEntry!");
332 return Instantiation;
333 }
Mike Stump1eb44332009-09-09 15:08:12 +0000334
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000335 static SLocEntry get(unsigned Offset, const FileInfo &FI) {
336 SLocEntry E;
337 E.Offset = Offset << 1;
338 E.File = FI;
339 return E;
340 }
341
342 static SLocEntry get(unsigned Offset, const InstantiationInfo &II) {
343 SLocEntry E;
344 E.Offset = (Offset << 1) | 1;
345 E.Instantiation = II;
346 return E;
347 }
348 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000349} // end SrcMgr namespace.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000350
351/// \brief External source of source location entries.
352class ExternalSLocEntrySource {
353public:
354 virtual ~ExternalSLocEntrySource();
355
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000356 /// \brief Read the source location entry with index ID, which will always be
357 /// less than -1.
Douglas Gregore23ac652011-04-20 00:21:03 +0000358 ///
359 /// \returns true if an error occurred that prevented the source-location
360 /// entry from being loaded.
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000361 virtual bool ReadSLocEntry(int ID) = 0;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000362};
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000363
364
365/// IsBeforeInTranslationUnitCache - This class holds the cache used by
366/// isBeforeInTranslationUnit. The cache structure is complex enough to be
367/// worth breaking out of SourceManager.
368class IsBeforeInTranslationUnitCache {
369 /// L/R QueryFID - These are the FID's of the cached query. If these match up
370 /// with a subsequent query, the result can be reused.
371 FileID LQueryFID, RQueryFID;
372
373 /// CommonFID - This is the file found in common between the two #include
374 /// traces. It is the nearest common ancestor of the #include tree.
375 FileID CommonFID;
376
377 /// L/R CommonOffset - This is the offset of the previous query in CommonFID.
378 /// Usually, this represents the location of the #include for QueryFID, but if
379 /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a
380 /// random token in the parent.
381 unsigned LCommonOffset, RCommonOffset;
382public:
383
384 /// isCacheValid - Return true if the currently cached values match up with
385 /// the specified LHS/RHS query. If not, we can't use the cache.
386 bool isCacheValid(FileID LHS, FileID RHS) const {
387 return LQueryFID == LHS && RQueryFID == RHS;
388 }
389
390 /// getCachedResult - If the cache is valid, compute the result given the
391 /// specified offsets in the LHS/RHS FID's.
392 bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
393 // If one of the query files is the common file, use the offset. Otherwise,
394 // use the #include loc in the common file.
395 if (LQueryFID != CommonFID) LOffset = LCommonOffset;
396 if (RQueryFID != CommonFID) ROffset = RCommonOffset;
397 return LOffset < ROffset;
398 }
399
400 // Set up a new query.
401 void setQueryFIDs(FileID LHS, FileID RHS) {
402 LQueryFID = LHS;
403 RQueryFID = RHS;
404 }
405
406 void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
407 unsigned rCommonOffset) {
408 CommonFID = commonFID;
409 LCommonOffset = lCommonOffset;
410 RCommonOffset = rCommonOffset;
411 }
412
413};
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000414
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000415/// \brief This class handles loading and caching of source files into memory.
416///
417/// This object owns the MemoryBuffer objects for all of the loaded
Reid Spencer5f016e22007-07-11 17:01:13 +0000418/// files and assigns unique FileID's for each unique #include chain.
419///
420/// The SourceManager can be queried for information about SourceLocation
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000421/// objects, turning them into either spelling or instantiation locations.
422/// Spelling locations represent where the bytes corresponding to a token came
423/// from and instantiation locations represent where the location is in the
424/// user's view. In the case of a macro expansion, for example, the spelling
Douglas Gregore23ac652011-04-20 00:21:03 +0000425/// location indicates where the expanded token came from and the instantiation
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000426/// location specifies where it was expanded.
Ted Kremenek4f327862011-03-21 18:40:17 +0000427class SourceManager : public llvm::RefCountedBase<SourceManager> {
Douglas Gregorf715ca12010-03-16 00:06:06 +0000428 /// \brief Diagnostic object.
429 Diagnostic &Diag;
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000430
431 FileManager &FileMgr;
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000432
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000433 mutable llvm::BumpPtrAllocator ContentCacheAlloc;
Mike Stump1eb44332009-09-09 15:08:12 +0000434
Reid Spencer5f016e22007-07-11 17:01:13 +0000435 /// FileInfos - Memoized information about all of the files tracked by this
Ted Kremenek0d892d82007-10-30 22:57:35 +0000436 /// SourceManager. This set allows us to merge ContentCache entries based
437 /// on their FileEntry*. All ContentCache objects will thus have unique,
Mike Stump1eb44332009-09-09 15:08:12 +0000438 /// non-null, FileEntry pointers.
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000439 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000440
Argyrios Kyrtzidis299a4a92011-03-08 23:35:24 +0000441 /// \brief True if the ContentCache for files that are overriden by other
442 /// files, should report the original file name. Defaults to true.
443 bool OverridenFilesKeepOriginalName;
444
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000445 /// \brief Files that have been overriden with the contents from another file.
446 llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
447
Reid Spencer5f016e22007-07-11 17:01:13 +0000448 /// MemBufferInfos - Information about various memory buffers that we have
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000449 /// read in. All FileEntry* within the stored ContentCache objects are NULL,
450 /// as they do not refer to a file.
451 std::vector<SrcMgr::ContentCache*> MemBufferInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000452
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000453 /// \brief The table of SLocEntries that are local to this module.
454 ///
455 /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid
456 /// instantiation.
457 std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000458
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000459 /// \brief The table of SLocEntries that are loaded from other modules.
460 ///
461 /// Negative FileIDs are indexes into this table. To get from ID to an index,
462 /// use (-ID - 2).
463 std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable;
464
465 /// \brief The starting offset of the next local SLocEntry.
466 ///
467 /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
468 unsigned NextLocalOffset;
469
470 /// \brief The starting offset of the latest batch of loaded SLocEntries.
471 ///
472 /// This is LoadedSLocEntryTable.back().Offset, except that that entry might
473 /// not have been loaded, so that value would be unknown.
474 unsigned CurrentLoadedOffset;
475
476 /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable
477 /// have already been loaded from the external source.
478 ///
479 /// Same indexing as LoadedSLocEntryTable.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000480 std::vector<bool> SLocEntryLoaded;
481
482 /// \brief An external source for source location entries.
483 ExternalSLocEntrySource *ExternalSLocEntries;
484
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000485 /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
486 /// LastFileIDLookup records the last FileID looked up or created, because it
487 /// is very common to look up many tokens from the same file.
488 mutable FileID LastFileIDLookup;
Mike Stump1eb44332009-09-09 15:08:12 +0000489
Chris Lattner5b9a5042009-01-26 07:57:50 +0000490 /// LineTable - This holds information for #line directives. It is referenced
491 /// by indices from SLocEntryTable.
492 LineTableInfo *LineTable;
Mike Stump1eb44332009-09-09 15:08:12 +0000493
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000494 /// LastLineNo - These ivars serve as a cache used in the getLineNumber
495 /// method which is used to speedup getLineNumber calls to nearby locations.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000496 mutable FileID LastLineNoFileIDQuery;
Chris Lattnerf812a452008-11-18 06:51:15 +0000497 mutable SrcMgr::ContentCache *LastLineNoContentCache;
498 mutable unsigned LastLineNoFilePos;
499 mutable unsigned LastLineNoResult;
Mike Stump1eb44332009-09-09 15:08:12 +0000500
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000501 /// MainFileID - The file ID for the main source file of the translation unit.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000502 FileID MainFileID;
Steve Naroff49c1f4a2008-02-02 00:10:46 +0000503
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000504 // Statistics for -print-stats.
505 mutable unsigned NumLinearScans, NumBinaryProbes;
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +0000507 // Cache results for the isBeforeInTranslationUnit method.
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000508 mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
Mike Stump1eb44332009-09-09 15:08:12 +0000509
Douglas Gregore23ac652011-04-20 00:21:03 +0000510 // Cache for the "fake" buffer used for error-recovery purposes.
511 mutable llvm::MemoryBuffer *FakeBufferForRecovery;
512
Steve Naroff49c1f4a2008-02-02 00:10:46 +0000513 // SourceManager doesn't support copy construction.
514 explicit SourceManager(const SourceManager&);
Mike Stump1eb44332009-09-09 15:08:12 +0000515 void operator=(const SourceManager&);
Reid Spencer5f016e22007-07-11 17:01:13 +0000516public:
Chris Lattner39b49bc2010-11-23 08:35:12 +0000517 SourceManager(Diagnostic &Diag, FileManager &FileMgr);
Chris Lattner5b9a5042009-01-26 07:57:50 +0000518 ~SourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000519
Chris Lattner5b9a5042009-01-26 07:57:50 +0000520 void clearIDTables();
Mike Stump1eb44332009-09-09 15:08:12 +0000521
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +0000522 Diagnostic &getDiagnostics() const { return Diag; }
523
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000524 FileManager &getFileManager() const { return FileMgr; }
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000525
Argyrios Kyrtzidis299a4a92011-03-08 23:35:24 +0000526 /// \brief Set true if the SourceManager should report the original file name
527 /// for contents of files that were overriden by other files.Defaults to true.
528 void setOverridenFilesKeepOriginalName(bool value) {
529 OverridenFilesKeepOriginalName = value;
530 }
531
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000532 /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
533 /// that will represent the FileID for the main source. One example
534 /// of when this would be used is when the main source is read from STDIN.
535 FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
536 assert(MainFileID.isInvalid() && "MainFileID already set!");
537 MainFileID = createFileIDForMemBuffer(Buffer);
538 return MainFileID;
539 }
540
Chris Lattner06a062d2009-01-19 08:02:45 +0000541 //===--------------------------------------------------------------------===//
542 // MainFileID creation and querying methods.
543 //===--------------------------------------------------------------------===//
544
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000545 /// getMainFileID - Returns the FileID of the main source file.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000546 FileID getMainFileID() const { return MainFileID; }
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Chris Lattner06a062d2009-01-19 08:02:45 +0000548 /// createMainFileID - Create the FileID for the main source file.
Dan Gohmanf155dfa2010-08-27 15:44:11 +0000549 FileID createMainFileID(const FileEntry *SourceFile) {
Chris Lattner06a062d2009-01-19 08:02:45 +0000550 assert(MainFileID.isInvalid() && "MainFileID already set!");
Dan Gohmanf155dfa2010-08-27 15:44:11 +0000551 MainFileID = createFileID(SourceFile, SourceLocation(), SrcMgr::C_User);
Chris Lattner06a062d2009-01-19 08:02:45 +0000552 return MainFileID;
553 }
Mike Stump1eb44332009-09-09 15:08:12 +0000554
Douglas Gregor414cb642010-11-30 05:23:00 +0000555 /// \brief Set the file ID for the precompiled preamble, which is also the
556 /// main file.
557 void SetPreambleFileID(FileID Preamble) {
558 assert(MainFileID.isInvalid() && "MainFileID already set!");
559 MainFileID = Preamble;
560 }
561
Chris Lattner06a062d2009-01-19 08:02:45 +0000562 //===--------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000563 // Methods to create new FileID's and instantiations.
Chris Lattner06a062d2009-01-19 08:02:45 +0000564 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000565
Reid Spencer5f016e22007-07-11 17:01:13 +0000566 /// createFileID - Create a new FileID that represents the specified file
Peter Collingbourned57b7ff2011-06-30 16:41:03 +0000567 /// being #included from the specified IncludePosition. This translates NULL
568 /// into standard input.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000569 FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000570 SrcMgr::CharacteristicKind FileCharacter,
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000571 int LoadedID = 0, unsigned LoadedOffset = 0) {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000572 const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
Dan Gohman0d06e992010-10-26 20:47:28 +0000573 assert(IR && "getOrCreateContentCache() cannot return NULL");
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000574 return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset);
Reid Spencer5f016e22007-07-11 17:01:13 +0000575 }
Mike Stump1eb44332009-09-09 15:08:12 +0000576
Reid Spencer5f016e22007-07-11 17:01:13 +0000577 /// createFileIDForMemBuffer - Create a new FileID that represents the
578 /// specified memory buffer. This does no caching of the buffer and takes
579 /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000580 FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000581 int LoadedID = 0, unsigned LoadedOffset = 0) {
Nico Weber7bfaaae2008-08-10 19:59:06 +0000582 return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000583 SrcMgr::C_User, LoadedID, LoadedOffset);
Ted Kremenek1036b682007-12-19 23:48:45 +0000584 }
Chris Lattner06a062d2009-01-19 08:02:45 +0000585
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000586 /// createMacroArgInstantiationLoc - Return a new SourceLocation that encodes
587 /// the fact that a token from SpellingLoc should actually be referenced from
588 /// InstantiationLoc, and that it represents the instantiation of a macro
589 /// argument into the function-like macro body.
590 SourceLocation createMacroArgInstantiationLoc(SourceLocation Loc,
591 SourceLocation InstantiationLoc,
592 unsigned TokLength);
593
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000594 /// createInstantiationLoc - Return a new SourceLocation that encodes the fact
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000595 /// that a token from SpellingLoc should actually be referenced from
596 /// InstantiationLoc.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000597 SourceLocation createInstantiationLoc(SourceLocation Loc,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000598 SourceLocation InstantiationLocStart,
599 SourceLocation InstantiationLocEnd,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000600 unsigned TokLength,
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000601 int LoadedID = 0,
602 unsigned LoadedOffset = 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000603
Douglas Gregor29684422009-12-02 06:49:09 +0000604 /// \brief Retrieve the memory buffer associated with the given file.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000605 ///
606 /// \param Invalid If non-NULL, will be set \c true if an error
607 /// occurs while retrieving the memory buffer.
608 const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
609 bool *Invalid = 0);
Douglas Gregor29684422009-12-02 06:49:09 +0000610
611 /// \brief Override the contents of the given source file by providing an
612 /// already-allocated buffer.
613 ///
Dan Gohmanafbf5f82010-08-26 02:27:03 +0000614 /// \param SourceFile the source file whose contents will be overriden.
Douglas Gregor29684422009-12-02 06:49:09 +0000615 ///
616 /// \param Buffer the memory buffer whose contents will be used as the
617 /// data in the given source file.
618 ///
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000619 /// \param DoNotFree If true, then the buffer will not be freed when the
620 /// source manager is destroyed.
Dan Gohman0d06e992010-10-26 20:47:28 +0000621 void overrideFileContents(const FileEntry *SourceFile,
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000622 const llvm::MemoryBuffer *Buffer,
623 bool DoNotFree = false);
Douglas Gregor29684422009-12-02 06:49:09 +0000624
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000625 /// \brief Override the the given source file with another one.
626 ///
627 /// \param SourceFile the source file which will be overriden.
628 ///
629 /// \param NewFile the file whose contents will be used as the
630 /// data instead of the contents of the given source file.
631 void overrideFileContents(const FileEntry *SourceFile,
632 const FileEntry *NewFile);
633
Chris Lattner06a062d2009-01-19 08:02:45 +0000634 //===--------------------------------------------------------------------===//
635 // FileID manipulation methods.
636 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000637
Daniel Dunbar2ffb14f2009-12-06 09:19:25 +0000638 /// getBuffer - Return the buffer for the specified FileID. If there is an
639 /// error opening this buffer the first time, this manufactures a temporary
640 /// buffer and returns a non-empty error string.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000641 const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
642 bool *Invalid = 0) const {
Douglas Gregore23ac652011-04-20 00:21:03 +0000643 bool MyInvalid = false;
644 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
645 if (MyInvalid || !Entry.isFile()) {
646 if (Invalid)
647 *Invalid = true;
648
649 return getFakeBufferForRecovery();
650 }
651
652 return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
653 Invalid);
Chris Lattner06a062d2009-01-19 08:02:45 +0000654 }
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Chris Lattnere127a0d2010-04-20 20:35:58 +0000656 const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
Douglas Gregore23ac652011-04-20 00:21:03 +0000657 bool MyInvalid = false;
658 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
659 if (MyInvalid || !Entry.isFile()) {
660 if (Invalid)
661 *Invalid = true;
662
663 return getFakeBufferForRecovery();
664 }
665
666 return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
667 SourceLocation(),
668 Invalid);
Chris Lattnere127a0d2010-04-20 20:35:58 +0000669 }
670
Chris Lattner06a062d2009-01-19 08:02:45 +0000671 /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
672 const FileEntry *getFileEntryForID(FileID FID) const {
Douglas Gregore23ac652011-04-20 00:21:03 +0000673 bool MyInvalid = false;
674 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
675 if (MyInvalid || !Entry.isFile())
676 return 0;
677
678 return Entry.getFile().getContentCache()->OrigEntry;
Chris Lattner06a062d2009-01-19 08:02:45 +0000679 }
Mike Stump1eb44332009-09-09 15:08:12 +0000680
Ted Kremenek9d5a1652011-03-23 02:16:44 +0000681 /// Returns the FileEntry record for the provided SLocEntry.
682 const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
683 {
684 return sloc.getFile().getContentCache()->OrigEntry;
685 }
686
Benjamin Kramerceafc4b2010-03-16 14:48:07 +0000687 /// getBufferData - Return a StringRef to the source buffer data for the
688 /// specified FileID.
689 ///
Douglas Gregorf715ca12010-03-16 00:06:06 +0000690 /// \param FID The file ID whose contents will be returned.
691 /// \param Invalid If non-NULL, will be set true if an error occurred.
Chris Lattner686775d2011-07-20 06:58:45 +0000692 StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +0000693
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Chris Lattner06a062d2009-01-19 08:02:45 +0000695 //===--------------------------------------------------------------------===//
696 // SourceLocation manipulation methods.
697 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000698
Chris Lattner668ab1a2009-03-13 01:05:57 +0000699 /// getFileID - Return the FileID for a SourceLocation. This is a very
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000700 /// hot method that is used for all SourceManager queries that start with a
701 /// SourceLocation object. It is responsible for finding the entry in
702 /// SLocEntryTable which contains the specified location.
703 ///
704 FileID getFileID(SourceLocation SpellingLoc) const {
705 unsigned SLocOffset = SpellingLoc.getOffset();
Mike Stump1eb44332009-09-09 15:08:12 +0000706
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000707 // If our one-entry cache covers this offset, just return it.
708 if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
709 return LastFileIDLookup;
710
711 return getFileIDSlow(SLocOffset);
712 }
Mike Stump1eb44332009-09-09 15:08:12 +0000713
Chris Lattner2b2453a2009-01-17 06:22:33 +0000714 /// getLocForStartOfFile - Return the source location corresponding to the
715 /// first byte of the specified file.
716 SourceLocation getLocForStartOfFile(FileID FID) const {
Douglas Gregore23ac652011-04-20 00:21:03 +0000717 bool Invalid = false;
718 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
719 if (Invalid || !Entry.isFile())
720 return SourceLocation();
721
722 unsigned FileOffset = Entry.getOffset();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000723 return SourceLocation::getFileLoc(FileOffset);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000724 }
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Chandler Carruth40278532011-07-25 16:49:02 +0000726 /// getExpansionLoc - Given a SourceLocation object, return the expansion
727 /// location referenced by the ID.
728 SourceLocation getExpansionLoc(SourceLocation Loc) const {
Chris Lattneraddb7972009-01-26 20:04:19 +0000729 // Handle the non-mapped case inline, defer to out of line code to handle
Chandler Carruth40278532011-07-25 16:49:02 +0000730 // expansions.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000731 if (Loc.isFileID()) return Loc;
Chandler Carruthf84ef952011-07-25 20:52:26 +0000732 return getExpansionLocSlowCase(Loc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000733 }
Mike Stump1eb44332009-09-09 15:08:12 +0000734
Chandler Carruth999f7392011-07-25 20:52:21 +0000735 /// getImmediateExpansionRange - Loc is required to be an expansion location.
736 /// Return the start/end of the expansion information.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000737 std::pair<SourceLocation,SourceLocation>
Chandler Carruth999f7392011-07-25 20:52:21 +0000738 getImmediateExpansionRange(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000739
Chandler Carruthedc3dcc2011-07-25 16:56:02 +0000740 /// getExpansionRange - Given a SourceLocation object, return the range of
741 /// tokens covered by the expansion the ultimate file.
Chris Lattner66781332009-02-15 21:26:50 +0000742 std::pair<SourceLocation,SourceLocation>
Chandler Carruthedc3dcc2011-07-25 16:56:02 +0000743 getExpansionRange(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000744
745
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000746 /// getSpellingLoc - Given a SourceLocation object, return the spelling
747 /// location referenced by the ID. This is the place where the characters
748 /// that make up the lexed token can be found.
749 SourceLocation getSpellingLoc(SourceLocation Loc) const {
Chris Lattneraddb7972009-01-26 20:04:19 +0000750 // Handle the non-mapped case inline, defer to out of line code to handle
751 // instantiations.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000752 if (Loc.isFileID()) return Loc;
Chris Lattneraddb7972009-01-26 20:04:19 +0000753 return getSpellingLocSlowCase(Loc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000754 }
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Chris Lattner387616e2009-02-17 08:04:48 +0000756 /// getImmediateSpellingLoc - Given a SourceLocation object, return the
757 /// spelling location referenced by the ID. This is the first level down
758 /// towards the place where the characters that make up the lexed token can be
759 /// found. This should not generally be used by clients.
Mike Stump1eb44332009-09-09 15:08:12 +0000760 SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000761
762 /// getDecomposedLoc - Decompose the specified location into a raw FileID +
763 /// Offset pair. The first element is the FileID, the second is the
764 /// offset from the start of the buffer of the location.
765 std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
766 FileID FID = getFileID(Loc);
767 return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset());
768 }
Mike Stump1eb44332009-09-09 15:08:12 +0000769
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +0000770 /// getDecomposedExpansionLoc - Decompose the specified location into a
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000771 /// raw FileID + Offset pair. If the location is an instantiation record,
772 /// walk through it until we find the final location instantiated.
773 std::pair<FileID, unsigned>
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +0000774 getDecomposedExpansionLoc(SourceLocation Loc) const {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000775 FileID FID = getFileID(Loc);
776 const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000777
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000778 unsigned Offset = Loc.getOffset()-E->getOffset();
779 if (Loc.isFileID())
780 return std::make_pair(FID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000781
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +0000782 return getDecomposedExpansionLocSlowCase(E);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000783 }
784
785 /// getDecomposedSpellingLoc - Decompose the specified location into a raw
786 /// FileID + Offset pair. If the location is an instantiation record, walk
787 /// through it until we find its spelling record.
788 std::pair<FileID, unsigned>
789 getDecomposedSpellingLoc(SourceLocation Loc) const {
790 FileID FID = getFileID(Loc);
791 const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000792
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000793 unsigned Offset = Loc.getOffset()-E->getOffset();
794 if (Loc.isFileID())
795 return std::make_pair(FID, Offset);
796 return getDecomposedSpellingLocSlowCase(E, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000797 }
798
Chris Lattner52c29082009-01-27 06:27:13 +0000799 /// getFileOffset - This method returns the offset from the start
800 /// of the file that the specified SourceLocation represents. This is not very
801 /// meaningful for a macro ID.
802 unsigned getFileOffset(SourceLocation SpellingLoc) const {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000803 return getDecomposedLoc(SpellingLoc).second;
804 }
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Chandler Carruth96d35892011-07-26 03:03:00 +0000806 /// isMacroArgExpansion - This method tests whether the given source location
807 /// represents a macro argument's expansion into the function-like macro
808 /// definition. Such source locations only appear inside of the expansion
809 /// locations representing where a particular function-like macro was
810 /// expanded.
811 bool isMacroArgExpansion(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000812
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000813 //===--------------------------------------------------------------------===//
814 // Queries about the code at a SourceLocation.
815 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000816
Reid Spencer5f016e22007-07-11 17:01:13 +0000817 /// getCharacterData - Return a pointer to the start of the specified location
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000818 /// in the appropriate spelling MemoryBuffer.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000819 ///
820 /// \param Invalid If non-NULL, will be set \c true if an error occurs.
821 const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000822
Chris Lattner9dc1f532007-07-20 16:37:10 +0000823 /// getColumnNumber - Return the column # for the specified file position.
824 /// This is significantly cheaper to compute than the line number. This
825 /// returns zero if the column number isn't known. This may only be called on
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000826 /// a file sloc, so you must choose a spelling or instantiation location
827 /// before calling this method.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000828 unsigned getColumnNumber(FileID FID, unsigned FilePos,
829 bool *Invalid = 0) const;
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000830 unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
Chandler Carrutha77c0312011-07-25 20:57:57 +0000831 unsigned getExpansionColumnNumber(SourceLocation Loc,
Chandler Carruthb49dcd22011-07-25 20:59:15 +0000832 bool *Invalid = 0) const;
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000833 unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000834
835
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000836 /// getLineNumber - Given a SourceLocation, return the spelling line number
Reid Spencer5f016e22007-07-11 17:01:13 +0000837 /// for the position indicated. This requires building and caching a table of
838 /// line offsets for the MemoryBuffer, so this is not cheap: use only when
839 /// about to emit a diagnostic.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000840 unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000841 unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
Chandler Carruth64211622011-07-25 21:09:52 +0000842 unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000843 unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000844
Chris Lattnerbff5c512009-02-17 08:39:06 +0000845 /// Return the filename or buffer identifier of the buffer the location is in.
846 /// Note that this name does not respect #line directives. Use getPresumedLoc
847 /// for normal clients.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000848 const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Chris Lattner6b306672009-02-04 05:33:01 +0000850 /// getFileCharacteristic - return the file characteristic of the specified
Mike Stump1eb44332009-09-09 15:08:12 +0000851 /// source location, indicating whether this is a normal file, a system
Chris Lattner6b306672009-02-04 05:33:01 +0000852 /// header, or an "implicit extern C" system header.
853 ///
854 /// This state can be modified with flags on GNU linemarker directives like:
855 /// # 4 "foo.h" 3
856 /// which changes all source locations in the current file after that to be
857 /// considered to be from a system header.
858 SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000859
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000860 /// getPresumedLoc - This method returns the "presumed" location of a
861 /// SourceLocation specifies. A "presumed location" can be modified by #line
862 /// or GNU line marker directives. This provides a view on the data that a
863 /// user should see in diagnostics, for example.
864 ///
865 /// Note that a presumed location is always given as the instantiation point
866 /// of an instantiation location, not at the spelling location.
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000867 ///
868 /// \returns The presumed location of the specified SourceLocation. If the
869 /// presumed location cannot be calculate (e.g., because \p Loc is invalid
870 /// or the file containing \p Loc has changed on disk), returns an invalid
871 /// presumed location.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000872 PresumedLoc getPresumedLoc(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000873
Ted Kremenek9fd87b12008-04-14 21:04:18 +0000874 /// isFromSameFile - Returns true if both SourceLocations correspond to
875 /// the same file.
876 bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
Chris Lattnera11d6172009-01-19 07:46:45 +0000877 return getFileID(Loc1) == getFileID(Loc2);
Ted Kremenek9fd87b12008-04-14 21:04:18 +0000878 }
Mike Stump1eb44332009-09-09 15:08:12 +0000879
Ted Kremenek9fd87b12008-04-14 21:04:18 +0000880 /// isFromMainFile - Returns true if the file of provided SourceLocation is
881 /// the main file.
882 bool isFromMainFile(SourceLocation Loc) const {
Chris Lattnera11d6172009-01-19 07:46:45 +0000883 return getFileID(Loc) == getMainFileID();
Mike Stump1eb44332009-09-09 15:08:12 +0000884 }
885
Nico Weber7bfaaae2008-08-10 19:59:06 +0000886 /// isInSystemHeader - Returns if a SourceLocation is in a system header.
887 bool isInSystemHeader(SourceLocation Loc) const {
Chris Lattner0b9e7362008-09-26 21:18:42 +0000888 return getFileCharacteristic(Loc) != SrcMgr::C_User;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000889 }
Mike Stump1eb44332009-09-09 15:08:12 +0000890
Chris Lattner0d456582009-06-13 23:31:51 +0000891 /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
892 /// system header.
893 bool isInExternCSystemHeader(SourceLocation Loc) const {
894 return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
895 }
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +0000897 /// \brief Given a specific chunk of a FileID (FileID with offset+length),
898 /// returns true if \arg Loc is inside that chunk and sets relative offset
899 /// (offset of \arg Loc from beginning of chunk) to \arg relativeOffset.
900 bool isInFileID(SourceLocation Loc,
901 FileID FID, unsigned offset, unsigned length,
902 unsigned *relativeOffset = 0) const {
903 assert(!FID.isInvalid());
904 if (Loc.isInvalid())
905 return false;
906
907 unsigned start = getSLocEntry(FID).getOffset() + offset;
908 unsigned end = start + length;
909
910#ifndef NDEBUG
911 // Make sure offset/length describe a chunk inside the given FileID.
912 unsigned NextOffset;
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000913 if (FID.ID == -2)
914 NextOffset = 1U << 31U;
915 else if (FID.ID+1 == (int)LocalSLocEntryTable.size())
916 NextOffset = getNextLocalOffset();
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +0000917 else
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000918 NextOffset = getSLocEntryByID(FID.ID+1).getOffset();
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +0000919 assert(start < NextOffset);
920 assert(end < NextOffset);
921#endif
922
923 if (Loc.getOffset() >= start && Loc.getOffset() < end) {
924 if (relativeOffset)
925 *relativeOffset = Loc.getOffset() - start;
926 return true;
927 }
928
929 return false;
930 }
Argyrios Kyrtzidis469244a2011-05-28 03:56:11 +0000931
Chris Lattner06a062d2009-01-19 08:02:45 +0000932 //===--------------------------------------------------------------------===//
Chris Lattner5b9a5042009-01-26 07:57:50 +0000933 // Line Table Manipulation Routines
934 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000935
Chris Lattner5b9a5042009-01-26 07:57:50 +0000936 /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
Mike Stump1eb44332009-09-09 15:08:12 +0000937 ///
Chris Lattner686775d2011-07-20 06:58:45 +0000938 unsigned getLineTableFilenameID(StringRef Str);
Mike Stump1eb44332009-09-09 15:08:12 +0000939
Chris Lattner4c4ea172009-02-03 21:52:55 +0000940 /// AddLineNote - Add a line note to the line table for the FileID and offset
941 /// specified by Loc. If FilenameID is -1, it is considered to be
942 /// unspecified.
943 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
Chris Lattner9d79eba2009-02-04 05:21:58 +0000944 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
Mike Stump1eb44332009-09-09 15:08:12 +0000945 bool IsFileEntry, bool IsFileExit,
Chris Lattner9d79eba2009-02-04 05:21:58 +0000946 bool IsSystemHeader, bool IsExternCHeader);
Douglas Gregorbd945002009-04-13 16:31:14 +0000947
948 /// \brief Determine if the source manager has a line table.
949 bool hasLineTable() const { return LineTable != 0; }
950
951 /// \brief Retrieve the stored line table.
952 LineTableInfo &getLineTable();
953
Chris Lattner5b9a5042009-01-26 07:57:50 +0000954 //===--------------------------------------------------------------------===//
Ted Kremenek457aaf02011-04-28 04:10:31 +0000955 // Queries for performance analysis.
956 //===--------------------------------------------------------------------===//
957
958 /// Return the total amount of physical memory allocated by the
959 /// ContentCache allocator.
960 size_t getContentCacheSize() const {
961 return ContentCacheAlloc.getTotalMemory();
962 }
Ted Kremenekf61b8312011-04-28 20:36:42 +0000963
964 struct MemoryBufferSizes {
965 const size_t malloc_bytes;
966 const size_t mmap_bytes;
967
968 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
969 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
970 };
971
972 /// Return the amount of memory used by memory buffers, breaking down
973 /// by heap-backed versus mmap'ed memory.
974 MemoryBufferSizes getMemoryBufferSizes() const;
Ted Kremenek457aaf02011-04-28 04:10:31 +0000975
976 //===--------------------------------------------------------------------===//
Chris Lattner06a062d2009-01-19 08:02:45 +0000977 // Other miscellaneous methods.
978 //===--------------------------------------------------------------------===//
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +0000979
980 /// \brief Get the source location for the given file:line:col triplet.
981 ///
982 /// If the source file is included multiple times, the source location will
983 /// be based upon the first inclusion.
984 SourceLocation getLocation(const FileEntry *SourceFile,
Douglas Gregor86a4d0d2011-02-03 17:17:35 +0000985 unsigned Line, unsigned Col);
Mike Stump1eb44332009-09-09 15:08:12 +0000986
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +0000987 /// \brief Determines the order of 2 source locations in the translation unit.
988 ///
989 /// \returns true if LHS source location comes before RHS, false otherwise.
990 bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
991
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +0000992 /// \brief Determines the order of 2 source locations in the "source location
993 /// address space".
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000994 bool isBeforeInSourceLocationOffset(SourceLocation LHS,
995 SourceLocation RHS) const {
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +0000996 return isBeforeInSourceLocationOffset(LHS, RHS.getOffset());
997 }
998
999 /// \brief Determines the order of a source location and a source location
1000 /// offset in the "source location address space".
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001001 ///
1002 /// Note that we always consider source locations loaded from
1003 bool isBeforeInSourceLocationOffset(SourceLocation LHS, unsigned RHS) const {
1004 unsigned LHSOffset = LHS.getOffset();
1005 bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1006 bool RHSLoaded = RHS >= CurrentLoadedOffset;
1007 if (LHSLoaded == RHSLoaded)
1008 return LHS.getOffset() < RHS;
1009
1010 return LHSLoaded;
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +00001011 }
1012
Chris Lattnerc6fe32a2009-01-17 03:48:08 +00001013 // Iterators over FileInfos.
Chris Lattner0d0bf8c2009-02-03 07:30:45 +00001014 typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
1015 ::const_iterator fileinfo_iterator;
Chris Lattnerc6fe32a2009-01-17 03:48:08 +00001016 fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
1017 fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
Douglas Gregord93256e2010-01-28 06:00:51 +00001018 bool hasFileInfo(const FileEntry *File) const {
1019 return FileInfos.find(File) != FileInfos.end();
1020 }
Chris Lattnerc6fe32a2009-01-17 03:48:08 +00001021
Reid Spencer5f016e22007-07-11 17:01:13 +00001022 /// PrintStats - Print statistics to stderr.
1023 ///
1024 void PrintStats() const;
Reid Spencer5f016e22007-07-11 17:01:13 +00001025
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001026 /// \brief Get the number of local SLocEntries we have.
1027 unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
1028
1029 /// \brief Get a local SLocEntry. This is exposed for indexing.
1030 const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index,
1031 bool *Invalid = 0) const {
1032 assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1033 return LocalSLocEntryTable[Index];
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001034 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001035
1036 /// \brief Get the number of loaded SLocEntries we have.
1037 unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
1038
1039 /// \brief Get a loaded SLocEntry. This is exposed for indexing.
1040 const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index, bool *Invalid=0) const {
1041 assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1042 if (!SLocEntryLoaded[Index])
1043 ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2));
1044 return LoadedSLocEntryTable[Index];
1045 }
1046
Douglas Gregore23ac652011-04-20 00:21:03 +00001047 const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001048 return getSLocEntryByID(FID.ID);
Douglas Gregorbd945002009-04-13 16:31:14 +00001049 }
1050
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001051 unsigned getNextLocalOffset() const { return NextLocalOffset; }
1052
1053 void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1054 assert(LoadedSLocEntryTable.empty() &&
1055 "Invalidating existing loaded entries");
1056 ExternalSLocEntries = Source;
1057 }
1058
1059 /// \brief Allocate a number of loaded SLocEntries, which will be actually
1060 /// loaded on demand from the external source.
1061 ///
1062 /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1063 /// in the global source view. The lowest ID and the base offset of the
1064 /// entries will be returned.
1065 std::pair<int, unsigned>
1066 AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
1067
Ted Kremenek78d85f52007-10-30 21:08:08 +00001068private:
Douglas Gregore23ac652011-04-20 00:21:03 +00001069 const llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1070
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001071 /// \brief Get the entry with the given unwrapped FileID.
1072 const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const {
1073 assert(ID != -1 && "Using FileID sentinel value");
1074 if (ID < 0)
1075 return getLoadedSLocEntryByID(ID);
1076 return getLocalSLocEntry(static_cast<unsigned>(ID));
1077 }
1078
1079 const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID) const {
1080 return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2));
1081 }
1082
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +00001083 /// createInstantiationLoc - Implements the common elements of storing an
1084 /// instantiation info struct into the SLocEntry table and producing a source
1085 /// location that refers to it.
1086 SourceLocation createInstantiationLocImpl(const SrcMgr::InstantiationInfo &II,
1087 unsigned TokLength,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001088 int LoadedID = 0,
1089 unsigned LoadedOffset = 0);
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +00001090
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001091 /// isOffsetInFileID - Return true if the specified FileID contains the
1092 /// specified SourceLocation offset. This is a very hot method.
1093 inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1094 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1095 // If the entry is after the offset, it can't contain it.
1096 if (SLocOffset < Entry.getOffset()) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001097
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001098 // If this is the very last entry then it does.
1099 if (FID.ID == -2)
1100 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001101
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001102 // If it is the last local entry, then it does if the location is local.
1103 if (static_cast<unsigned>(FID.ID+1) == LocalSLocEntryTable.size()) {
1104 return SLocOffset < NextLocalOffset;
1105 }
1106
1107 // Otherwise, the entry after it has to not include it. This works for both
1108 // local and loaded entries.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001109 return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001110 }
Mike Stump1eb44332009-09-09 15:08:12 +00001111
Ted Kremenek78d85f52007-10-30 21:08:08 +00001112 /// createFileID - Create a new fileID for the specified ContentCache and
1113 /// include position. This works regardless of whether the ContentCache
1114 /// corresponds to a file or some other input source.
Chris Lattner2b2453a2009-01-17 06:22:33 +00001115 FileID createFileID(const SrcMgr::ContentCache* File,
1116 SourceLocation IncludePos,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001117 SrcMgr::CharacteristicKind DirCharacter,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001118 int LoadedID, unsigned LoadedOffset);
Mike Stump1eb44332009-09-09 15:08:12 +00001119
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001120 const SrcMgr::ContentCache *
1121 getOrCreateContentCache(const FileEntry *SourceFile);
Ted Kremenekc16c2082009-01-06 01:55:26 +00001122
Ted Kremenek78d85f52007-10-30 21:08:08 +00001123 /// createMemBufferContentCache - Create a new ContentCache for the specified
1124 /// memory buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001125 const SrcMgr::ContentCache*
Chris Lattner2b2453a2009-01-17 06:22:33 +00001126 createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
Mike Stump1eb44332009-09-09 15:08:12 +00001127
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001128 FileID getFileIDSlow(unsigned SLocOffset) const;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001129 FileID getFileIDLocal(unsigned SLocOffset) const;
1130 FileID getFileIDLoaded(unsigned SLocOffset) const;
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001131
Chandler Carruthf84ef952011-07-25 20:52:26 +00001132 SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
Chris Lattneraddb7972009-01-26 20:04:19 +00001133 SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1134
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001135 std::pair<FileID, unsigned>
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +00001136 getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001137 std::pair<FileID, unsigned>
1138 getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1139 unsigned Offset) const;
Reid Spencer5f016e22007-07-11 17:01:13 +00001140};
1141
1142
1143} // end namespace clang
1144
1145#endif