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