SourceManager.h revision 54232ade44d31e98ea83f43ca066128e315dcbda
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 /// \brief True if LQueryFID was created before RQueryFID. This is used 372 /// to compare macro expansion locations. 373 bool IsLQFIDBeforeRQFID; 374 375 /// CommonFID - This is the file found in common between the two #include 376 /// traces. It is the nearest common ancestor of the #include tree. 377 FileID CommonFID; 378 379 /// L/R CommonOffset - This is the offset of the previous query in CommonFID. 380 /// Usually, this represents the location of the #include for QueryFID, but if 381 /// LQueryFID is a parent of RQueryFID (or vise versa) then these can be a 382 /// random token in the parent. 383 unsigned LCommonOffset, RCommonOffset; 384public: 385 386 /// isCacheValid - Return true if the currently cached values match up with 387 /// the specified LHS/RHS query. If not, we can't use the cache. 388 bool isCacheValid(FileID LHS, FileID RHS) const { 389 return LQueryFID == LHS && RQueryFID == RHS; 390 } 391 392 /// getCachedResult - If the cache is valid, compute the result given the 393 /// specified offsets in the LHS/RHS FID's. 394 bool getCachedResult(unsigned LOffset, unsigned ROffset) const { 395 // If one of the query files is the common file, use the offset. Otherwise, 396 // use the #include loc in the common file. 397 if (LQueryFID != CommonFID) LOffset = LCommonOffset; 398 if (RQueryFID != CommonFID) ROffset = RCommonOffset; 399 400 // It is common for multiple macro expansions to be "included" from the same 401 // location (expansion location), in which case use the order of the FileIDs 402 // to determine which came first. 403 if (LOffset == ROffset && LQueryFID != CommonFID && RQueryFID != CommonFID) 404 return IsLQFIDBeforeRQFID; 405 406 return LOffset < ROffset; 407 } 408 409 // Set up a new query. 410 void setQueryFIDs(FileID LHS, FileID RHS, bool isLFIDBeforeRFID) { 411 assert(LHS != RHS); 412 LQueryFID = LHS; 413 RQueryFID = RHS; 414 IsLQFIDBeforeRQFID = isLFIDBeforeRFID; 415 } 416 417 void clear() { 418 LQueryFID = RQueryFID = FileID(); 419 IsLQFIDBeforeRQFID = false; 420 } 421 422 void setCommonLoc(FileID commonFID, unsigned lCommonOffset, 423 unsigned rCommonOffset) { 424 CommonFID = commonFID; 425 LCommonOffset = lCommonOffset; 426 RCommonOffset = rCommonOffset; 427 } 428 429}; 430 431/// \brief This class handles loading and caching of source files into memory. 432/// 433/// This object owns the MemoryBuffer objects for all of the loaded 434/// files and assigns unique FileID's for each unique #include chain. 435/// 436/// The SourceManager can be queried for information about SourceLocation 437/// objects, turning them into either spelling or expansion locations. Spelling 438/// locations represent where the bytes corresponding to a token came from and 439/// expansion locations represent where the location is in the user's view. In 440/// the case of a macro expansion, for example, the spelling location indicates 441/// where the expanded token came from and the expansion location specifies 442/// where it was expanded. 443class SourceManager : public llvm::RefCountedBase<SourceManager> { 444 /// \brief Diagnostic object. 445 Diagnostic &Diag; 446 447 FileManager &FileMgr; 448 449 mutable llvm::BumpPtrAllocator ContentCacheAlloc; 450 451 /// FileInfos - Memoized information about all of the files tracked by this 452 /// SourceManager. This set allows us to merge ContentCache entries based 453 /// on their FileEntry*. All ContentCache objects will thus have unique, 454 /// non-null, FileEntry pointers. 455 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos; 456 457 /// \brief True if the ContentCache for files that are overriden by other 458 /// files, should report the original file name. Defaults to true. 459 bool OverridenFilesKeepOriginalName; 460 461 /// \brief Files that have been overriden with the contents from another file. 462 llvm::DenseMap<const FileEntry *, const FileEntry *> OverriddenFiles; 463 464 /// MemBufferInfos - Information about various memory buffers that we have 465 /// read in. All FileEntry* within the stored ContentCache objects are NULL, 466 /// as they do not refer to a file. 467 std::vector<SrcMgr::ContentCache*> MemBufferInfos; 468 469 /// \brief The table of SLocEntries that are local to this module. 470 /// 471 /// Positive FileIDs are indexes into this table. Entry 0 indicates an invalid 472 /// expansion. 473 std::vector<SrcMgr::SLocEntry> LocalSLocEntryTable; 474 475 /// \brief The table of SLocEntries that are loaded from other modules. 476 /// 477 /// Negative FileIDs are indexes into this table. To get from ID to an index, 478 /// use (-ID - 2). 479 std::vector<SrcMgr::SLocEntry> LoadedSLocEntryTable; 480 481 /// \brief The starting offset of the next local SLocEntry. 482 /// 483 /// This is LocalSLocEntryTable.back().Offset + the size of that entry. 484 unsigned NextLocalOffset; 485 486 /// \brief The starting offset of the latest batch of loaded SLocEntries. 487 /// 488 /// This is LoadedSLocEntryTable.back().Offset, except that that entry might 489 /// not have been loaded, so that value would be unknown. 490 unsigned CurrentLoadedOffset; 491 492 /// \brief The highest possible offset is 2^31-1, so CurrentLoadedOffset 493 /// starts at 2^31. 494 static const unsigned MaxLoadedOffset = 1U << 31U; 495 496 /// \brief A bitmap that indicates whether the entries of LoadedSLocEntryTable 497 /// have already been loaded from the external source. 498 /// 499 /// Same indexing as LoadedSLocEntryTable. 500 std::vector<bool> SLocEntryLoaded; 501 502 /// \brief An external source for source location entries. 503 ExternalSLocEntrySource *ExternalSLocEntries; 504 505 /// LastFileIDLookup - This is a one-entry cache to speed up getFileID. 506 /// LastFileIDLookup records the last FileID looked up or created, because it 507 /// is very common to look up many tokens from the same file. 508 mutable FileID LastFileIDLookup; 509 510 /// LineTable - This holds information for #line directives. It is referenced 511 /// by indices from SLocEntryTable. 512 LineTableInfo *LineTable; 513 514 /// LastLineNo - These ivars serve as a cache used in the getLineNumber 515 /// method which is used to speedup getLineNumber calls to nearby locations. 516 mutable FileID LastLineNoFileIDQuery; 517 mutable SrcMgr::ContentCache *LastLineNoContentCache; 518 mutable unsigned LastLineNoFilePos; 519 mutable unsigned LastLineNoResult; 520 521 /// MainFileID - The file ID for the main source file of the translation unit. 522 FileID MainFileID; 523 524 // Statistics for -print-stats. 525 mutable unsigned NumLinearScans, NumBinaryProbes; 526 527 // Cache results for the isBeforeInTranslationUnit method. 528 mutable IsBeforeInTranslationUnitCache IsBeforeInTUCache; 529 530 // Cache for the "fake" buffer used for error-recovery purposes. 531 mutable llvm::MemoryBuffer *FakeBufferForRecovery; 532 533 // SourceManager doesn't support copy construction. 534 explicit SourceManager(const SourceManager&); 535 void operator=(const SourceManager&); 536public: 537 SourceManager(Diagnostic &Diag, FileManager &FileMgr); 538 ~SourceManager(); 539 540 void clearIDTables(); 541 542 Diagnostic &getDiagnostics() const { return Diag; } 543 544 FileManager &getFileManager() const { return FileMgr; } 545 546 /// \brief Set true if the SourceManager should report the original file name 547 /// for contents of files that were overriden by other files.Defaults to true. 548 void setOverridenFilesKeepOriginalName(bool value) { 549 OverridenFilesKeepOriginalName = value; 550 } 551 552 /// createMainFileIDForMembuffer - Create the FileID for a memory buffer 553 /// that will represent the FileID for the main source. One example 554 /// of when this would be used is when the main source is read from STDIN. 555 FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) { 556 assert(MainFileID.isInvalid() && "MainFileID already set!"); 557 MainFileID = createFileIDForMemBuffer(Buffer); 558 return MainFileID; 559 } 560 561 //===--------------------------------------------------------------------===// 562 // MainFileID creation and querying methods. 563 //===--------------------------------------------------------------------===// 564 565 /// getMainFileID - Returns the FileID of the main source file. 566 FileID getMainFileID() const { return MainFileID; } 567 568 /// createMainFileID - Create the FileID for the main source file. 569 FileID createMainFileID(const FileEntry *SourceFile) { 570 assert(MainFileID.isInvalid() && "MainFileID already set!"); 571 MainFileID = createFileID(SourceFile, SourceLocation(), SrcMgr::C_User); 572 return MainFileID; 573 } 574 575 /// \brief Set the file ID for the precompiled preamble, which is also the 576 /// main file. 577 void SetPreambleFileID(FileID Preamble) { 578 assert(MainFileID.isInvalid() && "MainFileID already set!"); 579 MainFileID = Preamble; 580 } 581 582 //===--------------------------------------------------------------------===// 583 // Methods to create new FileID's and macro expansions. 584 //===--------------------------------------------------------------------===// 585 586 /// createFileID - Create a new FileID that represents the specified file 587 /// being #included from the specified IncludePosition. This translates NULL 588 /// into standard input. 589 FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, 590 SrcMgr::CharacteristicKind FileCharacter, 591 int LoadedID = 0, unsigned LoadedOffset = 0) { 592 const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile); 593 assert(IR && "getOrCreateContentCache() cannot return NULL"); 594 return createFileID(IR, IncludePos, FileCharacter, LoadedID, LoadedOffset); 595 } 596 597 /// createFileIDForMemBuffer - Create a new FileID that represents the 598 /// specified memory buffer. This does no caching of the buffer and takes 599 /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once. 600 FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer, 601 int LoadedID = 0, unsigned LoadedOffset = 0) { 602 return createFileID(createMemBufferContentCache(Buffer), SourceLocation(), 603 SrcMgr::C_User, LoadedID, LoadedOffset); 604 } 605 606 /// createMacroArgExpansionLoc - Return a new SourceLocation that encodes the 607 /// fact that a token from SpellingLoc should actually be referenced from 608 /// ExpansionLoc, and that it represents the expansion of a macro argument 609 /// into the function-like macro body. 610 SourceLocation createMacroArgExpansionLoc(SourceLocation Loc, 611 SourceLocation ExpansionLoc, 612 unsigned TokLength); 613 614 /// createExpansionLoc - Return a new SourceLocation that encodes the fact 615 /// that a token from SpellingLoc should actually be referenced from 616 /// ExpansionLoc. 617 SourceLocation createExpansionLoc(SourceLocation Loc, 618 SourceLocation ExpansionLocStart, 619 SourceLocation ExpansionLocEnd, 620 unsigned TokLength, 621 int LoadedID = 0, 622 unsigned LoadedOffset = 0); 623 624 /// \brief Retrieve the memory buffer associated with the given file. 625 /// 626 /// \param Invalid If non-NULL, will be set \c true if an error 627 /// occurs while retrieving the memory buffer. 628 const llvm::MemoryBuffer *getMemoryBufferForFile(const FileEntry *File, 629 bool *Invalid = 0); 630 631 /// \brief Override the contents of the given source file by providing an 632 /// already-allocated buffer. 633 /// 634 /// \param SourceFile the source file whose contents will be overriden. 635 /// 636 /// \param Buffer the memory buffer whose contents will be used as the 637 /// data in the given source file. 638 /// 639 /// \param DoNotFree If true, then the buffer will not be freed when the 640 /// source manager is destroyed. 641 void overrideFileContents(const FileEntry *SourceFile, 642 const llvm::MemoryBuffer *Buffer, 643 bool DoNotFree = false); 644 645 /// \brief Override the the given source file with another one. 646 /// 647 /// \param SourceFile the source file which will be overriden. 648 /// 649 /// \param NewFile the file whose contents will be used as the 650 /// data instead of the contents of the given source file. 651 void overrideFileContents(const FileEntry *SourceFile, 652 const FileEntry *NewFile); 653 654 //===--------------------------------------------------------------------===// 655 // FileID manipulation methods. 656 //===--------------------------------------------------------------------===// 657 658 /// getBuffer - Return the buffer for the specified FileID. If there is an 659 /// error opening this buffer the first time, this manufactures a temporary 660 /// buffer and returns a non-empty error string. 661 const llvm::MemoryBuffer *getBuffer(FileID FID, SourceLocation Loc, 662 bool *Invalid = 0) const { 663 bool MyInvalid = false; 664 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); 665 if (MyInvalid || !Entry.isFile()) { 666 if (Invalid) 667 *Invalid = true; 668 669 return getFakeBufferForRecovery(); 670 } 671 672 return Entry.getFile().getContentCache()->getBuffer(Diag, *this, Loc, 673 Invalid); 674 } 675 676 const llvm::MemoryBuffer *getBuffer(FileID FID, bool *Invalid = 0) const { 677 bool MyInvalid = false; 678 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); 679 if (MyInvalid || !Entry.isFile()) { 680 if (Invalid) 681 *Invalid = true; 682 683 return getFakeBufferForRecovery(); 684 } 685 686 return Entry.getFile().getContentCache()->getBuffer(Diag, *this, 687 SourceLocation(), 688 Invalid); 689 } 690 691 /// getFileEntryForID - Returns the FileEntry record for the provided FileID. 692 const FileEntry *getFileEntryForID(FileID FID) const { 693 bool MyInvalid = false; 694 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); 695 if (MyInvalid || !Entry.isFile()) 696 return 0; 697 698 return Entry.getFile().getContentCache()->OrigEntry; 699 } 700 701 /// Returns the FileEntry record for the provided SLocEntry. 702 const FileEntry *getFileEntryForSLocEntry(const SrcMgr::SLocEntry &sloc) const 703 { 704 return sloc.getFile().getContentCache()->OrigEntry; 705 } 706 707 /// getBufferData - Return a StringRef to the source buffer data for the 708 /// specified FileID. 709 /// 710 /// \param FID The file ID whose contents will be returned. 711 /// \param Invalid If non-NULL, will be set true if an error occurred. 712 StringRef getBufferData(FileID FID, bool *Invalid = 0) const; 713 714 715 //===--------------------------------------------------------------------===// 716 // SourceLocation manipulation methods. 717 //===--------------------------------------------------------------------===// 718 719 /// getFileID - Return the FileID for a SourceLocation. This is a very 720 /// hot method that is used for all SourceManager queries that start with a 721 /// SourceLocation object. It is responsible for finding the entry in 722 /// SLocEntryTable which contains the specified location. 723 /// 724 FileID getFileID(SourceLocation SpellingLoc) const { 725 unsigned SLocOffset = SpellingLoc.getOffset(); 726 727 // If our one-entry cache covers this offset, just return it. 728 if (isOffsetInFileID(LastFileIDLookup, SLocOffset)) 729 return LastFileIDLookup; 730 731 return getFileIDSlow(SLocOffset); 732 } 733 734 /// getLocForStartOfFile - Return the source location corresponding to the 735 /// first byte of the specified file. 736 SourceLocation getLocForStartOfFile(FileID FID) const { 737 bool Invalid = false; 738 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 739 if (Invalid || !Entry.isFile()) 740 return SourceLocation(); 741 742 unsigned FileOffset = Entry.getOffset(); 743 return SourceLocation::getFileLoc(FileOffset); 744 } 745 746 /// getExpansionLoc - Given a SourceLocation object, return the expansion 747 /// location referenced by the ID. 748 SourceLocation getExpansionLoc(SourceLocation Loc) const { 749 // Handle the non-mapped case inline, defer to out of line code to handle 750 // expansions. 751 if (Loc.isFileID()) return Loc; 752 return getExpansionLocSlowCase(Loc); 753 } 754 755 /// getImmediateExpansionRange - Loc is required to be an expansion location. 756 /// Return the start/end of the expansion information. 757 std::pair<SourceLocation,SourceLocation> 758 getImmediateExpansionRange(SourceLocation Loc) const; 759 760 /// getExpansionRange - Given a SourceLocation object, return the range of 761 /// tokens covered by the expansion the ultimate file. 762 std::pair<SourceLocation,SourceLocation> 763 getExpansionRange(SourceLocation Loc) const; 764 765 766 /// getSpellingLoc - Given a SourceLocation object, return the spelling 767 /// location referenced by the ID. This is the place where the characters 768 /// that make up the lexed token can be found. 769 SourceLocation getSpellingLoc(SourceLocation Loc) const { 770 // Handle the non-mapped case inline, defer to out of line code to handle 771 // expansions. 772 if (Loc.isFileID()) return Loc; 773 return getSpellingLocSlowCase(Loc); 774 } 775 776 /// getImmediateSpellingLoc - Given a SourceLocation object, return the 777 /// spelling location referenced by the ID. This is the first level down 778 /// towards the place where the characters that make up the lexed token can be 779 /// found. This should not generally be used by clients. 780 SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const; 781 782 /// getDecomposedLoc - Decompose the specified location into a raw FileID + 783 /// Offset pair. The first element is the FileID, the second is the 784 /// offset from the start of the buffer of the location. 785 std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const { 786 FileID FID = getFileID(Loc); 787 return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset()); 788 } 789 790 /// getDecomposedExpansionLoc - Decompose the specified location into a raw 791 /// FileID + Offset pair. If the location is an expansion record, walk 792 /// through it until we find the final location expanded. 793 std::pair<FileID, unsigned> 794 getDecomposedExpansionLoc(SourceLocation Loc) const { 795 FileID FID = getFileID(Loc); 796 const SrcMgr::SLocEntry *E = &getSLocEntry(FID); 797 798 unsigned Offset = Loc.getOffset()-E->getOffset(); 799 if (Loc.isFileID()) 800 return std::make_pair(FID, Offset); 801 802 return getDecomposedExpansionLocSlowCase(E); 803 } 804 805 /// getDecomposedSpellingLoc - Decompose the specified location into a raw 806 /// FileID + Offset pair. If the location is an expansion record, walk 807 /// through it until we find its spelling record. 808 std::pair<FileID, unsigned> 809 getDecomposedSpellingLoc(SourceLocation Loc) const { 810 FileID FID = getFileID(Loc); 811 const SrcMgr::SLocEntry *E = &getSLocEntry(FID); 812 813 unsigned Offset = Loc.getOffset()-E->getOffset(); 814 if (Loc.isFileID()) 815 return std::make_pair(FID, Offset); 816 return getDecomposedSpellingLocSlowCase(E, Offset); 817 } 818 819 /// getFileOffset - This method returns the offset from the start 820 /// of the file that the specified SourceLocation represents. This is not very 821 /// meaningful for a macro ID. 822 unsigned getFileOffset(SourceLocation SpellingLoc) const { 823 return getDecomposedLoc(SpellingLoc).second; 824 } 825 826 /// isMacroArgExpansion - This method tests whether the given source location 827 /// represents a macro argument's expansion into the function-like macro 828 /// definition. Such source locations only appear inside of the expansion 829 /// locations representing where a particular function-like macro was 830 /// expanded. 831 bool isMacroArgExpansion(SourceLocation Loc) const; 832 833 //===--------------------------------------------------------------------===// 834 // Queries about the code at a SourceLocation. 835 //===--------------------------------------------------------------------===// 836 837 /// getCharacterData - Return a pointer to the start of the specified location 838 /// in the appropriate spelling MemoryBuffer. 839 /// 840 /// \param Invalid If non-NULL, will be set \c true if an error occurs. 841 const char *getCharacterData(SourceLocation SL, bool *Invalid = 0) const; 842 843 /// getColumnNumber - Return the column # for the specified file position. 844 /// This is significantly cheaper to compute than the line number. This 845 /// returns zero if the column number isn't known. This may only be called 846 /// on a file sloc, so you must choose a spelling or expansion location 847 /// before calling this method. 848 unsigned getColumnNumber(FileID FID, unsigned FilePos, 849 bool *Invalid = 0) const; 850 unsigned getSpellingColumnNumber(SourceLocation Loc, bool *Invalid = 0) const; 851 unsigned getExpansionColumnNumber(SourceLocation Loc, 852 bool *Invalid = 0) const; 853 unsigned getPresumedColumnNumber(SourceLocation Loc, bool *Invalid = 0) const; 854 855 856 /// getLineNumber - Given a SourceLocation, return the spelling line number 857 /// for the position indicated. This requires building and caching a table of 858 /// line offsets for the MemoryBuffer, so this is not cheap: use only when 859 /// about to emit a diagnostic. 860 unsigned getLineNumber(FileID FID, unsigned FilePos, bool *Invalid = 0) const; 861 unsigned getSpellingLineNumber(SourceLocation Loc, bool *Invalid = 0) const; 862 unsigned getExpansionLineNumber(SourceLocation Loc, bool *Invalid = 0) const; 863 unsigned getPresumedLineNumber(SourceLocation Loc, bool *Invalid = 0) const; 864 865 /// Return the filename or buffer identifier of the buffer the location is in. 866 /// Note that this name does not respect #line directives. Use getPresumedLoc 867 /// for normal clients. 868 const char *getBufferName(SourceLocation Loc, bool *Invalid = 0) const; 869 870 /// getFileCharacteristic - return the file characteristic of the specified 871 /// source location, indicating whether this is a normal file, a system 872 /// header, or an "implicit extern C" system header. 873 /// 874 /// This state can be modified with flags on GNU linemarker directives like: 875 /// # 4 "foo.h" 3 876 /// which changes all source locations in the current file after that to be 877 /// considered to be from a system header. 878 SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const; 879 880 /// getPresumedLoc - This method returns the "presumed" location of a 881 /// SourceLocation specifies. A "presumed location" can be modified by #line 882 /// or GNU line marker directives. This provides a view on the data that a 883 /// user should see in diagnostics, for example. 884 /// 885 /// Note that a presumed location is always given as the expansion point of 886 /// an expansion location, not at the spelling location. 887 /// 888 /// \returns The presumed location of the specified SourceLocation. If the 889 /// presumed location cannot be calculate (e.g., because \p Loc is invalid 890 /// or the file containing \p Loc has changed on disk), returns an invalid 891 /// presumed location. 892 PresumedLoc getPresumedLoc(SourceLocation Loc) const; 893 894 /// isFromSameFile - Returns true if both SourceLocations correspond to 895 /// the same file. 896 bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const { 897 return getFileID(Loc1) == getFileID(Loc2); 898 } 899 900 /// isFromMainFile - Returns true if the file of provided SourceLocation is 901 /// the main file. 902 bool isFromMainFile(SourceLocation Loc) const { 903 return getFileID(Loc) == getMainFileID(); 904 } 905 906 /// isInSystemHeader - Returns if a SourceLocation is in a system header. 907 bool isInSystemHeader(SourceLocation Loc) const { 908 return getFileCharacteristic(Loc) != SrcMgr::C_User; 909 } 910 911 /// isInExternCSystemHeader - Returns if a SourceLocation is in an "extern C" 912 /// system header. 913 bool isInExternCSystemHeader(SourceLocation Loc) const { 914 return getFileCharacteristic(Loc) == SrcMgr::C_ExternCSystem; 915 } 916 917 /// \brief The size of the SLocEnty that \arg FID represents. 918 unsigned getFileIDSize(FileID FID) const { 919 bool Invalid = false; 920 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 921 if (Invalid) 922 return 0; 923 924 int ID = FID.ID; 925 unsigned NextOffset; 926 if ((ID > 0 && unsigned(ID+1) == local_sloc_entry_size())) 927 NextOffset = getNextLocalOffset(); 928 else if (ID+1 == -1) 929 NextOffset = MaxLoadedOffset; 930 else 931 NextOffset = getSLocEntry(FileID::get(ID+1)).getOffset(); 932 933 return NextOffset - Entry.getOffset() - 1; 934 } 935 936 /// \brief Given a specific chunk of a FileID (FileID with offset+length), 937 /// returns true if \arg Loc is inside that chunk and sets relative offset 938 /// (offset of \arg Loc from beginning of chunk) to \arg relativeOffset. 939 bool isInFileID(SourceLocation Loc, 940 FileID FID, unsigned offset, unsigned length, 941 unsigned *relativeOffset = 0) const { 942 assert(!FID.isInvalid()); 943 if (Loc.isInvalid()) 944 return false; 945 946 unsigned FIDOffs = getSLocEntry(FID).getOffset(); 947 unsigned start = FIDOffs + offset; 948 unsigned end = start + length; 949 950 // Make sure offset/length describe a chunk inside the given FileID. 951 assert(start < FIDOffs + getFileIDSize(FID)); 952 assert(end <= FIDOffs + getFileIDSize(FID)); 953 954 if (Loc.getOffset() >= start && Loc.getOffset() < end) { 955 if (relativeOffset) 956 *relativeOffset = Loc.getOffset() - start; 957 return true; 958 } 959 960 return false; 961 } 962 963 //===--------------------------------------------------------------------===// 964 // Line Table Manipulation Routines 965 //===--------------------------------------------------------------------===// 966 967 /// getLineTableFilenameID - Return the uniqued ID for the specified filename. 968 /// 969 unsigned getLineTableFilenameID(StringRef Str); 970 971 /// AddLineNote - Add a line note to the line table for the FileID and offset 972 /// specified by Loc. If FilenameID is -1, it is considered to be 973 /// unspecified. 974 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID); 975 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID, 976 bool IsFileEntry, bool IsFileExit, 977 bool IsSystemHeader, bool IsExternCHeader); 978 979 /// \brief Determine if the source manager has a line table. 980 bool hasLineTable() const { return LineTable != 0; } 981 982 /// \brief Retrieve the stored line table. 983 LineTableInfo &getLineTable(); 984 985 //===--------------------------------------------------------------------===// 986 // Queries for performance analysis. 987 //===--------------------------------------------------------------------===// 988 989 /// Return the total amount of physical memory allocated by the 990 /// ContentCache allocator. 991 size_t getContentCacheSize() const { 992 return ContentCacheAlloc.getTotalMemory(); 993 } 994 995 struct MemoryBufferSizes { 996 const size_t malloc_bytes; 997 const size_t mmap_bytes; 998 999 MemoryBufferSizes(size_t malloc_bytes, size_t mmap_bytes) 1000 : malloc_bytes(malloc_bytes), mmap_bytes(mmap_bytes) {} 1001 }; 1002 1003 /// Return the amount of memory used by memory buffers, breaking down 1004 /// by heap-backed versus mmap'ed memory. 1005 MemoryBufferSizes getMemoryBufferSizes() const; 1006 1007 // Return the amount of memory used for various side tables and 1008 // data structures in the SourceManager. 1009 size_t getDataStructureSizes() const; 1010 1011 //===--------------------------------------------------------------------===// 1012 // Other miscellaneous methods. 1013 //===--------------------------------------------------------------------===// 1014 1015 /// \brief Get the source location for the given file:line:col triplet. 1016 /// 1017 /// If the source file is included multiple times, the source location will 1018 /// be based upon the first inclusion. 1019 /// 1020 /// If the location points inside a function macro argument, the returned 1021 /// location will be the macro location in which the argument was expanded. 1022 /// \sa getMacroArgExpandedLocation 1023 SourceLocation getLocation(const FileEntry *SourceFile, 1024 unsigned Line, unsigned Col) { 1025 SourceLocation Loc = translateFileLineCol(SourceFile, Line, Col); 1026 return getMacroArgExpandedLocation(Loc); 1027 } 1028 1029 /// \brief Get the source location for the given file:line:col triplet. 1030 /// 1031 /// If the source file is included multiple times, the source location will 1032 /// be based upon the first inclusion. 1033 SourceLocation translateFileLineCol(const FileEntry *SourceFile, 1034 unsigned Line, unsigned Col); 1035 1036 /// \brief If \arg Loc points inside a function macro argument, the returned 1037 /// location will be the macro location in which the argument was expanded. 1038 /// If a macro argument is used multiple times, the expanded location will 1039 /// be at the first expansion of the argument. 1040 /// e.g. 1041 /// MY_MACRO(foo); 1042 /// ^ 1043 /// Passing a file location pointing at 'foo', will yield a macro location 1044 /// where 'foo' was expanded into. 1045 SourceLocation getMacroArgExpandedLocation(SourceLocation Loc); 1046 1047 /// \brief Determines the order of 2 source locations in the translation unit. 1048 /// 1049 /// \returns true if LHS source location comes before RHS, false otherwise. 1050 bool isBeforeInTranslationUnit(SourceLocation LHS, SourceLocation RHS) const; 1051 1052 /// \brief Determines the order of 2 source locations in the "source location 1053 /// address space". 1054 bool isBeforeInSourceLocationOffset(SourceLocation LHS, 1055 SourceLocation RHS) const { 1056 return isBeforeInSourceLocationOffset(LHS, RHS.getOffset()); 1057 } 1058 1059 /// \brief Determines the order of a source location and a source location 1060 /// offset in the "source location address space". 1061 /// 1062 /// Note that we always consider source locations loaded from 1063 bool isBeforeInSourceLocationOffset(SourceLocation LHS, unsigned RHS) const { 1064 unsigned LHSOffset = LHS.getOffset(); 1065 bool LHSLoaded = LHSOffset >= CurrentLoadedOffset; 1066 bool RHSLoaded = RHS >= CurrentLoadedOffset; 1067 if (LHSLoaded == RHSLoaded) 1068 return LHS.getOffset() < RHS; 1069 1070 return LHSLoaded; 1071 } 1072 1073 // Iterators over FileInfos. 1074 typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> 1075 ::const_iterator fileinfo_iterator; 1076 fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); } 1077 fileinfo_iterator fileinfo_end() const { return FileInfos.end(); } 1078 bool hasFileInfo(const FileEntry *File) const { 1079 return FileInfos.find(File) != FileInfos.end(); 1080 } 1081 1082 /// PrintStats - Print statistics to stderr. 1083 /// 1084 void PrintStats() const; 1085 1086 /// \brief Get the number of local SLocEntries we have. 1087 unsigned local_sloc_entry_size() const { return LocalSLocEntryTable.size(); } 1088 1089 /// \brief Get a local SLocEntry. This is exposed for indexing. 1090 const SrcMgr::SLocEntry &getLocalSLocEntry(unsigned Index, 1091 bool *Invalid = 0) const { 1092 assert(Index < LocalSLocEntryTable.size() && "Invalid index"); 1093 return LocalSLocEntryTable[Index]; 1094 } 1095 1096 /// \brief Get the number of loaded SLocEntries we have. 1097 unsigned loaded_sloc_entry_size() const { return LoadedSLocEntryTable.size();} 1098 1099 /// \brief Get a loaded SLocEntry. This is exposed for indexing. 1100 const SrcMgr::SLocEntry &getLoadedSLocEntry(unsigned Index, bool *Invalid=0) const { 1101 assert(Index < LoadedSLocEntryTable.size() && "Invalid index"); 1102 if (!SLocEntryLoaded[Index]) 1103 ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2)); 1104 return LoadedSLocEntryTable[Index]; 1105 } 1106 1107 const SrcMgr::SLocEntry &getSLocEntry(FileID FID, bool *Invalid = 0) const { 1108 return getSLocEntryByID(FID.ID); 1109 } 1110 1111 unsigned getNextLocalOffset() const { return NextLocalOffset; } 1112 1113 void setExternalSLocEntrySource(ExternalSLocEntrySource *Source) { 1114 assert(LoadedSLocEntryTable.empty() && 1115 "Invalidating existing loaded entries"); 1116 ExternalSLocEntries = Source; 1117 } 1118 1119 /// \brief Allocate a number of loaded SLocEntries, which will be actually 1120 /// loaded on demand from the external source. 1121 /// 1122 /// NumSLocEntries will be allocated, which occupy a total of TotalSize space 1123 /// in the global source view. The lowest ID and the base offset of the 1124 /// entries will be returned. 1125 std::pair<int, unsigned> 1126 AllocateLoadedSLocEntries(unsigned NumSLocEntries, unsigned TotalSize); 1127 1128private: 1129 const llvm::MemoryBuffer *getFakeBufferForRecovery() const; 1130 1131 /// \brief Get the entry with the given unwrapped FileID. 1132 const SrcMgr::SLocEntry &getSLocEntryByID(int ID) const { 1133 assert(ID != -1 && "Using FileID sentinel value"); 1134 if (ID < 0) 1135 return getLoadedSLocEntryByID(ID); 1136 return getLocalSLocEntry(static_cast<unsigned>(ID)); 1137 } 1138 1139 const SrcMgr::SLocEntry &getLoadedSLocEntryByID(int ID) const { 1140 return getLoadedSLocEntry(static_cast<unsigned>(-ID - 2)); 1141 } 1142 1143 /// createExpansionLoc - Implements the common elements of storing an 1144 /// expansion info struct into the SLocEntry table and producing a source 1145 /// location that refers to it. 1146 SourceLocation createExpansionLocImpl(const SrcMgr::ExpansionInfo &Expansion, 1147 unsigned TokLength, 1148 int LoadedID = 0, 1149 unsigned LoadedOffset = 0); 1150 1151 /// isOffsetInFileID - Return true if the specified FileID contains the 1152 /// specified SourceLocation offset. This is a very hot method. 1153 inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const { 1154 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID); 1155 // If the entry is after the offset, it can't contain it. 1156 if (SLocOffset < Entry.getOffset()) return false; 1157 1158 // If this is the very last entry then it does. 1159 if (FID.ID == -2) 1160 return true; 1161 1162 // If it is the last local entry, then it does if the location is local. 1163 if (static_cast<unsigned>(FID.ID+1) == LocalSLocEntryTable.size()) { 1164 return SLocOffset < NextLocalOffset; 1165 } 1166 1167 // Otherwise, the entry after it has to not include it. This works for both 1168 // local and loaded entries. 1169 return SLocOffset < getSLocEntry(FileID::get(FID.ID+1)).getOffset(); 1170 } 1171 1172 /// createFileID - Create a new fileID for the specified ContentCache and 1173 /// include position. This works regardless of whether the ContentCache 1174 /// corresponds to a file or some other input source. 1175 FileID createFileID(const SrcMgr::ContentCache* File, 1176 SourceLocation IncludePos, 1177 SrcMgr::CharacteristicKind DirCharacter, 1178 int LoadedID, unsigned LoadedOffset); 1179 1180 const SrcMgr::ContentCache * 1181 getOrCreateContentCache(const FileEntry *SourceFile); 1182 1183 /// createMemBufferContentCache - Create a new ContentCache for the specified 1184 /// memory buffer. 1185 const SrcMgr::ContentCache* 1186 createMemBufferContentCache(const llvm::MemoryBuffer *Buf); 1187 1188 FileID getFileIDSlow(unsigned SLocOffset) const; 1189 FileID getFileIDLocal(unsigned SLocOffset) const; 1190 FileID getFileIDLoaded(unsigned SLocOffset) const; 1191 1192 SourceLocation getExpansionLocSlowCase(SourceLocation Loc) const; 1193 SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const; 1194 1195 std::pair<FileID, unsigned> 1196 getDecomposedExpansionLocSlowCase(const SrcMgr::SLocEntry *E) const; 1197 std::pair<FileID, unsigned> 1198 getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, 1199 unsigned Offset) const; 1200}; 1201 1202 1203} // end namespace clang 1204 1205#endif 1206