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