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