1 //===- SourceManager.cpp - Track and cache source files -------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file implements the SourceManager interface. 10 // 11 //===----------------------------------------------------------------------===// 12 13 #include "clang/Basic/SourceManager.h" 14 #include "clang/Basic/Diagnostic.h" 15 #include "clang/Basic/FileManager.h" 16 #include "clang/Basic/LLVM.h" 17 #include "clang/Basic/SourceLocation.h" 18 #include "clang/Basic/SourceManagerInternals.h" 19 #include "llvm/ADT/DenseMap.h" 20 #include "llvm/ADT/Optional.h" 21 #include "llvm/ADT/None.h" 22 #include "llvm/ADT/STLExtras.h" 23 #include "llvm/ADT/SmallVector.h" 24 #include "llvm/ADT/StringSwitch.h" 25 #include "llvm/ADT/StringRef.h" 26 #include "llvm/Support/Allocator.h" 27 #include "llvm/Support/Capacity.h" 28 #include "llvm/Support/Compiler.h" 29 #include "llvm/Support/ErrorHandling.h" 30 #include "llvm/Support/FileSystem.h" 31 #include "llvm/Support/MathExtras.h" 32 #include "llvm/Support/MemoryBuffer.h" 33 #include "llvm/Support/Path.h" 34 #include "llvm/Support/raw_ostream.h" 35 #include <algorithm> 36 #include <cassert> 37 #include <cstddef> 38 #include <cstdint> 39 #include <memory> 40 #include <tuple> 41 #include <utility> 42 #include <vector> 43 44 using namespace clang; 45 using namespace SrcMgr; 46 using llvm::MemoryBuffer; 47 48 //===----------------------------------------------------------------------===// 49 // SourceManager Helper Classes 50 //===----------------------------------------------------------------------===// 51 52 ContentCache::~ContentCache() { 53 if (shouldFreeBuffer()) 54 delete Buffer.getPointer(); 55 } 56 57 /// getSizeBytesMapped - Returns the number of bytes actually mapped for this 58 /// ContentCache. This can be 0 if the MemBuffer was not actually expanded. 59 unsigned ContentCache::getSizeBytesMapped() const { 60 return Buffer.getPointer() ? Buffer.getPointer()->getBufferSize() : 0; 61 } 62 63 /// Returns the kind of memory used to back the memory buffer for 64 /// this content cache. This is used for performance analysis. 65 llvm::MemoryBuffer::BufferKind ContentCache::getMemoryBufferKind() const { 66 assert(Buffer.getPointer()); 67 68 // Should be unreachable, but keep for sanity. 69 if (!Buffer.getPointer()) 70 return llvm::MemoryBuffer::MemoryBuffer_Malloc; 71 72 const llvm::MemoryBuffer *buf = Buffer.getPointer(); 73 return buf->getBufferKind(); 74 } 75 76 /// getSize - Returns the size of the content encapsulated by this ContentCache. 77 /// This can be the size of the source file or the size of an arbitrary 78 /// scratch buffer. If the ContentCache encapsulates a source file, that 79 /// file is not lazily brought in from disk to satisfy this query. 80 unsigned ContentCache::getSize() const { 81 return Buffer.getPointer() ? (unsigned) Buffer.getPointer()->getBufferSize() 82 : (unsigned) ContentsEntry->getSize(); 83 } 84 85 void ContentCache::replaceBuffer(const llvm::MemoryBuffer *B, bool DoNotFree) { 86 if (B && B == Buffer.getPointer()) { 87 assert(0 && "Replacing with the same buffer"); 88 Buffer.setInt(DoNotFree? DoNotFreeFlag : 0); 89 return; 90 } 91 92 if (shouldFreeBuffer()) 93 delete Buffer.getPointer(); 94 Buffer.setPointer(B); 95 Buffer.setInt((B && DoNotFree) ? DoNotFreeFlag : 0); 96 } 97 98 const llvm::MemoryBuffer *ContentCache::getBuffer(DiagnosticsEngine &Diag, 99 const SourceManager &SM, 100 SourceLocation Loc, 101 bool *Invalid) const { 102 // Lazily create the Buffer for ContentCaches that wrap files. If we already 103 // computed it, just return what we have. 104 if (Buffer.getPointer() || !ContentsEntry) { 105 if (Invalid) 106 *Invalid = isBufferInvalid(); 107 108 return Buffer.getPointer(); 109 } 110 111 // Check that the file's size fits in an 'unsigned' (with room for a 112 // past-the-end value). This is deeply regrettable, but various parts of 113 // Clang (including elsewhere in this file!) use 'unsigned' to represent file 114 // offsets, line numbers, string literal lengths, and so on, and fail 115 // miserably on large source files. 116 if ((uint64_t)ContentsEntry->getSize() >= 117 std::numeric_limits<unsigned>::max()) { 118 // We can't make a memory buffer of the required size, so just make a small 119 // one. We should never hit a situation where we've already parsed to a 120 // later offset of the file, so it shouldn't matter that the buffer is 121 // smaller than the file. 122 Buffer.setPointer( 123 llvm::MemoryBuffer::getMemBuffer("", ContentsEntry->getName()) 124 .release()); 125 if (Diag.isDiagnosticInFlight()) 126 Diag.SetDelayedDiagnostic(diag::err_file_too_large, 127 ContentsEntry->getName()); 128 else 129 Diag.Report(Loc, diag::err_file_too_large) 130 << ContentsEntry->getName(); 131 132 Buffer.setInt(Buffer.getInt() | InvalidFlag); 133 if (Invalid) *Invalid = true; 134 return Buffer.getPointer(); 135 } 136 137 bool isVolatile = SM.userFilesAreVolatile() && !IsSystemFile; 138 auto BufferOrError = 139 SM.getFileManager().getBufferForFile(ContentsEntry, isVolatile); 140 141 // If we were unable to open the file, then we are in an inconsistent 142 // situation where the content cache referenced a file which no longer 143 // exists. Most likely, we were using a stat cache with an invalid entry but 144 // the file could also have been removed during processing. Since we can't 145 // really deal with this situation, just create an empty buffer. 146 // 147 // FIXME: This is definitely not ideal, but our immediate clients can't 148 // currently handle returning a null entry here. Ideally we should detect 149 // that we are in an inconsistent situation and error out as quickly as 150 // possible. 151 if (!BufferOrError) { 152 StringRef FillStr("<<<MISSING SOURCE FILE>>>\n"); 153 auto BackupBuffer = llvm::WritableMemoryBuffer::getNewUninitMemBuffer( 154 ContentsEntry->getSize(), "<invalid>"); 155 char *Ptr = BackupBuffer->getBufferStart(); 156 for (unsigned i = 0, e = ContentsEntry->getSize(); i != e; ++i) 157 Ptr[i] = FillStr[i % FillStr.size()]; 158 Buffer.setPointer(BackupBuffer.release()); 159 160 if (Diag.isDiagnosticInFlight()) 161 Diag.SetDelayedDiagnostic(diag::err_cannot_open_file, 162 ContentsEntry->getName(), 163 BufferOrError.getError().message()); 164 else 165 Diag.Report(Loc, diag::err_cannot_open_file) 166 << ContentsEntry->getName() << BufferOrError.getError().message(); 167 168 Buffer.setInt(Buffer.getInt() | InvalidFlag); 169 170 if (Invalid) *Invalid = true; 171 return Buffer.getPointer(); 172 } 173 174 Buffer.setPointer(BufferOrError->release()); 175 176 // Check that the file's size is the same as in the file entry (which may 177 // have come from a stat cache). 178 if (getRawBuffer()->getBufferSize() != (size_t)ContentsEntry->getSize()) { 179 if (Diag.isDiagnosticInFlight()) 180 Diag.SetDelayedDiagnostic(diag::err_file_modified, 181 ContentsEntry->getName()); 182 else 183 Diag.Report(Loc, diag::err_file_modified) 184 << ContentsEntry->getName(); 185 186 Buffer.setInt(Buffer.getInt() | InvalidFlag); 187 if (Invalid) *Invalid = true; 188 return Buffer.getPointer(); 189 } 190 191 // If the buffer is valid, check to see if it has a UTF Byte Order Mark 192 // (BOM). We only support UTF-8 with and without a BOM right now. See 193 // http://en.wikipedia.org/wiki/Byte_order_mark for more information. 194 StringRef BufStr = Buffer.getPointer()->getBuffer(); 195 const char *InvalidBOM = llvm::StringSwitch<const char *>(BufStr) 196 .StartsWith(llvm::StringLiteral::withInnerNUL("\x00\x00\xFE\xFF"), 197 "UTF-32 (BE)") 198 .StartsWith(llvm::StringLiteral::withInnerNUL("\xFF\xFE\x00\x00"), 199 "UTF-32 (LE)") 200 .StartsWith("\xFE\xFF", "UTF-16 (BE)") 201 .StartsWith("\xFF\xFE", "UTF-16 (LE)") 202 .StartsWith("\x2B\x2F\x76", "UTF-7") 203 .StartsWith("\xF7\x64\x4C", "UTF-1") 204 .StartsWith("\xDD\x73\x66\x73", "UTF-EBCDIC") 205 .StartsWith("\x0E\xFE\xFF", "SCSU") 206 .StartsWith("\xFB\xEE\x28", "BOCU-1") 207 .StartsWith("\x84\x31\x95\x33", "GB-18030") 208 .Default(nullptr); 209 210 if (InvalidBOM) { 211 Diag.Report(Loc, diag::err_unsupported_bom) 212 << InvalidBOM << ContentsEntry->getName(); 213 Buffer.setInt(Buffer.getInt() | InvalidFlag); 214 } 215 216 if (Invalid) 217 *Invalid = isBufferInvalid(); 218 219 return Buffer.getPointer(); 220 } 221 222 unsigned LineTableInfo::getLineTableFilenameID(StringRef Name) { 223 auto IterBool = FilenameIDs.try_emplace(Name, FilenamesByID.size()); 224 if (IterBool.second) 225 FilenamesByID.push_back(&*IterBool.first); 226 return IterBool.first->second; 227 } 228 229 /// Add a line note to the line table that indicates that there is a \#line or 230 /// GNU line marker at the specified FID/Offset location which changes the 231 /// presumed location to LineNo/FilenameID. If EntryExit is 0, then this doesn't 232 /// change the presumed \#include stack. If it is 1, this is a file entry, if 233 /// it is 2 then this is a file exit. FileKind specifies whether this is a 234 /// system header or extern C system header. 235 void LineTableInfo::AddLineNote(FileID FID, unsigned Offset, unsigned LineNo, 236 int FilenameID, unsigned EntryExit, 237 SrcMgr::CharacteristicKind FileKind) { 238 std::vector<LineEntry> &Entries = LineEntries[FID]; 239 240 // An unspecified FilenameID means use the last filename if available, or the 241 // main source file otherwise. 242 if (FilenameID == -1 && !Entries.empty()) 243 FilenameID = Entries.back().FilenameID; 244 245 assert((Entries.empty() || Entries.back().FileOffset < Offset) && 246 "Adding line entries out of order!"); 247 248 unsigned IncludeOffset = 0; 249 if (EntryExit == 0) { // No #include stack change. 250 IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset; 251 } else if (EntryExit == 1) { 252 IncludeOffset = Offset-1; 253 } else if (EntryExit == 2) { 254 assert(!Entries.empty() && Entries.back().IncludeOffset && 255 "PPDirectives should have caught case when popping empty include stack"); 256 257 // Get the include loc of the last entries' include loc as our include loc. 258 IncludeOffset = 0; 259 if (const LineEntry *PrevEntry = 260 FindNearestLineEntry(FID, Entries.back().IncludeOffset)) 261 IncludeOffset = PrevEntry->IncludeOffset; 262 } 263 264 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind, 265 IncludeOffset)); 266 } 267 268 /// FindNearestLineEntry - Find the line entry nearest to FID that is before 269 /// it. If there is no line entry before Offset in FID, return null. 270 const LineEntry *LineTableInfo::FindNearestLineEntry(FileID FID, 271 unsigned Offset) { 272 const std::vector<LineEntry> &Entries = LineEntries[FID]; 273 assert(!Entries.empty() && "No #line entries for this FID after all!"); 274 275 // It is very common for the query to be after the last #line, check this 276 // first. 277 if (Entries.back().FileOffset <= Offset) 278 return &Entries.back(); 279 280 // Do a binary search to find the maximal element that is still before Offset. 281 std::vector<LineEntry>::const_iterator I = llvm::upper_bound(Entries, Offset); 282 if (I == Entries.begin()) 283 return nullptr; 284 return &*--I; 285 } 286 287 /// Add a new line entry that has already been encoded into 288 /// the internal representation of the line table. 289 void LineTableInfo::AddEntry(FileID FID, 290 const std::vector<LineEntry> &Entries) { 291 LineEntries[FID] = Entries; 292 } 293 294 /// getLineTableFilenameID - Return the uniqued ID for the specified filename. 295 unsigned SourceManager::getLineTableFilenameID(StringRef Name) { 296 return getLineTable().getLineTableFilenameID(Name); 297 } 298 299 /// AddLineNote - Add a line note to the line table for the FileID and offset 300 /// specified by Loc. If FilenameID is -1, it is considered to be 301 /// unspecified. 302 void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo, 303 int FilenameID, bool IsFileEntry, 304 bool IsFileExit, 305 SrcMgr::CharacteristicKind FileKind) { 306 std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); 307 308 bool Invalid = false; 309 const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid); 310 if (!Entry.isFile() || Invalid) 311 return; 312 313 const SrcMgr::FileInfo &FileInfo = Entry.getFile(); 314 315 // Remember that this file has #line directives now if it doesn't already. 316 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives(); 317 318 (void) getLineTable(); 319 320 unsigned EntryExit = 0; 321 if (IsFileEntry) 322 EntryExit = 1; 323 else if (IsFileExit) 324 EntryExit = 2; 325 326 LineTable->AddLineNote(LocInfo.first, LocInfo.second, LineNo, FilenameID, 327 EntryExit, FileKind); 328 } 329 330 LineTableInfo &SourceManager::getLineTable() { 331 if (!LineTable) 332 LineTable = new LineTableInfo(); 333 return *LineTable; 334 } 335 336 //===----------------------------------------------------------------------===// 337 // Private 'Create' methods. 338 //===----------------------------------------------------------------------===// 339 340 SourceManager::SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr, 341 bool UserFilesAreVolatile) 342 : Diag(Diag), FileMgr(FileMgr), UserFilesAreVolatile(UserFilesAreVolatile) { 343 clearIDTables(); 344 Diag.setSourceManager(this); 345 } 346 347 SourceManager::~SourceManager() { 348 delete LineTable; 349 350 // Delete FileEntry objects corresponding to content caches. Since the actual 351 // content cache objects are bump pointer allocated, we just have to run the 352 // dtors, but we call the deallocate method for completeness. 353 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) { 354 if (MemBufferInfos[i]) { 355 MemBufferInfos[i]->~ContentCache(); 356 ContentCacheAlloc.Deallocate(MemBufferInfos[i]); 357 } 358 } 359 for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator 360 I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) { 361 if (I->second) { 362 I->second->~ContentCache(); 363 ContentCacheAlloc.Deallocate(I->second); 364 } 365 } 366 } 367 368 void SourceManager::clearIDTables() { 369 MainFileID = FileID(); 370 LocalSLocEntryTable.clear(); 371 LoadedSLocEntryTable.clear(); 372 SLocEntryLoaded.clear(); 373 LastLineNoFileIDQuery = FileID(); 374 LastLineNoContentCache = nullptr; 375 LastFileIDLookup = FileID(); 376 377 if (LineTable) 378 LineTable->clear(); 379 380 // Use up FileID #0 as an invalid expansion. 381 NextLocalOffset = 0; 382 CurrentLoadedOffset = MaxLoadedOffset; 383 createExpansionLoc(SourceLocation(), SourceLocation(), SourceLocation(), 1); 384 } 385 386 void SourceManager::initializeForReplay(const SourceManager &Old) { 387 assert(MainFileID.isInvalid() && "expected uninitialized SourceManager"); 388 389 auto CloneContentCache = [&](const ContentCache *Cache) -> ContentCache * { 390 auto *Clone = new (ContentCacheAlloc.Allocate<ContentCache>()) ContentCache; 391 Clone->OrigEntry = Cache->OrigEntry; 392 Clone->ContentsEntry = Cache->ContentsEntry; 393 Clone->BufferOverridden = Cache->BufferOverridden; 394 Clone->IsSystemFile = Cache->IsSystemFile; 395 Clone->IsTransient = Cache->IsTransient; 396 Clone->replaceBuffer(Cache->getRawBuffer(), /*DoNotFree*/true); 397 return Clone; 398 }; 399 400 // Ensure all SLocEntries are loaded from the external source. 401 for (unsigned I = 0, N = Old.LoadedSLocEntryTable.size(); I != N; ++I) 402 if (!Old.SLocEntryLoaded[I]) 403 Old.loadSLocEntry(I, nullptr); 404 405 // Inherit any content cache data from the old source manager. 406 for (auto &FileInfo : Old.FileInfos) { 407 SrcMgr::ContentCache *&Slot = FileInfos[FileInfo.first]; 408 if (Slot) 409 continue; 410 Slot = CloneContentCache(FileInfo.second); 411 } 412 } 413 414 /// getOrCreateContentCache - Create or return a cached ContentCache for the 415 /// specified file. 416 const ContentCache * 417 SourceManager::getOrCreateContentCache(const FileEntry *FileEnt, 418 bool isSystemFile) { 419 assert(FileEnt && "Didn't specify a file entry to use?"); 420 421 // Do we already have information about this file? 422 ContentCache *&Entry = FileInfos[FileEnt]; 423 if (Entry) return Entry; 424 425 // Nope, create a new Cache entry. 426 Entry = ContentCacheAlloc.Allocate<ContentCache>(); 427 428 if (OverriddenFilesInfo) { 429 // If the file contents are overridden with contents from another file, 430 // pass that file to ContentCache. 431 llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator 432 overI = OverriddenFilesInfo->OverriddenFiles.find(FileEnt); 433 if (overI == OverriddenFilesInfo->OverriddenFiles.end()) 434 new (Entry) ContentCache(FileEnt); 435 else 436 new (Entry) ContentCache(OverridenFilesKeepOriginalName ? FileEnt 437 : overI->second, 438 overI->second); 439 } else { 440 new (Entry) ContentCache(FileEnt); 441 } 442 443 Entry->IsSystemFile = isSystemFile; 444 Entry->IsTransient = FilesAreTransient; 445 446 return Entry; 447 } 448 449 /// Create a new ContentCache for the specified memory buffer. 450 /// This does no caching. 451 const ContentCache * 452 SourceManager::createMemBufferContentCache(const llvm::MemoryBuffer *Buffer, 453 bool DoNotFree) { 454 // Add a new ContentCache to the MemBufferInfos list and return it. 455 ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(); 456 new (Entry) ContentCache(); 457 MemBufferInfos.push_back(Entry); 458 Entry->replaceBuffer(Buffer, DoNotFree); 459 return Entry; 460 } 461 462 const SrcMgr::SLocEntry &SourceManager::loadSLocEntry(unsigned Index, 463 bool *Invalid) const { 464 assert(!SLocEntryLoaded[Index]); 465 if (ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2))) { 466 if (Invalid) 467 *Invalid = true; 468 // If the file of the SLocEntry changed we could still have loaded it. 469 if (!SLocEntryLoaded[Index]) { 470 // Try to recover; create a SLocEntry so the rest of clang can handle it. 471 LoadedSLocEntryTable[Index] = SLocEntry::get(0, 472 FileInfo::get(SourceLocation(), 473 getFakeContentCacheForRecovery(), 474 SrcMgr::C_User)); 475 } 476 } 477 478 return LoadedSLocEntryTable[Index]; 479 } 480 481 std::pair<int, unsigned> 482 SourceManager::AllocateLoadedSLocEntries(unsigned NumSLocEntries, 483 unsigned TotalSize) { 484 assert(ExternalSLocEntries && "Don't have an external sloc source"); 485 // Make sure we're not about to run out of source locations. 486 if (CurrentLoadedOffset - TotalSize < NextLocalOffset) 487 return std::make_pair(0, 0); 488 LoadedSLocEntryTable.resize(LoadedSLocEntryTable.size() + NumSLocEntries); 489 SLocEntryLoaded.resize(LoadedSLocEntryTable.size()); 490 CurrentLoadedOffset -= TotalSize; 491 int ID = LoadedSLocEntryTable.size(); 492 return std::make_pair(-ID - 1, CurrentLoadedOffset); 493 } 494 495 /// As part of recovering from missing or changed content, produce a 496 /// fake, non-empty buffer. 497 llvm::MemoryBuffer *SourceManager::getFakeBufferForRecovery() const { 498 if (!FakeBufferForRecovery) 499 FakeBufferForRecovery = 500 llvm::MemoryBuffer::getMemBuffer("<<<INVALID BUFFER>>"); 501 502 return FakeBufferForRecovery.get(); 503 } 504 505 /// As part of recovering from missing or changed content, produce a 506 /// fake content cache. 507 const SrcMgr::ContentCache * 508 SourceManager::getFakeContentCacheForRecovery() const { 509 if (!FakeContentCacheForRecovery) { 510 FakeContentCacheForRecovery = llvm::make_unique<SrcMgr::ContentCache>(); 511 FakeContentCacheForRecovery->replaceBuffer(getFakeBufferForRecovery(), 512 /*DoNotFree=*/true); 513 } 514 return FakeContentCacheForRecovery.get(); 515 } 516 517 /// Returns the previous in-order FileID or an invalid FileID if there 518 /// is no previous one. 519 FileID SourceManager::getPreviousFileID(FileID FID) const { 520 if (FID.isInvalid()) 521 return FileID(); 522 523 int ID = FID.ID; 524 if (ID == -1) 525 return FileID(); 526 527 if (ID > 0) { 528 if (ID-1 == 0) 529 return FileID(); 530 } else if (unsigned(-(ID-1) - 2) >= LoadedSLocEntryTable.size()) { 531 return FileID(); 532 } 533 534 return FileID::get(ID-1); 535 } 536 537 /// Returns the next in-order FileID or an invalid FileID if there is 538 /// no next one. 539 FileID SourceManager::getNextFileID(FileID FID) const { 540 if (FID.isInvalid()) 541 return FileID(); 542 543 int ID = FID.ID; 544 if (ID > 0) { 545 if (unsigned(ID+1) >= local_sloc_entry_size()) 546 return FileID(); 547 } else if (ID+1 >= -1) { 548 return FileID(); 549 } 550 551 return FileID::get(ID+1); 552 } 553 554 //===----------------------------------------------------------------------===// 555 // Methods to create new FileID's and macro expansions. 556 //===----------------------------------------------------------------------===// 557 558 /// createFileID - Create a new FileID for the specified ContentCache and 559 /// include position. This works regardless of whether the ContentCache 560 /// corresponds to a file or some other input source. 561 FileID SourceManager::createFileID(const ContentCache *File, 562 SourceLocation IncludePos, 563 SrcMgr::CharacteristicKind FileCharacter, 564 int LoadedID, unsigned LoadedOffset) { 565 if (LoadedID < 0) { 566 assert(LoadedID != -1 && "Loading sentinel FileID"); 567 unsigned Index = unsigned(-LoadedID) - 2; 568 assert(Index < LoadedSLocEntryTable.size() && "FileID out of range"); 569 assert(!SLocEntryLoaded[Index] && "FileID already loaded"); 570 LoadedSLocEntryTable[Index] = SLocEntry::get(LoadedOffset, 571 FileInfo::get(IncludePos, File, FileCharacter)); 572 SLocEntryLoaded[Index] = true; 573 return FileID::get(LoadedID); 574 } 575 LocalSLocEntryTable.push_back(SLocEntry::get(NextLocalOffset, 576 FileInfo::get(IncludePos, File, 577 FileCharacter))); 578 unsigned FileSize = File->getSize(); 579 assert(NextLocalOffset + FileSize + 1 > NextLocalOffset && 580 NextLocalOffset + FileSize + 1 <= CurrentLoadedOffset && 581 "Ran out of source locations!"); 582 // We do a +1 here because we want a SourceLocation that means "the end of the 583 // file", e.g. for the "no newline at the end of the file" diagnostic. 584 NextLocalOffset += FileSize + 1; 585 586 // Set LastFileIDLookup to the newly created file. The next getFileID call is 587 // almost guaranteed to be from that file. 588 FileID FID = FileID::get(LocalSLocEntryTable.size()-1); 589 return LastFileIDLookup = FID; 590 } 591 592 SourceLocation 593 SourceManager::createMacroArgExpansionLoc(SourceLocation SpellingLoc, 594 SourceLocation ExpansionLoc, 595 unsigned TokLength) { 596 ExpansionInfo Info = ExpansionInfo::createForMacroArg(SpellingLoc, 597 ExpansionLoc); 598 return createExpansionLocImpl(Info, TokLength); 599 } 600 601 SourceLocation 602 SourceManager::createExpansionLoc(SourceLocation SpellingLoc, 603 SourceLocation ExpansionLocStart, 604 SourceLocation ExpansionLocEnd, 605 unsigned TokLength, 606 bool ExpansionIsTokenRange, 607 int LoadedID, 608 unsigned LoadedOffset) { 609 ExpansionInfo Info = ExpansionInfo::create( 610 SpellingLoc, ExpansionLocStart, ExpansionLocEnd, ExpansionIsTokenRange); 611 return createExpansionLocImpl(Info, TokLength, LoadedID, LoadedOffset); 612 } 613 614 SourceLocation SourceManager::createTokenSplitLoc(SourceLocation Spelling, 615 SourceLocation TokenStart, 616 SourceLocation TokenEnd) { 617 assert(getFileID(TokenStart) == getFileID(TokenEnd) && 618 "token spans multiple files"); 619 return createExpansionLocImpl( 620 ExpansionInfo::createForTokenSplit(Spelling, TokenStart, TokenEnd), 621 TokenEnd.getOffset() - TokenStart.getOffset()); 622 } 623 624 SourceLocation 625 SourceManager::createExpansionLocImpl(const ExpansionInfo &Info, 626 unsigned TokLength, 627 int LoadedID, 628 unsigned LoadedOffset) { 629 if (LoadedID < 0) { 630 assert(LoadedID != -1 && "Loading sentinel FileID"); 631 unsigned Index = unsigned(-LoadedID) - 2; 632 assert(Index < LoadedSLocEntryTable.size() && "FileID out of range"); 633 assert(!SLocEntryLoaded[Index] && "FileID already loaded"); 634 LoadedSLocEntryTable[Index] = SLocEntry::get(LoadedOffset, Info); 635 SLocEntryLoaded[Index] = true; 636 return SourceLocation::getMacroLoc(LoadedOffset); 637 } 638 LocalSLocEntryTable.push_back(SLocEntry::get(NextLocalOffset, Info)); 639 assert(NextLocalOffset + TokLength + 1 > NextLocalOffset && 640 NextLocalOffset + TokLength + 1 <= CurrentLoadedOffset && 641 "Ran out of source locations!"); 642 // See createFileID for that +1. 643 NextLocalOffset += TokLength + 1; 644 return SourceLocation::getMacroLoc(NextLocalOffset - (TokLength + 1)); 645 } 646 647 const llvm::MemoryBuffer * 648 SourceManager::getMemoryBufferForFile(const FileEntry *File, bool *Invalid) { 649 const SrcMgr::ContentCache *IR = getOrCreateContentCache(File); 650 assert(IR && "getOrCreateContentCache() cannot return NULL"); 651 return IR->getBuffer(Diag, *this, SourceLocation(), Invalid); 652 } 653 654 void SourceManager::overrideFileContents(const FileEntry *SourceFile, 655 llvm::MemoryBuffer *Buffer, 656 bool DoNotFree) { 657 const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile); 658 assert(IR && "getOrCreateContentCache() cannot return NULL"); 659 660 const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(Buffer, DoNotFree); 661 const_cast<SrcMgr::ContentCache *>(IR)->BufferOverridden = true; 662 663 getOverriddenFilesInfo().OverriddenFilesWithBuffer.insert(SourceFile); 664 } 665 666 void SourceManager::overrideFileContents(const FileEntry *SourceFile, 667 const FileEntry *NewFile) { 668 assert(SourceFile->getSize() == NewFile->getSize() && 669 "Different sizes, use the FileManager to create a virtual file with " 670 "the correct size"); 671 assert(FileInfos.count(SourceFile) == 0 && 672 "This function should be called at the initialization stage, before " 673 "any parsing occurs."); 674 getOverriddenFilesInfo().OverriddenFiles[SourceFile] = NewFile; 675 } 676 677 void SourceManager::disableFileContentsOverride(const FileEntry *File) { 678 if (!isFileOverridden(File)) 679 return; 680 681 const SrcMgr::ContentCache *IR = getOrCreateContentCache(File); 682 const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(nullptr); 683 const_cast<SrcMgr::ContentCache *>(IR)->ContentsEntry = IR->OrigEntry; 684 685 assert(OverriddenFilesInfo); 686 OverriddenFilesInfo->OverriddenFiles.erase(File); 687 OverriddenFilesInfo->OverriddenFilesWithBuffer.erase(File); 688 } 689 690 void SourceManager::setFileIsTransient(const FileEntry *File) { 691 const SrcMgr::ContentCache *CC = getOrCreateContentCache(File); 692 const_cast<SrcMgr::ContentCache *>(CC)->IsTransient = true; 693 } 694 695 StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const { 696 bool MyInvalid = false; 697 const SLocEntry &SLoc = getSLocEntry(FID, &MyInvalid); 698 if (!SLoc.isFile() || MyInvalid) { 699 if (Invalid) 700 *Invalid = true; 701 return "<<<<<INVALID SOURCE LOCATION>>>>>"; 702 } 703 704 const llvm::MemoryBuffer *Buf = SLoc.getFile().getContentCache()->getBuffer( 705 Diag, *this, SourceLocation(), &MyInvalid); 706 if (Invalid) 707 *Invalid = MyInvalid; 708 709 if (MyInvalid) 710 return "<<<<<INVALID SOURCE LOCATION>>>>>"; 711 712 return Buf->getBuffer(); 713 } 714 715 //===----------------------------------------------------------------------===// 716 // SourceLocation manipulation methods. 717 //===----------------------------------------------------------------------===// 718 719 /// Return the FileID for a SourceLocation. 720 /// 721 /// This is the cache-miss path of getFileID. Not as hot as that function, but 722 /// still very important. It is responsible for finding the entry in the 723 /// SLocEntry tables that contains the specified location. 724 FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const { 725 if (!SLocOffset) 726 return FileID::get(0); 727 728 // Now it is time to search for the correct file. See where the SLocOffset 729 // sits in the global view and consult local or loaded buffers for it. 730 if (SLocOffset < NextLocalOffset) 731 return getFileIDLocal(SLocOffset); 732 return getFileIDLoaded(SLocOffset); 733 } 734 735 /// Return the FileID for a SourceLocation with a low offset. 736 /// 737 /// This function knows that the SourceLocation is in a local buffer, not a 738 /// loaded one. 739 FileID SourceManager::getFileIDLocal(unsigned SLocOffset) const { 740 assert(SLocOffset < NextLocalOffset && "Bad function choice"); 741 742 // After the first and second level caches, I see two common sorts of 743 // behavior: 1) a lot of searched FileID's are "near" the cached file 744 // location or are "near" the cached expansion location. 2) others are just 745 // completely random and may be a very long way away. 746 // 747 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly 748 // then we fall back to a less cache efficient, but more scalable, binary 749 // search to find the location. 750 751 // See if this is near the file point - worst case we start scanning from the 752 // most newly created FileID. 753 const SrcMgr::SLocEntry *I; 754 755 if (LastFileIDLookup.ID < 0 || 756 LocalSLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) { 757 // Neither loc prunes our search. 758 I = LocalSLocEntryTable.end(); 759 } else { 760 // Perhaps it is near the file point. 761 I = LocalSLocEntryTable.begin()+LastFileIDLookup.ID; 762 } 763 764 // Find the FileID that contains this. "I" is an iterator that points to a 765 // FileID whose offset is known to be larger than SLocOffset. 766 unsigned NumProbes = 0; 767 while (true) { 768 --I; 769 if (I->getOffset() <= SLocOffset) { 770 FileID Res = FileID::get(int(I - LocalSLocEntryTable.begin())); 771 772 // If this isn't an expansion, remember it. We have good locality across 773 // FileID lookups. 774 if (!I->isExpansion()) 775 LastFileIDLookup = Res; 776 NumLinearScans += NumProbes+1; 777 return Res; 778 } 779 if (++NumProbes == 8) 780 break; 781 } 782 783 // Convert "I" back into an index. We know that it is an entry whose index is 784 // larger than the offset we are looking for. 785 unsigned GreaterIndex = I - LocalSLocEntryTable.begin(); 786 // LessIndex - This is the lower bound of the range that we're searching. 787 // We know that the offset corresponding to the FileID is is less than 788 // SLocOffset. 789 unsigned LessIndex = 0; 790 NumProbes = 0; 791 while (true) { 792 bool Invalid = false; 793 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex; 794 unsigned MidOffset = getLocalSLocEntry(MiddleIndex, &Invalid).getOffset(); 795 if (Invalid) 796 return FileID::get(0); 797 798 ++NumProbes; 799 800 // If the offset of the midpoint is too large, chop the high side of the 801 // range to the midpoint. 802 if (MidOffset > SLocOffset) { 803 GreaterIndex = MiddleIndex; 804 continue; 805 } 806 807 // If the middle index contains the value, succeed and return. 808 // FIXME: This could be made faster by using a function that's aware of 809 // being in the local area. 810 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) { 811 FileID Res = FileID::get(MiddleIndex); 812 813 // If this isn't a macro expansion, remember it. We have good locality 814 // across FileID lookups. 815 if (!LocalSLocEntryTable[MiddleIndex].isExpansion()) 816 LastFileIDLookup = Res; 817 NumBinaryProbes += NumProbes; 818 return Res; 819 } 820 821 // Otherwise, move the low-side up to the middle index. 822 LessIndex = MiddleIndex; 823 } 824 } 825 826 /// Return the FileID for a SourceLocation with a high offset. 827 /// 828 /// This function knows that the SourceLocation is in a loaded buffer, not a 829 /// local one. 830 FileID SourceManager::getFileIDLoaded(unsigned SLocOffset) const { 831 // Sanity checking, otherwise a bug may lead to hanging in release build. 832 if (SLocOffset < CurrentLoadedOffset) { 833 assert(0 && "Invalid SLocOffset or bad function choice"); 834 return FileID(); 835 } 836 837 // Essentially the same as the local case, but the loaded array is sorted 838 // in the other direction. 839 840 // First do a linear scan from the last lookup position, if possible. 841 unsigned I; 842 int LastID = LastFileIDLookup.ID; 843 if (LastID >= 0 || getLoadedSLocEntryByID(LastID).getOffset() < SLocOffset) 844 I = 0; 845 else 846 I = (-LastID - 2) + 1; 847 848 unsigned NumProbes; 849 for (NumProbes = 0; NumProbes < 8; ++NumProbes, ++I) { 850 // Make sure the entry is loaded! 851 const SrcMgr::SLocEntry &E = getLoadedSLocEntry(I); 852 if (E.getOffset() <= SLocOffset) { 853 FileID Res = FileID::get(-int(I) - 2); 854 855 if (!E.isExpansion()) 856 LastFileIDLookup = Res; 857 NumLinearScans += NumProbes + 1; 858 return Res; 859 } 860 } 861 862 // Linear scan failed. Do the binary search. Note the reverse sorting of the 863 // table: GreaterIndex is the one where the offset is greater, which is 864 // actually a lower index! 865 unsigned GreaterIndex = I; 866 unsigned LessIndex = LoadedSLocEntryTable.size(); 867 NumProbes = 0; 868 while (true) { 869 ++NumProbes; 870 unsigned MiddleIndex = (LessIndex - GreaterIndex) / 2 + GreaterIndex; 871 const SrcMgr::SLocEntry &E = getLoadedSLocEntry(MiddleIndex); 872 if (E.getOffset() == 0) 873 return FileID(); // invalid entry. 874 875 ++NumProbes; 876 877 if (E.getOffset() > SLocOffset) { 878 // Sanity checking, otherwise a bug may lead to hanging in release build. 879 if (GreaterIndex == MiddleIndex) { 880 assert(0 && "binary search missed the entry"); 881 return FileID(); 882 } 883 GreaterIndex = MiddleIndex; 884 continue; 885 } 886 887 if (isOffsetInFileID(FileID::get(-int(MiddleIndex) - 2), SLocOffset)) { 888 FileID Res = FileID::get(-int(MiddleIndex) - 2); 889 if (!E.isExpansion()) 890 LastFileIDLookup = Res; 891 NumBinaryProbes += NumProbes; 892 return Res; 893 } 894 895 // Sanity checking, otherwise a bug may lead to hanging in release build. 896 if (LessIndex == MiddleIndex) { 897 assert(0 && "binary search missed the entry"); 898 return FileID(); 899 } 900 LessIndex = MiddleIndex; 901 } 902 } 903 904 SourceLocation SourceManager:: 905 getExpansionLocSlowCase(SourceLocation Loc) const { 906 do { 907 // Note: If Loc indicates an offset into a token that came from a macro 908 // expansion (e.g. the 5th character of the token) we do not want to add 909 // this offset when going to the expansion location. The expansion 910 // location is the macro invocation, which the offset has nothing to do 911 // with. This is unlike when we get the spelling loc, because the offset 912 // directly correspond to the token whose spelling we're inspecting. 913 Loc = getSLocEntry(getFileID(Loc)).getExpansion().getExpansionLocStart(); 914 } while (!Loc.isFileID()); 915 916 return Loc; 917 } 918 919 SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const { 920 do { 921 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); 922 Loc = getSLocEntry(LocInfo.first).getExpansion().getSpellingLoc(); 923 Loc = Loc.getLocWithOffset(LocInfo.second); 924 } while (!Loc.isFileID()); 925 return Loc; 926 } 927 928 SourceLocation SourceManager::getFileLocSlowCase(SourceLocation Loc) const { 929 do { 930 if (isMacroArgExpansion(Loc)) 931 Loc = getImmediateSpellingLoc(Loc); 932 else 933 Loc = getImmediateExpansionRange(Loc).getBegin(); 934 } while (!Loc.isFileID()); 935 return Loc; 936 } 937 938 939 std::pair<FileID, unsigned> 940 SourceManager::getDecomposedExpansionLocSlowCase( 941 const SrcMgr::SLocEntry *E) const { 942 // If this is an expansion record, walk through all the expansion points. 943 FileID FID; 944 SourceLocation Loc; 945 unsigned Offset; 946 do { 947 Loc = E->getExpansion().getExpansionLocStart(); 948 949 FID = getFileID(Loc); 950 E = &getSLocEntry(FID); 951 Offset = Loc.getOffset()-E->getOffset(); 952 } while (!Loc.isFileID()); 953 954 return std::make_pair(FID, Offset); 955 } 956 957 std::pair<FileID, unsigned> 958 SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, 959 unsigned Offset) const { 960 // If this is an expansion record, walk through all the expansion points. 961 FileID FID; 962 SourceLocation Loc; 963 do { 964 Loc = E->getExpansion().getSpellingLoc(); 965 Loc = Loc.getLocWithOffset(Offset); 966 967 FID = getFileID(Loc); 968 E = &getSLocEntry(FID); 969 Offset = Loc.getOffset()-E->getOffset(); 970 } while (!Loc.isFileID()); 971 972 return std::make_pair(FID, Offset); 973 } 974 975 /// getImmediateSpellingLoc - Given a SourceLocation object, return the 976 /// spelling location referenced by the ID. This is the first level down 977 /// towards the place where the characters that make up the lexed token can be 978 /// found. This should not generally be used by clients. 979 SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{ 980 if (Loc.isFileID()) return Loc; 981 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); 982 Loc = getSLocEntry(LocInfo.first).getExpansion().getSpellingLoc(); 983 return Loc.getLocWithOffset(LocInfo.second); 984 } 985 986 /// getImmediateExpansionRange - Loc is required to be an expansion location. 987 /// Return the start/end of the expansion information. 988 CharSourceRange 989 SourceManager::getImmediateExpansionRange(SourceLocation Loc) const { 990 assert(Loc.isMacroID() && "Not a macro expansion loc!"); 991 const ExpansionInfo &Expansion = getSLocEntry(getFileID(Loc)).getExpansion(); 992 return Expansion.getExpansionLocRange(); 993 } 994 995 SourceLocation SourceManager::getTopMacroCallerLoc(SourceLocation Loc) const { 996 while (isMacroArgExpansion(Loc)) 997 Loc = getImmediateSpellingLoc(Loc); 998 return Loc; 999 } 1000 1001 /// getExpansionRange - Given a SourceLocation object, return the range of 1002 /// tokens covered by the expansion in the ultimate file. 1003 CharSourceRange SourceManager::getExpansionRange(SourceLocation Loc) const { 1004 if (Loc.isFileID()) 1005 return CharSourceRange(SourceRange(Loc, Loc), true); 1006 1007 CharSourceRange Res = getImmediateExpansionRange(Loc); 1008 1009 // Fully resolve the start and end locations to their ultimate expansion 1010 // points. 1011 while (!Res.getBegin().isFileID()) 1012 Res.setBegin(getImmediateExpansionRange(Res.getBegin()).getBegin()); 1013 while (!Res.getEnd().isFileID()) { 1014 CharSourceRange EndRange = getImmediateExpansionRange(Res.getEnd()); 1015 Res.setEnd(EndRange.getEnd()); 1016 Res.setTokenRange(EndRange.isTokenRange()); 1017 } 1018 return Res; 1019 } 1020 1021 bool SourceManager::isMacroArgExpansion(SourceLocation Loc, 1022 SourceLocation *StartLoc) const { 1023 if (!Loc.isMacroID()) return false; 1024 1025 FileID FID = getFileID(Loc); 1026 const SrcMgr::ExpansionInfo &Expansion = getSLocEntry(FID).getExpansion(); 1027 if (!Expansion.isMacroArgExpansion()) return false; 1028 1029 if (StartLoc) 1030 *StartLoc = Expansion.getExpansionLocStart(); 1031 return true; 1032 } 1033 1034 bool SourceManager::isMacroBodyExpansion(SourceLocation Loc) const { 1035 if (!Loc.isMacroID()) return false; 1036 1037 FileID FID = getFileID(Loc); 1038 const SrcMgr::ExpansionInfo &Expansion = getSLocEntry(FID).getExpansion(); 1039 return Expansion.isMacroBodyExpansion(); 1040 } 1041 1042 bool SourceManager::isAtStartOfImmediateMacroExpansion(SourceLocation Loc, 1043 SourceLocation *MacroBegin) const { 1044 assert(Loc.isValid() && Loc.isMacroID() && "Expected a valid macro loc"); 1045 1046 std::pair<FileID, unsigned> DecompLoc = getDecomposedLoc(Loc); 1047 if (DecompLoc.second > 0) 1048 return false; // Does not point at the start of expansion range. 1049 1050 bool Invalid = false; 1051 const SrcMgr::ExpansionInfo &ExpInfo = 1052 getSLocEntry(DecompLoc.first, &Invalid).getExpansion(); 1053 if (Invalid) 1054 return false; 1055 SourceLocation ExpLoc = ExpInfo.getExpansionLocStart(); 1056 1057 if (ExpInfo.isMacroArgExpansion()) { 1058 // For macro argument expansions, check if the previous FileID is part of 1059 // the same argument expansion, in which case this Loc is not at the 1060 // beginning of the expansion. 1061 FileID PrevFID = getPreviousFileID(DecompLoc.first); 1062 if (!PrevFID.isInvalid()) { 1063 const SrcMgr::SLocEntry &PrevEntry = getSLocEntry(PrevFID, &Invalid); 1064 if (Invalid) 1065 return false; 1066 if (PrevEntry.isExpansion() && 1067 PrevEntry.getExpansion().getExpansionLocStart() == ExpLoc) 1068 return false; 1069 } 1070 } 1071 1072 if (MacroBegin) 1073 *MacroBegin = ExpLoc; 1074 return true; 1075 } 1076 1077 bool SourceManager::isAtEndOfImmediateMacroExpansion(SourceLocation Loc, 1078 SourceLocation *MacroEnd) const { 1079 assert(Loc.isValid() && Loc.isMacroID() && "Expected a valid macro loc"); 1080 1081 FileID FID = getFileID(Loc); 1082 SourceLocation NextLoc = Loc.getLocWithOffset(1); 1083 if (isInFileID(NextLoc, FID)) 1084 return false; // Does not point at the end of expansion range. 1085 1086 bool Invalid = false; 1087 const SrcMgr::ExpansionInfo &ExpInfo = 1088 getSLocEntry(FID, &Invalid).getExpansion(); 1089 if (Invalid) 1090 return false; 1091 1092 if (ExpInfo.isMacroArgExpansion()) { 1093 // For macro argument expansions, check if the next FileID is part of the 1094 // same argument expansion, in which case this Loc is not at the end of the 1095 // expansion. 1096 FileID NextFID = getNextFileID(FID); 1097 if (!NextFID.isInvalid()) { 1098 const SrcMgr::SLocEntry &NextEntry = getSLocEntry(NextFID, &Invalid); 1099 if (Invalid) 1100 return false; 1101 if (NextEntry.isExpansion() && 1102 NextEntry.getExpansion().getExpansionLocStart() == 1103 ExpInfo.getExpansionLocStart()) 1104 return false; 1105 } 1106 } 1107 1108 if (MacroEnd) 1109 *MacroEnd = ExpInfo.getExpansionLocEnd(); 1110 return true; 1111 } 1112 1113 //===----------------------------------------------------------------------===// 1114 // Queries about the code at a SourceLocation. 1115 //===----------------------------------------------------------------------===// 1116 1117 /// getCharacterData - Return a pointer to the start of the specified location 1118 /// in the appropriate MemoryBuffer. 1119 const char *SourceManager::getCharacterData(SourceLocation SL, 1120 bool *Invalid) const { 1121 // Note that this is a hot function in the getSpelling() path, which is 1122 // heavily used by -E mode. 1123 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL); 1124 1125 // Note that calling 'getBuffer()' may lazily page in a source file. 1126 bool CharDataInvalid = false; 1127 const SLocEntry &Entry = getSLocEntry(LocInfo.first, &CharDataInvalid); 1128 if (CharDataInvalid || !Entry.isFile()) { 1129 if (Invalid) 1130 *Invalid = true; 1131 1132 return "<<<<INVALID BUFFER>>>>"; 1133 } 1134 const llvm::MemoryBuffer *Buffer = 1135 Entry.getFile().getContentCache()->getBuffer( 1136 Diag, *this, SourceLocation(), &CharDataInvalid); 1137 if (Invalid) 1138 *Invalid = CharDataInvalid; 1139 return Buffer->getBufferStart() + (CharDataInvalid? 0 : LocInfo.second); 1140 } 1141 1142 /// getColumnNumber - Return the column # for the specified file position. 1143 /// this is significantly cheaper to compute than the line number. 1144 unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos, 1145 bool *Invalid) const { 1146 bool MyInvalid = false; 1147 const llvm::MemoryBuffer *MemBuf = getBuffer(FID, &MyInvalid); 1148 if (Invalid) 1149 *Invalid = MyInvalid; 1150 1151 if (MyInvalid) 1152 return 1; 1153 1154 // It is okay to request a position just past the end of the buffer. 1155 if (FilePos > MemBuf->getBufferSize()) { 1156 if (Invalid) 1157 *Invalid = true; 1158 return 1; 1159 } 1160 1161 const char *Buf = MemBuf->getBufferStart(); 1162 // See if we just calculated the line number for this FilePos and can use 1163 // that to lookup the start of the line instead of searching for it. 1164 if (LastLineNoFileIDQuery == FID && 1165 LastLineNoContentCache->SourceLineCache != nullptr && 1166 LastLineNoResult < LastLineNoContentCache->NumLines) { 1167 unsigned *SourceLineCache = LastLineNoContentCache->SourceLineCache; 1168 unsigned LineStart = SourceLineCache[LastLineNoResult - 1]; 1169 unsigned LineEnd = SourceLineCache[LastLineNoResult]; 1170 if (FilePos >= LineStart && FilePos < LineEnd) { 1171 // LineEnd is the LineStart of the next line. 1172 // A line ends with separator LF or CR+LF on Windows. 1173 // FilePos might point to the last separator, 1174 // but we need a column number at most 1 + the last column. 1175 if (FilePos + 1 == LineEnd && FilePos > LineStart) { 1176 if (Buf[FilePos - 1] == '\r' || Buf[FilePos - 1] == '\n') 1177 --FilePos; 1178 } 1179 return FilePos - LineStart + 1; 1180 } 1181 } 1182 1183 unsigned LineStart = FilePos; 1184 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r') 1185 --LineStart; 1186 return FilePos-LineStart+1; 1187 } 1188 1189 // isInvalid - Return the result of calling loc.isInvalid(), and 1190 // if Invalid is not null, set its value to same. 1191 template<typename LocType> 1192 static bool isInvalid(LocType Loc, bool *Invalid) { 1193 bool MyInvalid = Loc.isInvalid(); 1194 if (Invalid) 1195 *Invalid = MyInvalid; 1196 return MyInvalid; 1197 } 1198 1199 unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc, 1200 bool *Invalid) const { 1201 if (isInvalid(Loc, Invalid)) return 0; 1202 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc); 1203 return getColumnNumber(LocInfo.first, LocInfo.second, Invalid); 1204 } 1205 1206 unsigned SourceManager::getExpansionColumnNumber(SourceLocation Loc, 1207 bool *Invalid) const { 1208 if (isInvalid(Loc, Invalid)) return 0; 1209 std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); 1210 return getColumnNumber(LocInfo.first, LocInfo.second, Invalid); 1211 } 1212 1213 unsigned SourceManager::getPresumedColumnNumber(SourceLocation Loc, 1214 bool *Invalid) const { 1215 PresumedLoc PLoc = getPresumedLoc(Loc); 1216 if (isInvalid(PLoc, Invalid)) return 0; 1217 return PLoc.getColumn(); 1218 } 1219 1220 #ifdef __SSE2__ 1221 #include <emmintrin.h> 1222 #endif 1223 1224 static LLVM_ATTRIBUTE_NOINLINE void 1225 ComputeLineNumbers(DiagnosticsEngine &Diag, ContentCache *FI, 1226 llvm::BumpPtrAllocator &Alloc, 1227 const SourceManager &SM, bool &Invalid); 1228 static void ComputeLineNumbers(DiagnosticsEngine &Diag, ContentCache *FI, 1229 llvm::BumpPtrAllocator &Alloc, 1230 const SourceManager &SM, bool &Invalid) { 1231 // Note that calling 'getBuffer()' may lazily page in the file. 1232 const MemoryBuffer *Buffer = 1233 FI->getBuffer(Diag, SM, SourceLocation(), &Invalid); 1234 if (Invalid) 1235 return; 1236 1237 // Find the file offsets of all of the *physical* source lines. This does 1238 // not look at trigraphs, escaped newlines, or anything else tricky. 1239 SmallVector<unsigned, 256> LineOffsets; 1240 1241 // Line #1 starts at char 0. 1242 LineOffsets.push_back(0); 1243 1244 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart(); 1245 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd(); 1246 unsigned I = 0; 1247 while (true) { 1248 // Skip over the contents of the line. 1249 while (Buf[I] != '\n' && Buf[I] != '\r' && Buf[I] != '\0') 1250 ++I; 1251 1252 if (Buf[I] == '\n' || Buf[I] == '\r') { 1253 // If this is \r\n, skip both characters. 1254 if (Buf[I] == '\r' && Buf[I+1] == '\n') 1255 ++I; 1256 ++I; 1257 LineOffsets.push_back(I); 1258 } else { 1259 // Otherwise, this is a NUL. If end of file, exit. 1260 if (Buf+I == End) break; 1261 ++I; 1262 } 1263 } 1264 1265 // Copy the offsets into the FileInfo structure. 1266 FI->NumLines = LineOffsets.size(); 1267 FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size()); 1268 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache); 1269 } 1270 1271 /// getLineNumber - Given a SourceLocation, return the spelling line number 1272 /// for the position indicated. This requires building and caching a table of 1273 /// line offsets for the MemoryBuffer, so this is not cheap: use only when 1274 /// about to emit a diagnostic. 1275 unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos, 1276 bool *Invalid) const { 1277 if (FID.isInvalid()) { 1278 if (Invalid) 1279 *Invalid = true; 1280 return 1; 1281 } 1282 1283 ContentCache *Content; 1284 if (LastLineNoFileIDQuery == FID) 1285 Content = LastLineNoContentCache; 1286 else { 1287 bool MyInvalid = false; 1288 const SLocEntry &Entry = getSLocEntry(FID, &MyInvalid); 1289 if (MyInvalid || !Entry.isFile()) { 1290 if (Invalid) 1291 *Invalid = true; 1292 return 1; 1293 } 1294 1295 Content = const_cast<ContentCache*>(Entry.getFile().getContentCache()); 1296 } 1297 1298 // If this is the first use of line information for this buffer, compute the 1299 /// SourceLineCache for it on demand. 1300 if (!Content->SourceLineCache) { 1301 bool MyInvalid = false; 1302 ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid); 1303 if (Invalid) 1304 *Invalid = MyInvalid; 1305 if (MyInvalid) 1306 return 1; 1307 } else if (Invalid) 1308 *Invalid = false; 1309 1310 // Okay, we know we have a line number table. Do a binary search to find the 1311 // line number that this character position lands on. 1312 unsigned *SourceLineCache = Content->SourceLineCache; 1313 unsigned *SourceLineCacheStart = SourceLineCache; 1314 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines; 1315 1316 unsigned QueriedFilePos = FilePos+1; 1317 1318 // FIXME: I would like to be convinced that this code is worth being as 1319 // complicated as it is, binary search isn't that slow. 1320 // 1321 // If it is worth being optimized, then in my opinion it could be more 1322 // performant, simpler, and more obviously correct by just "galloping" outward 1323 // from the queried file position. In fact, this could be incorporated into a 1324 // generic algorithm such as lower_bound_with_hint. 1325 // 1326 // If someone gives me a test case where this matters, and I will do it! - DWD 1327 1328 // If the previous query was to the same file, we know both the file pos from 1329 // that query and the line number returned. This allows us to narrow the 1330 // search space from the entire file to something near the match. 1331 if (LastLineNoFileIDQuery == FID) { 1332 if (QueriedFilePos >= LastLineNoFilePos) { 1333 // FIXME: Potential overflow? 1334 SourceLineCache = SourceLineCache+LastLineNoResult-1; 1335 1336 // The query is likely to be nearby the previous one. Here we check to 1337 // see if it is within 5, 10 or 20 lines. It can be far away in cases 1338 // where big comment blocks and vertical whitespace eat up lines but 1339 // contribute no tokens. 1340 if (SourceLineCache+5 < SourceLineCacheEnd) { 1341 if (SourceLineCache[5] > QueriedFilePos) 1342 SourceLineCacheEnd = SourceLineCache+5; 1343 else if (SourceLineCache+10 < SourceLineCacheEnd) { 1344 if (SourceLineCache[10] > QueriedFilePos) 1345 SourceLineCacheEnd = SourceLineCache+10; 1346 else if (SourceLineCache+20 < SourceLineCacheEnd) { 1347 if (SourceLineCache[20] > QueriedFilePos) 1348 SourceLineCacheEnd = SourceLineCache+20; 1349 } 1350 } 1351 } 1352 } else { 1353 if (LastLineNoResult < Content->NumLines) 1354 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1; 1355 } 1356 } 1357 1358 unsigned *Pos 1359 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos); 1360 unsigned LineNo = Pos-SourceLineCacheStart; 1361 1362 LastLineNoFileIDQuery = FID; 1363 LastLineNoContentCache = Content; 1364 LastLineNoFilePos = QueriedFilePos; 1365 LastLineNoResult = LineNo; 1366 return LineNo; 1367 } 1368 1369 unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc, 1370 bool *Invalid) const { 1371 if (isInvalid(Loc, Invalid)) return 0; 1372 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc); 1373 return getLineNumber(LocInfo.first, LocInfo.second); 1374 } 1375 unsigned SourceManager::getExpansionLineNumber(SourceLocation Loc, 1376 bool *Invalid) const { 1377 if (isInvalid(Loc, Invalid)) return 0; 1378 std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); 1379 return getLineNumber(LocInfo.first, LocInfo.second); 1380 } 1381 unsigned SourceManager::getPresumedLineNumber(SourceLocation Loc, 1382 bool *Invalid) const { 1383 PresumedLoc PLoc = getPresumedLoc(Loc); 1384 if (isInvalid(PLoc, Invalid)) return 0; 1385 return PLoc.getLine(); 1386 } 1387 1388 /// getFileCharacteristic - return the file characteristic of the specified 1389 /// source location, indicating whether this is a normal file, a system 1390 /// header, or an "implicit extern C" system header. 1391 /// 1392 /// This state can be modified with flags on GNU linemarker directives like: 1393 /// # 4 "foo.h" 3 1394 /// which changes all source locations in the current file after that to be 1395 /// considered to be from a system header. 1396 SrcMgr::CharacteristicKind 1397 SourceManager::getFileCharacteristic(SourceLocation Loc) const { 1398 assert(Loc.isValid() && "Can't get file characteristic of invalid loc!"); 1399 std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); 1400 bool Invalid = false; 1401 const SLocEntry &SEntry = getSLocEntry(LocInfo.first, &Invalid); 1402 if (Invalid || !SEntry.isFile()) 1403 return C_User; 1404 1405 const SrcMgr::FileInfo &FI = SEntry.getFile(); 1406 1407 // If there are no #line directives in this file, just return the whole-file 1408 // state. 1409 if (!FI.hasLineDirectives()) 1410 return FI.getFileCharacteristic(); 1411 1412 assert(LineTable && "Can't have linetable entries without a LineTable!"); 1413 // See if there is a #line directive before the location. 1414 const LineEntry *Entry = 1415 LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second); 1416 1417 // If this is before the first line marker, use the file characteristic. 1418 if (!Entry) 1419 return FI.getFileCharacteristic(); 1420 1421 return Entry->FileKind; 1422 } 1423 1424 /// Return the filename or buffer identifier of the buffer the location is in. 1425 /// Note that this name does not respect \#line directives. Use getPresumedLoc 1426 /// for normal clients. 1427 StringRef SourceManager::getBufferName(SourceLocation Loc, 1428 bool *Invalid) const { 1429 if (isInvalid(Loc, Invalid)) return "<invalid loc>"; 1430 1431 return getBuffer(getFileID(Loc), Invalid)->getBufferIdentifier(); 1432 } 1433 1434 /// getPresumedLoc - This method returns the "presumed" location of a 1435 /// SourceLocation specifies. A "presumed location" can be modified by \#line 1436 /// or GNU line marker directives. This provides a view on the data that a 1437 /// user should see in diagnostics, for example. 1438 /// 1439 /// Note that a presumed location is always given as the expansion point of an 1440 /// expansion location, not at the spelling location. 1441 PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc, 1442 bool UseLineDirectives) const { 1443 if (Loc.isInvalid()) return PresumedLoc(); 1444 1445 // Presumed locations are always for expansion points. 1446 std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); 1447 1448 bool Invalid = false; 1449 const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid); 1450 if (Invalid || !Entry.isFile()) 1451 return PresumedLoc(); 1452 1453 const SrcMgr::FileInfo &FI = Entry.getFile(); 1454 const SrcMgr::ContentCache *C = FI.getContentCache(); 1455 1456 // To get the source name, first consult the FileEntry (if one exists) 1457 // before the MemBuffer as this will avoid unnecessarily paging in the 1458 // MemBuffer. 1459 FileID FID = LocInfo.first; 1460 StringRef Filename; 1461 if (C->OrigEntry) 1462 Filename = C->OrigEntry->getName(); 1463 else 1464 Filename = C->getBuffer(Diag, *this)->getBufferIdentifier(); 1465 1466 unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second, &Invalid); 1467 if (Invalid) 1468 return PresumedLoc(); 1469 unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second, &Invalid); 1470 if (Invalid) 1471 return PresumedLoc(); 1472 1473 SourceLocation IncludeLoc = FI.getIncludeLoc(); 1474 1475 // If we have #line directives in this file, update and overwrite the physical 1476 // location info if appropriate. 1477 if (UseLineDirectives && FI.hasLineDirectives()) { 1478 assert(LineTable && "Can't have linetable entries without a LineTable!"); 1479 // See if there is a #line directive before this. If so, get it. 1480 if (const LineEntry *Entry = 1481 LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second)) { 1482 // If the LineEntry indicates a filename, use it. 1483 if (Entry->FilenameID != -1) { 1484 Filename = LineTable->getFilename(Entry->FilenameID); 1485 // The contents of files referenced by #line are not in the 1486 // SourceManager 1487 FID = FileID::get(0); 1488 } 1489 1490 // Use the line number specified by the LineEntry. This line number may 1491 // be multiple lines down from the line entry. Add the difference in 1492 // physical line numbers from the query point and the line marker to the 1493 // total. 1494 unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset); 1495 LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1); 1496 1497 // Note that column numbers are not molested by line markers. 1498 1499 // Handle virtual #include manipulation. 1500 if (Entry->IncludeOffset) { 1501 IncludeLoc = getLocForStartOfFile(LocInfo.first); 1502 IncludeLoc = IncludeLoc.getLocWithOffset(Entry->IncludeOffset); 1503 } 1504 } 1505 } 1506 1507 return PresumedLoc(Filename.data(), FID, LineNo, ColNo, IncludeLoc); 1508 } 1509 1510 /// Returns whether the PresumedLoc for a given SourceLocation is 1511 /// in the main file. 1512 /// 1513 /// This computes the "presumed" location for a SourceLocation, then checks 1514 /// whether it came from a file other than the main file. This is different 1515 /// from isWrittenInMainFile() because it takes line marker directives into 1516 /// account. 1517 bool SourceManager::isInMainFile(SourceLocation Loc) const { 1518 if (Loc.isInvalid()) return false; 1519 1520 // Presumed locations are always for expansion points. 1521 std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc); 1522 1523 bool Invalid = false; 1524 const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid); 1525 if (Invalid || !Entry.isFile()) 1526 return false; 1527 1528 const SrcMgr::FileInfo &FI = Entry.getFile(); 1529 1530 // Check if there is a line directive for this location. 1531 if (FI.hasLineDirectives()) 1532 if (const LineEntry *Entry = 1533 LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second)) 1534 if (Entry->IncludeOffset) 1535 return false; 1536 1537 return FI.getIncludeLoc().isInvalid(); 1538 } 1539 1540 /// The size of the SLocEntry that \p FID represents. 1541 unsigned SourceManager::getFileIDSize(FileID FID) const { 1542 bool Invalid = false; 1543 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 1544 if (Invalid) 1545 return 0; 1546 1547 int ID = FID.ID; 1548 unsigned NextOffset; 1549 if ((ID > 0 && unsigned(ID+1) == local_sloc_entry_size())) 1550 NextOffset = getNextLocalOffset(); 1551 else if (ID+1 == -1) 1552 NextOffset = MaxLoadedOffset; 1553 else 1554 NextOffset = getSLocEntry(FileID::get(ID+1)).getOffset(); 1555 1556 return NextOffset - Entry.getOffset() - 1; 1557 } 1558 1559 //===----------------------------------------------------------------------===// 1560 // Other miscellaneous methods. 1561 //===----------------------------------------------------------------------===// 1562 1563 /// Retrieve the inode for the given file entry, if possible. 1564 /// 1565 /// This routine involves a system call, and therefore should only be used 1566 /// in non-performance-critical code. 1567 static Optional<llvm::sys::fs::UniqueID> 1568 getActualFileUID(const FileEntry *File) { 1569 if (!File) 1570 return None; 1571 1572 llvm::sys::fs::UniqueID ID; 1573 if (llvm::sys::fs::getUniqueID(File->getName(), ID)) 1574 return None; 1575 1576 return ID; 1577 } 1578 1579 /// Get the source location for the given file:line:col triplet. 1580 /// 1581 /// If the source file is included multiple times, the source location will 1582 /// be based upon an arbitrary inclusion. 1583 SourceLocation SourceManager::translateFileLineCol(const FileEntry *SourceFile, 1584 unsigned Line, 1585 unsigned Col) const { 1586 assert(SourceFile && "Null source file!"); 1587 assert(Line && Col && "Line and column should start from 1!"); 1588 1589 FileID FirstFID = translateFile(SourceFile); 1590 return translateLineCol(FirstFID, Line, Col); 1591 } 1592 1593 /// Get the FileID for the given file. 1594 /// 1595 /// If the source file is included multiple times, the FileID will be the 1596 /// first inclusion. 1597 FileID SourceManager::translateFile(const FileEntry *SourceFile) const { 1598 assert(SourceFile && "Null source file!"); 1599 1600 // Find the first file ID that corresponds to the given file. 1601 FileID FirstFID; 1602 1603 // First, check the main file ID, since it is common to look for a 1604 // location in the main file. 1605 Optional<llvm::sys::fs::UniqueID> SourceFileUID; 1606 Optional<StringRef> SourceFileName; 1607 if (MainFileID.isValid()) { 1608 bool Invalid = false; 1609 const SLocEntry &MainSLoc = getSLocEntry(MainFileID, &Invalid); 1610 if (Invalid) 1611 return FileID(); 1612 1613 if (MainSLoc.isFile()) { 1614 const ContentCache *MainContentCache 1615 = MainSLoc.getFile().getContentCache(); 1616 if (!MainContentCache || !MainContentCache->OrigEntry) { 1617 // Can't do anything 1618 } else if (MainContentCache->OrigEntry == SourceFile) { 1619 FirstFID = MainFileID; 1620 } else { 1621 // Fall back: check whether we have the same base name and inode 1622 // as the main file. 1623 const FileEntry *MainFile = MainContentCache->OrigEntry; 1624 SourceFileName = llvm::sys::path::filename(SourceFile->getName()); 1625 if (*SourceFileName == llvm::sys::path::filename(MainFile->getName())) { 1626 SourceFileUID = getActualFileUID(SourceFile); 1627 if (SourceFileUID) { 1628 if (Optional<llvm::sys::fs::UniqueID> MainFileUID = 1629 getActualFileUID(MainFile)) { 1630 if (*SourceFileUID == *MainFileUID) { 1631 FirstFID = MainFileID; 1632 SourceFile = MainFile; 1633 } 1634 } 1635 } 1636 } 1637 } 1638 } 1639 } 1640 1641 if (FirstFID.isInvalid()) { 1642 // The location we're looking for isn't in the main file; look 1643 // through all of the local source locations. 1644 for (unsigned I = 0, N = local_sloc_entry_size(); I != N; ++I) { 1645 bool Invalid = false; 1646 const SLocEntry &SLoc = getLocalSLocEntry(I, &Invalid); 1647 if (Invalid) 1648 return FileID(); 1649 1650 if (SLoc.isFile() && 1651 SLoc.getFile().getContentCache() && 1652 SLoc.getFile().getContentCache()->OrigEntry == SourceFile) { 1653 FirstFID = FileID::get(I); 1654 break; 1655 } 1656 } 1657 // If that still didn't help, try the modules. 1658 if (FirstFID.isInvalid()) { 1659 for (unsigned I = 0, N = loaded_sloc_entry_size(); I != N; ++I) { 1660 const SLocEntry &SLoc = getLoadedSLocEntry(I); 1661 if (SLoc.isFile() && 1662 SLoc.getFile().getContentCache() && 1663 SLoc.getFile().getContentCache()->OrigEntry == SourceFile) { 1664 FirstFID = FileID::get(-int(I) - 2); 1665 break; 1666 } 1667 } 1668 } 1669 } 1670 1671 // If we haven't found what we want yet, try again, but this time stat() 1672 // each of the files in case the files have changed since we originally 1673 // parsed the file. 1674 if (FirstFID.isInvalid() && 1675 (SourceFileName || 1676 (SourceFileName = llvm::sys::path::filename(SourceFile->getName()))) && 1677 (SourceFileUID || (SourceFileUID = getActualFileUID(SourceFile)))) { 1678 bool Invalid = false; 1679 for (unsigned I = 0, N = local_sloc_entry_size(); I != N; ++I) { 1680 FileID IFileID; 1681 IFileID.ID = I; 1682 const SLocEntry &SLoc = getSLocEntry(IFileID, &Invalid); 1683 if (Invalid) 1684 return FileID(); 1685 1686 if (SLoc.isFile()) { 1687 const ContentCache *FileContentCache 1688 = SLoc.getFile().getContentCache(); 1689 const FileEntry *Entry = FileContentCache ? FileContentCache->OrigEntry 1690 : nullptr; 1691 if (Entry && 1692 *SourceFileName == llvm::sys::path::filename(Entry->getName())) { 1693 if (Optional<llvm::sys::fs::UniqueID> EntryUID = 1694 getActualFileUID(Entry)) { 1695 if (*SourceFileUID == *EntryUID) { 1696 FirstFID = FileID::get(I); 1697 SourceFile = Entry; 1698 break; 1699 } 1700 } 1701 } 1702 } 1703 } 1704 } 1705 1706 (void) SourceFile; 1707 return FirstFID; 1708 } 1709 1710 /// Get the source location in \arg FID for the given line:col. 1711 /// Returns null location if \arg FID is not a file SLocEntry. 1712 SourceLocation SourceManager::translateLineCol(FileID FID, 1713 unsigned Line, 1714 unsigned Col) const { 1715 // Lines are used as a one-based index into a zero-based array. This assert 1716 // checks for possible buffer underruns. 1717 assert(Line && Col && "Line and column should start from 1!"); 1718 1719 if (FID.isInvalid()) 1720 return SourceLocation(); 1721 1722 bool Invalid = false; 1723 const SLocEntry &Entry = getSLocEntry(FID, &Invalid); 1724 if (Invalid) 1725 return SourceLocation(); 1726 1727 if (!Entry.isFile()) 1728 return SourceLocation(); 1729 1730 SourceLocation FileLoc = SourceLocation::getFileLoc(Entry.getOffset()); 1731 1732 if (Line == 1 && Col == 1) 1733 return FileLoc; 1734 1735 ContentCache *Content 1736 = const_cast<ContentCache *>(Entry.getFile().getContentCache()); 1737 if (!Content) 1738 return SourceLocation(); 1739 1740 // If this is the first use of line information for this buffer, compute the 1741 // SourceLineCache for it on demand. 1742 if (!Content->SourceLineCache) { 1743 bool MyInvalid = false; 1744 ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid); 1745 if (MyInvalid) 1746 return SourceLocation(); 1747 } 1748 1749 if (Line > Content->NumLines) { 1750 unsigned Size = Content->getBuffer(Diag, *this)->getBufferSize(); 1751 if (Size > 0) 1752 --Size; 1753 return FileLoc.getLocWithOffset(Size); 1754 } 1755 1756 const llvm::MemoryBuffer *Buffer = Content->getBuffer(Diag, *this); 1757 unsigned FilePos = Content->SourceLineCache[Line - 1]; 1758 const char *Buf = Buffer->getBufferStart() + FilePos; 1759 unsigned BufLength = Buffer->getBufferSize() - FilePos; 1760 if (BufLength == 0) 1761 return FileLoc.getLocWithOffset(FilePos); 1762 1763 unsigned i = 0; 1764 1765 // Check that the given column is valid. 1766 while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r') 1767 ++i; 1768 return FileLoc.getLocWithOffset(FilePos + i); 1769 } 1770 1771 /// Compute a map of macro argument chunks to their expanded source 1772 /// location. Chunks that are not part of a macro argument will map to an 1773 /// invalid source location. e.g. if a file contains one macro argument at 1774 /// offset 100 with length 10, this is how the map will be formed: 1775 /// 0 -> SourceLocation() 1776 /// 100 -> Expanded macro arg location 1777 /// 110 -> SourceLocation() 1778 void SourceManager::computeMacroArgsCache(MacroArgsMap &MacroArgsCache, 1779 FileID FID) const { 1780 assert(FID.isValid()); 1781 1782 // Initially no macro argument chunk is present. 1783 MacroArgsCache.insert(std::make_pair(0, SourceLocation())); 1784 1785 int ID = FID.ID; 1786 while (true) { 1787 ++ID; 1788 // Stop if there are no more FileIDs to check. 1789 if (ID > 0) { 1790 if (unsigned(ID) >= local_sloc_entry_size()) 1791 return; 1792 } else if (ID == -1) { 1793 return; 1794 } 1795 1796 bool Invalid = false; 1797 const SrcMgr::SLocEntry &Entry = getSLocEntryByID(ID, &Invalid); 1798 if (Invalid) 1799 return; 1800 if (Entry.isFile()) { 1801 SourceLocation IncludeLoc = Entry.getFile().getIncludeLoc(); 1802 if (IncludeLoc.isInvalid()) 1803 continue; 1804 if (!isInFileID(IncludeLoc, FID)) 1805 return; // No more files/macros that may be "contained" in this file. 1806 1807 // Skip the files/macros of the #include'd file, we only care about macros 1808 // that lexed macro arguments from our file. 1809 if (Entry.getFile().NumCreatedFIDs) 1810 ID += Entry.getFile().NumCreatedFIDs - 1/*because of next ++ID*/; 1811 continue; 1812 } 1813 1814 const ExpansionInfo &ExpInfo = Entry.getExpansion(); 1815 1816 if (ExpInfo.getExpansionLocStart().isFileID()) { 1817 if (!isInFileID(ExpInfo.getExpansionLocStart(), FID)) 1818 return; // No more files/macros that may be "contained" in this file. 1819 } 1820 1821 if (!ExpInfo.isMacroArgExpansion()) 1822 continue; 1823 1824 associateFileChunkWithMacroArgExp(MacroArgsCache, FID, 1825 ExpInfo.getSpellingLoc(), 1826 SourceLocation::getMacroLoc(Entry.getOffset()), 1827 getFileIDSize(FileID::get(ID))); 1828 } 1829 } 1830 1831 void SourceManager::associateFileChunkWithMacroArgExp( 1832 MacroArgsMap &MacroArgsCache, 1833 FileID FID, 1834 SourceLocation SpellLoc, 1835 SourceLocation ExpansionLoc, 1836 unsigned ExpansionLength) const { 1837 if (!SpellLoc.isFileID()) { 1838 unsigned SpellBeginOffs = SpellLoc.getOffset(); 1839 unsigned SpellEndOffs = SpellBeginOffs + ExpansionLength; 1840 1841 // The spelling range for this macro argument expansion can span multiple 1842 // consecutive FileID entries. Go through each entry contained in the 1843 // spelling range and if one is itself a macro argument expansion, recurse 1844 // and associate the file chunk that it represents. 1845 1846 FileID SpellFID; // Current FileID in the spelling range. 1847 unsigned SpellRelativeOffs; 1848 std::tie(SpellFID, SpellRelativeOffs) = getDecomposedLoc(SpellLoc); 1849 while (true) { 1850 const SLocEntry &Entry = getSLocEntry(SpellFID); 1851 unsigned SpellFIDBeginOffs = Entry.getOffset(); 1852 unsigned SpellFIDSize = getFileIDSize(SpellFID); 1853 unsigned SpellFIDEndOffs = SpellFIDBeginOffs + SpellFIDSize; 1854 const ExpansionInfo &Info = Entry.getExpansion(); 1855 if (Info.isMacroArgExpansion()) { 1856 unsigned CurrSpellLength; 1857 if (SpellFIDEndOffs < SpellEndOffs) 1858 CurrSpellLength = SpellFIDSize - SpellRelativeOffs; 1859 else 1860 CurrSpellLength = ExpansionLength; 1861 associateFileChunkWithMacroArgExp(MacroArgsCache, FID, 1862 Info.getSpellingLoc().getLocWithOffset(SpellRelativeOffs), 1863 ExpansionLoc, CurrSpellLength); 1864 } 1865 1866 if (SpellFIDEndOffs >= SpellEndOffs) 1867 return; // we covered all FileID entries in the spelling range. 1868 1869 // Move to the next FileID entry in the spelling range. 1870 unsigned advance = SpellFIDSize - SpellRelativeOffs + 1; 1871 ExpansionLoc = ExpansionLoc.getLocWithOffset(advance); 1872 ExpansionLength -= advance; 1873 ++SpellFID.ID; 1874 SpellRelativeOffs = 0; 1875 } 1876 } 1877 1878 assert(SpellLoc.isFileID()); 1879 1880 unsigned BeginOffs; 1881 if (!isInFileID(SpellLoc, FID, &BeginOffs)) 1882 return; 1883 1884 unsigned EndOffs = BeginOffs + ExpansionLength; 1885 1886 // Add a new chunk for this macro argument. A previous macro argument chunk 1887 // may have been lexed again, so e.g. if the map is 1888 // 0 -> SourceLocation() 1889 // 100 -> Expanded loc #1 1890 // 110 -> SourceLocation() 1891 // and we found a new macro FileID that lexed from offset 105 with length 3, 1892 // the new map will be: 1893 // 0 -> SourceLocation() 1894 // 100 -> Expanded loc #1 1895 // 105 -> Expanded loc #2 1896 // 108 -> Expanded loc #1 1897 // 110 -> SourceLocation() 1898 // 1899 // Since re-lexed macro chunks will always be the same size or less of 1900 // previous chunks, we only need to find where the ending of the new macro 1901 // chunk is mapped to and update the map with new begin/end mappings. 1902 1903 MacroArgsMap::iterator I = MacroArgsCache.upper_bound(EndOffs); 1904 --I; 1905 SourceLocation EndOffsMappedLoc = I->second; 1906 MacroArgsCache[BeginOffs] = ExpansionLoc; 1907 MacroArgsCache[EndOffs] = EndOffsMappedLoc; 1908 } 1909 1910 /// If \arg Loc points inside a function macro argument, the returned 1911 /// location will be the macro location in which the argument was expanded. 1912 /// If a macro argument is used multiple times, the expanded location will 1913 /// be at the first expansion of the argument. 1914 /// e.g. 1915 /// MY_MACRO(foo); 1916 /// ^ 1917 /// Passing a file location pointing at 'foo', will yield a macro location 1918 /// where 'foo' was expanded into. 1919 SourceLocation 1920 SourceManager::getMacroArgExpandedLocation(SourceLocation Loc) const { 1921 if (Loc.isInvalid() || !Loc.isFileID()) 1922 return Loc; 1923 1924 FileID FID; 1925 unsigned Offset; 1926 std::tie(FID, Offset) = getDecomposedLoc(Loc); 1927 if (FID.isInvalid()) 1928 return Loc; 1929 1930 std::unique_ptr<MacroArgsMap> &MacroArgsCache = MacroArgsCacheMap[FID]; 1931 if (!MacroArgsCache) { 1932 MacroArgsCache = llvm::make_unique<MacroArgsMap>(); 1933 computeMacroArgsCache(*MacroArgsCache, FID); 1934 } 1935 1936 assert(!MacroArgsCache->empty()); 1937 MacroArgsMap::iterator I = MacroArgsCache->upper_bound(Offset); 1938 --I; 1939 1940 unsigned MacroArgBeginOffs = I->first; 1941 SourceLocation MacroArgExpandedLoc = I->second; 1942 if (MacroArgExpandedLoc.isValid()) 1943 return MacroArgExpandedLoc.getLocWithOffset(Offset - MacroArgBeginOffs); 1944 1945 return Loc; 1946 } 1947 1948 std::pair<FileID, unsigned> 1949 SourceManager::getDecomposedIncludedLoc(FileID FID) const { 1950 if (FID.isInvalid()) 1951 return std::make_pair(FileID(), 0); 1952 1953 // Uses IncludedLocMap to retrieve/cache the decomposed loc. 1954 1955 using DecompTy = std::pair<FileID, unsigned>; 1956 auto InsertOp = IncludedLocMap.try_emplace(FID); 1957 DecompTy &DecompLoc = InsertOp.first->second; 1958 if (!InsertOp.second) 1959 return DecompLoc; // already in map. 1960 1961 SourceLocation UpperLoc; 1962 bool Invalid = false; 1963 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid); 1964 if (!Invalid) { 1965 if (Entry.isExpansion()) 1966 UpperLoc = Entry.getExpansion().getExpansionLocStart(); 1967 else 1968 UpperLoc = Entry.getFile().getIncludeLoc(); 1969 } 1970 1971 if (UpperLoc.isValid()) 1972 DecompLoc = getDecomposedLoc(UpperLoc); 1973 1974 return DecompLoc; 1975 } 1976 1977 /// Given a decomposed source location, move it up the include/expansion stack 1978 /// to the parent source location. If this is possible, return the decomposed 1979 /// version of the parent in Loc and return false. If Loc is the top-level 1980 /// entry, return true and don't modify it. 1981 static bool MoveUpIncludeHierarchy(std::pair<FileID, unsigned> &Loc, 1982 const SourceManager &SM) { 1983 std::pair<FileID, unsigned> UpperLoc = SM.getDecomposedIncludedLoc(Loc.first); 1984 if (UpperLoc.first.isInvalid()) 1985 return true; // We reached the top. 1986 1987 Loc = UpperLoc; 1988 return false; 1989 } 1990 1991 /// Return the cache entry for comparing the given file IDs 1992 /// for isBeforeInTranslationUnit. 1993 InBeforeInTUCacheEntry &SourceManager::getInBeforeInTUCache(FileID LFID, 1994 FileID RFID) const { 1995 // This is a magic number for limiting the cache size. It was experimentally 1996 // derived from a small Objective-C project (where the cache filled 1997 // out to ~250 items). We can make it larger if necessary. 1998 enum { MagicCacheSize = 300 }; 1999 IsBeforeInTUCacheKey Key(LFID, RFID); 2000 2001 // If the cache size isn't too large, do a lookup and if necessary default 2002 // construct an entry. We can then return it to the caller for direct 2003 // use. When they update the value, the cache will get automatically 2004 // updated as well. 2005 if (IBTUCache.size() < MagicCacheSize) 2006 return IBTUCache[Key]; 2007 2008 // Otherwise, do a lookup that will not construct a new value. 2009 InBeforeInTUCache::iterator I = IBTUCache.find(Key); 2010 if (I != IBTUCache.end()) 2011 return I->second; 2012 2013 // Fall back to the overflow value. 2014 return IBTUCacheOverflow; 2015 } 2016 2017 /// Determines the order of 2 source locations in the translation unit. 2018 /// 2019 /// \returns true if LHS source location comes before RHS, false otherwise. 2020 bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS, 2021 SourceLocation RHS) const { 2022 assert(LHS.isValid() && RHS.isValid() && "Passed invalid source location!"); 2023 if (LHS == RHS) 2024 return false; 2025 2026 std::pair<FileID, unsigned> LOffs = getDecomposedLoc(LHS); 2027 std::pair<FileID, unsigned> ROffs = getDecomposedLoc(RHS); 2028 2029 // getDecomposedLoc may have failed to return a valid FileID because, e.g. it 2030 // is a serialized one referring to a file that was removed after we loaded 2031 // the PCH. 2032 if (LOffs.first.isInvalid() || ROffs.first.isInvalid()) 2033 return LOffs.first.isInvalid() && !ROffs.first.isInvalid(); 2034 2035 std::pair<bool, bool> InSameTU = isInTheSameTranslationUnit(LOffs, ROffs); 2036 if (InSameTU.first) 2037 return InSameTU.second; 2038 2039 // If we arrived here, the location is either in a built-ins buffer or 2040 // associated with global inline asm. PR5662 and PR22576 are examples. 2041 2042 StringRef LB = getBuffer(LOffs.first)->getBufferIdentifier(); 2043 StringRef RB = getBuffer(ROffs.first)->getBufferIdentifier(); 2044 bool LIsBuiltins = LB == "<built-in>"; 2045 bool RIsBuiltins = RB == "<built-in>"; 2046 // Sort built-in before non-built-in. 2047 if (LIsBuiltins || RIsBuiltins) { 2048 if (LIsBuiltins != RIsBuiltins) 2049 return LIsBuiltins; 2050 // Both are in built-in buffers, but from different files. We just claim that 2051 // lower IDs come first. 2052 return LOffs.first < ROffs.first; 2053 } 2054 bool LIsAsm = LB == "<inline asm>"; 2055 bool RIsAsm = RB == "<inline asm>"; 2056 // Sort assembler after built-ins, but before the rest. 2057 if (LIsAsm || RIsAsm) { 2058 if (LIsAsm != RIsAsm) 2059 return RIsAsm; 2060 assert(LOffs.first == ROffs.first); 2061 return false; 2062 } 2063 bool LIsScratch = LB == "<scratch space>"; 2064 bool RIsScratch = RB == "<scratch space>"; 2065 // Sort scratch after inline asm, but before the rest. 2066 if (LIsScratch || RIsScratch) { 2067 if (LIsScratch != RIsScratch) 2068 return LIsScratch; 2069 return LOffs.second < ROffs.second; 2070 } 2071 llvm_unreachable("Unsortable locations found"); 2072 } 2073 2074 std::pair<bool, bool> SourceManager::isInTheSameTranslationUnit( 2075 std::pair<FileID, unsigned> &LOffs, 2076 std::pair<FileID, unsigned> &ROffs) const { 2077 // If the source locations are in the same file, just compare offsets. 2078 if (LOffs.first == ROffs.first) 2079 return std::make_pair(true, LOffs.second < ROffs.second); 2080 2081 // If we are comparing a source location with multiple locations in the same 2082 // file, we get a big win by caching the result. 2083 InBeforeInTUCacheEntry &IsBeforeInTUCache = 2084 getInBeforeInTUCache(LOffs.first, ROffs.first); 2085 2086 // If we are comparing a source location with multiple locations in the same 2087 // file, we get a big win by caching the result. 2088 if (IsBeforeInTUCache.isCacheValid(LOffs.first, ROffs.first)) 2089 return std::make_pair( 2090 true, IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second)); 2091 2092 // Okay, we missed in the cache, start updating the cache for this query. 2093 IsBeforeInTUCache.setQueryFIDs(LOffs.first, ROffs.first, 2094 /*isLFIDBeforeRFID=*/LOffs.first.ID < ROffs.first.ID); 2095 2096 // We need to find the common ancestor. The only way of doing this is to 2097 // build the complete include chain for one and then walking up the chain 2098 // of the other looking for a match. 2099 // We use a map from FileID to Offset to store the chain. Easier than writing 2100 // a custom set hash info that only depends on the first part of a pair. 2101 using LocSet = llvm::SmallDenseMap<FileID, unsigned, 16>; 2102 LocSet LChain; 2103 do { 2104 LChain.insert(LOffs); 2105 // We catch the case where LOffs is in a file included by ROffs and 2106 // quit early. The other way round unfortunately remains suboptimal. 2107 } while (LOffs.first != ROffs.first && !MoveUpIncludeHierarchy(LOffs, *this)); 2108 LocSet::iterator I; 2109 while((I = LChain.find(ROffs.first)) == LChain.end()) { 2110 if (MoveUpIncludeHierarchy(ROffs, *this)) 2111 break; // Met at topmost file. 2112 } 2113 if (I != LChain.end()) 2114 LOffs = *I; 2115 2116 // If we exited because we found a nearest common ancestor, compare the 2117 // locations within the common file and cache them. 2118 if (LOffs.first == ROffs.first) { 2119 IsBeforeInTUCache.setCommonLoc(LOffs.first, LOffs.second, ROffs.second); 2120 return std::make_pair( 2121 true, IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second)); 2122 } 2123 // Clear the lookup cache, it depends on a common location. 2124 IsBeforeInTUCache.clear(); 2125 return std::make_pair(false, false); 2126 } 2127 2128 void SourceManager::PrintStats() const { 2129 llvm::errs() << "\n*** Source Manager Stats:\n"; 2130 llvm::errs() << FileInfos.size() << " files mapped, " << MemBufferInfos.size() 2131 << " mem buffers mapped.\n"; 2132 llvm::errs() << LocalSLocEntryTable.size() << " local SLocEntry's allocated (" 2133 << llvm::capacity_in_bytes(LocalSLocEntryTable) 2134 << " bytes of capacity), " 2135 << NextLocalOffset << "B of Sloc address space used.\n"; 2136 llvm::errs() << LoadedSLocEntryTable.size() 2137 << " loaded SLocEntries allocated, " 2138 << MaxLoadedOffset - CurrentLoadedOffset 2139 << "B of Sloc address space used.\n"; 2140 2141 unsigned NumLineNumsComputed = 0; 2142 unsigned NumFileBytesMapped = 0; 2143 for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){ 2144 NumLineNumsComputed += I->second->SourceLineCache != nullptr; 2145 NumFileBytesMapped += I->second->getSizeBytesMapped(); 2146 } 2147 unsigned NumMacroArgsComputed = MacroArgsCacheMap.size(); 2148 2149 llvm::errs() << NumFileBytesMapped << " bytes of files mapped, " 2150 << NumLineNumsComputed << " files with line #'s computed, " 2151 << NumMacroArgsComputed << " files with macro args computed.\n"; 2152 llvm::errs() << "FileID scans: " << NumLinearScans << " linear, " 2153 << NumBinaryProbes << " binary.\n"; 2154 } 2155 2156 LLVM_DUMP_METHOD void SourceManager::dump() const { 2157 llvm::raw_ostream &out = llvm::errs(); 2158 2159 auto DumpSLocEntry = [&](int ID, const SrcMgr::SLocEntry &Entry, 2160 llvm::Optional<unsigned> NextStart) { 2161 out << "SLocEntry <FileID " << ID << "> " << (Entry.isFile() ? "file" : "expansion") 2162 << " <SourceLocation " << Entry.getOffset() << ":"; 2163 if (NextStart) 2164 out << *NextStart << ">\n"; 2165 else 2166 out << "???\?>\n"; 2167 if (Entry.isFile()) { 2168 auto &FI = Entry.getFile(); 2169 if (FI.NumCreatedFIDs) 2170 out << " covers <FileID " << ID << ":" << int(ID + FI.NumCreatedFIDs) 2171 << ">\n"; 2172 if (FI.getIncludeLoc().isValid()) 2173 out << " included from " << FI.getIncludeLoc().getOffset() << "\n"; 2174 if (auto *CC = FI.getContentCache()) { 2175 out << " for " << (CC->OrigEntry ? CC->OrigEntry->getName() : "<none>") 2176 << "\n"; 2177 if (CC->BufferOverridden) 2178 out << " contents overridden\n"; 2179 if (CC->ContentsEntry != CC->OrigEntry) { 2180 out << " contents from " 2181 << (CC->ContentsEntry ? CC->ContentsEntry->getName() : "<none>") 2182 << "\n"; 2183 } 2184 } 2185 } else { 2186 auto &EI = Entry.getExpansion(); 2187 out << " spelling from " << EI.getSpellingLoc().getOffset() << "\n"; 2188 out << " macro " << (EI.isMacroArgExpansion() ? "arg" : "body") 2189 << " range <" << EI.getExpansionLocStart().getOffset() << ":" 2190 << EI.getExpansionLocEnd().getOffset() << ">\n"; 2191 } 2192 }; 2193 2194 // Dump local SLocEntries. 2195 for (unsigned ID = 0, NumIDs = LocalSLocEntryTable.size(); ID != NumIDs; ++ID) { 2196 DumpSLocEntry(ID, LocalSLocEntryTable[ID], 2197 ID == NumIDs - 1 ? NextLocalOffset 2198 : LocalSLocEntryTable[ID + 1].getOffset()); 2199 } 2200 // Dump loaded SLocEntries. 2201 llvm::Optional<unsigned> NextStart; 2202 for (unsigned Index = 0; Index != LoadedSLocEntryTable.size(); ++Index) { 2203 int ID = -(int)Index - 2; 2204 if (SLocEntryLoaded[Index]) { 2205 DumpSLocEntry(ID, LoadedSLocEntryTable[Index], NextStart); 2206 NextStart = LoadedSLocEntryTable[Index].getOffset(); 2207 } else { 2208 NextStart = None; 2209 } 2210 } 2211 } 2212 2213 ExternalSLocEntrySource::~ExternalSLocEntrySource() = default; 2214 2215 /// Return the amount of memory used by memory buffers, breaking down 2216 /// by heap-backed versus mmap'ed memory. 2217 SourceManager::MemoryBufferSizes SourceManager::getMemoryBufferSizes() const { 2218 size_t malloc_bytes = 0; 2219 size_t mmap_bytes = 0; 2220 2221 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) 2222 if (size_t sized_mapped = MemBufferInfos[i]->getSizeBytesMapped()) 2223 switch (MemBufferInfos[i]->getMemoryBufferKind()) { 2224 case llvm::MemoryBuffer::MemoryBuffer_MMap: 2225 mmap_bytes += sized_mapped; 2226 break; 2227 case llvm::MemoryBuffer::MemoryBuffer_Malloc: 2228 malloc_bytes += sized_mapped; 2229 break; 2230 } 2231 2232 return MemoryBufferSizes(malloc_bytes, mmap_bytes); 2233 } 2234 2235 size_t SourceManager::getDataStructureSizes() const { 2236 size_t size = llvm::capacity_in_bytes(MemBufferInfos) 2237 + llvm::capacity_in_bytes(LocalSLocEntryTable) 2238 + llvm::capacity_in_bytes(LoadedSLocEntryTable) 2239 + llvm::capacity_in_bytes(SLocEntryLoaded) 2240 + llvm::capacity_in_bytes(FileInfos); 2241 2242 if (OverriddenFilesInfo) 2243 size += llvm::capacity_in_bytes(OverriddenFilesInfo->OverriddenFiles); 2244 2245 return size; 2246 } 2247 2248 SourceManagerForFile::SourceManagerForFile(StringRef FileName, 2249 StringRef Content) { 2250 // This is referenced by `FileMgr` and will be released by `FileMgr` when it 2251 // is deleted. 2252 IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem( 2253 new llvm::vfs::InMemoryFileSystem); 2254 InMemoryFileSystem->addFile( 2255 FileName, 0, 2256 llvm::MemoryBuffer::getMemBuffer(Content, FileName, 2257 /*RequiresNullTerminator=*/false)); 2258 // This is passed to `SM` as reference, so the pointer has to be referenced 2259 // in `Environment` so that `FileMgr` can out-live this function scope. 2260 FileMgr = 2261 llvm::make_unique<FileManager>(FileSystemOptions(), InMemoryFileSystem); 2262 // This is passed to `SM` as reference, so the pointer has to be referenced 2263 // by `Environment` due to the same reason above. 2264 Diagnostics = llvm::make_unique<DiagnosticsEngine>( 2265 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs), 2266 new DiagnosticOptions); 2267 SourceMgr = llvm::make_unique<SourceManager>(*Diagnostics, *FileMgr); 2268 FileID ID = SourceMgr->createFileID(FileMgr->getFile(FileName), 2269 SourceLocation(), clang::SrcMgr::C_User); 2270 assert(ID.isValid()); 2271 SourceMgr->setMainFileID(ID); 2272 } 2273