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