blob: 79eba4f3537fbb44c85bf1bfc5c8ffe3a0953961 [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
Douglas Gregoraea67db2010-03-15 22:54:52 +000032class Diagnostic;
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;
Douglas Gregoraea67db2010-03-15 22:54:52 +000040
Chris Lattner0b9e7362008-09-26 21:18:42 +000041/// SrcMgr - Public enums and private classes that are part of the
42/// SourceManager implementation.
Reid Spencer5f016e22007-07-11 17:01:13 +000043///
44namespace SrcMgr {
Chris Lattner9d728512008-10-27 01:19:25 +000045 /// CharacteristicKind - This is used to represent whether a file or directory
Chris Lattner0b9e7362008-09-26 21:18:42 +000046 /// holds normal user code, system code, or system code which is implicitly
47 /// 'extern "C"' in C++ mode. Entire directories can be tagged with this
48 /// (this is maintained by DirectoryLookup and friends) as can specific
Douglas Gregorf62d43d2011-07-19 16:10:42 +000049 /// FileInfos when a #pragma system_header is seen or various other cases.
Chris Lattner0b9e7362008-09-26 21:18:42 +000050 ///
Chris Lattner9d728512008-10-27 01:19:25 +000051 enum CharacteristicKind {
Chris Lattner0b9e7362008-09-26 21:18:42 +000052 C_User, C_System, C_ExternCSystem
53 };
Mike Stump1eb44332009-09-09 15:08:12 +000054
Dan Gohman4710a8e2010-08-25 21:59:25 +000055 /// ContentCache - One instance of this struct is kept for every file
Chris Lattner06a062d2009-01-19 08:02:45 +000056 /// loaded or used. This object owns the MemoryBuffer object.
Ted Kremenekc16c2082009-01-06 01:55:26 +000057 class ContentCache {
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +000058 enum CCFlags {
59 /// \brief Whether the buffer is invalid.
60 InvalidFlag = 0x01,
61 /// \brief Whether the buffer should not be freed on destruction.
62 DoNotFreeFlag = 0x02
63 };
64
Ted Kremenekc16c2082009-01-06 01:55:26 +000065 /// Buffer - The actual buffer containing the characters from the input
66 /// file. This is owned by the ContentCache object.
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +000067 /// The bits indicate indicates whether the buffer is invalid.
68 mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
Ted Kremenekc16c2082009-01-06 01:55:26 +000069
70 public:
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +000071 /// Reference to the file entry representing this ContentCache.
72 /// This reference does not own the FileEntry object.
73 /// It is possible for this to be NULL if
Ted Kremenek78d85f52007-10-30 21:08:08 +000074 /// the ContentCache encapsulates an imaginary text buffer.
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +000075 const FileEntry *OrigEntry;
76
77 /// \brief References the file which the contents were actually loaded from.
78 /// Can be different from 'Entry' if we overridden the contents of one file
79 /// with the contents of another file.
80 const FileEntry *ContentsEntry;
Mike Stump1eb44332009-09-09 15:08:12 +000081
Chris Lattner0d0bf8c2009-02-03 07:30:45 +000082 /// SourceLineCache - A bump pointer allocated array of offsets for each
83 /// source line. This is lazily computed. This is owned by the
84 /// SourceManager BumpPointerAllocator object.
Chris Lattner05816592009-01-17 03:54:16 +000085 unsigned *SourceLineCache;
Mike Stump1eb44332009-09-09 15:08:12 +000086
Ted Kremenekb6427f82007-12-04 18:59:28 +000087 /// NumLines - The number of lines in this ContentCache. This is only valid
88 /// if SourceLineCache is non-null.
Reid Spencer5f016e22007-07-11 17:01:13 +000089 unsigned NumLines;
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +000090
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +000091 /// \brief Lazily computed map of macro argument chunks to their expanded
92 /// source location.
93 typedef std::map<unsigned, SourceLocation> MacroArgsMap;
94 MacroArgsMap *MacroArgsCache;
95
Douglas Gregor36c35ba2010-03-16 00:35:39 +000096 /// getBuffer - Returns the memory buffer for the associated content.
97 ///
Jonathan D. Turnera92d7e72011-06-16 20:47:21 +000098 /// \param Diag Object through which diagnostics will be emitted if the
Douglas Gregor36c35ba2010-03-16 00:35:39 +000099 /// buffer cannot be retrieved.
100 ///
Chris Lattnere127a0d2010-04-20 20:35:58 +0000101 /// \param Loc If specified, is the location that invalid file diagnostics
102 /// will be emitted at.
103 ///
Douglas Gregor36c35ba2010-03-16 00:35:39 +0000104 /// \param Invalid If non-NULL, will be set \c true if an error occurred.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000105 const llvm::MemoryBuffer *getBuffer(Diagnostic &Diag,
106 const SourceManager &SM,
107 SourceLocation Loc = SourceLocation(),
Douglas Gregor36c35ba2010-03-16 00:35:39 +0000108 bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000109
Ted Kremenekc16c2082009-01-06 01:55:26 +0000110 /// getSize - Returns the size of the content encapsulated by this
111 /// ContentCache. This can be the size of the source file or the size of an
112 /// arbitrary scratch buffer. If the ContentCache encapsulates a source
113 /// file this size is retrieved from the file's FileEntry.
114 unsigned getSize() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000115
Ted Kremenekc16c2082009-01-06 01:55:26 +0000116 /// getSizeBytesMapped - Returns the number of bytes actually mapped for
Chandler Carruth3201f382011-07-26 05:17:23 +0000117 /// this ContentCache. This can be 0 if the MemBuffer was not actually
118 /// expanded.
Ted Kremenekc16c2082009-01-06 01:55:26 +0000119 unsigned getSizeBytesMapped() const;
Ted Kremenekf61b8312011-04-28 20:36:42 +0000120
121 /// Returns the kind of memory used to back the memory buffer for
122 /// this content cache. This is used for performance analysis.
123 llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
Mike Stump1eb44332009-09-09 15:08:12 +0000124
Chris Lattner05816592009-01-17 03:54:16 +0000125 void setBuffer(const llvm::MemoryBuffer *B) {
Douglas Gregorc8151082010-03-16 22:53:51 +0000126 assert(!Buffer.getPointer() && "MemoryBuffer already set.");
127 Buffer.setPointer(B);
128 Buffer.setInt(false);
Ted Kremenekc16c2082009-01-06 01:55:26 +0000129 }
Douglas Gregorcc5888d2010-07-31 00:40:00 +0000130
131 /// \brief Get the underlying buffer, returning NULL if the buffer is not
132 /// yet available.
133 const llvm::MemoryBuffer *getRawBuffer() const {
134 return Buffer.getPointer();
135 }
Mike Stump1eb44332009-09-09 15:08:12 +0000136
Douglas Gregor29684422009-12-02 06:49:09 +0000137 /// \brief Replace the existing buffer (which will be deleted)
138 /// with the given buffer.
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000139 void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
Douglas Gregor29684422009-12-02 06:49:09 +0000140
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000141 /// \brief Determine whether the buffer itself is invalid.
142 bool isBufferInvalid() const {
143 return Buffer.getInt() & InvalidFlag;
144 }
145
146 /// \brief Determine whether the buffer should be freed.
147 bool shouldFreeBuffer() const {
148 return (Buffer.getInt() & DoNotFreeFlag) == 0;
149 }
150
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000151 ContentCache(const FileEntry *Ent = 0)
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000152 : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent),
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000153 SourceLineCache(0), NumLines(0), MacroArgsCache(0) {}
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000154
155 ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
156 : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt),
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000157 SourceLineCache(0), NumLines(0), MacroArgsCache(0) {}
Ted Kremenek78d85f52007-10-30 21:08:08 +0000158
159 ~ContentCache();
Mike Stump1eb44332009-09-09 15:08:12 +0000160
Ted Kremenek0d892d82007-10-30 22:57:35 +0000161 /// The copy ctor does not allow copies where source object has either
162 /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory
Chris Lattnerfc8f0e12011-04-15 05:22:18 +0000163 /// is not transferred, so this is a logical error.
Douglas Gregorc8151082010-03-16 22:53:51 +0000164 ContentCache(const ContentCache &RHS)
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000165 : Buffer(0, false), SourceLineCache(0), MacroArgsCache(0)
Douglas Gregorc8151082010-03-16 22:53:51 +0000166 {
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000167 OrigEntry = RHS.OrigEntry;
168 ContentsEntry = RHS.ContentsEntry;
Ted Kremenek0d892d82007-10-30 22:57:35 +0000169
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000170 assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0 &&
171 RHS.MacroArgsCache == 0
Ted Kremenek0d892d82007-10-30 22:57:35 +0000172 && "Passed ContentCache object cannot own a buffer.");
Mike Stump1eb44332009-09-09 15:08:12 +0000173
174 NumLines = RHS.NumLines;
Ted Kremenek0d892d82007-10-30 22:57:35 +0000175 }
Mike Stump1eb44332009-09-09 15:08:12 +0000176
Ted Kremenek0d892d82007-10-30 22:57:35 +0000177 private:
178 // Disable assignments.
Mike Stump1eb44332009-09-09 15:08:12 +0000179 ContentCache &operator=(const ContentCache& RHS);
180 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000181
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000182 /// FileInfo - Information about a FileID, basically just the logical file
183 /// that it represents and include stack information.
Reid Spencer5f016e22007-07-11 17:01:13 +0000184 ///
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000185 /// Each FileInfo has include stack information, indicating where it came
Chandler Carruth3201f382011-07-26 05:17:23 +0000186 /// from. This information encodes the #include chain that a token was
187 /// expanded from. The main include file has an invalid IncludeLoc.
Reid Spencer5f016e22007-07-11 17:01:13 +0000188 ///
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000189 /// FileInfos contain a "ContentCache *", with the contents of the file.
Reid Spencer5f016e22007-07-11 17:01:13 +0000190 ///
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000191 class FileInfo {
Reid Spencer5f016e22007-07-11 17:01:13 +0000192 /// IncludeLoc - The location of the #include that brought in this file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000193 /// This is an invalid SLOC for the main file (top of the #include chain).
194 unsigned IncludeLoc; // Really a SourceLocation
Mike Stump1eb44332009-09-09 15:08:12 +0000195
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000196 /// \brief Number of FileIDs (files and macros) that were created during
197 /// preprocessing of this #include, including this SLocEntry.
198 /// Zero means the preprocessor didn't provide such info for this SLocEntry.
199 unsigned NumCreatedFIDs;
200
Chris Lattner6e1aff22009-01-26 06:49:09 +0000201 /// Data - This contains the ContentCache* and the bits indicating the
202 /// characteristic of the file and whether it has #line info, all bitmangled
203 /// together.
204 uintptr_t Data;
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000205
Argyrios Kyrtzidis21032df2011-08-21 23:49:52 +0000206 friend class clang::SourceManager;
207 friend class clang::ASTWriter;
208 friend class clang::ASTReader;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000209 public:
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000210 /// get - Return a FileInfo object.
211 static FileInfo get(SourceLocation IL, const ContentCache *Con,
212 CharacteristicKind FileCharacter) {
213 FileInfo X;
214 X.IncludeLoc = IL.getRawEncoding();
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000215 X.NumCreatedFIDs = 0;
Chris Lattner6e1aff22009-01-26 06:49:09 +0000216 X.Data = (uintptr_t)Con;
Chris Lattner00282d62009-02-03 07:41:46 +0000217 assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
Chris Lattner6e1aff22009-01-26 06:49:09 +0000218 assert((unsigned)FileCharacter < 4 && "invalid file character");
219 X.Data |= (unsigned)FileCharacter;
Reid Spencer5f016e22007-07-11 17:01:13 +0000220 return X;
221 }
Mike Stump1eb44332009-09-09 15:08:12 +0000222
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000223 SourceLocation getIncludeLoc() const {
224 return SourceLocation::getFromRawEncoding(IncludeLoc);
225 }
Chris Lattner6e1aff22009-01-26 06:49:09 +0000226 const ContentCache* getContentCache() const {
Chris Lattner00282d62009-02-03 07:41:46 +0000227 return reinterpret_cast<const ContentCache*>(Data & ~7UL);
Chris Lattner6e1aff22009-01-26 06:49:09 +0000228 }
Mike Stump1eb44332009-09-09 15:08:12 +0000229
Chris Lattner0b9e7362008-09-26 21:18:42 +0000230 /// getCharacteristic - Return whether this is a system header or not.
Mike Stump1eb44332009-09-09 15:08:12 +0000231 CharacteristicKind getFileCharacteristic() const {
Chris Lattner6e1aff22009-01-26 06:49:09 +0000232 return (CharacteristicKind)(Data & 3);
Chris Lattner0b9e7362008-09-26 21:18:42 +0000233 }
Chris Lattnerac50e342009-02-03 22:13:05 +0000234
235 /// hasLineDirectives - Return true if this FileID has #line directives in
236 /// it.
237 bool hasLineDirectives() const { return (Data & 4) != 0; }
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Chris Lattnerac50e342009-02-03 22:13:05 +0000239 /// setHasLineDirectives - Set the flag that indicates that this FileID has
240 /// line table entries associated with it.
241 void setHasLineDirectives() {
242 Data |= 4;
243 }
Chris Lattner9dc1f532007-07-20 16:37:10 +0000244 };
Mike Stump1eb44332009-09-09 15:08:12 +0000245
Chandler Carruth78df8362011-07-26 04:41:47 +0000246 /// ExpansionInfo - Each ExpansionInfo encodes the expansion location - where
247 /// the token was ultimately expanded, and the SpellingLoc - where the actual
248 /// character data for the token came from.
249 class ExpansionInfo {
250 // Really these are all SourceLocations.
Mike Stump1eb44332009-09-09 15:08:12 +0000251
Chris Lattnere7fb4842009-02-15 20:52:18 +0000252 /// SpellingLoc - Where the spelling for the token can be found.
253 unsigned SpellingLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000254
Chandler Carruth78df8362011-07-26 04:41:47 +0000255 /// ExpansionLocStart/ExpansionLocEnd - In a macro expansion, these
256 /// indicate the start and end of the expansion. In object-like macros,
Chandler Carruth3201f382011-07-26 05:17:23 +0000257 /// these will be the same. In a function-like macro expansion, the start
258 /// will be the identifier and the end will be the ')'. Finally, in
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000259 /// macro-argument instantitions, the end will be 'SourceLocation()', an
260 /// invalid location.
Chandler Carruth78df8362011-07-26 04:41:47 +0000261 unsigned ExpansionLocStart, ExpansionLocEnd;
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000262
Chris Lattner9dc1f532007-07-20 16:37:10 +0000263 public:
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000264 SourceLocation getSpellingLoc() const {
265 return SourceLocation::getFromRawEncoding(SpellingLoc);
266 }
Chandler Carruth78df8362011-07-26 04:41:47 +0000267 SourceLocation getExpansionLocStart() const {
268 return SourceLocation::getFromRawEncoding(ExpansionLocStart);
Chris Lattnere7fb4842009-02-15 20:52:18 +0000269 }
Chandler Carruth78df8362011-07-26 04:41:47 +0000270 SourceLocation getExpansionLocEnd() const {
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000271 SourceLocation EndLoc =
Chandler Carruth78df8362011-07-26 04:41:47 +0000272 SourceLocation::getFromRawEncoding(ExpansionLocEnd);
273 return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc;
Chris Lattnere7fb4842009-02-15 20:52:18 +0000274 }
Mike Stump1eb44332009-09-09 15:08:12 +0000275
Chandler Carruth78df8362011-07-26 04:41:47 +0000276 std::pair<SourceLocation,SourceLocation> getExpansionLocRange() const {
277 return std::make_pair(getExpansionLocStart(), getExpansionLocEnd());
Chris Lattnere7fb4842009-02-15 20:52:18 +0000278 }
Mike Stump1eb44332009-09-09 15:08:12 +0000279
Chandler Carruth96d35892011-07-26 03:03:00 +0000280 bool isMacroArgExpansion() const {
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000281 // Note that this needs to return false for default constructed objects.
Chandler Carruth78df8362011-07-26 04:41:47 +0000282 return getExpansionLocStart().isValid() &&
283 SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid();
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000284 }
285
Chandler Carruth78df8362011-07-26 04:41:47 +0000286 /// create - Return a ExpansionInfo for an expansion. Start and End specify
287 /// the expansion range (where the macro is expanded), and SpellingLoc
288 /// specifies the spelling location (where the characters from the token
289 /// come from). All three can refer to normal File SLocs or expansion
290 /// locations.
291 static ExpansionInfo create(SourceLocation SpellingLoc,
292 SourceLocation Start, SourceLocation End) {
293 ExpansionInfo X;
294 X.SpellingLoc = SpellingLoc.getRawEncoding();
295 X.ExpansionLocStart = Start.getRawEncoding();
296 X.ExpansionLocEnd = End.getRawEncoding();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000297 return X;
Reid Spencer5f016e22007-07-11 17:01:13 +0000298 }
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000299
Chandler Carruth78df8362011-07-26 04:41:47 +0000300 /// createForMacroArg - Return a special ExpansionInfo for the expansion of
301 /// a macro argument into a function-like macro's body. ExpansionLoc
302 /// specifies the expansion location (where the macro is expanded). This
303 /// doesn't need to be a range because a macro is always expanded at
304 /// a macro parameter reference, and macro parameters are always exactly
305 /// one token. SpellingLoc specifies the spelling location (where the
306 /// characters from the token come from). ExpansionLoc and SpellingLoc can
307 /// both refer to normal File SLocs or expansion locations.
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000308 ///
309 /// Given the code:
310 /// \code
311 /// #define F(x) f(x)
312 /// F(42);
313 /// \endcode
314 ///
Chandler Carruth78df8362011-07-26 04:41:47 +0000315 /// When expanding '\c F(42)', the '\c x' would call this with an
316 /// SpellingLoc pointing at '\c 42' anad an ExpansionLoc pointing at its
317 /// location in the definition of '\c F'.
318 static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc,
319 SourceLocation ExpansionLoc) {
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000320 // We store an intentionally invalid source location for the end of the
Chandler Carruth78df8362011-07-26 04:41:47 +0000321 // expansion range to mark that this is a macro argument ion rather than
322 // a normal one.
323 return create(SpellingLoc, ExpansionLoc, SourceLocation());
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000324 }
Reid Spencer5f016e22007-07-11 17:01:13 +0000325 };
Mike Stump1eb44332009-09-09 15:08:12 +0000326
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000327 /// SLocEntry - This is a discriminated union of FileInfo and
Chandler Carruth78df8362011-07-26 04:41:47 +0000328 /// ExpansionInfo. SourceManager keeps an array of these objects, and
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000329 /// they are uniquely identified by the FileID datatype.
330 class SLocEntry {
Chandler Carruth3201f382011-07-26 05:17:23 +0000331 unsigned Offset; // low bit is set for expansion info.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000332 union {
333 FileInfo File;
Chandler Carruth17287622011-07-26 04:56:51 +0000334 ExpansionInfo Expansion;
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000335 };
336 public:
337 unsigned getOffset() const { return Offset >> 1; }
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Chandler Carruth17287622011-07-26 04:56:51 +0000339 bool isExpansion() const { return Offset & 1; }
340 bool isFile() const { return !isExpansion(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000341
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000342 const FileInfo &getFile() const {
343 assert(isFile() && "Not a file SLocEntry!");
344 return File;
345 }
346
Chandler Carruth17287622011-07-26 04:56:51 +0000347 const ExpansionInfo &getExpansion() const {
348 assert(isExpansion() && "Not a macro expansion SLocEntry!");
349 return Expansion;
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000350 }
Mike Stump1eb44332009-09-09 15:08:12 +0000351
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000352 static SLocEntry get(unsigned Offset, const FileInfo &FI) {
353 SLocEntry E;
354 E.Offset = Offset << 1;
355 E.File = FI;
356 return E;
357 }
358
Chandler Carruth78df8362011-07-26 04:41:47 +0000359 static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000360 SLocEntry E;
361 E.Offset = (Offset << 1) | 1;
Chandler Carruth17287622011-07-26 04:56:51 +0000362 E.Expansion = Expansion;
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000363 return E;
364 }
365 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000366} // end SrcMgr namespace.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000367
368/// \brief External source of source location entries.
369class ExternalSLocEntrySource {
370public:
371 virtual ~ExternalSLocEntrySource();
372
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000373 /// \brief Read the source location entry with index ID, which will always be
374 /// less than -1.
Douglas Gregore23ac652011-04-20 00:21:03 +0000375 ///
376 /// \returns true if an error occurred that prevented the source-location
377 /// entry from being loaded.
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000378 virtual bool ReadSLocEntry(int ID) = 0;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000379};
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000380
381
382/// IsBeforeInTranslationUnitCache - This class holds the cache used by
383/// isBeforeInTranslationUnit. The cache structure is complex enough to be
384/// worth breaking out of SourceManager.
385class IsBeforeInTranslationUnitCache {
386 /// L/R QueryFID - These are the FID's of the cached query. If these match up
387 /// with a subsequent query, the result can be reused.
388 FileID LQueryFID, RQueryFID;
Argyrios Kyrtzidis37e59a12011-08-17 00:31:18 +0000389
390 /// \brief True if LQueryFID was created before RQueryFID. This is used
391 /// to compare macro expansion locations.
392 bool IsLQFIDBeforeRQFID;
393
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000394 /// CommonFID - This is the file found in common between the two #include
395 /// traces. It is the nearest common ancestor of the #include tree.
396 FileID CommonFID;
397
398 /// L/R CommonOffset - This is the offset of the previous query in CommonFID.
399 /// Usually, this represents the location of the #include for QueryFID, but if
400 /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a
401 /// random token in the parent.
402 unsigned LCommonOffset, RCommonOffset;
403public:
404
405 /// isCacheValid - Return true if the currently cached values match up with
406 /// the specified LHS/RHS query. If not, we can't use the cache.
407 bool isCacheValid(FileID LHS, FileID RHS) const {
408 return LQueryFID == LHS && RQueryFID == RHS;
409 }
410
411 /// getCachedResult - If the cache is valid, compute the result given the
412 /// specified offsets in the LHS/RHS FID's.
413 bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
414 // If one of the query files is the common file, use the offset. Otherwise,
415 // use the #include loc in the common file.
416 if (LQueryFID != CommonFID) LOffset = LCommonOffset;
417 if (RQueryFID != CommonFID) ROffset = RCommonOffset;
Argyrios Kyrtzidis37e59a12011-08-17 00:31:18 +0000418
419 // It is common for multiple macro expansions to be "included" from the same
420 // location (expansion location), in which case use the order of the FileIDs
421 // to determine which came first.
422 if (LOffset == ROffset && LQueryFID != CommonFID && RQueryFID != CommonFID)
423 return IsLQFIDBeforeRQFID;
424
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000425 return LOffset < ROffset;
426 }
427
428 // Set up a new query.
Argyrios Kyrtzidis37e59a12011-08-17 00:31:18 +0000429 void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) {
430 assert(LHS != RHS);
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000431 LQueryFID = LHS;
432 RQueryFID = RHS;
Argyrios Kyrtzidis37e59a12011-08-17 00:31:18 +0000433 IsLQFIDBeforeRQFID = isLFIDBeforeRFID;
434 }
435
436 void clear() {
437 LQueryFID = RQueryFID = FileID();
438 IsLQFIDBeforeRQFID = false;
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000439 }
440
441 void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
442 unsigned rCommonOffset) {
443 CommonFID = commonFID;
444 LCommonOffset = lCommonOffset;
445 RCommonOffset = rCommonOffset;
446 }
447
448};
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000449
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000450/// \brief This class handles loading and caching of source files into memory.
451///
452/// This object owns the MemoryBuffer objects for all of the loaded
Reid Spencer5f016e22007-07-11 17:01:13 +0000453/// files and assigns unique FileID's for each unique #include chain.
454///
455/// The SourceManager can be queried for information about SourceLocation
Chandler Carruth3201f382011-07-26 05:17:23 +0000456/// objects, turning them into either spelling or expansion locations. Spelling
457/// locations represent where the bytes corresponding to a token came from and
458/// expansion locations represent where the location is in the user's view. In
459/// the case of a macro expansion, for example, the spelling location indicates
460/// where the expanded token came from and the expansion location specifies
461/// where it was expanded.
Ted Kremenek4f327862011-03-21 18:40:17 +0000462class SourceManager : public llvm::RefCountedBase<SourceManager> {
Douglas Gregorf715ca12010-03-16 00:06:06 +0000463 /// \brief Diagnostic object.
464 Diagnostic &Diag;
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000465
466 FileManager &FileMgr;
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000467
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000468 mutable llvm::BumpPtrAllocator ContentCacheAlloc;
Mike Stump1eb44332009-09-09 15:08:12 +0000469
Reid Spencer5f016e22007-07-11 17:01:13 +0000470 /// FileInfos - Memoized information about all of the files tracked by this
Ted Kremenek0d892d82007-10-30 22:57:35 +0000471 /// SourceManager. This set allows us to merge ContentCache entries based
472 /// on their FileEntry*. All ContentCache objects will thus have unique,
Mike Stump1eb44332009-09-09 15:08:12 +0000473 /// non-null, FileEntry pointers.
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000474 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000475
Argyrios Kyrtzidis299a4a92011-03-08 23:35:24 +0000476 /// \brief True if the ContentCache for files that are overriden by other
477 /// files, should report the original file name. Defaults to true.
478 bool OverridenFilesKeepOriginalName;
479
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000480 /// \brief Files that have been overriden with the contents from another file.
481 llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
482
Reid Spencer5f016e22007-07-11 17:01:13 +0000483 /// MemBufferInfos - Information about various memory buffers that we have
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000484 /// read in. All FileEntry* within the stored ContentCache objects are NULL,
485 /// as they do not refer to a file.
486 std::vector<SrcMgr::ContentCache*> MemBufferInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000487
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000488 /// \brief The table of SLocEntries that are local to this module.
489 ///
490 /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid
Chandler Carruth3201f382011-07-26 05:17:23 +0000491 /// expansion.
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000492 std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000493
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000494 /// \brief The table of SLocEntries that are loaded from other modules.
495 ///
496 /// Negative FileIDs are indexes into this table. To get from ID to an index,
497 /// use (-ID - 2).
498 std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable;
499
500 /// \brief The starting offset of the next local SLocEntry.
501 ///
502 /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
503 unsigned NextLocalOffset;
504
505 /// \brief The starting offset of the latest batch of loaded SLocEntries.
506 ///
507 /// This is LoadedSLocEntryTable.back().Offset, except that that entry might
508 /// not have been loaded, so that value would be unknown.
509 unsigned CurrentLoadedOffset;
510
Argyrios Kyrtzidisac836e42011-08-17 00:31:20 +0000511 /// \brief The highest possible offset is 2^31-1, so CurrentLoadedOffset
512 /// starts at 2^31.
513 static const unsigned MaxLoadedOffset = 1U << 31U;
514
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000515 /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable
516 /// have already been loaded from the external source.
517 ///
518 /// Same indexing as LoadedSLocEntryTable.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000519 std::vector<bool> SLocEntryLoaded;
520
521 /// \brief An external source for source location entries.
522 ExternalSLocEntrySource *ExternalSLocEntries;
523
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000524 /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
525 /// LastFileIDLookup records the last FileID looked up or created, because it
526 /// is very common to look up many tokens from the same file.
527 mutable FileID LastFileIDLookup;
Mike Stump1eb44332009-09-09 15:08:12 +0000528
Chris Lattner5b9a5042009-01-26 07:57:50 +0000529 /// LineTable - This holds information for #line directives. It is referenced
530 /// by indices from SLocEntryTable.
531 LineTableInfo *LineTable;
Mike Stump1eb44332009-09-09 15:08:12 +0000532
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000533 /// LastLineNo - These ivars serve as a cache used in the getLineNumber
534 /// method which is used to speedup getLineNumber calls to nearby locations.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000535 mutable FileID LastLineNoFileIDQuery;
Chris Lattnerf812a452008-11-18 06:51:15 +0000536 mutable SrcMgr::ContentCache *LastLineNoContentCache;
537 mutable unsigned LastLineNoFilePos;
538 mutable unsigned LastLineNoResult;
Mike Stump1eb44332009-09-09 15:08:12 +0000539
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000540 /// MainFileID - The file ID for the main source file of the translation unit.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000541 FileID MainFileID;
Steve Naroff49c1f4a2008-02-02 00:10:46 +0000542
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000543 // Statistics for -print-stats.
544 mutable unsigned NumLinearScans, NumBinaryProbes;
Mike Stump1eb44332009-09-09 15:08:12 +0000545
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +0000546 // Cache results for the isBeforeInTranslationUnit method.
Chris Lattnerdcb1d682010-05-07 01:17:07 +0000547 mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
Mike Stump1eb44332009-09-09 15:08:12 +0000548
Douglas Gregore23ac652011-04-20 00:21:03 +0000549 // Cache for the "fake" buffer used for error-recovery purposes.
550 mutable llvm::MemoryBuffer *FakeBufferForRecovery;
551
Steve Naroff49c1f4a2008-02-02 00:10:46 +0000552 // SourceManager doesn't support copy construction.
553 explicit SourceManager(const SourceManager&);
Mike Stump1eb44332009-09-09 15:08:12 +0000554 void operator=(const SourceManager&);
Reid Spencer5f016e22007-07-11 17:01:13 +0000555public:
Chris Lattner39b49bc2010-11-23 08:35:12 +0000556 SourceManager(Diagnostic &Diag, FileManager &FileMgr);
Chris Lattner5b9a5042009-01-26 07:57:50 +0000557 ~SourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000558
Chris Lattner5b9a5042009-01-26 07:57:50 +0000559 void clearIDTables();
Mike Stump1eb44332009-09-09 15:08:12 +0000560
Argyrios Kyrtzidis78a916e2010-09-22 14:32:24 +0000561 Diagnostic &getDiagnostics() const { return Diag; }
562
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000563 FileManager &getFileManager() const { return FileMgr; }
Argyrios Kyrtzidis389db162010-11-03 22:45:23 +0000564
Argyrios Kyrtzidis299a4a92011-03-08 23:35:24 +0000565 /// \brief Set true if the SourceManager should report the original file name
566 /// for contents of files that were overriden by other files.Defaults to true.
567 void setOverridenFilesKeepOriginalName(bool value) {
568 OverridenFilesKeepOriginalName = value;
569 }
570
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000571 /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
572 /// that will represent the FileID for the main source. One example
573 /// of when this would be used is when the main source is read from STDIN.
574 FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
575 assert(MainFileID.isInvalid() && "MainFileID already set!");
576 MainFileID = createFileIDForMemBuffer(Buffer);
577 return MainFileID;
578 }
579
Chris Lattner06a062d2009-01-19 08:02:45 +0000580 //===--------------------------------------------------------------------===//
581 // MainFileID creation and querying methods.
582 //===--------------------------------------------------------------------===//
583
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000584 /// getMainFileID - Returns the FileID of the main source file.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000585 FileID getMainFileID() const { return MainFileID; }
Mike Stump1eb44332009-09-09 15:08:12 +0000586
Chris Lattner06a062d2009-01-19 08:02:45 +0000587 /// createMainFileID - Create the FileID for the main source file.
Dan Gohmanf155dfa2010-08-27 15:44:11 +0000588 FileID createMainFileID(const FileEntry *SourceFile) {
Chris Lattner06a062d2009-01-19 08:02:45 +0000589 assert(MainFileID.isInvalid() && "MainFileID already set!");
Dan Gohmanf155dfa2010-08-27 15:44:11 +0000590 MainFileID = createFileID(SourceFile, SourceLocation(), SrcMgr::C_User);
Chris Lattner06a062d2009-01-19 08:02:45 +0000591 return MainFileID;
592 }
Mike Stump1eb44332009-09-09 15:08:12 +0000593
Douglas Gregor414cb642010-11-30 05:23:00 +0000594 /// \brief Set the file ID for the precompiled preamble, which is also the
595 /// main file.
596 void SetPreambleFileID(FileID Preamble) {
597 assert(MainFileID.isInvalid() && "MainFileID already set!");
598 MainFileID = Preamble;
599 }
600
Chris Lattner06a062d2009-01-19 08:02:45 +0000601 //===--------------------------------------------------------------------===//
Chandler Carruth3201f382011-07-26 05:17:23 +0000602 // Methods to create new FileID's and macro expansions.
Chris Lattner06a062d2009-01-19 08:02:45 +0000603 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Reid Spencer5f016e22007-07-11 17:01:13 +0000605 /// createFileID - Create a new FileID that represents the specified file
Peter Collingbourned57b7ff2011-06-30 16:41:03 +0000606 /// being #included from the specified IncludePosition. This translates NULL
607 /// into standard input.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000608 FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000609 SrcMgr::CharacteristicKind FileCharacter,
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000610 int LoadedID = 0, unsigned LoadedOffset = 0) {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000611 const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
Dan Gohman0d06e992010-10-26 20:47:28 +0000612 assert(IR && "getOrCreateContentCache() cannot return NULL");
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000613 return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset);
Reid Spencer5f016e22007-07-11 17:01:13 +0000614 }
Mike Stump1eb44332009-09-09 15:08:12 +0000615
Reid Spencer5f016e22007-07-11 17:01:13 +0000616 /// createFileIDForMemBuffer - Create a new FileID that represents the
617 /// specified memory buffer. This does no caching of the buffer and takes
618 /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000619 FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000620 int LoadedID = 0, unsigned LoadedOffset = 0) {
Nico Weber7bfaaae2008-08-10 19:59:06 +0000621 return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
Douglas Gregorf62d43d2011-07-19 16:10:42 +0000622 SrcMgr::C_User, LoadedID, LoadedOffset);
Ted Kremenek1036b682007-12-19 23:48:45 +0000623 }
Chris Lattner06a062d2009-01-19 08:02:45 +0000624
Chandler Carruthbf340e42011-07-26 03:03:05 +0000625 /// createMacroArgExpansionLoc - Return a new SourceLocation that encodes the
626 /// fact that a token from SpellingLoc should actually be referenced from
Chandler Carruth3201f382011-07-26 05:17:23 +0000627 /// ExpansionLoc, and that it represents the expansion of a macro argument
628 /// into the function-like macro body.
Chandler Carruthbf340e42011-07-26 03:03:05 +0000629 SourceLocation createMacroArgExpansionLoc(SourceLocation Loc,
630 SourceLocation ExpansionLoc,
631 unsigned TokLength);
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000632
Chandler Carruthbf340e42011-07-26 03:03:05 +0000633 /// createExpansionLoc - Return a new SourceLocation that encodes the fact
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +0000634 /// that a token from SpellingLoc should actually be referenced from
Chandler Carruthbf340e42011-07-26 03:03:05 +0000635 /// ExpansionLoc.
636 SourceLocation createExpansionLoc(SourceLocation Loc,
637 SourceLocation ExpansionLocStart,
638 SourceLocation ExpansionLocEnd,
639 unsigned TokLength,
640 int LoadedID = 0,
641 unsigned LoadedOffset = 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000642
Douglas Gregor29684422009-12-02 06:49:09 +0000643 /// \brief Retrieve the memory buffer associated with the given file.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000644 ///
645 /// \param Invalid If non-NULL, will be set \c true if an error
646 /// occurs while retrieving the memory buffer.
647 const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
648 bool *Invalid = 0);
Douglas Gregor29684422009-12-02 06:49:09 +0000649
650 /// \brief Override the contents of the given source file by providing an
651 /// already-allocated buffer.
652 ///
Dan Gohmanafbf5f82010-08-26 02:27:03 +0000653 /// \param SourceFile the source file whose contents will be overriden.
Douglas Gregor29684422009-12-02 06:49:09 +0000654 ///
655 /// \param Buffer the memory buffer whose contents will be used as the
656 /// data in the given source file.
657 ///
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000658 /// \param DoNotFree If true, then the buffer will not be freed when the
659 /// source manager is destroyed.
Dan Gohman0d06e992010-10-26 20:47:28 +0000660 void overrideFileContents(const FileEntry *SourceFile,
Douglas Gregorf4f6c9d2010-07-26 21:36:20 +0000661 const llvm::MemoryBuffer *Buffer,
662 bool DoNotFree = false);
Douglas Gregor29684422009-12-02 06:49:09 +0000663
Argyrios Kyrtzidisb1c86492011-03-05 01:03:53 +0000664 /// \brief Override the the given source file with another one.
665 ///
666 /// \param SourceFile the source file which will be overriden.
667 ///
668 /// \param NewFile the file whose contents will be used as the
669 /// data instead of the contents of the given source file.
670 void overrideFileContents(const FileEntry *SourceFile,
671 const FileEntry *NewFile);
672
Chris Lattner06a062d2009-01-19 08:02:45 +0000673 //===--------------------------------------------------------------------===//
674 // FileID manipulation methods.
675 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000676
Daniel Dunbar2ffb14f2009-12-06 09:19:25 +0000677 /// getBuffer - Return the buffer for the specified FileID. If there is an
678 /// error opening this buffer the first time, this manufactures a temporary
679 /// buffer and returns a non-empty error string.
Chris Lattnere127a0d2010-04-20 20:35:58 +0000680 const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
681 bool *Invalid = 0) const {
Douglas Gregore23ac652011-04-20 00:21:03 +0000682 bool MyInvalid = false;
683 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
684 if (MyInvalid || !Entry.isFile()) {
685 if (Invalid)
686 *Invalid = true;
687
688 return getFakeBufferForRecovery();
689 }
690
691 return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
692 Invalid);
Chris Lattner06a062d2009-01-19 08:02:45 +0000693 }
Mike Stump1eb44332009-09-09 15:08:12 +0000694
Chris Lattnere127a0d2010-04-20 20:35:58 +0000695 const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
Douglas Gregore23ac652011-04-20 00:21:03 +0000696 bool MyInvalid = false;
697 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
698 if (MyInvalid || !Entry.isFile()) {
699 if (Invalid)
700 *Invalid = true;
701
702 return getFakeBufferForRecovery();
703 }
704
705 return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
706 SourceLocation(),
707 Invalid);
Chris Lattnere127a0d2010-04-20 20:35:58 +0000708 }
709
Chris Lattner06a062d2009-01-19 08:02:45 +0000710 /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
711 const FileEntry *getFileEntryForID(FileID FID) const {
Douglas Gregore23ac652011-04-20 00:21:03 +0000712 bool MyInvalid = false;
713 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
714 if (MyInvalid || !Entry.isFile())
715 return 0;
716
717 return Entry.getFile().getContentCache()->OrigEntry;
Chris Lattner06a062d2009-01-19 08:02:45 +0000718 }
Mike Stump1eb44332009-09-09 15:08:12 +0000719
Ted Kremenek9d5a1652011-03-23 02:16:44 +0000720 /// Returns the FileEntry record for the provided SLocEntry.
721 const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
722 {
723 return sloc.getFile().getContentCache()->OrigEntry;
724 }
725
Benjamin Kramerceafc4b2010-03-16 14:48:07 +0000726 /// getBufferData - Return a StringRef to the source buffer data for the
727 /// specified FileID.
728 ///
Douglas Gregorf715ca12010-03-16 00:06:06 +0000729 /// \param FID The file ID whose contents will be returned.
730 /// \param Invalid If non-NULL, will be set true if an error occurred.
Chris Lattner686775d2011-07-20 06:58:45 +0000731 StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +0000732
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000733 /// \brief Get the number of FileIDs (files and macros) that were created
734 /// during preprocessing of \arg FID, including it.
735 unsigned getNumCreatedFIDsForFileID(FileID FID) const {
736 bool Invalid = false;
737 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
738 if (Invalid || !Entry.isFile())
739 return 0;
740
741 return Entry.getFile().NumCreatedFIDs;
742 }
743
744 /// \brief Set the number of FileIDs (files and macros) that were created
745 /// during preprocessing of \arg FID, including it.
746 void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs) const {
747 bool Invalid = false;
748 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
749 if (Invalid || !Entry.isFile())
750 return;
751
752 assert(Entry.getFile().NumCreatedFIDs == 0 && "Already set!");
753 const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs;
754 }
Mike Stump1eb44332009-09-09 15:08:12 +0000755
Chris Lattner06a062d2009-01-19 08:02:45 +0000756 //===--------------------------------------------------------------------===//
757 // SourceLocation manipulation methods.
758 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000759
Chris Lattner668ab1a2009-03-13 01:05:57 +0000760 /// getFileID - Return the FileID for a SourceLocation. This is a very
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000761 /// hot method that is used for all SourceManager queries that start with a
762 /// SourceLocation object. It is responsible for finding the entry in
763 /// SLocEntryTable which contains the specified location.
764 ///
765 FileID getFileID(SourceLocation SpellingLoc) const {
766 unsigned SLocOffset = SpellingLoc.getOffset();
Mike Stump1eb44332009-09-09 15:08:12 +0000767
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000768 // If our one-entry cache covers this offset, just return it.
769 if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
770 return LastFileIDLookup;
771
772 return getFileIDSlow(SLocOffset);
773 }
Mike Stump1eb44332009-09-09 15:08:12 +0000774
Chris Lattner2b2453a2009-01-17 06:22:33 +0000775 /// getLocForStartOfFile - Return the source location corresponding to the
776 /// first byte of the specified file.
777 SourceLocation getLocForStartOfFile(FileID FID) const {
Douglas Gregore23ac652011-04-20 00:21:03 +0000778 bool Invalid = false;
779 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
780 if (Invalid || !Entry.isFile())
781 return SourceLocation();
782
783 unsigned FileOffset = Entry.getOffset();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000784 return SourceLocation::getFileLoc(FileOffset);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000785 }
Mike Stump1eb44332009-09-09 15:08:12 +0000786
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +0000787 /// \brief Returns the include location if \arg FID is a #include'd file
788 /// otherwise it returns an invalid location.
789 SourceLocation getIncludeLoc(FileID FID) const {
790 bool Invalid = false;
791 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
792 if (Invalid || !Entry.isFile())
793 return SourceLocation();
794
795 return Entry.getFile().getIncludeLoc();
796 }
797
Chandler Carruth40278532011-07-25 16:49:02 +0000798 /// getExpansionLoc - Given a SourceLocation object, return the expansion
799 /// location referenced by the ID.
800 SourceLocation getExpansionLoc(SourceLocation Loc) const {
Chris Lattneraddb7972009-01-26 20:04:19 +0000801 // Handle the non-mapped case inline, defer to out of line code to handle
Chandler Carruth40278532011-07-25 16:49:02 +0000802 // expansions.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000803 if (Loc.isFileID()) return Loc;
Chandler Carruthf84ef952011-07-25 20:52:26 +0000804 return getExpansionLocSlowCase(Loc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000805 }
Mike Stump1eb44332009-09-09 15:08:12 +0000806
Chandler Carruth999f7392011-07-25 20:52:21 +0000807 /// getImmediateExpansionRange - Loc is required to be an expansion location.
808 /// Return the start/end of the expansion information.
Chris Lattnere7fb4842009-02-15 20:52:18 +0000809 std::pair<SourceLocation,SourceLocation>
Chandler Carruth999f7392011-07-25 20:52:21 +0000810 getImmediateExpansionRange(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000811
Chandler Carruthedc3dcc2011-07-25 16:56:02 +0000812 /// getExpansionRange - Given a SourceLocation object, return the range of
813 /// tokens covered by the expansion the ultimate file.
Chris Lattner66781332009-02-15 21:26:50 +0000814 std::pair<SourceLocation,SourceLocation>
Chandler Carruthedc3dcc2011-07-25 16:56:02 +0000815 getExpansionRange(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000816
817
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000818 /// getSpellingLoc - Given a SourceLocation object, return the spelling
819 /// location referenced by the ID. This is the place where the characters
820 /// that make up the lexed token can be found.
821 SourceLocation getSpellingLoc(SourceLocation Loc) const {
Chris Lattneraddb7972009-01-26 20:04:19 +0000822 // Handle the non-mapped case inline, defer to out of line code to handle
Chandler Carruth3201f382011-07-26 05:17:23 +0000823 // expansions.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000824 if (Loc.isFileID()) return Loc;
Chris Lattneraddb7972009-01-26 20:04:19 +0000825 return getSpellingLocSlowCase(Loc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000826 }
Mike Stump1eb44332009-09-09 15:08:12 +0000827
Chris Lattner387616e2009-02-17 08:04:48 +0000828 /// getImmediateSpellingLoc - Given a SourceLocation object, return the
829 /// spelling location referenced by the ID. This is the first level down
830 /// towards the place where the characters that make up the lexed token can be
831 /// found. This should not generally be used by clients.
Mike Stump1eb44332009-09-09 15:08:12 +0000832 SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000833
834 /// getDecomposedLoc - Decompose the specified location into a raw FileID +
835 /// Offset pair. The first element is the FileID, the second is the
836 /// offset from the start of the buffer of the location.
837 std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
838 FileID FID = getFileID(Loc);
839 return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset());
840 }
Mike Stump1eb44332009-09-09 15:08:12 +0000841
Chandler Carruth3201f382011-07-26 05:17:23 +0000842 /// getDecomposedExpansionLoc - Decompose the specified location into a raw
843 /// FileID + Offset pair. If the location is an expansion record, walk
844 /// through it until we find the final location expanded.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000845 std::pair<FileID, unsigned>
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +0000846 getDecomposedExpansionLoc(SourceLocation Loc) const {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000847 FileID FID = getFileID(Loc);
848 const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000849
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000850 unsigned Offset = Loc.getOffset()-E->getOffset();
851 if (Loc.isFileID())
852 return std::make_pair(FID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000853
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +0000854 return getDecomposedExpansionLocSlowCase(E);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000855 }
856
857 /// getDecomposedSpellingLoc - Decompose the specified location into a raw
Chandler Carruth3201f382011-07-26 05:17:23 +0000858 /// FileID + Offset pair. If the location is an expansion record, walk
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000859 /// through it until we find its spelling record.
860 std::pair<FileID, unsigned>
861 getDecomposedSpellingLoc(SourceLocation Loc) const {
862 FileID FID = getFileID(Loc);
863 const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000864
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000865 unsigned Offset = Loc.getOffset()-E->getOffset();
866 if (Loc.isFileID())
867 return std::make_pair(FID, Offset);
868 return getDecomposedSpellingLocSlowCase(E, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000869 }
870
Chris Lattner52c29082009-01-27 06:27:13 +0000871 /// getFileOffset - This method returns the offset from the start
872 /// of the file that the specified SourceLocation represents. This is not very
873 /// meaningful for a macro ID.
874 unsigned getFileOffset(SourceLocation SpellingLoc) const {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000875 return getDecomposedLoc(SpellingLoc).second;
876 }
Mike Stump1eb44332009-09-09 15:08:12 +0000877
Chandler Carruth96d35892011-07-26 03:03:00 +0000878 /// isMacroArgExpansion - This method tests whether the given source location
879 /// represents a macro argument's expansion into the function-like macro
880 /// definition. Such source locations only appear inside of the expansion
881 /// locations representing where a particular function-like macro was
882 /// expanded.
883 bool isMacroArgExpansion(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000884
Argyrios Kyrtzidis499ea552011-08-23 21:02:38 +0000885 /// \brief Returns true if \arg Loc is inside the [\arg Start, +\arg Length)
886 /// chunk of the source location address space.
887 /// If it's true and \arg RelativeOffset is non-null, it will be set to the
888 /// relative offset of \arg Loc inside the chunk.
889 bool isInSLocAddrSpace(SourceLocation Loc,
890 SourceLocation Start, unsigned Length,
891 unsigned *RelativeOffset = 0) const {
892 assert(((Start.getOffset() < NextLocalOffset &&
893 Start.getOffset()+Length <= NextLocalOffset) ||
894 (Start.getOffset() >= CurrentLoadedOffset &&
895 Start.getOffset()+Length < MaxLoadedOffset)) &&
896 "Chunk is not valid SLoc address space");
897 unsigned LocOffs = Loc.getOffset();
898 unsigned BeginOffs = Start.getOffset();
899 unsigned EndOffs = BeginOffs + Length;
900 if (LocOffs >= BeginOffs && LocOffs < EndOffs) {
901 if (RelativeOffset)
902 *RelativeOffset = LocOffs - BeginOffs;
903 return true;
904 }
905
906 return false;
907 }
908
Argyrios Kyrtzidisb6c465e2011-08-23 21:02:41 +0000909 /// \brief Return true if both \arg LHS and \arg RHS are in the local source
910 /// location address space or the loaded one. If it's true and
911 /// \arg RelativeOffset is non-null, it will be set to the offset of \arg RHS
912 /// relative to \arg LHS.
913 bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS,
914 int *RelativeOffset) const {
915 unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset();
916 bool LHSLoaded = LHSOffs >= CurrentLoadedOffset;
917 bool RHSLoaded = RHSOffs >= CurrentLoadedOffset;
918
919 if (LHSLoaded == RHSLoaded) {
920 if (RelativeOffset)
921 *RelativeOffset = RHSOffs - LHSOffs;
922 return true;
923 }
924
925 return false;
926 }
927
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000928 //===--------------------------------------------------------------------===//
929 // Queries about the code at a SourceLocation.
930 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000931
Reid Spencer5f016e22007-07-11 17:01:13 +0000932 /// getCharacterData - Return a pointer to the start of the specified location
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000933 /// in the appropriate spelling MemoryBuffer.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000934 ///
935 /// \param Invalid If non-NULL, will be set \c true if an error occurs.
936 const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000937
Chris Lattner9dc1f532007-07-20 16:37:10 +0000938 /// getColumnNumber - Return the column # for the specified file position.
939 /// This is significantly cheaper to compute than the line number. This
Chandler Carruth3201f382011-07-26 05:17:23 +0000940 /// returns zero if the column number isn't known. This may only be called
941 /// on a file sloc, so you must choose a spelling or expansion location
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000942 /// before calling this method.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000943 unsigned getColumnNumber(FileID FID, unsigned FilePos,
944 bool *Invalid = 0) const;
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000945 unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
Chandler Carrutha77c0312011-07-25 20:57:57 +0000946 unsigned getExpansionColumnNumber(SourceLocation Loc,
Chandler Carruthb49dcd22011-07-25 20:59:15 +0000947 bool *Invalid = 0) const;
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000948 unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000949
950
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000951 /// getLineNumber - Given a SourceLocation, return the spelling line number
Reid Spencer5f016e22007-07-11 17:01:13 +0000952 /// for the position indicated. This requires building and caching a table of
953 /// line offsets for the MemoryBuffer, so this is not cheap: use only when
954 /// about to emit a diagnostic.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000955 unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000956 unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
Chandler Carruth64211622011-07-25 21:09:52 +0000957 unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
Chandler Carruth5ef04ee2011-02-23 00:47:48 +0000958 unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000959
Chris Lattnerbff5c512009-02-17 08:39:06 +0000960 /// Return the filename or buffer identifier of the buffer the location is in.
961 /// Note that this name does not respect #line directives. Use getPresumedLoc
962 /// for normal clients.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000963 const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000964
Chris Lattner6b306672009-02-04 05:33:01 +0000965 /// getFileCharacteristic - return the file characteristic of the specified
Mike Stump1eb44332009-09-09 15:08:12 +0000966 /// source location, indicating whether this is a normal file, a system
Chris Lattner6b306672009-02-04 05:33:01 +0000967 /// header, or an "implicit extern C" system header.
968 ///
969 /// This state can be modified with flags on GNU linemarker directives like:
970 /// # 4 "foo.h" 3
971 /// which changes all source locations in the current file after that to be
972 /// considered to be from a system header.
973 SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000974
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000975 /// getPresumedLoc - This method returns the "presumed" location of a
976 /// SourceLocation specifies. A "presumed location" can be modified by #line
977 /// or GNU line marker directives. This provides a view on the data that a
978 /// user should see in diagnostics, for example.
979 ///
Chandler Carruth3201f382011-07-26 05:17:23 +0000980 /// Note that a presumed location is always given as the expansion point of
981 /// an expansion location, not at the spelling location.
Douglas Gregorcb7b1e12010-11-12 07:15:47 +0000982 ///
983 /// \returns The presumed location of the specified SourceLocation. If the
984 /// presumed location cannot be calculate (e.g., because \p Loc is invalid
985 /// or the file containing \p Loc has changed on disk), returns an invalid
986 /// presumed location.
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000987 PresumedLoc getPresumedLoc(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000988
Ted Kremenek9fd87b12008-04-14 21:04:18 +0000989 /// isFromSameFile - Returns true if both SourceLocations correspond to
990 /// the same file.
991 bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
Chris Lattnera11d6172009-01-19 07:46:45 +0000992 return getFileID(Loc1) == getFileID(Loc2);
Ted Kremenek9fd87b12008-04-14 21:04:18 +0000993 }
Mike Stump1eb44332009-09-09 15:08:12 +0000994
Ted Kremenek9fd87b12008-04-14 21:04:18 +0000995 /// isFromMainFile - Returns true if the file of provided SourceLocation is
996 /// the main file.
997 bool isFromMainFile(SourceLocation Loc) const {
Chris Lattnera11d6172009-01-19 07:46:45 +0000998 return getFileID(Loc) == getMainFileID();
Mike Stump1eb44332009-09-09 15:08:12 +0000999 }
1000
Nico Weber7bfaaae2008-08-10 19:59:06 +00001001 /// isInSystemHeader - Returns if a SourceLocation is in a system header.
1002 bool isInSystemHeader(SourceLocation Loc) const {
Chris Lattner0b9e7362008-09-26 21:18:42 +00001003 return getFileCharacteristic(Loc) != SrcMgr::C_User;
Nico Weber7bfaaae2008-08-10 19:59:06 +00001004 }
Mike Stump1eb44332009-09-09 15:08:12 +00001005
Chris Lattner0d456582009-06-13 23:31:51 +00001006 /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
1007 /// system header.
1008 bool isInExternCSystemHeader(SourceLocation Loc) const {
1009 return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
1010 }
Mike Stump1eb44332009-09-09 15:08:12 +00001011
Argyrios Kyrtzidis54232ad2011-08-19 22:34:01 +00001012 /// \brief The size of the SLocEnty that \arg FID represents.
Argyrios Kyrtzidis984e42c2011-08-23 21:02:28 +00001013 unsigned getFileIDSize(FileID FID) const;
Argyrios Kyrtzidis54232ad2011-08-19 22:34:01 +00001014
Argyrios Kyrtzidisd60a34a2011-08-19 22:34:17 +00001015 /// \brief Given a specific FileID, returns true if \arg Loc is inside that
1016 /// FileID chunk and sets relative offset (offset of \arg Loc from beginning
1017 /// of FileID) to \arg relativeOffset.
1018 bool isInFileID(SourceLocation Loc, FileID FID,
1019 unsigned *RelativeOffset = 0) const {
1020 return isInFileID(Loc, FID, 0, getFileIDSize(FID), RelativeOffset);
1021 }
1022
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +00001023 /// \brief Given a specific chunk of a FileID (FileID with offset+length),
1024 /// returns true if \arg Loc is inside that chunk and sets relative offset
1025 /// (offset of \arg Loc from beginning of chunk) to \arg relativeOffset.
1026 bool isInFileID(SourceLocation Loc,
1027 FileID FID, unsigned offset, unsigned length,
Argyrios Kyrtzidis984e42c2011-08-23 21:02:28 +00001028 unsigned *relativeOffset = 0) const;
Argyrios Kyrtzidis469244a2011-05-28 03:56:11 +00001029
Chris Lattner06a062d2009-01-19 08:02:45 +00001030 //===--------------------------------------------------------------------===//
Chris Lattner5b9a5042009-01-26 07:57:50 +00001031 // Line Table Manipulation Routines
1032 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +00001033
Chris Lattner5b9a5042009-01-26 07:57:50 +00001034 /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
Mike Stump1eb44332009-09-09 15:08:12 +00001035 ///
Chris Lattner686775d2011-07-20 06:58:45 +00001036 unsigned getLineTableFilenameID(StringRef Str);
Mike Stump1eb44332009-09-09 15:08:12 +00001037
Chris Lattner4c4ea172009-02-03 21:52:55 +00001038 /// AddLineNote - Add a line note to the line table for the FileID and offset
1039 /// specified by Loc. If FilenameID is -1, it is considered to be
1040 /// unspecified.
1041 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
Chris Lattner9d79eba2009-02-04 05:21:58 +00001042 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
Mike Stump1eb44332009-09-09 15:08:12 +00001043 bool IsFileEntry, bool IsFileExit,
Chris Lattner9d79eba2009-02-04 05:21:58 +00001044 bool IsSystemHeader, bool IsExternCHeader);
Douglas Gregorbd945002009-04-13 16:31:14 +00001045
1046 /// \brief Determine if the source manager has a line table.
1047 bool hasLineTable() const { return LineTable != 0; }
1048
1049 /// \brief Retrieve the stored line table.
1050 LineTableInfo &getLineTable();
1051
Chris Lattner5b9a5042009-01-26 07:57:50 +00001052 //===--------------------------------------------------------------------===//
Ted Kremenek457aaf02011-04-28 04:10:31 +00001053 // Queries for performance analysis.
1054 //===--------------------------------------------------------------------===//
1055
1056 /// Return the total amount of physical memory allocated by the
1057 /// ContentCache allocator.
1058 size_t getContentCacheSize() const {
1059 return ContentCacheAlloc.getTotalMemory();
1060 }
Ted Kremenekf61b8312011-04-28 20:36:42 +00001061
1062 struct MemoryBufferSizes {
1063 const size_t malloc_bytes;
1064 const size_t mmap_bytes;
1065
1066 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
1067 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
1068 };
1069
1070 /// Return the amount of memory used by memory buffers, breaking down
1071 /// by heap-backed versus mmap'ed memory.
1072 MemoryBufferSizes getMemoryBufferSizes() const;
Ted Kremenekca7dc2b2011-07-26 23:46:06 +00001073
1074 // Return the amount of memory used for various side tables and
1075 // data structures in the SourceManager.
1076 size_t getDataStructureSizes() const;
Ted Kremenek457aaf02011-04-28 04:10:31 +00001077
1078 //===--------------------------------------------------------------------===//
Chris Lattner06a062d2009-01-19 08:02:45 +00001079 // Other miscellaneous methods.
1080 //===--------------------------------------------------------------------===//
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +00001081
1082 /// \brief Get the source location for the given file:line:col triplet.
1083 ///
1084 /// If the source file is included multiple times, the source location will
1085 /// be based upon the first inclusion.
Argyrios Kyrtzidisac836e42011-08-17 00:31:20 +00001086 ///
1087 /// If the location points inside a function macro argument, the returned
1088 /// location will be the macro location in which the argument was expanded.
1089 /// \sa getMacroArgExpandedLocation
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +00001090 SourceLocation getLocation(const FileEntry *SourceFile,
Argyrios Kyrtzidisac836e42011-08-17 00:31:20 +00001091 unsigned Line, unsigned Col) {
1092 SourceLocation Loc = translateFileLineCol(SourceFile, Line, Col);
1093 return getMacroArgExpandedLocation(Loc);
1094 }
1095
1096 /// \brief Get the source location for the given file:line:col triplet.
1097 ///
1098 /// If the source file is included multiple times, the source location will
1099 /// be based upon the first inclusion.
1100 SourceLocation translateFileLineCol(const FileEntry *SourceFile,
1101 unsigned Line, unsigned Col);
1102
1103 /// \brief If \arg Loc points inside a function macro argument, the returned
1104 /// location will be the macro location in which the argument was expanded.
1105 /// If a macro argument is used multiple times, the expanded location will
1106 /// be at the first expansion of the argument.
1107 /// e.g.
1108 /// MY_MACRO(foo);
1109 /// ^
1110 /// Passing a file location pointing at 'foo', will yield a macro location
1111 /// where 'foo' was expanded into.
1112 SourceLocation getMacroArgExpandedLocation(SourceLocation Loc);
Mike Stump1eb44332009-09-09 15:08:12 +00001113
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +00001114 /// \brief Determines the order of 2 source locations in the translation unit.
1115 ///
1116 /// \returns true if LHS source location comes before RHS, false otherwise.
1117 bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
1118
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +00001119 /// \brief Determines the order of 2 source locations in the "source location
1120 /// address space".
Argyrios Kyrtzidis5d579e72011-08-23 21:02:35 +00001121 bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const {
1122 return isBeforeInSLocAddrSpace(LHS, RHS.getOffset());
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +00001123 }
1124
1125 /// \brief Determines the order of a source location and a source location
1126 /// offset in the "source location address space".
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001127 ///
1128 /// Note that we always consider source locations loaded from
Argyrios Kyrtzidis5d579e72011-08-23 21:02:35 +00001129 bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001130 unsigned LHSOffset = LHS.getOffset();
1131 bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1132 bool RHSLoaded = RHS >= CurrentLoadedOffset;
1133 if (LHSLoaded == RHSLoaded)
Argyrios Kyrtzidis5d579e72011-08-23 21:02:35 +00001134 return LHSOffset < RHS;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001135
1136 return LHSLoaded;
Argyrios Kyrtzidisb73377e2011-07-07 03:40:34 +00001137 }
1138
Chris Lattnerc6fe32a2009-01-17 03:48:08 +00001139 // Iterators over FileInfos.
Chris Lattner0d0bf8c2009-02-03 07:30:45 +00001140 typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
1141 ::const_iterator fileinfo_iterator;
Chris Lattnerc6fe32a2009-01-17 03:48:08 +00001142 fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
1143 fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
Douglas Gregord93256e2010-01-28 06:00:51 +00001144 bool hasFileInfo(const FileEntry *File) const {
1145 return FileInfos.find(File) != FileInfos.end();
1146 }
Chris Lattnerc6fe32a2009-01-17 03:48:08 +00001147
Reid Spencer5f016e22007-07-11 17:01:13 +00001148 /// PrintStats - Print statistics to stderr.
1149 ///
1150 void PrintStats() const;
Reid Spencer5f016e22007-07-11 17:01:13 +00001151
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001152 /// \brief Get the number of local SLocEntries we have.
1153 unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
1154
1155 /// \brief Get a local SLocEntry. This is exposed for indexing.
1156 const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index,
1157 bool *Invalid = 0) const {
1158 assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1159 return LocalSLocEntryTable[Index];
Douglas Gregorbdfe48a2009-10-16 22:46:09 +00001160 }
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001161
1162 /// \brief Get the number of loaded SLocEntries we have.
1163 unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
1164
1165 /// \brief Get a loaded SLocEntry. This is exposed for indexing.
1166 const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index, bool *Invalid=0) const {
1167 assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1168 if (!SLocEntryLoaded[Index])
1169 ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2));
1170 return LoadedSLocEntryTable[Index];
1171 }
1172
Douglas Gregore23ac652011-04-20 00:21:03 +00001173 const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001174 return getSLocEntryByID(FID.ID);
Douglas Gregorbd945002009-04-13 16:31:14 +00001175 }
1176
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001177 unsigned getNextLocalOffset() const { return NextLocalOffset; }
1178
1179 void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1180 assert(LoadedSLocEntryTable.empty() &&
1181 "Invalidating existing loaded entries");
1182 ExternalSLocEntries = Source;
1183 }
1184
1185 /// \brief Allocate a number of loaded SLocEntries, which will be actually
1186 /// loaded on demand from the external source.
1187 ///
1188 /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1189 /// in the global source view. The lowest ID and the base offset of the
1190 /// entries will be returned.
1191 std::pair<int, unsigned>
1192 AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
1193
Ted Kremenek78d85f52007-10-30 21:08:08 +00001194private:
Douglas Gregore23ac652011-04-20 00:21:03 +00001195 const llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1196
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001197 /// \brief Get the entry with the given unwrapped FileID.
1198 const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const {
1199 assert(ID != -1 && "Using FileID sentinel value");
1200 if (ID < 0)
1201 return getLoadedSLocEntryByID(ID);
1202 return getLocalSLocEntry(static_cast<unsigned>(ID));
1203 }
1204
1205 const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID) const {
1206 return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2));
1207 }
1208
Chandler Carruthbf340e42011-07-26 03:03:05 +00001209 /// createExpansionLoc - Implements the common elements of storing an
Chandler Carruth3201f382011-07-26 05:17:23 +00001210 /// expansion info struct into the SLocEntry table and producing a source
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +00001211 /// location that refers to it.
Chandler Carruth78df8362011-07-26 04:41:47 +00001212 SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
Chandler Carruthbf340e42011-07-26 03:03:05 +00001213 unsigned TokLength,
1214 int LoadedID = 0,
1215 unsigned LoadedOffset = 0);
Chandler Carruthc8d1ecc2011-07-07 23:56:36 +00001216
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001217 /// isOffsetInFileID - Return true if the specified FileID contains the
1218 /// specified SourceLocation offset. This is a very hot method.
1219 inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1220 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1221 // If the entry is after the offset, it can't contain it.
1222 if (SLocOffset < Entry.getOffset()) return false;
Mike Stump1eb44332009-09-09 15:08:12 +00001223
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001224 // If this is the very last entry then it does.
1225 if (FID.ID == -2)
1226 return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001227
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001228 // If it is the last local entry, then it does if the location is local.
1229 if (static_cast<unsigned>(FID.ID+1) == LocalSLocEntryTable.size()) {
1230 return SLocOffset < NextLocalOffset;
1231 }
1232
1233 // Otherwise, the entry after it has to not include it. This works for both
1234 // local and loaded entries.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001235 return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001236 }
Mike Stump1eb44332009-09-09 15:08:12 +00001237
Ted Kremenek78d85f52007-10-30 21:08:08 +00001238 /// createFileID - Create a new fileID for the specified ContentCache and
1239 /// include position. This works regardless of whether the ContentCache
1240 /// corresponds to a file or some other input source.
Chris Lattner2b2453a2009-01-17 06:22:33 +00001241 FileID createFileID(const SrcMgr::ContentCache* File,
1242 SourceLocation IncludePos,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +00001243 SrcMgr::CharacteristicKind DirCharacter,
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001244 int LoadedID, unsigned LoadedOffset);
Mike Stump1eb44332009-09-09 15:08:12 +00001245
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001246 const SrcMgr::ContentCache *
1247 getOrCreateContentCache(const FileEntry *SourceFile);
Ted Kremenekc16c2082009-01-06 01:55:26 +00001248
Ted Kremenek78d85f52007-10-30 21:08:08 +00001249 /// createMemBufferContentCache - Create a new ContentCache for the specified
1250 /// memory buffer.
Mike Stump1eb44332009-09-09 15:08:12 +00001251 const SrcMgr::ContentCache*
Chris Lattner2b2453a2009-01-17 06:22:33 +00001252 createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
Mike Stump1eb44332009-09-09 15:08:12 +00001253
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001254 FileID getFileIDSlow(unsigned SLocOffset) const;
Douglas Gregorf62d43d2011-07-19 16:10:42 +00001255 FileID getFileIDLocal(unsigned SLocOffset) const;
1256 FileID getFileIDLoaded(unsigned SLocOffset) const;
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001257
Chandler Carruthf84ef952011-07-25 20:52:26 +00001258 SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
Chris Lattneraddb7972009-01-26 20:04:19 +00001259 SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1260
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001261 std::pair<FileID, unsigned>
Chandler Carruthe7b2b6e2011-07-25 20:52:32 +00001262 getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
Chris Lattnerde7aeef2009-01-26 00:43:02 +00001263 std::pair<FileID, unsigned>
1264 getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1265 unsigned Offset) const;
Argyrios Kyrtzidisd9d2b672011-08-21 23:33:04 +00001266 void computeMacroArgsCache(SrcMgr::ContentCache *Content, FileID FID);
Reid Spencer5f016e22007-07-11 17:01:13 +00001267};
1268
1269
1270} // end namespace clang
1271
1272#endif