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