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