1 //===--- SourceManager.h - Track and cache source files ---------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 /// 10 /// \file 11 /// \brief Defines the SourceManager interface. 12 /// 13 /// There are three different types of locations in a file: a spelling 14 /// location, an expansion location, and a presumed location. 15 /// 16 /// Given an example of: 17 /// \code 18 /// #define min(x, y) x < y ? x : y 19 /// \endcode 20 /// 21 /// and then later on a use of min: 22 /// \code 23 /// #line 17 24 /// return min(a, b); 25 /// \endcode 26 /// 27 /// The expansion location is the line in the source code where the macro 28 /// was expanded (the return statement), the spelling location is the 29 /// location in the source where the macro was originally defined, 30 /// and the presumed location is where the line directive states that 31 /// the line is 17, or any other line. 32 /// 33 //===----------------------------------------------------------------------===// 34 35 #ifndef LLVM_CLANG_SOURCEMANAGER_H 36 #define LLVM_CLANG_SOURCEMANAGER_H 37 38 #include "clang/Basic/LLVM.h" 39 #include "clang/Basic/FileManager.h" 40 #include "clang/Basic/SourceLocation.h" 41 #include "llvm/Support/Allocator.h" 42 #include "llvm/Support/DataTypes.h" 43 #include "llvm/ADT/PointerIntPair.h" 44 #include "llvm/ADT/PointerUnion.h" 45 #include "llvm/ADT/IntrusiveRefCntPtr.h" 46 #include "llvm/ADT/OwningPtr.h" 47 #include "llvm/ADT/DenseMap.h" 48 #include "llvm/ADT/DenseSet.h" 49 #include "llvm/Support/MemoryBuffer.h" 50 #include <map> 51 #include <vector> 52 #include <cassert> 53 54 namespace clang { 55 56 class DiagnosticsEngine; 57 class SourceManager; 58 class FileManager; 59 class FileEntry; 60 class LineTableInfo; 61 class LangOptions; 62 class ASTWriter; 63 class ASTReader; 64 65 /// \namespace 66 /// \brief Public enums and private classes that are part of the 67 /// SourceManager implementation. 68 /// 69 namespace SrcMgr { 70 /// \brief Indicates whether a file or directory holds normal user code, 71 /// system code, or system code which is implicitly 'extern "C"' in C++ mode. 72 /// 73 /// Entire directories can be tagged with this (this is maintained by 74 /// DirectoryLookup and friends) as can specific FileInfos when a \#pragma 75 /// system_header is seen or in various other cases. 76 /// 77 enum CharacteristicKind { 78 C_User, C_System, C_ExternCSystem 79 }; 80 81 /// \brief One instance of this struct is kept for every file loaded or used. 82 //// 83 /// This object owns the MemoryBuffer object. 84 class ContentCache { 85 enum CCFlags { 86 /// \brief Whether the buffer is invalid. 87 InvalidFlag = 0x01, 88 /// \brief Whether the buffer should not be freed on destruction. 89 DoNotFreeFlag = 0x02 90 }; 91 92 /// \brief The actual buffer containing the characters from the input 93 /// file. 94 /// 95 /// This is owned by the ContentCache object. The bits indicate 96 /// whether the buffer is invalid. 97 mutable llvm::PointerIntPair<const llvm::MemoryBuffer *, 2> Buffer; 98 99 public: 100 /// \brief Reference to the file entry representing this ContentCache. 101 /// 102 /// This reference does not own the FileEntry object. 103 /// 104 /// It is possible for this to be NULL if the ContentCache encapsulates 105 /// an imaginary text buffer. 106 const FileEntry *OrigEntry; 107 108 /// \brief References the file which the contents were actually loaded from. 109 /// 110 /// Can be different from 'Entry' if we overridden the contents of one file 111 /// with the contents of another file. 112 const FileEntry *ContentsEntry; 113 114 /// \brief A bump pointer allocated array of offsets for each source line. 115 /// 116 /// This is lazily computed. This is owned by the SourceManager 117 /// BumpPointerAllocator object. 118 unsigned *SourceLineCache; 119 120 /// \brief The number of lines in this ContentCache. 121 /// 122 /// This is only valid if SourceLineCache is non-null. 123 unsigned NumLines : 31; 124 125 /// \brief Indicates whether the buffer itself was provided to override 126 /// the actual file contents. 127 /// 128 /// When true, the original entry may be a virtual file that does not 129 /// exist. 130 unsigned BufferOverridden : 1; 131 132 /// \brief True if this content cache was initially created for a source 133 /// file considered as a system one. 134 unsigned IsSystemFile : 1; 135 136 ContentCache(const FileEntry *Ent = 0) 137 : Buffer(0, false), OrigEntry(Ent), ContentsEntry(Ent), 138 SourceLineCache(0), NumLines(0), BufferOverridden(false), 139 IsSystemFile(false) {} 140 ContentCache(const FileEntry * Ent,const FileEntry * contentEnt)141 ContentCache(const FileEntry *Ent, const FileEntry *contentEnt) 142 : Buffer(0, false), OrigEntry(Ent), ContentsEntry(contentEnt), 143 SourceLineCache(0), NumLines(0), BufferOverridden(false), 144 IsSystemFile(false) {} 145 146 ~ContentCache(); 147 148 /// The copy ctor does not allow copies where source object has either 149 /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory 150 /// is not transferred, so this is a logical error. ContentCache(const ContentCache & RHS)151 ContentCache(const ContentCache &RHS) 152 : Buffer(0, false), SourceLineCache(0), BufferOverridden(false), 153 IsSystemFile(false) 154 { 155 OrigEntry = RHS.OrigEntry; 156 ContentsEntry = RHS.ContentsEntry; 157 158 assert (RHS.Buffer.getPointer() == 0 && RHS.SourceLineCache == 0 && 159 "Passed ContentCache object cannot own a buffer."); 160 161 NumLines = RHS.NumLines; 162 } 163 164 /// \brief Returns the memory buffer for the associated content. 165 /// 166 /// \param Diag Object through which diagnostics will be emitted if the 167 /// buffer cannot be retrieved. 168 /// 169 /// \param Loc If specified, is the location that invalid file diagnostics 170 /// will be emitted at. 171 /// 172 /// \param Invalid If non-NULL, will be set \c true if an error occurred. 173 const llvm::MemoryBuffer *getBuffer(DiagnosticsEngine &Diag, 174 const SourceManager &SM, 175 SourceLocation Loc = SourceLocation(), 176 bool *Invalid = 0) const; 177 178 /// \brief Returns the size of the content encapsulated by this 179 /// ContentCache. 180 /// 181 /// This can be the size of the source file or the size of an 182 /// arbitrary scratch buffer. If the ContentCache encapsulates a source 183 /// file this size is retrieved from the file's FileEntry. 184 unsigned getSize() const; 185 186 /// \brief Returns the number of bytes actually mapped for this 187 /// ContentCache. 188 /// 189 /// This can be 0 if the MemBuffer was not actually expanded. 190 unsigned getSizeBytesMapped() const; 191 192 /// Returns the kind of memory used to back the memory buffer for 193 /// this content cache. This is used for performance analysis. 194 llvm::MemoryBuffer::BufferKind getMemoryBufferKind() const; 195 setBuffer(const llvm::MemoryBuffer * B)196 void setBuffer(const llvm::MemoryBuffer *B) { 197 assert(!Buffer.getPointer() && "MemoryBuffer already set."); 198 Buffer.setPointer(B); 199 Buffer.setInt(false); 200 } 201 202 /// \brief Get the underlying buffer, returning NULL if the buffer is not 203 /// yet available. getRawBuffer()204 const llvm::MemoryBuffer *getRawBuffer() const { 205 return Buffer.getPointer(); 206 } 207 208 /// \brief Replace the existing buffer (which will be deleted) 209 /// with the given buffer. 210 void replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree = false); 211 212 /// \brief Determine whether the buffer itself is invalid. isBufferInvalid()213 bool isBufferInvalid() const { 214 return Buffer.getInt() & InvalidFlag; 215 } 216 217 /// \brief Determine whether the buffer should be freed. shouldFreeBuffer()218 bool shouldFreeBuffer() const { 219 return (Buffer.getInt() & DoNotFreeFlag) == 0; 220 } 221 222 private: 223 // Disable assignments. 224 ContentCache &operator=(const ContentCache& RHS); 225 }; 226 227 /// \brief Information about a FileID, basically just the logical file 228 /// that it represents and include stack information. 229 /// 230 /// Each FileInfo has include stack information, indicating where it came 231 /// from. This information encodes the \#include chain that a token was 232 /// expanded from. The main include file has an invalid IncludeLoc. 233 /// 234 /// FileInfos contain a "ContentCache *", with the contents of the file. 235 /// 236 class FileInfo { 237 /// \brief The location of the \#include that brought in this file. 238 /// 239 /// This is an invalid SLOC for the main file (top of the \#include chain). 240 unsigned IncludeLoc; // Really a SourceLocation 241 242 /// \brief Number of FileIDs (files and macros) that were created during 243 /// preprocessing of this \#include, including this SLocEntry. 244 /// 245 /// Zero means the preprocessor didn't provide such info for this SLocEntry. 246 unsigned NumCreatedFIDs; 247 248 /// \brief Contains the ContentCache* and the bits indicating the 249 /// characteristic of the file and whether it has \#line info, all 250 /// bitmangled together. 251 uintptr_t Data; 252 253 friend class clang::SourceManager; 254 friend class clang::ASTWriter; 255 friend class clang::ASTReader; 256 public: 257 /// \brief Return a FileInfo object. get(SourceLocation IL,const ContentCache * Con,CharacteristicKind FileCharacter)258 static FileInfo get(SourceLocation IL, const ContentCache *Con, 259 CharacteristicKind FileCharacter) { 260 FileInfo X; 261 X.IncludeLoc = IL.getRawEncoding(); 262 X.NumCreatedFIDs = 0; 263 X.Data = (uintptr_t)Con; 264 assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned"); 265 assert((unsigned)FileCharacter < 4 && "invalid file character"); 266 X.Data |= (unsigned)FileCharacter; 267 return X; 268 } 269 getIncludeLoc()270 SourceLocation getIncludeLoc() const { 271 return SourceLocation::getFromRawEncoding(IncludeLoc); 272 } getContentCache()273 const ContentCache* getContentCache() const { 274 return reinterpret_cast<const ContentCache*>(Data & ~7UL); 275 } 276 277 /// \brief Return whether this is a system header or not. getFileCharacteristic()278 CharacteristicKind getFileCharacteristic() const { 279 return (CharacteristicKind)(Data & 3); 280 } 281 282 /// \brief Return true if this FileID has \#line directives in it. hasLineDirectives()283 bool hasLineDirectives() const { return (Data & 4) != 0; } 284 285 /// \brief Set the flag that indicates that this FileID has 286 /// line table entries associated with it. setHasLineDirectives()287 void setHasLineDirectives() { 288 Data |= 4; 289 } 290 }; 291 292 /// \brief Each ExpansionInfo encodes the expansion location - where 293 /// the token was ultimately expanded, and the SpellingLoc - where the actual 294 /// character data for the token came from. 295 class ExpansionInfo { 296 // Really these are all SourceLocations. 297 298 /// \brief Where the spelling for the token can be found. 299 unsigned SpellingLoc; 300 301 /// In a macro expansion, ExpansionLocStart and ExpansionLocEnd 302 /// indicate the start and end of the expansion. In object-like macros, 303 /// they will be the same. In a function-like macro expansion, the start 304 /// will be the identifier and the end will be the ')'. Finally, in 305 /// macro-argument instantiations, the end will be 'SourceLocation()', an 306 /// invalid location. 307 unsigned ExpansionLocStart, ExpansionLocEnd; 308 309 public: getSpellingLoc()310 SourceLocation getSpellingLoc() const { 311 return SourceLocation::getFromRawEncoding(SpellingLoc); 312 } getExpansionLocStart()313 SourceLocation getExpansionLocStart() const { 314 return SourceLocation::getFromRawEncoding(ExpansionLocStart); 315 } getExpansionLocEnd()316 SourceLocation getExpansionLocEnd() const { 317 SourceLocation EndLoc = 318 SourceLocation::getFromRawEncoding(ExpansionLocEnd); 319 return EndLoc.isInvalid() ? getExpansionLocStart() : EndLoc; 320 } 321 getExpansionLocRange()322 std::pair<SourceLocation,SourceLocation> getExpansionLocRange() const { 323 return std::make_pair(getExpansionLocStart(), getExpansionLocEnd()); 324 } 325 isMacroArgExpansion()326 bool isMacroArgExpansion() const { 327 // Note that this needs to return false for default constructed objects. 328 return getExpansionLocStart().isValid() && 329 SourceLocation::getFromRawEncoding(ExpansionLocEnd).isInvalid(); 330 } 331 isFunctionMacroExpansion()332 bool isFunctionMacroExpansion() const { 333 return getExpansionLocStart().isValid() && 334 getExpansionLocStart() != getExpansionLocEnd(); 335 } 336 337 /// \brief Return a ExpansionInfo for an expansion. 338 /// 339 /// Start and End specify the expansion range (where the macro is 340 /// expanded), and SpellingLoc specifies the spelling location (where 341 /// the characters from the token come from). All three can refer to 342 /// normal File SLocs or expansion locations. create(SourceLocation SpellingLoc,SourceLocation Start,SourceLocation End)343 static ExpansionInfo create(SourceLocation SpellingLoc, 344 SourceLocation Start, SourceLocation End) { 345 ExpansionInfo X; 346 X.SpellingLoc = SpellingLoc.getRawEncoding(); 347 X.ExpansionLocStart = Start.getRawEncoding(); 348 X.ExpansionLocEnd = End.getRawEncoding(); 349 return X; 350 } 351 352 /// \brief Return a special ExpansionInfo for the expansion of 353 /// a macro argument into a function-like macro's body. 354 /// 355 /// ExpansionLoc specifies the expansion location (where the macro is 356 /// expanded). This doesn't need to be a range because a macro is always 357 /// expanded at a macro parameter reference, and macro parameters are 358 /// always exactly one token. SpellingLoc specifies the spelling location 359 /// (where the characters from the token come from). ExpansionLoc and 360 /// SpellingLoc can both refer to normal File SLocs or expansion locations. 361 /// 362 /// Given the code: 363 /// \code 364 /// #define F(x) f(x) 365 /// F(42); 366 /// \endcode 367 /// 368 /// When expanding '\c F(42)', the '\c x' would call this with an 369 /// SpellingLoc pointing at '\c 42' and an ExpansionLoc pointing at its 370 /// location in the definition of '\c F'. createForMacroArg(SourceLocation SpellingLoc,SourceLocation ExpansionLoc)371 static ExpansionInfo createForMacroArg(SourceLocation SpellingLoc, 372 SourceLocation ExpansionLoc) { 373 // We store an intentionally invalid source location for the end of the 374 // expansion range to mark that this is a macro argument ion rather than 375 // a normal one. 376 return create(SpellingLoc, ExpansionLoc, SourceLocation()); 377 } 378 }; 379 380 /// \brief This is a discriminated union of FileInfo and ExpansionInfo. 381 /// 382 /// SourceManager keeps an array of these objects, and they are uniquely 383 /// identified by the FileID datatype. 384 class SLocEntry { 385 unsigned Offset; // low bit is set for expansion info. 386 union { 387 FileInfo File; 388 ExpansionInfo Expansion; 389 }; 390 public: getOffset()391 unsigned getOffset() const { return Offset >> 1; } 392 isExpansion()393 bool isExpansion() const { return Offset & 1; } isFile()394 bool isFile() const { return !isExpansion(); } 395 getFile()396 const FileInfo &getFile() const { 397 assert(isFile() && "Not a file SLocEntry!"); 398 return File; 399 } 400 getExpansion()401 const ExpansionInfo &getExpansion() const { 402 assert(isExpansion() && "Not a macro expansion SLocEntry!"); 403 return Expansion; 404 } 405 get(unsigned Offset,const FileInfo & FI)406 static SLocEntry get(unsigned Offset, const FileInfo &FI) { 407 SLocEntry E; 408 E.Offset = Offset << 1; 409 E.File = FI; 410 return E; 411 } 412 get(unsigned Offset,const ExpansionInfo & Expansion)413 static SLocEntry get(unsigned Offset, const ExpansionInfo &Expansion) { 414 SLocEntry E; 415 E.Offset = (Offset << 1) | 1; 416 E.Expansion = Expansion; 417 return E; 418 } 419 }; 420 } // end SrcMgr namespace. 421 422 /// \brief External source of source location entries. 423 class ExternalSLocEntrySource { 424 public: 425 virtual ~ExternalSLocEntrySource(); 426 427 /// \brief Read the source location entry with index ID, which will always be 428 /// less than -1. 429 /// 430 /// \returns true if an error occurred that prevented the source-location 431 /// entry from being loaded. 432 virtual bool ReadSLocEntry(int ID) = 0; 433 }; 434 435 436 /// \brief Holds the cache used by isBeforeInTranslationUnit. 437 /// 438 /// The cache structure is complex enough to be worth breaking out of 439 /// SourceManager. 440 class IsBeforeInTranslationUnitCache { 441 /// \brief The FileID's of the cached query. 442 /// 443 /// If these match up with a subsequent query, the result can be reused. 444 FileID LQueryFID, RQueryFID; 445 446 /// \brief True if LQueryFID was created before RQueryFID. 447 /// 448 /// This is used to compare macro expansion locations. 449 bool IsLQFIDBeforeRQFID; 450 451 /// \brief The file found in common between the two \#include traces, i.e., 452 /// the nearest common ancestor of the \#include tree. 453 FileID CommonFID; 454 455 /// \brief The offset of the previous query in CommonFID. 456 /// 457 /// Usually, this represents the location of the \#include for QueryFID, but 458 /// if LQueryFID is a parent of RQueryFID (or vice versa) then these can be a 459 /// random token in the parent. 460 unsigned LCommonOffset, RCommonOffset; 461 public: 462 463 /// \brief Return true if the currently cached values match up with 464 /// the specified LHS/RHS query. 465 /// 466 /// If not, we can't use the cache. isCacheValid(FileID LHS,FileID RHS)467 bool isCacheValid(FileID LHS, FileID RHS) const { 468 return LQueryFID == LHS && RQueryFID == RHS; 469 } 470 471 /// \brief If the cache is valid, compute the result given the 472 /// specified offsets in the LHS/RHS FileID's. getCachedResult(unsigned LOffset,unsigned ROffset)473 bool getCachedResult(unsigned LOffset, unsigned ROffset) const { 474 // If one of the query files is the common file, use the offset. Otherwise, 475 // use the #include loc in the common file. 476 if (LQueryFID != CommonFID) LOffset = LCommonOffset; 477 if (RQueryFID != CommonFID) ROffset = RCommonOffset; 478 479 // It is common for multiple macro expansions to be "included" from the same 480 // location (expansion location), in which case use the order of the FileIDs 481 // to determine which came first. This will also take care the case where 482 // one of the locations points at the inclusion/expansion point of the other 483 // in which case its FileID will come before the other. 484 if (LOffset == ROffset) 485 return IsLQFIDBeforeRQFID; 486 487 return LOffset < ROffset; 488 } 489 490 /// \brief Set up a new query. setQueryFIDs(FileID LHS,FileID RHS,bool isLFIDBeforeRFID)491 void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) { 492 assert(LHS != RHS); 493 LQueryFID = LHS; 494 RQueryFID = RHS; 495 IsLQFIDBeforeRQFID = isLFIDBeforeRFID; 496 } 497 clear()498 void clear() { 499 LQueryFID = RQueryFID = FileID(); 500 IsLQFIDBeforeRQFID = false; 501 } 502 setCommonLoc(FileID commonFID,unsigned lCommonOffset,unsigned rCommonOffset)503 void setCommonLoc(FileID commonFID, unsigned lCommonOffset, 504 unsigned rCommonOffset) { 505 CommonFID = commonFID; 506 LCommonOffset = lCommonOffset; 507 RCommonOffset = rCommonOffset; 508 } 509 510 }; 511 512 /// \brief This class handles loading and caching of source files into memory. 513 /// 514 /// This object owns the MemoryBuffer objects for all of the loaded 515 /// files and assigns unique FileID's for each unique \#include chain. 516 /// 517 /// The SourceManager can be queried for information about SourceLocation 518 /// objects, turning them into either spelling or expansion locations. Spelling 519 /// locations represent where the bytes corresponding to a token came from and 520 /// expansion locations represent where the location is in the user's view. In 521 /// the case of a macro expansion, for example, the spelling location indicates 522 /// where the expanded token came from and the expansion location specifies 523 /// where it was expanded. 524 class SourceManager : public RefCountedBase<SourceManager> { 525 /// \brief DiagnosticsEngine object. 526 DiagnosticsEngine &Diag; 527 528 FileManager &FileMgr; 529 530 mutable llvm::BumpPtrAllocator ContentCacheAlloc; 531 532 /// \brief Memoized information about all of the files tracked by this 533 /// SourceManager. 534 /// 535 /// This map allows us to merge ContentCache entries based 536 /// on their FileEntry*. All ContentCache objects will thus have unique, 537 /// non-null, FileEntry pointers. 538 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos; 539 540 /// \brief True if the ContentCache for files that are overriden by other 541 /// files, should report the original file name. Defaults to true. 542 bool OverridenFilesKeepOriginalName; 543 544 /// \brief True if non-system source files should be treated as volatile 545 /// (likely to change while trying to use them). Defaults to false. 546 bool UserFilesAreVolatile; 547 548 struct OverriddenFilesInfoTy { 549 /// \brief Files that have been overriden with the contents from another 550 /// file. 551 llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles; 552 /// \brief Files that were overridden with a memory buffer. 553 llvm::DenseSet<const FileEntry *> OverriddenFilesWithBuffer; 554 }; 555 556 /// \brief Lazily create the object keeping overridden files info, since 557 /// it is uncommonly used. 558 OwningPtr<OverriddenFilesInfoTy> OverriddenFilesInfo; 559 getOverriddenFilesInfo()560 OverriddenFilesInfoTy &getOverriddenFilesInfo() { 561 if (!OverriddenFilesInfo) 562 OverriddenFilesInfo.reset(new OverriddenFilesInfoTy); 563 return *OverriddenFilesInfo; 564 } 565 566 /// \brief Information about various memory buffers that we have read in. 567 /// 568 /// All FileEntry* within the stored ContentCache objects are NULL, 569 /// as they do not refer to a file. 570 std::vector<SrcMgr::ContentCache*> MemBufferInfos; 571 572 /// \brief The table of SLocEntries that are local to this module. 573 /// 574 /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid 575 /// expansion. 576 std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable; 577 578 /// \brief The table of SLocEntries that are loaded from other modules. 579 /// 580 /// Negative FileIDs are indexes into this table. To get from ID to an index, 581 /// use (-ID - 2). 582 mutable std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable; 583 584 /// \brief The starting offset of the next local SLocEntry. 585 /// 586 /// This is LocalSLocEntryTable.back().Offset + the size of that entry. 587 unsigned NextLocalOffset; 588 589 /// \brief The starting offset of the latest batch of loaded SLocEntries. 590 /// 591 /// This is LoadedSLocEntryTable.back().Offset, except that that entry might 592 /// not have been loaded, so that value would be unknown. 593 unsigned CurrentLoadedOffset; 594 595 /// \brief The highest possible offset is 2^31-1, so CurrentLoadedOffset 596 /// starts at 2^31. 597 static const unsigned MaxLoadedOffset = 1U << 31U; 598 599 /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable 600 /// have already been loaded from the external source. 601 /// 602 /// Same indexing as LoadedSLocEntryTable. 603 std::vector<bool> SLocEntryLoaded; 604 605 /// \brief An external source for source location entries. 606 ExternalSLocEntrySource *ExternalSLocEntries; 607 608 /// \brief A one-entry cache to speed up getFileID. 609 /// 610 /// LastFileIDLookup records the last FileID looked up or created, because it 611 /// is very common to look up many tokens from the same file. 612 mutable FileID LastFileIDLookup; 613 614 /// \brief Holds information for \#line directives. 615 /// 616 /// This is referenced by indices from SLocEntryTable. 617 LineTableInfo *LineTable; 618 619 /// \brief These ivars serve as a cache used in the getLineNumber 620 /// method which is used to speedup getLineNumber calls to nearby locations. 621 mutable FileID LastLineNoFileIDQuery; 622 mutable SrcMgr::ContentCache *LastLineNoContentCache; 623 mutable unsigned LastLineNoFilePos; 624 mutable unsigned LastLineNoResult; 625 626 /// \brief The file ID for the main source file of the translation unit. 627 FileID MainFileID; 628 629 /// \brief The file ID for the precompiled preamble there is one. 630 FileID PreambleFileID; 631 632 // Statistics for -print-stats. 633 mutable unsigned NumLinearScans, NumBinaryProbes; 634 635 // Cache results for the isBeforeInTranslationUnit method. 636 mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache; 637 638 // Cache for the "fake" buffer used for error-recovery purposes. 639 mutable llvm::MemoryBuffer *FakeBufferForRecovery; 640 641 mutable SrcMgr::ContentCache *FakeContentCacheForRecovery; 642 643 /// \brief Lazily computed map of macro argument chunks to their expanded 644 /// source location. 645 typedef std::map<unsigned, SourceLocation> MacroArgsMap; 646 647 mutable llvm::DenseMap<FileID, MacroArgsMap *> MacroArgsCacheMap; 648 649 // SourceManager doesn't support copy construction. 650 explicit SourceManager(const SourceManager&); 651 void operator=(const SourceManager&); 652 public: 653 SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr, 654 bool UserFilesAreVolatile = false); 655 ~SourceManager(); 656 657 void clearIDTables(); 658 getDiagnostics()659 DiagnosticsEngine &getDiagnostics() const { return Diag; } 660 getFileManager()661 FileManager &getFileManager() const { return FileMgr; } 662 663 /// \brief Set true if the SourceManager should report the original file name 664 /// for contents of files that were overriden by other files.Defaults to true. setOverridenFilesKeepOriginalName(bool value)665 void setOverridenFilesKeepOriginalName(bool value) { 666 OverridenFilesKeepOriginalName = value; 667 } 668 669 /// \brief True if non-system source files should be treated as volatile 670 /// (likely to change while trying to use them). userFilesAreVolatile()671 bool userFilesAreVolatile() const { return UserFilesAreVolatile; } 672 673 /// \brief Create the FileID for a memory buffer that will represent the 674 /// FileID for the main source. 675 /// 676 /// One example of when this would be used is when the main source is read 677 /// from STDIN. createMainFileIDForMemBuffer(const llvm::MemoryBuffer * Buffer)678 FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) { 679 assert(MainFileID.isInvalid() && "MainFileID already set!"); 680 MainFileID = createFileIDForMemBuffer(Buffer); 681 return MainFileID; 682 } 683 684 //===--------------------------------------------------------------------===// 685 // MainFileID creation and querying methods. 686 //===--------------------------------------------------------------------===// 687 688 /// \brief Returns the FileID of the main source file. getMainFileID()689 FileID getMainFileID() const { return MainFileID; } 690 691 /// \brief Create the FileID for the main source file. 692 FileID createMainFileID(const FileEntry *SourceFile, 693 SrcMgr::CharacteristicKind Kind = SrcMgr::C_User) { 694 assert(MainFileID.isInvalid() && "MainFileID already set!"); 695 MainFileID = createFileID(SourceFile, SourceLocation(), Kind); 696 return MainFileID; 697 } 698 699 /// \brief Set the file ID for the main source file. setMainFileID(FileID FID)700 void setMainFileID(FileID FID) { 701 assert(MainFileID.isInvalid() && "MainFileID already set!"); 702 MainFileID = FID; 703 } 704 705 /// \brief Set the file ID for the precompiled preamble. setPreambleFileID(FileID Preamble)706 void setPreambleFileID(FileID Preamble) { 707 assert(PreambleFileID.isInvalid() && "PreambleFileID already set!"); 708 PreambleFileID = Preamble; 709 } 710 711 /// \brief Get the file ID for the precompiled preamble if there is one. getPreambleFileID()712 FileID getPreambleFileID() const { return PreambleFileID; } 713 714 //===--------------------------------------------------------------------===// 715 // Methods to create new FileID's and macro expansions. 716 //===--------------------------------------------------------------------===// 717 718 /// \brief Create a new FileID that represents the specified file 719 /// being \#included from the specified IncludePosition. 720 /// 721 /// This translates NULL into standard input. 722 FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, 723 SrcMgr::CharacteristicKind FileCharacter, 724 int LoadedID = 0, unsigned LoadedOffset = 0) { 725 const SrcMgr::ContentCache * 726 IR = getOrCreateContentCache(SourceFile, 727 /*isSystemFile=*/FileCharacter != SrcMgr::C_User); 728 assert(IR && "getOrCreateContentCache() cannot return NULL"); 729 return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset); 730 } 731 732 /// \brief Create a new FileID that represents the specified memory buffer. 733 /// 734 /// This does no caching of the buffer and takes ownership of the 735 /// MemoryBuffer, so only pass a MemoryBuffer to this once. 736 FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer, 737 int LoadedID = 0, unsigned LoadedOffset = 0, 738 SourceLocation IncludeLoc = SourceLocation()) { 739 return createFileID(createMemBufferContentCache(Buffer), IncludeLoc, 740 SrcMgr::C_User, LoadedID, LoadedOffset); 741 } 742 743 /// \brief Return a new SourceLocation that encodes the 744 /// fact that a token from SpellingLoc should actually be referenced from 745 /// ExpansionLoc, and that it represents the expansion of a macro argument 746 /// into the function-like macro body. 747 SourceLocation createMacroArgExpansionLoc(SourceLocation Loc, 748 SourceLocation ExpansionLoc, 749 unsigned TokLength); 750 751 /// \brief Return a new SourceLocation that encodes the fact 752 /// that a token from SpellingLoc should actually be referenced from 753 /// ExpansionLoc. 754 SourceLocation createExpansionLoc(SourceLocation Loc, 755 SourceLocation ExpansionLocStart, 756 SourceLocation ExpansionLocEnd, 757 unsigned TokLength, 758 int LoadedID = 0, 759 unsigned LoadedOffset = 0); 760 761 /// \brief Retrieve the memory buffer associated with the given file. 762 /// 763 /// \param Invalid If non-NULL, will be set \c true if an error 764 /// occurs while retrieving the memory buffer. 765 const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File, 766 bool *Invalid = 0); 767 768 /// \brief Override the contents of the given source file by providing an 769 /// already-allocated buffer. 770 /// 771 /// \param SourceFile the source file whose contents will be overriden. 772 /// 773 /// \param Buffer the memory buffer whose contents will be used as the 774 /// data in the given source file. 775 /// 776 /// \param DoNotFree If true, then the buffer will not be freed when the 777 /// source manager is destroyed. 778 void overrideFileContents(const FileEntry *SourceFile, 779 const llvm::MemoryBuffer *Buffer, 780 bool DoNotFree = false); 781 782 /// \brief Override the given source file with another one. 783 /// 784 /// \param SourceFile the source file which will be overriden. 785 /// 786 /// \param NewFile the file whose contents will be used as the 787 /// data instead of the contents of the given source file. 788 void overrideFileContents(const FileEntry *SourceFile, 789 const FileEntry *NewFile); 790 791 /// \brief Returns true if the file contents have been overridden. isFileOverridden(const FileEntry * File)792 bool isFileOverridden(const FileEntry *File) { 793 if (OverriddenFilesInfo) { 794 if (OverriddenFilesInfo->OverriddenFilesWithBuffer.count(File)) 795 return true; 796 if (OverriddenFilesInfo->OverriddenFiles.find(File) != 797 OverriddenFilesInfo->OverriddenFiles.end()) 798 return true; 799 } 800 return false; 801 } 802 803 /// \brief Disable overridding the contents of a file, previously enabled 804 /// with #overrideFileContents. 805 /// 806 /// This should be called before parsing has begun. 807 void disableFileContentsOverride(const FileEntry *File); 808 809 //===--------------------------------------------------------------------===// 810 // FileID manipulation methods. 811 //===--------------------------------------------------------------------===// 812 813 /// \brief Return the buffer for the specified FileID. 814 /// 815 /// If there is an error opening this buffer the first time, this 816 /// manufactures a temporary buffer and returns a non-empty error string. 817 const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc, 818 bool *Invalid = 0) const { 819 bool MyInvalid = false; 820 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); 821 if (MyInvalid || !Entry.isFile()) { 822 if (Invalid) 823 *Invalid = true; 824 825 return getFakeBufferForRecovery(); 826 } 827 828 return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc, 829 Invalid); 830 } 831 832 const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const { 833 bool MyInvalid = false; 834 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); 835 if (MyInvalid || !Entry.isFile()) { 836 if (Invalid) 837 *Invalid = true; 838 839 return getFakeBufferForRecovery(); 840 } 841 842 return Entry.getFile().getContentCache()->getBuffer(Diag, *this, 843 SourceLocation(), 844 Invalid); 845 } 846 847 /// \brief Returns the FileEntry record for the provided FileID. getFileEntryForID(FileID FID)848 const FileEntry *getFileEntryForID(FileID FID) const { 849 bool MyInvalid = false; 850 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); 851 if (MyInvalid || !Entry.isFile()) 852 return 0; 853 854 const SrcMgr::ContentCache *Content = Entry.getFile().getContentCache(); 855 if (!Content) 856 return 0; 857 return Content->OrigEntry; 858 } 859 860 /// \brief Returns the FileEntry record for the provided SLocEntry. getFileEntryForSLocEntry(const SrcMgr::SLocEntry & sloc)861 const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const 862 { 863 const SrcMgr::ContentCache *Content = sloc.getFile().getContentCache(); 864 if (!Content) 865 return 0; 866 return Content->OrigEntry; 867 } 868 869 /// \brief Return a StringRef to the source buffer data for the 870 /// specified FileID. 871 /// 872 /// \param FID The file ID whose contents will be returned. 873 /// \param Invalid If non-NULL, will be set true if an error occurred. 874 StringRef getBufferData(FileID FID, bool *Invalid = 0) const; 875 876 /// \brief Get the number of FileIDs (files and macros) that were created 877 /// during preprocessing of \p FID, including it. getNumCreatedFIDsForFileID(FileID FID)878 unsigned getNumCreatedFIDsForFileID(FileID FID) const { 879 bool Invalid = false; 880 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 881 if (Invalid || !Entry.isFile()) 882 return 0; 883 884 return Entry.getFile().NumCreatedFIDs; 885 } 886 887 /// \brief Set the number of FileIDs (files and macros) that were created 888 /// during preprocessing of \p FID, including it. setNumCreatedFIDsForFileID(FileID FID,unsigned NumFIDs)889 void setNumCreatedFIDsForFileID(FileID FID, unsigned NumFIDs) const { 890 bool Invalid = false; 891 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 892 if (Invalid || !Entry.isFile()) 893 return; 894 895 assert(Entry.getFile().NumCreatedFIDs == 0 && "Already set!"); 896 const_cast<SrcMgr::FileInfo &>(Entry.getFile()).NumCreatedFIDs = NumFIDs; 897 } 898 899 //===--------------------------------------------------------------------===// 900 // SourceLocation manipulation methods. 901 //===--------------------------------------------------------------------===// 902 903 /// \brief Return the FileID for a SourceLocation. 904 /// 905 /// This is a very hot method that is used for all SourceManager queries 906 /// that start with a SourceLocation object. It is responsible for finding 907 /// the entry in SLocEntryTable which contains the specified location. 908 /// getFileID(SourceLocation SpellingLoc)909 FileID getFileID(SourceLocation SpellingLoc) const { 910 unsigned SLocOffset = SpellingLoc.getOffset(); 911 912 // If our one-entry cache covers this offset, just return it. 913 if (isOffsetInFileID(LastFileIDLookup, SLocOffset)) 914 return LastFileIDLookup; 915 916 return getFileIDSlow(SLocOffset); 917 } 918 919 /// \brief Return the filename of the file containing a SourceLocation. getFilename(SourceLocation SpellingLoc)920 StringRef getFilename(SourceLocation SpellingLoc) const { 921 if (const FileEntry *F = getFileEntryForID(getFileID(SpellingLoc))) 922 return F->getName(); 923 return StringRef(); 924 } 925 926 /// \brief Return the source location corresponding to the first byte of 927 /// the specified file. getLocForStartOfFile(FileID FID)928 SourceLocation getLocForStartOfFile(FileID FID) const { 929 bool Invalid = false; 930 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 931 if (Invalid || !Entry.isFile()) 932 return SourceLocation(); 933 934 unsigned FileOffset = Entry.getOffset(); 935 return SourceLocation::getFileLoc(FileOffset); 936 } 937 938 /// \brief Return the source location corresponding to the last byte of the 939 /// specified file. getLocForEndOfFile(FileID FID)940 SourceLocation getLocForEndOfFile(FileID FID) const { 941 bool Invalid = false; 942 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 943 if (Invalid || !Entry.isFile()) 944 return SourceLocation(); 945 946 unsigned FileOffset = Entry.getOffset(); 947 return SourceLocation::getFileLoc(FileOffset + getFileIDSize(FID) - 1); 948 } 949 950 /// \brief Returns the include location if \p FID is a \#include'd file 951 /// otherwise it returns an invalid location. getIncludeLoc(FileID FID)952 SourceLocation getIncludeLoc(FileID FID) const { 953 bool Invalid = false; 954 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 955 if (Invalid || !Entry.isFile()) 956 return SourceLocation(); 957 958 return Entry.getFile().getIncludeLoc(); 959 } 960 961 /// \brief Given a SourceLocation object \p Loc, return the expansion 962 /// location referenced by the ID. getExpansionLoc(SourceLocation Loc)963 SourceLocation getExpansionLoc(SourceLocation Loc) const { 964 // Handle the non-mapped case inline, defer to out of line code to handle 965 // expansions. 966 if (Loc.isFileID()) return Loc; 967 return getExpansionLocSlowCase(Loc); 968 } 969 970 /// \brief Given \p Loc, if it is a macro location return the expansion 971 /// location or the spelling location, depending on if it comes from a 972 /// macro argument or not. getFileLoc(SourceLocation Loc)973 SourceLocation getFileLoc(SourceLocation Loc) const { 974 if (Loc.isFileID()) return Loc; 975 return getFileLocSlowCase(Loc); 976 } 977 978 /// \brief Return the start/end of the expansion information for an 979 /// expansion location. 980 /// 981 /// \pre \p Loc is required to be an expansion location. 982 std::pair<SourceLocation,SourceLocation> 983 getImmediateExpansionRange(SourceLocation Loc) const; 984 985 /// \brief Given a SourceLocation object, return the range of 986 /// tokens covered by the expansion the ultimate file. 987 std::pair<SourceLocation,SourceLocation> 988 getExpansionRange(SourceLocation Loc) const; 989 990 991 /// \brief Given a SourceLocation object, return the spelling 992 /// location referenced by the ID. 993 /// 994 /// This is the place where the characters that make up the lexed token 995 /// can be found. getSpellingLoc(SourceLocation Loc)996 SourceLocation getSpellingLoc(SourceLocation Loc) const { 997 // Handle the non-mapped case inline, defer to out of line code to handle 998 // expansions. 999 if (Loc.isFileID()) return Loc; 1000 return getSpellingLocSlowCase(Loc); 1001 } 1002 1003 /// \brief Given a SourceLocation object, return the spelling location 1004 /// referenced by the ID. 1005 /// 1006 /// This is the first level down towards the place where the characters 1007 /// that make up the lexed token can be found. This should not generally 1008 /// be used by clients. 1009 SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const; 1010 1011 /// \brief Decompose the specified location into a raw FileID + Offset pair. 1012 /// 1013 /// The first element is the FileID, the second is the offset from the 1014 /// start of the buffer of the location. getDecomposedLoc(SourceLocation Loc)1015 std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const { 1016 FileID FID = getFileID(Loc); 1017 bool Invalid = false; 1018 const SrcMgr::SLocEntry &E = getSLocEntry(FID, &Invalid); 1019 if (Invalid) 1020 return std::make_pair(FileID(), 0); 1021 return std::make_pair(FID, Loc.getOffset()-E.getOffset()); 1022 } 1023 1024 /// \brief Decompose the specified location into a raw FileID + Offset pair. 1025 /// 1026 /// If the location is an expansion record, walk through it until we find 1027 /// the final location expanded. 1028 std::pair<FileID, unsigned> getDecomposedExpansionLoc(SourceLocation Loc)1029 getDecomposedExpansionLoc(SourceLocation Loc) const { 1030 FileID FID = getFileID(Loc); 1031 bool Invalid = false; 1032 const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid); 1033 if (Invalid) 1034 return std::make_pair(FileID(), 0); 1035 1036 unsigned Offset = Loc.getOffset()-E->getOffset(); 1037 if (Loc.isFileID()) 1038 return std::make_pair(FID, Offset); 1039 1040 return getDecomposedExpansionLocSlowCase(E); 1041 } 1042 1043 /// \brief Decompose the specified location into a raw FileID + Offset pair. 1044 /// 1045 /// If the location is an expansion record, walk through it until we find 1046 /// its spelling record. 1047 std::pair<FileID, unsigned> getDecomposedSpellingLoc(SourceLocation Loc)1048 getDecomposedSpellingLoc(SourceLocation Loc) const { 1049 FileID FID = getFileID(Loc); 1050 bool Invalid = false; 1051 const SrcMgr::SLocEntry *E = &getSLocEntry(FID, &Invalid); 1052 if (Invalid) 1053 return std::make_pair(FileID(), 0); 1054 1055 unsigned Offset = Loc.getOffset()-E->getOffset(); 1056 if (Loc.isFileID()) 1057 return std::make_pair(FID, Offset); 1058 return getDecomposedSpellingLocSlowCase(E, Offset); 1059 } 1060 1061 /// \brief Returns the offset from the start of the file that the 1062 /// specified SourceLocation represents. 1063 /// 1064 /// This is not very meaningful for a macro ID. getFileOffset(SourceLocation SpellingLoc)1065 unsigned getFileOffset(SourceLocation SpellingLoc) const { 1066 return getDecomposedLoc(SpellingLoc).second; 1067 } 1068 1069 /// \brief Tests whether the given source location represents a macro 1070 /// argument's expansion into the function-like macro definition. 1071 /// 1072 /// Such source locations only appear inside of the expansion 1073 /// locations representing where a particular function-like macro was 1074 /// expanded. 1075 bool isMacroArgExpansion(SourceLocation Loc) const; 1076 1077 /// \brief Returns true if \p Loc is inside the [\p Start, +\p Length) 1078 /// chunk of the source location address space. 1079 /// 1080 /// If it's true and \p RelativeOffset is non-null, it will be set to the 1081 /// relative offset of \p Loc inside the chunk. 1082 bool isInSLocAddrSpace(SourceLocation Loc, 1083 SourceLocation Start, unsigned Length, 1084 unsigned *RelativeOffset = 0) const { 1085 assert(((Start.getOffset() < NextLocalOffset && 1086 Start.getOffset()+Length <= NextLocalOffset) || 1087 (Start.getOffset() >= CurrentLoadedOffset && 1088 Start.getOffset()+Length < MaxLoadedOffset)) && 1089 "Chunk is not valid SLoc address space"); 1090 unsigned LocOffs = Loc.getOffset(); 1091 unsigned BeginOffs = Start.getOffset(); 1092 unsigned EndOffs = BeginOffs + Length; 1093 if (LocOffs >= BeginOffs && LocOffs < EndOffs) { 1094 if (RelativeOffset) 1095 *RelativeOffset = LocOffs - BeginOffs; 1096 return true; 1097 } 1098 1099 return false; 1100 } 1101 1102 /// \brief Return true if both \p LHS and \p RHS are in the local source 1103 /// location address space or the loaded one. 1104 /// 1105 /// If it's true and \p RelativeOffset is non-null, it will be set to the 1106 /// offset of \p RHS relative to \p LHS. isInSameSLocAddrSpace(SourceLocation LHS,SourceLocation RHS,int * RelativeOffset)1107 bool isInSameSLocAddrSpace(SourceLocation LHS, SourceLocation RHS, 1108 int *RelativeOffset) const { 1109 unsigned LHSOffs = LHS.getOffset(), RHSOffs = RHS.getOffset(); 1110 bool LHSLoaded = LHSOffs >= CurrentLoadedOffset; 1111 bool RHSLoaded = RHSOffs >= CurrentLoadedOffset; 1112 1113 if (LHSLoaded == RHSLoaded) { 1114 if (RelativeOffset) 1115 *RelativeOffset = RHSOffs - LHSOffs; 1116 return true; 1117 } 1118 1119 return false; 1120 } 1121 1122 //===--------------------------------------------------------------------===// 1123 // Queries about the code at a SourceLocation. 1124 //===--------------------------------------------------------------------===// 1125 1126 /// \brief Return a pointer to the start of the specified location 1127 /// in the appropriate spelling MemoryBuffer. 1128 /// 1129 /// \param Invalid If non-NULL, will be set \c true if an error occurs. 1130 const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const; 1131 1132 /// \brief Return the column # for the specified file position. 1133 /// 1134 /// This is significantly cheaper to compute than the line number. This 1135 /// returns zero if the column number isn't known. This may only be called 1136 /// on a file sloc, so you must choose a spelling or expansion location 1137 /// before calling this method. 1138 unsigned getColumnNumber(FileID FID, unsigned FilePos, 1139 bool *Invalid = 0) const; 1140 unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const; 1141 unsigned getExpansionColumnNumber(SourceLocation Loc, 1142 bool *Invalid = 0) const; 1143 unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const; 1144 1145 1146 /// \brief Given a SourceLocation, return the spelling line number 1147 /// for the position indicated. 1148 /// 1149 /// This requires building and caching a table of line offsets for the 1150 /// MemoryBuffer, so this is not cheap: use only when about to emit a 1151 /// diagnostic. 1152 unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const; 1153 unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const; 1154 unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const; 1155 unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const; 1156 1157 /// \brief Return the filename or buffer identifier of the buffer the 1158 /// location is in. 1159 /// 1160 /// Note that this name does not respect \#line directives. Use 1161 /// getPresumedLoc for normal clients. 1162 const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const; 1163 1164 /// \brief Return the file characteristic of the specified source 1165 /// location, indicating whether this is a normal file, a system 1166 /// header, or an "implicit extern C" system header. 1167 /// 1168 /// This state can be modified with flags on GNU linemarker directives like: 1169 /// \code 1170 /// # 4 "foo.h" 3 1171 /// \endcode 1172 /// which changes all source locations in the current file after that to be 1173 /// considered to be from a system header. 1174 SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const; 1175 1176 /// \brief Returns the "presumed" location of a SourceLocation specifies. 1177 /// 1178 /// A "presumed location" can be modified by \#line or GNU line marker 1179 /// directives. This provides a view on the data that a user should see 1180 /// in diagnostics, for example. 1181 /// 1182 /// Note that a presumed location is always given as the expansion point of 1183 /// an expansion location, not at the spelling location. 1184 /// 1185 /// \returns The presumed location of the specified SourceLocation. If the 1186 /// presumed location cannot be calculate (e.g., because \p Loc is invalid 1187 /// or the file containing \p Loc has changed on disk), returns an invalid 1188 /// presumed location. 1189 PresumedLoc getPresumedLoc(SourceLocation Loc) const; 1190 1191 /// \brief Returns true if both SourceLocations correspond to the same file. isFromSameFile(SourceLocation Loc1,SourceLocation Loc2)1192 bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const { 1193 return getFileID(Loc1) == getFileID(Loc2); 1194 } 1195 1196 /// \brief Returns true if the file of provided SourceLocation is the main 1197 /// file. isFromMainFile(SourceLocation Loc)1198 bool isFromMainFile(SourceLocation Loc) const { 1199 return getFileID(Loc) == getMainFileID(); 1200 } 1201 1202 /// \brief Returns if a SourceLocation is in a system header. isInSystemHeader(SourceLocation Loc)1203 bool isInSystemHeader(SourceLocation Loc) const { 1204 return getFileCharacteristic(Loc) != SrcMgr::C_User; 1205 } 1206 1207 /// \brief Returns if a SourceLocation is in an "extern C" system header. isInExternCSystemHeader(SourceLocation Loc)1208 bool isInExternCSystemHeader(SourceLocation Loc) const { 1209 return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem; 1210 } 1211 1212 /// \brief Returns whether \p Loc is expanded from a macro in a system header. isInSystemMacro(SourceLocation loc)1213 bool isInSystemMacro(SourceLocation loc) { 1214 return loc.isMacroID() && isInSystemHeader(getSpellingLoc(loc)); 1215 } 1216 1217 /// \brief The size of the SLocEnty that \p FID represents. 1218 unsigned getFileIDSize(FileID FID) const; 1219 1220 /// \brief Given a specific FileID, returns true if \p Loc is inside that 1221 /// FileID chunk and sets relative offset (offset of \p Loc from beginning 1222 /// of FileID) to \p relativeOffset. 1223 bool isInFileID(SourceLocation Loc, FileID FID, 1224 unsigned *RelativeOffset = 0) const { 1225 unsigned Offs = Loc.getOffset(); 1226 if (isOffsetInFileID(FID, Offs)) { 1227 if (RelativeOffset) 1228 *RelativeOffset = Offs - getSLocEntry(FID).getOffset(); 1229 return true; 1230 } 1231 1232 return false; 1233 } 1234 1235 //===--------------------------------------------------------------------===// 1236 // Line Table Manipulation Routines 1237 //===--------------------------------------------------------------------===// 1238 1239 /// \brief Return the uniqued ID for the specified filename. 1240 /// 1241 unsigned getLineTableFilenameID(StringRef Str); 1242 1243 /// \brief Add a line note to the line table for the FileID and offset 1244 /// specified by Loc. 1245 /// 1246 /// If FilenameID is -1, it is considered to be unspecified. 1247 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID); 1248 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID, 1249 bool IsFileEntry, bool IsFileExit, 1250 bool IsSystemHeader, bool IsExternCHeader); 1251 1252 /// \brief Determine if the source manager has a line table. hasLineTable()1253 bool hasLineTable() const { return LineTable != 0; } 1254 1255 /// \brief Retrieve the stored line table. 1256 LineTableInfo &getLineTable(); 1257 1258 //===--------------------------------------------------------------------===// 1259 // Queries for performance analysis. 1260 //===--------------------------------------------------------------------===// 1261 1262 /// \brief Return the total amount of physical memory allocated by the 1263 /// ContentCache allocator. getContentCacheSize()1264 size_t getContentCacheSize() const { 1265 return ContentCacheAlloc.getTotalMemory(); 1266 } 1267 1268 struct MemoryBufferSizes { 1269 const size_t malloc_bytes; 1270 const size_t mmap_bytes; 1271 MemoryBufferSizesMemoryBufferSizes1272 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes) 1273 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {} 1274 }; 1275 1276 /// \brief Return the amount of memory used by memory buffers, breaking down 1277 /// by heap-backed versus mmap'ed memory. 1278 MemoryBufferSizes getMemoryBufferSizes() const; 1279 1280 /// \brief Return the amount of memory used for various side tables and 1281 /// data structures in the SourceManager. 1282 size_t getDataStructureSizes() const; 1283 1284 //===--------------------------------------------------------------------===// 1285 // Other miscellaneous methods. 1286 //===--------------------------------------------------------------------===// 1287 1288 /// \brief Get the source location for the given file:line:col triplet. 1289 /// 1290 /// If the source file is included multiple times, the source location will 1291 /// be based upon the first inclusion. 1292 SourceLocation translateFileLineCol(const FileEntry *SourceFile, 1293 unsigned Line, unsigned Col) const; 1294 1295 /// \brief Get the FileID for the given file. 1296 /// 1297 /// If the source file is included multiple times, the FileID will be the 1298 /// first inclusion. 1299 FileID translateFile(const FileEntry *SourceFile) const; 1300 1301 /// \brief Get the source location in \p FID for the given line:col. 1302 /// Returns null location if \p FID is not a file SLocEntry. 1303 SourceLocation translateLineCol(FileID FID, 1304 unsigned Line, unsigned Col) const; 1305 1306 /// \brief If \p Loc points inside a function macro argument, the returned 1307 /// location will be the macro location in which the argument was expanded. 1308 /// If a macro argument is used multiple times, the expanded location will 1309 /// be at the first expansion of the argument. 1310 /// e.g. 1311 /// MY_MACRO(foo); 1312 /// ^ 1313 /// Passing a file location pointing at 'foo', will yield a macro location 1314 /// where 'foo' was expanded into. 1315 SourceLocation getMacroArgExpandedLocation(SourceLocation Loc) const; 1316 1317 /// \brief Determines the order of 2 source locations in the translation unit. 1318 /// 1319 /// \returns true if LHS source location comes before RHS, false otherwise. 1320 bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const; 1321 1322 /// \brief Determines the order of 2 source locations in the "source location 1323 /// address space". isBeforeInSLocAddrSpace(SourceLocation LHS,SourceLocation RHS)1324 bool isBeforeInSLocAddrSpace(SourceLocation LHS, SourceLocation RHS) const { 1325 return isBeforeInSLocAddrSpace(LHS, RHS.getOffset()); 1326 } 1327 1328 /// \brief Determines the order of a source location and a source location 1329 /// offset in the "source location address space". 1330 /// 1331 /// Note that we always consider source locations loaded from isBeforeInSLocAddrSpace(SourceLocation LHS,unsigned RHS)1332 bool isBeforeInSLocAddrSpace(SourceLocation LHS, unsigned RHS) const { 1333 unsigned LHSOffset = LHS.getOffset(); 1334 bool LHSLoaded = LHSOffset >= CurrentLoadedOffset; 1335 bool RHSLoaded = RHS >= CurrentLoadedOffset; 1336 if (LHSLoaded == RHSLoaded) 1337 return LHSOffset < RHS; 1338 1339 return LHSLoaded; 1340 } 1341 1342 // Iterators over FileInfos. 1343 typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> 1344 ::const_iterator fileinfo_iterator; fileinfo_begin()1345 fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); } fileinfo_end()1346 fileinfo_iterator fileinfo_end() const { return FileInfos.end(); } hasFileInfo(const FileEntry * File)1347 bool hasFileInfo(const FileEntry *File) const { 1348 return FileInfos.find(File) != FileInfos.end(); 1349 } 1350 1351 /// \brief Print statistics to stderr. 1352 /// 1353 void PrintStats() const; 1354 1355 /// \brief Get the number of local SLocEntries we have. local_sloc_entry_size()1356 unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); } 1357 1358 /// \brief Get a local SLocEntry. This is exposed for indexing. 1359 const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index, 1360 bool *Invalid = 0) const { 1361 assert(Index < LocalSLocEntryTable.size() && "Invalid index"); 1362 return LocalSLocEntryTable[Index]; 1363 } 1364 1365 /// \brief Get the number of loaded SLocEntries we have. loaded_sloc_entry_size()1366 unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();} 1367 1368 /// \brief Get a loaded SLocEntry. This is exposed for indexing. 1369 const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index, 1370 bool *Invalid = 0) const { 1371 assert(Index < LoadedSLocEntryTable.size() && "Invalid index"); 1372 if (SLocEntryLoaded[Index]) 1373 return LoadedSLocEntryTable[Index]; 1374 return loadSLocEntry(Index, Invalid); 1375 } 1376 1377 const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const { 1378 if (FID.ID == 0 || FID.ID == -1) { 1379 if (Invalid) *Invalid = true; 1380 return LocalSLocEntryTable[0]; 1381 } 1382 return getSLocEntryByID(FID.ID); 1383 } 1384 getNextLocalOffset()1385 unsigned getNextLocalOffset() const { return NextLocalOffset; } 1386 setExternalSLocEntrySource(ExternalSLocEntrySource * Source)1387 void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) { 1388 assert(LoadedSLocEntryTable.empty() && 1389 "Invalidating existing loaded entries"); 1390 ExternalSLocEntries = Source; 1391 } 1392 1393 /// \brief Allocate a number of loaded SLocEntries, which will be actually 1394 /// loaded on demand from the external source. 1395 /// 1396 /// NumSLocEntries will be allocated, which occupy a total of TotalSize space 1397 /// in the global source view. The lowest ID and the base offset of the 1398 /// entries will be returned. 1399 std::pair<int, unsigned> 1400 AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize); 1401 1402 /// \brief Returns true if \p Loc came from a PCH/Module. isLoadedSourceLocation(SourceLocation Loc)1403 bool isLoadedSourceLocation(SourceLocation Loc) const { 1404 return Loc.getOffset() >= CurrentLoadedOffset; 1405 } 1406 1407 /// \brief Returns true if \p Loc did not come from a PCH/Module. isLocalSourceLocation(SourceLocation Loc)1408 bool isLocalSourceLocation(SourceLocation Loc) const { 1409 return Loc.getOffset() < NextLocalOffset; 1410 } 1411 1412 /// \brief Returns true if \p FID came from a PCH/Module. isLoadedFileID(FileID FID)1413 bool isLoadedFileID(FileID FID) const { 1414 assert(FID.ID != -1 && "Using FileID sentinel value"); 1415 return FID.ID < 0; 1416 } 1417 1418 /// \brief Returns true if \p FID did not come from a PCH/Module. isLocalFileID(FileID FID)1419 bool isLocalFileID(FileID FID) const { 1420 return !isLoadedFileID(FID); 1421 } 1422 1423 /// Get a presumed location suitable for displaying in a diagnostic message, 1424 /// taking into account macro arguments and expansions. getPresumedLocForDisplay(SourceLocation Loc)1425 PresumedLoc getPresumedLocForDisplay(SourceLocation Loc) const { 1426 // This is a condensed form of the algorithm used by emitCaretDiagnostic to 1427 // walk to the top of the macro call stack. 1428 while (Loc.isMacroID()) { 1429 Loc = skipToMacroArgExpansion(Loc); 1430 Loc = getImmediateMacroCallerLoc(Loc); 1431 } 1432 1433 return getPresumedLoc(Loc); 1434 } 1435 1436 /// Look through spelling locations for a macro argument expansion, and if 1437 /// found skip to it so that we can trace the argument rather than the macros 1438 /// in which that argument is used. If no macro argument expansion is found, 1439 /// don't skip anything and return the starting location. skipToMacroArgExpansion(SourceLocation StartLoc)1440 SourceLocation skipToMacroArgExpansion(SourceLocation StartLoc) const { 1441 for (SourceLocation L = StartLoc; L.isMacroID(); 1442 L = getImmediateSpellingLoc(L)) { 1443 if (isMacroArgExpansion(L)) 1444 return L; 1445 } 1446 // Otherwise just return initial location, there's nothing to skip. 1447 return StartLoc; 1448 } 1449 1450 /// Gets the location of the immediate macro caller, one level up the stack 1451 /// toward the initial macro typed into the source. getImmediateMacroCallerLoc(SourceLocation Loc)1452 SourceLocation getImmediateMacroCallerLoc(SourceLocation Loc) const { 1453 if (!Loc.isMacroID()) return Loc; 1454 1455 // When we have the location of (part of) an expanded parameter, its 1456 // spelling location points to the argument as typed into the macro call, 1457 // and therefore is used to locate the macro caller. 1458 if (isMacroArgExpansion(Loc)) 1459 return getImmediateSpellingLoc(Loc); 1460 1461 // Otherwise, the caller of the macro is located where this macro is 1462 // expanded (while the spelling is part of the macro definition). 1463 return getImmediateExpansionRange(Loc).first; 1464 } 1465 1466 /// Gets the location of the immediate macro callee, one level down the stack 1467 /// toward the leaf macro. getImmediateMacroCalleeLoc(SourceLocation Loc)1468 SourceLocation getImmediateMacroCalleeLoc(SourceLocation Loc) const { 1469 if (!Loc.isMacroID()) return Loc; 1470 1471 // When we have the location of (part of) an expanded parameter, its 1472 // expansion location points to the unexpanded parameter reference within 1473 // the macro definition (or callee). 1474 if (isMacroArgExpansion(Loc)) 1475 return getImmediateExpansionRange(Loc).first; 1476 1477 // Otherwise, the callee of the macro is located where this location was 1478 // spelled inside the macro definition. 1479 return getImmediateSpellingLoc(Loc); 1480 } 1481 1482 private: 1483 const llvm::MemoryBuffer *getFakeBufferForRecovery() const; 1484 const SrcMgr::ContentCache *getFakeContentCacheForRecovery() const; 1485 1486 const SrcMgr::SLocEntry &loadSLocEntry(unsigned Index, bool *Invalid) const; 1487 1488 /// \brief Get the entry with the given unwrapped FileID. getSLocEntryByID(int ID)1489 const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const { 1490 assert(ID != -1 && "Using FileID sentinel value"); 1491 if (ID < 0) 1492 return getLoadedSLocEntryByID(ID); 1493 return getLocalSLocEntry(static_cast<unsigned>(ID)); 1494 } 1495 1496 const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID, 1497 bool *Invalid = 0) const { 1498 return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2), Invalid); 1499 } 1500 1501 /// Implements the common elements of storing an expansion info struct into 1502 /// the SLocEntry table and producing a source location that refers to it. 1503 SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion, 1504 unsigned TokLength, 1505 int LoadedID = 0, 1506 unsigned LoadedOffset = 0); 1507 1508 /// \brief Return true if the specified FileID contains the 1509 /// specified SourceLocation offset. This is a very hot method. isOffsetInFileID(FileID FID,unsigned SLocOffset)1510 inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const { 1511 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID); 1512 // If the entry is after the offset, it can't contain it. 1513 if (SLocOffset < Entry.getOffset()) return false; 1514 1515 // If this is the very last entry then it does. 1516 if (FID.ID == -2) 1517 return true; 1518 1519 // If it is the last local entry, then it does if the location is local. 1520 if (FID.ID+1 == static_cast<int>(LocalSLocEntryTable.size())) 1521 return SLocOffset < NextLocalOffset; 1522 1523 // Otherwise, the entry after it has to not include it. This works for both 1524 // local and loaded entries. 1525 return SLocOffset < getSLocEntryByID(FID.ID+1).getOffset(); 1526 } 1527 1528 /// \brief Create a new fileID for the specified ContentCache and 1529 /// include position. 1530 /// 1531 /// This works regardless of whether the ContentCache corresponds to a 1532 /// file or some other input source. 1533 FileID createFileID(const SrcMgr::ContentCache* File, 1534 SourceLocation IncludePos, 1535 SrcMgr::CharacteristicKind DirCharacter, 1536 int LoadedID, unsigned LoadedOffset); 1537 1538 const SrcMgr::ContentCache * 1539 getOrCreateContentCache(const FileEntry *SourceFile, 1540 bool isSystemFile = false); 1541 1542 /// \brief Create a new ContentCache for the specified memory buffer. 1543 const SrcMgr::ContentCache* 1544 createMemBufferContentCache(const llvm::MemoryBuffer *Buf); 1545 1546 FileID getFileIDSlow(unsigned SLocOffset) const; 1547 FileID getFileIDLocal(unsigned SLocOffset) const; 1548 FileID getFileIDLoaded(unsigned SLocOffset) const; 1549 1550 SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const; 1551 SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const; 1552 SourceLocation getFileLocSlowCase(SourceLocation Loc) const; 1553 1554 std::pair<FileID, unsigned> 1555 getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const; 1556 std::pair<FileID, unsigned> 1557 getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, 1558 unsigned Offset) const; 1559 void computeMacroArgsCache(MacroArgsMap *&MacroArgsCache, FileID FID) const; 1560 1561 friend class ASTReader; 1562 friend class ASTWriter; 1563 }; 1564 1565 /// \brief Comparison function object. 1566 template<typename T> 1567 class BeforeThanCompare; 1568 1569 /// \brief Compare two source locations. 1570 template<> 1571 class BeforeThanCompare<SourceLocation> { 1572 SourceManager &SM; 1573 1574 public: BeforeThanCompare(SourceManager & SM)1575 explicit BeforeThanCompare(SourceManager &SM) : SM(SM) { } 1576 operator()1577 bool operator()(SourceLocation LHS, SourceLocation RHS) const { 1578 return SM.isBeforeInTranslationUnit(LHS, RHS); 1579 } 1580 }; 1581 1582 /// \brief Compare two non-overlapping source ranges. 1583 template<> 1584 class BeforeThanCompare<SourceRange> { 1585 SourceManager &SM; 1586 1587 public: BeforeThanCompare(SourceManager & SM)1588 explicit BeforeThanCompare(SourceManager &SM) : SM(SM) { } 1589 operator()1590 bool operator()(SourceRange LHS, SourceRange RHS) { 1591 return SM.isBeforeInTranslationUnit(LHS.getBegin(), RHS.getBegin()); 1592 } 1593 }; 1594 1595 } // end namespace clang 1596 1597 #endif 1598