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 // The specified source location may be a mapped location, due to a macro 205 // instantiation or #line directive. Strip off this information to find out 206 // where the characters are actually located. 207 SpellingLoc = getSpellingLoc(SpellingLoc); 208 209 // Resolve InstantLoc down to a real instantiation location. 210 InstantLoc = getInstantiationLoc(InstantLoc); 211 212 SLocEntryTable.push_back(SLocEntry::get(NextOffset, 213 InstantiationInfo::get(InstantLoc, 214 SpellingLoc))); 215 assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!"); 216 NextOffset += TokLength+1; 217 return SourceLocation::getMacroLoc(NextOffset-(TokLength+1)); 218 } 219 220 /// getBufferData - Return a pointer to the start and end of the source buffer 221 /// data for the specified FileID. 222 std::pair<const char*, const char*> 223 SourceManager::getBufferData(FileID FID) const { 224 const llvm::MemoryBuffer *Buf = getBuffer(FID); 225 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd()); 226 } 227 228 229 //===--------------------------------------------------------------------===// 230 // SourceLocation manipulation methods. 231 //===--------------------------------------------------------------------===// 232 233 /// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot 234 /// method that is used for all SourceManager queries that start with a 235 /// SourceLocation object. It is responsible for finding the entry in 236 /// SLocEntryTable which contains the specified location. 237 /// 238 FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const { 239 assert(SLocOffset && "Invalid FileID"); 240 241 // After the first and second level caches, I see two common sorts of 242 // behavior: 1) a lot of searched FileID's are "near" the cached file location 243 // or are "near" the cached instantiation location. 2) others are just 244 // completely random and may be a very long way away. 245 // 246 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly 247 // then we fall back to a less cache efficient, but more scalable, binary 248 // search to find the location. 249 250 // See if this is near the file point - worst case we start scanning from the 251 // most newly created FileID. 252 std::vector<SrcMgr::SLocEntry>::const_iterator I; 253 254 if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) { 255 // Neither loc prunes our search. 256 I = SLocEntryTable.end(); 257 } else { 258 // Perhaps it is near the file point. 259 I = SLocEntryTable.begin()+LastFileIDLookup.ID; 260 } 261 262 // Find the FileID that contains this. "I" is an iterator that points to a 263 // FileID whose offset is known to be larger than SLocOffset. 264 unsigned NumProbes = 0; 265 while (1) { 266 --I; 267 if (I->getOffset() <= SLocOffset) { 268 #if 0 269 printf("lin %d -> %d [%s] %d %d\n", SLocOffset, 270 I-SLocEntryTable.begin(), 271 I->isInstantiation() ? "inst" : "file", 272 LastFileIDLookup.ID, int(SLocEntryTable.end()-I)); 273 #endif 274 FileID Res = FileID::get(I-SLocEntryTable.begin()); 275 276 // If this isn't an instantiation, remember it. We have good locality 277 // across FileID lookups. 278 if (!I->isInstantiation()) 279 LastFileIDLookup = Res; 280 NumLinearScans += NumProbes+1; 281 return Res; 282 } 283 if (++NumProbes == 8) 284 break; 285 } 286 287 // Convert "I" back into an index. We know that it is an entry whose index is 288 // larger than the offset we are looking for. 289 unsigned GreaterIndex = I-SLocEntryTable.begin(); 290 // LessIndex - This is the lower bound of the range that we're searching. 291 // We know that the offset corresponding to the FileID is is less than 292 // SLocOffset. 293 unsigned LessIndex = 0; 294 NumProbes = 0; 295 while (1) { 296 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex; 297 unsigned MidOffset = SLocEntryTable[MiddleIndex].getOffset(); 298 299 ++NumProbes; 300 301 // If the offset of the midpoint is too large, chop the high side of the 302 // range to the midpoint. 303 if (MidOffset > SLocOffset) { 304 GreaterIndex = MiddleIndex; 305 continue; 306 } 307 308 // If the middle index contains the value, succeed and return. 309 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) { 310 #if 0 311 printf("bin %d -> %d [%s] %d %d\n", SLocOffset, 312 I-SLocEntryTable.begin(), 313 I->isInstantiation() ? "inst" : "file", 314 LastFileIDLookup.ID, int(SLocEntryTable.end()-I)); 315 #endif 316 FileID Res = FileID::get(MiddleIndex); 317 318 // If this isn't an instantiation, remember it. We have good locality 319 // across FileID lookups. 320 if (!I->isInstantiation()) 321 LastFileIDLookup = Res; 322 NumBinaryProbes += NumProbes; 323 return Res; 324 } 325 326 // Otherwise, move the low-side up to the middle index. 327 LessIndex = MiddleIndex; 328 } 329 } 330 331 std::pair<FileID, unsigned> 332 SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E, 333 unsigned Offset) const { 334 // If this is an instantiation record, walk through all the instantiation 335 // points. 336 FileID FID; 337 SourceLocation Loc; 338 do { 339 Loc = E->getInstantiation().getInstantiationLoc(); 340 341 FID = getFileID(Loc); 342 E = &getSLocEntry(FID); 343 Offset += Loc.getOffset()-E->getOffset(); 344 } while (!Loc.isFileID()); 345 346 return std::make_pair(FID, Offset); 347 } 348 349 std::pair<FileID, unsigned> 350 SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, 351 unsigned Offset) const { 352 // If this is an instantiation record, walk through all the instantiation 353 // points. 354 FileID FID; 355 SourceLocation Loc; 356 do { 357 Loc = E->getInstantiation().getSpellingLoc(); 358 359 FID = getFileID(Loc); 360 E = &getSLocEntry(FID); 361 Offset += Loc.getOffset()-E->getOffset(); 362 } while (!Loc.isFileID()); 363 364 return std::make_pair(FID, Offset); 365 } 366 367 368 //===----------------------------------------------------------------------===// 369 // Queries about the code at a SourceLocation. 370 //===----------------------------------------------------------------------===// 371 372 /// getCharacterData - Return a pointer to the start of the specified location 373 /// in the appropriate MemoryBuffer. 374 const char *SourceManager::getCharacterData(SourceLocation SL) const { 375 // Note that this is a hot function in the getSpelling() path, which is 376 // heavily used by -E mode. 377 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL); 378 379 // Note that calling 'getBuffer()' may lazily page in a source file. 380 return getSLocEntry(LocInfo.first).getFile().getContentCache() 381 ->getBuffer()->getBufferStart() + LocInfo.second; 382 } 383 384 385 /// getColumnNumber - Return the column # for the specified file position. 386 /// this is significantly cheaper to compute than the line number. This returns 387 /// zero if the column number isn't known. 388 unsigned SourceManager::getColumnNumber(SourceLocation Loc) const { 389 if (Loc.isInvalid()) return 0; 390 assert(Loc.isFileID() && "Don't know what part of instantiation loc to get"); 391 392 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); 393 unsigned FilePos = LocInfo.second; 394 395 const char *Buf = getBuffer(LocInfo.first)->getBufferStart(); 396 397 unsigned LineStart = FilePos; 398 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r') 399 --LineStart; 400 return FilePos-LineStart+1; 401 } 402 403 static void ComputeLineNumbers(ContentCache* FI) DISABLE_INLINE; 404 static void ComputeLineNumbers(ContentCache* FI) { 405 // Note that calling 'getBuffer()' may lazily page in the file. 406 const MemoryBuffer *Buffer = FI->getBuffer(); 407 408 // Find the file offsets of all of the *physical* source lines. This does 409 // not look at trigraphs, escaped newlines, or anything else tricky. 410 std::vector<unsigned> LineOffsets; 411 412 // Line #1 starts at char 0. 413 LineOffsets.push_back(0); 414 415 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart(); 416 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd(); 417 unsigned Offs = 0; 418 while (1) { 419 // Skip over the contents of the line. 420 // TODO: Vectorize this? This is very performance sensitive for programs 421 // with lots of diagnostics and in -E mode. 422 const unsigned char *NextBuf = (const unsigned char *)Buf; 423 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0') 424 ++NextBuf; 425 Offs += NextBuf-Buf; 426 Buf = NextBuf; 427 428 if (Buf[0] == '\n' || Buf[0] == '\r') { 429 // If this is \n\r or \r\n, skip both characters. 430 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1]) 431 ++Offs, ++Buf; 432 ++Offs, ++Buf; 433 LineOffsets.push_back(Offs); 434 } else { 435 // Otherwise, this is a null. If end of file, exit. 436 if (Buf == End) break; 437 // Otherwise, skip the null. 438 ++Offs, ++Buf; 439 } 440 } 441 442 // Copy the offsets into the FileInfo structure. 443 FI->NumLines = LineOffsets.size(); 444 FI->SourceLineCache = new unsigned[LineOffsets.size()]; 445 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache); 446 } 447 448 /// getLineNumber - Given a SourceLocation, return the spelling line number 449 /// for the position indicated. This requires building and caching a table of 450 /// line offsets for the MemoryBuffer, so this is not cheap: use only when 451 /// about to emit a diagnostic. 452 unsigned SourceManager::getLineNumber(SourceLocation Loc) const { 453 if (Loc.isInvalid()) return 0; 454 assert(Loc.isFileID() && "Don't know what part of instantiation loc to get"); 455 456 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); 457 458 ContentCache *Content; 459 if (LastLineNoFileIDQuery == LocInfo.first) 460 Content = LastLineNoContentCache; 461 else 462 Content = const_cast<ContentCache*>(getSLocEntry(LocInfo.first) 463 .getFile().getContentCache()); 464 465 // If this is the first use of line information for this buffer, compute the 466 /// SourceLineCache for it on demand. 467 if (Content->SourceLineCache == 0) 468 ComputeLineNumbers(Content); 469 470 // Okay, we know we have a line number table. Do a binary search to find the 471 // line number that this character position lands on. 472 unsigned *SourceLineCache = Content->SourceLineCache; 473 unsigned *SourceLineCacheStart = SourceLineCache; 474 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines; 475 476 unsigned QueriedFilePos = LocInfo.second+1; 477 478 // If the previous query was to the same file, we know both the file pos from 479 // that query and the line number returned. This allows us to narrow the 480 // search space from the entire file to something near the match. 481 if (LastLineNoFileIDQuery == LocInfo.first) { 482 if (QueriedFilePos >= LastLineNoFilePos) { 483 SourceLineCache = SourceLineCache+LastLineNoResult-1; 484 485 // The query is likely to be nearby the previous one. Here we check to 486 // see if it is within 5, 10 or 20 lines. It can be far away in cases 487 // where big comment blocks and vertical whitespace eat up lines but 488 // contribute no tokens. 489 if (SourceLineCache+5 < SourceLineCacheEnd) { 490 if (SourceLineCache[5] > QueriedFilePos) 491 SourceLineCacheEnd = SourceLineCache+5; 492 else if (SourceLineCache+10 < SourceLineCacheEnd) { 493 if (SourceLineCache[10] > QueriedFilePos) 494 SourceLineCacheEnd = SourceLineCache+10; 495 else if (SourceLineCache+20 < SourceLineCacheEnd) { 496 if (SourceLineCache[20] > QueriedFilePos) 497 SourceLineCacheEnd = SourceLineCache+20; 498 } 499 } 500 } 501 } else { 502 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1; 503 } 504 } 505 506 // If the spread is large, do a "radix" test as our initial guess, based on 507 // the assumption that lines average to approximately the same length. 508 // NOTE: This is currently disabled, as it does not appear to be profitable in 509 // initial measurements. 510 if (0 && SourceLineCacheEnd-SourceLineCache > 20) { 511 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1]; 512 513 // Take a stab at guessing where it is. 514 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen; 515 516 // Check for -10 and +10 lines. 517 unsigned LowerBound = std::max(int(ApproxPos-10), 0); 518 unsigned UpperBound = std::min(ApproxPos+10, FileLen); 519 520 // If the computed lower bound is less than the query location, move it in. 521 if (SourceLineCache < SourceLineCacheStart+LowerBound && 522 SourceLineCacheStart[LowerBound] < QueriedFilePos) 523 SourceLineCache = SourceLineCacheStart+LowerBound; 524 525 // If the computed upper bound is greater than the query location, move it. 526 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound && 527 SourceLineCacheStart[UpperBound] >= QueriedFilePos) 528 SourceLineCacheEnd = SourceLineCacheStart+UpperBound; 529 } 530 531 unsigned *Pos 532 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos); 533 unsigned LineNo = Pos-SourceLineCacheStart; 534 535 LastLineNoFileIDQuery = LocInfo.first; 536 LastLineNoContentCache = Content; 537 LastLineNoFilePos = QueriedFilePos; 538 LastLineNoResult = LineNo; 539 return LineNo; 540 } 541 542 /// getSourceName - This method returns the name of the file or buffer that 543 /// the SourceLocation specifies. This can be modified with #line directives, 544 /// etc. 545 const char *SourceManager::getSourceName(SourceLocation Loc) const { 546 if (Loc.isInvalid()) return ""; 547 548 const SrcMgr::ContentCache *C = 549 getSLocEntry(getFileID(getSpellingLoc(Loc))).getFile().getContentCache(); 550 551 // To get the source name, first consult the FileEntry (if one exists) before 552 // the MemBuffer as this will avoid unnecessarily paging in the MemBuffer. 553 return C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier(); 554 } 555 556 //===----------------------------------------------------------------------===// 557 // Other miscellaneous methods. 558 //===----------------------------------------------------------------------===// 559 560 561 /// PrintStats - Print statistics to stderr. 562 /// 563 void SourceManager::PrintStats() const { 564 llvm::cerr << "\n*** Source Manager Stats:\n"; 565 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size() 566 << " mem buffers mapped, " << SLocEntryTable.size() 567 << " SLocEntry's allocated.\n"; 568 569 unsigned NumLineNumsComputed = 0; 570 unsigned NumFileBytesMapped = 0; 571 for (std::set<ContentCache>::const_iterator I = 572 FileInfos.begin(), E = FileInfos.end(); I != E; ++I) { 573 NumLineNumsComputed += I->SourceLineCache != 0; 574 NumFileBytesMapped += I->getSizeBytesMapped(); 575 } 576 577 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, " 578 << NumLineNumsComputed << " files with line #'s computed.\n"; 579 llvm::cerr << "FileID scans: " << NumLinearScans << " linear, " 580 << NumBinaryProbes << " binary.\n"; 581 } 582 583 //===----------------------------------------------------------------------===// 584 // Serialization. 585 //===----------------------------------------------------------------------===// 586 587 void ContentCache::Emit(llvm::Serializer& S) const { 588 S.FlushRecord(); 589 S.EmitPtr(this); 590 591 if (Entry) { 592 llvm::sys::Path Fname(Buffer->getBufferIdentifier()); 593 594 if (Fname.isAbsolute()) 595 S.EmitCStr(Fname.c_str()); 596 else { 597 // Create an absolute path. 598 // FIXME: This will potentially contain ".." and "." in the path. 599 llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory(); 600 path.appendComponent(Fname.c_str()); 601 S.EmitCStr(path.c_str()); 602 } 603 } 604 else { 605 const char* p = Buffer->getBufferStart(); 606 const char* e = Buffer->getBufferEnd(); 607 608 S.EmitInt(e-p); 609 610 for ( ; p != e; ++p) 611 S.EmitInt(*p); 612 } 613 614 S.FlushRecord(); 615 } 616 617 void ContentCache::ReadToSourceManager(llvm::Deserializer& D, 618 SourceManager& SMgr, 619 FileManager* FMgr, 620 std::vector<char>& Buf) { 621 if (FMgr) { 622 llvm::SerializedPtrID PtrID = D.ReadPtrID(); 623 D.ReadCStr(Buf,false); 624 625 // Create/fetch the FileEntry. 626 const char* start = &Buf[0]; 627 const FileEntry* E = FMgr->getFile(start,start+Buf.size()); 628 629 // FIXME: Ideally we want a lazy materialization of the ContentCache 630 // anyway, because we don't want to read in source files unless this 631 // is absolutely needed. 632 if (!E) 633 D.RegisterPtr(PtrID,NULL); 634 else 635 // Get the ContextCache object and register it with the deserializer. 636 D.RegisterPtr(PtrID, SMgr.getOrCreateContentCache(E)); 637 return; 638 } 639 640 // Register the ContextCache object with the deserializer. 641 SMgr.MemBufferInfos.push_back(ContentCache()); 642 ContentCache& Entry = const_cast<ContentCache&>(SMgr.MemBufferInfos.back()); 643 D.RegisterPtr(&Entry); 644 645 // Create the buffer. 646 unsigned Size = D.ReadInt(); 647 Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size); 648 649 // Read the contents of the buffer. 650 char* p = const_cast<char*>(Entry.Buffer->getBufferStart()); 651 for (unsigned i = 0; i < Size ; ++i) 652 p[i] = D.ReadInt(); 653 } 654 655 void SourceManager::Emit(llvm::Serializer& S) const { 656 S.EnterBlock(); 657 S.EmitPtr(this); 658 S.EmitInt(MainFileID.getOpaqueValue()); 659 660 // Emit: FileInfos. Just emit the file name. 661 S.EnterBlock(); 662 663 std::for_each(FileInfos.begin(),FileInfos.end(), 664 S.MakeEmitter<ContentCache>()); 665 666 S.ExitBlock(); 667 668 // Emit: MemBufferInfos 669 S.EnterBlock(); 670 671 std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(), 672 S.MakeEmitter<ContentCache>()); 673 674 S.ExitBlock(); 675 676 // FIXME: Emit SLocEntryTable. 677 678 S.ExitBlock(); 679 } 680 681 SourceManager* 682 SourceManager::CreateAndRegister(llvm::Deserializer& D, FileManager& FMgr){ 683 SourceManager *M = new SourceManager(); 684 D.RegisterPtr(M); 685 686 // Read: the FileID of the main source file of the translation unit. 687 M->MainFileID = FileID::get(D.ReadInt()); 688 689 std::vector<char> Buf; 690 691 { // Read: FileInfos. 692 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation(); 693 while (!D.FinishedBlock(BLoc)) 694 ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf); 695 } 696 697 { // Read: MemBufferInfos. 698 llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation(); 699 while (!D.FinishedBlock(BLoc)) 700 ContentCache::ReadToSourceManager(D,*M,NULL,Buf); 701 } 702 703 // FIXME: Read SLocEntryTable. 704 705 return M; 706 } 707