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 /// \file
11 /// \brief Defines the SourceManager interface.
12 ///
13 /// There are three different types of locations in a %file: a spelling
14 /// location, an expansion location, and a presumed location.
15 ///
16 /// Given an example of:
17 /// \code
18 /// #define min(x, y) x < y ? x : y
19 /// \endcode
20 ///
21 /// and then later on a use of min:
22 /// \code
23 /// #line 17
24 /// return min(a, b);
25 /// \endcode
26 ///
27 /// The expansion location is the line in the source code where the macro
28 /// was expanded (the return statement), the spelling location is the
29 /// location in the source where the macro was originally defined,
30 /// and the presumed location is where the line directive states that
31 /// the line is 17, or any other line.
32 ///
33 //===----------------------------------------------------------------------===//
34 
35 #ifndef LLVM_CLANG_BASIC_SOURCEMANAGER_H
36 #define LLVM_CLANG_BASIC_SOURCEMANAGER_H
37 
38 #include "clang/Basic/FileManager.h"
39 #include "clang/Basic/LLVM.h"
40 #include "clang/Basic/SourceLocation.h"
41 #include "llvm/ADT/ArrayRef.h"
42 #include "llvm/ADT/BitVector.h"
43 #include "llvm/ADT/DenseMap.h"
44 #include "llvm/ADT/DenseSet.h"
45 #include "llvm/ADT/IntrusiveRefCntPtr.h"
46 #include "llvm/ADT/PointerIntPair.h"
47 #include "llvm/ADT/PointerUnion.h"
48 #include "llvm/Support/AlignOf.h"
49 #include "llvm/Support/Allocator.h"
50 #include "llvm/Support/DataTypes.h"
51 #include "llvm/Support/MemoryBuffer.h"
52 #include <cassert>
53 #include <map>
54 #include <memory>
55 #include <vector>
56 
57 namespace clang {
58 
59 class DiagnosticsEngine;
60 class SourceManager;
61 class FileManager;
62 class FileEntry;
63 class LineTableInfo;
64 class ASTWriter;
65 class ASTReader;
66 
67 /// \brief Public enums and private classes that are part of the
68 /// SourceManager implementation.
69 ///
70 namespace SrcMgr {
71   /// \brief Indicates whether a file or directory holds normal user code,
72   /// system code, or system code which is implicitly 'extern "C"' in C++ mode.
73   ///
74   /// Entire directories can be tagged with this (this is maintained by
75   /// DirectoryLookup and friends) as can specific FileInfos when a \#pragma
76   /// system_header is seen or in various other cases.
77   ///
78   enum CharacteristicKind {
79     C_User, C_System, C_ExternCSystem
80   };
81 
82   /// \brief One instance of this struct is kept for every file loaded or used.
83   ///
84   /// This object owns the MemoryBuffer object.
85   class LLVM_ALIGNAS(8) ContentCache {
86     enum CCFlags {
87       /// \brief Whether the buffer is invalid.
88       InvalidFlag = 0x01,
89       /// \brief Whether the buffer should not be freed on destruction.
90       DoNotFreeFlag = 0x02
91     };
92 
93     /// \brief The actual buffer containing the characters from the input
94     /// file.
95     ///
96     /// This is owned by the ContentCache object.  The bits indicate
97     /// whether the buffer is invalid.
98     mutable llvm::PointerIntPair<llvm::MemoryBuffer *, 2> Buffer;
99 
100   public:
101     /// \brief Reference to the file entry representing this ContentCache.
102     ///
103     /// This reference does not own the FileEntry object.
104     ///
105     /// It is possible for this to be NULL if the ContentCache encapsulates
106     /// an imaginary text buffer.
107     const FileEntry *OrigEntry;
108 
109     /// \brief References the file which the contents were actually loaded from.
110     ///
111     /// Can be different from 'Entry' if we overridden the contents of one file
112     /// with the contents of another file.
113     const FileEntry *ContentsEntry;
114 
115     /// \brief A bump pointer allocated array of offsets for each source line.
116     ///
117     /// This is lazily computed.  This is owned by the SourceManager
118     /// BumpPointerAllocator object.
119     unsigned *SourceLineCache;
120 
121     /// \brief The number of lines in this ContentCache.
122     ///
123     /// This is only valid if SourceLineCache is non-null.
124     unsigned NumLines;
125 
126     /// \brief Indicates whether the buffer itself was provided to override
127     /// the actual file contents.
128     ///
129     /// When true, the original entry may be a virtual file that does not
130     /// exist.
131     unsigned BufferOverridden : 1;
132 
133     /// \brief True if this content cache was initially created for a source
134     /// file considered as a system one.
135     unsigned IsSystemFile : 1;
136 
137     /// \brief True if this file may be transient, that is, if it might not
138     /// exist at some later point in time when this content entry is used,
139     /// after serialization and deserialization.
140     unsigned IsTransient : 1;
141 
ContentCache(Ent,Ent)142     ContentCache(const FileEntry *Ent = nullptr) : ContentCache(Ent, Ent) {}
143 
ContentCache(const FileEntry * Ent,const FileEntry * contentEnt)144     ContentCache(const FileEntry *Ent, const FileEntry *contentEnt)
145       : Buffer(nullptr, false), OrigEntry(Ent), ContentsEntry(contentEnt),
146         SourceLineCache(nullptr), NumLines(0), BufferOverridden(false),
147         IsSystemFile(false), IsTransient(false) {}
148 
149     ~ContentCache();
150 
151     /// The copy ctor does not allow copies where source object has either
152     /// a non-NULL Buffer or SourceLineCache.  Ownership of allocated memory
153     /// is not transferred, so this is a logical error.
ContentCache(const ContentCache & RHS)154     ContentCache(const ContentCache &RHS)
155       : Buffer(nullptr, false), SourceLineCache(nullptr),
156         BufferOverridden(false), IsSystemFile(false), IsTransient(false) {
157       OrigEntry = RHS.OrigEntry;
158       ContentsEntry = RHS.ContentsEntry;
159 
160       assert(RHS.Buffer.getPointer() == nullptr &&
161              RHS.SourceLineCache == nullptr &&
162              "Passed ContentCache object cannot own a buffer.");
163 
164       NumLines = RHS.NumLines;
165     }
166 
167     /// \brief Returns the memory buffer for the associated content.
168     ///
169     /// \param Diag Object through which diagnostics will be emitted if the
170     ///   buffer cannot be retrieved.
171     ///
172     /// \param Loc If specified, is the location that invalid file diagnostics
173     ///   will be emitted at.
174     ///
175     /// \param Invalid If non-NULL, will be set \c true if an error occurred.
176     llvm::MemoryBuffer *getBuffer(DiagnosticsEngine &Diag,
177                                   const SourceManager &SM,
178                                   SourceLocation Loc = SourceLocation(),
179                                   bool *Invalid = nullptr) const;
180 
181     /// \brief Returns the size of the content encapsulated by this
182     /// ContentCache.
183     ///
184     /// This can be the size of the source file or the size of an
185     /// arbitrary scratch buffer.  If the ContentCache encapsulates a source
186     /// file this size is retrieved from the file's FileEntry.
187     unsigned getSize() const;
188 
189     /// \brief Returns the number of bytes actually mapped for this
190     /// ContentCache.
191     ///
192     /// This can be 0 if the MemBuffer was not actually expanded.
193     unsigned getSizeBytesMapped() const;
194 
195     /// Returns the kind of memory used to back the memory buffer for
196     /// this content cache.  This is used for performance analysis.
197     llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const;
198 
setBuffer(std::unique_ptr<llvm::MemoryBuffer> B)199     void setBuffer(std::unique_ptr<llvm::MemoryBuffer> B) {
200       assert(!Buffer.getPointer() && "MemoryBuffer already set.");
201       Buffer.setPointer(B.release());
202       Buffer.setInt(0);
203     }
204 
205     /// \brief Get the underlying buffer, returning NULL if the buffer is not
206     /// yet available.
getRawBuffer()207     llvm::MemoryBuffer *getRawBuffer() const { return Buffer.getPointer(); }
208 
209     /// \brief Replace the existing buffer (which will be deleted)
210     /// with the given buffer.
211     void replaceBuffer(llvm::MemoryBuffer *B, bool DoNotFree = false);
212 
213     /// \brief Determine whether the buffer itself is invalid.
isBufferInvalid()214     bool isBufferInvalid() const {
215       return Buffer.getInt() & InvalidFlag;
216     }
217 
218     /// \brief Determine whether the buffer should be freed.
shouldFreeBuffer()219     bool shouldFreeBuffer() const {
220       return (Buffer.getInt() & DoNotFreeFlag) == 0;
221     }
222 
223   private:
224     // Disable assignments.
225     ContentCache &operator=(const ContentCache& RHS) = delete;
226   };
227 
228   // Assert that the \c ContentCache objects will always be 8-byte aligned so
229   // that we can pack 3 bits of integer into pointers to such objects.
230   static_assert(llvm::AlignOf<ContentCache>::Alignment >= 8,
231                 "ContentCache must be 8-byte aligned.");
232 
233   /// \brief Information about a FileID, basically just the logical file
234   /// that it represents and include stack information.
235   ///
236   /// Each FileInfo has include stack information, indicating where it came
237   /// from. This information encodes the \#include chain that a token was
238   /// expanded from. The main include file has an invalid IncludeLoc.
239   ///
240   /// FileInfos contain a "ContentCache *", with the contents of the file.
241   ///
242   class FileInfo {
243     /// \brief The location of the \#include that brought in this file.
244     ///
245     /// This is an invalid SLOC for the main file (top of the \#include chain).
246     unsigned IncludeLoc;  // Really a SourceLocation
247 
248     /// \brief Number of FileIDs (files and macros) that were created during
249     /// preprocessing of this \#include, including this SLocEntry.
250     ///
251     /// Zero means the preprocessor didn't provide such info for this SLocEntry.
252     unsigned NumCreatedFIDs;
253 
254     /// \brief Contains the ContentCache* and the bits indicating the
255     /// characteristic of the file and whether it has \#line info, all
256     /// bitmangled together.
257     uintptr_t Data;
258 
259     friend class clang::SourceManager;
260     friend class clang::ASTWriter;
261     friend class clang::ASTReader;
262   public:
263     /// \brief Return a FileInfo object.
get(SourceLocation IL,const ContentCache * Con,CharacteristicKind FileCharacter)264     static FileInfo get(SourceLocation IL, const ContentCache *Con,
265                         CharacteristicKind FileCharacter) {
266       FileInfo X;
267       X.IncludeLoc = IL.getRawEncoding();
268       X.NumCreatedFIDs = 0;
269       X.Data = (uintptr_t)Con;
270       assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned");
271       assert((unsigned)FileCharacter < 4 && "invalid file character");
272       X.Data |= (unsigned)FileCharacter;
273       return X;
274     }
275 
getIncludeLoc()276     SourceLocation getIncludeLoc() const {
277       return SourceLocation::getFromRawEncoding(IncludeLoc);
278     }
getContentCache()279     const ContentCache* getContentCache() const {
280       return reinterpret_cast<const ContentCache*>(Data & ~uintptr_t(7));
281     }
282 
283     /// \brief Return whether this is a system header or not.
getFileCharacteristic()284     CharacteristicKind getFileCharacteristic() const {
285       return (CharacteristicKind)(Data & 3);
286     }
287 
288     /// \brief Return true if this FileID has \#line directives in it.
hasLineDirectives()289     bool hasLineDirectives() const { return (Data & 4) != 0; }
290 
291     /// \brief Set the flag that indicates that this FileID has
292     /// line table entries associated with it.
setHasLineDirectives()293     void setHasLineDirectives() {
294       Data |= 4;
295     }
296   };
297 
298   /// \brief Each ExpansionInfo encodes the expansion location - where
299   /// the token was ultimately expanded, and the SpellingLoc - where the actual
300   /// character data for the token came from.
301   class ExpansionInfo {
302     // Really these are all SourceLocations.
303 
304     /// \brief Where the spelling for the token can be found.
305     unsigned SpellingLoc;
306 
307     /// In a macro expansion, ExpansionLocStart and ExpansionLocEnd
308     /// indicate the start and end of the expansion. In object-like macros,
309     /// they will be the same. In a function-like macro expansion, the start
310     /// will be the identifier and the end will be the ')'. Finally, in
311     /// macro-argument instantiations, the end will be 'SourceLocation()', an
312     /// invalid location.
313     unsigned ExpansionLocStart, ExpansionLocEnd;
314 
315   public:
getSpellingLoc()316     SourceLocation getSpellingLoc() const {
317       return SourceLocation::getFromRawEncoding(SpellingLoc);
318     }
getExpansionLocStart()319     SourceLocation getExpansionLocStart() const {
320       return SourceLocation::getFromRawEncoding(ExpansionLocStart);
321     }
getExpansionLocEnd()322     SourceLocation getExpansionLocEnd() const {
323       SourceLocation EndLoc =
324         SourceLocation::getFromRawEncoding(ExpansionLocEnd);
325       return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc;
326     }
327 
getExpansionLocRange()328     std::pair<SourceLocation,SourceLocation> getExpansionLocRange() const {
329       return std::make_pair(getExpansionLocStart(), getExpansionLocEnd());
330     }
331 
isMacroArgExpansion()332     bool isMacroArgExpansion() const {
333       // Note that this needs to return false for default constructed objects.
334       return getExpansionLocStart().isValid() &&
335         SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid();
336     }
337 
isMacroBodyExpansion()338     bool isMacroBodyExpansion() const {
339       return getExpansionLocStart().isValid() &&
340         SourceLocation::getFromRawEncoding(ExpansionLocEnd).isValid();
341     }
342 
isFunctionMacroExpansion()343     bool isFunctionMacroExpansion() const {
344       return getExpansionLocStart().isValid() &&
345           getExpansionLocStart() != getExpansionLocEnd();
346     }
347 
348     /// \brief Return a ExpansionInfo for an expansion.
349     ///
350     /// Start and End specify the expansion range (where the macro is
351     /// expanded), and SpellingLoc specifies the spelling location (where
352     /// the characters from the token come from). All three can refer to
353     /// normal File SLocs or expansion locations.
create(SourceLocation SpellingLoc,SourceLocation Start,SourceLocation End)354     static ExpansionInfo create(SourceLocation SpellingLoc,
355                                 SourceLocation Start, SourceLocation End) {
356       ExpansionInfo X;
357       X.SpellingLoc = SpellingLoc.getRawEncoding();
358       X.ExpansionLocStart = Start.getRawEncoding();
359       X.ExpansionLocEnd = End.getRawEncoding();
360       return X;
361     }
362 
363     /// \brief Return a special ExpansionInfo for the expansion of
364     /// a macro argument into a function-like macro's body.
365     ///
366     /// ExpansionLoc specifies the expansion location (where the macro is
367     /// expanded). This doesn't need to be a range because a macro is always
368     /// expanded at a macro parameter reference, and macro parameters are
369     /// always exactly one token. SpellingLoc specifies the spelling location
370     /// (where the characters from the token come from). ExpansionLoc and
371     /// SpellingLoc can both refer to normal File SLocs or expansion locations.
372     ///
373     /// Given the code:
374     /// \code
375     ///   #define F(x) f(x)
376     ///   F(42);
377     /// \endcode
378     ///
379     /// When expanding '\c F(42)', the '\c x' would call this with an
380     /// SpellingLoc pointing at '\c 42' and an ExpansionLoc pointing at its
381     /// location in the definition of '\c F'.
createForMacroArg(SourceLocation SpellingLoc,SourceLocation ExpansionLoc)382     static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc,
383                                            SourceLocation ExpansionLoc) {
384       // We store an intentionally invalid source location for the end of the
385       // expansion range to mark that this is a macro argument ion rather than
386       // a normal one.
387       return create(SpellingLoc, ExpansionLoc, SourceLocation());
388     }
389   };
390 
391   /// \brief This is a discriminated union of FileInfo and ExpansionInfo.
392   ///
393   /// SourceManager keeps an array of these objects, and they are uniquely
394   /// identified by the FileID datatype.
395   class SLocEntry {
396     unsigned Offset : 31;
397     unsigned IsExpansion : 1;
398     union {
399       FileInfo File;
400       ExpansionInfo Expansion;
401     };
402   public:
getOffset()403     unsigned getOffset() const { return Offset; }
404 
isExpansion()405     bool isExpansion() const { return IsExpansion; }
isFile()406     bool isFile() const { return !isExpansion(); }
407 
getFile()408     const FileInfo &getFile() const {
409       assert(isFile() && "Not a file SLocEntry!");
410       return File;
411     }
412 
getExpansion()413     const ExpansionInfo &getExpansion() const {
414       assert(isExpansion() && "Not a macro expansion SLocEntry!");
415       return Expansion;
416     }
417 
get(unsigned Offset,const FileInfo & FI)418     static SLocEntry get(unsigned Offset, const FileInfo &FI) {
419       assert(!(Offset & (1 << 31)) && "Offset is too large");
420       SLocEntry E;
421       E.Offset = Offset;
422       E.IsExpansion = false;
423       E.File = FI;
424       return E;
425     }
426 
get(unsigned Offset,const ExpansionInfo & Expansion)427     static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) {
428       assert(!(Offset & (1 << 31)) && "Offset is too large");
429       SLocEntry E;
430       E.Offset = Offset;
431       E.IsExpansion = true;
432       E.Expansion = Expansion;
433       return E;
434     }
435   };
436 }  // end SrcMgr namespace.
437 
438 /// \brief External source of source location entries.
439 class ExternalSLocEntrySource {
440 public:
441   virtual ~ExternalSLocEntrySource();
442 
443   /// \brief Read the source location entry with index ID, which will always be
444   /// less than -1.
445   ///
446   /// \returns true if an error occurred that prevented the source-location
447   /// entry from being loaded.
448   virtual bool ReadSLocEntry(int ID) = 0;
449 
450   /// \brief Retrieve the module import location and name for the given ID, if
451   /// in fact it was loaded from a module (rather than, say, a precompiled
452   /// header).
453   virtual std::pair<SourceLocation, StringRef> getModuleImportLoc(int ID) = 0;
454 };
455 
456 
457 /// \brief Holds the cache used by isBeforeInTranslationUnit.
458 ///
459 /// The cache structure is complex enough to be worth breaking out of
460 /// SourceManager.
461 class InBeforeInTUCacheEntry {
462   /// \brief The FileID's of the cached query.
463   ///
464   /// If these match up with a subsequent query, the result can be reused.
465   FileID LQueryFID, RQueryFID;
466 
467   /// \brief True if LQueryFID was created before RQueryFID.
468   ///
469   /// This is used to compare macro expansion locations.
470   bool IsLQFIDBeforeRQFID;
471 
472   /// \brief The file found in common between the two \#include traces, i.e.,
473   /// the nearest common ancestor of the \#include tree.
474   FileID CommonFID;
475 
476   /// \brief The offset of the previous query in CommonFID.
477   ///
478   /// Usually, this represents the location of the \#include for QueryFID, but
479   /// if LQueryFID is a parent of RQueryFID (or vice versa) then these can be a
480   /// random token in the parent.
481   unsigned LCommonOffset, RCommonOffset;
482 public:
483   /// \brief Return true if the currently cached values match up with
484   /// the specified LHS/RHS query.
485   ///
486   /// If not, we can't use the cache.
isCacheValid(FileID LHS,FileID RHS)487   bool isCacheValid(FileID LHS, FileID RHS) const {
488     return LQueryFID == LHS && RQueryFID == RHS;
489   }
490 
491   /// \brief If the cache is valid, compute the result given the
492   /// specified offsets in the LHS/RHS FileID's.
getCachedResult(unsigned LOffset,unsigned ROffset)493   bool getCachedResult(unsigned LOffset, unsigned ROffset) const {
494     // If one of the query files is the common file, use the offset.  Otherwise,
495     // use the #include loc in the common file.
496     if (LQueryFID != CommonFID) LOffset = LCommonOffset;
497     if (RQueryFID != CommonFID) ROffset = RCommonOffset;
498 
499     // It is common for multiple macro expansions to be "included" from the same
500     // location (expansion location), in which case use the order of the FileIDs
501     // to determine which came first. This will also take care the case where
502     // one of the locations points at the inclusion/expansion point of the other
503     // in which case its FileID will come before the other.
504     if (LOffset == ROffset)
505       return IsLQFIDBeforeRQFID;
506 
507     return LOffset < ROffset;
508   }
509 
510   /// \brief Set up a new query.
setQueryFIDs(FileID LHS,FileID RHS,bool isLFIDBeforeRFID)511   void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) {
512     assert(LHS != RHS);
513     LQueryFID = LHS;
514     RQueryFID = RHS;
515     IsLQFIDBeforeRQFID = isLFIDBeforeRFID;
516   }
517 
clear()518   void clear() {
519     LQueryFID = RQueryFID = FileID();
520     IsLQFIDBeforeRQFID = false;
521   }
522 
setCommonLoc(FileID commonFID,unsigned lCommonOffset,unsigned rCommonOffset)523   void setCommonLoc(FileID commonFID, unsigned lCommonOffset,
524                     unsigned rCommonOffset) {
525     CommonFID = commonFID;
526     LCommonOffset = lCommonOffset;
527     RCommonOffset = rCommonOffset;
528   }
529 
530 };
531 
532 /// \brief The stack used when building modules on demand, which is used
533 /// to provide a link between the source managers of the different compiler
534 /// instances.
535 typedef ArrayRef<std::pair<std::string, FullSourceLoc> > ModuleBuildStack;
536 
537 /// \brief This class handles loading and caching of source files into memory.
538 ///
539 /// This object owns the MemoryBuffer objects for all of the loaded
540 /// files and assigns unique FileID's for each unique \#include chain.
541 ///
542 /// The SourceManager can be queried for information about SourceLocation
543 /// objects, turning them into either spelling or expansion locations. Spelling
544 /// locations represent where the bytes corresponding to a token came from and
545 /// expansion locations represent where the location is in the user's view. In
546 /// the case of a macro expansion, for example, the spelling location indicates
547 /// where the expanded token came from and the expansion location specifies
548 /// where it was expanded.
549 class SourceManager : public RefCountedBase<SourceManager> {
550   /// \brief DiagnosticsEngine object.
551   DiagnosticsEngine &Diag;
552 
553   FileManager &FileMgr;
554 
555   mutable llvm::BumpPtrAllocator ContentCacheAlloc;
556 
557   /// \brief Memoized information about all of the files tracked by this
558   /// SourceManager.
559   ///
560   /// This map allows us to merge ContentCache entries based
561   /// on their FileEntry*.  All ContentCache objects will thus have unique,
562   /// non-null, FileEntry pointers.
563   llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos;
564 
565   /// \brief True if the ContentCache for files that are overridden by other
566   /// files, should report the original file name. Defaults to true.
567   bool OverridenFilesKeepOriginalName;
568 
569   /// \brief True if non-system source files should be treated as volatile
570   /// (likely to change while trying to use them). Defaults to false.
571   bool UserFilesAreVolatile;
572 
573   /// \brief True if all files read during this compilation should be treated
574   /// as transient (may not be present in later compilations using a module
575   /// file created from this compilation). Defaults to false.
576   bool FilesAreTransient;
577 
578   struct OverriddenFilesInfoTy {
579     /// \brief Files that have been overridden with the contents from another
580     /// file.
581     llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles;
582     /// \brief Files that were overridden with a memory buffer.
583     llvm::DenseSet<const FileEntry *> OverriddenFilesWithBuffer;
584   };
585 
586   /// \brief Lazily create the object keeping overridden files info, since
587   /// it is uncommonly used.
588   std::unique_ptr<OverriddenFilesInfoTy> OverriddenFilesInfo;
589 
getOverriddenFilesInfo()590   OverriddenFilesInfoTy &getOverriddenFilesInfo() {
591     if (!OverriddenFilesInfo)
592       OverriddenFilesInfo.reset(new OverriddenFilesInfoTy);
593     return *OverriddenFilesInfo;
594   }
595 
596   /// \brief Information about various memory buffers that we have read in.
597   ///
598   /// All FileEntry* within the stored ContentCache objects are NULL,
599   /// as they do not refer to a file.
600   std::vector<SrcMgr::ContentCache*> MemBufferInfos;
601 
602   /// \brief The table of SLocEntries that are local to this module.
603   ///
604   /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid
605   /// expansion.
606   SmallVector<SrcMgr::SLocEntry, 0> LocalSLocEntryTable;
607 
608   /// \brief The table of SLocEntries that are loaded from other modules.
609   ///
610   /// Negative FileIDs are indexes into this table. To get from ID to an index,
611   /// use (-ID - 2).
612   mutable SmallVector<SrcMgr::SLocEntry, 0> LoadedSLocEntryTable;
613 
614   /// \brief The starting offset of the next local SLocEntry.
615   ///
616   /// This is LocalSLocEntryTable.back().Offset + the size of that entry.
617   unsigned NextLocalOffset;
618 
619   /// \brief The starting offset of the latest batch of loaded SLocEntries.
620   ///
621   /// This is LoadedSLocEntryTable.back().Offset, except that that entry might
622   /// not have been loaded, so that value would be unknown.
623   unsigned CurrentLoadedOffset;
624 
625   /// \brief The highest possible offset is 2^31-1, so CurrentLoadedOffset
626   /// starts at 2^31.
627   static const unsigned MaxLoadedOffset = 1U << 31U;
628 
629   /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable
630   /// have already been loaded from the external source.
631   ///
632   /// Same indexing as LoadedSLocEntryTable.
633   llvm::BitVector SLocEntryLoaded;
634 
635   /// \brief An external source for source location entries.
636   ExternalSLocEntrySource *ExternalSLocEntries;
637 
638   /// \brief A one-entry cache to speed up getFileID.
639   ///
640   /// LastFileIDLookup records the last FileID looked up or created, because it
641   /// is very common to look up many tokens from the same file.
642   mutable FileID LastFileIDLookup;
643 
644   /// \brief Holds information for \#line directives.
645   ///
646   /// This is referenced by indices from SLocEntryTable.
647   LineTableInfo *LineTable;
648 
649   /// \brief These ivars serve as a cache used in the getLineNumber
650   /// method which is used to speedup getLineNumber calls to nearby locations.
651   mutable FileID LastLineNoFileIDQuery;
652   mutable SrcMgr::ContentCache *LastLineNoContentCache;
653   mutable unsigned LastLineNoFilePos;
654   mutable unsigned LastLineNoResult;
655 
656   /// \brief The file ID for the main source file of the translation unit.
657   FileID MainFileID;
658 
659   /// \brief The file ID for the precompiled preamble there is one.
660   FileID PreambleFileID;
661 
662   // Statistics for -print-stats.
663   mutable unsigned NumLinearScans, NumBinaryProbes;
664 
665   /// \brief Associates a FileID with its "included/expanded in" decomposed
666   /// location.
667   ///
668   /// Used to cache results from and speed-up \c getDecomposedIncludedLoc
669   /// function.
670   mutable llvm::DenseMap<FileID, std::pair<FileID, unsigned> > IncludedLocMap;
671 
672   /// The key value into the IsBeforeInTUCache table.
673   typedef std::pair<FileID, FileID> IsBeforeInTUCacheKey;
674 
675   /// The IsBeforeInTranslationUnitCache is a mapping from FileID pairs
676   /// to cache results.
677   typedef llvm::DenseMap<IsBeforeInTUCacheKey, InBeforeInTUCacheEntry>
678           InBeforeInTUCache;
679 
680   /// Cache results for the isBeforeInTranslationUnit method.
681   mutable InBeforeInTUCache IBTUCache;
682   mutable InBeforeInTUCacheEntry IBTUCacheOverflow;
683 
684   /// Return the cache entry for comparing the given file IDs
685   /// for isBeforeInTranslationUnit.
686   InBeforeInTUCacheEntry &getInBeforeInTUCache(FileID LFID, FileID RFID) const;
687 
688   // Cache for the "fake" buffer used for error-recovery purposes.
689   mutable std::unique_ptr<llvm::MemoryBuffer> FakeBufferForRecovery;
690 
691   mutable std::unique_ptr<SrcMgr::ContentCache> FakeContentCacheForRecovery;
692 
693   /// \brief Lazily computed map of macro argument chunks to their expanded
694   /// source location.
695   typedef std::map<unsigned, SourceLocation> MacroArgsMap;
696 
697   mutable llvm::DenseMap<FileID, MacroArgsMap *> MacroArgsCacheMap;
698 
699   /// \brief The stack of modules being built, which is used to detect
700   /// cycles in the module dependency graph as modules are being built, as
701   /// well as to describe why we're rebuilding a particular module.
702   ///
703   /// There is no way to set this value from the command line. If we ever need
704   /// to do so (e.g., if on-demand module construction moves out-of-process),
705   /// we can add a cc1-level option to do so.
706   SmallVector<std::pair<std::string, FullSourceLoc>, 2> StoredModuleBuildStack;
707 
708   // SourceManager doesn't support copy construction.
709   explicit SourceManager(const SourceManager&) = delete;
710   void operator=(const SourceManager&) = delete;
711 public:
712   SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr,
713                 bool UserFilesAreVolatile = false);
714   ~SourceManager();
715 
716   void clearIDTables();
717 
getDiagnostics()718   DiagnosticsEngine &getDiagnostics() const { return Diag; }
719 
getFileManager()720   FileManager &getFileManager() const { return FileMgr; }
721 
722   /// \brief Set true if the SourceManager should report the original file name
723   /// for contents of files that were overridden by other files. Defaults to
724   /// true.
setOverridenFilesKeepOriginalName(bool value)725   void setOverridenFilesKeepOriginalName(bool value) {
726     OverridenFilesKeepOriginalName = value;
727   }
728 
729   /// \brief True if non-system source files should be treated as volatile
730   /// (likely to change while trying to use them).
userFilesAreVolatile()731   bool userFilesAreVolatile() const { return UserFilesAreVolatile; }
732 
733   /// \brief Retrieve the module build stack.
getModuleBuildStack()734   ModuleBuildStack getModuleBuildStack() const {
735     return StoredModuleBuildStack;
736   }
737 
738   /// \brief Set the module build stack.
setModuleBuildStack(ModuleBuildStack stack)739   void setModuleBuildStack(ModuleBuildStack stack) {
740     StoredModuleBuildStack.clear();
741     StoredModuleBuildStack.append(stack.begin(), stack.end());
742   }
743 
744   /// \brief Push an entry to the module build stack.
pushModuleBuildStack(StringRef moduleName,FullSourceLoc importLoc)745   void pushModuleBuildStack(StringRef moduleName, FullSourceLoc importLoc) {
746     StoredModuleBuildStack.push_back(std::make_pair(moduleName.str(),importLoc));
747   }
748 
749   //===--------------------------------------------------------------------===//
750   // MainFileID creation and querying methods.
751   //===--------------------------------------------------------------------===//
752 
753   /// \brief Returns the FileID of the main source file.
getMainFileID()754   FileID getMainFileID() const { return MainFileID; }
755 
756   /// \brief Set the file ID for the main source file.
setMainFileID(FileID FID)757   void setMainFileID(FileID FID) {
758     MainFileID = FID;
759   }
760 
761   /// \brief Set the file ID for the precompiled preamble.
setPreambleFileID(FileID Preamble)762   void setPreambleFileID(FileID Preamble) {
763     assert(PreambleFileID.isInvalid() && "PreambleFileID already set!");
764     PreambleFileID = Preamble;
765   }
766 
767   /// \brief Get the file ID for the precompiled preamble if there is one.
getPreambleFileID()768   FileID getPreambleFileID() const { return PreambleFileID; }
769 
770   //===--------------------------------------------------------------------===//
771   // Methods to create new FileID's and macro expansions.
772   //===--------------------------------------------------------------------===//
773 
774   /// \brief Create a new FileID that represents the specified file
775   /// being \#included from the specified IncludePosition.
776   ///
777   /// This translates NULL into standard input.
778   FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos,
779                       SrcMgr::CharacteristicKind FileCharacter,
780                       int LoadedID = 0, unsigned LoadedOffset = 0) {
781     const SrcMgr::ContentCache *
782       IR = getOrCreateContentCache(SourceFile,
783                               /*isSystemFile=*/FileCharacter != SrcMgr::C_User);
784     assert(IR && "getOrCreateContentCache() cannot return NULL");
785     return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset);
786   }
787 
788   /// \brief Create a new FileID that represents the specified memory buffer.
789   ///
790   /// This does no caching of the buffer and takes ownership of the
791   /// MemoryBuffer, so only pass a MemoryBuffer to this once.
792   FileID createFileID(std::unique_ptr<llvm::MemoryBuffer> Buffer,
793                       SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User,
794                       int LoadedID = 0, unsigned LoadedOffset = 0,
795                       SourceLocation IncludeLoc = SourceLocation()) {
796     return createFileID(createMemBufferContentCache(std::move(Buffer)),
797                         IncludeLoc, FileCharacter, LoadedID, LoadedOffset);
798   }
799 
800   /// \brief Get the FileID for \p SourceFile if it exists. Otherwise, create a
801   /// new FileID for the \p SourceFile.
getOrCreateFileID(const FileEntry * SourceFile,SrcMgr::CharacteristicKind FileCharacter)802   FileID getOrCreateFileID(const FileEntry *SourceFile,
803                            SrcMgr::CharacteristicKind FileCharacter) {
804     FileID ID = translateFile(SourceFile);
805     return ID.isValid() ? ID : createFileID(SourceFile, SourceLocation(),
806                                             FileCharacter);
807   }
808 
809   /// \brief Return a new SourceLocation that encodes the
810   /// fact that a token from SpellingLoc should actually be referenced from
811   /// ExpansionLoc, and that it represents the expansion of a macro argument
812   /// into the function-like macro body.
813   SourceLocation createMacroArgExpansionLoc(SourceLocation Loc,
814                                             SourceLocation ExpansionLoc,
815                                             unsigned TokLength);
816 
817   /// \brief Return a new SourceLocation that encodes the fact
818   /// that a token from SpellingLoc should actually be referenced from
819   /// ExpansionLoc.
820   SourceLocation createExpansionLoc(SourceLocation Loc,
821                                     SourceLocation ExpansionLocStart,
822                                     SourceLocation ExpansionLocEnd,
823                                     unsigned TokLength,
824                                     int LoadedID = 0,
825                                     unsigned LoadedOffset = 0);
826 
827   /// \brief Retrieve the memory buffer associated with the given file.
828   ///
829   /// \param Invalid If non-NULL, will be set \c true if an error
830   /// occurs while retrieving the memory buffer.
831   llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File,
832                                              bool *Invalid = nullptr);
833 
834   /// \brief Override the contents of the given source file by providing an
835   /// already-allocated buffer.
836   ///
837   /// \param SourceFile the source file whose contents will be overridden.
838   ///
839   /// \param Buffer the memory buffer whose contents will be used as the
840   /// data in the given source file.
841   ///
842   /// \param DoNotFree If true, then the buffer will not be freed when the
843   /// source manager is destroyed.
844   void overrideFileContents(const FileEntry *SourceFile,
845                             llvm::MemoryBuffer *Buffer, bool DoNotFree);
overrideFileContents(const FileEntry * SourceFile,std::unique_ptr<llvm::MemoryBuffer> Buffer)846   void overrideFileContents(const FileEntry *SourceFile,
847                             std::unique_ptr<llvm::MemoryBuffer> Buffer) {
848     overrideFileContents(SourceFile, Buffer.release(), /*DoNotFree*/ false);
849   }
850 
851   /// \brief Override the given source file with another one.
852   ///
853   /// \param SourceFile the source file which will be overridden.
854   ///
855   /// \param NewFile the file whose contents will be used as the
856   /// data instead of the contents of the given source file.
857   void overrideFileContents(const FileEntry *SourceFile,
858                             const FileEntry *NewFile);
859 
860   /// \brief Returns true if the file contents have been overridden.
isFileOverridden(const FileEntry * File)861   bool isFileOverridden(const FileEntry *File) {
862     if (OverriddenFilesInfo) {
863       if (OverriddenFilesInfo->OverriddenFilesWithBuffer.count(File))
864         return true;
865       if (OverriddenFilesInfo->OverriddenFiles.find(File) !=
866           OverriddenFilesInfo->OverriddenFiles.end())
867         return true;
868     }
869     return false;
870   }
871 
872   /// \brief Disable overridding the contents of a file, previously enabled
873   /// with #overrideFileContents.
874   ///
875   /// This should be called before parsing has begun.
876   void disableFileContentsOverride(const FileEntry *File);
877 
878   /// \brief Specify that a file is transient.
879   void setFileIsTransient(const FileEntry *SourceFile);
880 
881   /// \brief Specify that all files that are read during this compilation are
882   /// transient.
setAllFilesAreTransient(bool Transient)883   void setAllFilesAreTransient(bool Transient) {
884     FilesAreTransient = Transient;
885   }
886 
887   //===--------------------------------------------------------------------===//
888   // FileID manipulation methods.
889   //===--------------------------------------------------------------------===//
890 
891   /// \brief Return the buffer for the specified FileID.
892   ///
893   /// If there is an error opening this buffer the first time, this
894   /// manufactures a temporary buffer and returns a non-empty error string.
895   llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc,
896                                 bool *Invalid = nullptr) const {
897     bool MyInvalid = false;
898     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
899     if (MyInvalid || !Entry.isFile()) {
900       if (Invalid)
901         *Invalid = true;
902 
903       return getFakeBufferForRecovery();
904     }
905 
906     return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc,
907                                                         Invalid);
908   }
909 
910   llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = nullptr) const {
911     bool MyInvalid = false;
912     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
913     if (MyInvalid || !Entry.isFile()) {
914       if (Invalid)
915         *Invalid = true;
916 
917       return getFakeBufferForRecovery();
918     }
919 
920     return Entry.getFile().getContentCache()->getBuffer(Diag, *this,
921                                                         SourceLocation(),
922                                                         Invalid);
923   }
924 
925   /// \brief Returns the FileEntry record for the provided FileID.
getFileEntryForID(FileID FID)926   const FileEntry *getFileEntryForID(FileID FID) const {
927     bool MyInvalid = false;
928     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
929     if (MyInvalid || !Entry.isFile())
930       return nullptr;
931 
932     const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache();
933     if (!Content)
934       return nullptr;
935     return Content->OrigEntry;
936   }
937 
938   /// \brief Returns the FileEntry record for the provided SLocEntry.
getFileEntryForSLocEntry(const SrcMgr::SLocEntry & sloc)939   const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const
940   {
941     const SrcMgr::ContentCache *Content = sloc.getFile().getContentCache();
942     if (!Content)
943       return nullptr;
944     return Content->OrigEntry;
945   }
946 
947   /// \brief Return a StringRef to the source buffer data for the
948   /// specified FileID.
949   ///
950   /// \param FID The file ID whose contents will be returned.
951   /// \param Invalid If non-NULL, will be set true if an error occurred.
952   StringRef getBufferData(FileID FID, bool *Invalid = nullptr) const;
953 
954   /// \brief Get the number of FileIDs (files and macros) that were created
955   /// during preprocessing of \p FID, including it.
getNumCreatedFIDsForFileID(FileID FID)956   unsigned getNumCreatedFIDsForFileID(FileID FID) const {
957     bool Invalid = false;
958     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
959     if (Invalid || !Entry.isFile())
960       return 0;
961 
962     return Entry.getFile().NumCreatedFIDs;
963   }
964 
965   /// \brief Set the number of FileIDs (files and macros) that were created
966   /// during preprocessing of \p FID, including it.
setNumCreatedFIDsForFileID(FileID FID,unsigned NumFIDs)967   void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs) const {
968     bool Invalid = false;
969     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
970     if (Invalid || !Entry.isFile())
971       return;
972 
973     assert(Entry.getFile().NumCreatedFIDs == 0 && "Already set!");
974     const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs;
975   }
976 
977   //===--------------------------------------------------------------------===//
978   // SourceLocation manipulation methods.
979   //===--------------------------------------------------------------------===//
980 
981   /// \brief Return the FileID for a SourceLocation.
982   ///
983   /// This is a very hot method that is used for all SourceManager queries
984   /// that start with a SourceLocation object.  It is responsible for finding
985   /// the entry in SLocEntryTable which contains the specified location.
986   ///
getFileID(SourceLocation SpellingLoc)987   FileID getFileID(SourceLocation SpellingLoc) const {
988     unsigned SLocOffset = SpellingLoc.getOffset();
989 
990     // If our one-entry cache covers this offset, just return it.
991     if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
992       return LastFileIDLookup;
993 
994     return getFileIDSlow(SLocOffset);
995   }
996 
997   /// \brief Return the filename of the file containing a SourceLocation.
getFilename(SourceLocation SpellingLoc)998   StringRef getFilename(SourceLocation SpellingLoc) const {
999     if (const FileEntry *F = getFileEntryForID(getFileID(SpellingLoc)))
1000       return F->getName();
1001     return StringRef();
1002   }
1003 
1004   /// \brief Return the source location corresponding to the first byte of
1005   /// the specified file.
getLocForStartOfFile(FileID FID)1006   SourceLocation getLocForStartOfFile(FileID FID) const {
1007     bool Invalid = false;
1008     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1009     if (Invalid || !Entry.isFile())
1010       return SourceLocation();
1011 
1012     unsigned FileOffset = Entry.getOffset();
1013     return SourceLocation::getFileLoc(FileOffset);
1014   }
1015 
1016   /// \brief Return the source location corresponding to the last byte of the
1017   /// specified file.
getLocForEndOfFile(FileID FID)1018   SourceLocation getLocForEndOfFile(FileID FID) const {
1019     bool Invalid = false;
1020     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1021     if (Invalid || !Entry.isFile())
1022       return SourceLocation();
1023 
1024     unsigned FileOffset = Entry.getOffset();
1025     return SourceLocation::getFileLoc(FileOffset + getFileIDSize(FID));
1026   }
1027 
1028   /// \brief Returns the include location if \p FID is a \#include'd file
1029   /// otherwise it returns an invalid location.
getIncludeLoc(FileID FID)1030   SourceLocation getIncludeLoc(FileID FID) const {
1031     bool Invalid = false;
1032     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1033     if (Invalid || !Entry.isFile())
1034       return SourceLocation();
1035 
1036     return Entry.getFile().getIncludeLoc();
1037   }
1038 
1039   // \brief Returns the import location if the given source location is
1040   // located within a module, or an invalid location if the source location
1041   // is within the current translation unit.
1042   std::pair<SourceLocation, StringRef>
getModuleImportLoc(SourceLocation Loc)1043   getModuleImportLoc(SourceLocation Loc) const {
1044     FileID FID = getFileID(Loc);
1045 
1046     // Positive file IDs are in the current translation unit, and -1 is a
1047     // placeholder.
1048     if (FID.ID >= -1)
1049       return std::make_pair(SourceLocation(), "");
1050 
1051     return ExternalSLocEntries->getModuleImportLoc(FID.ID);
1052   }
1053 
1054   /// \brief Given a SourceLocation object \p Loc, return the expansion
1055   /// location referenced by the ID.
getExpansionLoc(SourceLocation Loc)1056   SourceLocation getExpansionLoc(SourceLocation Loc) const {
1057     // Handle the non-mapped case inline, defer to out of line code to handle
1058     // expansions.
1059     if (Loc.isFileID()) return Loc;
1060     return getExpansionLocSlowCase(Loc);
1061   }
1062 
1063   /// \brief Given \p Loc, if it is a macro location return the expansion
1064   /// location or the spelling location, depending on if it comes from a
1065   /// macro argument or not.
getFileLoc(SourceLocation Loc)1066   SourceLocation getFileLoc(SourceLocation Loc) const {
1067     if (Loc.isFileID()) return Loc;
1068     return getFileLocSlowCase(Loc);
1069   }
1070 
1071   /// \brief Return the start/end of the expansion information for an
1072   /// expansion location.
1073   ///
1074   /// \pre \p Loc is required to be an expansion location.
1075   std::pair<SourceLocation,SourceLocation>
1076   getImmediateExpansionRange(SourceLocation Loc) const;
1077 
1078   /// \brief Given a SourceLocation object, return the range of
1079   /// tokens covered by the expansion in the ultimate file.
1080   std::pair<SourceLocation,SourceLocation>
1081   getExpansionRange(SourceLocation Loc) const;
1082 
1083   /// \brief Given a SourceRange object, return the range of
1084   /// tokens covered by the expansion in the ultimate file.
getExpansionRange(SourceRange Range)1085   SourceRange getExpansionRange(SourceRange Range) const {
1086     return SourceRange(getExpansionRange(Range.getBegin()).first,
1087                        getExpansionRange(Range.getEnd()).second);
1088   }
1089 
1090   /// \brief Given a SourceLocation object, return the spelling
1091   /// location referenced by the ID.
1092   ///
1093   /// This is the place where the characters that make up the lexed token
1094   /// can be found.
getSpellingLoc(SourceLocation Loc)1095   SourceLocation getSpellingLoc(SourceLocation Loc) const {
1096     // Handle the non-mapped case inline, defer to out of line code to handle
1097     // expansions.
1098     if (Loc.isFileID()) return Loc;
1099     return getSpellingLocSlowCase(Loc);
1100   }
1101 
1102   /// \brief Given a SourceLocation object, return the spelling location
1103   /// referenced by the ID.
1104   ///
1105   /// This is the first level down towards the place where the characters
1106   /// that make up the lexed token can be found.  This should not generally
1107   /// be used by clients.
1108   SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const;
1109 
1110   /// \brief Decompose the specified location into a raw FileID + Offset pair.
1111   ///
1112   /// The first element is the FileID, the second is the offset from the
1113   /// start of the buffer of the location.
getDecomposedLoc(SourceLocation Loc)1114   std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
1115     FileID FID = getFileID(Loc);
1116     bool Invalid = false;
1117     const SrcMgr::SLocEntry &E = getSLocEntry(FID, &Invalid);
1118     if (Invalid)
1119       return std::make_pair(FileID(), 0);
1120     return std::make_pair(FID, Loc.getOffset()-E.getOffset());
1121   }
1122 
1123   /// \brief Decompose the specified location into a raw FileID + Offset pair.
1124   ///
1125   /// If the location is an expansion record, walk through it until we find
1126   /// the final location expanded.
1127   std::pair<FileID, unsigned>
getDecomposedExpansionLoc(SourceLocation Loc)1128   getDecomposedExpansionLoc(SourceLocation Loc) const {
1129     FileID FID = getFileID(Loc);
1130     bool Invalid = false;
1131     const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
1132     if (Invalid)
1133       return std::make_pair(FileID(), 0);
1134 
1135     unsigned Offset = Loc.getOffset()-E->getOffset();
1136     if (Loc.isFileID())
1137       return std::make_pair(FID, Offset);
1138 
1139     return getDecomposedExpansionLocSlowCase(E);
1140   }
1141 
1142   /// \brief Decompose the specified location into a raw FileID + Offset pair.
1143   ///
1144   /// If the location is an expansion record, walk through it until we find
1145   /// its spelling record.
1146   std::pair<FileID, unsigned>
getDecomposedSpellingLoc(SourceLocation Loc)1147   getDecomposedSpellingLoc(SourceLocation Loc) const {
1148     FileID FID = getFileID(Loc);
1149     bool Invalid = false;
1150     const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid);
1151     if (Invalid)
1152       return std::make_pair(FileID(), 0);
1153 
1154     unsigned Offset = Loc.getOffset()-E->getOffset();
1155     if (Loc.isFileID())
1156       return std::make_pair(FID, Offset);
1157     return getDecomposedSpellingLocSlowCase(E, Offset);
1158   }
1159 
1160   /// \brief Returns the "included/expanded in" decomposed location of the given
1161   /// FileID.
1162   std::pair<FileID, unsigned> getDecomposedIncludedLoc(FileID FID) const;
1163 
1164   /// \brief Returns the offset from the start of the file that the
1165   /// specified SourceLocation represents.
1166   ///
1167   /// This is not very meaningful for a macro ID.
getFileOffset(SourceLocation SpellingLoc)1168   unsigned getFileOffset(SourceLocation SpellingLoc) const {
1169     return getDecomposedLoc(SpellingLoc).second;
1170   }
1171 
1172   /// \brief Tests whether the given source location represents a macro
1173   /// argument's expansion into the function-like macro definition.
1174   ///
1175   /// \param StartLoc If non-null and function returns true, it is set to the
1176   /// start location of the macro argument expansion.
1177   ///
1178   /// Such source locations only appear inside of the expansion
1179   /// locations representing where a particular function-like macro was
1180   /// expanded.
1181   bool isMacroArgExpansion(SourceLocation Loc,
1182                            SourceLocation *StartLoc = nullptr) const;
1183 
1184   /// \brief Tests whether the given source location represents the expansion of
1185   /// a macro body.
1186   ///
1187   /// This is equivalent to testing whether the location is part of a macro
1188   /// expansion but not the expansion of an argument to a function-like macro.
1189   bool isMacroBodyExpansion(SourceLocation Loc) const;
1190 
1191   /// \brief Returns true if the given MacroID location points at the beginning
1192   /// of the immediate macro expansion.
1193   ///
1194   /// \param MacroBegin If non-null and function returns true, it is set to the
1195   /// begin location of the immediate macro expansion.
1196   bool isAtStartOfImmediateMacroExpansion(SourceLocation Loc,
1197                                     SourceLocation *MacroBegin = nullptr) const;
1198 
1199   /// \brief Returns true if the given MacroID location points at the character
1200   /// end of the immediate macro expansion.
1201   ///
1202   /// \param MacroEnd If non-null and function returns true, it is set to the
1203   /// character end location of the immediate macro expansion.
1204   bool
1205   isAtEndOfImmediateMacroExpansion(SourceLocation Loc,
1206                                    SourceLocation *MacroEnd = nullptr) const;
1207 
1208   /// \brief Returns true if \p Loc is inside the [\p Start, +\p Length)
1209   /// chunk of the source location address space.
1210   ///
1211   /// If it's true and \p RelativeOffset is non-null, it will be set to the
1212   /// relative offset of \p Loc inside the chunk.
1213   bool isInSLocAddrSpace(SourceLocation Loc,
1214                          SourceLocation Start, unsigned Length,
1215                          unsigned *RelativeOffset = nullptr) const {
1216     assert(((Start.getOffset() < NextLocalOffset &&
1217                Start.getOffset()+Length <= NextLocalOffset) ||
1218             (Start.getOffset() >= CurrentLoadedOffset &&
1219                 Start.getOffset()+Length < MaxLoadedOffset)) &&
1220            "Chunk is not valid SLoc address space");
1221     unsigned LocOffs = Loc.getOffset();
1222     unsigned BeginOffs = Start.getOffset();
1223     unsigned EndOffs = BeginOffs + Length;
1224     if (LocOffs >= BeginOffs && LocOffs < EndOffs) {
1225       if (RelativeOffset)
1226         *RelativeOffset = LocOffs - BeginOffs;
1227       return true;
1228     }
1229 
1230     return false;
1231   }
1232 
1233   /// \brief Return true if both \p LHS and \p RHS are in the local source
1234   /// location address space or the loaded one.
1235   ///
1236   /// If it's true and \p RelativeOffset is non-null, it will be set to the
1237   /// offset of \p RHS relative to \p LHS.
isInSameSLocAddrSpace(SourceLocation LHS,SourceLocation RHS,int * RelativeOffset)1238   bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS,
1239                              int *RelativeOffset) const {
1240     unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset();
1241     bool LHSLoaded = LHSOffs >= CurrentLoadedOffset;
1242     bool RHSLoaded = RHSOffs >= CurrentLoadedOffset;
1243 
1244     if (LHSLoaded == RHSLoaded) {
1245       if (RelativeOffset)
1246         *RelativeOffset = RHSOffs - LHSOffs;
1247       return true;
1248     }
1249 
1250     return false;
1251   }
1252 
1253   //===--------------------------------------------------------------------===//
1254   // Queries about the code at a SourceLocation.
1255   //===--------------------------------------------------------------------===//
1256 
1257   /// \brief Return a pointer to the start of the specified location
1258   /// in the appropriate spelling MemoryBuffer.
1259   ///
1260   /// \param Invalid If non-NULL, will be set \c true if an error occurs.
1261   const char *getCharacterData(SourceLocation SL,
1262                                bool *Invalid = nullptr) const;
1263 
1264   /// \brief Return the column # for the specified file position.
1265   ///
1266   /// This is significantly cheaper to compute than the line number.  This
1267   /// returns zero if the column number isn't known.  This may only be called
1268   /// on a file sloc, so you must choose a spelling or expansion location
1269   /// before calling this method.
1270   unsigned getColumnNumber(FileID FID, unsigned FilePos,
1271                            bool *Invalid = nullptr) const;
1272   unsigned getSpellingColumnNumber(SourceLocation Loc,
1273                                    bool *Invalid = nullptr) const;
1274   unsigned getExpansionColumnNumber(SourceLocation Loc,
1275                                     bool *Invalid = nullptr) const;
1276   unsigned getPresumedColumnNumber(SourceLocation Loc,
1277                                    bool *Invalid = nullptr) const;
1278 
1279   /// \brief Given a SourceLocation, return the spelling line number
1280   /// for the position indicated.
1281   ///
1282   /// This requires building and caching a table of line offsets for the
1283   /// MemoryBuffer, so this is not cheap: use only when about to emit a
1284   /// diagnostic.
1285   unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = nullptr) const;
1286   unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
1287   unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
1288   unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const;
1289 
1290   /// \brief Return the filename or buffer identifier of the buffer the
1291   /// location is in.
1292   ///
1293   /// Note that this name does not respect \#line directives.  Use
1294   /// getPresumedLoc for normal clients.
1295   const char *getBufferName(SourceLocation Loc, bool *Invalid = nullptr) const;
1296 
1297   /// \brief Return the file characteristic of the specified source
1298   /// location, indicating whether this is a normal file, a system
1299   /// header, or an "implicit extern C" system header.
1300   ///
1301   /// This state can be modified with flags on GNU linemarker directives like:
1302   /// \code
1303   ///   # 4 "foo.h" 3
1304   /// \endcode
1305   /// which changes all source locations in the current file after that to be
1306   /// considered to be from a system header.
1307   SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const;
1308 
1309   /// \brief Returns the "presumed" location of a SourceLocation specifies.
1310   ///
1311   /// A "presumed location" can be modified by \#line or GNU line marker
1312   /// directives.  This provides a view on the data that a user should see
1313   /// in diagnostics, for example.
1314   ///
1315   /// Note that a presumed location is always given as the expansion point of
1316   /// an expansion location, not at the spelling location.
1317   ///
1318   /// \returns The presumed location of the specified SourceLocation. If the
1319   /// presumed location cannot be calculated (e.g., because \p Loc is invalid
1320   /// or the file containing \p Loc has changed on disk), returns an invalid
1321   /// presumed location.
1322   PresumedLoc getPresumedLoc(SourceLocation Loc,
1323                              bool UseLineDirectives = true) const;
1324 
1325   /// \brief Returns whether the PresumedLoc for a given SourceLocation is
1326   /// in the main file.
1327   ///
1328   /// This computes the "presumed" location for a SourceLocation, then checks
1329   /// whether it came from a file other than the main file. This is different
1330   /// from isWrittenInMainFile() because it takes line marker directives into
1331   /// account.
1332   bool isInMainFile(SourceLocation Loc) const;
1333 
1334   /// \brief Returns true if the spelling locations for both SourceLocations
1335   /// are part of the same file buffer.
1336   ///
1337   /// This check ignores line marker directives.
isWrittenInSameFile(SourceLocation Loc1,SourceLocation Loc2)1338   bool isWrittenInSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
1339     return getFileID(Loc1) == getFileID(Loc2);
1340   }
1341 
1342   /// \brief Returns true if the spelling location for the given location
1343   /// is in the main file buffer.
1344   ///
1345   /// This check ignores line marker directives.
isWrittenInMainFile(SourceLocation Loc)1346   bool isWrittenInMainFile(SourceLocation Loc) const {
1347     return getFileID(Loc) == getMainFileID();
1348   }
1349 
1350   /// \brief Returns if a SourceLocation is in a system header.
isInSystemHeader(SourceLocation Loc)1351   bool isInSystemHeader(SourceLocation Loc) const {
1352     return getFileCharacteristic(Loc) != SrcMgr::C_User;
1353   }
1354 
1355   /// \brief Returns if a SourceLocation is in an "extern C" system header.
isInExternCSystemHeader(SourceLocation Loc)1356   bool isInExternCSystemHeader(SourceLocation Loc) const {
1357     return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
1358   }
1359 
1360   /// \brief Returns whether \p Loc is expanded from a macro in a system header.
isInSystemMacro(SourceLocation loc)1361   bool isInSystemMacro(SourceLocation loc) {
1362     return loc.isMacroID() && isInSystemHeader(getSpellingLoc(loc));
1363   }
1364 
1365   /// \brief The size of the SLocEntry that \p FID represents.
1366   unsigned getFileIDSize(FileID FID) const;
1367 
1368   /// \brief Given a specific FileID, returns true if \p Loc is inside that
1369   /// FileID chunk and sets relative offset (offset of \p Loc from beginning
1370   /// of FileID) to \p relativeOffset.
1371   bool isInFileID(SourceLocation Loc, FileID FID,
1372                   unsigned *RelativeOffset = nullptr) const {
1373     unsigned Offs = Loc.getOffset();
1374     if (isOffsetInFileID(FID, Offs)) {
1375       if (RelativeOffset)
1376         *RelativeOffset = Offs - getSLocEntry(FID).getOffset();
1377       return true;
1378     }
1379 
1380     return false;
1381   }
1382 
1383   //===--------------------------------------------------------------------===//
1384   // Line Table Manipulation Routines
1385   //===--------------------------------------------------------------------===//
1386 
1387   /// \brief Return the uniqued ID for the specified filename.
1388   ///
1389   unsigned getLineTableFilenameID(StringRef Str);
1390 
1391   /// \brief Add a line note to the line table for the FileID and offset
1392   /// specified by Loc.
1393   ///
1394   /// If FilenameID is -1, it is considered to be unspecified.
1395   void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID);
1396   void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
1397                    bool IsFileEntry, bool IsFileExit,
1398                    bool IsSystemHeader, bool IsExternCHeader);
1399 
1400   /// \brief Determine if the source manager has a line table.
hasLineTable()1401   bool hasLineTable() const { return LineTable != nullptr; }
1402 
1403   /// \brief Retrieve the stored line table.
1404   LineTableInfo &getLineTable();
1405 
1406   //===--------------------------------------------------------------------===//
1407   // Queries for performance analysis.
1408   //===--------------------------------------------------------------------===//
1409 
1410   /// \brief Return the total amount of physical memory allocated by the
1411   /// ContentCache allocator.
getContentCacheSize()1412   size_t getContentCacheSize() const {
1413     return ContentCacheAlloc.getTotalMemory();
1414   }
1415 
1416   struct MemoryBufferSizes {
1417     const size_t malloc_bytes;
1418     const size_t mmap_bytes;
1419 
MemoryBufferSizesMemoryBufferSizes1420     MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
1421       : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
1422   };
1423 
1424   /// \brief Return the amount of memory used by memory buffers, breaking down
1425   /// by heap-backed versus mmap'ed memory.
1426   MemoryBufferSizes getMemoryBufferSizes() const;
1427 
1428   /// \brief Return the amount of memory used for various side tables and
1429   /// data structures in the SourceManager.
1430   size_t getDataStructureSizes() const;
1431 
1432   //===--------------------------------------------------------------------===//
1433   // Other miscellaneous methods.
1434   //===--------------------------------------------------------------------===//
1435 
1436   /// \brief Get the source location for the given file:line:col triplet.
1437   ///
1438   /// If the source file is included multiple times, the source location will
1439   /// be based upon the first inclusion.
1440   SourceLocation translateFileLineCol(const FileEntry *SourceFile,
1441                                       unsigned Line, unsigned Col) const;
1442 
1443   /// \brief Get the FileID for the given file.
1444   ///
1445   /// If the source file is included multiple times, the FileID will be the
1446   /// first inclusion.
1447   FileID translateFile(const FileEntry *SourceFile) const;
1448 
1449   /// \brief Get the source location in \p FID for the given line:col.
1450   /// Returns null location if \p FID is not a file SLocEntry.
1451   SourceLocation translateLineCol(FileID FID,
1452                                   unsigned Line, unsigned Col) const;
1453 
1454   /// \brief If \p Loc points inside a function macro argument, the returned
1455   /// location will be the macro location in which the argument was expanded.
1456   /// If a macro argument is used multiple times, the expanded location will
1457   /// be at the first expansion of the argument.
1458   /// e.g.
1459   ///   MY_MACRO(foo);
1460   ///             ^
1461   /// Passing a file location pointing at 'foo', will yield a macro location
1462   /// where 'foo' was expanded into.
1463   SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const;
1464 
1465   /// \brief Determines the order of 2 source locations in the translation unit.
1466   ///
1467   /// \returns true if LHS source location comes before RHS, false otherwise.
1468   bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
1469 
1470   /// \brief Determines the order of 2 source locations in the "source location
1471   /// address space".
isBeforeInSLocAddrSpace(SourceLocation LHS,SourceLocation RHS)1472   bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const {
1473     return isBeforeInSLocAddrSpace(LHS, RHS.getOffset());
1474   }
1475 
1476   /// \brief Determines the order of a source location and a source location
1477   /// offset in the "source location address space".
1478   ///
1479   /// Note that we always consider source locations loaded from
isBeforeInSLocAddrSpace(SourceLocation LHS,unsigned RHS)1480   bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const {
1481     unsigned LHSOffset = LHS.getOffset();
1482     bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1483     bool RHSLoaded = RHS >= CurrentLoadedOffset;
1484     if (LHSLoaded == RHSLoaded)
1485       return LHSOffset < RHS;
1486 
1487     return LHSLoaded;
1488   }
1489 
1490   // Iterators over FileInfos.
1491   typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>
1492       ::const_iterator fileinfo_iterator;
fileinfo_begin()1493   fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
fileinfo_end()1494   fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
hasFileInfo(const FileEntry * File)1495   bool hasFileInfo(const FileEntry *File) const {
1496     return FileInfos.find(File) != FileInfos.end();
1497   }
1498 
1499   /// \brief Print statistics to stderr.
1500   ///
1501   void PrintStats() const;
1502 
1503   void dump() const;
1504 
1505   /// \brief Get the number of local SLocEntries we have.
local_sloc_entry_size()1506   unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
1507 
1508   /// \brief Get a local SLocEntry. This is exposed for indexing.
1509   const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index,
1510                                              bool *Invalid = nullptr) const {
1511     assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1512     return LocalSLocEntryTable[Index];
1513   }
1514 
1515   /// \brief Get the number of loaded SLocEntries we have.
loaded_sloc_entry_size()1516   unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
1517 
1518   /// \brief Get a loaded SLocEntry. This is exposed for indexing.
1519   const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index,
1520                                               bool *Invalid = nullptr) const {
1521     assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1522     if (SLocEntryLoaded[Index])
1523       return LoadedSLocEntryTable[Index];
1524     return loadSLocEntry(Index, Invalid);
1525   }
1526 
1527   const SrcMgr::SLocEntry &getSLocEntry(FileID FID,
1528                                         bool *Invalid = nullptr) const {
1529     if (FID.ID == 0 || FID.ID == -1) {
1530       if (Invalid) *Invalid = true;
1531       return LocalSLocEntryTable[0];
1532     }
1533     return getSLocEntryByID(FID.ID, Invalid);
1534   }
1535 
getNextLocalOffset()1536   unsigned getNextLocalOffset() const { return NextLocalOffset; }
1537 
setExternalSLocEntrySource(ExternalSLocEntrySource * Source)1538   void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1539     assert(LoadedSLocEntryTable.empty() &&
1540            "Invalidating existing loaded entries");
1541     ExternalSLocEntries = Source;
1542   }
1543 
1544   /// \brief Allocate a number of loaded SLocEntries, which will be actually
1545   /// loaded on demand from the external source.
1546   ///
1547   /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1548   /// in the global source view. The lowest ID and the base offset of the
1549   /// entries will be returned.
1550   std::pair<int, unsigned>
1551   AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize);
1552 
1553   /// \brief Returns true if \p Loc came from a PCH/Module.
isLoadedSourceLocation(SourceLocation Loc)1554   bool isLoadedSourceLocation(SourceLocation Loc) const {
1555     return Loc.getOffset() >= CurrentLoadedOffset;
1556   }
1557 
1558   /// \brief Returns true if \p Loc did not come from a PCH/Module.
isLocalSourceLocation(SourceLocation Loc)1559   bool isLocalSourceLocation(SourceLocation Loc) const {
1560     return Loc.getOffset() < NextLocalOffset;
1561   }
1562 
1563   /// \brief Returns true if \p FID came from a PCH/Module.
isLoadedFileID(FileID FID)1564   bool isLoadedFileID(FileID FID) const {
1565     assert(FID.ID != -1 && "Using FileID sentinel value");
1566     return FID.ID < 0;
1567   }
1568 
1569   /// \brief Returns true if \p FID did not come from a PCH/Module.
isLocalFileID(FileID FID)1570   bool isLocalFileID(FileID FID) const {
1571     return !isLoadedFileID(FID);
1572   }
1573 
1574   /// Gets the location of the immediate macro caller, one level up the stack
1575   /// toward the initial macro typed into the source.
getImmediateMacroCallerLoc(SourceLocation Loc)1576   SourceLocation getImmediateMacroCallerLoc(SourceLocation Loc) const {
1577     if (!Loc.isMacroID()) return Loc;
1578 
1579     // When we have the location of (part of) an expanded parameter, its
1580     // spelling location points to the argument as expanded in the macro call,
1581     // and therefore is used to locate the macro caller.
1582     if (isMacroArgExpansion(Loc))
1583       return getImmediateSpellingLoc(Loc);
1584 
1585     // Otherwise, the caller of the macro is located where this macro is
1586     // expanded (while the spelling is part of the macro definition).
1587     return getImmediateExpansionRange(Loc).first;
1588   }
1589 
1590 private:
1591   llvm::MemoryBuffer *getFakeBufferForRecovery() const;
1592   const SrcMgr::ContentCache *getFakeContentCacheForRecovery() const;
1593 
1594   const SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid) const;
1595 
1596   /// \brief Get the entry with the given unwrapped FileID.
1597   const SrcMgr::SLocEntry &getSLocEntryByID(int ID,
1598                                             bool *Invalid = nullptr) const {
1599     assert(ID != -1 && "Using FileID sentinel value");
1600     if (ID < 0)
1601       return getLoadedSLocEntryByID(ID, Invalid);
1602     return getLocalSLocEntry(static_cast<unsigned>(ID), Invalid);
1603   }
1604 
1605   const SrcMgr::SLocEntry &
1606   getLoadedSLocEntryByID(int ID, bool *Invalid = nullptr) const {
1607     return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2), Invalid);
1608   }
1609 
1610   /// Implements the common elements of storing an expansion info struct into
1611   /// the SLocEntry table and producing a source location that refers to it.
1612   SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
1613                                         unsigned TokLength,
1614                                         int LoadedID = 0,
1615                                         unsigned LoadedOffset = 0);
1616 
1617   /// \brief Return true if the specified FileID contains the
1618   /// specified SourceLocation offset.  This is a very hot method.
isOffsetInFileID(FileID FID,unsigned SLocOffset)1619   inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const {
1620     const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1621     // If the entry is after the offset, it can't contain it.
1622     if (SLocOffset < Entry.getOffset()) return false;
1623 
1624     // If this is the very last entry then it does.
1625     if (FID.ID == -2)
1626       return true;
1627 
1628     // If it is the last local entry, then it does if the location is local.
1629     if (FID.ID+1 == static_cast<int>(LocalSLocEntryTable.size()))
1630       return SLocOffset < NextLocalOffset;
1631 
1632     // Otherwise, the entry after it has to not include it. This works for both
1633     // local and loaded entries.
1634     return SLocOffset < getSLocEntryByID(FID.ID+1).getOffset();
1635   }
1636 
1637   /// \brief Returns the previous in-order FileID or an invalid FileID if there
1638   /// is no previous one.
1639   FileID getPreviousFileID(FileID FID) const;
1640 
1641   /// \brief Returns the next in-order FileID or an invalid FileID if there is
1642   /// no next one.
1643   FileID getNextFileID(FileID FID) const;
1644 
1645   /// \brief Create a new fileID for the specified ContentCache and
1646   /// include position.
1647   ///
1648   /// This works regardless of whether the ContentCache corresponds to a
1649   /// file or some other input source.
1650   FileID createFileID(const SrcMgr::ContentCache* File,
1651                       SourceLocation IncludePos,
1652                       SrcMgr::CharacteristicKind DirCharacter,
1653                       int LoadedID, unsigned LoadedOffset);
1654 
1655   const SrcMgr::ContentCache *
1656     getOrCreateContentCache(const FileEntry *SourceFile,
1657                             bool isSystemFile = false);
1658 
1659   /// \brief Create a new ContentCache for the specified  memory buffer.
1660   const SrcMgr::ContentCache *
1661   createMemBufferContentCache(std::unique_ptr<llvm::MemoryBuffer> Buf);
1662 
1663   FileID getFileIDSlow(unsigned SLocOffset) const;
1664   FileID getFileIDLocal(unsigned SLocOffset) const;
1665   FileID getFileIDLoaded(unsigned SLocOffset) const;
1666 
1667   SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
1668   SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1669   SourceLocation getFileLocSlowCase(SourceLocation Loc) const;
1670 
1671   std::pair<FileID, unsigned>
1672   getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
1673   std::pair<FileID, unsigned>
1674   getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1675                                    unsigned Offset) const;
1676   void computeMacroArgsCache(MacroArgsMap *&MacroArgsCache, FileID FID) const;
1677   void associateFileChunkWithMacroArgExp(MacroArgsMap &MacroArgsCache,
1678                                          FileID FID,
1679                                          SourceLocation SpellLoc,
1680                                          SourceLocation ExpansionLoc,
1681                                          unsigned ExpansionLength) const;
1682   friend class ASTReader;
1683   friend class ASTWriter;
1684 };
1685 
1686 /// \brief Comparison function object.
1687 template<typename T>
1688 class BeforeThanCompare;
1689 
1690 /// \brief Compare two source locations.
1691 template<>
1692 class BeforeThanCompare<SourceLocation> {
1693   SourceManager &SM;
1694 
1695 public:
BeforeThanCompare(SourceManager & SM)1696   explicit BeforeThanCompare(SourceManager &SM) : SM(SM) { }
1697 
operator()1698   bool operator()(SourceLocation LHS, SourceLocation RHS) const {
1699     return SM.isBeforeInTranslationUnit(LHS, RHS);
1700   }
1701 };
1702 
1703 /// \brief Compare two non-overlapping source ranges.
1704 template<>
1705 class BeforeThanCompare<SourceRange> {
1706   SourceManager &SM;
1707 
1708 public:
BeforeThanCompare(SourceManager & SM)1709   explicit BeforeThanCompare(SourceManager &SM) : SM(SM) { }
1710 
operator()1711   bool operator()(SourceRange LHS, SourceRange RHS) const {
1712     return SM.isBeforeInTranslationUnit(LHS.getBegin(), RHS.getBegin());
1713   }
1714 };
1715 
1716 }  // end namespace clang
1717 
1718 
1719 #endif
1720