1 //===--- SourceManager.cpp - Track and cache source files -----------------===// 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 implements the SourceManager interface. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "clang/Basic/SourceManager.h" 15 #include "clang/Basic/FileManager.h" 16 #include "llvm/Support/Compiler.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 #include "llvm/System/Path.h" 19 #include "llvm/Bitcode/Serialize.h" 20 #include "llvm/Bitcode/Deserialize.h" 21 #include "llvm/Support/Streams.h" 22 #include <algorithm> 23 using namespace clang; 24 using namespace SrcMgr; 25 using llvm::MemoryBuffer; 26 27 //===--------------------------------------------------------------------===// 28 // SourceManager Helper Classes 29 //===--------------------------------------------------------------------===// 30 31 ContentCache::~ContentCache() { 32 delete Buffer; 33 } 34 35 /// getSizeBytesMapped - Returns the number of bytes actually mapped for 36 /// this ContentCache. This can be 0 if the MemBuffer was not actually 37 /// instantiated. 38 unsigned ContentCache::getSizeBytesMapped() const { 39 return Buffer ? Buffer->getBufferSize() : 0; 40 } 41 42 /// getSize - Returns the size of the content encapsulated by this ContentCache. 43 /// This can be the size of the source file or the size of an arbitrary 44 /// scratch buffer. If the ContentCache encapsulates a source file, that 45 /// file is not lazily brought in from disk to satisfy this query. 46 unsigned ContentCache::getSize() const { 47 return Entry ? Entry->getSize() : Buffer->getBufferSize(); 48 } 49 50 const llvm::MemoryBuffer *ContentCache::getBuffer() const { 51 // Lazily create the Buffer for ContentCaches that wrap files. 52 if (!Buffer && Entry) { 53 // FIXME: Should we support a way to not have to do this check over 54 // and over if we cannot open the file? 55 Buffer = MemoryBuffer::getFile(Entry->getName(), 0, Entry->getSize()); 56 } 57 return Buffer; 58 } 59 60 //===--------------------------------------------------------------------===// 61 // Line Table Implementation 62 //===--------------------------------------------------------------------===// 63 64 namespace clang { 65 /// LineTableInfo - This class is used to hold and unique data used to 66 /// represent #line information. 67 class LineTableInfo { 68 /// FilenameIDs - This map is used to assign unique IDs to filenames in 69 /// #line directives. This allows us to unique the filenames that 70 /// frequently reoccur and reference them with indices. FilenameIDs holds 71 /// the mapping from string -> ID, and FilenamesByID holds the mapping of ID 72 /// to string. 73 llvm::StringMap<unsigned, llvm::BumpPtrAllocator> FilenameIDs; 74 std::vector<llvm::StringMapEntry<unsigned>*> FilenamesByID; 75 public: 76 LineTableInfo() { 77 } 78 79 void clear() { 80 FilenameIDs.clear(); 81 FilenamesByID.clear(); 82 } 83 84 ~LineTableInfo() {} 85 86 unsigned getLineTableFilenameID(const char *Ptr, unsigned Len); 87 88 }; 89 } // namespace clang 90 91 92 93 94 unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) { 95 // Look up the filename in the string table, returning the pre-existing value 96 // if it exists. 97 llvm::StringMapEntry<unsigned> &Entry = 98 FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U); 99 if (Entry.getValue() != ~0U) 100 return Entry.getValue(); 101 102 // Otherwise, assign this the next available ID. 103 Entry.setValue(FilenamesByID.size()); 104 FilenamesByID.push_back(&Entry); 105 return FilenamesByID.size()-1; 106 } 107 108 /// getLineTableFilenameID - Return the uniqued ID for the specified filename. 109 /// 110 unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) { 111 if (LineTable == 0) 112 LineTable = new LineTableInfo(); 113 return LineTable->getLineTableFilenameID(Ptr, Len); 114 } 115 116 117 //===--------------------------------------------------------------------===// 118 // Private 'Create' methods. 119 //===--------------------------------------------------------------------===// 120 121 SourceManager::~SourceManager() { 122 delete LineTable; 123 124 // Delete FileEntry objects corresponding to content caches. Since the actual 125 // content cache objects are bump pointer allocated, we just have to run the 126 // dtors, but we call the deallocate method for completeness. 127 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) { 128 MemBufferInfos[i]->~ContentCache(); 129 ContentCacheAlloc.Deallocate(MemBufferInfos[i]); 130 } 131 for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator 132 I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) { 133 I->second->~ContentCache(); 134 ContentCacheAlloc.Deallocate(I->second); 135 } 136 } 137 138 void SourceManager::clearIDTables() { 139 MainFileID = FileID(); 140 SLocEntryTable.clear(); 141 LastLineNoFileIDQuery = FileID(); 142 LastLineNoContentCache = 0; 143 LastFileIDLookup = FileID(); 144 145 if (LineTable) 146 LineTable->clear(); 147 148 // Use up FileID #0 as an invalid instantiation. 149 NextOffset = 0; 150 createInstantiationLoc(SourceLocation(), SourceLocation(), 1); 151 } 152 153 /// getOrCreateContentCache - Create or return a cached ContentCache for the 154 /// specified file. 155 const ContentCache * 156 SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) { 157 assert(FileEnt && "Didn't specify a file entry to use?"); 158 159 // Do we already have information about this file? 160 ContentCache *&Entry = FileInfos[FileEnt]; 161 if (Entry) return Entry; 162 163 // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned 164 // so that FileInfo can use the low 3 bits of the pointer for its own 165 // nefarious purposes. 166 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment; 167 EntryAlign = std::max(8U, EntryAlign); 168 Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign); 169 new (Entry) ContentCache(FileEnt); 170 return Entry; 171 } 172 173 174 /// createMemBufferContentCache - Create a new ContentCache for the specified 175 /// memory buffer. This does no caching. 176 const ContentCache* 177 SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) { 178 // Add a new ContentCache to the MemBufferInfos list and return it. Make sure 179 // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of 180 // the pointer for its own nefarious purposes. 181 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment; 182 EntryAlign = std::max(8U, EntryAlign); 183 ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign); 184 new (Entry) ContentCache(); 185 MemBufferInfos.push_back(Entry); 186 Entry->setBuffer(Buffer); 187 return Entry; 188 } 189 190 //===----------------------------------------------------------------------===// 191 // Methods to create new FileID's and instantiations. 192 //===----------------------------------------------------------------------===// 193 194 /// createFileID - Create a new fileID for the specified ContentCache and 195 /// include position. This works regardless of whether the ContentCache 196 /// corresponds to a file or some other input source. 197 FileID SourceManager::createFileID(const ContentCache *File, 198 SourceLocation IncludePos, 199 SrcMgr::CharacteristicKind FileCharacter) { 200 SLocEntryTable.push_back(SLocEntry::get(NextOffset, 201 FileInfo::get(IncludePos, File, 202 FileCharacter))); 203 unsigned FileSize = File->getSize(); 204 assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!"); 205 NextOffset += FileSize+1; 206 207 // Set LastFileIDLookup to the newly created file. The next getFileID call is 208 // almost guaranteed to be from that file. 209 return LastFileIDLookup = FileID::get(SLocEntryTable.size()-1); 210 } 211 212 /// createInstantiationLoc - Return a new SourceLocation that encodes the fact 213 /// that a token from SpellingLoc should actually be referenced from 214 /// InstantiationLoc. 215 SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc, 216 SourceLocation InstantLoc, 217 unsigned TokLength) { 218 SLocEntryTable.push_back(SLocEntry::get(NextOffset, 219 InstantiationInfo::get(InstantLoc, 220 SpellingLoc))); 221 assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!"); 222 NextOffset += TokLength+1; 223 return SourceLocation::getMacroLoc(NextOffset-(TokLength+1)); 224 } 225 226 /// getBufferData - Return a pointer to the start and end of the source buffer 227 /// data for the specified FileID. 228 std::pair<const char*, const char*> 229 SourceManager::getBufferData(FileID FID) const { 230 const llvm::MemoryBuffer *Buf = getBuffer(FID); 231 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd()); 232 } 233 234 235 //===--------------------------------------------------------------------===// 236 // SourceLocation manipulation methods. 237 //===--------------------------------------------------------------------===// 238 239 /// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot 240 /// method that is used for all SourceManager queries that start with a 241 /// SourceLocation object. It is responsible for finding the entry in 242 /// SLocEntryTable which contains the specified location. 243 /// 244 FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const { 245 assert(SLocOffset && "Invalid FileID"); 246 247 // After the first and second level caches, I see two common sorts of 248 // behavior: 1) a lot of searched FileID's are "near" the cached file location 249 // or are "near" the cached instantiation location. 2) others are just 250 // completely random and may be a very long way away. 251 // 252 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly 253 // then we fall back to a less cache efficient, but more scalable, binary 254 // search to find the location. 255 256 // See if this is near the file point - worst case we start scanning from the 257 // most newly created FileID. 258 std::vector<SrcMgr::SLocEntry>::const_iterator I; 259 260 if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) { 261 // Neither loc prunes our search. 262 I = SLocEntryTable.end(); 263 } else { 264 // Perhaps it is near the file point. 265 I = SLocEntryTable.begin()+LastFileIDLookup.ID; 266 } 267 268 // Find the FileID that contains this. "I" is an iterator that points to a 269 // FileID whose offset is known to be larger than SLocOffset. 270 unsigned NumProbes = 0; 271 while (1) { 272 --I; 273 if (I->getOffset() <= SLocOffset) { 274 #if 0 275 printf("lin %d -> %d [%s] %d %d\n", SLocOffset, 276 I-SLocEntryTable.begin(), 277 I->isInstantiation() ? "inst" : "file", 278 LastFileIDLookup.ID, int(SLocEntryTable.end()-I)); 279 #endif 280 FileID Res = FileID::get(I-SLocEntryTable.begin()); 281 282 // If this isn't an instantiation, remember it. We have good locality 283 // across FileID lookups. 284 if (!I->isInstantiation()) 285 LastFileIDLookup = Res; 286 NumLinearScans += NumProbes+1; 287 return Res; 288 } 289 if (++NumProbes == 8) 290 break; 291 } 292 293 // Convert "I" back into an index. We know that it is an entry whose index is 294 // larger than the offset we are looking for. 295 unsigned GreaterIndex = I-SLocEntryTable.begin(); 296 // LessIndex - This is the lower bound of the range that we're searching. 297 // We know that the offset corresponding to the FileID is is less than 298 // SLocOffset. 299 unsigned LessIndex = 0; 300 NumProbes = 0; 301 while (1) { 302 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex; 303 unsigned MidOffset = SLocEntryTable[MiddleIndex].getOffset(); 304 305 ++NumProbes; 306 307 // If the offset of the midpoint is too large, chop the high side of the 308 // range to the midpoint. 309 if (MidOffset > SLocOffset) { 310 GreaterIndex = MiddleIndex; 311 continue; 312 } 313 314 // If the middle index contains the value, succeed and return. 315 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) { 316 #if 0 317 printf("bin %d -> %d [%s] %d %d\n", SLocOffset, 318 I-SLocEntryTable.begin(), 319 I->isInstantiation() ? "inst" : "file", 320 LastFileIDLookup.ID, int(SLocEntryTable.end()-I)); 321 #endif 322 FileID Res = FileID::get(MiddleIndex); 323 324 // If this isn't an instantiation, remember it. We have good locality 325 // across FileID lookups. 326 if (!I->isInstantiation()) 327 LastFileIDLookup = Res; 328 NumBinaryProbes += NumProbes; 329 return Res; 330 } 331 332 // Otherwise, move the low-side up to the middle index. 333 LessIndex = MiddleIndex; 334 } 335 } 336 337 SourceLocation SourceManager:: 338 getInstantiationLocSlowCase(SourceLocation Loc) const { 339 do { 340 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); 341 Loc =getSLocEntry(LocInfo.first).getInstantiation().getInstantiationLoc(); 342 Loc = Loc.getFileLocWithOffset(LocInfo.second); 343 } while (!Loc.isFileID()); 344 345 return Loc; 346 } 347 348 SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const { 349 do { 350 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); 351 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc(); 352 Loc = Loc.getFileLocWithOffset(LocInfo.second); 353 } while (!Loc.isFileID()); 354 return Loc; 355 } 356 357 358 std::pair<FileID, unsigned> 359 SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E, 360 unsigned Offset) const { 361 // If this is an instantiation record, walk through all the instantiation 362 // points. 363 FileID FID; 364 SourceLocation Loc; 365 do { 366 Loc = E->getInstantiation().getInstantiationLoc(); 367 368 FID = getFileID(Loc); 369 E = &getSLocEntry(FID); 370 Offset += Loc.getOffset()-E->getOffset(); 371 } while (!Loc.isFileID()); 372 373 return std::make_pair(FID, Offset); 374 } 375 376 std::pair<FileID, unsigned> 377 SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, 378 unsigned Offset) const { 379 // If this is an instantiation record, walk through all the instantiation 380 // points. 381 FileID FID; 382 SourceLocation Loc; 383 do { 384 Loc = E->getInstantiation().getSpellingLoc(); 385 386 FID = getFileID(Loc); 387 E = &getSLocEntry(FID); 388 Offset += Loc.getOffset()-E->getOffset(); 389 } while (!Loc.isFileID()); 390 391 return std::make_pair(FID, Offset); 392 } 393 394 395 //===----------------------------------------------------------------------===// 396 // Queries about the code at a SourceLocation. 397 //===----------------------------------------------------------------------===// 398 399 /// getCharacterData - Return a pointer to the start of the specified location 400 /// in the appropriate MemoryBuffer. 401 const char *SourceManager::getCharacterData(SourceLocation SL) const { 402 // Note that this is a hot function in the getSpelling() path, which is 403 // heavily used by -E mode. 404 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL); 405 406 // Note that calling 'getBuffer()' may lazily page in a source file. 407 return getSLocEntry(LocInfo.first).getFile().getContentCache() 408 ->getBuffer()->getBufferStart() + LocInfo.second; 409 } 410 411 412 /// getColumnNumber - Return the column # for the specified file position. 413 /// this is significantly cheaper to compute than the line number. This returns 414 /// zero if the column number isn't known. 415 unsigned SourceManager::getColumnNumber(SourceLocation Loc) const { 416 if (Loc.isInvalid()) return 0; 417 assert(Loc.isFileID() && "Don't know what part of instantiation loc to get"); 418 419 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); 420 unsigned FilePos = LocInfo.second; 421 422 const char *Buf = getBuffer(LocInfo.first)->getBufferStart(); 423 424 unsigned LineStart = FilePos; 425 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r') 426 --LineStart; 427 return FilePos-LineStart+1; 428 } 429 430 static void ComputeLineNumbers(ContentCache* FI, 431 llvm::BumpPtrAllocator &Alloc) DISABLE_INLINE; 432 static void ComputeLineNumbers(ContentCache* FI, llvm::BumpPtrAllocator &Alloc){ 433 // Note that calling 'getBuffer()' may lazily page in the file. 434 const MemoryBuffer *Buffer = FI->getBuffer(); 435 436 // Find the file offsets of all of the *physical* source lines. This does 437 // not look at trigraphs, escaped newlines, or anything else tricky. 438 std::vector<unsigned> LineOffsets; 439 440 // Line #1 starts at char 0. 441 LineOffsets.push_back(0); 442 443 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart(); 444 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd(); 445 unsigned Offs = 0; 446 while (1) { 447 // Skip over the contents of the line. 448 // TODO: Vectorize this? This is very performance sensitive for programs 449 // with lots of diagnostics and in -E mode. 450 const unsigned char *NextBuf = (const unsigned char *)Buf; 451 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0') 452 ++NextBuf; 453 Offs += NextBuf-Buf; 454 Buf = NextBuf; 455 456 if (Buf[0] == '\n' || Buf[0] == '\r') { 457 // If this is \n\r or \r\n, skip both characters. 458 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1]) 459 ++Offs, ++Buf; 460 ++Offs, ++Buf; 461 LineOffsets.push_back(Offs); 462 } else { 463 // Otherwise, this is a null. If end of file, exit. 464 if (Buf == End) break; 465 // Otherwise, skip the null. 466 ++Offs, ++Buf; 467 } 468 } 469 470 // Copy the offsets into the FileInfo structure. 471 FI->NumLines = LineOffsets.size(); 472 FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size()); 473 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache); 474 } 475 476 /// getLineNumber - Given a SourceLocation, return the spelling line number 477 /// for the position indicated. This requires building and caching a table of 478 /// line offsets for the MemoryBuffer, so this is not cheap: use only when 479 /// about to emit a diagnostic. 480 unsigned SourceManager::getLineNumber(SourceLocation Loc) const { 481 if (Loc.isInvalid()) return 0; 482 assert(Loc.isFileID() && "Don't know what part of instantiation loc to get"); 483 484 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); 485 486 ContentCache *Content; 487 if (LastLineNoFileIDQuery == LocInfo.first) 488 Content = LastLineNoContentCache; 489 else 490 Content = const_cast<ContentCache*>(getSLocEntry(LocInfo.first) 491 .getFile().getContentCache()); 492 493 // If this is the first use of line information for this buffer, compute the 494 /// SourceLineCache for it on demand. 495 if (Content->SourceLineCache == 0) 496 ComputeLineNumbers(Content, ContentCacheAlloc); 497 498 // Okay, we know we have a line number table. Do a binary search to find the 499 // line number that this character position lands on. 500 unsigned *SourceLineCache = Content->SourceLineCache; 501 unsigned *SourceLineCacheStart = SourceLineCache; 502 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines; 503 504 unsigned QueriedFilePos = LocInfo.second+1; 505 506 // If the previous query was to the same file, we know both the file pos from 507 // that query and the line number returned. This allows us to narrow the 508 // search space from the entire file to something near the match. 509 if (LastLineNoFileIDQuery == LocInfo.first) { 510 if (QueriedFilePos >= LastLineNoFilePos) { 511 SourceLineCache = SourceLineCache+LastLineNoResult-1; 512 513 // The query is likely to be nearby the previous one. Here we check to 514 // see if it is within 5, 10 or 20 lines. It can be far away in cases 515 // where big comment blocks and vertical whitespace eat up lines but 516 // contribute no tokens. 517 if (SourceLineCache+5 < SourceLineCacheEnd) { 518 if (SourceLineCache[5] > QueriedFilePos) 519 SourceLineCacheEnd = SourceLineCache+5; 520 else if (SourceLineCache+10 < SourceLineCacheEnd) { 521 if (SourceLineCache[10] > QueriedFilePos) 522 SourceLineCacheEnd = SourceLineCache+10; 523 else if (SourceLineCache+20 < SourceLineCacheEnd) { 524 if (SourceLineCache[20] > QueriedFilePos) 525 SourceLineCacheEnd = SourceLineCache+20; 526 } 527 } 528 } 529 } else { 530 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1; 531 } 532 } 533 534 // If the spread is large, do a "radix" test as our initial guess, based on 535 // the assumption that lines average to approximately the same length. 536 // NOTE: This is currently disabled, as it does not appear to be profitable in 537 // initial measurements. 538 if (0 && SourceLineCacheEnd-SourceLineCache > 20) { 539 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1]; 540 541 // Take a stab at guessing where it is. 542 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen; 543 544 // Check for -10 and +10 lines. 545 unsigned LowerBound = std::max(int(ApproxPos-10), 0); 546 unsigned UpperBound = std::min(ApproxPos+10, FileLen); 547 548 // If the computed lower bound is less than the query location, move it in. 549 if (SourceLineCache < SourceLineCacheStart+LowerBound && 550 SourceLineCacheStart[LowerBound] < QueriedFilePos) 551 SourceLineCache = SourceLineCacheStart+LowerBound; 552 553 // If the computed upper bound is greater than the query location, move it. 554 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound && 555 SourceLineCacheStart[UpperBound] >= QueriedFilePos) 556 SourceLineCacheEnd = SourceLineCacheStart+UpperBound; 557 } 558 559 unsigned *Pos 560 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos); 561 unsigned LineNo = Pos-SourceLineCacheStart; 562 563 LastLineNoFileIDQuery = LocInfo.first; 564 LastLineNoContentCache = Content; 565 LastLineNoFilePos = QueriedFilePos; 566 LastLineNoResult = LineNo; 567 return LineNo; 568 } 569 570 /// getPresumedLoc - This method returns the "presumed" location of a 571 /// SourceLocation specifies. A "presumed location" can be modified by #line 572 /// or GNU line marker directives. This provides a view on the data that a 573 /// user should see in diagnostics, for example. 574 /// 575 /// Note that a presumed location is always given as the instantiation point 576 /// of an instantiation location, not at the spelling location. 577 PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const { 578 if (Loc.isInvalid()) return PresumedLoc(); 579 580 // Presumed locations are always for instantiation points. 581 Loc = getInstantiationLoc(Loc); 582 583 // FIXME: Could just decompose Loc once! 584 585 const SrcMgr::FileInfo &FI = getSLocEntry(getFileID(Loc)).getFile(); 586 const SrcMgr::ContentCache *C = FI.getContentCache(); 587 588 // To get the source name, first consult the FileEntry (if one exists) before 589 // the MemBuffer as this will avoid unnecessarily paging in the MemBuffer. 590 const char *Filename = 591 C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier(); 592 593 return PresumedLoc(Filename, getLineNumber(Loc), getColumnNumber(Loc), 594 FI.getIncludeLoc()); 595 } 596 597 //===----------------------------------------------------------------------===// 598 // Other miscellaneous methods. 599 //===----------------------------------------------------------------------===// 600 601 602 /// PrintStats - Print statistics to stderr. 603 /// 604 void SourceManager::PrintStats() const { 605 llvm::cerr << "\n*** Source Manager Stats:\n"; 606 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size() 607 << " mem buffers mapped.\n"; 608 llvm::cerr << SLocEntryTable.size() << " SLocEntry's allocated, " 609 << NextOffset << "B of Sloc address space used.\n"; 610 611 unsigned NumLineNumsComputed = 0; 612 unsigned NumFileBytesMapped = 0; 613 for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){ 614 NumLineNumsComputed += I->second->SourceLineCache != 0; 615 NumFileBytesMapped += I->second->getSizeBytesMapped(); 616 } 617 618 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, " 619 << NumLineNumsComputed << " files with line #'s computed.\n"; 620 llvm::cerr << "FileID scans: " << NumLinearScans << " linear, " 621 << NumBinaryProbes << " binary.\n"; 622 } 623 624 //===----------------------------------------------------------------------===// 625 // Serialization. 626 //===----------------------------------------------------------------------===// 627 628 void ContentCache::Emit(llvm::Serializer& S) const { 629 S.FlushRecord(); 630 S.EmitPtr(this); 631 632 if (Entry) { 633 llvm::sys::Path Fname(Buffer->getBufferIdentifier()); 634 635 if (Fname.isAbsolute()) 636 S.EmitCStr(Fname.c_str()); 637 else { 638 // Create an absolute path. 639 // FIXME: This will potentially contain ".." and "." in the path. 640 llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory(); 641 path.appendComponent(Fname.c_str()); 642 S.EmitCStr(path.c_str()); 643 } 644 } 645 else { 646 const char* p = Buffer->getBufferStart(); 647 const char* e = Buffer->getBufferEnd(); 648 649 S.EmitInt(e-p); 650 651 for ( ; p != e; ++p) 652 S.EmitInt(*p); 653 } 654 655 S.FlushRecord(); 656 } 657 658 void ContentCache::ReadToSourceManager(llvm::Deserializer& D, 659 SourceManager& SMgr, 660 FileManager* FMgr, 661 std::vector<char>& Buf) { 662 if (FMgr) { 663 llvm::SerializedPtrID PtrID = D.ReadPtrID(); 664 D.ReadCStr(Buf,false); 665 666 // Create/fetch the FileEntry. 667 const char* start = &Buf[0]; 668 const FileEntry* E = FMgr->getFile(start,start+Buf.size()); 669 670 // FIXME: Ideally we want a lazy materialization of the ContentCache 671 // anyway, because we don't want to read in source files unless this 672 // is absolutely needed. 673 if (!E) 674 D.RegisterPtr(PtrID,NULL); 675 else 676 // Get the ContextCache object and register it with the deserializer. 677 D.RegisterPtr(PtrID, SMgr.getOrCreateContentCache(E)); 678 return; 679 } 680 681 // Register the ContextCache object with the deserializer. 682 /* FIXME: 683 ContentCache *Entry 684 SMgr.MemBufferInfos.push_back(ContentCache()); 685 = const_cast<ContentCache&>(SMgr.MemBufferInfos.back()); 686 D.RegisterPtr(&Entry); 687 688 // Create the buffer. 689 unsigned Size = D.ReadInt(); 690 Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size); 691 692 // Read the contents of the buffer. 693 char* p = const_cast<char*>(Entry.Buffer->getBufferStart()); 694 for (unsigned i = 0; i < Size ; ++i) 695 p[i] = D.ReadInt(); 696 */ 697 } 698 699 void SourceManager::Emit(llvm::Serializer& S) const { 700 S.EnterBlock(); 701 S.EmitPtr(this); 702 S.EmitInt(MainFileID.getOpaqueValue()); 703 704 // Emit: FileInfos. Just emit the file name. 705 S.EnterBlock(); 706 707 // FIXME: Emit FileInfos. 708 //std::for_each(FileInfos.begin(), FileInfos.end(), 709 // S.MakeEmitter<ContentCache>()); 710 711 S.ExitBlock(); 712 713 // Emit: MemBufferInfos 714 S.EnterBlock(); 715 716 /* FIXME: EMIT. 717 std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(), 718 S.MakeEmitter<ContentCache>()); 719 */ 720 721 S.ExitBlock(); 722 723 // FIXME: Emit SLocEntryTable. 724 725 S.ExitBlock(); 726 } 727 728 SourceManager* 729 SourceManager::CreateAndRegister(llvm::Deserializer& D, FileManager& FMgr){ 730 SourceManager *M = new SourceManager(); 731 D.RegisterPtr(M); 732 733 // Read: the FileID of the main source file of the translation unit. 734 M->MainFileID = FileID::get(D.ReadInt()); 735 736 std::vector<char> Buf; 737 738 /*{ // FIXME Read: FileInfos. 739 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation(); 740 while (!D.FinishedBlock(BLoc)) 741 ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf); 742 }*/ 743 744 { // Read: MemBufferInfos. 745 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation(); 746 while (!D.FinishedBlock(BLoc)) 747 ContentCache::ReadToSourceManager(D,*M,NULL,Buf); 748 } 749 750 // FIXME: Read SLocEntryTable. 751 752 return M; 753 } 754