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