SourceManager.h revision 370187c8a3e96517c943329f2511737a04b85450
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/Support/DataTypes.h" 20#include "llvm/ADT/DenseMap.h" 21#include <vector> 22#include <cassert> 23 24namespace llvm { 25class MemoryBuffer; 26} 27 28namespace clang { 29 30class SourceManager; 31class FileManager; 32class FileEntry; 33class IdentifierTokenInfo; 34class LineTableInfo; 35 36/// SrcMgr - Public enums and private classes that are part of the 37/// SourceManager implementation. 38/// 39namespace SrcMgr { 40 /// CharacteristicKind - This is used to represent whether a file or directory 41 /// holds normal user code, system code, or system code which is implicitly 42 /// 'extern "C"' in C++ mode. Entire directories can be tagged with this 43 /// (this is maintained by DirectoryLookup and friends) as can specific 44 /// FileIDInfos when a #pragma system_header is seen or various other cases. 45 /// 46 enum CharacteristicKind { 47 C_User, C_System, C_ExternCSystem 48 }; 49 50 /// ContentCache - Once instance of this struct is kept for every file 51 /// loaded or used. This object owns the MemoryBuffer object. 52 class ContentCache { 53 /// Buffer - The actual buffer containing the characters from the input 54 /// file. This is owned by the ContentCache object. 55 mutable const llvm::MemoryBuffer *Buffer; 56 57 public: 58 /// Reference to the file entry. This reference does not own 59 /// the FileEntry object. It is possible for this to be NULL if 60 /// the ContentCache encapsulates an imaginary text buffer. 61 const FileEntry *Entry; 62 63 /// SourceLineCache - A bump pointer allocated array of offsets for each 64 /// source line. This is lazily computed. This is owned by the 65 /// SourceManager BumpPointerAllocator object. 66 unsigned *SourceLineCache; 67 68 /// NumLines - The number of lines in this ContentCache. This is only valid 69 /// if SourceLineCache is non-null. 70 unsigned NumLines; 71 72 /// getBuffer - Returns the memory buffer for the associated content. 73 const llvm::MemoryBuffer *getBuffer() const; 74 75 /// getSize - Returns the size of the content encapsulated by this 76 /// ContentCache. This can be the size of the source file or the size of an 77 /// arbitrary scratch buffer. If the ContentCache encapsulates a source 78 /// file this size is retrieved from the file's FileEntry. 79 unsigned getSize() const; 80 81 /// getSizeBytesMapped - Returns the number of bytes actually mapped for 82 /// this ContentCache. This can be 0 if the MemBuffer was not actually 83 /// instantiated. 84 unsigned getSizeBytesMapped() const; 85 86 void setBuffer(const llvm::MemoryBuffer *B) { 87 assert(!Buffer && "MemoryBuffer already set."); 88 Buffer = B; 89 } 90 91 ContentCache(const FileEntry *Ent = 0) 92 : Buffer(0), Entry(Ent), SourceLineCache(0), NumLines(0) {} 93 94 ~ContentCache(); 95 96 /// The copy ctor does not allow copies where source object has either 97 /// a non-NULL Buffer or SourceLineCache. Ownership of allocated memory 98 /// is not transfered, so this is a logical error. 99 ContentCache(const ContentCache &RHS) : Buffer(0), SourceLineCache(0) { 100 Entry = RHS.Entry; 101 102 assert (RHS.Buffer == 0 && RHS.SourceLineCache == 0 103 && "Passed ContentCache object cannot own a buffer."); 104 105 NumLines = RHS.NumLines; 106 } 107 108 private: 109 // Disable assignments. 110 ContentCache &operator=(const ContentCache& RHS); 111 }; 112 113 /// FileInfo - Information about a FileID, basically just the logical file 114 /// that it represents and include stack information. 115 /// 116 /// Each FileInfo has include stack information, indicating where it came 117 /// from. This information encodes the #include chain that a token was 118 /// instantiated from. The main include file has an invalid IncludeLoc. 119 /// 120 /// FileInfos contain a "ContentCache *", with the contents of the file. 121 /// 122 class FileInfo { 123 /// IncludeLoc - The location of the #include that brought in this file. 124 /// This is an invalid SLOC for the main file (top of the #include chain). 125 unsigned IncludeLoc; // Really a SourceLocation 126 127 /// Data - This contains the ContentCache* and the bits indicating the 128 /// characteristic of the file and whether it has #line info, all bitmangled 129 /// together. 130 uintptr_t Data; 131 public: 132 /// get - Return a FileInfo object. 133 static FileInfo get(SourceLocation IL, const ContentCache *Con, 134 CharacteristicKind FileCharacter) { 135 FileInfo X; 136 X.IncludeLoc = IL.getRawEncoding(); 137 X.Data = (uintptr_t)Con; 138 assert((X.Data & 7) == 0 &&"ContentCache pointer insufficiently aligned"); 139 assert((unsigned)FileCharacter < 4 && "invalid file character"); 140 X.Data |= (unsigned)FileCharacter; 141 return X; 142 } 143 144 SourceLocation getIncludeLoc() const { 145 return SourceLocation::getFromRawEncoding(IncludeLoc); 146 } 147 const ContentCache* getContentCache() const { 148 return reinterpret_cast<const ContentCache*>(Data & ~7UL); 149 } 150 151 /// getCharacteristic - Return whether this is a system header or not. 152 CharacteristicKind getFileCharacteristic() const { 153 return (CharacteristicKind)(Data & 3); 154 } 155 156 /// hasLineDirectives - Return true if this FileID has #line directives in 157 /// it. 158 bool hasLineDirectives() const { return (Data & 4) != 0; } 159 160 /// setHasLineDirectives - Set the flag that indicates that this FileID has 161 /// line table entries associated with it. 162 void setHasLineDirectives() { 163 Data |= 4; 164 } 165 }; 166 167 /// InstantiationInfo - Each InstantiationInfo encodes the Instantiation 168 /// location - where the token was ultimately instantiated, and the 169 /// SpellingLoc - where the actual character data for the token came from. 170 class InstantiationInfo { 171 // Really these are all SourceLocations. 172 173 /// SpellingLoc - Where the spelling for the token can be found. 174 unsigned SpellingLoc; 175 176 /// InstantiationLocStart/InstantiationLocEnd - In a macro expansion, these 177 /// indicate the start and end of the instantiation. In object-like macros, 178 /// these will be the same. In a function-like macro instantiation, the 179 /// start will be the identifier and the end will be the ')'. 180 unsigned InstantiationLocStart, InstantiationLocEnd; 181 public: 182 SourceLocation getSpellingLoc() const { 183 return SourceLocation::getFromRawEncoding(SpellingLoc); 184 } 185 SourceLocation getInstantiationLocStart() const { 186 return SourceLocation::getFromRawEncoding(InstantiationLocStart); 187 } 188 SourceLocation getInstantiationLocEnd() const { 189 return SourceLocation::getFromRawEncoding(InstantiationLocEnd); 190 } 191 192 std::pair<SourceLocation,SourceLocation> getInstantiationLocRange() const { 193 return std::make_pair(getInstantiationLocStart(), 194 getInstantiationLocEnd()); 195 } 196 197 /// get - Return a InstantiationInfo for an expansion. IL specifies 198 /// the instantiation location (where the macro is expanded), and SL 199 /// specifies the spelling location (where the characters from the token 200 /// come from). IL and PL can both refer to normal File SLocs or 201 /// instantiation locations. 202 static InstantiationInfo get(SourceLocation ILStart, SourceLocation ILEnd, 203 SourceLocation SL) { 204 InstantiationInfo X; 205 X.SpellingLoc = SL.getRawEncoding(); 206 X.InstantiationLocStart = ILStart.getRawEncoding(); 207 X.InstantiationLocEnd = ILEnd.getRawEncoding(); 208 return X; 209 } 210 }; 211 212 /// SLocEntry - This is a discriminated union of FileInfo and 213 /// InstantiationInfo. SourceManager keeps an array of these objects, and 214 /// they are uniquely identified by the FileID datatype. 215 class SLocEntry { 216 unsigned Offset; // low bit is set for instantiation info. 217 union { 218 FileInfo File; 219 InstantiationInfo Instantiation; 220 }; 221 public: 222 unsigned getOffset() const { return Offset >> 1; } 223 224 bool isInstantiation() const { return Offset & 1; } 225 bool isFile() const { return !isInstantiation(); } 226 227 const FileInfo &getFile() const { 228 assert(isFile() && "Not a file SLocEntry!"); 229 return File; 230 } 231 232 const InstantiationInfo &getInstantiation() const { 233 assert(isInstantiation() && "Not an instantiation SLocEntry!"); 234 return Instantiation; 235 } 236 237 static SLocEntry get(unsigned Offset, const FileInfo &FI) { 238 SLocEntry E; 239 E.Offset = Offset << 1; 240 E.File = FI; 241 return E; 242 } 243 244 static SLocEntry get(unsigned Offset, const InstantiationInfo &II) { 245 SLocEntry E; 246 E.Offset = (Offset << 1) | 1; 247 E.Instantiation = II; 248 return E; 249 } 250 }; 251} // end SrcMgr namespace. 252 253/// SourceManager - This file handles loading and caching of source files into 254/// memory. This object owns the MemoryBuffer objects for all of the loaded 255/// files and assigns unique FileID's for each unique #include chain. 256/// 257/// The SourceManager can be queried for information about SourceLocation 258/// objects, turning them into either spelling or instantiation locations. 259/// Spelling locations represent where the bytes corresponding to a token came 260/// from and instantiation locations represent where the location is in the 261/// user's view. In the case of a macro expansion, for example, the spelling 262/// location indicates where the expanded token came from and the instantiation 263/// location specifies where it was expanded. 264class SourceManager { 265 mutable llvm::BumpPtrAllocator ContentCacheAlloc; 266 267 /// FileInfos - Memoized information about all of the files tracked by this 268 /// SourceManager. This set allows us to merge ContentCache entries based 269 /// on their FileEntry*. All ContentCache objects will thus have unique, 270 /// non-null, FileEntry pointers. 271 llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> FileInfos; 272 273 /// MemBufferInfos - Information about various memory buffers that we have 274 /// read in. All FileEntry* within the stored ContentCache objects are NULL, 275 /// as they do not refer to a file. 276 std::vector<SrcMgr::ContentCache*> MemBufferInfos; 277 278 /// SLocEntryTable - This is an array of SLocEntry's that we have created. 279 /// FileID is an index into this vector. This array is sorted by the offset. 280 std::vector<SrcMgr::SLocEntry> SLocEntryTable; 281 /// NextOffset - This is the next available offset that a new SLocEntry can 282 /// start at. It is SLocEntryTable.back().getOffset()+size of back() entry. 283 unsigned NextOffset; 284 285 /// LastFileIDLookup - This is a one-entry cache to speed up getFileID. 286 /// LastFileIDLookup records the last FileID looked up or created, because it 287 /// is very common to look up many tokens from the same file. 288 mutable FileID LastFileIDLookup; 289 290 /// LineTable - This holds information for #line directives. It is referenced 291 /// by indices from SLocEntryTable. 292 LineTableInfo *LineTable; 293 294 /// LastLineNo - These ivars serve as a cache used in the getLineNumber 295 /// method which is used to speedup getLineNumber calls to nearby locations. 296 mutable FileID LastLineNoFileIDQuery; 297 mutable SrcMgr::ContentCache *LastLineNoContentCache; 298 mutable unsigned LastLineNoFilePos; 299 mutable unsigned LastLineNoResult; 300 301 /// MainFileID - The file ID for the main source file of the translation unit. 302 FileID MainFileID; 303 304 // Statistics for -print-stats. 305 mutable unsigned NumLinearScans, NumBinaryProbes; 306 307 // SourceManager doesn't support copy construction. 308 explicit SourceManager(const SourceManager&); 309 void operator=(const SourceManager&); 310public: 311 SourceManager() : LineTable(0), NumLinearScans(0), NumBinaryProbes(0) { 312 clearIDTables(); 313 } 314 ~SourceManager(); 315 316 void clearIDTables(); 317 318 //===--------------------------------------------------------------------===// 319 // MainFileID creation and querying methods. 320 //===--------------------------------------------------------------------===// 321 322 /// getMainFileID - Returns the FileID of the main source file. 323 FileID getMainFileID() const { return MainFileID; } 324 325 /// createMainFileID - Create the FileID for the main source file. 326 FileID createMainFileID(const FileEntry *SourceFile, 327 SourceLocation IncludePos) { 328 assert(MainFileID.isInvalid() && "MainFileID already set!"); 329 MainFileID = createFileID(SourceFile, IncludePos, SrcMgr::C_User); 330 return MainFileID; 331 } 332 333 //===--------------------------------------------------------------------===// 334 // Methods to create new FileID's and instantiations. 335 //===--------------------------------------------------------------------===// 336 337 /// createFileID - Create a new FileID that represents the specified file 338 /// being #included from the specified IncludePosition. This returns 0 on 339 /// error and translates NULL into standard input. 340 FileID createFileID(const FileEntry *SourceFile, SourceLocation IncludePos, 341 SrcMgr::CharacteristicKind FileCharacter) { 342 const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile); 343 if (IR == 0) return FileID(); // Error opening file? 344 return createFileID(IR, IncludePos, FileCharacter); 345 } 346 347 /// createFileIDForMemBuffer - Create a new FileID that represents the 348 /// specified memory buffer. This does no caching of the buffer and takes 349 /// ownership of the MemoryBuffer, so only pass a MemoryBuffer to this once. 350 FileID createFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) { 351 return createFileID(createMemBufferContentCache(Buffer), SourceLocation(), 352 SrcMgr::C_User); 353 } 354 355 /// createMainFileIDForMembuffer - Create the FileID for a memory buffer 356 /// that will represent the FileID for the main source. One example 357 /// of when this would be used is when the main source is read from STDIN. 358 FileID createMainFileIDForMemBuffer(const llvm::MemoryBuffer *Buffer) { 359 assert(MainFileID.isInvalid() && "MainFileID already set!"); 360 MainFileID = createFileIDForMemBuffer(Buffer); 361 return MainFileID; 362 } 363 364 /// createInstantiationLoc - Return a new SourceLocation that encodes the fact 365 /// that a token at Loc should actually be referenced from InstantiationLoc. 366 /// TokLength is the length of the token being instantiated. 367 SourceLocation createInstantiationLoc(SourceLocation Loc, 368 SourceLocation InstantiationLocStart, 369 SourceLocation InstantiationLocEnd, 370 unsigned TokLength); 371 372 //===--------------------------------------------------------------------===// 373 // FileID manipulation methods. 374 //===--------------------------------------------------------------------===// 375 376 /// getBuffer - Return the buffer for the specified FileID. 377 /// 378 const llvm::MemoryBuffer *getBuffer(FileID FID) const { 379 return getSLocEntry(FID).getFile().getContentCache()->getBuffer(); 380 } 381 382 /// getFileEntryForID - Returns the FileEntry record for the provided FileID. 383 const FileEntry *getFileEntryForID(FileID FID) const { 384 return getSLocEntry(FID).getFile().getContentCache()->Entry; 385 } 386 387 /// getBufferData - Return a pointer to the start and end of the source buffer 388 /// data for the specified FileID. 389 std::pair<const char*, const char*> getBufferData(FileID FID) const; 390 391 392 //===--------------------------------------------------------------------===// 393 // SourceLocation manipulation methods. 394 //===--------------------------------------------------------------------===// 395 396 /// getFileID - Return the FileID for a SourceLocation. This is a very 397 /// hot method that is used for all SourceManager queries that start with a 398 /// SourceLocation object. It is responsible for finding the entry in 399 /// SLocEntryTable which contains the specified location. 400 /// 401 FileID getFileID(SourceLocation SpellingLoc) const { 402 unsigned SLocOffset = SpellingLoc.getOffset(); 403 404 // If our one-entry cache covers this offset, just return it. 405 if (isOffsetInFileID(LastFileIDLookup, SLocOffset)) 406 return LastFileIDLookup; 407 408 return getFileIDSlow(SLocOffset); 409 } 410 411 /// getLocForStartOfFile - Return the source location corresponding to the 412 /// first byte of the specified file. 413 SourceLocation getLocForStartOfFile(FileID FID) const { 414 assert(FID.ID < SLocEntryTable.size() && SLocEntryTable[FID.ID].isFile()); 415 unsigned FileOffset = SLocEntryTable[FID.ID].getOffset(); 416 return SourceLocation::getFileLoc(FileOffset); 417 } 418 419 /// getInstantiationLoc - Given a SourceLocation object, return the 420 /// instantiation location referenced by the ID. 421 SourceLocation getInstantiationLoc(SourceLocation Loc) const { 422 // Handle the non-mapped case inline, defer to out of line code to handle 423 // instantiations. 424 if (Loc.isFileID()) return Loc; 425 return getInstantiationLocSlowCase(Loc); 426 } 427 428 /// getImmediateInstantiationRange - Loc is required to be an instantiation 429 /// location. Return the start/end of the instantiation information. 430 std::pair<SourceLocation,SourceLocation> 431 getImmediateInstantiationRange(SourceLocation Loc) const; 432 433 /// getInstantiationRange - Given a SourceLocation object, return the 434 /// range of tokens covered by the instantiation in the ultimate file. 435 std::pair<SourceLocation,SourceLocation> 436 getInstantiationRange(SourceLocation Loc) const; 437 438 439 /// getSpellingLoc - Given a SourceLocation object, return the spelling 440 /// location referenced by the ID. This is the place where the characters 441 /// that make up the lexed token can be found. 442 SourceLocation getSpellingLoc(SourceLocation Loc) const { 443 // Handle the non-mapped case inline, defer to out of line code to handle 444 // instantiations. 445 if (Loc.isFileID()) return Loc; 446 return getSpellingLocSlowCase(Loc); 447 } 448 449 /// getImmediateSpellingLoc - Given a SourceLocation object, return the 450 /// spelling location referenced by the ID. This is the first level down 451 /// towards the place where the characters that make up the lexed token can be 452 /// found. This should not generally be used by clients. 453 SourceLocation getImmediateSpellingLoc(SourceLocation Loc) const; 454 455 /// getDecomposedLoc - Decompose the specified location into a raw FileID + 456 /// Offset pair. The first element is the FileID, the second is the 457 /// offset from the start of the buffer of the location. 458 std::pair<FileID, unsigned> getDecomposedLoc(SourceLocation Loc) const { 459 FileID FID = getFileID(Loc); 460 return std::make_pair(FID, Loc.getOffset()-getSLocEntry(FID).getOffset()); 461 } 462 463 /// getDecomposedInstantiationLoc - Decompose the specified location into a 464 /// raw FileID + Offset pair. If the location is an instantiation record, 465 /// walk through it until we find the final location instantiated. 466 std::pair<FileID, unsigned> 467 getDecomposedInstantiationLoc(SourceLocation Loc) const { 468 FileID FID = getFileID(Loc); 469 const SrcMgr::SLocEntry *E = &getSLocEntry(FID); 470 471 unsigned Offset = Loc.getOffset()-E->getOffset(); 472 if (Loc.isFileID()) 473 return std::make_pair(FID, Offset); 474 475 return getDecomposedInstantiationLocSlowCase(E, Offset); 476 } 477 478 /// getDecomposedSpellingLoc - Decompose the specified location into a raw 479 /// FileID + Offset pair. If the location is an instantiation record, walk 480 /// through it until we find its spelling record. 481 std::pair<FileID, unsigned> 482 getDecomposedSpellingLoc(SourceLocation Loc) const { 483 FileID FID = getFileID(Loc); 484 const SrcMgr::SLocEntry *E = &getSLocEntry(FID); 485 486 unsigned Offset = Loc.getOffset()-E->getOffset(); 487 if (Loc.isFileID()) 488 return std::make_pair(FID, Offset); 489 return getDecomposedSpellingLocSlowCase(E, Offset); 490 } 491 492 /// getFileOffset - This method returns the offset from the start 493 /// of the file that the specified SourceLocation represents. This is not very 494 /// meaningful for a macro ID. 495 unsigned getFileOffset(SourceLocation SpellingLoc) const { 496 return getDecomposedLoc(SpellingLoc).second; 497 } 498 499 500 //===--------------------------------------------------------------------===// 501 // Queries about the code at a SourceLocation. 502 //===--------------------------------------------------------------------===// 503 504 /// getCharacterData - Return a pointer to the start of the specified location 505 /// in the appropriate spelling MemoryBuffer. 506 const char *getCharacterData(SourceLocation SL) const; 507 508 /// getColumnNumber - Return the column # for the specified file position. 509 /// This is significantly cheaper to compute than the line number. This 510 /// returns zero if the column number isn't known. This may only be called on 511 /// a file sloc, so you must choose a spelling or instantiation location 512 /// before calling this method. 513 unsigned getColumnNumber(FileID FID, unsigned FilePos) const; 514 unsigned getSpellingColumnNumber(SourceLocation Loc) const; 515 unsigned getInstantiationColumnNumber(SourceLocation Loc) const; 516 517 518 /// getLineNumber - Given a SourceLocation, return the spelling line number 519 /// for the position indicated. This requires building and caching a table of 520 /// line offsets for the MemoryBuffer, so this is not cheap: use only when 521 /// about to emit a diagnostic. 522 unsigned getLineNumber(FileID FID, unsigned FilePos) const; 523 524 unsigned getInstantiationLineNumber(SourceLocation Loc) const; 525 unsigned getSpellingLineNumber(SourceLocation Loc) const; 526 527 /// Return the filename or buffer identifier of the buffer the location is in. 528 /// Note that this name does not respect #line directives. Use getPresumedLoc 529 /// for normal clients. 530 const char *getBufferName(SourceLocation Loc) const; 531 532 /// getFileCharacteristic - return the file characteristic of the specified 533 /// source location, indicating whether this is a normal file, a system 534 /// header, or an "implicit extern C" system header. 535 /// 536 /// This state can be modified with flags on GNU linemarker directives like: 537 /// # 4 "foo.h" 3 538 /// which changes all source locations in the current file after that to be 539 /// considered to be from a system header. 540 SrcMgr::CharacteristicKind getFileCharacteristic(SourceLocation Loc) const; 541 542 /// getPresumedLoc - This method returns the "presumed" location of a 543 /// SourceLocation specifies. A "presumed location" can be modified by #line 544 /// or GNU line marker directives. This provides a view on the data that a 545 /// user should see in diagnostics, for example. 546 /// 547 /// Note that a presumed location is always given as the instantiation point 548 /// of an instantiation location, not at the spelling location. 549 PresumedLoc getPresumedLoc(SourceLocation Loc) const; 550 551 /// isFromSameFile - Returns true if both SourceLocations correspond to 552 /// the same file. 553 bool isFromSameFile(SourceLocation Loc1, SourceLocation Loc2) const { 554 return getFileID(Loc1) == getFileID(Loc2); 555 } 556 557 /// isFromMainFile - Returns true if the file of provided SourceLocation is 558 /// the main file. 559 bool isFromMainFile(SourceLocation Loc) const { 560 return getFileID(Loc) == getMainFileID(); 561 } 562 563 /// isInSystemHeader - Returns if a SourceLocation is in a system header. 564 bool isInSystemHeader(SourceLocation Loc) const { 565 return getFileCharacteristic(Loc) != SrcMgr::C_User; 566 } 567 568 //===--------------------------------------------------------------------===// 569 // Line Table Manipulation Routines 570 //===--------------------------------------------------------------------===// 571 572 /// getLineTableFilenameID - Return the uniqued ID for the specified filename. 573 /// 574 unsigned getLineTableFilenameID(const char *Ptr, unsigned Len); 575 576 /// AddLineNote - Add a line note to the line table for the FileID and offset 577 /// specified by Loc. If FilenameID is -1, it is considered to be 578 /// unspecified. 579 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID); 580 void AddLineNote(SourceLocation Loc, unsigned LineNo, int FilenameID, 581 bool IsFileEntry, bool IsFileExit, 582 bool IsSystemHeader, bool IsExternCHeader); 583 584 /// \brief Determine if the source manager has a line table. 585 bool hasLineTable() const { return LineTable != 0; } 586 587 /// \brief Retrieve the stored line table. 588 LineTableInfo &getLineTable(); 589 590 //===--------------------------------------------------------------------===// 591 // Other miscellaneous methods. 592 //===--------------------------------------------------------------------===// 593 594 // Iterators over FileInfos. 595 typedef llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*> 596 ::const_iterator fileinfo_iterator; 597 fileinfo_iterator fileinfo_begin() const { return FileInfos.begin(); } 598 fileinfo_iterator fileinfo_end() const { return FileInfos.end(); } 599 600 /// PrintStats - Print statistics to stderr. 601 /// 602 void PrintStats() const; 603 604 // Iteration over the source location entry table. 605 typedef std::vector<SrcMgr::SLocEntry>::const_iterator sloc_entry_iterator; 606 607 sloc_entry_iterator sloc_entry_begin() const { 608 return SLocEntryTable.begin(); 609 } 610 611 sloc_entry_iterator sloc_entry_end() const { 612 return SLocEntryTable.end(); 613 } 614 615 unsigned sloc_entry_size() const { return SLocEntryTable.size(); } 616 617 const SrcMgr::SLocEntry &getSLocEntry(FileID FID) const { 618 assert(FID.ID < SLocEntryTable.size() && "Invalid id"); 619 return SLocEntryTable[FID.ID]; 620 } 621 622 unsigned getNextOffset() const { return NextOffset; } 623 624private: 625 /// isOffsetInFileID - Return true if the specified FileID contains the 626 /// specified SourceLocation offset. This is a very hot method. 627 inline bool isOffsetInFileID(FileID FID, unsigned SLocOffset) const { 628 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID); 629 // If the entry is after the offset, it can't contain it. 630 if (SLocOffset < Entry.getOffset()) return false; 631 632 // If this is the last entry than it does. Otherwise, the entry after it 633 // has to not include it. 634 if (FID.ID+1 == SLocEntryTable.size()) return true; 635 return SLocOffset < SLocEntryTable[FID.ID+1].getOffset(); 636 } 637 638 /// createFileID - Create a new fileID for the specified ContentCache and 639 /// include position. This works regardless of whether the ContentCache 640 /// corresponds to a file or some other input source. 641 FileID createFileID(const SrcMgr::ContentCache* File, 642 SourceLocation IncludePos, 643 SrcMgr::CharacteristicKind DirCharacter); 644 645 const SrcMgr::ContentCache * 646 getOrCreateContentCache(const FileEntry *SourceFile); 647 648 /// createMemBufferContentCache - Create a new ContentCache for the specified 649 /// memory buffer. 650 const SrcMgr::ContentCache* 651 createMemBufferContentCache(const llvm::MemoryBuffer *Buf); 652 653 FileID getFileIDSlow(unsigned SLocOffset) const; 654 655 SourceLocation getInstantiationLocSlowCase(SourceLocation Loc) const; 656 SourceLocation getSpellingLocSlowCase(SourceLocation Loc) const; 657 658 std::pair<FileID, unsigned> 659 getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E, 660 unsigned Offset) const; 661 std::pair<FileID, unsigned> 662 getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, 663 unsigned Offset) const; 664}; 665 666 667} // end namespace clang 668 669#endif 670