blob: 3d1fcd3dbb7c603451a41acec66efb28e583f58b [file] [log] [blame]
Reid Spencer5f016e22007-07-11 17:01:13 +00001//===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner0bc735f2007-12-29 19:59:25 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Reid Spencer5f016e22007-07-11 17:01:13 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file defines the SourceManager interface.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef LLVM_CLANG_SOURCEMANAGER_H
15#define LLVM_CLANG_SOURCEMANAGER_H
16
17#include "clang/Basic/SourceLocation.h"
Chris Lattner0d0bf8c2009-02-03 07:30:45 +000018#include "llvm/Support/Allocator.h"
Chandler Carruth9f8eb202009-10-26 01:37:10 +000019#include "llvm/System/DataTypes.h"
Douglas Gregoraea67db2010-03-15 22:54:52 +000020#include "llvm/ADT/PointerUnion.h"
Chris Lattner0d0bf8c2009-02-03 07:30:45 +000021#include "llvm/ADT/DenseMap.h"
Reid Spencer5f016e22007-07-11 17:01:13 +000022#include <vector>
Chris Lattner9dc62f02007-07-12 15:32:57 +000023#include <cassert>
Reid Spencer5f016e22007-07-11 17:01:13 +000024
25namespace llvm {
26class MemoryBuffer;
Douglas Gregoraea67db2010-03-15 22:54:52 +000027class StringRef;
Reid Spencer5f016e22007-07-11 17:01:13 +000028}
Mike Stump1eb44332009-09-09 15:08:12 +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;
Douglas Gregoraea67db2010-03-15 22:54:52 +000037
Chris Lattner0b9e7362008-09-26 21:18:42 +000038/// SrcMgr - Public enums and private classes that are part of the
39/// SourceManager implementation.
Reid Spencer5f016e22007-07-11 17:01:13 +000040///
41namespace SrcMgr {
Chris Lattner9d728512008-10-27 01:19:25 +000042 /// CharacteristicKind - This is used to represent whether a file or directory
Chris Lattner0b9e7362008-09-26 21:18:42 +000043 /// holds normal user code, system code, or system code which is implicitly
44 /// 'extern "C"' in C++ mode. Entire directories can be tagged with this
45 /// (this is maintained by DirectoryLookup and friends) as can specific
46 /// FileIDInfos when a #pragma system_header is seen or various other cases.
47 ///
Chris Lattner9d728512008-10-27 01:19:25 +000048 enum CharacteristicKind {
Chris Lattner0b9e7362008-09-26 21:18:42 +000049 C_User, C_System, C_ExternCSystem
50 };
Mike Stump1eb44332009-09-09 15:08:12 +000051
Ted Kremenek78d85f52007-10-30 21:08:08 +000052 /// ContentCache - Once instance of this struct is kept for every file
Chris Lattner06a062d2009-01-19 08:02:45 +000053 /// loaded or used. This object owns the MemoryBuffer object.
Ted Kremenekc16c2082009-01-06 01:55:26 +000054 class ContentCache {
55 /// Buffer - The actual buffer containing the characters from the input
56 /// file. This is owned by the ContentCache object.
Chris Lattner05816592009-01-17 03:54:16 +000057 mutable const llvm::MemoryBuffer *Buffer;
Ted Kremenekc16c2082009-01-06 01:55:26 +000058
59 public:
Ted Kremenek78d85f52007-10-30 21:08:08 +000060 /// Reference to the file entry. This reference does not own
61 /// the FileEntry object. It is possible for this to be NULL if
62 /// the ContentCache encapsulates an imaginary text buffer.
Chris Lattner05816592009-01-17 03:54:16 +000063 const FileEntry *Entry;
Mike Stump1eb44332009-09-09 15:08:12 +000064
Chris Lattner0d0bf8c2009-02-03 07:30:45 +000065 /// SourceLineCache - A bump pointer allocated array of offsets for each
66 /// source line. This is lazily computed. This is owned by the
67 /// SourceManager BumpPointerAllocator object.
Chris Lattner05816592009-01-17 03:54:16 +000068 unsigned *SourceLineCache;
Mike Stump1eb44332009-09-09 15:08:12 +000069
Ted Kremenekb6427f82007-12-04 18:59:28 +000070 /// NumLines - The number of lines in this ContentCache. This is only valid
71 /// if SourceLineCache is non-null.
Reid Spencer5f016e22007-07-11 17:01:13 +000072 unsigned NumLines;
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +000073
Douglas Gregor36c35ba2010-03-16 00:35:39 +000074 /// getBuffer - Returns the memory buffer for the associated content.
75 ///
76 /// \param Diag Object through which diagnostics will be emitted it the
77 /// buffer cannot be retrieved.
78 ///
79 /// \param Invalid If non-NULL, will be set \c true if an error occurred.
80 const llvm::MemoryBuffer *getBuffer(Diagnostic &Diag,
81 bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +000082
Ted Kremenekc16c2082009-01-06 01:55:26 +000083 /// getSize - Returns the size of the content encapsulated by this
84 /// ContentCache. This can be the size of the source file or the size of an
85 /// arbitrary scratch buffer. If the ContentCache encapsulates a source
86 /// file this size is retrieved from the file's FileEntry.
87 unsigned getSize() const;
Mike Stump1eb44332009-09-09 15:08:12 +000088
Ted Kremenekc16c2082009-01-06 01:55:26 +000089 /// getSizeBytesMapped - Returns the number of bytes actually mapped for
90 /// this ContentCache. This can be 0 if the MemBuffer was not actually
91 /// instantiated.
92 unsigned getSizeBytesMapped() const;
Mike Stump1eb44332009-09-09 15:08:12 +000093
Chris Lattner05816592009-01-17 03:54:16 +000094 void setBuffer(const llvm::MemoryBuffer *B) {
Ted Kremenekc16c2082009-01-06 01:55:26 +000095 assert(!Buffer && "MemoryBuffer already set.");
96 Buffer = B;
97 }
Mike Stump1eb44332009-09-09 15:08:12 +000098
Douglas Gregor29684422009-12-02 06:49:09 +000099 /// \brief Replace the existing buffer (which will be deleted)
100 /// with the given buffer.
101 void replaceBuffer(const llvm::MemoryBuffer *B);
102
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000103 ContentCache(const FileEntry *Ent = 0)
Douglas Gregor29684422009-12-02 06:49:09 +0000104 : Buffer(0), Entry(Ent), SourceLineCache(0), NumLines(0) {}
Ted Kremenek78d85f52007-10-30 21:08:08 +0000105
106 ~ContentCache();
Mike Stump1eb44332009-09-09 15:08:12 +0000107
Ted Kremenek0d892d82007-10-30 22:57:35 +0000108 /// The copy ctor does not allow copies where source object has either
109 /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory
110 /// is not transfered, so this is a logical error.
Douglas Gregor29684422009-12-02 06:49:09 +0000111 ContentCache(const ContentCache &RHS) : Buffer(0), SourceLineCache(0) {
Ted Kremenek0d892d82007-10-30 22:57:35 +0000112 Entry = RHS.Entry;
113
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000114 assert (RHS.Buffer == 0 && RHS.SourceLineCache == 0
Ted Kremenek0d892d82007-10-30 22:57:35 +0000115 && "Passed ContentCache object cannot own a buffer.");
Mike Stump1eb44332009-09-09 15:08:12 +0000116
117 NumLines = RHS.NumLines;
Ted Kremenek0d892d82007-10-30 22:57:35 +0000118 }
Mike Stump1eb44332009-09-09 15:08:12 +0000119
Ted Kremenek0d892d82007-10-30 22:57:35 +0000120 private:
121 // Disable assignments.
Mike Stump1eb44332009-09-09 15:08:12 +0000122 ContentCache &operator=(const ContentCache& RHS);
123 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000124
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000125 /// FileInfo - Information about a FileID, basically just the logical file
126 /// that it represents and include stack information.
Reid Spencer5f016e22007-07-11 17:01:13 +0000127 ///
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000128 /// Each FileInfo has include stack information, indicating where it came
129 /// from. This information encodes the #include chain that a token was
130 /// instantiated from. The main include file has an invalid IncludeLoc.
Reid Spencer5f016e22007-07-11 17:01:13 +0000131 ///
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000132 /// FileInfos contain a "ContentCache *", with the contents of the file.
Reid Spencer5f016e22007-07-11 17:01:13 +0000133 ///
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000134 class FileInfo {
Reid Spencer5f016e22007-07-11 17:01:13 +0000135 /// IncludeLoc - The location of the #include that brought in this file.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000136 /// This is an invalid SLOC for the main file (top of the #include chain).
137 unsigned IncludeLoc; // Really a SourceLocation
Mike Stump1eb44332009-09-09 15:08:12 +0000138
Chris Lattner6e1aff22009-01-26 06:49:09 +0000139 /// Data - This contains the ContentCache* and the bits indicating the
140 /// characteristic of the file and whether it has #line info, all bitmangled
141 /// together.
142 uintptr_t Data;
Ted Kremenek78d85f52007-10-30 21:08:08 +0000143 public:
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000144 /// get - Return a FileInfo object.
145 static FileInfo get(SourceLocation IL, const ContentCache *Con,
146 CharacteristicKind FileCharacter) {
147 FileInfo X;
148 X.IncludeLoc = IL.getRawEncoding();
Chris Lattner6e1aff22009-01-26 06:49:09 +0000149 X.Data = (uintptr_t)Con;
Chris Lattner00282d62009-02-03 07:41:46 +0000150 assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
Chris Lattner6e1aff22009-01-26 06:49:09 +0000151 assert((unsigned)FileCharacter < 4 && "invalid file character");
152 X.Data |= (unsigned)FileCharacter;
Reid Spencer5f016e22007-07-11 17:01:13 +0000153 return X;
154 }
Mike Stump1eb44332009-09-09 15:08:12 +0000155
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000156 SourceLocation getIncludeLoc() const {
157 return SourceLocation::getFromRawEncoding(IncludeLoc);
158 }
Chris Lattner6e1aff22009-01-26 06:49:09 +0000159 const ContentCache* getContentCache() const {
Chris Lattner00282d62009-02-03 07:41:46 +0000160 return reinterpret_cast<const ContentCache*>(Data & ~7UL);
Chris Lattner6e1aff22009-01-26 06:49:09 +0000161 }
Mike Stump1eb44332009-09-09 15:08:12 +0000162
Chris Lattner0b9e7362008-09-26 21:18:42 +0000163 /// getCharacteristic - Return whether this is a system header or not.
Mike Stump1eb44332009-09-09 15:08:12 +0000164 CharacteristicKind getFileCharacteristic() const {
Chris Lattner6e1aff22009-01-26 06:49:09 +0000165 return (CharacteristicKind)(Data & 3);
Chris Lattner0b9e7362008-09-26 21:18:42 +0000166 }
Chris Lattnerac50e342009-02-03 22:13:05 +0000167
168 /// hasLineDirectives - Return true if this FileID has #line directives in
169 /// it.
170 bool hasLineDirectives() const { return (Data & 4) != 0; }
Mike Stump1eb44332009-09-09 15:08:12 +0000171
Chris Lattnerac50e342009-02-03 22:13:05 +0000172 /// setHasLineDirectives - Set the flag that indicates that this FileID has
173 /// line table entries associated with it.
174 void setHasLineDirectives() {
175 Data |= 4;
176 }
Chris Lattner9dc1f532007-07-20 16:37:10 +0000177 };
Mike Stump1eb44332009-09-09 15:08:12 +0000178
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000179 /// InstantiationInfo - Each InstantiationInfo encodes the Instantiation
180 /// location - where the token was ultimately instantiated, and the
181 /// SpellingLoc - where the actual character data for the token came from.
182 class InstantiationInfo {
Chris Lattnere7fb4842009-02-15 20:52:18 +0000183 // Really these are all SourceLocations.
Mike Stump1eb44332009-09-09 15:08:12 +0000184
Chris Lattnere7fb4842009-02-15 20:52:18 +0000185 /// SpellingLoc - Where the spelling for the token can be found.
186 unsigned SpellingLoc;
Mike Stump1eb44332009-09-09 15:08:12 +0000187
Chris Lattnere7fb4842009-02-15 20:52:18 +0000188 /// InstantiationLocStart/InstantiationLocEnd - In a macro expansion, these
Douglas Gregor14f79002009-04-10 03:52:48 +0000189 /// indicate the start and end of the instantiation. In object-like macros,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000190 /// these will be the same. In a function-like macro instantiation, the
191 /// start will be the identifier and the end will be the ')'.
192 unsigned InstantiationLocStart, InstantiationLocEnd;
Chris Lattner9dc1f532007-07-20 16:37:10 +0000193 public:
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000194 SourceLocation getSpellingLoc() const {
195 return SourceLocation::getFromRawEncoding(SpellingLoc);
196 }
Chris Lattnere7fb4842009-02-15 20:52:18 +0000197 SourceLocation getInstantiationLocStart() const {
198 return SourceLocation::getFromRawEncoding(InstantiationLocStart);
199 }
200 SourceLocation getInstantiationLocEnd() const {
201 return SourceLocation::getFromRawEncoding(InstantiationLocEnd);
202 }
Mike Stump1eb44332009-09-09 15:08:12 +0000203
Chris Lattnere7fb4842009-02-15 20:52:18 +0000204 std::pair<SourceLocation,SourceLocation> getInstantiationLocRange() const {
205 return std::make_pair(getInstantiationLocStart(),
206 getInstantiationLocEnd());
207 }
Mike Stump1eb44332009-09-09 15:08:12 +0000208
Chris Lattnere7fb4842009-02-15 20:52:18 +0000209 /// get - Return a InstantiationInfo for an expansion. IL specifies
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000210 /// the instantiation location (where the macro is expanded), and SL
211 /// specifies the spelling location (where the characters from the token
Chris Lattnere7fb4842009-02-15 20:52:18 +0000212 /// come from). IL and PL can both refer to normal File SLocs or
213 /// instantiation locations.
214 static InstantiationInfo get(SourceLocation ILStart, SourceLocation ILEnd,
215 SourceLocation SL) {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000216 InstantiationInfo X;
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000217 X.SpellingLoc = SL.getRawEncoding();
Chris Lattnere7fb4842009-02-15 20:52:18 +0000218 X.InstantiationLocStart = ILStart.getRawEncoding();
219 X.InstantiationLocEnd = ILEnd.getRawEncoding();
Chris Lattner9dc1f532007-07-20 16:37:10 +0000220 return X;
Reid Spencer5f016e22007-07-11 17:01:13 +0000221 }
222 };
Mike Stump1eb44332009-09-09 15:08:12 +0000223
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000224 /// SLocEntry - This is a discriminated union of FileInfo and
225 /// InstantiationInfo. SourceManager keeps an array of these objects, and
226 /// they are uniquely identified by the FileID datatype.
227 class SLocEntry {
228 unsigned Offset; // low bit is set for instantiation info.
229 union {
230 FileInfo File;
231 InstantiationInfo Instantiation;
232 };
233 public:
234 unsigned getOffset() const { return Offset >> 1; }
Mike Stump1eb44332009-09-09 15:08:12 +0000235
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000236 bool isInstantiation() const { return Offset & 1; }
237 bool isFile() const { return !isInstantiation(); }
Mike Stump1eb44332009-09-09 15:08:12 +0000238
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000239 const FileInfo &getFile() const {
240 assert(isFile() && "Not a file SLocEntry!");
241 return File;
242 }
243
244 const InstantiationInfo &getInstantiation() const {
245 assert(isInstantiation() && "Not an instantiation SLocEntry!");
246 return Instantiation;
247 }
Mike Stump1eb44332009-09-09 15:08:12 +0000248
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000249 static SLocEntry get(unsigned Offset, const FileInfo &FI) {
250 SLocEntry E;
251 E.Offset = Offset << 1;
252 E.File = FI;
253 return E;
254 }
255
256 static SLocEntry get(unsigned Offset, const InstantiationInfo &II) {
257 SLocEntry E;
258 E.Offset = (Offset << 1) | 1;
259 E.Instantiation = II;
260 return E;
261 }
262 };
Reid Spencer5f016e22007-07-11 17:01:13 +0000263} // end SrcMgr namespace.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000264
265/// \brief External source of source location entries.
266class ExternalSLocEntrySource {
267public:
268 virtual ~ExternalSLocEntrySource();
269
270 /// \brief Read the source location entry with index ID.
271 virtual void ReadSLocEntry(unsigned ID) = 0;
272};
273
Reid Spencer5f016e22007-07-11 17:01:13 +0000274/// SourceManager - This file handles loading and caching of source files into
275/// memory. This object owns the MemoryBuffer objects for all of the loaded
276/// files and assigns unique FileID's for each unique #include chain.
277///
278/// The SourceManager can be queried for information about SourceLocation
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000279/// objects, turning them into either spelling or instantiation locations.
280/// Spelling locations represent where the bytes corresponding to a token came
281/// from and instantiation locations represent where the location is in the
282/// user's view. In the case of a macro expansion, for example, the spelling
283/// location indicates where the expanded token came from and the instantiation
284/// location specifies where it was expanded.
Reid Spencer5f016e22007-07-11 17:01:13 +0000285class SourceManager {
Douglas Gregorf715ca12010-03-16 00:06:06 +0000286 /// \brief Diagnostic object.
287 Diagnostic &Diag;
288
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000289 mutable llvm::BumpPtrAllocator ContentCacheAlloc;
Mike Stump1eb44332009-09-09 15:08:12 +0000290
Reid Spencer5f016e22007-07-11 17:01:13 +0000291 /// FileInfos - Memoized information about all of the files tracked by this
Ted Kremenek0d892d82007-10-30 22:57:35 +0000292 /// SourceManager. This set allows us to merge ContentCache entries based
293 /// on their FileEntry*. All ContentCache objects will thus have unique,
Mike Stump1eb44332009-09-09 15:08:12 +0000294 /// non-null, FileEntry pointers.
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000295 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000296
Reid Spencer5f016e22007-07-11 17:01:13 +0000297 /// MemBufferInfos - Information about various memory buffers that we have
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000298 /// read in. All FileEntry* within the stored ContentCache objects are NULL,
299 /// as they do not refer to a file.
300 std::vector<SrcMgr::ContentCache*> MemBufferInfos;
Mike Stump1eb44332009-09-09 15:08:12 +0000301
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000302 /// SLocEntryTable - This is an array of SLocEntry's that we have created.
303 /// FileID is an index into this vector. This array is sorted by the offset.
304 std::vector<SrcMgr::SLocEntry> SLocEntryTable;
305 /// NextOffset - This is the next available offset that a new SLocEntry can
306 /// start at. It is SLocEntryTable.back().getOffset()+size of back() entry.
307 unsigned NextOffset;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000308
309 /// \brief If source location entries are being lazily loaded from
310 /// an external source, this vector indicates whether the Ith source
311 /// location entry has already been loaded from the external storage.
312 std::vector<bool> SLocEntryLoaded;
313
314 /// \brief An external source for source location entries.
315 ExternalSLocEntrySource *ExternalSLocEntries;
316
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000317 /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
318 /// LastFileIDLookup records the last FileID looked up or created, because it
319 /// is very common to look up many tokens from the same file.
320 mutable FileID LastFileIDLookup;
Mike Stump1eb44332009-09-09 15:08:12 +0000321
Chris Lattner5b9a5042009-01-26 07:57:50 +0000322 /// LineTable - This holds information for #line directives. It is referenced
323 /// by indices from SLocEntryTable.
324 LineTableInfo *LineTable;
Mike Stump1eb44332009-09-09 15:08:12 +0000325
Chris Lattner5e36a7a2007-07-24 05:57:19 +0000326 /// LastLineNo - These ivars serve as a cache used in the getLineNumber
327 /// method which is used to speedup getLineNumber calls to nearby locations.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000328 mutable FileID LastLineNoFileIDQuery;
Chris Lattnerf812a452008-11-18 06:51:15 +0000329 mutable SrcMgr::ContentCache *LastLineNoContentCache;
330 mutable unsigned LastLineNoFilePos;
331 mutable unsigned LastLineNoResult;
Mike Stump1eb44332009-09-09 15:08:12 +0000332
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000333 /// MainFileID - The file ID for the main source file of the translation unit.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000334 FileID MainFileID;
Steve Naroff49c1f4a2008-02-02 00:10:46 +0000335
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000336 // Statistics for -print-stats.
337 mutable unsigned NumLinearScans, NumBinaryProbes;
Mike Stump1eb44332009-09-09 15:08:12 +0000338
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +0000339 // Cache results for the isBeforeInTranslationUnit method.
340 mutable FileID LastLFIDForBeforeTUCheck;
341 mutable FileID LastRFIDForBeforeTUCheck;
342 mutable bool LastResForBeforeTUCheck;
Mike Stump1eb44332009-09-09 15:08:12 +0000343
Steve Naroff49c1f4a2008-02-02 00:10:46 +0000344 // SourceManager doesn't support copy construction.
345 explicit SourceManager(const SourceManager&);
Mike Stump1eb44332009-09-09 15:08:12 +0000346 void operator=(const SourceManager&);
Reid Spencer5f016e22007-07-11 17:01:13 +0000347public:
Douglas Gregorf715ca12010-03-16 00:06:06 +0000348 SourceManager(Diagnostic &Diag)
349 : Diag(Diag), ExternalSLocEntries(0), LineTable(0), NumLinearScans(0),
Douglas Gregor29684422009-12-02 06:49:09 +0000350 NumBinaryProbes(0) {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000351 clearIDTables();
352 }
Chris Lattner5b9a5042009-01-26 07:57:50 +0000353 ~SourceManager();
Mike Stump1eb44332009-09-09 15:08:12 +0000354
Chris Lattner5b9a5042009-01-26 07:57:50 +0000355 void clearIDTables();
Mike Stump1eb44332009-09-09 15:08:12 +0000356
Chris Lattner06a062d2009-01-19 08:02:45 +0000357 //===--------------------------------------------------------------------===//
358 // MainFileID creation and querying methods.
359 //===--------------------------------------------------------------------===//
360
Ted Kremenek76edd0e2007-12-19 22:29:55 +0000361 /// getMainFileID - Returns the FileID of the main source file.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000362 FileID getMainFileID() const { return MainFileID; }
Mike Stump1eb44332009-09-09 15:08:12 +0000363
Chris Lattner06a062d2009-01-19 08:02:45 +0000364 /// createMainFileID - Create the FileID for the main source file.
365 FileID createMainFileID(const FileEntry *SourceFile,
366 SourceLocation IncludePos) {
367 assert(MainFileID.isInvalid() && "MainFileID already set!");
368 MainFileID = createFileID(SourceFile, IncludePos, SrcMgr::C_User);
369 return MainFileID;
370 }
Mike Stump1eb44332009-09-09 15:08:12 +0000371
Chris Lattner06a062d2009-01-19 08:02:45 +0000372 //===--------------------------------------------------------------------===//
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000373 // Methods to create new FileID's and instantiations.
Chris Lattner06a062d2009-01-19 08:02:45 +0000374 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000375
Reid Spencer5f016e22007-07-11 17:01:13 +0000376 /// createFileID - Create a new FileID that represents the specified file
377 /// being #included from the specified IncludePosition. This returns 0 on
378 /// error and translates NULL into standard input.
Mike Stump1eb44332009-09-09 15:08:12 +0000379 /// PreallocateID should be non-zero to specify which a pre-allocated,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000380 /// lazily computed source location is being filled in by this operation.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000381 FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000382 SrcMgr::CharacteristicKind FileCharacter,
383 unsigned PreallocatedID = 0,
384 unsigned Offset = 0) {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000385 const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000386 if (IR == 0) return FileID(); // Error opening file?
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000387 return createFileID(IR, IncludePos, FileCharacter, PreallocatedID, Offset);
Reid Spencer5f016e22007-07-11 17:01:13 +0000388 }
Mike Stump1eb44332009-09-09 15:08:12 +0000389
Reid Spencer5f016e22007-07-11 17:01:13 +0000390 /// createFileIDForMemBuffer - Create a new FileID that represents the
391 /// specified memory buffer. This does no caching of the buffer and takes
392 /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000393 FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
394 unsigned PreallocatedID = 0,
395 unsigned Offset = 0) {
Nico Weber7bfaaae2008-08-10 19:59:06 +0000396 return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000397 SrcMgr::C_User, PreallocatedID, Offset);
Reid Spencer5f016e22007-07-11 17:01:13 +0000398 }
Mike Stump1eb44332009-09-09 15:08:12 +0000399
Ted Kremenek1036b682007-12-19 23:48:45 +0000400 /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
401 /// that will represent the FileID for the main source. One example
402 /// of when this would be used is when the main source is read from STDIN.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000403 FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
404 assert(MainFileID.isInvalid() && "MainFileID already set!");
Chris Lattner7697b5c2007-12-20 01:38:17 +0000405 MainFileID = createFileIDForMemBuffer(Buffer);
Ted Kremenek1036b682007-12-19 23:48:45 +0000406 return MainFileID;
407 }
Chris Lattner06a062d2009-01-19 08:02:45 +0000408
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000409 /// createInstantiationLoc - Return a new SourceLocation that encodes the fact
410 /// that a token at Loc should actually be referenced from InstantiationLoc.
411 /// TokLength is the length of the token being instantiated.
412 SourceLocation createInstantiationLoc(SourceLocation Loc,
Chris Lattnere7fb4842009-02-15 20:52:18 +0000413 SourceLocation InstantiationLocStart,
414 SourceLocation InstantiationLocEnd,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000415 unsigned TokLength,
416 unsigned PreallocatedID = 0,
417 unsigned Offset = 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000418
Douglas Gregor29684422009-12-02 06:49:09 +0000419 /// \brief Retrieve the memory buffer associated with the given file.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000420 ///
421 /// \param Invalid If non-NULL, will be set \c true if an error
422 /// occurs while retrieving the memory buffer.
423 const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
424 bool *Invalid = 0);
Douglas Gregor29684422009-12-02 06:49:09 +0000425
426 /// \brief Override the contents of the given source file by providing an
427 /// already-allocated buffer.
428 ///
429 /// \param SourceFile the source file whose contents will be override.
430 ///
431 /// \param Buffer the memory buffer whose contents will be used as the
432 /// data in the given source file.
433 ///
434 /// \returns true if an error occurred, false otherwise.
435 bool overrideFileContents(const FileEntry *SourceFile,
436 const llvm::MemoryBuffer *Buffer);
437
Chris Lattner06a062d2009-01-19 08:02:45 +0000438 //===--------------------------------------------------------------------===//
439 // FileID manipulation methods.
440 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000441
Daniel Dunbar2ffb14f2009-12-06 09:19:25 +0000442 /// getBuffer - Return the buffer for the specified FileID. If there is an
443 /// error opening this buffer the first time, this manufactures a temporary
444 /// buffer and returns a non-empty error string.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000445 const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
446 return getSLocEntry(FID).getFile().getContentCache()->getBuffer(Diag,
447 Invalid);
Chris Lattner06a062d2009-01-19 08:02:45 +0000448 }
Mike Stump1eb44332009-09-09 15:08:12 +0000449
Chris Lattner06a062d2009-01-19 08:02:45 +0000450 /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
451 const FileEntry *getFileEntryForID(FileID FID) const {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000452 return getSLocEntry(FID).getFile().getContentCache()->Entry;
Chris Lattner06a062d2009-01-19 08:02:45 +0000453 }
Mike Stump1eb44332009-09-09 15:08:12 +0000454
Chris Lattner06a062d2009-01-19 08:02:45 +0000455 /// getBufferData - Return a pointer to the start and end of the source buffer
456 /// data for the specified FileID.
Douglas Gregoraea67db2010-03-15 22:54:52 +0000457 ///
Douglas Gregorf715ca12010-03-16 00:06:06 +0000458 /// \param FID The file ID whose contents will be returned.
459 /// \param Invalid If non-NULL, will be set true if an error occurred.
Benjamin Kramerf6ac97b2010-03-16 14:14:31 +0000460 llvm::StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
461
Mike Stump1eb44332009-09-09 15:08:12 +0000462
Chris Lattner06a062d2009-01-19 08:02:45 +0000463 //===--------------------------------------------------------------------===//
464 // SourceLocation manipulation methods.
465 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000466
Chris Lattner668ab1a2009-03-13 01:05:57 +0000467 /// getFileID - Return the FileID for a SourceLocation. This is a very
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000468 /// hot method that is used for all SourceManager queries that start with a
469 /// SourceLocation object. It is responsible for finding the entry in
470 /// SLocEntryTable which contains the specified location.
471 ///
472 FileID getFileID(SourceLocation SpellingLoc) const {
473 unsigned SLocOffset = SpellingLoc.getOffset();
Mike Stump1eb44332009-09-09 15:08:12 +0000474
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000475 // If our one-entry cache covers this offset, just return it.
476 if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
477 return LastFileIDLookup;
478
479 return getFileIDSlow(SLocOffset);
480 }
Mike Stump1eb44332009-09-09 15:08:12 +0000481
Chris Lattner2b2453a2009-01-17 06:22:33 +0000482 /// getLocForStartOfFile - Return the source location corresponding to the
483 /// first byte of the specified file.
484 SourceLocation getLocForStartOfFile(FileID FID) const {
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000485 assert(FID.ID < SLocEntryTable.size() && "FileID out of range");
486 assert(getSLocEntry(FID).isFile() && "FileID is not a file");
487 unsigned FileOffset = getSLocEntry(FID).getOffset();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000488 return SourceLocation::getFileLoc(FileOffset);
Chris Lattner2b2453a2009-01-17 06:22:33 +0000489 }
Mike Stump1eb44332009-09-09 15:08:12 +0000490
Chris Lattner66781332009-02-15 21:26:50 +0000491 /// getInstantiationLoc - Given a SourceLocation object, return the
492 /// instantiation location referenced by the ID.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000493 SourceLocation getInstantiationLoc(SourceLocation Loc) const {
Chris Lattneraddb7972009-01-26 20:04:19 +0000494 // Handle the non-mapped case inline, defer to out of line code to handle
495 // instantiations.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000496 if (Loc.isFileID()) return Loc;
Chris Lattneraddb7972009-01-26 20:04:19 +0000497 return getInstantiationLocSlowCase(Loc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000498 }
Mike Stump1eb44332009-09-09 15:08:12 +0000499
Chris Lattnere7fb4842009-02-15 20:52:18 +0000500 /// getImmediateInstantiationRange - Loc is required to be an instantiation
501 /// location. Return the start/end of the instantiation information.
502 std::pair<SourceLocation,SourceLocation>
503 getImmediateInstantiationRange(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000504
Chris Lattner66781332009-02-15 21:26:50 +0000505 /// getInstantiationRange - Given a SourceLocation object, return the
506 /// range of tokens covered by the instantiation in the ultimate file.
507 std::pair<SourceLocation,SourceLocation>
508 getInstantiationRange(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000509
510
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000511 /// getSpellingLoc - Given a SourceLocation object, return the spelling
512 /// location referenced by the ID. This is the place where the characters
513 /// that make up the lexed token can be found.
514 SourceLocation getSpellingLoc(SourceLocation Loc) const {
Chris Lattneraddb7972009-01-26 20:04:19 +0000515 // Handle the non-mapped case inline, defer to out of line code to handle
516 // instantiations.
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000517 if (Loc.isFileID()) return Loc;
Chris Lattneraddb7972009-01-26 20:04:19 +0000518 return getSpellingLocSlowCase(Loc);
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000519 }
Mike Stump1eb44332009-09-09 15:08:12 +0000520
Chris Lattner387616e2009-02-17 08:04:48 +0000521 /// getImmediateSpellingLoc - Given a SourceLocation object, return the
522 /// spelling location referenced by the ID. This is the first level down
523 /// towards the place where the characters that make up the lexed token can be
524 /// found. This should not generally be used by clients.
Mike Stump1eb44332009-09-09 15:08:12 +0000525 SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000526
527 /// getDecomposedLoc - Decompose the specified location into a raw FileID +
528 /// Offset pair. The first element is the FileID, the second is the
529 /// offset from the start of the buffer of the location.
530 std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
531 FileID FID = getFileID(Loc);
532 return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset());
533 }
Mike Stump1eb44332009-09-09 15:08:12 +0000534
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000535 /// getDecomposedInstantiationLoc - Decompose the specified location into a
536 /// raw FileID + Offset pair. If the location is an instantiation record,
537 /// walk through it until we find the final location instantiated.
538 std::pair<FileID, unsigned>
539 getDecomposedInstantiationLoc(SourceLocation Loc) const {
540 FileID FID = getFileID(Loc);
541 const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000542
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000543 unsigned Offset = Loc.getOffset()-E->getOffset();
544 if (Loc.isFileID())
545 return std::make_pair(FID, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000546
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000547 return getDecomposedInstantiationLocSlowCase(E, Offset);
548 }
549
550 /// getDecomposedSpellingLoc - Decompose the specified location into a raw
551 /// FileID + Offset pair. If the location is an instantiation record, walk
552 /// through it until we find its spelling record.
553 std::pair<FileID, unsigned>
554 getDecomposedSpellingLoc(SourceLocation Loc) const {
555 FileID FID = getFileID(Loc);
556 const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
Mike Stump1eb44332009-09-09 15:08:12 +0000557
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000558 unsigned Offset = Loc.getOffset()-E->getOffset();
559 if (Loc.isFileID())
560 return std::make_pair(FID, Offset);
561 return getDecomposedSpellingLocSlowCase(E, Offset);
Mike Stump1eb44332009-09-09 15:08:12 +0000562 }
563
Chris Lattner52c29082009-01-27 06:27:13 +0000564 /// getFileOffset - This method returns the offset from the start
565 /// of the file that the specified SourceLocation represents. This is not very
566 /// meaningful for a macro ID.
567 unsigned getFileOffset(SourceLocation SpellingLoc) const {
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000568 return getDecomposedLoc(SpellingLoc).second;
569 }
Mike Stump1eb44332009-09-09 15:08:12 +0000570
571
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000572 //===--------------------------------------------------------------------===//
573 // Queries about the code at a SourceLocation.
574 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000575
Reid Spencer5f016e22007-07-11 17:01:13 +0000576 /// getCharacterData - Return a pointer to the start of the specified location
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000577 /// in the appropriate spelling MemoryBuffer.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000578 ///
579 /// \param Invalid If non-NULL, will be set \c true if an error occurs.
580 const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000581
Chris Lattner9dc1f532007-07-20 16:37:10 +0000582 /// getColumnNumber - Return the column # for the specified file position.
583 /// This is significantly cheaper to compute than the line number. This
584 /// returns zero if the column number isn't known. This may only be called on
Chris Lattnerf7cf85b2009-01-16 07:36:28 +0000585 /// a file sloc, so you must choose a spelling or instantiation location
586 /// before calling this method.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000587 unsigned getColumnNumber(FileID FID, unsigned FilePos,
588 bool *Invalid = 0) const;
589 unsigned getSpellingColumnNumber(SourceLocation Loc,
590 bool *Invalid = 0) const;
591 unsigned getInstantiationColumnNumber(SourceLocation Loc,
592 bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000593
594
Chris Lattnerdf7c17a2009-01-16 07:00:02 +0000595 /// getLineNumber - Given a SourceLocation, return the spelling line number
Reid Spencer5f016e22007-07-11 17:01:13 +0000596 /// for the position indicated. This requires building and caching a table of
597 /// line offsets for the MemoryBuffer, so this is not cheap: use only when
598 /// about to emit a diagnostic.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000599 unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000600
Douglas Gregor50f6af72010-03-16 05:20:39 +0000601 unsigned getInstantiationLineNumber(SourceLocation Loc,
602 bool *Invalid = 0) const;
603 unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000604
Chris Lattnerbff5c512009-02-17 08:39:06 +0000605 /// Return the filename or buffer identifier of the buffer the location is in.
606 /// Note that this name does not respect #line directives. Use getPresumedLoc
607 /// for normal clients.
Douglas Gregor50f6af72010-03-16 05:20:39 +0000608 const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000609
Chris Lattner6b306672009-02-04 05:33:01 +0000610 /// getFileCharacteristic - return the file characteristic of the specified
Mike Stump1eb44332009-09-09 15:08:12 +0000611 /// source location, indicating whether this is a normal file, a system
Chris Lattner6b306672009-02-04 05:33:01 +0000612 /// header, or an "implicit extern C" system header.
613 ///
614 /// This state can be modified with flags on GNU linemarker directives like:
615 /// # 4 "foo.h" 3
616 /// which changes all source locations in the current file after that to be
617 /// considered to be from a system header.
618 SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000619
Chris Lattnerb9c3f962009-01-27 07:57:44 +0000620 /// getPresumedLoc - This method returns the "presumed" location of a
621 /// SourceLocation specifies. A "presumed location" can be modified by #line
622 /// or GNU line marker directives. This provides a view on the data that a
623 /// user should see in diagnostics, for example.
624 ///
625 /// Note that a presumed location is always given as the instantiation point
626 /// of an instantiation location, not at the spelling location.
627 PresumedLoc getPresumedLoc(SourceLocation Loc) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000628
Ted Kremenek9fd87b12008-04-14 21:04:18 +0000629 /// isFromSameFile - Returns true if both SourceLocations correspond to
630 /// the same file.
631 bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
Chris Lattnera11d6172009-01-19 07:46:45 +0000632 return getFileID(Loc1) == getFileID(Loc2);
Ted Kremenek9fd87b12008-04-14 21:04:18 +0000633 }
Mike Stump1eb44332009-09-09 15:08:12 +0000634
Ted Kremenek9fd87b12008-04-14 21:04:18 +0000635 /// isFromMainFile - Returns true if the file of provided SourceLocation is
636 /// the main file.
637 bool isFromMainFile(SourceLocation Loc) const {
Chris Lattnera11d6172009-01-19 07:46:45 +0000638 return getFileID(Loc) == getMainFileID();
Mike Stump1eb44332009-09-09 15:08:12 +0000639 }
640
Nico Weber7bfaaae2008-08-10 19:59:06 +0000641 /// isInSystemHeader - Returns if a SourceLocation is in a system header.
642 bool isInSystemHeader(SourceLocation Loc) const {
Chris Lattner0b9e7362008-09-26 21:18:42 +0000643 return getFileCharacteristic(Loc) != SrcMgr::C_User;
Nico Weber7bfaaae2008-08-10 19:59:06 +0000644 }
Mike Stump1eb44332009-09-09 15:08:12 +0000645
Chris Lattner0d456582009-06-13 23:31:51 +0000646 /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
647 /// system header.
648 bool isInExternCSystemHeader(SourceLocation Loc) const {
649 return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
650 }
Mike Stump1eb44332009-09-09 15:08:12 +0000651
Chris Lattner06a062d2009-01-19 08:02:45 +0000652 //===--------------------------------------------------------------------===//
Chris Lattner5b9a5042009-01-26 07:57:50 +0000653 // Line Table Manipulation Routines
654 //===--------------------------------------------------------------------===//
Mike Stump1eb44332009-09-09 15:08:12 +0000655
Chris Lattner5b9a5042009-01-26 07:57:50 +0000656 /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
Mike Stump1eb44332009-09-09 15:08:12 +0000657 ///
Chris Lattner5b9a5042009-01-26 07:57:50 +0000658 unsigned getLineTableFilenameID(const char *Ptr, unsigned Len);
Mike Stump1eb44332009-09-09 15:08:12 +0000659
Chris Lattner4c4ea172009-02-03 21:52:55 +0000660 /// AddLineNote - Add a line note to the line table for the FileID and offset
661 /// specified by Loc. If FilenameID is -1, it is considered to be
662 /// unspecified.
663 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
Chris Lattner9d79eba2009-02-04 05:21:58 +0000664 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
Mike Stump1eb44332009-09-09 15:08:12 +0000665 bool IsFileEntry, bool IsFileExit,
Chris Lattner9d79eba2009-02-04 05:21:58 +0000666 bool IsSystemHeader, bool IsExternCHeader);
Douglas Gregorbd945002009-04-13 16:31:14 +0000667
668 /// \brief Determine if the source manager has a line table.
669 bool hasLineTable() const { return LineTable != 0; }
670
671 /// \brief Retrieve the stored line table.
672 LineTableInfo &getLineTable();
673
Chris Lattner5b9a5042009-01-26 07:57:50 +0000674 //===--------------------------------------------------------------------===//
Chris Lattner06a062d2009-01-19 08:02:45 +0000675 // Other miscellaneous methods.
676 //===--------------------------------------------------------------------===//
Argyrios Kyrtzidis10b46d22009-06-20 08:09:57 +0000677
678 /// \brief Get the source location for the given file:line:col triplet.
679 ///
680 /// If the source file is included multiple times, the source location will
681 /// be based upon the first inclusion.
682 SourceLocation getLocation(const FileEntry *SourceFile,
683 unsigned Line, unsigned Col) const;
Mike Stump1eb44332009-09-09 15:08:12 +0000684
Argyrios Kyrtzidis2aa03d52009-06-23 22:01:48 +0000685 /// \brief Determines the order of 2 source locations in the translation unit.
686 ///
687 /// \returns true if LHS source location comes before RHS, false otherwise.
688 bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
689
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000690 // Iterators over FileInfos.
Chris Lattner0d0bf8c2009-02-03 07:30:45 +0000691 typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
692 ::const_iterator fileinfo_iterator;
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000693 fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
694 fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
Douglas Gregord93256e2010-01-28 06:00:51 +0000695 bool hasFileInfo(const FileEntry *File) const {
696 return FileInfos.find(File) != FileInfos.end();
697 }
Chris Lattnerc6fe32a2009-01-17 03:48:08 +0000698
Reid Spencer5f016e22007-07-11 17:01:13 +0000699 /// PrintStats - Print statistics to stderr.
700 ///
701 void PrintStats() const;
Reid Spencer5f016e22007-07-11 17:01:13 +0000702
Douglas Gregor14f79002009-04-10 03:52:48 +0000703 unsigned sloc_entry_size() const { return SLocEntryTable.size(); }
Kovarththanan Rajaratnam2d684c22010-03-15 17:31:29 +0000704
Ted Kremenek16b55a72010-01-26 19:31:51 +0000705 // FIXME: Exposing this is a little gross; what we want is a good way
706 // to iterate the entries that were not defined in a PCH file (or
707 // any other external source).
708 unsigned sloc_loaded_entry_size() const { return SLocEntryLoaded.size(); }
Douglas Gregor14f79002009-04-10 03:52:48 +0000709
Douglas Gregorbdfe48a2009-10-16 22:46:09 +0000710 const SrcMgr::SLocEntry &getSLocEntry(unsigned ID) const {
711 assert(ID < SLocEntryTable.size() && "Invalid id");
Mike Stump1eb44332009-09-09 15:08:12 +0000712 if (ExternalSLocEntries &&
Douglas Gregorbdfe48a2009-10-16 22:46:09 +0000713 ID < SLocEntryLoaded.size() &&
714 !SLocEntryLoaded[ID])
715 ExternalSLocEntries->ReadSLocEntry(ID);
716 return SLocEntryTable[ID];
717 }
Kovarththanan Rajaratnam2d684c22010-03-15 17:31:29 +0000718
719 const SrcMgr::SLocEntry &getSLocEntry(FileID FID) const {
Douglas Gregorbdfe48a2009-10-16 22:46:09 +0000720 return getSLocEntry(FID.ID);
Douglas Gregorbd945002009-04-13 16:31:14 +0000721 }
722
Douglas Gregorf60e9912009-04-15 18:05:10 +0000723 unsigned getNextOffset() const { return NextOffset; }
724
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000725 /// \brief Preallocate some number of source location entries, which
726 /// will be loaded as needed from the given external source.
727 void PreallocateSLocEntries(ExternalSLocEntrySource *Source,
728 unsigned NumSLocEntries,
729 unsigned NextOffset);
730
Douglas Gregor2bf1eb02009-04-27 21:28:04 +0000731 /// \brief Clear out any preallocated source location entries that
732 /// haven't already been loaded.
733 void ClearPreallocatedSLocEntries();
734
Ted Kremenek78d85f52007-10-30 21:08:08 +0000735private:
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000736 /// isOffsetInFileID - Return true if the specified FileID contains the
737 /// specified SourceLocation offset. This is a very hot method.
738 inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
739 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
740 // If the entry is after the offset, it can't contain it.
741 if (SLocOffset < Entry.getOffset()) return false;
Mike Stump1eb44332009-09-09 15:08:12 +0000742
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000743 // If this is the last entry than it does. Otherwise, the entry after it
744 // has to not include it.
745 if (FID.ID+1 == SLocEntryTable.size()) return true;
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000746
747 return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000748 }
Mike Stump1eb44332009-09-09 15:08:12 +0000749
Ted Kremenek78d85f52007-10-30 21:08:08 +0000750 /// createFileID - Create a new fileID for the specified ContentCache and
751 /// include position. This works regardless of whether the ContentCache
752 /// corresponds to a file or some other input source.
Chris Lattner2b2453a2009-01-17 06:22:33 +0000753 FileID createFileID(const SrcMgr::ContentCache* File,
754 SourceLocation IncludePos,
Douglas Gregor7f94b0b2009-04-27 06:38:32 +0000755 SrcMgr::CharacteristicKind DirCharacter,
756 unsigned PreallocatedID = 0,
757 unsigned Offset = 0);
Mike Stump1eb44332009-09-09 15:08:12 +0000758
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000759 const SrcMgr::ContentCache *
760 getOrCreateContentCache(const FileEntry *SourceFile);
Ted Kremenekc16c2082009-01-06 01:55:26 +0000761
Ted Kremenek78d85f52007-10-30 21:08:08 +0000762 /// createMemBufferContentCache - Create a new ContentCache for the specified
763 /// memory buffer.
Mike Stump1eb44332009-09-09 15:08:12 +0000764 const SrcMgr::ContentCache*
Chris Lattner2b2453a2009-01-17 06:22:33 +0000765 createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
Mike Stump1eb44332009-09-09 15:08:12 +0000766
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000767 FileID getFileIDSlow(unsigned SLocOffset) const;
768
Chris Lattneraddb7972009-01-26 20:04:19 +0000769 SourceLocation getInstantiationLocSlowCase(SourceLocation Loc) const;
770 SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
771
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000772 std::pair<FileID, unsigned>
773 getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
Mike Stump1eb44332009-09-09 15:08:12 +0000774 unsigned Offset) const;
Chris Lattnerde7aeef2009-01-26 00:43:02 +0000775 std::pair<FileID, unsigned>
776 getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
777 unsigned Offset) const;
Reid Spencer5f016e22007-07-11 17:01:13 +0000778};
779
780
781} // end namespace clang
782
783#endif