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