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