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