• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 //===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
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"
18 #include "llvm/Support/Allocator.h"
19 #include "llvm/Support/DataTypes.h"
20 #include "llvm/ADT/PointerIntPair.h"
21 #include "llvm/ADT/PointerUnion.h"
22 #include "llvm/ADT/IntrusiveRefCntPtr.h"
23 #include "llvm/ADT/DenseMap.h"
24 #include "llvm/Support/MemoryBuffer.h"
25 #include <vector>
26 #include <cassert>
27 
28 namespace llvm {
29 class StringRef;
30 }
31 
32 namespace clang {
33 
34 class Diagnostic;
35 class SourceManager;
36 class FileManager;
37 class FileEntry;
38 class LineTableInfo;
39 class LangOptions;
40 
41 /// SrcMgr - Public enums and private classes that are part of the
42 /// SourceManager implementation.
43 ///
44 namespace SrcMgr {
45   /// CharacteristicKind - This is used to represent whether a file or directory
46   /// 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
49   /// FileInfos when a #pragma system_header is seen or various other cases.
50   ///
51   enum CharacteristicKind {
52     C_User, C_System, C_ExternCSystem
53   };
54 
55   /// ContentCache - One instance of this struct is kept for every file
56   /// loaded or used.  This object owns the MemoryBuffer object.
57   class ContentCache {
58     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 
65     /// Buffer - The actual buffer containing the characters from the input
66     /// file.  This is owned by the ContentCache object.
67     /// The bits indicate indicates whether the buffer is invalid.
68     mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer;
69 
70   public:
71     /// 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
74     /// the ContentCache encapsulates an imaginary text buffer.
75     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;
81 
82     /// 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.
85     unsigned *SourceLineCache;
86 
87     /// NumLines - The number of lines in this ContentCache.  This is only valid
88     /// if SourceLineCache is non-null.
89     unsigned NumLines;
90 
91     /// getBuffer - Returns the memory buffer for the associated content.
92     ///
93     /// \param Diag Object through which diagnostics will be emitted if the
94     /// buffer cannot be retrieved.
95     ///
96     /// \param Loc If specified, is the location that invalid file diagnostics
97     ///     will be emitted at.
98     ///
99     /// \param Invalid If non-NULL, will be set \c true if an error occurred.
100     const llvm::MemoryBuffer *getBuffer(Diagnostic &Diag,
101                                         const SourceManager &SM,
102                                         SourceLocation Loc = SourceLocation(),
103                                         bool *Invalid = 0) const;
104 
105     /// getSize - Returns the size of the content encapsulated by this
106     ///  ContentCache. This can be the size of the source file or the size of an
107     ///  arbitrary scratch buffer.  If the ContentCache encapsulates a source
108     ///  file this size is retrieved from the file's FileEntry.
109     unsigned getSize() const;
110 
111     /// getSizeBytesMapped - Returns the number of bytes actually mapped for
112     ///  this ContentCache.  This can be 0 if the MemBuffer was not actually
113     ///  instantiated.
114     unsigned getSizeBytesMapped() const;
115 
116     /// Returns the kind of memory used to back the memory buffer for
117     /// this content cache.  This is used for performance analysis.
118     llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
119 
setBuffer(const llvm::MemoryBuffer * B)120     void setBuffer(const llvm::MemoryBuffer *B) {
121       assert(!Buffer.getPointer() && "MemoryBuffer already set.");
122       Buffer.setPointer(B);
123       Buffer.setInt(false);
124     }
125 
126     /// \brief Get the underlying buffer, returning NULL if the buffer is not
127     /// yet available.
getRawBuffer()128     const llvm::MemoryBuffer *getRawBuffer() const {
129       return Buffer.getPointer();
130     }
131 
132     /// \brief Replace the existing buffer (which will be deleted)
133     /// with the given buffer.
134     void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false);
135 
136     /// \brief Determine whether the buffer itself is invalid.
isBufferInvalid()137     bool isBufferInvalid() const {
138       return Buffer.getInt() & InvalidFlag;
139     }
140 
141     /// \brief Determine whether the buffer should be freed.
shouldFreeBuffer()142     bool shouldFreeBuffer() const {
143       return (Buffer.getInt() & DoNotFreeFlag) == 0;
144     }
145 
146     ContentCache(const FileEntry *Ent = 0)
147       : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent),
148         SourceLineCache(0), NumLines(0) {}
149 
ContentCache(const FileEntry * Ent,const FileEntry * contentEnt)150     ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
151       : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt),
152         SourceLineCache(0), NumLines(0) {}
153 
154     ~ContentCache();
155 
156     /// The copy ctor does not allow copies where source object has either
157     ///  a non-NULL Buffer or SourceLineCache.  Ownership of allocated memory
158     ///  is not transferred, so this is a logical error.
ContentCache(const ContentCache & RHS)159     ContentCache(const ContentCache &RHS)
160       : Buffer(0, false), SourceLineCache(0)
161     {
162       OrigEntry = RHS.OrigEntry;
163       ContentsEntry = RHS.ContentsEntry;
164 
165       assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0
166               && "Passed ContentCache object cannot own a buffer.");
167 
168       NumLines = RHS.NumLines;
169     }
170 
171   private:
172     // Disable assignments.
173     ContentCache &operator=(const ContentCache& RHS);
174   };
175 
176   /// FileInfo - Information about a FileID, basically just the logical file
177   /// that it represents and include stack information.
178   ///
179   /// Each FileInfo has include stack information, indicating where it came
180   /// from.  This information encodes the #include chain that a token was
181   /// instantiated from.  The main include file has an invalid IncludeLoc.
182   ///
183   /// FileInfos contain a "ContentCache *", with the contents of the file.
184   ///
185   class FileInfo {
186     /// IncludeLoc - The location of the #include that brought in this file.
187     /// This is an invalid SLOC for the main file (top of the #include chain).
188     unsigned IncludeLoc;  // Really a SourceLocation
189 
190     /// Data - This contains the ContentCache* and the bits indicating the
191     /// characteristic of the file and whether it has #line info, all bitmangled
192     /// together.
193     uintptr_t Data;
194   public:
195     /// get - Return a FileInfo object.
get(SourceLocation IL,const ContentCache * Con,CharacteristicKind FileCharacter)196     static FileInfo get(SourceLocation IL, const ContentCache *Con,
197                         CharacteristicKind FileCharacter) {
198       FileInfo X;
199       X.IncludeLoc = IL.getRawEncoding();
200       X.Data = (uintptr_t)Con;
201       assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
202       assert((unsigned)FileCharacter < 4 && "invalid file character");
203       X.Data |= (unsigned)FileCharacter;
204       return X;
205     }
206 
getIncludeLoc()207     SourceLocation getIncludeLoc() const {
208       return SourceLocation::getFromRawEncoding(IncludeLoc);
209     }
getContentCache()210     const ContentCache* getContentCache() const {
211       return reinterpret_cast<const ContentCache*>(Data & ~7UL);
212     }
213 
214     /// getCharacteristic - Return whether this is a system header or not.
getFileCharacteristic()215     CharacteristicKind getFileCharacteristic() const {
216       return (CharacteristicKind)(Data & 3);
217     }
218 
219     /// hasLineDirectives - Return true if this FileID has #line directives in
220     /// it.
hasLineDirectives()221     bool hasLineDirectives() const { return (Data & 4) != 0; }
222 
223     /// setHasLineDirectives - Set the flag that indicates that this FileID has
224     /// line table entries associated with it.
setHasLineDirectives()225     void setHasLineDirectives() {
226       Data |= 4;
227     }
228   };
229 
230   /// InstantiationInfo - Each InstantiationInfo encodes the Instantiation
231   /// location - where the token was ultimately instantiated, and the
232   /// SpellingLoc - where the actual character data for the token came from.
233   class InstantiationInfo {
234      // Really these are all SourceLocations.
235 
236     /// SpellingLoc - Where the spelling for the token can be found.
237     unsigned SpellingLoc;
238 
239     /// InstantiationLocStart/InstantiationLocEnd - In a macro expansion, these
240     /// indicate the start and end of the instantiation.  In object-like macros,
241     /// these will be the same.  In a function-like macro instantiation, the
242     /// start will be the identifier and the end will be the ')'.  Finally, in
243     /// macro-argument instantitions, the end will be 'SourceLocation()', an
244     /// invalid location.
245     unsigned InstantiationLocStart, InstantiationLocEnd;
246 
247   public:
getSpellingLoc()248     SourceLocation getSpellingLoc() const {
249       return SourceLocation::getFromRawEncoding(SpellingLoc);
250     }
getInstantiationLocStart()251     SourceLocation getInstantiationLocStart() const {
252       return SourceLocation::getFromRawEncoding(InstantiationLocStart);
253     }
getInstantiationLocEnd()254     SourceLocation getInstantiationLocEnd() const {
255       SourceLocation EndLoc =
256         SourceLocation::getFromRawEncoding(InstantiationLocEnd);
257       return EndLoc.isInvalid() ? getInstantiationLocStart() : EndLoc;
258     }
259 
getInstantiationLocRange()260     std::pair<SourceLocation,SourceLocation> getInstantiationLocRange() const {
261       return std::make_pair(getInstantiationLocStart(),
262                             getInstantiationLocEnd());
263     }
264 
isMacroArgInstantiation()265     bool isMacroArgInstantiation() const {
266       // Note that this needs to return false for default constructed objects.
267       return getInstantiationLocStart().isValid() &&
268         SourceLocation::getFromRawEncoding(InstantiationLocEnd).isInvalid();
269     }
270 
271     /// create - Return a InstantiationInfo for an expansion. ILStart and
272     /// ILEnd specify the instantiation range (where the macro is expanded),
273     /// and SL specifies the spelling location (where the characters from the
274     /// token come from). All three can refer to normal File SLocs or
275     /// instantiation locations.
create(SourceLocation SL,SourceLocation ILStart,SourceLocation ILEnd)276     static InstantiationInfo create(SourceLocation SL,
277                                     SourceLocation ILStart,
278                                     SourceLocation ILEnd) {
279       InstantiationInfo X;
280       X.SpellingLoc = SL.getRawEncoding();
281       X.InstantiationLocStart = ILStart.getRawEncoding();
282       X.InstantiationLocEnd = ILEnd.getRawEncoding();
283       return X;
284     }
285 
286     /// createForMacroArg - Return a special InstantiationInfo for the
287     /// expansion of a macro argument into a function-like macro's body. IL
288     /// specifies the instantiation location (where the macro is expanded).
289     /// This doesn't need to be a range because a macro is always instantiated
290     /// at a macro parameter reference, and macro parameters are always exactly
291     /// one token. SL specifies the spelling location (where the characters
292     /// from the token come from). IL and SL can both refer to normal File
293     /// SLocs or instantiation locations.
294     ///
295     /// Given the code:
296     /// \code
297     ///   #define F(x) f(x)
298     ///   F(42);
299     /// \endcode
300     ///
301     /// When expanding '\c F(42)', the '\c x' would call this with an SL
302     /// pointing at '\c 42' anad an IL pointing at its location in the
303     /// definition of '\c F'.
createForMacroArg(SourceLocation SL,SourceLocation IL)304     static InstantiationInfo createForMacroArg(SourceLocation SL,
305                                                SourceLocation IL) {
306       // We store an intentionally invalid source location for the end of the
307       // instantiation range to mark that this is a macro argument instantation
308       // rather than a normal one.
309       return create(SL, IL, SourceLocation());
310     }
311   };
312 
313   /// SLocEntry - This is a discriminated union of FileInfo and
314   /// InstantiationInfo.  SourceManager keeps an array of these objects, and
315   /// they are uniquely identified by the FileID datatype.
316   class SLocEntry {
317     unsigned Offset;   // low bit is set for instantiation info.
318     union {
319       FileInfo File;
320       InstantiationInfo Instantiation;
321     };
322   public:
getOffset()323     unsigned getOffset() const { return Offset >> 1; }
324 
isInstantiation()325     bool isInstantiation() const { return Offset & 1; }
isFile()326     bool isFile() const { return !isInstantiation(); }
327 
getFile()328     const FileInfo &getFile() const {
329       assert(isFile() && "Not a file SLocEntry!");
330       return File;
331     }
332 
getInstantiation()333     const InstantiationInfo &getInstantiation() const {
334       assert(isInstantiation() && "Not an instantiation SLocEntry!");
335       return Instantiation;
336     }
337 
get(unsigned Offset,const FileInfo & FI)338     static SLocEntry get(unsigned Offset, const FileInfo &FI) {
339       SLocEntry E;
340       E.Offset = Offset << 1;
341       E.File = FI;
342       return E;
343     }
344 
get(unsigned Offset,const InstantiationInfo & II)345     static SLocEntry get(unsigned Offset, const InstantiationInfo &II) {
346       SLocEntry E;
347       E.Offset = (Offset << 1) | 1;
348       E.Instantiation = II;
349       return E;
350     }
351   };
352 }  // end SrcMgr namespace.
353 
354 /// \brief External source of source location entries.
355 class ExternalSLocEntrySource {
356 public:
357   virtual ~ExternalSLocEntrySource();
358 
359   /// \brief Read the source location entry with index ID, which will always be
360   /// less than -1.
361   ///
362   /// \returns true if an error occurred that prevented the source-location
363   /// entry from being loaded.
364   virtual bool ReadSLocEntry(int ID) = 0;
365 };
366 
367 
368 /// IsBeforeInTranslationUnitCache - This class holds the cache used by
369 /// isBeforeInTranslationUnit.  The cache structure is complex enough to be
370 /// worth breaking out of SourceManager.
371 class IsBeforeInTranslationUnitCache {
372   /// L/R QueryFID - These are the FID's of the cached query.  If these match up
373   /// with a subsequent query, the result can be reused.
374   FileID LQueryFID, RQueryFID;
375 
376   /// CommonFID - This is the file found in common between the two #include
377   /// traces.  It is the nearest common ancestor of the #include tree.
378   FileID CommonFID;
379 
380   /// L/R CommonOffset - This is the offset of the previous query in CommonFID.
381   /// Usually, this represents the location of the #include for QueryFID, but if
382   /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a
383   /// random token in the parent.
384   unsigned LCommonOffset, RCommonOffset;
385 public:
386 
387   /// isCacheValid - Return true if the currently cached values match up with
388   /// the specified LHS/RHS query.  If not, we can't use the cache.
isCacheValid(FileID LHS,FileID RHS)389   bool isCacheValid(FileID LHS, FileID RHS) const {
390     return LQueryFID == LHS && RQueryFID == RHS;
391   }
392 
393   /// getCachedResult - If the cache is valid, compute the result given the
394   /// specified offsets in the LHS/RHS FID's.
getCachedResult(unsigned LOffset,unsigned ROffset)395   bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
396     // If one of the query files is the common file, use the offset.  Otherwise,
397     // use the #include loc in the common file.
398     if (LQueryFID != CommonFID) LOffset = LCommonOffset;
399     if (RQueryFID != CommonFID) ROffset = RCommonOffset;
400     return LOffset < ROffset;
401   }
402 
403   // Set up a new query.
setQueryFIDs(FileID LHS,FileID RHS)404   void setQueryFIDs(FileID LHS, FileID RHS) {
405     LQueryFID = LHS;
406     RQueryFID = RHS;
407   }
408 
setCommonLoc(FileID commonFID,unsigned lCommonOffset,unsigned rCommonOffset)409   void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
410                     unsigned rCommonOffset) {
411     CommonFID = commonFID;
412     LCommonOffset = lCommonOffset;
413     RCommonOffset = rCommonOffset;
414   }
415 
416 };
417 
418 /// \brief This class handles loading and caching of source files into memory.
419 ///
420 /// This object owns the MemoryBuffer objects for all of the loaded
421 /// files and assigns unique FileID's for each unique #include chain.
422 ///
423 /// The SourceManager can be queried for information about SourceLocation
424 /// objects, turning them into either spelling or instantiation locations.
425 /// Spelling locations represent where the bytes corresponding to a token came
426 /// from and instantiation locations represent where the location is in the
427 /// user's view.  In the case of a macro expansion, for example, the spelling
428 /// location indicates  where the expanded token came from and the instantiation
429 /// location specifies where it was expanded.
430 class SourceManager : public llvm::RefCountedBase<SourceManager> {
431   /// \brief Diagnostic object.
432   Diagnostic &Diag;
433 
434   FileManager &FileMgr;
435 
436   mutable llvm::BumpPtrAllocator ContentCacheAlloc;
437 
438   /// FileInfos - Memoized information about all of the files tracked by this
439   /// SourceManager.  This set allows us to merge ContentCache entries based
440   /// on their FileEntry*.  All ContentCache objects will thus have unique,
441   /// non-null, FileEntry pointers.
442   llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
443 
444   /// \brief True if the ContentCache for files that are overriden by other
445   /// files, should report the original file name. Defaults to true.
446   bool OverridenFilesKeepOriginalName;
447 
448   /// \brief Files that have been overriden with the contents from another file.
449   llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
450 
451   /// MemBufferInfos - Information about various memory buffers that we have
452   /// read in.  All FileEntry* within the stored ContentCache objects are NULL,
453   /// as they do not refer to a file.
454   std::vector<SrcMgr::ContentCache*> MemBufferInfos;
455 
456   /// \brief The table of SLocEntries that are local to this module.
457   ///
458   /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid
459   /// instantiation.
460   std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable;
461 
462   /// \brief The table of SLocEntries that are loaded from other modules.
463   ///
464   /// Negative FileIDs are indexes into this table. To get from ID to an index,
465   /// use (-ID - 2).
466   std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable;
467 
468   /// \brief The starting offset of the next local SLocEntry.
469   ///
470   /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
471   unsigned NextLocalOffset;
472 
473   /// \brief The starting offset of the latest batch of loaded SLocEntries.
474   ///
475   /// This is LoadedSLocEntryTable.back().Offset, except that that entry might
476   /// not have been loaded, so that value would be unknown.
477   unsigned CurrentLoadedOffset;
478 
479   /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable
480   /// have already been loaded from the external source.
481   ///
482   /// Same indexing as LoadedSLocEntryTable.
483   std::vector<bool> SLocEntryLoaded;
484 
485   /// \brief An external source for source location entries.
486   ExternalSLocEntrySource *ExternalSLocEntries;
487 
488   /// LastFileIDLookup - This is a one-entry cache to speed up getFileID.
489   /// LastFileIDLookup records the last FileID looked up or created, because it
490   /// is very common to look up many tokens from the same file.
491   mutable FileID LastFileIDLookup;
492 
493   /// LineTable - This holds information for #line directives.  It is referenced
494   /// by indices from SLocEntryTable.
495   LineTableInfo *LineTable;
496 
497   /// LastLineNo - These ivars serve as a cache used in the getLineNumber
498   /// method which is used to speedup getLineNumber calls to nearby locations.
499   mutable FileID LastLineNoFileIDQuery;
500   mutable SrcMgr::ContentCache *LastLineNoContentCache;
501   mutable unsigned LastLineNoFilePos;
502   mutable unsigned LastLineNoResult;
503 
504   /// MainFileID - The file ID for the main source file of the translation unit.
505   FileID MainFileID;
506 
507   // Statistics for -print-stats.
508   mutable unsigned NumLinearScans, NumBinaryProbes;
509 
510   // Cache results for the isBeforeInTranslationUnit method.
511   mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache;
512 
513   // Cache for the "fake" buffer used for error-recovery purposes.
514   mutable llvm::MemoryBuffer *FakeBufferForRecovery;
515 
516   // SourceManager doesn't support copy construction.
517   explicit SourceManager(const SourceManager&);
518   void operator=(const SourceManager&);
519 public:
520   SourceManager(Diagnostic &Diag, FileManager &FileMgr);
521   ~SourceManager();
522 
523   void clearIDTables();
524 
getDiagnostics()525   Diagnostic &getDiagnostics() const { return Diag; }
526 
getFileManager()527   FileManager &getFileManager() const { return FileMgr; }
528 
529   /// \brief Set true if the SourceManager should report the original file name
530   /// for contents of files that were overriden by other files.Defaults to true.
setOverridenFilesKeepOriginalName(bool value)531   void setOverridenFilesKeepOriginalName(bool value) {
532     OverridenFilesKeepOriginalName = value;
533   }
534 
535   /// createMainFileIDForMembuffer - Create the FileID for a memory buffer
536   ///  that will represent the FileID for the main source.  One example
537   ///  of when this would be used is when the main source is read from STDIN.
createMainFileIDForMemBuffer(const llvm::MemoryBuffer * Buffer)538   FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) {
539     assert(MainFileID.isInvalid() && "MainFileID already set!");
540     MainFileID = createFileIDForMemBuffer(Buffer);
541     return MainFileID;
542   }
543 
544   //===--------------------------------------------------------------------===//
545   // MainFileID creation and querying methods.
546   //===--------------------------------------------------------------------===//
547 
548   /// getMainFileID - Returns the FileID of the main source file.
getMainFileID()549   FileID getMainFileID() const { return MainFileID; }
550 
551   /// createMainFileID - Create the FileID for the main source file.
createMainFileID(const FileEntry * SourceFile)552   FileID createMainFileID(const FileEntry *SourceFile) {
553     assert(MainFileID.isInvalid() && "MainFileID already set!");
554     MainFileID = createFileID(SourceFile, SourceLocation(), SrcMgr::C_User);
555     return MainFileID;
556   }
557 
558   /// \brief Set the file ID for the precompiled preamble, which is also the
559   /// main file.
SetPreambleFileID(FileID Preamble)560   void SetPreambleFileID(FileID Preamble) {
561     assert(MainFileID.isInvalid() && "MainFileID already set!");
562     MainFileID = Preamble;
563   }
564 
565   //===--------------------------------------------------------------------===//
566   // Methods to create new FileID's and instantiations.
567   //===--------------------------------------------------------------------===//
568 
569   /// createFileID - Create a new FileID that represents the specified file
570   /// being #included from the specified IncludePosition.  This translates NULL
571   /// into standard input.
572   FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
573                       SrcMgr::CharacteristicKind FileCharacter,
574                       int LoadedID = 0, unsigned LoadedOffset = 0) {
575     const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
576     assert(IR && "getOrCreateContentCache() cannot return NULL");
577     return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset);
578   }
579 
580   /// createFileIDForMemBuffer - Create a new FileID that represents the
581   /// specified memory buffer.  This does no caching of the buffer and takes
582   /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once.
583   FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer,
584                                   int LoadedID = 0, unsigned LoadedOffset = 0) {
585     return createFileID(createMemBufferContentCache(Buffer), SourceLocation(),
586                         SrcMgr::C_User, LoadedID, LoadedOffset);
587   }
588 
589   /// createMacroArgInstantiationLoc - Return a new SourceLocation that encodes
590   /// the fact that a token from SpellingLoc should actually be referenced from
591   /// InstantiationLoc, and that it represents the instantiation of a macro
592   /// argument into the function-like macro body.
593   SourceLocation createMacroArgInstantiationLoc(SourceLocation Loc,
594                                                 SourceLocation InstantiationLoc,
595                                                 unsigned TokLength);
596 
597   /// createInstantiationLoc - Return a new SourceLocation that encodes the fact
598   /// that a token from SpellingLoc should actually be referenced from
599   /// InstantiationLoc.
600   SourceLocation createInstantiationLoc(SourceLocation Loc,
601                                         SourceLocation InstantiationLocStart,
602                                         SourceLocation InstantiationLocEnd,
603                                         unsigned TokLength,
604                                         int LoadedID = 0,
605                                         unsigned LoadedOffset = 0);
606 
607   /// \brief Retrieve the memory buffer associated with the given file.
608   ///
609   /// \param Invalid If non-NULL, will be set \c true if an error
610   /// occurs while retrieving the memory buffer.
611   const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
612                                                    bool *Invalid = 0);
613 
614   /// \brief Override the contents of the given source file by providing an
615   /// already-allocated buffer.
616   ///
617   /// \param SourceFile the source file whose contents will be overriden.
618   ///
619   /// \param Buffer the memory buffer whose contents will be used as the
620   /// data in the given source file.
621   ///
622   /// \param DoNotFree If true, then the buffer will not be freed when the
623   /// source manager is destroyed.
624   void overrideFileContents(const FileEntry *SourceFile,
625                             const llvm::MemoryBuffer *Buffer,
626                             bool DoNotFree = false);
627 
628   /// \brief Override the the given source file with another one.
629   ///
630   /// \param SourceFile the source file which will be overriden.
631   ///
632   /// \param NewFile the file whose contents will be used as the
633   /// data instead of the contents of the given source file.
634   void overrideFileContents(const FileEntry *SourceFile,
635                             const FileEntry *NewFile);
636 
637   //===--------------------------------------------------------------------===//
638   // FileID manipulation methods.
639   //===--------------------------------------------------------------------===//
640 
641   /// getBuffer - Return the buffer for the specified FileID. If there is an
642   /// error opening this buffer the first time, this manufactures a temporary
643   /// buffer and returns a non-empty error string.
644   const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
645                                       bool *Invalid = 0) const {
646     bool MyInvalid = false;
647     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
648     if (MyInvalid || !Entry.isFile()) {
649       if (Invalid)
650         *Invalid = true;
651 
652       return getFakeBufferForRecovery();
653     }
654 
655     return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
656                                                         Invalid);
657   }
658 
659   const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const {
660     bool MyInvalid = false;
661     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
662     if (MyInvalid || !Entry.isFile()) {
663       if (Invalid)
664         *Invalid = true;
665 
666       return getFakeBufferForRecovery();
667     }
668 
669     return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
670                                                         SourceLocation(),
671                                                         Invalid);
672   }
673 
674   /// getFileEntryForID - Returns the FileEntry record for the provided FileID.
getFileEntryForID(FileID FID)675   const FileEntry *getFileEntryForID(FileID FID) const {
676     bool MyInvalid = false;
677     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
678     if (MyInvalid || !Entry.isFile())
679       return 0;
680 
681     return Entry.getFile().getContentCache()->OrigEntry;
682   }
683 
684   /// Returns the FileEntry record for the provided SLocEntry.
getFileEntryForSLocEntry(const SrcMgr::SLocEntry & sloc)685   const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
686   {
687     return sloc.getFile().getContentCache()->OrigEntry;
688   }
689 
690   /// getBufferData - Return a StringRef to the source buffer data for the
691   /// specified FileID.
692   ///
693   /// \param FID The file ID whose contents will be returned.
694   /// \param Invalid If non-NULL, will be set true if an error occurred.
695   llvm::StringRef getBufferData(FileID FID, bool *Invalid = 0) const;
696 
697 
698   //===--------------------------------------------------------------------===//
699   // SourceLocation manipulation methods.
700   //===--------------------------------------------------------------------===//
701 
702   /// getFileID - Return the FileID for a SourceLocation.  This is a very
703   /// hot method that is used for all SourceManager queries that start with a
704   /// SourceLocation object.  It is responsible for finding the entry in
705   /// SLocEntryTable which contains the specified location.
706   ///
getFileID(SourceLocation SpellingLoc)707   FileID getFileID(SourceLocation SpellingLoc) const {
708     unsigned SLocOffset = SpellingLoc.getOffset();
709 
710     // If our one-entry cache covers this offset, just return it.
711     if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
712       return LastFileIDLookup;
713 
714     return getFileIDSlow(SLocOffset);
715   }
716 
717   /// getLocForStartOfFile - Return the source location corresponding to the
718   /// first byte of the specified file.
getLocForStartOfFile(FileID FID)719   SourceLocation getLocForStartOfFile(FileID FID) const {
720     bool Invalid = false;
721     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
722     if (Invalid || !Entry.isFile())
723       return SourceLocation();
724 
725     unsigned FileOffset = Entry.getOffset();
726     return SourceLocation::getFileLoc(FileOffset);
727   }
728 
729   /// getInstantiationLoc - Given a SourceLocation object, return the
730   /// instantiation location referenced by the ID.
getInstantiationLoc(SourceLocation Loc)731   SourceLocation getInstantiationLoc(SourceLocation Loc) const {
732     // Handle the non-mapped case inline, defer to out of line code to handle
733     // instantiations.
734     if (Loc.isFileID()) return Loc;
735     return getInstantiationLocSlowCase(Loc);
736   }
737 
738   /// getImmediateInstantiationRange - Loc is required to be an instantiation
739   /// location.  Return the start/end of the instantiation information.
740   std::pair<SourceLocation,SourceLocation>
741   getImmediateInstantiationRange(SourceLocation Loc) const;
742 
743   /// getInstantiationRange - Given a SourceLocation object, return the
744   /// range of tokens covered by the instantiation in the ultimate file.
745   std::pair<SourceLocation,SourceLocation>
746   getInstantiationRange(SourceLocation Loc) const;
747 
748 
749   /// getSpellingLoc - Given a SourceLocation object, return the spelling
750   /// location referenced by the ID.  This is the place where the characters
751   /// that make up the lexed token can be found.
getSpellingLoc(SourceLocation Loc)752   SourceLocation getSpellingLoc(SourceLocation Loc) const {
753     // Handle the non-mapped case inline, defer to out of line code to handle
754     // instantiations.
755     if (Loc.isFileID()) return Loc;
756     return getSpellingLocSlowCase(Loc);
757   }
758 
759   /// getImmediateSpellingLoc - Given a SourceLocation object, return the
760   /// spelling location referenced by the ID.  This is the first level down
761   /// towards the place where the characters that make up the lexed token can be
762   /// found.  This should not generally be used by clients.
763   SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
764 
765   /// getDecomposedLoc - Decompose the specified location into a raw FileID +
766   /// Offset pair.  The first element is the FileID, the second is the
767   /// offset from the start of the buffer of the location.
getDecomposedLoc(SourceLocation Loc)768   std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
769     FileID FID = getFileID(Loc);
770     return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset());
771   }
772 
773   /// getDecomposedInstantiationLoc - Decompose the specified location into a
774   /// raw FileID + Offset pair.  If the location is an instantiation record,
775   /// walk through it until we find the final location instantiated.
776   std::pair<FileID, unsigned>
getDecomposedInstantiationLoc(SourceLocation Loc)777   getDecomposedInstantiationLoc(SourceLocation Loc) const {
778     FileID FID = getFileID(Loc);
779     const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
780 
781     unsigned Offset = Loc.getOffset()-E->getOffset();
782     if (Loc.isFileID())
783       return std::make_pair(FID, Offset);
784 
785     return getDecomposedInstantiationLocSlowCase(E);
786   }
787 
788   /// getDecomposedSpellingLoc - Decompose the specified location into a raw
789   /// FileID + Offset pair.  If the location is an instantiation record, walk
790   /// through it until we find its spelling record.
791   std::pair<FileID, unsigned>
getDecomposedSpellingLoc(SourceLocation Loc)792   getDecomposedSpellingLoc(SourceLocation Loc) const {
793     FileID FID = getFileID(Loc);
794     const SrcMgr::SLocEntry *E = &getSLocEntry(FID);
795 
796     unsigned Offset = Loc.getOffset()-E->getOffset();
797     if (Loc.isFileID())
798       return std::make_pair(FID, Offset);
799     return getDecomposedSpellingLocSlowCase(E, Offset);
800   }
801 
802   /// getFileOffset - This method returns the offset from the start
803   /// of the file that the specified SourceLocation represents. This is not very
804   /// meaningful for a macro ID.
getFileOffset(SourceLocation SpellingLoc)805   unsigned getFileOffset(SourceLocation SpellingLoc) const {
806     return getDecomposedLoc(SpellingLoc).second;
807   }
808 
809   /// isMacroArgInstantiation - This method tests whether the given source
810   /// location represents a macro argument's instantiation into the
811   /// function-like macro definition. Such source locations only appear inside
812   /// of the instantiation locations representing where a particular
813   /// function-like macro was expanded.
814   bool isMacroArgInstantiation(SourceLocation Loc) const;
815 
816   //===--------------------------------------------------------------------===//
817   // Queries about the code at a SourceLocation.
818   //===--------------------------------------------------------------------===//
819 
820   /// getCharacterData - Return a pointer to the start of the specified location
821   /// in the appropriate spelling MemoryBuffer.
822   ///
823   /// \param Invalid If non-NULL, will be set \c true if an error occurs.
824   const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const;
825 
826   /// getColumnNumber - Return the column # for the specified file position.
827   /// This is significantly cheaper to compute than the line number.  This
828   /// returns zero if the column number isn't known.  This may only be called on
829   /// a file sloc, so you must choose a spelling or instantiation location
830   /// before calling this method.
831   unsigned getColumnNumber(FileID FID, unsigned FilePos,
832                            bool *Invalid = 0) const;
833   unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
834   unsigned getInstantiationColumnNumber(SourceLocation Loc,
835                                         bool *Invalid = 0) const;
836   unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const;
837 
838 
839   /// getLineNumber - Given a SourceLocation, return the spelling line number
840   /// for the position indicated.  This requires building and caching a table of
841   /// line offsets for the MemoryBuffer, so this is not cheap: use only when
842   /// about to emit a diagnostic.
843   unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const;
844   unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
845   unsigned getInstantiationLineNumber(SourceLocation Loc,
846                                       bool *Invalid = 0) const;
847   unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const;
848 
849   /// Return the filename or buffer identifier of the buffer the location is in.
850   /// Note that this name does not respect #line directives.  Use getPresumedLoc
851   /// for normal clients.
852   const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const;
853 
854   /// getFileCharacteristic - return the file characteristic of the specified
855   /// source location, indicating whether this is a normal file, a system
856   /// header, or an "implicit extern C" system header.
857   ///
858   /// This state can be modified with flags on GNU linemarker directives like:
859   ///   # 4 "foo.h" 3
860   /// which changes all source locations in the current file after that to be
861   /// considered to be from a system header.
862   SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
863 
864   /// getPresumedLoc - This method returns the "presumed" location of a
865   /// SourceLocation specifies.  A "presumed location" can be modified by #line
866   /// or GNU line marker directives.  This provides a view on the data that a
867   /// user should see in diagnostics, for example.
868   ///
869   /// Note that a presumed location is always given as the instantiation point
870   /// of an instantiation location, not at the spelling location.
871   ///
872   /// \returns The presumed location of the specified SourceLocation. If the
873   /// presumed location cannot be calculate (e.g., because \p Loc is invalid
874   /// or the file containing \p Loc has changed on disk), returns an invalid
875   /// presumed location.
876   PresumedLoc getPresumedLoc(SourceLocation Loc) const;
877 
878   /// isFromSameFile - Returns true if both SourceLocations correspond to
879   ///  the same file.
isFromSameFile(SourceLocation Loc1,SourceLocation Loc2)880   bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
881     return getFileID(Loc1) == getFileID(Loc2);
882   }
883 
884   /// isFromMainFile - Returns true if the file of provided SourceLocation is
885   ///   the main file.
isFromMainFile(SourceLocation Loc)886   bool isFromMainFile(SourceLocation Loc) const {
887     return getFileID(Loc) == getMainFileID();
888   }
889 
890   /// isInSystemHeader - Returns if a SourceLocation is in a system header.
isInSystemHeader(SourceLocation Loc)891   bool isInSystemHeader(SourceLocation Loc) const {
892     return getFileCharacteristic(Loc) != SrcMgr::C_User;
893   }
894 
895   /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C"
896   /// system header.
isInExternCSystemHeader(SourceLocation Loc)897   bool isInExternCSystemHeader(SourceLocation Loc) const {
898     return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
899   }
900 
901   /// \brief Given a specific chunk of a FileID (FileID with offset+length),
902   /// returns true if \arg Loc is inside that chunk and sets relative offset
903   /// (offset of \arg Loc from beginning of chunk) to \arg relativeOffset.
904   bool isInFileID(SourceLocation Loc,
905                   FileID FID, unsigned offset, unsigned length,
906                   unsigned *relativeOffset = 0) const {
907     assert(!FID.isInvalid());
908     if (Loc.isInvalid())
909       return false;
910 
911     unsigned start = getSLocEntry(FID).getOffset() + offset;
912     unsigned end = start + length;
913 
914 #ifndef NDEBUG
915     // Make sure offset/length describe a chunk inside the given FileID.
916     unsigned NextOffset;
917     if (FID.ID == -2)
918       NextOffset = 1U << 31U;
919     else if (FID.ID+1 == (int)LocalSLocEntryTable.size())
920       NextOffset = getNextLocalOffset();
921     else
922       NextOffset = getSLocEntryByID(FID.ID+1).getOffset();
923     assert(start < NextOffset);
924     assert(end   < NextOffset);
925 #endif
926 
927     if (Loc.getOffset() >= start && Loc.getOffset() < end) {
928       if (relativeOffset)
929         *relativeOffset = Loc.getOffset() - start;
930       return true;
931     }
932 
933     return false;
934   }
935 
936   //===--------------------------------------------------------------------===//
937   // Line Table Manipulation Routines
938   //===--------------------------------------------------------------------===//
939 
940   /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
941   ///
942   unsigned getLineTableFilenameID(llvm::StringRef Str);
943 
944   /// AddLineNote - Add a line note to the line table for the FileID and offset
945   /// specified by Loc.  If FilenameID is -1, it is considered to be
946   /// unspecified.
947   void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
948   void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
949                    bool IsFileEntry, bool IsFileExit,
950                    bool IsSystemHeader, bool IsExternCHeader);
951 
952   /// \brief Determine if the source manager has a line table.
hasLineTable()953   bool hasLineTable() const { return LineTable != 0; }
954 
955   /// \brief Retrieve the stored line table.
956   LineTableInfo &getLineTable();
957 
958   //===--------------------------------------------------------------------===//
959   // Queries for performance analysis.
960   //===--------------------------------------------------------------------===//
961 
962   /// Return the total amount of physical memory allocated by the
963   /// ContentCache allocator.
getContentCacheSize()964   size_t getContentCacheSize() const {
965     return ContentCacheAlloc.getTotalMemory();
966   }
967 
968   struct MemoryBufferSizes {
969     const size_t malloc_bytes;
970     const size_t mmap_bytes;
971 
MemoryBufferSizesMemoryBufferSizes972     MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
973       : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
974   };
975 
976   /// Return the amount of memory used by memory buffers, breaking down
977   /// by heap-backed versus mmap'ed memory.
978   MemoryBufferSizes getMemoryBufferSizes() const;
979 
980   //===--------------------------------------------------------------------===//
981   // Other miscellaneous methods.
982   //===--------------------------------------------------------------------===//
983 
984   /// \brief Get the source location for the given file:line:col triplet.
985   ///
986   /// If the source file is included multiple times, the source location will
987   /// be based upon the first inclusion.
988   SourceLocation getLocation(const FileEntry *SourceFile,
989                              unsigned Line, unsigned Col);
990 
991   /// \brief Determines the order of 2 source locations in the translation unit.
992   ///
993   /// \returns true if LHS source location comes before RHS, false otherwise.
994   bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
995 
996   /// \brief Determines the order of 2 source locations in the "source location
997   /// address space".
isBeforeInSourceLocationOffset(SourceLocation LHS,SourceLocation RHS)998   bool isBeforeInSourceLocationOffset(SourceLocation LHS,
999                                       SourceLocation RHS) const {
1000     return isBeforeInSourceLocationOffset(LHS, RHS.getOffset());
1001   }
1002 
1003   /// \brief Determines the order of a source location and a source location
1004   /// offset in the "source location address space".
1005   ///
1006   /// Note that we always consider source locations loaded from
isBeforeInSourceLocationOffset(SourceLocation LHS,unsigned RHS)1007   bool isBeforeInSourceLocationOffset(SourceLocation LHS, unsigned RHS) const {
1008     unsigned LHSOffset = LHS.getOffset();
1009     bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1010     bool RHSLoaded = RHS >= CurrentLoadedOffset;
1011     if (LHSLoaded == RHSLoaded)
1012       return LHS.getOffset() < RHS;
1013 
1014     return LHSLoaded;
1015   }
1016 
1017   // Iterators over FileInfos.
1018   typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
1019       ::const_iterator fileinfo_iterator;
fileinfo_begin()1020   fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
fileinfo_end()1021   fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
hasFileInfo(const FileEntry * File)1022   bool hasFileInfo(const FileEntry *File) const {
1023     return FileInfos.find(File) != FileInfos.end();
1024   }
1025 
1026   /// PrintStats - Print statistics to stderr.
1027   ///
1028   void PrintStats() const;
1029 
1030   /// \brief Get the number of local SLocEntries we have.
local_sloc_entry_size()1031   unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
1032 
1033   /// \brief Get a local SLocEntry. This is exposed for indexing.
1034   const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index,
1035                                              bool *Invalid = 0) const {
1036     assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1037     return LocalSLocEntryTable[Index];
1038   }
1039 
1040   /// \brief Get the number of loaded SLocEntries we have.
loaded_sloc_entry_size()1041   unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
1042 
1043   /// \brief Get a loaded SLocEntry. This is exposed for indexing.
1044   const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index, bool *Invalid=0) const {
1045     assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1046     if (!SLocEntryLoaded[Index])
1047       ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2));
1048     return LoadedSLocEntryTable[Index];
1049   }
1050 
1051   const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const {
1052     return getSLocEntryByID(FID.ID);
1053   }
1054 
getNextLocalOffset()1055   unsigned getNextLocalOffset() const { return NextLocalOffset; }
1056 
setExternalSLocEntrySource(ExternalSLocEntrySource * Source)1057   void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1058     assert(LoadedSLocEntryTable.empty() &&
1059            "Invalidating existing loaded entries");
1060     ExternalSLocEntries = Source;
1061   }
1062 
1063   /// \brief Allocate a number of loaded SLocEntries, which will be actually
1064   /// loaded on demand from the external source.
1065   ///
1066   /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1067   /// in the global source view. The lowest ID and the base offset of the
1068   /// entries will be returned.
1069   std::pair<int, unsigned>
1070   AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
1071 
1072 private:
1073   const llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1074 
1075   /// \brief Get the entry with the given unwrapped FileID.
getSLocEntryByID(int ID)1076   const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const {
1077     assert(ID != -1 && "Using FileID sentinel value");
1078     if (ID < 0)
1079       return getLoadedSLocEntryByID(ID);
1080     return getLocalSLocEntry(static_cast<unsigned>(ID));
1081   }
1082 
getLoadedSLocEntryByID(int ID)1083   const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID) const {
1084     return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2));
1085   }
1086 
1087   /// createInstantiationLoc - Implements the common elements of storing an
1088   /// instantiation info struct into the SLocEntry table and producing a source
1089   /// location that refers to it.
1090   SourceLocation createInstantiationLocImpl(const SrcMgr::InstantiationInfo &II,
1091                                             unsigned TokLength,
1092                                             int LoadedID = 0,
1093                                             unsigned LoadedOffset = 0);
1094 
1095   /// isOffsetInFileID - Return true if the specified FileID contains the
1096   /// specified SourceLocation offset.  This is a very hot method.
isOffsetInFileID(FileID FID,unsigned SLocOffset)1097   inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1098     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1099     // If the entry is after the offset, it can't contain it.
1100     if (SLocOffset < Entry.getOffset()) return false;
1101 
1102     // If this is the very last entry then it does.
1103     if (FID.ID == -2)
1104       return true;
1105 
1106     // If it is the last local entry, then it does if the location is local.
1107     if (static_cast<unsigned>(FID.ID+1) == LocalSLocEntryTable.size()) {
1108       return SLocOffset < NextLocalOffset;
1109     }
1110 
1111     // Otherwise, the entry after it has to not include it. This works for both
1112     // local and loaded entries.
1113     return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset();
1114   }
1115 
1116   /// createFileID - Create a new fileID for the specified ContentCache and
1117   ///  include position.  This works regardless of whether the ContentCache
1118   ///  corresponds to a file or some other input source.
1119   FileID createFileID(const SrcMgr::ContentCache* File,
1120                       SourceLocation IncludePos,
1121                       SrcMgr::CharacteristicKind DirCharacter,
1122                       int LoadedID, unsigned LoadedOffset);
1123 
1124   const SrcMgr::ContentCache *
1125     getOrCreateContentCache(const FileEntry *SourceFile);
1126 
1127   /// createMemBufferContentCache - Create a new ContentCache for the specified
1128   ///  memory buffer.
1129   const SrcMgr::ContentCache*
1130   createMemBufferContentCache(const llvm::MemoryBuffer *Buf);
1131 
1132   FileID getFileIDSlow(unsigned SLocOffset) const;
1133   FileID getFileIDLocal(unsigned SLocOffset) const;
1134   FileID getFileIDLoaded(unsigned SLocOffset) const;
1135 
1136   SourceLocation getInstantiationLocSlowCase(SourceLocation Loc) const;
1137   SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1138 
1139   std::pair<FileID, unsigned>
1140   getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E) const;
1141   std::pair<FileID, unsigned>
1142   getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1143                                    unsigned Offset) const;
1144 };
1145 
1146 
1147 }  // end namespace clang
1148 
1149 #endif
1150