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