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