blob: 4aed17ac13f915d3bad02c4580b9643643b2a3a7 [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"
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +000026#include <map>
Reid Spencer5f016e22007-07-11 17:01:13 +000027#include <vector>
Chris Lattner9dc62f02007-07-12 15:32:57 +000028#include <cassert>
Reid Spencer5f016e22007-07-11 17:01:13 +000029
Reid Spencer5f016e22007-07-11 17:01:13 +000030namespace clang {
Mike Stump1eb44332009-09-09 15:08:12 +000031
David Blaikied6471f72011-09-25 23:23:43 +000032class DiagnosticsEngine;
Reid Spencer5f016e22007-07-11 17:01:13 +000033class SourceManager;
Ted Kremenek099b4742007-12-05 00:14:18 +000034class FileManager;
Reid Spencer5f016e22007-07-11 17:01:13 +000035class FileEntry;
Chris Lattner5b9a5042009-01-26 07:57:50 +000036class LineTableInfo;
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +000037class LangOptions;
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +000038class ASTWriter;
39class ASTReader;
Eric Christopher5330ee02011-09-08 23:28:19 +000040
Eric Christopher29f39422011-09-08 17:15:01 +000041/// There are three different types of locations in a file: a spelling
42/// location, an expansion location, and a presumed location.
43///
44/// Given an example of:
45/// #define min(x, y) x < y ? x : y
46///
47/// and then later on a use of min:
Eric Christopher29f39422011-09-08 17:15:01 +000048/// #line 17
Eric Christopher5330ee02011-09-08 23:28:19 +000049/// return min(a, b);
Eric Christopher29f39422011-09-08 17:15:01 +000050///
51/// The expansion location is the line in the source code where the macro
52/// was expanded (the return statement), the spelling location is the
53/// location in the source where the macro was originally defined,
54/// and the presumed location is where the line directive states that
55/// the line is 17, or any other line.
56
Chris Lattner0b9e7362008-09-26 21:18:42 +000057/// SrcMgr - Public enums and private classes that are part of the
58/// SourceManager implementation.
Reid Spencer5f016e22007-07-11 17:01:13 +000059///
60namespace SrcMgr {
Chris Lattner9d728512008-10-27 01:19:25 +000061 /// CharacteristicKind - This is used to represent whether a file or directory
Chris Lattner0b9e7362008-09-26 21:18:42 +000062 /// holds normal user code, system code, or system code which is implicitly
63 /// 'extern "C"' in C++ mode. Entire directories can be tagged with this
64 /// (this is maintained by DirectoryLookup and friends) as can specific
Douglas Gregorf62d43d2011-07-19 16:10:42 +000065 /// FileInfos when a #pragma system_header is seen or various other cases.
Chris Lattner0b9e7362008-09-26 21:18:42 +000066 ///
Chris Lattner9d728512008-10-27 01:19:25 +000067 enum CharacteristicKind {
Chris Lattner0b9e7362008-09-26 21:18:42 +000068 C_User, C_System, C_ExternCSystem
69 };
Mike Stump1eb44332009-09-09 15:08:12 +000070
Dan Gohman4710a8e2010-08-25 21:59:25 +000071 /// ContentCache - One instance of this struct is kept for every file
Chris Lattner06a062d2009-01-19 08:02:45 +000072 /// loaded or used. This object owns the MemoryBuffer object.
Ted Kremenekc16c2082009-01-06 01:55:26 +000073 class ContentCache {
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +000074 enum CCFlags {
75 /// \brief Whether the buffer is invalid.
76 InvalidFlag = 0x01,
77 /// \brief Whether the buffer should not be freed on destruction.
78 DoNotFreeFlag = 0x02
79 };
Eric Christopher5330ee02011-09-08 23:28:19 +000080
Ted Kremenekc16c2082009-01-06 01:55:26 +000081 /// Buffer - The actual buffer containing the characters from the input
82 /// file. This is owned by the ContentCache object.
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +000083 /// The bits indicate indicates whether the buffer is invalid.
84 mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
Ted Kremenekc16c2082009-01-06 01:55:26 +000085
86 public:
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +000087 /// Reference to the file entry representing this ContentCache.
88 /// This reference does not own the FileEntry object.
89 /// It is possible for this to be NULL if
Ted Kremenek78d85f52007-10-30 21:08:08 +000090 /// the ContentCache encapsulates an imaginary text buffer.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +000091 const FileEntry *OrigEntry;
92
93 /// \brief References the file which the contents were actually loaded from.
94 /// Can be different from 'Entry' if we overridden the contents of one file
95 /// with the contents of another file.
96 const FileEntry *ContentsEntry;
Mike Stump1eb44332009-09-09 15:08:12 +000097
Chris Lattner0d0bf8c2009-02-03 07:30:45 +000098 /// SourceLineCache - A bump pointer allocated array of offsets for each
99 /// source line. This is lazily computed. This is owned by the
100 /// SourceManager BumpPointerAllocator object.
Chris Lattner05816592009-01-17 03:54:16 +0000101 unsigned *SourceLineCache;
Mike Stump1eb44332009-09-09 15:08:12 +0000102
Ted Kremenekb6427f82007-12-04 18:59:28 +0000103 /// NumLines - The number of lines in this ContentCache. This is only valid
104 /// if SourceLineCache is non-null.
Douglas Gregora081da52011-11-16 20:05:18 +0000105 unsigned NumLines : 31;
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +0000106
Douglas Gregora081da52011-11-16 20:05:18 +0000107 /// \brief Indicates whether the buffer itself was provided to override
108 /// the actual file contents.
109 ///
110 /// When true, the original entry may be a virtual file that does not
111 /// exist.
112 unsigned BufferOverridden : 1;
113
Douglas Gregor7955a252011-11-19 09:42:42 +0000114 ContentCache(const FileEntry *Ent = 0)
115 : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent),
116 SourceLineCache(0), NumLines(0), BufferOverridden(false) {}
117
118 ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
119 : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt),
120 SourceLineCache(0), NumLines(0), BufferOverridden(false) {}
121
122 ~ContentCache();
123
124 /// The copy ctor does not allow copies where source object has either
125 /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory
126 /// is not transferred, so this is a logical error.
127 ContentCache(const ContentCache &RHS)
128 : Buffer(0, false), SourceLineCache(0), BufferOverridden(false)
129 {
130 OrigEntry = RHS.OrigEntry;
131 ContentsEntry = RHS.ContentsEntry;
132
133 assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0 &&
134 "Passed ContentCache object cannot own a buffer.");
135
136 NumLines = RHS.NumLines;
137 }
138
Douglas Gregor36c35ba2010-03-16 00:35:39 +0000139 /// getBuffer - Returns the memory buffer for the associated content.
140 ///
Jonathan D. Turnera92d7e72011-06-16 20:47:21 +0000141 /// \param Diag Object through which diagnostics will be emitted if the
Douglas Gregor36c35ba2010-03-16 00:35:39 +0000142 /// buffer cannot be retrieved.
Eric Christopher5330ee02011-09-08 23:28:19 +0000143 ///
Chris Lattnere127a0d2010-04-20 20:35:58 +0000144 /// \param Loc If specified, is the location that invalid file diagnostics
145 /// will be emitted at.
146 ///
Douglas Gregor36c35ba2010-03-16 00:35:39 +0000147 /// \param Invalid If non-NULL, will be set \c true if an error occurred.
David Blaikied6471f72011-09-25 23:23:43 +0000148 const llvm::MemoryBuffer *getBuffer(DiagnosticsEngine &Diag,
Chris Lattnere127a0d2010-04-20 20:35:58 +0000149 const SourceManager &SM,
150 SourceLocation Loc = SourceLocation(),
Douglas Gregor36c35ba2010-03-16 00:35:39 +0000151 bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000152
Ted Kremenekc16c2082009-01-06 01:55:26 +0000153 /// getSize - Returns the size of the content encapsulated by this
154 /// ContentCache. This can be the size of the source file or the size of an
155 /// arbitrary scratch buffer. If the ContentCache encapsulates a source
156 /// file this size is retrieved from the file's FileEntry.
157 unsigned getSize() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000158
Ted Kremenekc16c2082009-01-06 01:55:26 +0000159 /// getSizeBytesMapped - Returns the number of bytes actually mapped for
Chandler Carruth3201f382011-07-26 05:17:23 +0000160 /// this ContentCache. This can be 0 if the MemBuffer was not actually
161 /// expanded.
Ted Kremenekc16c2082009-01-06 01:55:26 +0000162 unsigned getSizeBytesMapped() const;
Eric Christopher5330ee02011-09-08 23:28:19 +0000163
Ted Kremenekf61b8312011-04-28 20:36:42 +0000164 /// Returns the kind of memory used to back the memory buffer for
165 /// this content cache. This is used for performance analysis.
166 llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000167
Chris Lattner05816592009-01-17 03:54:16 +0000168 void setBuffer(const llvm::MemoryBuffer *B) {
Douglas Gregorc8151082010-03-16 22:53:51 +0000169 assert(!Buffer.getPointer() && "MemoryBuffer already set.");
170 Buffer.setPointer(B);
171 Buffer.setInt(false);
Ted Kremenekc16c2082009-01-06 01:55:26 +0000172 }
Eric Christopher5330ee02011-09-08 23:28:19 +0000173
Douglas Gregorcc5888d2010-07-31 00:40:00 +0000174 /// \brief Get the underlying buffer, returning NULL if the buffer is not
175 /// yet available.
176 const llvm::MemoryBuffer *getRawBuffer() const {
177 return Buffer.getPointer();
178 }
Mike Stump1eb44332009-09-09 15:08:12 +0000179
Douglas Gregor29684422009-12-02 06:49:09 +0000180 /// \brief Replace the existing buffer (which will be deleted)
181 /// with the given buffer.
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000182 void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
Douglas Gregor29684422009-12-02 06:49:09 +0000183
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000184 /// \brief Determine whether the buffer itself is invalid.
185 bool isBufferInvalid() const {
186 return Buffer.getInt() & InvalidFlag;
187 }
Eric Christopher5330ee02011-09-08 23:28:19 +0000188
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000189 /// \brief Determine whether the buffer should be freed.
190 bool shouldFreeBuffer() const {
191 return (Buffer.getInt() & DoNotFreeFlag) == 0;
192 }
Eric Christopher5330ee02011-09-08 23:28:19 +0000193
Ted Kremenek0d892d82007-10-30 22:57:35 +0000194 private:
195 // Disable assignments.
Mike Stump1eb44332009-09-09 15:08:12 +0000196 ContentCache &operator=(const ContentCache& RHS);
197 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000198
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000199 /// FileInfo - Information about a FileID, basically just the logical file
200 /// that it represents and include stack information.
Reid Spencer5f016e22007-07-11 17:01:13 +0000201 ///
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000202 /// Each FileInfo has include stack information, indicating where it came
Chandler Carruth3201f382011-07-26 05:17:23 +0000203 /// from. This information encodes the #include chain that a token was
204 /// expanded from. The main include file has an invalid IncludeLoc.
Reid Spencer5f016e22007-07-11 17:01:13 +0000205 ///
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000206 /// FileInfos contain a "ContentCache *", with the contents of the file.
Reid Spencer5f016e22007-07-11 17:01:13 +0000207 ///
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000208 class FileInfo {
Reid Spencer5f016e22007-07-11 17:01:13 +0000209 /// IncludeLoc - The location of the #include that brought in this file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000210 /// This is an invalid SLOC for the main file (top of the #include chain).
211 unsigned IncludeLoc; // Really a SourceLocation
Mike Stump1eb44332009-09-09 15:08:12 +0000212
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000213 /// \brief Number of FileIDs (files and macros) that were created during
214 /// preprocessing of this #include, including this SLocEntry.
215 /// Zero means the preprocessor didn't provide such info for this SLocEntry.
216 unsigned NumCreatedFIDs;
217
Chris Lattner6e1aff22009-01-26 06:49:09 +0000218 /// Data - This contains the ContentCache* and the bits indicating the
219 /// characteristic of the file and whether it has #line info, all bitmangled
220 /// together.
221 uintptr_t Data;
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000222
Argyrios Kyrtzidis21032df2011-08-21 23:49:52 +0000223 friend class clang::SourceManager;
224 friend class clang::ASTWriter;
225 friend class clang::ASTReader;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000226 public:
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000227 /// get - Return a FileInfo object.
228 static FileInfo get(SourceLocation IL, const ContentCache *Con,
229 CharacteristicKind FileCharacter) {
230 FileInfo X;
231 X.IncludeLoc = IL.getRawEncoding();
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000232 X.NumCreatedFIDs = 0;
Chris Lattner6e1aff22009-01-26 06:49:09 +0000233 X.Data = (uintptr_t)Con;
Chris Lattner00282d62009-02-03 07:41:46 +0000234 assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
Chris Lattner6e1aff22009-01-26 06:49:09 +0000235 assert((unsigned)FileCharacter < 4 && "invalid file character");
236 X.Data |= (unsigned)FileCharacter;
Reid Spencer5f016e22007-07-11 17:01:13 +0000237 return X;
238 }
Mike Stump1eb44332009-09-09 15:08:12 +0000239
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000240 SourceLocation getIncludeLoc() const {
241 return SourceLocation::getFromRawEncoding(IncludeLoc);
242 }
Chris Lattner6e1aff22009-01-26 06:49:09 +0000243 const ContentCache* getContentCache() const {
Chris Lattner00282d62009-02-03 07:41:46 +0000244 return reinterpret_cast<const ContentCache*>(Data & ~7UL);
Chris Lattner6e1aff22009-01-26 06:49:09 +0000245 }
Mike Stump1eb44332009-09-09 15:08:12 +0000246
Chris Lattner0b9e7362008-09-26 21:18:42 +0000247 /// getCharacteristic - Return whether this is a system header or not.
Mike Stump1eb44332009-09-09 15:08:12 +0000248 CharacteristicKind getFileCharacteristic() const {
Chris Lattner6e1aff22009-01-26 06:49:09 +0000249 return (CharacteristicKind)(Data & 3);
Chris Lattner0b9e7362008-09-26 21:18:42 +0000250 }
Chris Lattnerac50e342009-02-03 22:13:05 +0000251
252 /// hasLineDirectives - Return true if this FileID has #line directives in
253 /// it.
254 bool hasLineDirectives() const { return (Data & 4) != 0; }
Mike Stump1eb44332009-09-09 15:08:12 +0000255
Chris Lattnerac50e342009-02-03 22:13:05 +0000256 /// setHasLineDirectives - Set the flag that indicates that this FileID has
257 /// line table entries associated with it.
258 void setHasLineDirectives() {
259 Data |= 4;
260 }
Chris Lattner9dc1f532007-07-20 16:37:10 +0000261 };
Mike Stump1eb44332009-09-09 15:08:12 +0000262
Chandler Carruth78df8362011-07-26 04:41:47 +0000263 /// ExpansionInfo - Each ExpansionInfo encodes the expansion location - where
264 /// the token was ultimately expanded, and the SpellingLoc - where the actual
265 /// character data for the token came from.
266 class ExpansionInfo {
267 // Really these are all SourceLocations.
Mike Stump1eb44332009-09-09 15:08:12 +0000268
Chris Lattnere7fb4842009-02-15 20:52:18 +0000269 /// SpellingLoc - Where the spelling for the token can be found.
270 unsigned SpellingLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000271
Chandler Carruth78df8362011-07-26 04:41:47 +0000272 /// ExpansionLocStart/ExpansionLocEnd - In a macro expansion, these
273 /// indicate the start and end of the expansion. In object-like macros,
Chandler Carruth3201f382011-07-26 05:17:23 +0000274 /// these will be the same. In a function-like macro expansion, the start
275 /// will be the identifier and the end will be the ')'. Finally, in
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000276 /// macro-argument instantitions, the end will be 'SourceLocation()', an
277 /// invalid location.
Chandler Carruth78df8362011-07-26 04:41:47 +0000278 unsigned ExpansionLocStart, ExpansionLocEnd;
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000279
Chris Lattner9dc1f532007-07-20 16:37:10 +0000280 public:
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000281 SourceLocation getSpellingLoc() const {
282 return SourceLocation::getFromRawEncoding(SpellingLoc);
283 }
Chandler Carruth78df8362011-07-26 04:41:47 +0000284 SourceLocation getExpansionLocStart() const {
285 return SourceLocation::getFromRawEncoding(ExpansionLocStart);
Chris Lattnere7fb4842009-02-15 20:52:18 +0000286 }
Chandler Carruth78df8362011-07-26 04:41:47 +0000287 SourceLocation getExpansionLocEnd() const {
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000288 SourceLocation EndLoc =
Chandler Carruth78df8362011-07-26 04:41:47 +0000289 SourceLocation::getFromRawEncoding(ExpansionLocEnd);
290 return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc;
Chris Lattnere7fb4842009-02-15 20:52:18 +0000291 }
Mike Stump1eb44332009-09-09 15:08:12 +0000292
Chandler Carruth78df8362011-07-26 04:41:47 +0000293 std::pair<SourceLocation,SourceLocation> getExpansionLocRange() const {
294 return std::make_pair(getExpansionLocStart(), getExpansionLocEnd());
Chris Lattnere7fb4842009-02-15 20:52:18 +0000295 }
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Chandler Carruth96d35892011-07-26 03:03:00 +0000297 bool isMacroArgExpansion() const {
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000298 // Note that this needs to return false for default constructed objects.
Chandler Carruth78df8362011-07-26 04:41:47 +0000299 return getExpansionLocStart().isValid() &&
300 SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid();
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000301 }
302
Chandler Carruth78df8362011-07-26 04:41:47 +0000303 /// create - Return a ExpansionInfo for an expansion. Start and End specify
304 /// the expansion range (where the macro is expanded), and SpellingLoc
305 /// specifies the spelling location (where the characters from the token
306 /// come from). All three can refer to normal File SLocs or expansion
307 /// locations.
308 static ExpansionInfo create(SourceLocation SpellingLoc,
309 SourceLocation Start, SourceLocation End) {
310 ExpansionInfo X;
311 X.SpellingLoc = SpellingLoc.getRawEncoding();
312 X.ExpansionLocStart = Start.getRawEncoding();
313 X.ExpansionLocEnd = End.getRawEncoding();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000314 return X;
Reid Spencer5f016e22007-07-11 17:01:13 +0000315 }
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000316
Chandler Carruth78df8362011-07-26 04:41:47 +0000317 /// createForMacroArg - Return a special ExpansionInfo for the expansion of
318 /// a macro argument into a function-like macro's body. ExpansionLoc
319 /// specifies the expansion location (where the macro is expanded). This
320 /// doesn't need to be a range because a macro is always expanded at
321 /// a macro parameter reference, and macro parameters are always exactly
322 /// one token. SpellingLoc specifies the spelling location (where the
323 /// characters from the token come from). ExpansionLoc and SpellingLoc can
324 /// both refer to normal File SLocs or expansion locations.
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000325 ///
326 /// Given the code:
327 /// \code
328 /// #define F(x) f(x)
329 /// F(42);
330 /// \endcode
331 ///
Chandler Carruth78df8362011-07-26 04:41:47 +0000332 /// When expanding '\c F(42)', the '\c x' would call this with an
333 /// SpellingLoc pointing at '\c 42' anad an ExpansionLoc pointing at its
334 /// location in the definition of '\c F'.
335 static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc,
336 SourceLocation ExpansionLoc) {
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000337 // We store an intentionally invalid source location for the end of the
Chandler Carruth78df8362011-07-26 04:41:47 +0000338 // expansion range to mark that this is a macro argument ion rather than
339 // a normal one.
340 return create(SpellingLoc, ExpansionLoc, SourceLocation());
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000341 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000342 };
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000344 /// SLocEntry - This is a discriminated union of FileInfo and
Chandler Carruth78df8362011-07-26 04:41:47 +0000345 /// ExpansionInfo. SourceManager keeps an array of these objects, and
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000346 /// they are uniquely identified by the FileID datatype.
347 class SLocEntry {
Chandler Carruth3201f382011-07-26 05:17:23 +0000348 unsigned Offset; // low bit is set for expansion info.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000349 union {
350 FileInfo File;
Chandler Carruth17287622011-07-26 04:56:51 +0000351 ExpansionInfo Expansion;
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000352 };
353 public:
354 unsigned getOffset() const { return Offset >> 1; }
Mike Stump1eb44332009-09-09 15:08:12 +0000355
Chandler Carruth17287622011-07-26 04:56:51 +0000356 bool isExpansion() const { return Offset & 1; }
357 bool isFile() const { return !isExpansion(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000358
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000359 const FileInfo &getFile() const {
360 assert(isFile() && "Not a file SLocEntry!");
361 return File;
362 }
363
Chandler Carruth17287622011-07-26 04:56:51 +0000364 const ExpansionInfo &getExpansion() const {
365 assert(isExpansion() && "Not a macro expansion SLocEntry!");
366 return Expansion;
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000367 }
Mike Stump1eb44332009-09-09 15:08:12 +0000368
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000369 static SLocEntry get(unsigned Offset, const FileInfo &FI) {
370 SLocEntry E;
371 E.Offset = Offset << 1;
372 E.File = FI;
373 return E;
374 }
375
Chandler Carruth78df8362011-07-26 04:41:47 +0000376 static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000377 SLocEntry E;
378 E.Offset = (Offset << 1) | 1;
Chandler Carruth17287622011-07-26 04:56:51 +0000379 E.Expansion = Expansion;
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000380 return E;
381 }
382 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000383} // end SrcMgr namespace.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000384
385/// \brief External source of source location entries.
386class ExternalSLocEntrySource {
387public:
388 virtual ~ExternalSLocEntrySource();
389
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000390 /// \brief Read the source location entry with index ID, which will always be
391 /// less than -1.
Douglas Gregore23ac652011-04-20 00:21:03 +0000392 ///
393 /// \returns true if an error occurred that prevented the source-location
394 /// entry from being loaded.
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000395 virtual bool ReadSLocEntry(int ID) = 0;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000396};
Eric Christopher5330ee02011-09-08 23:28:19 +0000397
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000398
399/// IsBeforeInTranslationUnitCache - This class holds the cache used by
400/// isBeforeInTranslationUnit. The cache structure is complex enough to be
401/// worth breaking out of SourceManager.
402class IsBeforeInTranslationUnitCache {
403 /// L/R QueryFID - These are the FID's of the cached query. If these match up
404 /// with a subsequent query, the result can be reused.
405 FileID LQueryFID, RQueryFID;
Argyrios Kyrtzidis37e59a12011-08-17 00:31:18 +0000406
407 /// \brief True if LQueryFID was created before RQueryFID. This is used
408 /// to compare macro expansion locations.
409 bool IsLQFIDBeforeRQFID;
410
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000411 /// CommonFID - This is the file found in common between the two #include
412 /// traces. It is the nearest common ancestor of the #include tree.
413 FileID CommonFID;
Eric Christopher5330ee02011-09-08 23:28:19 +0000414
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000415 /// L/R CommonOffset - This is the offset of the previous query in CommonFID.
416 /// Usually, this represents the location of the #include for QueryFID, but if
417 /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a
418 /// random token in the parent.
419 unsigned LCommonOffset, RCommonOffset;
420public:
Eric Christopher5330ee02011-09-08 23:28:19 +0000421
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000422 /// isCacheValid - Return true if the currently cached values match up with
423 /// the specified LHS/RHS query. If not, we can't use the cache.
424 bool isCacheValid(FileID LHS, FileID RHS) const {
425 return LQueryFID == LHS && RQueryFID == RHS;
426 }
Eric Christopher5330ee02011-09-08 23:28:19 +0000427
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000428 /// getCachedResult - If the cache is valid, compute the result given the
429 /// specified offsets in the LHS/RHS FID's.
430 bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
431 // If one of the query files is the common file, use the offset. Otherwise,
432 // use the #include loc in the common file.
433 if (LQueryFID != CommonFID) LOffset = LCommonOffset;
434 if (RQueryFID != CommonFID) ROffset = RCommonOffset;
Argyrios Kyrtzidis37e59a12011-08-17 00:31:18 +0000435
436 // It is common for multiple macro expansions to be "included" from the same
437 // location (expansion location), in which case use the order of the FileIDs
Argyrios Kyrtzidis4d1cbcf2011-09-19 20:39:51 +0000438 // to determine which came first. This will also take care the case where
439 // one of the locations points at the inclusion/expansion point of the other
440 // in which case its FileID will come before the other.
Argyrios Kyrtzidisd7711ec2011-12-21 16:56:29 +0000441 if (LOffset == ROffset)
Argyrios Kyrtzidis37e59a12011-08-17 00:31:18 +0000442 return IsLQFIDBeforeRQFID;
443
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000444 return LOffset < ROffset;
445 }
Eric Christopher5330ee02011-09-08 23:28:19 +0000446
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000447 // Set up a new query.
Argyrios Kyrtzidis37e59a12011-08-17 00:31:18 +0000448 void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) {
449 assert(LHS != RHS);
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000450 LQueryFID = LHS;
451 RQueryFID = RHS;
Argyrios Kyrtzidis37e59a12011-08-17 00:31:18 +0000452 IsLQFIDBeforeRQFID = isLFIDBeforeRFID;
453 }
454
455 void clear() {
456 LQueryFID = RQueryFID = FileID();
457 IsLQFIDBeforeRQFID = false;
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000458 }
Eric Christopher5330ee02011-09-08 23:28:19 +0000459
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000460 void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
461 unsigned rCommonOffset) {
462 CommonFID = commonFID;
463 LCommonOffset = lCommonOffset;
464 RCommonOffset = rCommonOffset;
465 }
Eric Christopher5330ee02011-09-08 23:28:19 +0000466
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000467};
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000468
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000469/// \brief This class handles loading and caching of source files into memory.
470///
471/// This object owns the MemoryBuffer objects for all of the loaded
Reid Spencer5f016e22007-07-11 17:01:13 +0000472/// files and assigns unique FileID's for each unique #include chain.
473///
474/// The SourceManager can be queried for information about SourceLocation
Chandler Carruth3201f382011-07-26 05:17:23 +0000475/// objects, turning them into either spelling or expansion locations. Spelling
476/// locations represent where the bytes corresponding to a token came from and
477/// expansion locations represent where the location is in the user's view. In
478/// the case of a macro expansion, for example, the spelling location indicates
479/// where the expanded token came from and the expansion location specifies
480/// where it was expanded.
Ted Kremenek4f327862011-03-21 18:40:17 +0000481class SourceManager : public llvm::RefCountedBase<SourceManager> {
David Blaikied6471f72011-09-25 23:23:43 +0000482 /// \brief DiagnosticsEngine object.
483 DiagnosticsEngine &Diag;
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000484
485 FileManager &FileMgr;
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000486
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000487 mutable llvm::BumpPtrAllocator ContentCacheAlloc;
Mike Stump1eb44332009-09-09 15:08:12 +0000488
Reid Spencer5f016e22007-07-11 17:01:13 +0000489 /// FileInfos - Memoized information about all of the files tracked by this
Ted Kremenek0d892d82007-10-30 22:57:35 +0000490 /// SourceManager. This set allows us to merge ContentCache entries based
491 /// on their FileEntry*. All ContentCache objects will thus have unique,
Mike Stump1eb44332009-09-09 15:08:12 +0000492 /// non-null, FileEntry pointers.
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000493 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000494
Argyrios Kyrtzidis299a4a92011-03-08 23:35:24 +0000495 /// \brief True if the ContentCache for files that are overriden by other
496 /// files, should report the original file name. Defaults to true.
497 bool OverridenFilesKeepOriginalName;
498
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000499 /// \brief Files that have been overriden with the contents from another file.
500 llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
501
Reid Spencer5f016e22007-07-11 17:01:13 +0000502 /// MemBufferInfos - Information about various memory buffers that we have
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000503 /// read in. All FileEntry* within the stored ContentCache objects are NULL,
504 /// as they do not refer to a file.
505 std::vector<SrcMgr::ContentCache*> MemBufferInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000506
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000507 /// \brief The table of SLocEntries that are local to this module.
508 ///
509 /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid
Chandler Carruth3201f382011-07-26 05:17:23 +0000510 /// expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000511 std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000512
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000513 /// \brief The table of SLocEntries that are loaded from other modules.
514 ///
515 /// Negative FileIDs are indexes into this table. To get from ID to an index,
516 /// use (-ID - 2).
517 std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable;
518
519 /// \brief The starting offset of the next local SLocEntry.
520 ///
521 /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
522 unsigned NextLocalOffset;
523
524 /// \brief The starting offset of the latest batch of loaded SLocEntries.
525 ///
526 /// This is LoadedSLocEntryTable.back().Offset, except that that entry might
527 /// not have been loaded, so that value would be unknown.
528 unsigned CurrentLoadedOffset;
529
Argyrios Kyrtzidisac836e42011-08-17 00:31:20 +0000530 /// \brief The highest possible offset is 2^31-1, so CurrentLoadedOffset
531 /// starts at 2^31.
532 static const unsigned MaxLoadedOffset = 1U << 31U;
533
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000534 /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable
535 /// have already been loaded from the external source.
536 ///
537 /// Same indexing as LoadedSLocEntryTable.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000538 std::vector<bool> SLocEntryLoaded;
539
540 /// \brief An external source for source location entries.
541 ExternalSLocEntrySource *ExternalSLocEntries;
542
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000543 /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
544 /// LastFileIDLookup records the last FileID looked up or created, because it
545 /// is very common to look up many tokens from the same file.
546 mutable FileID LastFileIDLookup;
Mike Stump1eb44332009-09-09 15:08:12 +0000547
Chris Lattner5b9a5042009-01-26 07:57:50 +0000548 /// LineTable - This holds information for #line directives. It is referenced
549 /// by indices from SLocEntryTable.
550 LineTableInfo *LineTable;
Mike Stump1eb44332009-09-09 15:08:12 +0000551
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000552 /// LastLineNo - These ivars serve as a cache used in the getLineNumber
553 /// method which is used to speedup getLineNumber calls to nearby locations.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000554 mutable FileID LastLineNoFileIDQuery;
Chris Lattnerf812a452008-11-18 06:51:15 +0000555 mutable SrcMgr::ContentCache *LastLineNoContentCache;
556 mutable unsigned LastLineNoFilePos;
557 mutable unsigned LastLineNoResult;
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000559 /// MainFileID - The file ID for the main source file of the translation unit.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000560 FileID MainFileID;
Steve Naroff49c1f4a2008-02-02 00:10:46 +0000561
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +0000562 /// \brief The file ID for the precompiled preamble there is one.
563 FileID PreambleFileID;
564
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000565 // Statistics for -print-stats.
566 mutable unsigned NumLinearScans, NumBinaryProbes;
Mike Stump1eb44332009-09-09 15:08:12 +0000567
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +0000568 // Cache results for the isBeforeInTranslationUnit method.
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000569 mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
Mike Stump1eb44332009-09-09 15:08:12 +0000570
Douglas Gregore23ac652011-04-20 00:21:03 +0000571 // Cache for the "fake" buffer used for error-recovery purposes.
572 mutable llvm::MemoryBuffer *FakeBufferForRecovery;
Eric Christopher5330ee02011-09-08 23:28:19 +0000573
Argyrios Kyrtzidisfb3612e2011-09-26 08:01:50 +0000574 /// \brief Lazily computed map of macro argument chunks to their expanded
575 /// source location.
576 typedef std::map<unsigned, SourceLocation> MacroArgsMap;
577
David Blaikie70042f52011-10-20 01:45:20 +0000578 mutable llvm::DenseMap<FileID, MacroArgsMap *> MacroArgsCacheMap;
Argyrios Kyrtzidisfb3612e2011-09-26 08:01:50 +0000579
Steve Naroff49c1f4a2008-02-02 00:10:46 +0000580 // SourceManager doesn't support copy construction.
581 explicit SourceManager(const SourceManager&);
Mike Stump1eb44332009-09-09 15:08:12 +0000582 void operator=(const SourceManager&);
Reid Spencer5f016e22007-07-11 17:01:13 +0000583public:
David Blaikied6471f72011-09-25 23:23:43 +0000584 SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr);
Chris Lattner5b9a5042009-01-26 07:57:50 +0000585 ~SourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Chris Lattner5b9a5042009-01-26 07:57:50 +0000587 void clearIDTables();
Mike Stump1eb44332009-09-09 15:08:12 +0000588
David Blaikied6471f72011-09-25 23:23:43 +0000589 DiagnosticsEngine &getDiagnostics() const { return Diag; }
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +0000590
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000591 FileManager &getFileManager() const { return FileMgr; }
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000592
Argyrios Kyrtzidis299a4a92011-03-08 23:35:24 +0000593 /// \brief Set true if the SourceManager should report the original file name
594 /// for contents of files that were overriden by other files.Defaults to true.
595 void setOverridenFilesKeepOriginalName(bool value) {
596 OverridenFilesKeepOriginalName = value;
597 }
598
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000599 /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
600 /// that will represent the FileID for the main source. One example
601 /// of when this would be used is when the main source is read from STDIN.
602 FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
603 assert(MainFileID.isInvalid() && "MainFileID already set!");
604 MainFileID = createFileIDForMemBuffer(Buffer);
605 return MainFileID;
606 }
607
Chris Lattner06a062d2009-01-19 08:02:45 +0000608 //===--------------------------------------------------------------------===//
609 // MainFileID creation and querying methods.
610 //===--------------------------------------------------------------------===//
611
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000612 /// getMainFileID - Returns the FileID of the main source file.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000613 FileID getMainFileID() const { return MainFileID; }
Mike Stump1eb44332009-09-09 15:08:12 +0000614
Chris Lattner06a062d2009-01-19 08:02:45 +0000615 /// createMainFileID - Create the FileID for the main source file.
Dan Gohmanf155dfa2010-08-27 15:44:11 +0000616 FileID createMainFileID(const FileEntry *SourceFile) {
Chris Lattner06a062d2009-01-19 08:02:45 +0000617 assert(MainFileID.isInvalid() && "MainFileID already set!");
Dan Gohmanf155dfa2010-08-27 15:44:11 +0000618 MainFileID = createFileID(SourceFile, SourceLocation(), SrcMgr::C_User);
Chris Lattner06a062d2009-01-19 08:02:45 +0000619 return MainFileID;
620 }
Mike Stump1eb44332009-09-09 15:08:12 +0000621
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +0000622 /// \brief Set the file ID for the precompiled preamble.
623 void setPreambleFileID(FileID Preamble) {
624 assert(PreambleFileID.isInvalid() && "PreambleFileID already set!");
625 PreambleFileID = Preamble;
Douglas Gregor414cb642010-11-30 05:23:00 +0000626 }
Eric Christopher5330ee02011-09-08 23:28:19 +0000627
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +0000628 /// \brief Get the file ID for the precompiled preamble if there is one.
629 FileID getPreambleFileID() const { return PreambleFileID; }
630
Chris Lattner06a062d2009-01-19 08:02:45 +0000631 //===--------------------------------------------------------------------===//
Chandler Carruth3201f382011-07-26 05:17:23 +0000632 // Methods to create new FileID's and macro expansions.
Chris Lattner06a062d2009-01-19 08:02:45 +0000633 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Reid Spencer5f016e22007-07-11 17:01:13 +0000635 /// createFileID - Create a new FileID that represents the specified file
Peter Collingbourned57b7ff2011-06-30 16:41:03 +0000636 /// being #included from the specified IncludePosition. This translates NULL
637 /// into standard input.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000638 FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000639 SrcMgr::CharacteristicKind FileCharacter,
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000640 int LoadedID = 0, unsigned LoadedOffset = 0) {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000641 const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
Dan Gohman0d06e992010-10-26 20:47:28 +0000642 assert(IR && "getOrCreateContentCache() cannot return NULL");
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000643 return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset);
Reid Spencer5f016e22007-07-11 17:01:13 +0000644 }
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Reid Spencer5f016e22007-07-11 17:01:13 +0000646 /// createFileIDForMemBuffer - Create a new FileID that represents the
647 /// specified memory buffer. This does no caching of the buffer and takes
648 /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000649 FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
Axel Naumannf453cb92011-10-31 11:02:24 +0000650 int LoadedID = 0, unsigned LoadedOffset = 0,
651 SourceLocation IncludeLoc = SourceLocation()) {
652 return createFileID(createMemBufferContentCache(Buffer), IncludeLoc,
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000653 SrcMgr::C_User, LoadedID, LoadedOffset);
Ted Kremenek1036b682007-12-19 23:48:45 +0000654 }
Chris Lattner06a062d2009-01-19 08:02:45 +0000655
Chandler Carruthbf340e42011-07-26 03:03:05 +0000656 /// createMacroArgExpansionLoc - Return a new SourceLocation that encodes the
657 /// fact that a token from SpellingLoc should actually be referenced from
Chandler Carruth3201f382011-07-26 05:17:23 +0000658 /// ExpansionLoc, and that it represents the expansion of a macro argument
659 /// into the function-like macro body.
Chandler Carruthbf340e42011-07-26 03:03:05 +0000660 SourceLocation createMacroArgExpansionLoc(SourceLocation Loc,
661 SourceLocation ExpansionLoc,
662 unsigned TokLength);
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000663
Chandler Carruthbf340e42011-07-26 03:03:05 +0000664 /// createExpansionLoc - Return a new SourceLocation that encodes the fact
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000665 /// that a token from SpellingLoc should actually be referenced from
Chandler Carruthbf340e42011-07-26 03:03:05 +0000666 /// ExpansionLoc.
667 SourceLocation createExpansionLoc(SourceLocation Loc,
668 SourceLocation ExpansionLocStart,
669 SourceLocation ExpansionLocEnd,
670 unsigned TokLength,
671 int LoadedID = 0,
672 unsigned LoadedOffset = 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000673
Douglas Gregor29684422009-12-02 06:49:09 +0000674 /// \brief Retrieve the memory buffer associated with the given file.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000675 ///
676 /// \param Invalid If non-NULL, will be set \c true if an error
677 /// occurs while retrieving the memory buffer.
678 const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
679 bool *Invalid = 0);
Douglas Gregor29684422009-12-02 06:49:09 +0000680
681 /// \brief Override the contents of the given source file by providing an
682 /// already-allocated buffer.
683 ///
Dan Gohmanafbf5f82010-08-26 02:27:03 +0000684 /// \param SourceFile the source file whose contents will be overriden.
Douglas Gregor29684422009-12-02 06:49:09 +0000685 ///
686 /// \param Buffer the memory buffer whose contents will be used as the
687 /// data in the given source file.
688 ///
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000689 /// \param DoNotFree If true, then the buffer will not be freed when the
690 /// source manager is destroyed.
Dan Gohman0d06e992010-10-26 20:47:28 +0000691 void overrideFileContents(const FileEntry *SourceFile,
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000692 const llvm::MemoryBuffer *Buffer,
693 bool DoNotFree = false);
Douglas Gregor29684422009-12-02 06:49:09 +0000694
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000695 /// \brief Override the the given source file with another one.
696 ///
697 /// \param SourceFile the source file which will be overriden.
698 ///
699 /// \param NewFile the file whose contents will be used as the
700 /// data instead of the contents of the given source file.
701 void overrideFileContents(const FileEntry *SourceFile,
702 const FileEntry *NewFile);
703
Chris Lattner06a062d2009-01-19 08:02:45 +0000704 //===--------------------------------------------------------------------===//
705 // FileID manipulation methods.
706 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000707
Daniel Dunbar2ffb14f2009-12-06 09:19:25 +0000708 /// getBuffer - Return the buffer for the specified FileID. If there is an
709 /// error opening this buffer the first time, this manufactures a temporary
710 /// buffer and returns a non-empty error string.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000711 const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
712 bool *Invalid = 0) const {
Douglas Gregore23ac652011-04-20 00:21:03 +0000713 bool MyInvalid = false;
714 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
715 if (MyInvalid || !Entry.isFile()) {
716 if (Invalid)
717 *Invalid = true;
Eric Christopher5330ee02011-09-08 23:28:19 +0000718
Douglas Gregore23ac652011-04-20 00:21:03 +0000719 return getFakeBufferForRecovery();
720 }
Eric Christopher5330ee02011-09-08 23:28:19 +0000721
722 return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
Douglas Gregore23ac652011-04-20 00:21:03 +0000723 Invalid);
Chris Lattner06a062d2009-01-19 08:02:45 +0000724 }
Mike Stump1eb44332009-09-09 15:08:12 +0000725
Chris Lattnere127a0d2010-04-20 20:35:58 +0000726 const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
Douglas Gregore23ac652011-04-20 00:21:03 +0000727 bool MyInvalid = false;
728 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
729 if (MyInvalid || !Entry.isFile()) {
730 if (Invalid)
731 *Invalid = true;
Eric Christopher5330ee02011-09-08 23:28:19 +0000732
Douglas Gregore23ac652011-04-20 00:21:03 +0000733 return getFakeBufferForRecovery();
734 }
735
Eric Christopher5330ee02011-09-08 23:28:19 +0000736 return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
737 SourceLocation(),
Douglas Gregore23ac652011-04-20 00:21:03 +0000738 Invalid);
Chris Lattnere127a0d2010-04-20 20:35:58 +0000739 }
Eric Christopher5330ee02011-09-08 23:28:19 +0000740
Chris Lattner06a062d2009-01-19 08:02:45 +0000741 /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
742 const FileEntry *getFileEntryForID(FileID FID) const {
Douglas Gregore23ac652011-04-20 00:21:03 +0000743 bool MyInvalid = false;
744 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
745 if (MyInvalid || !Entry.isFile())
746 return 0;
Eric Christopher5330ee02011-09-08 23:28:19 +0000747
Douglas Gregore23ac652011-04-20 00:21:03 +0000748 return Entry.getFile().getContentCache()->OrigEntry;
Chris Lattner06a062d2009-01-19 08:02:45 +0000749 }
Mike Stump1eb44332009-09-09 15:08:12 +0000750
Ted Kremenek9d5a1652011-03-23 02:16:44 +0000751 /// Returns the FileEntry record for the provided SLocEntry.
752 const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
753 {
754 return sloc.getFile().getContentCache()->OrigEntry;
755 }
756
Benjamin Kramerceafc4b2010-03-16 14:48:07 +0000757 /// getBufferData - Return a StringRef to the source buffer data for the
758 /// specified FileID.
759 ///
Douglas Gregorf715ca12010-03-16 00:06:06 +0000760 /// \param FID The file ID whose contents will be returned.
761 /// \param Invalid If non-NULL, will be set true if an error occurred.
Chris Lattner686775d2011-07-20 06:58:45 +0000762 StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +0000763
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000764 /// \brief Get the number of FileIDs (files and macros) that were created
Matt Beaumont-Gay2c3c7672011-12-09 23:16:01 +0000765 /// during preprocessing of \p FID, including it.
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000766 unsigned getNumCreatedFIDsForFileID(FileID FID) const {
767 bool Invalid = false;
768 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
769 if (Invalid || !Entry.isFile())
770 return 0;
771
772 return Entry.getFile().NumCreatedFIDs;
773 }
774
775 /// \brief Set the number of FileIDs (files and macros) that were created
Matt Beaumont-Gay2c3c7672011-12-09 23:16:01 +0000776 /// during preprocessing of \p FID, including it.
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000777 void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs) const {
778 bool Invalid = false;
779 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
780 if (Invalid || !Entry.isFile())
781 return;
782
783 assert(Entry.getFile().NumCreatedFIDs == 0 && "Already set!");
784 const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs;
785 }
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Chris Lattner06a062d2009-01-19 08:02:45 +0000787 //===--------------------------------------------------------------------===//
788 // SourceLocation manipulation methods.
789 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000790
Chris Lattner668ab1a2009-03-13 01:05:57 +0000791 /// getFileID - Return the FileID for a SourceLocation. This is a very
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000792 /// hot method that is used for all SourceManager queries that start with a
793 /// SourceLocation object. It is responsible for finding the entry in
794 /// SLocEntryTable which contains the specified location.
795 ///
796 FileID getFileID(SourceLocation SpellingLoc) const {
797 unsigned SLocOffset = SpellingLoc.getOffset();
Mike Stump1eb44332009-09-09 15:08:12 +0000798
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000799 // If our one-entry cache covers this offset, just return it.
800 if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
801 return LastFileIDLookup;
802
803 return getFileIDSlow(SLocOffset);
804 }
Mike Stump1eb44332009-09-09 15:08:12 +0000805
Chris Lattner2b2453a2009-01-17 06:22:33 +0000806 /// getLocForStartOfFile - Return the source location corresponding to the
807 /// first byte of the specified file.
808 SourceLocation getLocForStartOfFile(FileID FID) const {
Douglas Gregore23ac652011-04-20 00:21:03 +0000809 bool Invalid = false;
810 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
811 if (Invalid || !Entry.isFile())
812 return SourceLocation();
Eric Christopher5330ee02011-09-08 23:28:19 +0000813
Douglas Gregore23ac652011-04-20 00:21:03 +0000814 unsigned FileOffset = Entry.getOffset();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000815 return SourceLocation::getFileLoc(FileOffset);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000816 }
Argyrios Kyrtzidisf226ff92011-10-25 00:29:50 +0000817
818 /// \brief Return the source location corresponding to the last byte of the
819 /// specified file.
820 SourceLocation getLocForEndOfFile(FileID FID) const {
821 bool Invalid = false;
822 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
823 if (Invalid || !Entry.isFile())
824 return SourceLocation();
825
826 unsigned FileOffset = Entry.getOffset();
827 return SourceLocation::getFileLoc(FileOffset + getFileIDSize(FID) - 1);
828 }
Mike Stump1eb44332009-09-09 15:08:12 +0000829
Matt Beaumont-Gay2c3c7672011-12-09 23:16:01 +0000830 /// \brief Returns the include location if \p FID is a #include'd file
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000831 /// otherwise it returns an invalid location.
832 SourceLocation getIncludeLoc(FileID FID) const {
833 bool Invalid = false;
834 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
835 if (Invalid || !Entry.isFile())
836 return SourceLocation();
Eric Christopher5330ee02011-09-08 23:28:19 +0000837
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000838 return Entry.getFile().getIncludeLoc();
839 }
840
Chandler Carruth40278532011-07-25 16:49:02 +0000841 /// getExpansionLoc - Given a SourceLocation object, return the expansion
842 /// location referenced by the ID.
843 SourceLocation getExpansionLoc(SourceLocation Loc) const {
Chris Lattneraddb7972009-01-26 20:04:19 +0000844 // Handle the non-mapped case inline, defer to out of line code to handle
Chandler Carruth40278532011-07-25 16:49:02 +0000845 // expansions.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000846 if (Loc.isFileID()) return Loc;
Chandler Carruthf84ef952011-07-25 20:52:26 +0000847 return getExpansionLocSlowCase(Loc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000848 }
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Matt Beaumont-Gay2c3c7672011-12-09 23:16:01 +0000850 /// \brief Given \p Loc, if it is a macro location return the expansion
Argyrios Kyrtzidis796dbfb2011-10-12 07:07:40 +0000851 /// location or the spelling location, depending on if it comes from a
852 /// macro argument or not.
853 SourceLocation getFileLoc(SourceLocation Loc) const {
854 if (Loc.isFileID()) return Loc;
855 return getFileLocSlowCase(Loc);
856 }
857
Chandler Carruth999f7392011-07-25 20:52:21 +0000858 /// getImmediateExpansionRange - Loc is required to be an expansion location.
859 /// Return the start/end of the expansion information.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000860 std::pair<SourceLocation,SourceLocation>
Chandler Carruth999f7392011-07-25 20:52:21 +0000861 getImmediateExpansionRange(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000862
Chandler Carruthedc3dcc2011-07-25 16:56:02 +0000863 /// getExpansionRange - Given a SourceLocation object, return the range of
864 /// tokens covered by the expansion the ultimate file.
Chris Lattner66781332009-02-15 21:26:50 +0000865 std::pair<SourceLocation,SourceLocation>
Chandler Carruthedc3dcc2011-07-25 16:56:02 +0000866 getExpansionRange(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000867
868
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000869 /// getSpellingLoc - Given a SourceLocation object, return the spelling
870 /// location referenced by the ID. This is the place where the characters
871 /// that make up the lexed token can be found.
872 SourceLocation getSpellingLoc(SourceLocation Loc) const {
Chris Lattneraddb7972009-01-26 20:04:19 +0000873 // Handle the non-mapped case inline, defer to out of line code to handle
Chandler Carruth3201f382011-07-26 05:17:23 +0000874 // expansions.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000875 if (Loc.isFileID()) return Loc;
Chris Lattneraddb7972009-01-26 20:04:19 +0000876 return getSpellingLocSlowCase(Loc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000877 }
Mike Stump1eb44332009-09-09 15:08:12 +0000878
Chris Lattner387616e2009-02-17 08:04:48 +0000879 /// getImmediateSpellingLoc - Given a SourceLocation object, return the
880 /// spelling location referenced by the ID. This is the first level down
881 /// towards the place where the characters that make up the lexed token can be
882 /// found. This should not generally be used by clients.
Mike Stump1eb44332009-09-09 15:08:12 +0000883 SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000884
885 /// getDecomposedLoc - Decompose the specified location into a raw FileID +
886 /// Offset pair. The first element is the FileID, the second is the
887 /// offset from the start of the buffer of the location.
888 std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
889 FileID FID = getFileID(Loc);
Argyrios Kyrtzidisa246d272011-11-04 23:43:06 +0000890 bool Invalid = false;
891 const SrcMgr::SLocEntry &E = getSLocEntry(FID, &Invalid);
892 if (Invalid)
893 return std::make_pair(FileID(), 0);
894 return std::make_pair(FID, Loc.getOffset()-E.getOffset());
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000895 }
Mike Stump1eb44332009-09-09 15:08:12 +0000896
Chandler Carruth3201f382011-07-26 05:17:23 +0000897 /// getDecomposedExpansionLoc - Decompose the specified location into a raw
898 /// FileID + Offset pair. If the location is an expansion record, walk
899 /// through it until we find the final location expanded.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000900 std::pair<FileID, unsigned>
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +0000901 getDecomposedExpansionLoc(SourceLocation Loc) const {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000902 FileID FID = getFileID(Loc);
Argyrios Kyrtzidisa246d272011-11-04 23:43:06 +0000903 bool Invalid = false;
904 const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
905 if (Invalid)
906 return std::make_pair(FileID(), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000907
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000908 unsigned Offset = Loc.getOffset()-E->getOffset();
909 if (Loc.isFileID())
910 return std::make_pair(FID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000911
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +0000912 return getDecomposedExpansionLocSlowCase(E);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000913 }
914
915 /// getDecomposedSpellingLoc - Decompose the specified location into a raw
Chandler Carruth3201f382011-07-26 05:17:23 +0000916 /// FileID + Offset pair. If the location is an expansion record, walk
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000917 /// through it until we find its spelling record.
918 std::pair<FileID, unsigned>
919 getDecomposedSpellingLoc(SourceLocation Loc) const {
920 FileID FID = getFileID(Loc);
Argyrios Kyrtzidisa246d272011-11-04 23:43:06 +0000921 bool Invalid = false;
922 const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
923 if (Invalid)
924 return std::make_pair(FileID(), 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000925
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000926 unsigned Offset = Loc.getOffset()-E->getOffset();
927 if (Loc.isFileID())
928 return std::make_pair(FID, Offset);
929 return getDecomposedSpellingLocSlowCase(E, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000930 }
931
Chris Lattner52c29082009-01-27 06:27:13 +0000932 /// getFileOffset - This method returns the offset from the start
933 /// of the file that the specified SourceLocation represents. This is not very
934 /// meaningful for a macro ID.
935 unsigned getFileOffset(SourceLocation SpellingLoc) const {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000936 return getDecomposedLoc(SpellingLoc).second;
937 }
Mike Stump1eb44332009-09-09 15:08:12 +0000938
Chandler Carruth96d35892011-07-26 03:03:00 +0000939 /// isMacroArgExpansion - This method tests whether the given source location
940 /// represents a macro argument's expansion into the function-like macro
941 /// definition. Such source locations only appear inside of the expansion
942 /// locations representing where a particular function-like macro was
943 /// expanded.
944 bool isMacroArgExpansion(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000945
Matt Beaumont-Gay2c3c7672011-12-09 23:16:01 +0000946 /// \brief Returns true if \p Loc is inside the [\p Start, +\p Length)
Argyrios Kyrtzidis499ea552011-08-23 21:02:38 +0000947 /// chunk of the source location address space.
Matt Beaumont-Gay2c3c7672011-12-09 23:16:01 +0000948 /// If it's true and \p RelativeOffset is non-null, it will be set to the
949 /// relative offset of \p Loc inside the chunk.
Argyrios Kyrtzidis499ea552011-08-23 21:02:38 +0000950 bool isInSLocAddrSpace(SourceLocation Loc,
951 SourceLocation Start, unsigned Length,
952 unsigned *RelativeOffset = 0) const {
953 assert(((Start.getOffset() < NextLocalOffset &&
954 Start.getOffset()+Length <= NextLocalOffset) ||
955 (Start.getOffset() >= CurrentLoadedOffset &&
956 Start.getOffset()+Length < MaxLoadedOffset)) &&
957 "Chunk is not valid SLoc address space");
958 unsigned LocOffs = Loc.getOffset();
959 unsigned BeginOffs = Start.getOffset();
960 unsigned EndOffs = BeginOffs + Length;
961 if (LocOffs >= BeginOffs && LocOffs < EndOffs) {
962 if (RelativeOffset)
963 *RelativeOffset = LocOffs - BeginOffs;
964 return true;
965 }
966
967 return false;
968 }
969
Matt Beaumont-Gay2c3c7672011-12-09 23:16:01 +0000970 /// \brief Return true if both \p LHS and \p RHS are in the local source
971 /// location address space or the loaded one. If it's true and \p
972 /// RelativeOffset is non-null, it will be set to the offset of \p RHS
973 /// relative to \p LHS.
Argyrios Kyrtzidisb6c465e2011-08-23 21:02:41 +0000974 bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS,
975 int *RelativeOffset) const {
976 unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset();
977 bool LHSLoaded = LHSOffs >= CurrentLoadedOffset;
978 bool RHSLoaded = RHSOffs >= CurrentLoadedOffset;
979
980 if (LHSLoaded == RHSLoaded) {
981 if (RelativeOffset)
982 *RelativeOffset = RHSOffs - LHSOffs;
983 return true;
984 }
985
986 return false;
987 }
988
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000989 //===--------------------------------------------------------------------===//
990 // Queries about the code at a SourceLocation.
991 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000992
Reid Spencer5f016e22007-07-11 17:01:13 +0000993 /// getCharacterData - Return a pointer to the start of the specified location
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000994 /// in the appropriate spelling MemoryBuffer.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000995 ///
996 /// \param Invalid If non-NULL, will be set \c true if an error occurs.
997 const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000998
Chris Lattner9dc1f532007-07-20 16:37:10 +0000999 /// getColumnNumber - Return the column # for the specified file position.
1000 /// This is significantly cheaper to compute than the line number. This
Chandler Carruth3201f382011-07-26 05:17:23 +00001001 /// returns zero if the column number isn't known. This may only be called
1002 /// on a file sloc, so you must choose a spelling or expansion location
Chris Lattnerf7cf85b2009-01-16 07:36:28 +00001003 /// before calling this method.
Eric Christopher5330ee02011-09-08 23:28:19 +00001004 unsigned getColumnNumber(FileID FID, unsigned FilePos,
Douglas Gregor50f6af72010-03-16 05:20:39 +00001005 bool *Invalid = 0) const;
Chandler Carruth5ef04ee2011-02-23 00:47:48 +00001006 unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
Chandler Carrutha77c0312011-07-25 20:57:57 +00001007 unsigned getExpansionColumnNumber(SourceLocation Loc,
Chandler Carruthb49dcd22011-07-25 20:59:15 +00001008 bool *Invalid = 0) const;
Chandler Carruth5ef04ee2011-02-23 00:47:48 +00001009 unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +00001010
1011
Chris Lattnerdf7c17a2009-01-16 07:00:02 +00001012 /// getLineNumber - Given a SourceLocation, return the spelling line number
Reid Spencer5f016e22007-07-11 17:01:13 +00001013 /// for the position indicated. This requires building and caching a table of
1014 /// line offsets for the MemoryBuffer, so this is not cheap: use only when
1015 /// about to emit a diagnostic.
Douglas Gregor50f6af72010-03-16 05:20:39 +00001016 unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
Chandler Carruth5ef04ee2011-02-23 00:47:48 +00001017 unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
Chandler Carruth64211622011-07-25 21:09:52 +00001018 unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
Chandler Carruth5ef04ee2011-02-23 00:47:48 +00001019 unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +00001020
Chris Lattnerbff5c512009-02-17 08:39:06 +00001021 /// Return the filename or buffer identifier of the buffer the location is in.
1022 /// Note that this name does not respect #line directives. Use getPresumedLoc
1023 /// for normal clients.
Douglas Gregor50f6af72010-03-16 05:20:39 +00001024 const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +00001025
Chris Lattner6b306672009-02-04 05:33:01 +00001026 /// getFileCharacteristic - return the file characteristic of the specified
Mike Stump1eb44332009-09-09 15:08:12 +00001027 /// source location, indicating whether this is a normal file, a system
Chris Lattner6b306672009-02-04 05:33:01 +00001028 /// header, or an "implicit extern C" system header.
1029 ///
1030 /// This state can be modified with flags on GNU linemarker directives like:
1031 /// # 4 "foo.h" 3
1032 /// which changes all source locations in the current file after that to be
1033 /// considered to be from a system header.
1034 SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +00001035
Chris Lattnerb9c3f962009-01-27 07:57:44 +00001036 /// getPresumedLoc - This method returns the "presumed" location of a
1037 /// SourceLocation specifies. A "presumed location" can be modified by #line
1038 /// or GNU line marker directives. This provides a view on the data that a
1039 /// user should see in diagnostics, for example.
1040 ///
Chandler Carruth3201f382011-07-26 05:17:23 +00001041 /// Note that a presumed location is always given as the expansion point of
1042 /// an expansion location, not at the spelling location.
Douglas Gregorcb7b1e12010-11-12 07:15:47 +00001043 ///
1044 /// \returns The presumed location of the specified SourceLocation. If the
1045 /// presumed location cannot be calculate (e.g., because \p Loc is invalid
1046 /// or the file containing \p Loc has changed on disk), returns an invalid
1047 /// presumed location.
Chris Lattnerb9c3f962009-01-27 07:57:44 +00001048 PresumedLoc getPresumedLoc(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +00001049
Ted Kremenek9fd87b12008-04-14 21:04:18 +00001050 /// isFromSameFile - Returns true if both SourceLocations correspond to
1051 /// the same file.
1052 bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
Chris Lattnera11d6172009-01-19 07:46:45 +00001053 return getFileID(Loc1) == getFileID(Loc2);
Ted Kremenek9fd87b12008-04-14 21:04:18 +00001054 }
Mike Stump1eb44332009-09-09 15:08:12 +00001055
Ted Kremenek9fd87b12008-04-14 21:04:18 +00001056 /// isFromMainFile - Returns true if the file of provided SourceLocation is
1057 /// the main file.
1058 bool isFromMainFile(SourceLocation Loc) const {
Chris Lattnera11d6172009-01-19 07:46:45 +00001059 return getFileID(Loc) == getMainFileID();
Mike Stump1eb44332009-09-09 15:08:12 +00001060 }
1061
Nico Weber7bfaaae2008-08-10 19:59:06 +00001062 /// isInSystemHeader - Returns if a SourceLocation is in a system header.
1063 bool isInSystemHeader(SourceLocation Loc) const {
Chris Lattner0b9e7362008-09-26 21:18:42 +00001064 return getFileCharacteristic(Loc) != SrcMgr::C_User;
Nico Weber7bfaaae2008-08-10 19:59:06 +00001065 }
Mike Stump1eb44332009-09-09 15:08:12 +00001066
Chris Lattner0d456582009-06-13 23:31:51 +00001067 /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
1068 /// system header.
1069 bool isInExternCSystemHeader(SourceLocation Loc) const {
1070 return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
1071 }
Mike Stump1eb44332009-09-09 15:08:12 +00001072
Matt Beaumont-Gay2c3c7672011-12-09 23:16:01 +00001073 /// \brief The size of the SLocEnty that \p FID represents.
Argyrios Kyrtzidis984e42c2011-08-23 21:02:28 +00001074 unsigned getFileIDSize(FileID FID) const;
Argyrios Kyrtzidis54232ad2011-08-19 22:34:01 +00001075
Matt Beaumont-Gay2c3c7672011-12-09 23:16:01 +00001076 /// \brief Given a specific FileID, returns true if \p Loc is inside that
1077 /// FileID chunk and sets relative offset (offset of \p Loc from beginning
1078 /// of FileID) to \p relativeOffset.
Argyrios Kyrtzidisd60a34a2011-08-19 22:34:17 +00001079 bool isInFileID(SourceLocation Loc, FileID FID,
1080 unsigned *RelativeOffset = 0) const {
Argyrios Kyrtzidisd7cb46c2011-08-23 21:02:45 +00001081 unsigned Offs = Loc.getOffset();
1082 if (isOffsetInFileID(FID, Offs)) {
1083 if (RelativeOffset)
1084 *RelativeOffset = Offs - getSLocEntry(FID).getOffset();
1085 return true;
1086 }
Argyrios Kyrtzidisd60a34a2011-08-19 22:34:17 +00001087
Argyrios Kyrtzidisd7cb46c2011-08-23 21:02:45 +00001088 return false;
1089 }
Argyrios Kyrtzidis469244a2011-05-28 03:56:11 +00001090
Chris Lattner06a062d2009-01-19 08:02:45 +00001091 //===--------------------------------------------------------------------===//
Chris Lattner5b9a5042009-01-26 07:57:50 +00001092 // Line Table Manipulation Routines
1093 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +00001094
Chris Lattner5b9a5042009-01-26 07:57:50 +00001095 /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
Mike Stump1eb44332009-09-09 15:08:12 +00001096 ///
Chris Lattner686775d2011-07-20 06:58:45 +00001097 unsigned getLineTableFilenameID(StringRef Str);
Mike Stump1eb44332009-09-09 15:08:12 +00001098
Chris Lattner4c4ea172009-02-03 21:52:55 +00001099 /// AddLineNote - Add a line note to the line table for the FileID and offset
1100 /// specified by Loc. If FilenameID is -1, it is considered to be
1101 /// unspecified.
1102 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
Chris Lattner9d79eba2009-02-04 05:21:58 +00001103 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
Mike Stump1eb44332009-09-09 15:08:12 +00001104 bool IsFileEntry, bool IsFileExit,
Chris Lattner9d79eba2009-02-04 05:21:58 +00001105 bool IsSystemHeader, bool IsExternCHeader);
Douglas Gregorbd945002009-04-13 16:31:14 +00001106
1107 /// \brief Determine if the source manager has a line table.
1108 bool hasLineTable() const { return LineTable != 0; }
1109
1110 /// \brief Retrieve the stored line table.
1111 LineTableInfo &getLineTable();
1112
Chris Lattner5b9a5042009-01-26 07:57:50 +00001113 //===--------------------------------------------------------------------===//
Ted Kremenek457aaf02011-04-28 04:10:31 +00001114 // Queries for performance analysis.
1115 //===--------------------------------------------------------------------===//
1116
1117 /// Return the total amount of physical memory allocated by the
1118 /// ContentCache allocator.
1119 size_t getContentCacheSize() const {
1120 return ContentCacheAlloc.getTotalMemory();
1121 }
Eric Christopher5330ee02011-09-08 23:28:19 +00001122
Ted Kremenekf61b8312011-04-28 20:36:42 +00001123 struct MemoryBufferSizes {
1124 const size_t malloc_bytes;
1125 const size_t mmap_bytes;
Eric Christopher5330ee02011-09-08 23:28:19 +00001126
Ted Kremenekf61b8312011-04-28 20:36:42 +00001127 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
1128 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
1129 };
1130
1131 /// Return the amount of memory used by memory buffers, breaking down
1132 /// by heap-backed versus mmap'ed memory.
1133 MemoryBufferSizes getMemoryBufferSizes() const;
Eric Christopher5330ee02011-09-08 23:28:19 +00001134
Ted Kremenekca7dc2b2011-07-26 23:46:06 +00001135 // Return the amount of memory used for various side tables and
1136 // data structures in the SourceManager.
1137 size_t getDataStructureSizes() const;
Ted Kremenek457aaf02011-04-28 04:10:31 +00001138
1139 //===--------------------------------------------------------------------===//
Chris Lattner06a062d2009-01-19 08:02:45 +00001140 // Other miscellaneous methods.
1141 //===--------------------------------------------------------------------===//
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +00001142
1143 /// \brief Get the source location for the given file:line:col triplet.
1144 ///
1145 /// If the source file is included multiple times, the source location will
1146 /// be based upon the first inclusion.
Argyrios Kyrtzidisac836e42011-08-17 00:31:20 +00001147 SourceLocation translateFileLineCol(const FileEntry *SourceFile,
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00001148 unsigned Line, unsigned Col) const;
Argyrios Kyrtzidisac836e42011-08-17 00:31:20 +00001149
Argyrios Kyrtzidisb201e162011-09-27 17:22:25 +00001150 /// \brief Get the FileID for the given file.
1151 ///
1152 /// If the source file is included multiple times, the FileID will be the
1153 /// first inclusion.
1154 FileID translateFile(const FileEntry *SourceFile) const;
1155
Matt Beaumont-Gay2c3c7672011-12-09 23:16:01 +00001156 /// \brief Get the source location in \p FID for the given line:col.
1157 /// Returns null location if \p FID is not a file SLocEntry.
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00001158 SourceLocation translateLineCol(FileID FID,
1159 unsigned Line, unsigned Col) const;
Argyrios Kyrtzidisefa2ff82011-09-19 20:40:29 +00001160
Matt Beaumont-Gay2c3c7672011-12-09 23:16:01 +00001161 /// \brief If \p Loc points inside a function macro argument, the returned
Argyrios Kyrtzidisac836e42011-08-17 00:31:20 +00001162 /// location will be the macro location in which the argument was expanded.
1163 /// If a macro argument is used multiple times, the expanded location will
1164 /// be at the first expansion of the argument.
1165 /// e.g.
1166 /// MY_MACRO(foo);
1167 /// ^
1168 /// Passing a file location pointing at 'foo', will yield a macro location
1169 /// where 'foo' was expanded into.
Argyrios Kyrtzidis507097e2011-09-19 20:40:35 +00001170 SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +00001171
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001172 /// \brief Determines the order of 2 source locations in the translation unit.
1173 ///
1174 /// \returns true if LHS source location comes before RHS, false otherwise.
1175 bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
1176
Argyrios Kyrtzidisaec230d2011-09-01 20:53:18 +00001177 /// \brief Comparison function class.
1178 class LocBeforeThanCompare : public std::binary_function<SourceLocation,
1179 SourceLocation, bool> {
1180 SourceManager &SM;
1181
1182 public:
1183 explicit LocBeforeThanCompare(SourceManager &SM) : SM(SM) { }
1184
1185 bool operator()(SourceLocation LHS, SourceLocation RHS) const {
1186 return SM.isBeforeInTranslationUnit(LHS, RHS);
1187 }
1188 };
1189
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +00001190 /// \brief Determines the order of 2 source locations in the "source location
1191 /// address space".
Argyrios Kyrtzidis5d579e72011-08-23 21:02:35 +00001192 bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const {
1193 return isBeforeInSLocAddrSpace(LHS, RHS.getOffset());
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +00001194 }
1195
1196 /// \brief Determines the order of a source location and a source location
1197 /// offset in the "source location address space".
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001198 ///
Eric Christopher5330ee02011-09-08 23:28:19 +00001199 /// Note that we always consider source locations loaded from
Argyrios Kyrtzidis5d579e72011-08-23 21:02:35 +00001200 bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001201 unsigned LHSOffset = LHS.getOffset();
1202 bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1203 bool RHSLoaded = RHS >= CurrentLoadedOffset;
1204 if (LHSLoaded == RHSLoaded)
Argyrios Kyrtzidis5d579e72011-08-23 21:02:35 +00001205 return LHSOffset < RHS;
Eric Christopher5330ee02011-09-08 23:28:19 +00001206
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001207 return LHSLoaded;
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +00001208 }
1209
Chris Lattnerc6fe32a2009-01-17 03:48:08 +00001210 // Iterators over FileInfos.
Chris Lattner0d0bf8c2009-02-03 07:30:45 +00001211 typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
1212 ::const_iterator fileinfo_iterator;
Chris Lattnerc6fe32a2009-01-17 03:48:08 +00001213 fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
1214 fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
Douglas Gregord93256e2010-01-28 06:00:51 +00001215 bool hasFileInfo(const FileEntry *File) const {
1216 return FileInfos.find(File) != FileInfos.end();
1217 }
Chris Lattnerc6fe32a2009-01-17 03:48:08 +00001218
Reid Spencer5f016e22007-07-11 17:01:13 +00001219 /// PrintStats - Print statistics to stderr.
1220 ///
1221 void PrintStats() const;
Reid Spencer5f016e22007-07-11 17:01:13 +00001222
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001223 /// \brief Get the number of local SLocEntries we have.
1224 unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
Eric Christopher5330ee02011-09-08 23:28:19 +00001225
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001226 /// \brief Get a local SLocEntry. This is exposed for indexing.
Eric Christopher5330ee02011-09-08 23:28:19 +00001227 const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001228 bool *Invalid = 0) const {
1229 assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1230 return LocalSLocEntryTable[Index];
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001231 }
Eric Christopher5330ee02011-09-08 23:28:19 +00001232
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001233 /// \brief Get the number of loaded SLocEntries we have.
1234 unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
Eric Christopher5330ee02011-09-08 23:28:19 +00001235
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001236 /// \brief Get a loaded SLocEntry. This is exposed for indexing.
David Blaikie70042f52011-10-20 01:45:20 +00001237 const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index,
1238 bool *Invalid = 0) const {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001239 assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1240 if (!SLocEntryLoaded[Index])
1241 ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2));
1242 return LoadedSLocEntryTable[Index];
1243 }
Eric Christopher5330ee02011-09-08 23:28:19 +00001244
Douglas Gregore23ac652011-04-20 00:21:03 +00001245 const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
Argyrios Kyrtzidisc705d252011-10-18 21:59:54 +00001246 if (FID.ID == 0 || FID.ID == -1) {
1247 if (Invalid) *Invalid = true;
1248 return LocalSLocEntryTable[0];
1249 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001250 return getSLocEntryByID(FID.ID);
Douglas Gregorbd945002009-04-13 16:31:14 +00001251 }
1252
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001253 unsigned getNextLocalOffset() const { return NextLocalOffset; }
Eric Christopher5330ee02011-09-08 23:28:19 +00001254
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001255 void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1256 assert(LoadedSLocEntryTable.empty() &&
1257 "Invalidating existing loaded entries");
1258 ExternalSLocEntries = Source;
1259 }
Eric Christopher5330ee02011-09-08 23:28:19 +00001260
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001261 /// \brief Allocate a number of loaded SLocEntries, which will be actually
1262 /// loaded on demand from the external source.
1263 ///
1264 /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1265 /// in the global source view. The lowest ID and the base offset of the
1266 /// entries will be returned.
1267 std::pair<int, unsigned>
1268 AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
Eric Christopher5330ee02011-09-08 23:28:19 +00001269
Matt Beaumont-Gay2c3c7672011-12-09 23:16:01 +00001270 /// \brief Returns true if \p Loc came from a PCH/Module.
Argyrios Kyrtzidisaa6edae2011-09-19 20:40:05 +00001271 bool isLoadedSourceLocation(SourceLocation Loc) const {
1272 return Loc.getOffset() >= CurrentLoadedOffset;
1273 }
1274
Matt Beaumont-Gay2c3c7672011-12-09 23:16:01 +00001275 /// \brief Returns true if \p Loc did not come from a PCH/Module.
Argyrios Kyrtzidisaa6edae2011-09-19 20:40:05 +00001276 bool isLocalSourceLocation(SourceLocation Loc) const {
1277 return Loc.getOffset() < NextLocalOffset;
1278 }
1279
Matt Beaumont-Gay2c3c7672011-12-09 23:16:01 +00001280 /// \brief Returns true if \p FID came from a PCH/Module.
Argyrios Kyrtzidis71869912011-10-31 07:20:03 +00001281 bool isLoadedFileID(FileID FID) const {
1282 assert(FID.ID != -1 && "Using FileID sentinel value");
1283 return FID.ID < 0;
1284 }
1285
Matt Beaumont-Gay2c3c7672011-12-09 23:16:01 +00001286 /// \brief Returns true if \p FID did not come from a PCH/Module.
Argyrios Kyrtzidis71869912011-10-31 07:20:03 +00001287 bool isLocalFileID(FileID FID) const {
1288 return !isLoadedFileID(FID);
1289 }
1290
Ted Kremenek78d85f52007-10-30 21:08:08 +00001291private:
Douglas Gregore23ac652011-04-20 00:21:03 +00001292 const llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1293
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001294 /// \brief Get the entry with the given unwrapped FileID.
1295 const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const {
1296 assert(ID != -1 && "Using FileID sentinel value");
1297 if (ID < 0)
1298 return getLoadedSLocEntryByID(ID);
1299 return getLocalSLocEntry(static_cast<unsigned>(ID));
1300 }
Eric Christopher5330ee02011-09-08 23:28:19 +00001301
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001302 const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID) const {
1303 return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2));
1304 }
Eric Christopher5330ee02011-09-08 23:28:19 +00001305
Chandler Carruthbf340e42011-07-26 03:03:05 +00001306 /// createExpansionLoc - Implements the common elements of storing an
Chandler Carruth3201f382011-07-26 05:17:23 +00001307 /// expansion info struct into the SLocEntry table and producing a source
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +00001308 /// location that refers to it.
Chandler Carruth78df8362011-07-26 04:41:47 +00001309 SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
Chandler Carruthbf340e42011-07-26 03:03:05 +00001310 unsigned TokLength,
1311 int LoadedID = 0,
1312 unsigned LoadedOffset = 0);
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +00001313
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001314 /// isOffsetInFileID - Return true if the specified FileID contains the
1315 /// specified SourceLocation offset. This is a very hot method.
1316 inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1317 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1318 // If the entry is after the offset, it can't contain it.
1319 if (SLocOffset < Entry.getOffset()) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001320
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001321 // If this is the very last entry then it does.
1322 if (FID.ID == -2)
1323 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001324
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001325 // If it is the last local entry, then it does if the location is local.
1326 if (static_cast<unsigned>(FID.ID+1) == LocalSLocEntryTable.size()) {
1327 return SLocOffset < NextLocalOffset;
1328 }
1329
1330 // Otherwise, the entry after it has to not include it. This works for both
1331 // local and loaded entries.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001332 return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001333 }
Mike Stump1eb44332009-09-09 15:08:12 +00001334
Ted Kremenek78d85f52007-10-30 21:08:08 +00001335 /// createFileID - Create a new fileID for the specified ContentCache and
1336 /// include position. This works regardless of whether the ContentCache
1337 /// corresponds to a file or some other input source.
Chris Lattner2b2453a2009-01-17 06:22:33 +00001338 FileID createFileID(const SrcMgr::ContentCache* File,
1339 SourceLocation IncludePos,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001340 SrcMgr::CharacteristicKind DirCharacter,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001341 int LoadedID, unsigned LoadedOffset);
Mike Stump1eb44332009-09-09 15:08:12 +00001342
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001343 const SrcMgr::ContentCache *
1344 getOrCreateContentCache(const FileEntry *SourceFile);
Ted Kremenekc16c2082009-01-06 01:55:26 +00001345
Ted Kremenek78d85f52007-10-30 21:08:08 +00001346 /// createMemBufferContentCache - Create a new ContentCache for the specified
1347 /// memory buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001348 const SrcMgr::ContentCache*
Chris Lattner2b2453a2009-01-17 06:22:33 +00001349 createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
Mike Stump1eb44332009-09-09 15:08:12 +00001350
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001351 FileID getFileIDSlow(unsigned SLocOffset) const;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001352 FileID getFileIDLocal(unsigned SLocOffset) const;
1353 FileID getFileIDLoaded(unsigned SLocOffset) const;
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001354
Chandler Carruthf84ef952011-07-25 20:52:26 +00001355 SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
Chris Lattneraddb7972009-01-26 20:04:19 +00001356 SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
Argyrios Kyrtzidis796dbfb2011-10-12 07:07:40 +00001357 SourceLocation getFileLocSlowCase(SourceLocation Loc) const;
Chris Lattneraddb7972009-01-26 20:04:19 +00001358
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001359 std::pair<FileID, unsigned>
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +00001360 getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001361 std::pair<FileID, unsigned>
1362 getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1363 unsigned Offset) const;
Argyrios Kyrtzidisfb3612e2011-09-26 08:01:50 +00001364 void computeMacroArgsCache(MacroArgsMap *&MacroArgsCache, FileID FID) const;
Argyrios Kyrtzidisac1ffcc2011-09-19 20:39:54 +00001365
1366 friend class ASTReader;
1367 friend class ASTWriter;
Reid Spencer5f016e22007-07-11 17:01:13 +00001368};
1369
1370
1371} // end namespace clang
1372
1373#endif