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