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