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