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