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 unsigned Offset : 31; 469 unsigned IsExpansion : 1; 470 union { 471 FileInfo File; 472 ExpansionInfo Expansion; 473 }; 474 475 public: 476 SLocEntry() : Offset(), IsExpansion(), File() {} 477 478 unsigned getOffset() const { return Offset; } 479 480 bool isExpansion() const { return IsExpansion; } 481 bool isFile() const { return !isExpansion(); } 482 483 const FileInfo &getFile() const { 484 assert(isFile() && "Not a file SLocEntry!"); 485 return File; 486 } 487 488 const ExpansionInfo &getExpansion() const { 489 assert(isExpansion() && "Not a macro expansion SLocEntry!"); 490 return Expansion; 491 } 492 493 static SLocEntry get(unsigned Offset, const FileInfo &FI) { 494 assert(!(Offset & (1u << 31)) && "Offset is too large"); 495 SLocEntry E; 496 E.Offset = Offset; 497 E.IsExpansion = false; 498 E.File = FI; 499 return E; 500 } 501 502 static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) { 503 assert(!(Offset & (1u << 31)) && "Offset is too large"); 504 SLocEntry E; 505 E.Offset = Offset; 506 E.IsExpansion = true; 507 new (&E.Expansion) ExpansionInfo(Expansion); 508 return E; 509 } 510 }; 511 512 } // namespace SrcMgr 513 514 /// External source of source location entries. 515 class ExternalSLocEntrySource { 516 public: 517 virtual ~ExternalSLocEntrySource(); 518 519 /// Read the source location entry with index ID, which will always be 520 /// less than -1. 521 /// 522 /// \returns true if an error occurred that prevented the source-location 523 /// entry from being loaded. 524 virtual bool ReadSLocEntry(int ID) = 0; 525 526 /// Retrieve the module import location and name for the given ID, if 527 /// in fact it was loaded from a module (rather than, say, a precompiled 528 /// header). 529 virtual std::pair<SourceLocation, StringRef> getModuleImportLoc(int ID) = 0; 530 }; 531 532 /// Holds the cache used by isBeforeInTranslationUnit. 533 /// 534 /// The cache structure is complex enough to be worth breaking out of 535 /// SourceManager. 536 class InBeforeInTUCacheEntry { 537 /// The FileID's of the cached query. 538 /// 539 /// If these match up with a subsequent query, the result can be reused. 540 FileID LQueryFID, RQueryFID; 541 542 /// True if LQueryFID was created before RQueryFID. 543 /// 544 /// This is used to compare macro expansion locations. 545 bool IsLQFIDBeforeRQFID; 546 547 /// The file found in common between the two \#include traces, i.e., 548 /// the nearest common ancestor of the \#include tree. 549 FileID CommonFID; 550 551 /// The offset of the previous query in CommonFID. 552 /// 553 /// Usually, this represents the location of the \#include for QueryFID, but 554 /// if LQueryFID is a parent of RQueryFID (or vice versa) then these can be a 555 /// random token in the parent. 556 unsigned LCommonOffset, RCommonOffset; 557 558 public: 559 /// Return true if the currently cached values match up with 560 /// the specified LHS/RHS query. 561 /// 562 /// If not, we can't use the cache. 563 bool isCacheValid(FileID LHS, FileID RHS) const { 564 return LQueryFID == LHS && RQueryFID == RHS; 565 } 566 567 /// If the cache is valid, compute the result given the 568 /// specified offsets in the LHS/RHS FileID's. 569 bool getCachedResult(unsigned LOffset, unsigned ROffset) const { 570 // If one of the query files is the common file, use the offset. Otherwise, 571 // use the #include loc in the common file. 572 if (LQueryFID != CommonFID) LOffset = LCommonOffset; 573 if (RQueryFID != CommonFID) ROffset = RCommonOffset; 574 575 // It is common for multiple macro expansions to be "included" from the same 576 // location (expansion location), in which case use the order of the FileIDs 577 // to determine which came first. This will also take care the case where 578 // one of the locations points at the inclusion/expansion point of the other 579 // in which case its FileID will come before the other. 580 if (LOffset == ROffset) 581 return IsLQFIDBeforeRQFID; 582 583 return LOffset < ROffset; 584 } 585 586 /// Set up a new query. 587 void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) { 588 assert(LHS != RHS); 589 LQueryFID = LHS; 590 RQueryFID = RHS; 591 IsLQFIDBeforeRQFID = isLFIDBeforeRFID; 592 } 593 594 void clear() { 595 LQueryFID = RQueryFID = FileID(); 596 IsLQFIDBeforeRQFID = false; 597 } 598 599 void setCommonLoc(FileID commonFID, unsigned lCommonOffset, 600 unsigned rCommonOffset) { 601 CommonFID = commonFID; 602 LCommonOffset = lCommonOffset; 603 RCommonOffset = rCommonOffset; 604 } 605 }; 606 607 /// The stack used when building modules on demand, which is used 608 /// to provide a link between the source managers of the different compiler 609 /// instances. 610 using ModuleBuildStack = ArrayRef<std::pair<std::string, FullSourceLoc>>; 611 612 /// This class handles loading and caching of source files into memory. 613 /// 614 /// This object owns the MemoryBuffer objects for all of the loaded 615 /// files and assigns unique FileID's for each unique \#include chain. 616 /// 617 /// The SourceManager can be queried for information about SourceLocation 618 /// objects, turning them into either spelling or expansion locations. Spelling 619 /// locations represent where the bytes corresponding to a token came from and 620 /// expansion locations represent where the location is in the user's view. In 621 /// the case of a macro expansion, for example, the spelling location indicates 622 /// where the expanded token came from and the expansion location specifies 623 /// where it was expanded. 624 class SourceManager : public RefCountedBase<SourceManager> { 625 /// DiagnosticsEngine object. 626 DiagnosticsEngine &Diag; 627 628 FileManager &FileMgr; 629 630 mutable llvm::BumpPtrAllocator ContentCacheAlloc; 631 632 /// Memoized information about all of the files tracked by this 633 /// SourceManager. 634 /// 635 /// This map allows us to merge ContentCache entries based 636 /// on their FileEntry*. All ContentCache objects will thus have unique, 637 /// non-null, FileEntry pointers. 638 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos; 639 640 /// True if the ContentCache for files that are overridden by other 641 /// files, should report the original file name. Defaults to true. 642 bool OverridenFilesKeepOriginalName = true; 643 644 /// True if non-system source files should be treated as volatile 645 /// (likely to change while trying to use them). Defaults to false. 646 bool UserFilesAreVolatile; 647 648 /// True if all files read during this compilation should be treated 649 /// as transient (may not be present in later compilations using a module 650 /// file created from this compilation). Defaults to false. 651 bool FilesAreTransient = false; 652 653 struct OverriddenFilesInfoTy { 654 /// Files that have been overridden with the contents from another 655 /// file. 656 llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles; 657 658 /// Files that were overridden with a memory buffer. 659 llvm::DenseSet<const FileEntry *> OverriddenFilesWithBuffer; 660 }; 661 662 /// Lazily create the object keeping overridden files info, since 663 /// it is uncommonly used. 664 std::unique_ptr<OverriddenFilesInfoTy> OverriddenFilesInfo; 665 666 OverriddenFilesInfoTy &getOverriddenFilesInfo() { 667 if (!OverriddenFilesInfo) 668 OverriddenFilesInfo.reset(new OverriddenFilesInfoTy); 669 return *OverriddenFilesInfo; 670 } 671 672 /// Information about various memory buffers that we have read in. 673 /// 674 /// All FileEntry* within the stored ContentCache objects are NULL, 675 /// as they do not refer to a file. 676 std::vector<SrcMgr::ContentCache*> MemBufferInfos; 677 678 /// The table of SLocEntries that are local to this module. 679 /// 680 /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid 681 /// expansion. 682 SmallVector<SrcMgr::SLocEntry, 0> LocalSLocEntryTable; 683 684 /// The table of SLocEntries that are loaded from other modules. 685 /// 686 /// Negative FileIDs are indexes into this table. To get from ID to an index, 687 /// use (-ID - 2). 688 SmallVector<SrcMgr::SLocEntry, 0> LoadedSLocEntryTable; 689 690 /// The starting offset of the next local SLocEntry. 691 /// 692 /// This is LocalSLocEntryTable.back().Offset + the size of that entry. 693 unsigned NextLocalOffset; 694 695 /// The starting offset of the latest batch of loaded SLocEntries. 696 /// 697 /// This is LoadedSLocEntryTable.back().Offset, except that that entry might 698 /// not have been loaded, so that value would be unknown. 699 unsigned CurrentLoadedOffset; 700 701 /// The highest possible offset is 2^31-1, so CurrentLoadedOffset 702 /// starts at 2^31. 703 static const unsigned MaxLoadedOffset = 1U << 31U; 704 705 /// A bitmap that indicates whether the entries of LoadedSLocEntryTable 706 /// have already been loaded from the external source. 707 /// 708 /// Same indexing as LoadedSLocEntryTable. 709 llvm::BitVector SLocEntryLoaded; 710 711 /// An external source for source location entries. 712 ExternalSLocEntrySource *ExternalSLocEntries = nullptr; 713 714 /// A one-entry cache to speed up getFileID. 715 /// 716 /// LastFileIDLookup records the last FileID looked up or created, because it 717 /// is very common to look up many tokens from the same file. 718 mutable FileID LastFileIDLookup; 719 720 /// Holds information for \#line directives. 721 /// 722 /// This is referenced by indices from SLocEntryTable. 723 std::unique_ptr<LineTableInfo> LineTable; 724 725 /// These ivars serve as a cache used in the getLineNumber 726 /// method which is used to speedup getLineNumber calls to nearby locations. 727 mutable FileID LastLineNoFileIDQuery; 728 mutable const SrcMgr::ContentCache *LastLineNoContentCache; 729 mutable unsigned LastLineNoFilePos; 730 mutable unsigned LastLineNoResult; 731 732 /// The file ID for the main source file of the translation unit. 733 FileID MainFileID; 734 735 /// The file ID for the precompiled preamble there is one. 736 FileID PreambleFileID; 737 738 // Statistics for -print-stats. 739 mutable unsigned NumLinearScans = 0; 740 mutable unsigned NumBinaryProbes = 0; 741 742 /// Associates a FileID with its "included/expanded in" decomposed 743 /// location. 744 /// 745 /// Used to cache results from and speed-up \c getDecomposedIncludedLoc 746 /// function. 747 mutable llvm::DenseMap<FileID, std::pair<FileID, unsigned>> IncludedLocMap; 748 749 /// The key value into the IsBeforeInTUCache table. 750 using IsBeforeInTUCacheKey = std::pair<FileID, FileID>; 751 752 /// The IsBeforeInTranslationUnitCache is a mapping from FileID pairs 753 /// to cache results. 754 using InBeforeInTUCache = 755 llvm::DenseMap<IsBeforeInTUCacheKey, InBeforeInTUCacheEntry>; 756 757 /// Cache results for the isBeforeInTranslationUnit method. 758 mutable InBeforeInTUCache IBTUCache; 759 mutable InBeforeInTUCacheEntry IBTUCacheOverflow; 760 761 /// Return the cache entry for comparing the given file IDs 762 /// for isBeforeInTranslationUnit. 763 InBeforeInTUCacheEntry &getInBeforeInTUCache(FileID LFID, FileID RFID) const; 764 765 // Cache for the "fake" buffer used for error-recovery purposes. 766 mutable std::unique_ptr<llvm::MemoryBuffer> FakeBufferForRecovery; 767 768 mutable std::unique_ptr<SrcMgr::ContentCache> FakeContentCacheForRecovery; 769 770 mutable std::unique_ptr<SrcMgr::SLocEntry> FakeSLocEntryForRecovery; 771 772 /// Lazily computed map of macro argument chunks to their expanded 773 /// source location. 774 using MacroArgsMap = std::map<unsigned, SourceLocation>; 775 776 mutable llvm::DenseMap<FileID, std::unique_ptr<MacroArgsMap>> 777 MacroArgsCacheMap; 778 779 /// The stack of modules being built, which is used to detect 780 /// cycles in the module dependency graph as modules are being built, as 781 /// well as to describe why we're rebuilding a particular module. 782 /// 783 /// There is no way to set this value from the command line. If we ever need 784 /// to do so (e.g., if on-demand module construction moves out-of-process), 785 /// we can add a cc1-level option to do so. 786 SmallVector<std::pair<std::string, FullSourceLoc>, 2> StoredModuleBuildStack; 787 788 public: 789 SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr, 790 bool UserFilesAreVolatile = false); 791 explicit SourceManager(const SourceManager &) = delete; 792 SourceManager &operator=(const SourceManager &) = delete; 793 ~SourceManager(); 794 795 void clearIDTables(); 796 797 /// Initialize this source manager suitably to replay the compilation 798 /// described by \p Old. Requires that \p Old outlive \p *this. 799 void initializeForReplay(const SourceManager &Old); 800 801 DiagnosticsEngine &getDiagnostics() const { return Diag; } 802 803 FileManager &getFileManager() const { return FileMgr; } 804 805 /// Set true if the SourceManager should report the original file name 806 /// for contents of files that were overridden by other files. Defaults to 807 /// true. 808 void setOverridenFilesKeepOriginalName(bool value) { 809 OverridenFilesKeepOriginalName = value; 810 } 811 812 /// True if non-system source files should be treated as volatile 813 /// (likely to change while trying to use them). 814 bool userFilesAreVolatile() const { return UserFilesAreVolatile; } 815 816 /// Retrieve the module build stack. 817 ModuleBuildStack getModuleBuildStack() const { 818 return StoredModuleBuildStack; 819 } 820 821 /// Set the module build stack. 822 void setModuleBuildStack(ModuleBuildStack stack) { 823 StoredModuleBuildStack.clear(); 824 StoredModuleBuildStack.append(stack.begin(), stack.end()); 825 } 826 827 /// Push an entry to the module build stack. 828 void pushModuleBuildStack(StringRef moduleName, FullSourceLoc importLoc) { 829 StoredModuleBuildStack.push_back(std::make_pair(moduleName.str(),importLoc)); 830 } 831 832 //===--------------------------------------------------------------------===// 833 // MainFileID creation and querying methods. 834 //===--------------------------------------------------------------------===// 835 836 /// Returns the FileID of the main source file. 837 FileID getMainFileID() const { return MainFileID; } 838 839 /// Set the file ID for the main source file. 840 void setMainFileID(FileID FID) { 841 MainFileID = FID; 842 } 843 844 /// Returns true when the given FileEntry corresponds to the main file. 845 /// 846 /// The main file should be set prior to calling this function. 847 bool isMainFile(const FileEntry &SourceFile); 848 849 /// Set the file ID for the precompiled preamble. 850 void setPreambleFileID(FileID Preamble) { 851 assert(PreambleFileID.isInvalid() && "PreambleFileID already set!"); 852 PreambleFileID = Preamble; 853 } 854 855 /// Get the file ID for the precompiled preamble if there is one. 856 FileID getPreambleFileID() const { return PreambleFileID; } 857 858 //===--------------------------------------------------------------------===// 859 // Methods to create new FileID's and macro expansions. 860 //===--------------------------------------------------------------------===// 861 862 /// Create a new FileID that represents the specified file 863 /// being \#included from the specified IncludePosition. 864 /// 865 /// This translates NULL into standard input. 866 FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, 867 SrcMgr::CharacteristicKind FileCharacter, 868 int LoadedID = 0, unsigned LoadedOffset = 0); 869 870 FileID createFileID(FileEntryRef SourceFile, SourceLocation IncludePos, 871 SrcMgr::CharacteristicKind FileCharacter, 872 int LoadedID = 0, unsigned LoadedOffset = 0); 873 874 /// Create a new FileID that represents the specified memory buffer. 875 /// 876 /// This does no caching of the buffer and takes ownership of the 877 /// MemoryBuffer, so only pass a MemoryBuffer to this once. 878 FileID createFileID(std::unique_ptr<llvm::MemoryBuffer> Buffer, 879 SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User, 880 int LoadedID = 0, unsigned LoadedOffset = 0, 881 SourceLocation IncludeLoc = SourceLocation()); 882 883 /// Create a new FileID that represents the specified memory buffer. 884 /// 885 /// This does not take ownership of the MemoryBuffer. The memory buffer must 886 /// outlive the SourceManager. 887 FileID createFileID(const llvm::MemoryBufferRef &Buffer, 888 SrcMgr::CharacteristicKind FileCharacter = SrcMgr::C_User, 889 int LoadedID = 0, unsigned LoadedOffset = 0, 890 SourceLocation IncludeLoc = SourceLocation()); 891 892 /// Get the FileID for \p SourceFile if it exists. Otherwise, create a 893 /// new FileID for the \p SourceFile. 894 FileID getOrCreateFileID(const FileEntry *SourceFile, 895 SrcMgr::CharacteristicKind FileCharacter); 896 897 /// Return a new SourceLocation that encodes the 898 /// fact that a token from SpellingLoc should actually be referenced from 899 /// ExpansionLoc, and that it represents the expansion of a macro argument 900 /// into the function-like macro body. 901 SourceLocation createMacroArgExpansionLoc(SourceLocation Loc, 902 SourceLocation ExpansionLoc, 903 unsigned TokLength); 904 905 /// Return a new SourceLocation that encodes the fact 906 /// that a token from SpellingLoc should actually be referenced from 907 /// ExpansionLoc. 908 SourceLocation createExpansionLoc(SourceLocation Loc, 909 SourceLocation ExpansionLocStart, 910 SourceLocation ExpansionLocEnd, 911 unsigned TokLength, 912 bool ExpansionIsTokenRange = true, 913 int LoadedID = 0, 914 unsigned LoadedOffset = 0); 915 916 /// Return a new SourceLocation that encodes that the token starting 917 /// at \p TokenStart ends prematurely at \p TokenEnd. 918 SourceLocation createTokenSplitLoc(SourceLocation SpellingLoc, 919 SourceLocation TokenStart, 920 SourceLocation TokenEnd); 921 922 /// Retrieve the memory buffer associated with the given file. 923 /// 924 /// Returns None if the buffer is not valid. 925 llvm::Optional<llvm::MemoryBufferRef> 926 getMemoryBufferForFileOrNone(const FileEntry *File); 927 928 /// Retrieve the memory buffer associated with the given file. 929 /// 930 /// Returns a fake buffer if there isn't a real one. 931 llvm::MemoryBufferRef getMemoryBufferForFileOrFake(const FileEntry *File) { 932 if (auto B = getMemoryBufferForFileOrNone(File)) 933 return *B; 934 return getFakeBufferForRecovery(); 935 } 936 937 /// Override the contents of the given source file by providing an 938 /// already-allocated buffer. 939 /// 940 /// \param SourceFile the source file whose contents will be overridden. 941 /// 942 /// \param Buffer the memory buffer whose contents will be used as the 943 /// data in the given source file. 944 void overrideFileContents(const FileEntry *SourceFile, 945 const llvm::MemoryBufferRef &Buffer) { 946 overrideFileContents(SourceFile, llvm::MemoryBuffer::getMemBuffer(Buffer)); 947 } 948 949 /// Override the contents of the given source file by providing an 950 /// already-allocated buffer. 951 /// 952 /// \param SourceFile the source file whose contents will be overridden. 953 /// 954 /// \param Buffer the memory buffer whose contents will be used as the 955 /// data in the given source file. 956 void overrideFileContents(const FileEntry *SourceFile, 957 std::unique_ptr<llvm::MemoryBuffer> Buffer); 958 void overrideFileContents(FileEntryRef SourceFile, 959 std::unique_ptr<llvm::MemoryBuffer> Buffer) { 960 overrideFileContents(&SourceFile.getFileEntry(), std::move(Buffer)); 961 } 962 963 /// Override the given source file with another one. 964 /// 965 /// \param SourceFile the source file which will be overridden. 966 /// 967 /// \param NewFile the file whose contents will be used as the 968 /// data instead of the contents of the given source file. 969 void overrideFileContents(const FileEntry *SourceFile, 970 const FileEntry *NewFile); 971 972 /// Returns true if the file contents have been overridden. 973 bool isFileOverridden(const FileEntry *File) const { 974 if (OverriddenFilesInfo) { 975 if (OverriddenFilesInfo->OverriddenFilesWithBuffer.count(File)) 976 return true; 977 if (OverriddenFilesInfo->OverriddenFiles.find(File) != 978 OverriddenFilesInfo->OverriddenFiles.end()) 979 return true; 980 } 981 return false; 982 } 983 984 /// Bypass the overridden contents of a file. This creates a new FileEntry 985 /// and initializes the content cache for it. Returns None if there is no 986 /// such file in the filesystem. 987 /// 988 /// This should be called before parsing has begun. 989 Optional<FileEntryRef> bypassFileContentsOverride(FileEntryRef File); 990 991 /// Specify that a file is transient. 992 void setFileIsTransient(const FileEntry *SourceFile); 993 994 /// Specify that all files that are read during this compilation are 995 /// transient. 996 void setAllFilesAreTransient(bool Transient) { 997 FilesAreTransient = Transient; 998 } 999 1000 //===--------------------------------------------------------------------===// 1001 // FileID manipulation methods. 1002 //===--------------------------------------------------------------------===// 1003 1004 /// Return the buffer for the specified FileID. 1005 /// 1006 /// If there is an error opening this buffer the first time, return None. 1007 llvm::Optional<llvm::MemoryBufferRef> 1008 getBufferOrNone(FileID FID, SourceLocation Loc = SourceLocation()) const { 1009 if (auto *Entry = getSLocEntryForFile(FID)) 1010 return Entry->getFile().getContentCache().getBufferOrNone( 1011 Diag, getFileManager(), Loc); 1012 return None; 1013 } 1014 1015 /// Return the buffer for the specified FileID. 1016 /// 1017 /// If there is an error opening this buffer the first time, this 1018 /// manufactures a temporary buffer and returns it. 1019 llvm::MemoryBufferRef 1020 getBufferOrFake(FileID FID, SourceLocation Loc = SourceLocation()) const { 1021 if (auto B = getBufferOrNone(FID, Loc)) 1022 return *B; 1023 return getFakeBufferForRecovery(); 1024 } 1025 1026 /// Returns the FileEntry record for the provided FileID. 1027 const FileEntry *getFileEntryForID(FileID FID) const { 1028 if (auto *Entry = getSLocEntryForFile(FID)) 1029 return Entry->getFile().getContentCache().OrigEntry; 1030 return nullptr; 1031 } 1032 1033 /// Returns the FileEntryRef for the provided FileID. 1034 Optional<FileEntryRef> getFileEntryRefForID(FileID FID) const { 1035 if (auto *Entry = getFileEntryForID(FID)) 1036 return Entry->getLastRef(); 1037 return None; 1038 } 1039 1040 /// Returns the filename for the provided FileID, unless it's a built-in 1041 /// buffer that's not represented by a filename. 1042 /// 1043 /// Returns None for non-files and built-in files. 1044 Optional<StringRef> getNonBuiltinFilenameForID(FileID FID) const; 1045 1046 /// Returns the FileEntry record for the provided SLocEntry. 1047 const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const 1048 { 1049 return sloc.getFile().getContentCache().OrigEntry; 1050 } 1051 1052 /// Return a StringRef to the source buffer data for the 1053 /// specified FileID. 1054 /// 1055 /// \param FID The file ID whose contents will be returned. 1056 /// \param Invalid If non-NULL, will be set true if an error occurred. 1057 StringRef getBufferData(FileID FID, bool *Invalid = nullptr) const; 1058 1059 /// Return a StringRef to the source buffer data for the 1060 /// specified FileID, returning None if invalid. 1061 /// 1062 /// \param FID The file ID whose contents will be returned. 1063 llvm::Optional<StringRef> getBufferDataOrNone(FileID FID) const; 1064 1065 /// Return a StringRef to the source buffer data for the 1066 /// specified FileID, returning None if it's not yet loaded. 1067 /// 1068 /// \param FID The file ID whose contents will be returned. 1069 llvm::Optional<StringRef> getBufferDataIfLoaded(FileID FID) const; 1070 1071 /// Get the number of FileIDs (files and macros) that were created 1072 /// during preprocessing of \p FID, including it. 1073 unsigned getNumCreatedFIDsForFileID(FileID FID) const { 1074 if (auto *Entry = getSLocEntryForFile(FID)) 1075 return Entry->getFile().NumCreatedFIDs; 1076 return 0; 1077 } 1078 1079 /// Set the number of FileIDs (files and macros) that were created 1080 /// during preprocessing of \p FID, including it. 1081 void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs, 1082 bool Force = false) const { 1083 auto *Entry = getSLocEntryForFile(FID); 1084 if (!Entry) 1085 return; 1086 assert((Force || Entry->getFile().NumCreatedFIDs == 0) && "Already set!"); 1087 const_cast<SrcMgr::FileInfo &>(Entry->getFile()).NumCreatedFIDs = NumFIDs; 1088 } 1089 1090 //===--------------------------------------------------------------------===// 1091 // SourceLocation manipulation methods. 1092 //===--------------------------------------------------------------------===// 1093 1094 /// Return the FileID for a SourceLocation. 1095 /// 1096 /// This is a very hot method that is used for all SourceManager queries 1097 /// that start with a SourceLocation object. It is responsible for finding 1098 /// the entry in SLocEntryTable which contains the specified location. 1099 /// 1100 FileID getFileID(SourceLocation SpellingLoc) const { 1101 unsigned SLocOffset = SpellingLoc.getOffset(); 1102 1103 // If our one-entry cache covers this offset, just return it. 1104 if (isOffsetInFileID(LastFileIDLookup, SLocOffset)) 1105 return LastFileIDLookup; 1106 1107 return getFileIDSlow(SLocOffset); 1108 } 1109 1110 /// Return the filename of the file containing a SourceLocation. 1111 StringRef getFilename(SourceLocation SpellingLoc) const; 1112 1113 /// Return the source location corresponding to the first byte of 1114 /// the specified file. 1115 SourceLocation getLocForStartOfFile(FileID FID) const { 1116 if (auto *Entry = getSLocEntryForFile(FID)) 1117 return SourceLocation::getFileLoc(Entry->getOffset()); 1118 return SourceLocation(); 1119 } 1120 1121 /// Return the source location corresponding to the last byte of the 1122 /// specified file. 1123 SourceLocation getLocForEndOfFile(FileID FID) const { 1124 if (auto *Entry = getSLocEntryForFile(FID)) 1125 return SourceLocation::getFileLoc(Entry->getOffset() + 1126 getFileIDSize(FID)); 1127 return SourceLocation(); 1128 } 1129 1130 /// Returns the include location if \p FID is a \#include'd file 1131 /// otherwise it returns an invalid location. 1132 SourceLocation getIncludeLoc(FileID FID) const { 1133 if (auto *Entry = getSLocEntryForFile(FID)) 1134 return Entry->getFile().getIncludeLoc(); 1135 return SourceLocation(); 1136 } 1137 1138 // Returns the import location if the given source location is 1139 // located within a module, or an invalid location if the source location 1140 // is within the current translation unit. 1141 std::pair<SourceLocation, StringRef> 1142 getModuleImportLoc(SourceLocation Loc) const { 1143 FileID FID = getFileID(Loc); 1144 1145 // Positive file IDs are in the current translation unit, and -1 is a 1146 // placeholder. 1147 if (FID.ID >= -1) 1148 return std::make_pair(SourceLocation(), ""); 1149 1150 return ExternalSLocEntries->getModuleImportLoc(FID.ID); 1151 } 1152 1153 /// Given a SourceLocation object \p Loc, return the expansion 1154 /// location referenced by the ID. 1155 SourceLocation getExpansionLoc(SourceLocation Loc) const { 1156 // Handle the non-mapped case inline, defer to out of line code to handle 1157 // expansions. 1158 if (Loc.isFileID()) return Loc; 1159 return getExpansionLocSlowCase(Loc); 1160 } 1161 1162 /// Given \p Loc, if it is a macro location return the expansion 1163 /// location or the spelling location, depending on if it comes from a 1164 /// macro argument or not. 1165 SourceLocation getFileLoc(SourceLocation Loc) const { 1166 if (Loc.isFileID()) return Loc; 1167 return getFileLocSlowCase(Loc); 1168 } 1169 1170 /// Return the start/end of the expansion information for an 1171 /// expansion location. 1172 /// 1173 /// \pre \p Loc is required to be an expansion location. 1174 CharSourceRange getImmediateExpansionRange(SourceLocation Loc) const; 1175 1176 /// Given a SourceLocation object, return the range of 1177 /// tokens covered by the expansion in the ultimate file. 1178 CharSourceRange getExpansionRange(SourceLocation Loc) const; 1179 1180 /// Given a SourceRange object, return the range of 1181 /// tokens or characters covered by the expansion in the ultimate file. 1182 CharSourceRange getExpansionRange(SourceRange Range) const { 1183 SourceLocation Begin = getExpansionRange(Range.getBegin()).getBegin(); 1184 CharSourceRange End = getExpansionRange(Range.getEnd()); 1185 return CharSourceRange(SourceRange(Begin, End.getEnd()), 1186 End.isTokenRange()); 1187 } 1188 1189 /// Given a CharSourceRange object, return the range of 1190 /// tokens or characters covered by the expansion in the ultimate file. 1191 CharSourceRange getExpansionRange(CharSourceRange Range) const { 1192 CharSourceRange Expansion = getExpansionRange(Range.getAsRange()); 1193 if (Expansion.getEnd() == Range.getEnd()) 1194 Expansion.setTokenRange(Range.isTokenRange()); 1195 return Expansion; 1196 } 1197 1198 /// Given a SourceLocation object, return the spelling 1199 /// location referenced by the ID. 1200 /// 1201 /// This is the place where the characters that make up the lexed token 1202 /// can be found. 1203 SourceLocation getSpellingLoc(SourceLocation Loc) const { 1204 // Handle the non-mapped case inline, defer to out of line code to handle 1205 // expansions. 1206 if (Loc.isFileID()) return Loc; 1207 return getSpellingLocSlowCase(Loc); 1208 } 1209 1210 /// Given a SourceLocation object, return the spelling location 1211 /// referenced by the ID. 1212 /// 1213 /// This is the first level down towards the place where the characters 1214 /// that make up the lexed token can be found. This should not generally 1215 /// be used by clients. 1216 SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const; 1217 1218 /// Form a SourceLocation from a FileID and Offset pair. 1219 SourceLocation getComposedLoc(FileID FID, unsigned Offset) const { 1220 auto *Entry = getSLocEntryOrNull(FID); 1221 if (!Entry) 1222 return SourceLocation(); 1223 1224 unsigned GlobalOffset = Entry->getOffset() + Offset; 1225 return Entry->isFile() ? SourceLocation::getFileLoc(GlobalOffset) 1226 : SourceLocation::getMacroLoc(GlobalOffset); 1227 } 1228 1229 /// Decompose the specified location into a raw FileID + Offset pair. 1230 /// 1231 /// The first element is the FileID, the second is the offset from the 1232 /// start of the buffer of the location. 1233 std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const { 1234 FileID FID = getFileID(Loc); 1235 auto *Entry = getSLocEntryOrNull(FID); 1236 if (!Entry) 1237 return std::make_pair(FileID(), 0); 1238 return std::make_pair(FID, Loc.getOffset() - Entry->getOffset()); 1239 } 1240 1241 /// Decompose the specified location into a raw FileID + Offset pair. 1242 /// 1243 /// If the location is an expansion record, walk through it until we find 1244 /// the final location expanded. 1245 std::pair<FileID, unsigned> 1246 getDecomposedExpansionLoc(SourceLocation Loc) const { 1247 FileID FID = getFileID(Loc); 1248 auto *E = getSLocEntryOrNull(FID); 1249 if (!E) 1250 return std::make_pair(FileID(), 0); 1251 1252 unsigned Offset = Loc.getOffset()-E->getOffset(); 1253 if (Loc.isFileID()) 1254 return std::make_pair(FID, Offset); 1255 1256 return getDecomposedExpansionLocSlowCase(E); 1257 } 1258 1259 /// Decompose the specified location into a raw FileID + Offset pair. 1260 /// 1261 /// If the location is an expansion record, walk through it until we find 1262 /// its spelling record. 1263 std::pair<FileID, unsigned> 1264 getDecomposedSpellingLoc(SourceLocation Loc) const { 1265 FileID FID = getFileID(Loc); 1266 auto *E = getSLocEntryOrNull(FID); 1267 if (!E) 1268 return std::make_pair(FileID(), 0); 1269 1270 unsigned Offset = Loc.getOffset()-E->getOffset(); 1271 if (Loc.isFileID()) 1272 return std::make_pair(FID, Offset); 1273 return getDecomposedSpellingLocSlowCase(E, Offset); 1274 } 1275 1276 /// Returns the "included/expanded in" decomposed location of the given 1277 /// FileID. 1278 std::pair<FileID, unsigned> getDecomposedIncludedLoc(FileID FID) const; 1279 1280 /// Returns the offset from the start of the file that the 1281 /// specified SourceLocation represents. 1282 /// 1283 /// This is not very meaningful for a macro ID. 1284 unsigned getFileOffset(SourceLocation SpellingLoc) const { 1285 return getDecomposedLoc(SpellingLoc).second; 1286 } 1287 1288 /// Tests whether the given source location represents a macro 1289 /// argument's expansion into the function-like macro definition. 1290 /// 1291 /// \param StartLoc If non-null and function returns true, it is set to the 1292 /// start location of the macro argument expansion. 1293 /// 1294 /// Such source locations only appear inside of the expansion 1295 /// locations representing where a particular function-like macro was 1296 /// expanded. 1297 bool isMacroArgExpansion(SourceLocation Loc, 1298 SourceLocation *StartLoc = nullptr) const; 1299 1300 /// Tests whether the given source location represents the expansion of 1301 /// a macro body. 1302 /// 1303 /// This is equivalent to testing whether the location is part of a macro 1304 /// expansion but not the expansion of an argument to a function-like macro. 1305 bool isMacroBodyExpansion(SourceLocation Loc) const; 1306 1307 /// Returns true if the given MacroID location points at the beginning 1308 /// of the immediate macro expansion. 1309 /// 1310 /// \param MacroBegin If non-null and function returns true, it is set to the 1311 /// begin location of the immediate macro expansion. 1312 bool isAtStartOfImmediateMacroExpansion(SourceLocation Loc, 1313 SourceLocation *MacroBegin = nullptr) const; 1314 1315 /// Returns true if the given MacroID location points at the character 1316 /// end of the immediate macro expansion. 1317 /// 1318 /// \param MacroEnd If non-null and function returns true, it is set to the 1319 /// character end location of the immediate macro expansion. 1320 bool 1321 isAtEndOfImmediateMacroExpansion(SourceLocation Loc, 1322 SourceLocation *MacroEnd = nullptr) const; 1323 1324 /// Returns true if \p Loc is inside the [\p Start, +\p Length) 1325 /// chunk of the source location address space. 1326 /// 1327 /// If it's true and \p RelativeOffset is non-null, it will be set to the 1328 /// relative offset of \p Loc inside the chunk. 1329 bool isInSLocAddrSpace(SourceLocation Loc, 1330 SourceLocation Start, unsigned Length, 1331 unsigned *RelativeOffset = nullptr) const { 1332 assert(((Start.getOffset() < NextLocalOffset && 1333 Start.getOffset()+Length <= NextLocalOffset) || 1334 (Start.getOffset() >= CurrentLoadedOffset && 1335 Start.getOffset()+Length < MaxLoadedOffset)) && 1336 "Chunk is not valid SLoc address space"); 1337 unsigned LocOffs = Loc.getOffset(); 1338 unsigned BeginOffs = Start.getOffset(); 1339 unsigned EndOffs = BeginOffs + Length; 1340 if (LocOffs >= BeginOffs && LocOffs < EndOffs) { 1341 if (RelativeOffset) 1342 *RelativeOffset = LocOffs - BeginOffs; 1343 return true; 1344 } 1345 1346 return false; 1347 } 1348 1349 /// Return true if both \p LHS and \p RHS are in the local source 1350 /// location address space or the loaded one. 1351 /// 1352 /// If it's true and \p RelativeOffset is non-null, it will be set to the 1353 /// offset of \p RHS relative to \p LHS. 1354 bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS, 1355 int *RelativeOffset) const { 1356 unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset(); 1357 bool LHSLoaded = LHSOffs >= CurrentLoadedOffset; 1358 bool RHSLoaded = RHSOffs >= CurrentLoadedOffset; 1359 1360 if (LHSLoaded == RHSLoaded) { 1361 if (RelativeOffset) 1362 *RelativeOffset = RHSOffs - LHSOffs; 1363 return true; 1364 } 1365 1366 return false; 1367 } 1368 1369 //===--------------------------------------------------------------------===// 1370 // Queries about the code at a SourceLocation. 1371 //===--------------------------------------------------------------------===// 1372 1373 /// Return a pointer to the start of the specified location 1374 /// in the appropriate spelling MemoryBuffer. 1375 /// 1376 /// \param Invalid If non-NULL, will be set \c true if an error occurs. 1377 const char *getCharacterData(SourceLocation SL, 1378 bool *Invalid = nullptr) const; 1379 1380 /// Return the column # for the specified file position. 1381 /// 1382 /// This is significantly cheaper to compute than the line number. This 1383 /// returns zero if the column number isn't known. This may only be called 1384 /// on a file sloc, so you must choose a spelling or expansion location 1385 /// before calling this method. 1386 unsigned getColumnNumber(FileID FID, unsigned FilePos, 1387 bool *Invalid = nullptr) const; 1388 unsigned getSpellingColumnNumber(SourceLocation Loc, 1389 bool *Invalid = nullptr) const; 1390 unsigned getExpansionColumnNumber(SourceLocation Loc, 1391 bool *Invalid = nullptr) const; 1392 unsigned getPresumedColumnNumber(SourceLocation Loc, 1393 bool *Invalid = nullptr) const; 1394 1395 /// Given a SourceLocation, return the spelling line number 1396 /// for the position indicated. 1397 /// 1398 /// This requires building and caching a table of line offsets for the 1399 /// MemoryBuffer, so this is not cheap: use only when about to emit a 1400 /// diagnostic. 1401 unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = nullptr) const; 1402 unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const; 1403 unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const; 1404 unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = nullptr) const; 1405 1406 /// Return the filename or buffer identifier of the buffer the 1407 /// location is in. 1408 /// 1409 /// Note that this name does not respect \#line directives. Use 1410 /// getPresumedLoc for normal clients. 1411 StringRef getBufferName(SourceLocation Loc, bool *Invalid = nullptr) const; 1412 1413 /// Return the file characteristic of the specified source 1414 /// location, indicating whether this is a normal file, a system 1415 /// header, or an "implicit extern C" system header. 1416 /// 1417 /// This state can be modified with flags on GNU linemarker directives like: 1418 /// \code 1419 /// # 4 "foo.h" 3 1420 /// \endcode 1421 /// which changes all source locations in the current file after that to be 1422 /// considered to be from a system header. 1423 SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const; 1424 1425 /// Returns the "presumed" location of a SourceLocation specifies. 1426 /// 1427 /// A "presumed location" can be modified by \#line or GNU line marker 1428 /// directives. This provides a view on the data that a user should see 1429 /// in diagnostics, for example. 1430 /// 1431 /// Note that a presumed location is always given as the expansion point of 1432 /// an expansion location, not at the spelling location. 1433 /// 1434 /// \returns The presumed location of the specified SourceLocation. If the 1435 /// presumed location cannot be calculated (e.g., because \p Loc is invalid 1436 /// or the file containing \p Loc has changed on disk), returns an invalid 1437 /// presumed location. 1438 PresumedLoc getPresumedLoc(SourceLocation Loc, 1439 bool UseLineDirectives = true) const; 1440 1441 /// Returns whether the PresumedLoc for a given SourceLocation is 1442 /// in the main file. 1443 /// 1444 /// This computes the "presumed" location for a SourceLocation, then checks 1445 /// whether it came from a file other than the main file. This is different 1446 /// from isWrittenInMainFile() because it takes line marker directives into 1447 /// account. 1448 bool isInMainFile(SourceLocation Loc) const; 1449 1450 /// Returns true if the spelling locations for both SourceLocations 1451 /// are part of the same file buffer. 1452 /// 1453 /// This check ignores line marker directives. 1454 bool isWrittenInSameFile(SourceLocation Loc1, SourceLocation Loc2) const { 1455 return getFileID(Loc1) == getFileID(Loc2); 1456 } 1457 1458 /// Returns true if the spelling location for the given location 1459 /// is in the main file buffer. 1460 /// 1461 /// This check ignores line marker directives. 1462 bool isWrittenInMainFile(SourceLocation Loc) const { 1463 return getFileID(Loc) == getMainFileID(); 1464 } 1465 1466 /// Returns whether \p Loc is located in a <built-in> file. 1467 bool isWrittenInBuiltinFile(SourceLocation Loc) const { 1468 StringRef Filename(getPresumedLoc(Loc).getFilename()); 1469 return Filename.equals("<built-in>"); 1470 } 1471 1472 /// Returns whether \p Loc is located in a <command line> file. 1473 bool isWrittenInCommandLineFile(SourceLocation Loc) const { 1474 StringRef Filename(getPresumedLoc(Loc).getFilename()); 1475 return Filename.equals("<command line>"); 1476 } 1477 1478 /// Returns whether \p Loc is located in a <scratch space> file. 1479 bool isWrittenInScratchSpace(SourceLocation Loc) const { 1480 StringRef Filename(getPresumedLoc(Loc).getFilename()); 1481 return Filename.equals("<scratch space>"); 1482 } 1483 1484 /// Returns if a SourceLocation is in a system header. 1485 bool isInSystemHeader(SourceLocation Loc) const { 1486 return isSystem(getFileCharacteristic(Loc)); 1487 } 1488 1489 /// Returns if a SourceLocation is in an "extern C" system header. 1490 bool isInExternCSystemHeader(SourceLocation Loc) const { 1491 return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem; 1492 } 1493 1494 /// Returns whether \p Loc is expanded from a macro in a system header. 1495 bool isInSystemMacro(SourceLocation loc) const { 1496 if (!loc.isMacroID()) 1497 return false; 1498 1499 // This happens when the macro is the result of a paste, in that case 1500 // its spelling is the scratch memory, so we take the parent context. 1501 // There can be several level of token pasting. 1502 if (isWrittenInScratchSpace(getSpellingLoc(loc))) { 1503 do { 1504 loc = getImmediateMacroCallerLoc(loc); 1505 } while (isWrittenInScratchSpace(getSpellingLoc(loc))); 1506 return isInSystemMacro(loc); 1507 } 1508 1509 return isInSystemHeader(getSpellingLoc(loc)); 1510 } 1511 1512 /// The size of the SLocEntry that \p FID represents. 1513 unsigned getFileIDSize(FileID FID) const; 1514 1515 /// Given a specific FileID, returns true if \p Loc is inside that 1516 /// FileID chunk and sets relative offset (offset of \p Loc from beginning 1517 /// of FileID) to \p relativeOffset. 1518 bool isInFileID(SourceLocation Loc, FileID FID, 1519 unsigned *RelativeOffset = nullptr) const { 1520 unsigned Offs = Loc.getOffset(); 1521 if (isOffsetInFileID(FID, Offs)) { 1522 if (RelativeOffset) 1523 *RelativeOffset = Offs - getSLocEntry(FID).getOffset(); 1524 return true; 1525 } 1526 1527 return false; 1528 } 1529 1530 //===--------------------------------------------------------------------===// 1531 // Line Table Manipulation Routines 1532 //===--------------------------------------------------------------------===// 1533 1534 /// Return the uniqued ID for the specified filename. 1535 unsigned getLineTableFilenameID(StringRef Str); 1536 1537 /// Add a line note to the line table for the FileID and offset 1538 /// specified by Loc. 1539 /// 1540 /// If FilenameID is -1, it is considered to be unspecified. 1541 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID, 1542 bool IsFileEntry, bool IsFileExit, 1543 SrcMgr::CharacteristicKind FileKind); 1544 1545 /// Determine if the source manager has a line table. 1546 bool hasLineTable() const { return LineTable != nullptr; } 1547 1548 /// Retrieve the stored line table. 1549 LineTableInfo &getLineTable(); 1550 1551 //===--------------------------------------------------------------------===// 1552 // Queries for performance analysis. 1553 //===--------------------------------------------------------------------===// 1554 1555 /// Return the total amount of physical memory allocated by the 1556 /// ContentCache allocator. 1557 size_t getContentCacheSize() const { 1558 return ContentCacheAlloc.getTotalMemory(); 1559 } 1560 1561 struct MemoryBufferSizes { 1562 const size_t malloc_bytes; 1563 const size_t mmap_bytes; 1564 1565 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes) 1566 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {} 1567 }; 1568 1569 /// Return the amount of memory used by memory buffers, breaking down 1570 /// by heap-backed versus mmap'ed memory. 1571 MemoryBufferSizes getMemoryBufferSizes() const; 1572 1573 /// Return the amount of memory used for various side tables and 1574 /// data structures in the SourceManager. 1575 size_t getDataStructureSizes() const; 1576 1577 //===--------------------------------------------------------------------===// 1578 // Other miscellaneous methods. 1579 //===--------------------------------------------------------------------===// 1580 1581 /// Get the source location for the given file:line:col triplet. 1582 /// 1583 /// If the source file is included multiple times, the source location will 1584 /// be based upon the first inclusion. 1585 SourceLocation translateFileLineCol(const FileEntry *SourceFile, 1586 unsigned Line, unsigned Col) const; 1587 1588 /// Get the FileID for the given file. 1589 /// 1590 /// If the source file is included multiple times, the FileID will be the 1591 /// first inclusion. 1592 FileID translateFile(const FileEntry *SourceFile) const; 1593 FileID translateFile(FileEntryRef SourceFile) const { 1594 return translateFile(&SourceFile.getFileEntry()); 1595 } 1596 1597 /// Get the source location in \p FID for the given line:col. 1598 /// Returns null location if \p FID is not a file SLocEntry. 1599 SourceLocation translateLineCol(FileID FID, 1600 unsigned Line, unsigned Col) const; 1601 1602 /// If \p Loc points inside a function macro argument, the returned 1603 /// location will be the macro location in which the argument was expanded. 1604 /// If a macro argument is used multiple times, the expanded location will 1605 /// be at the first expansion of the argument. 1606 /// e.g. 1607 /// MY_MACRO(foo); 1608 /// ^ 1609 /// Passing a file location pointing at 'foo', will yield a macro location 1610 /// where 'foo' was expanded into. 1611 SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const; 1612 1613 /// Determines the order of 2 source locations in the translation unit. 1614 /// 1615 /// \returns true if LHS source location comes before RHS, false otherwise. 1616 bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const; 1617 1618 /// Determines whether the two decomposed source location is in the 1619 /// same translation unit. As a byproduct, it also calculates the order 1620 /// of the source locations in case they are in the same TU. 1621 /// 1622 /// \returns Pair of bools the first component is true if the two locations 1623 /// are in the same TU. The second bool is true if the first is true 1624 /// and \p LOffs is before \p ROffs. 1625 std::pair<bool, bool> 1626 isInTheSameTranslationUnit(std::pair<FileID, unsigned> &LOffs, 1627 std::pair<FileID, unsigned> &ROffs) const; 1628 1629 /// Determines the order of 2 source locations in the "source location 1630 /// address space". 1631 bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const { 1632 return isBeforeInSLocAddrSpace(LHS, RHS.getOffset()); 1633 } 1634 1635 /// Determines the order of a source location and a source location 1636 /// offset in the "source location address space". 1637 /// 1638 /// Note that we always consider source locations loaded from 1639 bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const { 1640 unsigned LHSOffset = LHS.getOffset(); 1641 bool LHSLoaded = LHSOffset >= CurrentLoadedOffset; 1642 bool RHSLoaded = RHS >= CurrentLoadedOffset; 1643 if (LHSLoaded == RHSLoaded) 1644 return LHSOffset < RHS; 1645 1646 return LHSLoaded; 1647 } 1648 1649 /// Return true if the Point is within Start and End. 1650 bool isPointWithin(SourceLocation Location, SourceLocation Start, 1651 SourceLocation End) const { 1652 return Location == Start || Location == End || 1653 (isBeforeInTranslationUnit(Start, Location) && 1654 isBeforeInTranslationUnit(Location, End)); 1655 } 1656 1657 // Iterators over FileInfos. 1658 using fileinfo_iterator = 1659 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::const_iterator; 1660 1661 fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); } 1662 fileinfo_iterator fileinfo_end() const { return FileInfos.end(); } 1663 bool hasFileInfo(const FileEntry *File) const { 1664 return FileInfos.find(File) != FileInfos.end(); 1665 } 1666 1667 /// Print statistics to stderr. 1668 void PrintStats() const; 1669 1670 void dump() const; 1671 1672 /// Get the number of local SLocEntries we have. 1673 unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); } 1674 1675 /// Get a local SLocEntry. This is exposed for indexing. 1676 const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index) const { 1677 assert(Index < LocalSLocEntryTable.size() && "Invalid index"); 1678 return LocalSLocEntryTable[Index]; 1679 } 1680 1681 /// Get the number of loaded SLocEntries we have. 1682 unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();} 1683 1684 /// Get a loaded SLocEntry. This is exposed for indexing. 1685 const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index, 1686 bool *Invalid = nullptr) const { 1687 assert(Index < LoadedSLocEntryTable.size() && "Invalid index"); 1688 if (SLocEntryLoaded[Index]) 1689 return LoadedSLocEntryTable[Index]; 1690 return loadSLocEntry(Index, Invalid); 1691 } 1692 1693 const SrcMgr::SLocEntry &getSLocEntry(FileID FID, 1694 bool *Invalid = nullptr) const { 1695 if (FID.ID == 0 || FID.ID == -1) { 1696 if (Invalid) *Invalid = true; 1697 return LocalSLocEntryTable[0]; 1698 } 1699 return getSLocEntryByID(FID.ID, Invalid); 1700 } 1701 1702 unsigned getNextLocalOffset() const { return NextLocalOffset; } 1703 1704 void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) { 1705 assert(LoadedSLocEntryTable.empty() && 1706 "Invalidating existing loaded entries"); 1707 ExternalSLocEntries = Source; 1708 } 1709 1710 /// Allocate a number of loaded SLocEntries, which will be actually 1711 /// loaded on demand from the external source. 1712 /// 1713 /// NumSLocEntries will be allocated, which occupy a total of TotalSize space 1714 /// in the global source view. The lowest ID and the base offset of the 1715 /// entries will be returned. 1716 std::pair<int, unsigned> 1717 AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize); 1718 1719 /// Returns true if \p Loc came from a PCH/Module. 1720 bool isLoadedSourceLocation(SourceLocation Loc) const { 1721 return Loc.getOffset() >= CurrentLoadedOffset; 1722 } 1723 1724 /// Returns true if \p Loc did not come from a PCH/Module. 1725 bool isLocalSourceLocation(SourceLocation Loc) const { 1726 return Loc.getOffset() < NextLocalOffset; 1727 } 1728 1729 /// Returns true if \p FID came from a PCH/Module. 1730 bool isLoadedFileID(FileID FID) const { 1731 assert(FID.ID != -1 && "Using FileID sentinel value"); 1732 return FID.ID < 0; 1733 } 1734 1735 /// Returns true if \p FID did not come from a PCH/Module. 1736 bool isLocalFileID(FileID FID) const { 1737 return !isLoadedFileID(FID); 1738 } 1739 1740 /// Gets the location of the immediate macro caller, one level up the stack 1741 /// toward the initial macro typed into the source. 1742 SourceLocation getImmediateMacroCallerLoc(SourceLocation Loc) const { 1743 if (!Loc.isMacroID()) return Loc; 1744 1745 // When we have the location of (part of) an expanded parameter, its 1746 // spelling location points to the argument as expanded in the macro call, 1747 // and therefore is used to locate the macro caller. 1748 if (isMacroArgExpansion(Loc)) 1749 return getImmediateSpellingLoc(Loc); 1750 1751 // Otherwise, the caller of the macro is located where this macro is 1752 // expanded (while the spelling is part of the macro definition). 1753 return getImmediateExpansionRange(Loc).getBegin(); 1754 } 1755 1756 /// \return Location of the top-level macro caller. 1757 SourceLocation getTopMacroCallerLoc(SourceLocation Loc) const; 1758 1759 private: 1760 friend class ASTReader; 1761 friend class ASTWriter; 1762 1763 llvm::MemoryBufferRef getFakeBufferForRecovery() const; 1764 SrcMgr::ContentCache &getFakeContentCacheForRecovery() const; 1765 1766 const SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid) const; 1767 1768 const SrcMgr::SLocEntry *getSLocEntryOrNull(FileID FID) const { 1769 bool Invalid = false; 1770 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 1771 return Invalid ? nullptr : &Entry; 1772 } 1773 1774 const SrcMgr::SLocEntry *getSLocEntryForFile(FileID FID) const { 1775 if (auto *Entry = getSLocEntryOrNull(FID)) 1776 if (Entry->isFile()) 1777 return Entry; 1778 return nullptr; 1779 } 1780 1781 /// Get the entry with the given unwrapped FileID. 1782 /// Invalid will not be modified for Local IDs. 1783 const SrcMgr::SLocEntry &getSLocEntryByID(int ID, 1784 bool *Invalid = nullptr) const { 1785 assert(ID != -1 && "Using FileID sentinel value"); 1786 if (ID < 0) 1787 return getLoadedSLocEntryByID(ID, Invalid); 1788 return getLocalSLocEntry(static_cast<unsigned>(ID)); 1789 } 1790 1791 const SrcMgr::SLocEntry & 1792 getLoadedSLocEntryByID(int ID, bool *Invalid = nullptr) const { 1793 return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2), Invalid); 1794 } 1795 1796 /// Implements the common elements of storing an expansion info struct into 1797 /// the SLocEntry table and producing a source location that refers to it. 1798 SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion, 1799 unsigned TokLength, 1800 int LoadedID = 0, 1801 unsigned LoadedOffset = 0); 1802 1803 /// Return true if the specified FileID contains the 1804 /// specified SourceLocation offset. This is a very hot method. 1805 inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const { 1806 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID); 1807 // If the entry is after the offset, it can't contain it. 1808 if (SLocOffset < Entry.getOffset()) return false; 1809 1810 // If this is the very last entry then it does. 1811 if (FID.ID == -2) 1812 return true; 1813 1814 // If it is the last local entry, then it does if the location is local. 1815 if (FID.ID+1 == static_cast<int>(LocalSLocEntryTable.size())) 1816 return SLocOffset < NextLocalOffset; 1817 1818 // Otherwise, the entry after it has to not include it. This works for both 1819 // local and loaded entries. 1820 return SLocOffset < getSLocEntryByID(FID.ID+1).getOffset(); 1821 } 1822 1823 /// Returns the previous in-order FileID or an invalid FileID if there 1824 /// is no previous one. 1825 FileID getPreviousFileID(FileID FID) const; 1826 1827 /// Returns the next in-order FileID or an invalid FileID if there is 1828 /// no next one. 1829 FileID getNextFileID(FileID FID) const; 1830 1831 /// Create a new fileID for the specified ContentCache and 1832 /// include position. 1833 /// 1834 /// This works regardless of whether the ContentCache corresponds to a 1835 /// file or some other input source. 1836 FileID createFileIDImpl(SrcMgr::ContentCache &File, StringRef Filename, 1837 SourceLocation IncludePos, 1838 SrcMgr::CharacteristicKind DirCharacter, int LoadedID, 1839 unsigned LoadedOffset); 1840 1841 SrcMgr::ContentCache &getOrCreateContentCache(FileEntryRef SourceFile, 1842 bool isSystemFile = false); 1843 1844 /// Create a new ContentCache for the specified memory buffer. 1845 SrcMgr::ContentCache & 1846 createMemBufferContentCache(std::unique_ptr<llvm::MemoryBuffer> Buf); 1847 1848 FileID getFileIDSlow(unsigned SLocOffset) const; 1849 FileID getFileIDLocal(unsigned SLocOffset) const; 1850 FileID getFileIDLoaded(unsigned SLocOffset) const; 1851 1852 SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const; 1853 SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const; 1854 SourceLocation getFileLocSlowCase(SourceLocation Loc) const; 1855 1856 std::pair<FileID, unsigned> 1857 getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const; 1858 std::pair<FileID, unsigned> 1859 getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, 1860 unsigned Offset) const; 1861 void computeMacroArgsCache(MacroArgsMap &MacroArgsCache, FileID FID) const; 1862 void associateFileChunkWithMacroArgExp(MacroArgsMap &MacroArgsCache, 1863 FileID FID, 1864 SourceLocation SpellLoc, 1865 SourceLocation ExpansionLoc, 1866 unsigned ExpansionLength) const; 1867 }; 1868 1869 /// Comparison function object. 1870 template<typename T> 1871 class BeforeThanCompare; 1872 1873 /// Compare two source locations. 1874 template<> 1875 class BeforeThanCompare<SourceLocation> { 1876 SourceManager &SM; 1877 1878 public: 1879 explicit BeforeThanCompare(SourceManager &SM) : SM(SM) {} 1880 1881 bool operator()(SourceLocation LHS, SourceLocation RHS) const { 1882 return SM.isBeforeInTranslationUnit(LHS, RHS); 1883 } 1884 }; 1885 1886 /// Compare two non-overlapping source ranges. 1887 template<> 1888 class BeforeThanCompare<SourceRange> { 1889 SourceManager &SM; 1890 1891 public: 1892 explicit BeforeThanCompare(SourceManager &SM) : SM(SM) {} 1893 1894 bool operator()(SourceRange LHS, SourceRange RHS) const { 1895 return SM.isBeforeInTranslationUnit(LHS.getBegin(), RHS.getBegin()); 1896 } 1897 }; 1898 1899 /// SourceManager and necessary depdencies (e.g. VFS, FileManager) for a single 1900 /// in-memorty file. 1901 class SourceManagerForFile { 1902 public: 1903 /// Creates SourceManager and necessary depdencies (e.g. VFS, FileManager). 1904 /// The main file in the SourceManager will be \p FileName with \p Content. 1905 SourceManagerForFile(StringRef FileName, StringRef Content); 1906 1907 SourceManager &get() { 1908 assert(SourceMgr); 1909 return *SourceMgr; 1910 } 1911 1912 private: 1913 // The order of these fields are important - they should be in the same order 1914 // as they are created in `createSourceManagerForFile` so that they can be 1915 // deleted in the reverse order as they are created. 1916 std::unique_ptr<FileManager> FileMgr; 1917 std::unique_ptr<DiagnosticsEngine> Diagnostics; 1918 std::unique_ptr<SourceManager> SourceMgr; 1919 }; 1920 1921 } // namespace clang 1922 1923 #endif // LLVM_CLANG_BASIC_SOURCEMANAGER_H 1924