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
62 namespace clang {
63
64 class ASTReader;
65 class ASTWriter;
66 class FileManager;
67 class LineTableInfo;
68 class SourceManager;
69
70 /// Public enums and private classes that are part of the
71 /// SourceManager implementation.
72 namespace 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 ///
81 enum 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.
isSystem(CharacteristicKind CK)90 inline 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.
isModuleMap(CharacteristicKind CK)95 inline 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.
101 class LineOffsetMapping {
102 public:
103 explicit operator bool() const { return Storage; }
size()104 unsigned size() const {
105 assert(Storage);
106 return Storage[0];
107 }
getLines()108 ArrayRef<unsigned> getLines() const {
109 assert(Storage);
110 return ArrayRef<unsigned>(Storage + 1, Storage + 1 + size());
111 }
begin()112 const unsigned *begin() const { return getLines().begin(); }
end()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
123 private:
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.
131 class alignas(8) ContentCache {
132 /// The actual buffer containing the characters from the input
133 /// file.
134 mutable std::unique_ptr<llvm::MemoryBuffer> Buffer;
135
136 public:
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
ContentCache()187 ContentCache()
188 : OrigEntry(std::nullopt), ContentsEntry(std::nullopt),
189 BufferOverridden(false), IsFileVolatile(false), IsTransient(false),
190 IsBufferInvalid(false) {}
191
ContentCache(FileEntryRef Ent)192 ContentCache(FileEntryRef Ent) : ContentCache(Ent, Ent) {}
193
ContentCache(FileEntryRef Ent,FileEntryRef contentEnt)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.
ContentCache(const ContentCache & RHS)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.
getBufferIfLoaded()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.
getBufferDataIfLoaded()251 std::optional<StringRef> getBufferDataIfLoaded() const {
252 if (Buffer)
253 return Buffer->getBuffer();
254 return std::nullopt;
255 }
256
257 /// Set the buffer.
setBuffer(std::unique_ptr<llvm::MemoryBuffer> B)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.
setUnownedBuffer(std::optional<llvm::MemoryBufferRef> B)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(*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.
279 static_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 ///
295 class 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
319 public:
320 /// Return a FileInfo object.
get(SourceLocation IL,ContentCache & Con,CharacteristicKind FileCharacter,StringRef Filename)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
getIncludeLoc()333 SourceLocation getIncludeLoc() const {
334 return IncludeLoc;
335 }
336
getContentCache()337 const ContentCache &getContentCache() const {
338 return *ContentAndKind.getPointer();
339 }
340
341 /// Return whether this is a system header or not.
getFileCharacteristic()342 CharacteristicKind getFileCharacteristic() const {
343 return ContentAndKind.getInt();
344 }
345
346 /// Return true if this FileID has \#line directives in it.
hasLineDirectives()347 bool hasLineDirectives() const { return HasLineDirectives; }
348
349 /// Set the flag that indicates that this FileID has
350 /// line table entries associated with it.
setHasLineDirectives()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.
getName()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.
361 class 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
378 public:
getSpellingLoc()379 SourceLocation getSpellingLoc() const {
380 return SpellingLoc.isInvalid() ? getExpansionLocStart() : SpellingLoc;
381 }
382
getExpansionLocStart()383 SourceLocation getExpansionLocStart() const {
384 return ExpansionLocStart;
385 }
386
getExpansionLocEnd()387 SourceLocation getExpansionLocEnd() const {
388 return ExpansionLocEnd.isInvalid() ? getExpansionLocStart()
389 : ExpansionLocEnd;
390 }
391
isExpansionTokenRange()392 bool isExpansionTokenRange() const { return ExpansionIsTokenRange; }
393
getExpansionLocRange()394 CharSourceRange getExpansionLocRange() const {
395 return CharSourceRange(
396 SourceRange(getExpansionLocStart(), getExpansionLocEnd()),
397 isExpansionTokenRange());
398 }
399
isMacroArgExpansion()400 bool isMacroArgExpansion() const {
401 // Note that this needs to return false for default constructed objects.
402 return getExpansionLocStart().isValid() && ExpansionLocEnd.isInvalid();
403 }
404
isMacroBodyExpansion()405 bool isMacroBodyExpansion() const {
406 return getExpansionLocStart().isValid() && ExpansionLocEnd.isValid();
407 }
408
isFunctionMacroExpansion()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'.
createForMacroArg(SourceLocation SpellingLoc,SourceLocation ExpansionLoc)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, ExpansionLoc, 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.
createForTokenSplit(SourceLocation SpellingLoc,SourceLocation Start,SourceLocation End)463 static ExpansionInfo createForTokenSplit(SourceLocation SpellingLoc,
464 SourceLocation Start,
465 SourceLocation End) {
466 return create(SpellingLoc, Start, End, 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.
474 static_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.
481 class 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
491 public:
SLocEntry()492 SLocEntry() : Offset(), IsExpansion(), File() {}
493
getOffset()494 SourceLocation::UIntTy getOffset() const { return Offset; }
495
isExpansion()496 bool isExpansion() const { return IsExpansion; }
isFile()497 bool isFile() const { return !isExpansion(); }
498
getFile()499 const FileInfo &getFile() const {
500 return const_cast<SLocEntry *>(this)->getFile();
501 }
502
getFile()503 FileInfo &getFile() {
504 assert(isFile() && "Not a file SLocEntry!");
505 return File;
506 }
507
getExpansion()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.
getOffsetOnly(SourceLocation::UIntTy 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
get(SourceLocation::UIntTy Offset,const FileInfo & FI)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
get(SourceLocation::UIntTy Offset,const ExpansionInfo & Expansion)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.
544 class ExternalSLocEntrySource {
545 public:
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.
571 class 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
593 public:
594 InBeforeInTUCacheEntry() = default;
InBeforeInTUCacheEntry(FileID L,FileID R)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.
isCacheValid()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.
getCachedResult(unsigned LOffset,unsigned ROffset)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.
setQueryFIDs(FileID LHS,FileID RHS)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
setCommonLoc(FileID commonFID,unsigned lCommonOffset,unsigned rCommonOffset,bool LParentBeforeRParent)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.
649 using 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.
663 class 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
getOverriddenFilesInfo()705 OverriddenFilesInfoTy &getOverriddenFilesInfo() {
706 if (!OverriddenFilesInfo)
707 OverriddenFilesInfo.reset(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> 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
839 public:
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
getDiagnostics()852 DiagnosticsEngine &getDiagnostics() const { return Diag; }
853
getFileManager()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.
setOverridenFilesKeepOriginalName(bool value)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).
userFilesAreVolatile()865 bool userFilesAreVolatile() const { return UserFilesAreVolatile; }
866
867 /// Retrieve the module build stack.
getModuleBuildStack()868 ModuleBuildStack getModuleBuildStack() const {
869 return StoredModuleBuildStack;
870 }
871
872 /// Set the module build stack.
setModuleBuildStack(ModuleBuildStack stack)873 void setModuleBuildStack(ModuleBuildStack stack) {
874 StoredModuleBuildStack.clear();
875 StoredModuleBuildStack.append(stack.begin(), stack.end());
876 }
877
878 /// Push an entry to the module build stack.
pushModuleBuildStack(StringRef moduleName,FullSourceLoc importLoc)879 void pushModuleBuildStack(StringRef moduleName, FullSourceLoc importLoc) {
880 StoredModuleBuildStack.push_back(std::make_pair(moduleName.str(),importLoc));
881 }
882
883 //===--------------------------------------------------------------------===//
884 // MainFileID creation and querying methods.
885 //===--------------------------------------------------------------------===//
886
887 /// Returns the FileID of the main source file.
getMainFileID()888 FileID getMainFileID() const { return MainFileID; }
889
890 /// Set the file ID for the main source file.
setMainFileID(FileID FID)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.
setPreambleFileID(FileID 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.
getPreambleFileID()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.
getMemoryBufferForFileOrFake(FileEntryRef File)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.
overrideFileContents(FileEntryRef SourceFile,const llvm::MemoryBufferRef & Buffer)992 void overrideFileContents(FileEntryRef SourceFile,
993 const llvm::MemoryBufferRef &Buffer) {
994 overrideFileContents(SourceFile, llvm::MemoryBuffer::getMemBuffer(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.
isFileOverridden(const FileEntry * File)1016 bool isFileOverridden(const FileEntry *File) const {
1017 if (OverriddenFilesInfo) {
1018 if (OverriddenFilesInfo->OverriddenFilesWithBuffer.count(File))
1019 return true;
1020 if (OverriddenFilesInfo->OverriddenFiles.contains(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.
setAllFilesAreTransient(bool 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, 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.
getFileEntryForID(FileID FID)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.
getFileEntryRefForID(FileID FID)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 *
getFileEntryForSLocEntry(const SrcMgr::SLocEntry & SLocEntry)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.
getNumCreatedFIDsForFileID(FileID FID)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 ///
getFileID(SourceLocation SpellingLoc)1145 FileID getFileID(SourceLocation SpellingLoc) const {
1146 return getFileID(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.
getLocForStartOfFile(FileID FID)1154 SourceLocation getLocForStartOfFile(FileID FID) const {
1155 if (auto *Entry = getSLocEntryForFile(FID))
1156 return SourceLocation::getFileLoc(Entry->getOffset());
1157 return SourceLocation();
1158 }
1159
1160 /// Return the source location corresponding to the last byte of the
1161 /// specified file.
getLocForEndOfFile(FileID FID)1162 SourceLocation getLocForEndOfFile(FileID FID) const {
1163 if (auto *Entry = getSLocEntryForFile(FID))
1164 return SourceLocation::getFileLoc(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.
getIncludeLoc(FileID FID)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>
getModuleImportLoc(SourceLocation Loc)1181 getModuleImportLoc(SourceLocation Loc) const {
1182 FileID FID = getFileID(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(SourceLocation(), "");
1188
1189 return ExternalSLocEntries->getModuleImportLoc(FID.ID);
1190 }
1191
1192 /// Given a SourceLocation object \p Loc, return the expansion
1193 /// location referenced by the ID.
getExpansionLoc(SourceLocation Loc)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.
getFileLoc(SourceLocation Loc)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.
getExpansionRange(SourceRange Range)1221 CharSourceRange getExpansionRange(SourceRange Range) const {
1222 SourceLocation Begin = getExpansionRange(Range.getBegin()).getBegin();
1223 CharSourceRange End = getExpansionRange(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.
getExpansionRange(CharSourceRange Range)1230 CharSourceRange getExpansionRange(CharSourceRange Range) const {
1231 CharSourceRange Expansion = getExpansionRange(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.
getSpellingLoc(SourceLocation Loc)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.
getComposedLoc(FileID FID,unsigned Offset)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(GlobalOffset)
1265 : SourceLocation::getMacroLoc(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.
getDecomposedLoc(SourceLocation Loc)1272 std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const {
1273 FileID FID = getFileID(Loc);
1274 auto *Entry = getSLocEntryOrNull(FID);
1275 if (!Entry)
1276 return std::make_pair(FileID(), 0);
1277 return std::make_pair(FID, 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>
getDecomposedExpansionLoc(SourceLocation Loc)1285 getDecomposedExpansionLoc(SourceLocation Loc) const {
1286 FileID FID = getFileID(Loc);
1287 auto *E = getSLocEntryOrNull(FID);
1288 if (!E)
1289 return std::make_pair(FileID(), 0);
1290
1291 unsigned Offset = Loc.getOffset()-E->getOffset();
1292 if (Loc.isFileID())
1293 return std::make_pair(FID, 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>
getDecomposedSpellingLoc(SourceLocation Loc)1303 getDecomposedSpellingLoc(SourceLocation Loc) const {
1304 FileID FID = getFileID(Loc);
1305 auto *E = getSLocEntryOrNull(FID);
1306 if (!E)
1307 return std::make_pair(FileID(), 0);
1308
1309 unsigned Offset = Loc.getOffset()-E->getOffset();
1310 if (Loc.isFileID())
1311 return std::make_pair(FID, 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.
getFileOffset(SourceLocation SpellingLoc)1323 unsigned getFileOffset(SourceLocation SpellingLoc) const {
1324 return getDecomposedLoc(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.
isInSameSLocAddrSpace(SourceLocation LHS,SourceLocation RHS,SourceLocation::IntTy * RelativeOffset)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.
isWrittenInSameFile(SourceLocation Loc1,SourceLocation Loc2)1493 bool isWrittenInSameFile(SourceLocation Loc1, SourceLocation Loc2) const {
1494 return getFileID(Loc1) == getFileID(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.
isWrittenInMainFile(SourceLocation Loc)1501 bool isWrittenInMainFile(SourceLocation Loc) const {
1502 return getFileID(Loc) == getMainFileID();
1503 }
1504
1505 /// Returns whether \p Loc is located in a <built-in> file.
isWrittenInBuiltinFile(SourceLocation Loc)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.
isWrittenInCommandLineFile(SourceLocation Loc)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.
isWrittenInScratchSpace(SourceLocation Loc)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 if a SourceLocation is in a system header.
isInSystemHeader(SourceLocation Loc)1533 bool isInSystemHeader(SourceLocation Loc) const {
1534 if (Loc.isInvalid())
1535 return false;
1536 return isSystem(getFileCharacteristic(Loc));
1537 }
1538
1539 /// Returns if a SourceLocation is in an "extern C" system header.
isInExternCSystemHeader(SourceLocation Loc)1540 bool isInExternCSystemHeader(SourceLocation Loc) const {
1541 return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem;
1542 }
1543
1544 /// Returns whether \p Loc is expanded from a macro in a system header.
isInSystemMacro(SourceLocation loc)1545 bool isInSystemMacro(SourceLocation loc) const {
1546 if (!loc.isMacroID())
1547 return false;
1548
1549 // This happens when the macro is the result of a paste, in that case
1550 // its spelling is the scratch memory, so we take the parent context.
1551 // There can be several level of token pasting.
1552 if (isWrittenInScratchSpace(getSpellingLoc(loc))) {
1553 do {
1554 loc = getImmediateMacroCallerLoc(loc);
1555 } while (isWrittenInScratchSpace(getSpellingLoc(loc)));
1556 return isInSystemMacro(loc);
1557 }
1558
1559 return isInSystemHeader(getSpellingLoc(loc));
1560 }
1561
1562 /// The size of the SLocEntry that \p FID represents.
1563 unsigned getFileIDSize(FileID FID) const;
1564
1565 /// Given a specific FileID, returns true if \p Loc is inside that
1566 /// FileID chunk and sets relative offset (offset of \p Loc from beginning
1567 /// of FileID) to \p relativeOffset.
1568 bool isInFileID(SourceLocation Loc, FileID FID,
1569 unsigned *RelativeOffset = nullptr) const {
1570 SourceLocation::UIntTy Offs = Loc.getOffset();
1571 if (isOffsetInFileID(FID, Offs)) {
1572 if (RelativeOffset)
1573 *RelativeOffset = Offs - getSLocEntry(FID).getOffset();
1574 return true;
1575 }
1576
1577 return false;
1578 }
1579
1580 //===--------------------------------------------------------------------===//
1581 // Line Table Manipulation Routines
1582 //===--------------------------------------------------------------------===//
1583
1584 /// Return the uniqued ID for the specified filename.
1585 unsigned getLineTableFilenameID(StringRef Str);
1586
1587 /// Add a line note to the line table for the FileID and offset
1588 /// specified by Loc.
1589 ///
1590 /// If FilenameID is -1, it is considered to be unspecified.
1591 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID,
1592 bool IsFileEntry, bool IsFileExit,
1593 SrcMgr::CharacteristicKind FileKind);
1594
1595 /// Determine if the source manager has a line table.
hasLineTable()1596 bool hasLineTable() const { return LineTable != nullptr; }
1597
1598 /// Retrieve the stored line table.
1599 LineTableInfo &getLineTable();
1600
1601 //===--------------------------------------------------------------------===//
1602 // Queries for performance analysis.
1603 //===--------------------------------------------------------------------===//
1604
1605 /// Return the total amount of physical memory allocated by the
1606 /// ContentCache allocator.
getContentCacheSize()1607 size_t getContentCacheSize() const {
1608 return ContentCacheAlloc.getTotalMemory();
1609 }
1610
1611 struct MemoryBufferSizes {
1612 const size_t malloc_bytes;
1613 const size_t mmap_bytes;
1614
MemoryBufferSizesMemoryBufferSizes1615 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes)
1616 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {}
1617 };
1618
1619 /// Return the amount of memory used by memory buffers, breaking down
1620 /// by heap-backed versus mmap'ed memory.
1621 MemoryBufferSizes getMemoryBufferSizes() const;
1622
1623 /// Return the amount of memory used for various side tables and
1624 /// data structures in the SourceManager.
1625 size_t getDataStructureSizes() const;
1626
1627 //===--------------------------------------------------------------------===//
1628 // Other miscellaneous methods.
1629 //===--------------------------------------------------------------------===//
1630
1631 /// Get the source location for the given file:line:col triplet.
1632 ///
1633 /// If the source file is included multiple times, the source location will
1634 /// be based upon the first inclusion.
1635 SourceLocation translateFileLineCol(const FileEntry *SourceFile,
1636 unsigned Line, unsigned Col) const;
1637
1638 /// Get the FileID for the given file.
1639 ///
1640 /// If the source file is included multiple times, the FileID will be the
1641 /// first inclusion.
1642 FileID translateFile(const FileEntry *SourceFile) const;
translateFile(FileEntryRef SourceFile)1643 FileID translateFile(FileEntryRef SourceFile) const {
1644 return translateFile(&SourceFile.getFileEntry());
1645 }
1646
1647 /// Get the source location in \p FID for the given line:col.
1648 /// Returns null location if \p FID is not a file SLocEntry.
1649 SourceLocation translateLineCol(FileID FID,
1650 unsigned Line, unsigned Col) const;
1651
1652 /// If \p Loc points inside a function macro argument, the returned
1653 /// location will be the macro location in which the argument was expanded.
1654 /// If a macro argument is used multiple times, the expanded location will
1655 /// be at the first expansion of the argument.
1656 /// e.g.
1657 /// MY_MACRO(foo);
1658 /// ^
1659 /// Passing a file location pointing at 'foo', will yield a macro location
1660 /// where 'foo' was expanded into.
1661 SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const;
1662
1663 /// Determines the order of 2 source locations in the translation unit.
1664 ///
1665 /// \returns true if LHS source location comes before RHS, false otherwise.
1666 bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const;
1667
1668 /// Determines whether the two decomposed source location is in the
1669 /// same translation unit. As a byproduct, it also calculates the order
1670 /// of the source locations in case they are in the same TU.
1671 ///
1672 /// \returns Pair of bools the first component is true if the two locations
1673 /// are in the same TU. The second bool is true if the first is true
1674 /// and \p LOffs is before \p ROffs.
1675 std::pair<bool, bool>
1676 isInTheSameTranslationUnit(std::pair<FileID, unsigned> &LOffs,
1677 std::pair<FileID, unsigned> &ROffs) const;
1678
1679 /// \param Loc a source location in a loaded AST (of a PCH/Module file).
1680 /// \returns a FileID uniquely identifies the AST of a loaded
1681 /// module/PCH where `Loc` is at.
1682 FileID getUniqueLoadedASTFileID(SourceLocation Loc) const;
1683
1684 /// Determines whether the two decomposed source location is in the same TU.
1685 bool isInTheSameTranslationUnitImpl(
1686 const std::pair<FileID, unsigned> &LOffs,
1687 const std::pair<FileID, unsigned> &ROffs) const;
1688
1689 /// Determines the order of 2 source locations in the "source location
1690 /// address space".
isBeforeInSLocAddrSpace(SourceLocation LHS,SourceLocation RHS)1691 bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const {
1692 return isBeforeInSLocAddrSpace(LHS, RHS.getOffset());
1693 }
1694
1695 /// Determines the order of a source location and a source location
1696 /// offset in the "source location address space".
1697 ///
1698 /// Note that we always consider source locations loaded from
isBeforeInSLocAddrSpace(SourceLocation LHS,SourceLocation::UIntTy RHS)1699 bool isBeforeInSLocAddrSpace(SourceLocation LHS,
1700 SourceLocation::UIntTy RHS) const {
1701 SourceLocation::UIntTy LHSOffset = LHS.getOffset();
1702 bool LHSLoaded = LHSOffset >= CurrentLoadedOffset;
1703 bool RHSLoaded = RHS >= CurrentLoadedOffset;
1704 if (LHSLoaded == RHSLoaded)
1705 return LHSOffset < RHS;
1706
1707 return LHSLoaded;
1708 }
1709
1710 /// Return true if the Point is within Start and End.
isPointWithin(SourceLocation Location,SourceLocation Start,SourceLocation End)1711 bool isPointWithin(SourceLocation Location, SourceLocation Start,
1712 SourceLocation End) const {
1713 return Location == Start || Location == End ||
1714 (isBeforeInTranslationUnit(Start, Location) &&
1715 isBeforeInTranslationUnit(Location, End));
1716 }
1717
1718 // Iterators over FileInfos.
1719 using fileinfo_iterator =
1720 llvm::DenseMap<FileEntryRef, SrcMgr::ContentCache *>::const_iterator;
1721
fileinfo_begin()1722 fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); }
fileinfo_end()1723 fileinfo_iterator fileinfo_end() const { return FileInfos.end(); }
hasFileInfo(const FileEntry * File)1724 bool hasFileInfo(const FileEntry *File) const {
1725 return FileInfos.find_as(File) != FileInfos.end();
1726 }
1727
1728 /// Print statistics to stderr.
1729 void PrintStats() const;
1730
1731 void dump() const;
1732
1733 // Produce notes describing the current source location address space usage.
1734 void noteSLocAddressSpaceUsage(DiagnosticsEngine &Diag,
1735 std::optional<unsigned> MaxNotes = 32) const;
1736
1737 /// Get the number of local SLocEntries we have.
local_sloc_entry_size()1738 unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); }
1739
1740 /// Get a local SLocEntry. This is exposed for indexing.
getLocalSLocEntry(unsigned Index)1741 const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index) const {
1742 return const_cast<SourceManager *>(this)->getLocalSLocEntry(Index);
1743 }
1744
1745 /// Get a local SLocEntry. This is exposed for indexing.
getLocalSLocEntry(unsigned Index)1746 SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index) {
1747 assert(Index < LocalSLocEntryTable.size() && "Invalid index");
1748 return LocalSLocEntryTable[Index];
1749 }
1750
1751 /// Get the number of loaded SLocEntries we have.
loaded_sloc_entry_size()1752 unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();}
1753
1754 /// Get a loaded SLocEntry. This is exposed for indexing.
1755 const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index,
1756 bool *Invalid = nullptr) const {
1757 return const_cast<SourceManager *>(this)->getLoadedSLocEntry(Index,
1758 Invalid);
1759 }
1760
1761 /// Get a loaded SLocEntry. This is exposed for indexing.
1762 SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index,
1763 bool *Invalid = nullptr) {
1764 assert(Index < LoadedSLocEntryTable.size() && "Invalid index");
1765 if (SLocEntryLoaded[Index])
1766 return LoadedSLocEntryTable[Index];
1767 return loadSLocEntry(Index, Invalid);
1768 }
1769
1770 const SrcMgr::SLocEntry &getSLocEntry(FileID FID,
1771 bool *Invalid = nullptr) const {
1772 return const_cast<SourceManager *>(this)->getSLocEntry(FID, Invalid);
1773 }
1774
1775 SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = nullptr) {
1776 if (FID.ID == 0 || FID.ID == -1) {
1777 if (Invalid) *Invalid = true;
1778 return LocalSLocEntryTable[0];
1779 }
1780 return getSLocEntryByID(FID.ID, Invalid);
1781 }
1782
getNextLocalOffset()1783 SourceLocation::UIntTy getNextLocalOffset() const { return NextLocalOffset; }
1784
setExternalSLocEntrySource(ExternalSLocEntrySource * Source)1785 void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) {
1786 assert(LoadedSLocEntryTable.empty() &&
1787 "Invalidating existing loaded entries");
1788 ExternalSLocEntries = Source;
1789 }
1790
1791 /// Allocate a number of loaded SLocEntries, which will be actually
1792 /// loaded on demand from the external source.
1793 ///
1794 /// NumSLocEntries will be allocated, which occupy a total of TotalSize space
1795 /// in the global source view. The lowest ID and the base offset of the
1796 /// entries will be returned.
1797 std::pair<int, SourceLocation::UIntTy>
1798 AllocateLoadedSLocEntries(unsigned NumSLocEntries,
1799 SourceLocation::UIntTy TotalSize);
1800
1801 /// Returns true if \p Loc came from a PCH/Module.
isLoadedSourceLocation(SourceLocation Loc)1802 bool isLoadedSourceLocation(SourceLocation Loc) const {
1803 return isLoadedOffset(Loc.getOffset());
1804 }
1805
1806 /// Returns true if \p Loc did not come from a PCH/Module.
isLocalSourceLocation(SourceLocation Loc)1807 bool isLocalSourceLocation(SourceLocation Loc) const {
1808 return isLocalOffset(Loc.getOffset());
1809 }
1810
1811 /// Returns true if \p FID came from a PCH/Module.
isLoadedFileID(FileID FID)1812 bool isLoadedFileID(FileID FID) const {
1813 assert(FID.ID != -1 && "Using FileID sentinel value");
1814 return FID.ID < 0;
1815 }
1816
1817 /// Returns true if \p FID did not come from a PCH/Module.
isLocalFileID(FileID FID)1818 bool isLocalFileID(FileID FID) const {
1819 return !isLoadedFileID(FID);
1820 }
1821
1822 /// Gets the location of the immediate macro caller, one level up the stack
1823 /// toward the initial macro typed into the source.
getImmediateMacroCallerLoc(SourceLocation Loc)1824 SourceLocation getImmediateMacroCallerLoc(SourceLocation Loc) const {
1825 if (!Loc.isMacroID()) return Loc;
1826
1827 // When we have the location of (part of) an expanded parameter, its
1828 // spelling location points to the argument as expanded in the macro call,
1829 // and therefore is used to locate the macro caller.
1830 if (isMacroArgExpansion(Loc))
1831 return getImmediateSpellingLoc(Loc);
1832
1833 // Otherwise, the caller of the macro is located where this macro is
1834 // expanded (while the spelling is part of the macro definition).
1835 return getImmediateExpansionRange(Loc).getBegin();
1836 }
1837
1838 /// \return Location of the top-level macro caller.
1839 SourceLocation getTopMacroCallerLoc(SourceLocation Loc) const;
1840
1841 private:
1842 friend class ASTReader;
1843 friend class ASTWriter;
1844
1845 llvm::MemoryBufferRef getFakeBufferForRecovery() const;
1846 SrcMgr::ContentCache &getFakeContentCacheForRecovery() const;
1847
1848 const SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid) const;
1849 SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid);
1850
getSLocEntryOrNull(FileID FID)1851 const SrcMgr::SLocEntry *getSLocEntryOrNull(FileID FID) const {
1852 return const_cast<SourceManager *>(this)->getSLocEntryOrNull(FID);
1853 }
1854
getSLocEntryOrNull(FileID FID)1855 SrcMgr::SLocEntry *getSLocEntryOrNull(FileID FID) {
1856 bool Invalid = false;
1857 SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1858 return Invalid ? nullptr : &Entry;
1859 }
1860
getSLocEntryForFile(FileID FID)1861 const SrcMgr::SLocEntry *getSLocEntryForFile(FileID FID) const {
1862 return const_cast<SourceManager *>(this)->getSLocEntryForFile(FID);
1863 }
1864
getSLocEntryForFile(FileID FID)1865 SrcMgr::SLocEntry *getSLocEntryForFile(FileID FID) {
1866 if (auto *Entry = getSLocEntryOrNull(FID))
1867 if (Entry->isFile())
1868 return Entry;
1869 return nullptr;
1870 }
1871
1872 /// Get the entry with the given unwrapped FileID.
1873 /// Invalid will not be modified for Local IDs.
1874 const SrcMgr::SLocEntry &getSLocEntryByID(int ID,
1875 bool *Invalid = nullptr) const {
1876 return const_cast<SourceManager *>(this)->getSLocEntryByID(ID, Invalid);
1877 }
1878
1879 SrcMgr::SLocEntry &getSLocEntryByID(int ID, bool *Invalid = nullptr) {
1880 assert(ID != -1 && "Using FileID sentinel value");
1881 if (ID < 0)
1882 return getLoadedSLocEntryByID(ID, Invalid);
1883 return getLocalSLocEntry(static_cast<unsigned>(ID));
1884 }
1885
1886 const SrcMgr::SLocEntry &
1887 getLoadedSLocEntryByID(int ID, bool *Invalid = nullptr) const {
1888 return const_cast<SourceManager *>(this)->getLoadedSLocEntryByID(ID,
1889 Invalid);
1890 }
1891
1892 SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID, bool *Invalid = nullptr) {
1893 return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2), Invalid);
1894 }
1895
getFileID(SourceLocation::UIntTy SLocOffset)1896 FileID getFileID(SourceLocation::UIntTy SLocOffset) const {
1897 // If our one-entry cache covers this offset, just return it.
1898 if (isOffsetInFileID(LastFileIDLookup, SLocOffset))
1899 return LastFileIDLookup;
1900
1901 return getFileIDSlow(SLocOffset);
1902 }
1903
isLocalOffset(SourceLocation::UIntTy SLocOffset)1904 bool isLocalOffset(SourceLocation::UIntTy SLocOffset) const {
1905 return SLocOffset < CurrentLoadedOffset;
1906 }
1907
isLoadedOffset(SourceLocation::UIntTy SLocOffset)1908 bool isLoadedOffset(SourceLocation::UIntTy SLocOffset) const {
1909 return SLocOffset >= CurrentLoadedOffset;
1910 }
1911
1912 /// Implements the common elements of storing an expansion info struct into
1913 /// the SLocEntry table and producing a source location that refers to it.
1914 SourceLocation
1915 createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion,
1916 unsigned Length, int LoadedID = 0,
1917 SourceLocation::UIntTy LoadedOffset = 0);
1918
1919 /// Return true if the specified FileID contains the
1920 /// specified SourceLocation offset. This is a very hot method.
isOffsetInFileID(FileID FID,SourceLocation::UIntTy SLocOffset)1921 inline bool isOffsetInFileID(FileID FID,
1922 SourceLocation::UIntTy SLocOffset) const {
1923 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID);
1924 // If the entry is after the offset, it can't contain it.
1925 if (SLocOffset < Entry.getOffset()) return false;
1926
1927 // If this is the very last entry then it does.
1928 if (FID.ID == -2)
1929 return true;
1930
1931 // If it is the last local entry, then it does if the location is local.
1932 if (FID.ID+1 == static_cast<int>(LocalSLocEntryTable.size()))
1933 return SLocOffset < NextLocalOffset;
1934
1935 // Otherwise, the entry after it has to not include it. This works for both
1936 // local and loaded entries.
1937 return SLocOffset < getSLocEntryByID(FID.ID+1).getOffset();
1938 }
1939
1940 /// Returns the previous in-order FileID or an invalid FileID if there
1941 /// is no previous one.
1942 FileID getPreviousFileID(FileID FID) const;
1943
1944 /// Returns the next in-order FileID or an invalid FileID if there is
1945 /// no next one.
1946 FileID getNextFileID(FileID FID) const;
1947
1948 /// Create a new fileID for the specified ContentCache and
1949 /// include position.
1950 ///
1951 /// This works regardless of whether the ContentCache corresponds to a
1952 /// file or some other input source.
1953 FileID createFileIDImpl(SrcMgr::ContentCache &File, StringRef Filename,
1954 SourceLocation IncludePos,
1955 SrcMgr::CharacteristicKind DirCharacter, int LoadedID,
1956 SourceLocation::UIntTy LoadedOffset);
1957
1958 SrcMgr::ContentCache &getOrCreateContentCache(FileEntryRef SourceFile,
1959 bool isSystemFile = false);
1960
1961 /// Create a new ContentCache for the specified memory buffer.
1962 SrcMgr::ContentCache &
1963 createMemBufferContentCache(std::unique_ptr<llvm::MemoryBuffer> Buf);
1964
1965 FileID getFileIDSlow(SourceLocation::UIntTy SLocOffset) const;
1966 FileID getFileIDLocal(SourceLocation::UIntTy SLocOffset) const;
1967 FileID getFileIDLoaded(SourceLocation::UIntTy SLocOffset) const;
1968
1969 SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const;
1970 SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const;
1971 SourceLocation getFileLocSlowCase(SourceLocation Loc) const;
1972
1973 std::pair<FileID, unsigned>
1974 getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const;
1975 std::pair<FileID, unsigned>
1976 getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
1977 unsigned Offset) const;
1978 void computeMacroArgsCache(MacroArgsMap &MacroArgsCache, FileID FID) const;
1979 void associateFileChunkWithMacroArgExp(MacroArgsMap &MacroArgsCache,
1980 FileID FID,
1981 SourceLocation SpellLoc,
1982 SourceLocation ExpansionLoc,
1983 unsigned ExpansionLength) const;
1984 void updateSlocUsageStats() const;
1985 };
1986
1987 /// Comparison function object.
1988 template<typename T>
1989 class BeforeThanCompare;
1990
1991 /// Compare two source locations.
1992 template<>
1993 class BeforeThanCompare<SourceLocation> {
1994 SourceManager &SM;
1995
1996 public:
BeforeThanCompare(SourceManager & SM)1997 explicit BeforeThanCompare(SourceManager &SM) : SM(SM) {}
1998
operator()1999 bool operator()(SourceLocation LHS, SourceLocation RHS) const {
2000 return SM.isBeforeInTranslationUnit(LHS, RHS);
2001 }
2002 };
2003
2004 /// Compare two non-overlapping source ranges.
2005 template<>
2006 class BeforeThanCompare<SourceRange> {
2007 SourceManager &SM;
2008
2009 public:
BeforeThanCompare(SourceManager & SM)2010 explicit BeforeThanCompare(SourceManager &SM) : SM(SM) {}
2011
operator()2012 bool operator()(SourceRange LHS, SourceRange RHS) const {
2013 return SM.isBeforeInTranslationUnit(LHS.getBegin(), RHS.getBegin());
2014 }
2015 };
2016
2017 /// SourceManager and necessary dependencies (e.g. VFS, FileManager) for a
2018 /// single in-memorty file.
2019 class SourceManagerForFile {
2020 public:
2021 /// Creates SourceManager and necessary dependencies (e.g. VFS, FileManager).
2022 /// The main file in the SourceManager will be \p FileName with \p Content.
2023 SourceManagerForFile(StringRef FileName, StringRef Content);
2024
get()2025 SourceManager &get() {
2026 assert(SourceMgr);
2027 return *SourceMgr;
2028 }
2029
2030 private:
2031 // The order of these fields are important - they should be in the same order
2032 // as they are created in `createSourceManagerForFile` so that they can be
2033 // deleted in the reverse order as they are created.
2034 std::unique_ptr<FileManager> FileMgr;
2035 std::unique_ptr<DiagnosticsEngine> Diagnostics;
2036 std::unique_ptr<SourceManager> SourceMgr;
2037 };
2038
2039 } // namespace clang
2040
2041 #endif // LLVM_CLANG_BASIC_SOURCEMANAGER_H
2042