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