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

Provided by KDAB

Privacy Policy
Update your C++ knowledge – Modern C++11/14/17 Training
Find out more

source code of clang/include/clang/Basic/SourceManager.h