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