1 //===- SourceManager.cpp - Track and cache source files -------------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the SourceManager interface.
11 //===----------------------------------------------------------------------===//
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"
44 using namespace clang;
45 using namespace SrcMgr;
46 using llvm::MemoryBuffer;
48 //===----------------------------------------------------------------------===//
49 // SourceManager Helper Classes
50 //===----------------------------------------------------------------------===//
52 ContentCache::~ContentCache() {
53 if (shouldFreeBuffer())
54 delete Buffer.getPointer();
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;
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());
68 // Should be unreachable, but keep for sanity.
69 if (!Buffer.getPointer())
70 return llvm::MemoryBuffer::MemoryBuffer_Malloc;
72 const llvm::MemoryBuffer *buf = Buffer.getPointer();
73 return buf->getBufferKind();
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();
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);
92 if (shouldFreeBuffer())
93 delete Buffer.getPointer();
95 Buffer.setInt((B && DoNotFree) ? DoNotFreeFlag : 0);
98 const llvm::MemoryBuffer *ContentCache::getBuffer(DiagnosticsEngine &Diag,
99 const SourceManager &SM,
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) {
106 *Invalid = isBufferInvalid();
108 return Buffer.getPointer();
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.
123 llvm::MemoryBuffer::getMemBuffer("", ContentsEntry->getName())
125 if (Diag.isDiagnosticInFlight())
126 Diag.SetDelayedDiagnostic(diag::err_file_too_large,
127 ContentsEntry->getName());
129 Diag.Report(Loc, diag::err_file_too_large)
130 << ContentsEntry->getName();
132 Buffer.setInt(Buffer.getInt() | InvalidFlag);
133 if (Invalid) *Invalid = true;
134 return Buffer.getPointer();
137 bool isVolatile = SM.userFilesAreVolatile() && !IsSystemFile;
139 SM.getFileManager().getBufferForFile(ContentsEntry, isVolatile);
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.
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
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());
160 if (Diag.isDiagnosticInFlight())
161 Diag.SetDelayedDiagnostic(diag::err_cannot_open_file,
162 ContentsEntry->getName(),
163 BufferOrError.getError().message());
165 Diag.Report(Loc, diag::err_cannot_open_file)
166 << ContentsEntry->getName() << BufferOrError.getError().message();
168 Buffer.setInt(Buffer.getInt() | InvalidFlag);
170 if (Invalid) *Invalid = true;
171 return Buffer.getPointer();
174 Buffer.setPointer(BufferOrError->release());
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());
183 Diag.Report(Loc, diag::err_file_modified)
184 << ContentsEntry->getName();
186 Buffer.setInt(Buffer.getInt() | InvalidFlag);
187 if (Invalid) *Invalid = true;
188 return Buffer.getPointer();
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"),
198 .StartsWith(llvm::StringLiteral::withInnerNUL("\xFF\xFE\x00\x00"),
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")
211 Diag.Report(Loc, diag::err_unsupported_bom)
212 << InvalidBOM << ContentsEntry->getName();
213 Buffer.setInt(Buffer.getInt() | InvalidFlag);
217 *Invalid = isBufferInvalid();
219 return Buffer.getPointer();
222 unsigned LineTableInfo::getLineTableFilenameID(StringRef Name) {
223 auto IterBool = FilenameIDs.try_emplace(Name, FilenamesByID.size());
225 FilenamesByID.push_back(&*IterBool.first);
226 return IterBool.first->second;
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];
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;
245 assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
246 "Adding line entries out of order!");
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");
257 // Get the include loc of the last entries' include loc as our include loc.
259 if (const LineEntry *PrevEntry =
260 FindNearestLineEntry(FID, Entries.back().IncludeOffset))
261 IncludeOffset = PrevEntry->IncludeOffset;
264 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind,
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,
272 const std::vector<LineEntry> &Entries = LineEntries[FID];
273 assert(!Entries.empty() && "No #line entries for this FID after all!");
275 // It is very common for the query to be after the last #line, check this
277 if (Entries.back().FileOffset <= Offset)
278 return &Entries.back();
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())
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;
294 /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
295 unsigned SourceManager::getLineTableFilenameID(StringRef Name) {
296 return getLineTable().getLineTableFilenameID(Name);
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
302 void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
303 int FilenameID, bool IsFileEntry,
305 SrcMgr::CharacteristicKind FileKind) {
306 std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
308 bool Invalid = false;
309 const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid);
310 if (!Entry.isFile() || Invalid)
313 const SrcMgr::FileInfo &FileInfo = Entry.getFile();
315 // Remember that this file has #line directives now if it doesn't already.
316 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
318 (void) getLineTable();
320 unsigned EntryExit = 0;
326 LineTable->AddLineNote(LocInfo.first, LocInfo.second, LineNo, FilenameID,
327 EntryExit, FileKind);
330 LineTableInfo &SourceManager::getLineTable() {
332 LineTable.reset(new LineTableInfo());
336 //===----------------------------------------------------------------------===//
337 // Private 'Create' methods.
338 //===----------------------------------------------------------------------===//
340 SourceManager::SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr,
341 bool UserFilesAreVolatile)
342 : Diag(Diag), FileMgr(FileMgr), UserFilesAreVolatile(UserFilesAreVolatile) {
344 Diag.setSourceManager(this);
347 SourceManager::~SourceManager() {
348 // Delete FileEntry objects corresponding to content caches. Since the actual
349 // content cache objects are bump pointer allocated, we just have to run the
350 // dtors, but we call the deallocate method for completeness.
351 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) {
352 if (MemBufferInfos[i]) {
353 MemBufferInfos[i]->~ContentCache();
354 ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
357 for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
358 I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
360 I->second->~ContentCache();
361 ContentCacheAlloc.Deallocate(I->second);
366 void SourceManager::clearIDTables() {
367 MainFileID = FileID();
368 LocalSLocEntryTable.clear();
369 LoadedSLocEntryTable.clear();
370 SLocEntryLoaded.clear();
371 LastLineNoFileIDQuery = FileID();
372 LastLineNoContentCache = nullptr;
373 LastFileIDLookup = FileID();
378 // Use up FileID #0 as an invalid expansion.
380 CurrentLoadedOffset = MaxLoadedOffset;
381 createExpansionLoc(SourceLocation(), SourceLocation(), SourceLocation(), 1);
384 void SourceManager::initializeForReplay(const SourceManager &Old) {
385 assert(MainFileID.isInvalid() && "expected uninitialized SourceManager");
387 auto CloneContentCache = [&](const ContentCache *Cache) -> ContentCache * {
388 auto *Clone = new (ContentCacheAlloc.Allocate<ContentCache>()) ContentCache;
389 Clone->OrigEntry = Cache->OrigEntry;
390 Clone->ContentsEntry = Cache->ContentsEntry;
391 Clone->BufferOverridden = Cache->BufferOverridden;
392 Clone->IsSystemFile = Cache->IsSystemFile;
393 Clone->IsTransient = Cache->IsTransient;
394 Clone->replaceBuffer(Cache->getRawBuffer(), /*DoNotFree*/true);
398 // Ensure all SLocEntries are loaded from the external source.
399 for (unsigned I = 0, N = Old.LoadedSLocEntryTable.size(); I != N; ++I)
400 if (!Old.SLocEntryLoaded[I])
401 Old.loadSLocEntry(I, nullptr);
403 // Inherit any content cache data from the old source manager.
404 for (auto &FileInfo : Old.FileInfos) {
405 SrcMgr::ContentCache *&Slot = FileInfos[FileInfo.first];
408 Slot = CloneContentCache(FileInfo.second);
412 /// getOrCreateContentCache - Create or return a cached ContentCache for the
415 SourceManager::getOrCreateContentCache(const FileEntry *FileEnt,
417 assert(FileEnt && "Didn't specify a file entry to use?");
419 // Do we already have information about this file?
420 ContentCache *&Entry = FileInfos[FileEnt];
421 if (Entry) return Entry;
423 // Nope, create a new Cache entry.
424 Entry = ContentCacheAlloc.Allocate<ContentCache>();
426 if (OverriddenFilesInfo) {
427 // If the file contents are overridden with contents from another file,
428 // pass that file to ContentCache.
429 llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
430 overI = OverriddenFilesInfo->OverriddenFiles.find(FileEnt);
431 if (overI == OverriddenFilesInfo->OverriddenFiles.end())
432 new (Entry) ContentCache(FileEnt);
434 new (Entry) ContentCache(OverridenFilesKeepOriginalName ? FileEnt
438 new (Entry) ContentCache(FileEnt);
441 Entry->IsSystemFile = isSystemFile;
442 Entry->IsTransient = FilesAreTransient;
447 /// Create a new ContentCache for the specified memory buffer.
448 /// This does no caching.
450 SourceManager::createMemBufferContentCache(const llvm::MemoryBuffer *Buffer,
452 // Add a new ContentCache to the MemBufferInfos list and return it.
453 ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>();
454 new (Entry) ContentCache();
455 MemBufferInfos.push_back(Entry);
456 Entry->replaceBuffer(Buffer, DoNotFree);
460 const SrcMgr::SLocEntry &SourceManager::loadSLocEntry(unsigned Index,
461 bool *Invalid) const {
462 assert(!SLocEntryLoaded[Index]);
463 if (ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2))) {
466 // If the file of the SLocEntry changed we could still have loaded it.
467 if (!SLocEntryLoaded[Index]) {
468 // Try to recover; create a SLocEntry so the rest of clang can handle it.
469 LoadedSLocEntryTable[Index] = SLocEntry::get(0,
470 FileInfo::get(SourceLocation(),
471 getFakeContentCacheForRecovery(),
476 return LoadedSLocEntryTable[Index];
479 std::pair<int, unsigned>
480 SourceManager::AllocateLoadedSLocEntries(unsigned NumSLocEntries,
481 unsigned TotalSize) {
482 assert(ExternalSLocEntries && "Don't have an external sloc source");
483 // Make sure we're not about to run out of source locations.
484 if (CurrentLoadedOffset - TotalSize < NextLocalOffset)
485 return std::make_pair(0, 0);
486 LoadedSLocEntryTable.resize(LoadedSLocEntryTable.size() + NumSLocEntries);
487 SLocEntryLoaded.resize(LoadedSLocEntryTable.size());
488 CurrentLoadedOffset -= TotalSize;
489 int ID = LoadedSLocEntryTable.size();
490 return std::make_pair(-ID - 1, CurrentLoadedOffset);
493 /// As part of recovering from missing or changed content, produce a
494 /// fake, non-empty buffer.
495 llvm::MemoryBuffer *SourceManager::getFakeBufferForRecovery() const {
496 if (!FakeBufferForRecovery)
497 FakeBufferForRecovery =
498 llvm::MemoryBuffer::getMemBuffer("<<<INVALID BUFFER>>");
500 return FakeBufferForRecovery.get();
503 /// As part of recovering from missing or changed content, produce a
504 /// fake content cache.
505 const SrcMgr::ContentCache *
506 SourceManager::getFakeContentCacheForRecovery() const {
507 if (!FakeContentCacheForRecovery) {
508 FakeContentCacheForRecovery = llvm::make_unique<SrcMgr::ContentCache>();
509 FakeContentCacheForRecovery->replaceBuffer(getFakeBufferForRecovery(),
512 return FakeContentCacheForRecovery.get();
515 /// Returns the previous in-order FileID or an invalid FileID if there
516 /// is no previous one.
517 FileID SourceManager::getPreviousFileID(FileID FID) const {
528 } else if (unsigned(-(ID-1) - 2) >= LoadedSLocEntryTable.size()) {
532 return FileID::get(ID-1);
535 /// Returns the next in-order FileID or an invalid FileID if there is
537 FileID SourceManager::getNextFileID(FileID FID) const {
543 if (unsigned(ID+1) >= local_sloc_entry_size())
545 } else if (ID+1 >= -1) {
549 return FileID::get(ID+1);
552 //===----------------------------------------------------------------------===//
553 // Methods to create new FileID's and macro expansions.
554 //===----------------------------------------------------------------------===//
556 /// createFileID - Create a new FileID for the specified ContentCache and
557 /// include position. This works regardless of whether the ContentCache
558 /// corresponds to a file or some other input source.
559 FileID SourceManager::createFileID(const ContentCache *File,
560 SourceLocation IncludePos,
561 SrcMgr::CharacteristicKind FileCharacter,
562 int LoadedID, unsigned LoadedOffset) {
564 assert(LoadedID != -1 && "Loading sentinel FileID");
565 unsigned Index = unsigned(-LoadedID) - 2;
566 assert(Index < LoadedSLocEntryTable.size() && "FileID out of range");
567 assert(!SLocEntryLoaded[Index] && "FileID already loaded");
568 LoadedSLocEntryTable[Index] = SLocEntry::get(LoadedOffset,
569 FileInfo::get(IncludePos, File, FileCharacter));
570 SLocEntryLoaded[Index] = true;
571 return FileID::get(LoadedID);
573 LocalSLocEntryTable.push_back(SLocEntry::get(NextLocalOffset,
574 FileInfo::get(IncludePos, File,
576 unsigned FileSize = File->getSize();
577 assert(NextLocalOffset + FileSize + 1 > NextLocalOffset &&
578 NextLocalOffset + FileSize + 1 <= CurrentLoadedOffset &&
579 "Ran out of source locations!");
580 // We do a +1 here because we want a SourceLocation that means "the end of the
581 // file", e.g. for the "no newline at the end of the file" diagnostic.
582 NextLocalOffset += FileSize + 1;
584 // Set LastFileIDLookup to the newly created file. The next getFileID call is
585 // almost guaranteed to be from that file.
586 FileID FID = FileID::get(LocalSLocEntryTable.size()-1);
587 return LastFileIDLookup = FID;
591 SourceManager::createMacroArgExpansionLoc(SourceLocation SpellingLoc,
592 SourceLocation ExpansionLoc,
593 unsigned TokLength) {
594 ExpansionInfo Info = ExpansionInfo::createForMacroArg(SpellingLoc,
596 return createExpansionLocImpl(Info, TokLength);
600 SourceManager::createExpansionLoc(SourceLocation SpellingLoc,
601 SourceLocation ExpansionLocStart,
602 SourceLocation ExpansionLocEnd,
604 bool ExpansionIsTokenRange,
606 unsigned LoadedOffset) {
607 ExpansionInfo Info = ExpansionInfo::create(
608 SpellingLoc, ExpansionLocStart, ExpansionLocEnd, ExpansionIsTokenRange);
609 return createExpansionLocImpl(Info, TokLength, LoadedID, LoadedOffset);
612 SourceLocation SourceManager::createTokenSplitLoc(SourceLocation Spelling,
613 SourceLocation TokenStart,
614 SourceLocation TokenEnd) {
615 assert(getFileID(TokenStart) == getFileID(TokenEnd) &&
616 "token spans multiple files");
617 return createExpansionLocImpl(
618 ExpansionInfo::createForTokenSplit(Spelling, TokenStart, TokenEnd),
619 TokenEnd.getOffset() - TokenStart.getOffset());
623 SourceManager::createExpansionLocImpl(const ExpansionInfo &Info,
626 unsigned LoadedOffset) {
628 assert(LoadedID != -1 && "Loading sentinel FileID");
629 unsigned Index = unsigned(-LoadedID) - 2;
630 assert(Index < LoadedSLocEntryTable.size() && "FileID out of range");
631 assert(!SLocEntryLoaded[Index] && "FileID already loaded");
632 LoadedSLocEntryTable[Index] = SLocEntry::get(LoadedOffset, Info);
633 SLocEntryLoaded[Index] = true;
634 return SourceLocation::getMacroLoc(LoadedOffset);
636 LocalSLocEntryTable.push_back(SLocEntry::get(NextLocalOffset, Info));
637 assert(NextLocalOffset + TokLength + 1 > NextLocalOffset &&
638 NextLocalOffset + TokLength + 1 <= CurrentLoadedOffset &&
639 "Ran out of source locations!");
640 // See createFileID for that +1.
641 NextLocalOffset += TokLength + 1;
642 return SourceLocation::getMacroLoc(NextLocalOffset - (TokLength + 1));
645 const llvm::MemoryBuffer *
646 SourceManager::getMemoryBufferForFile(const FileEntry *File, bool *Invalid) {
647 const SrcMgr::ContentCache *IR = getOrCreateContentCache(File);
648 assert(IR && "getOrCreateContentCache() cannot return NULL");
649 return IR->getBuffer(Diag, *this, SourceLocation(), Invalid);
652 void SourceManager::overrideFileContents(const FileEntry *SourceFile,
653 llvm::MemoryBuffer *Buffer,
655 const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
656 assert(IR && "getOrCreateContentCache() cannot return NULL");
658 const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(Buffer, DoNotFree);
659 const_cast<SrcMgr::ContentCache *>(IR)->BufferOverridden = true;
661 getOverriddenFilesInfo().OverriddenFilesWithBuffer.insert(SourceFile);
664 void SourceManager::overrideFileContents(const FileEntry *SourceFile,
665 const FileEntry *NewFile) {
666 assert(SourceFile->getSize() == NewFile->getSize() &&
667 "Different sizes, use the FileManager to create a virtual file with "
669 assert(FileInfos.count(SourceFile) == 0 &&
670 "This function should be called at the initialization stage, before "
671 "any parsing occurs.");
672 getOverriddenFilesInfo().OverriddenFiles[SourceFile] = NewFile;
675 void SourceManager::disableFileContentsOverride(const FileEntry *File) {
676 if (!isFileOverridden(File))
679 const SrcMgr::ContentCache *IR = getOrCreateContentCache(File);
680 const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(nullptr);
681 const_cast<SrcMgr::ContentCache *>(IR)->ContentsEntry = IR->OrigEntry;
683 assert(OverriddenFilesInfo);
684 OverriddenFilesInfo->OverriddenFiles.erase(File);
685 OverriddenFilesInfo->OverriddenFilesWithBuffer.erase(File);
688 void SourceManager::setFileIsTransient(const FileEntry *File) {
689 const SrcMgr::ContentCache *CC = getOrCreateContentCache(File);
690 const_cast<SrcMgr::ContentCache *>(CC)->IsTransient = true;
693 StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const {
694 bool MyInvalid = false;
695 const SLocEntry &SLoc = getSLocEntry(FID, &MyInvalid);
696 if (!SLoc.isFile() || MyInvalid) {
699 return "<<<<<INVALID SOURCE LOCATION>>>>>";
702 const llvm::MemoryBuffer *Buf = SLoc.getFile().getContentCache()->getBuffer(
703 Diag, *this, SourceLocation(), &MyInvalid);
705 *Invalid = MyInvalid;
708 return "<<<<<INVALID SOURCE LOCATION>>>>>";
710 return Buf->getBuffer();
713 //===----------------------------------------------------------------------===//
714 // SourceLocation manipulation methods.
715 //===----------------------------------------------------------------------===//
717 /// Return the FileID for a SourceLocation.
719 /// This is the cache-miss path of getFileID. Not as hot as that function, but
720 /// still very important. It is responsible for finding the entry in the
721 /// SLocEntry tables that contains the specified location.
722 FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
724 return FileID::get(0);
726 // Now it is time to search for the correct file. See where the SLocOffset
727 // sits in the global view and consult local or loaded buffers for it.
728 if (SLocOffset < NextLocalOffset)
729 return getFileIDLocal(SLocOffset);
730 return getFileIDLoaded(SLocOffset);
733 /// Return the FileID for a SourceLocation with a low offset.
735 /// This function knows that the SourceLocation is in a local buffer, not a
737 FileID SourceManager::getFileIDLocal(unsigned SLocOffset) const {
738 assert(SLocOffset < NextLocalOffset && "Bad function choice");
740 // After the first and second level caches, I see two common sorts of
741 // behavior: 1) a lot of searched FileID's are "near" the cached file
742 // location or are "near" the cached expansion location. 2) others are just
743 // completely random and may be a very long way away.
745 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
746 // then we fall back to a less cache efficient, but more scalable, binary
747 // search to find the location.
749 // See if this is near the file point - worst case we start scanning from the
750 // most newly created FileID.
751 const SrcMgr::SLocEntry *I;
753 if (LastFileIDLookup.ID < 0 ||
754 LocalSLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
755 // Neither loc prunes our search.
756 I = LocalSLocEntryTable.end();
758 // Perhaps it is near the file point.
759 I = LocalSLocEntryTable.begin()+LastFileIDLookup.ID;
762 // Find the FileID that contains this. "I" is an iterator that points to a
763 // FileID whose offset is known to be larger than SLocOffset.
764 unsigned NumProbes = 0;
767 if (I->getOffset() <= SLocOffset) {
768 FileID Res = FileID::get(int(I - LocalSLocEntryTable.begin()));
770 // If this isn't an expansion, remember it. We have good locality across
772 if (!I->isExpansion())
773 LastFileIDLookup = Res;
774 NumLinearScans += NumProbes+1;
777 if (++NumProbes == 8)
781 // Convert "I" back into an index. We know that it is an entry whose index is
782 // larger than the offset we are looking for.
783 unsigned GreaterIndex = I - LocalSLocEntryTable.begin();
784 // LessIndex - This is the lower bound of the range that we're searching.
785 // We know that the offset corresponding to the FileID is is less than
787 unsigned LessIndex = 0;
790 bool Invalid = false;
791 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
792 unsigned MidOffset = getLocalSLocEntry(MiddleIndex, &Invalid).getOffset();
794 return FileID::get(0);
798 // If the offset of the midpoint is too large, chop the high side of the
799 // range to the midpoint.
800 if (MidOffset > SLocOffset) {
801 GreaterIndex = MiddleIndex;
805 // If the middle index contains the value, succeed and return.
806 // FIXME: This could be made faster by using a function that's aware of
807 // being in the local area.
808 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
809 FileID Res = FileID::get(MiddleIndex);
811 // If this isn't a macro expansion, remember it. We have good locality
812 // across FileID lookups.
813 if (!LocalSLocEntryTable[MiddleIndex].isExpansion())
814 LastFileIDLookup = Res;
815 NumBinaryProbes += NumProbes;
819 // Otherwise, move the low-side up to the middle index.
820 LessIndex = MiddleIndex;
824 /// Return the FileID for a SourceLocation with a high offset.
826 /// This function knows that the SourceLocation is in a loaded buffer, not a
828 FileID SourceManager::getFileIDLoaded(unsigned SLocOffset) const {
829 // Sanity checking, otherwise a bug may lead to hanging in release build.
830 if (SLocOffset < CurrentLoadedOffset) {
831 assert(0 && "Invalid SLocOffset or bad function choice");
835 // Essentially the same as the local case, but the loaded array is sorted
836 // in the other direction.
838 // First do a linear scan from the last lookup position, if possible.
840 int LastID = LastFileIDLookup.ID;
841 if (LastID >= 0 || getLoadedSLocEntryByID(LastID).getOffset() < SLocOffset)
844 I = (-LastID - 2) + 1;
847 for (NumProbes = 0; NumProbes < 8; ++NumProbes, ++I) {
848 // Make sure the entry is loaded!
849 const SrcMgr::SLocEntry &E = getLoadedSLocEntry(I);
850 if (E.getOffset() <= SLocOffset) {
851 FileID Res = FileID::get(-int(I) - 2);
853 if (!E.isExpansion())
854 LastFileIDLookup = Res;
855 NumLinearScans += NumProbes + 1;
860 // Linear scan failed. Do the binary search. Note the reverse sorting of the
861 // table: GreaterIndex is the one where the offset is greater, which is
862 // actually a lower index!
863 unsigned GreaterIndex = I;
864 unsigned LessIndex = LoadedSLocEntryTable.size();
868 unsigned MiddleIndex = (LessIndex - GreaterIndex) / 2 + GreaterIndex;
869 const SrcMgr::SLocEntry &E = getLoadedSLocEntry(MiddleIndex);
870 if (E.getOffset() == 0)
871 return FileID(); // invalid entry.
875 if (E.getOffset() > SLocOffset) {
876 // Sanity checking, otherwise a bug may lead to hanging in release build.
877 if (GreaterIndex == MiddleIndex) {
878 assert(0 && "binary search missed the entry");
881 GreaterIndex = MiddleIndex;
885 if (isOffsetInFileID(FileID::get(-int(MiddleIndex) - 2), SLocOffset)) {
886 FileID Res = FileID::get(-int(MiddleIndex) - 2);
887 if (!E.isExpansion())
888 LastFileIDLookup = Res;
889 NumBinaryProbes += NumProbes;
893 // Sanity checking, otherwise a bug may lead to hanging in release build.
894 if (LessIndex == MiddleIndex) {
895 assert(0 && "binary search missed the entry");
898 LessIndex = MiddleIndex;
902 SourceLocation SourceManager::
903 getExpansionLocSlowCase(SourceLocation Loc) const {
905 // Note: If Loc indicates an offset into a token that came from a macro
906 // expansion (e.g. the 5th character of the token) we do not want to add
907 // this offset when going to the expansion location. The expansion
908 // location is the macro invocation, which the offset has nothing to do
909 // with. This is unlike when we get the spelling loc, because the offset
910 // directly correspond to the token whose spelling we're inspecting.
911 Loc = getSLocEntry(getFileID(Loc)).getExpansion().getExpansionLocStart();
912 } while (!Loc.isFileID());
917 SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
919 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
920 Loc = getSLocEntry(LocInfo.first).getExpansion().getSpellingLoc();
921 Loc = Loc.getLocWithOffset(LocInfo.second);
922 } while (!Loc.isFileID());
926 SourceLocation SourceManager::getFileLocSlowCase(SourceLocation Loc) const {
928 if (isMacroArgExpansion(Loc))
929 Loc = getImmediateSpellingLoc(Loc);
931 Loc = getImmediateExpansionRange(Loc).getBegin();
932 } while (!Loc.isFileID());
937 std::pair<FileID, unsigned>
938 SourceManager::getDecomposedExpansionLocSlowCase(
939 const SrcMgr::SLocEntry *E) const {
940 // If this is an expansion record, walk through all the expansion points.
945 Loc = E->getExpansion().getExpansionLocStart();
947 FID = getFileID(Loc);
948 E = &getSLocEntry(FID);
949 Offset = Loc.getOffset()-E->getOffset();
950 } while (!Loc.isFileID());
952 return std::make_pair(FID, Offset);
955 std::pair<FileID, unsigned>
956 SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
957 unsigned Offset) const {
958 // If this is an expansion record, walk through all the expansion points.
962 Loc = E->getExpansion().getSpellingLoc();
963 Loc = Loc.getLocWithOffset(Offset);
965 FID = getFileID(Loc);
966 E = &getSLocEntry(FID);
967 Offset = Loc.getOffset()-E->getOffset();
968 } while (!Loc.isFileID());
970 return std::make_pair(FID, Offset);
973 /// getImmediateSpellingLoc - Given a SourceLocation object, return the
974 /// spelling location referenced by the ID. This is the first level down
975 /// towards the place where the characters that make up the lexed token can be
976 /// found. This should not generally be used by clients.
977 SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{
978 if (Loc.isFileID()) return Loc;
979 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
980 Loc = getSLocEntry(LocInfo.first).getExpansion().getSpellingLoc();
981 return Loc.getLocWithOffset(LocInfo.second);
984 /// getImmediateExpansionRange - Loc is required to be an expansion location.
985 /// Return the start/end of the expansion information.
987 SourceManager::getImmediateExpansionRange(SourceLocation Loc) const {
988 assert(Loc.isMacroID() && "Not a macro expansion loc!");
989 const ExpansionInfo &Expansion = getSLocEntry(getFileID(Loc)).getExpansion();
990 return Expansion.getExpansionLocRange();
993 SourceLocation SourceManager::getTopMacroCallerLoc(SourceLocation Loc) const {
994 while (isMacroArgExpansion(Loc))
995 Loc = getImmediateSpellingLoc(Loc);
999 /// getExpansionRange - Given a SourceLocation object, return the range of
1000 /// tokens covered by the expansion in the ultimate file.
1001 CharSourceRange SourceManager::getExpansionRange(SourceLocation Loc) const {
1003 return CharSourceRange(SourceRange(Loc, Loc), true);
1005 CharSourceRange Res = getImmediateExpansionRange(Loc);
1007 // Fully resolve the start and end locations to their ultimate expansion
1009 while (!Res.getBegin().isFileID())
1010 Res.setBegin(getImmediateExpansionRange(Res.getBegin()).getBegin());
1011 while (!Res.getEnd().isFileID()) {
1012 CharSourceRange EndRange = getImmediateExpansionRange(Res.getEnd());
1013 Res.setEnd(EndRange.getEnd());
1014 Res.setTokenRange(EndRange.isTokenRange());
1019 bool SourceManager::isMacroArgExpansion(SourceLocation Loc,
1020 SourceLocation *StartLoc) const {
1021 if (!Loc.isMacroID()) return false;
1023 FileID FID = getFileID(Loc);
1024 const SrcMgr::ExpansionInfo &Expansion = getSLocEntry(FID).getExpansion();
1025 if (!Expansion.isMacroArgExpansion()) return false;
1028 *StartLoc = Expansion.getExpansionLocStart();
1032 bool SourceManager::isMacroBodyExpansion(SourceLocation Loc) const {
1033 if (!Loc.isMacroID()) return false;
1035 FileID FID = getFileID(Loc);
1036 const SrcMgr::ExpansionInfo &Expansion = getSLocEntry(FID).getExpansion();
1037 return Expansion.isMacroBodyExpansion();
1040 bool SourceManager::isAtStartOfImmediateMacroExpansion(SourceLocation Loc,
1041 SourceLocation *MacroBegin) const {
1042 assert(Loc.isValid() && Loc.isMacroID() && "Expected a valid macro loc");
1044 std::pair<FileID, unsigned> DecompLoc = getDecomposedLoc(Loc);
1045 if (DecompLoc.second > 0)
1046 return false; // Does not point at the start of expansion range.
1048 bool Invalid = false;
1049 const SrcMgr::ExpansionInfo &ExpInfo =
1050 getSLocEntry(DecompLoc.first, &Invalid).getExpansion();
1053 SourceLocation ExpLoc = ExpInfo.getExpansionLocStart();
1055 if (ExpInfo.isMacroArgExpansion()) {
1056 // For macro argument expansions, check if the previous FileID is part of
1057 // the same argument expansion, in which case this Loc is not at the
1058 // beginning of the expansion.
1059 FileID PrevFID = getPreviousFileID(DecompLoc.first);
1060 if (!PrevFID.isInvalid()) {
1061 const SrcMgr::SLocEntry &PrevEntry = getSLocEntry(PrevFID, &Invalid);
1064 if (PrevEntry.isExpansion() &&
1065 PrevEntry.getExpansion().getExpansionLocStart() == ExpLoc)
1071 *MacroBegin = ExpLoc;
1075 bool SourceManager::isAtEndOfImmediateMacroExpansion(SourceLocation Loc,
1076 SourceLocation *MacroEnd) const {
1077 assert(Loc.isValid() && Loc.isMacroID() && "Expected a valid macro loc");
1079 FileID FID = getFileID(Loc);
1080 SourceLocation NextLoc = Loc.getLocWithOffset(1);
1081 if (isInFileID(NextLoc, FID))
1082 return false; // Does not point at the end of expansion range.
1084 bool Invalid = false;
1085 const SrcMgr::ExpansionInfo &ExpInfo =
1086 getSLocEntry(FID, &Invalid).getExpansion();
1090 if (ExpInfo.isMacroArgExpansion()) {
1091 // For macro argument expansions, check if the next FileID is part of the
1092 // same argument expansion, in which case this Loc is not at the end of the
1094 FileID NextFID = getNextFileID(FID);
1095 if (!NextFID.isInvalid()) {
1096 const SrcMgr::SLocEntry &NextEntry = getSLocEntry(NextFID, &Invalid);
1099 if (NextEntry.isExpansion() &&
1100 NextEntry.getExpansion().getExpansionLocStart() ==
1101 ExpInfo.getExpansionLocStart())
1107 *MacroEnd = ExpInfo.getExpansionLocEnd();
1111 //===----------------------------------------------------------------------===//
1112 // Queries about the code at a SourceLocation.
1113 //===----------------------------------------------------------------------===//
1115 /// getCharacterData - Return a pointer to the start of the specified location
1116 /// in the appropriate MemoryBuffer.
1117 const char *SourceManager::getCharacterData(SourceLocation SL,
1118 bool *Invalid) const {
1119 // Note that this is a hot function in the getSpelling() path, which is
1120 // heavily used by -E mode.
1121 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
1123 // Note that calling 'getBuffer()' may lazily page in a source file.
1124 bool CharDataInvalid = false;
1125 const SLocEntry &Entry = getSLocEntry(LocInfo.first, &CharDataInvalid);
1126 if (CharDataInvalid || !Entry.isFile()) {
1130 return "<<<<INVALID BUFFER>>>>";
1132 const llvm::MemoryBuffer *Buffer =
1133 Entry.getFile().getContentCache()->getBuffer(
1134 Diag, *this, SourceLocation(), &CharDataInvalid);
1136 *Invalid = CharDataInvalid;
1137 return Buffer->getBufferStart() + (CharDataInvalid? 0 : LocInfo.second);
1140 /// getColumnNumber - Return the column # for the specified file position.
1141 /// this is significantly cheaper to compute than the line number.
1142 unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos,
1143 bool *Invalid) const {
1144 bool MyInvalid = false;
1145 const llvm::MemoryBuffer *MemBuf = getBuffer(FID, &MyInvalid);
1147 *Invalid = MyInvalid;
1152 // It is okay to request a position just past the end of the buffer.
1153 if (FilePos > MemBuf->getBufferSize()) {
1159 const char *Buf = MemBuf->getBufferStart();
1160 // See if we just calculated the line number for this FilePos and can use
1161 // that to lookup the start of the line instead of searching for it.
1162 if (LastLineNoFileIDQuery == FID &&
1163 LastLineNoContentCache->SourceLineCache != nullptr &&
1164 LastLineNoResult < LastLineNoContentCache->NumLines) {
1165 unsigned *SourceLineCache = LastLineNoContentCache->SourceLineCache;
1166 unsigned LineStart = SourceLineCache[LastLineNoResult - 1];
1167 unsigned LineEnd = SourceLineCache[LastLineNoResult];
1168 if (FilePos >= LineStart && FilePos < LineEnd) {
1169 // LineEnd is the LineStart of the next line.
1170 // A line ends with separator LF or CR+LF on Windows.
1171 // FilePos might point to the last separator,
1172 // but we need a column number at most 1 + the last column.
1173 if (FilePos + 1 == LineEnd && FilePos > LineStart) {
1174 if (Buf[FilePos - 1] == '\r' || Buf[FilePos - 1] == '\n')
1177 return FilePos - LineStart + 1;
1181 unsigned LineStart = FilePos;
1182 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
1184 return FilePos-LineStart+1;
1187 // isInvalid - Return the result of calling loc.isInvalid(), and
1188 // if Invalid is not null, set its value to same.
1189 template<typename LocType>
1190 static bool isInvalid(LocType Loc, bool *Invalid) {
1191 bool MyInvalid = Loc.isInvalid();
1193 *Invalid = MyInvalid;
1197 unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc,
1198 bool *Invalid) const {
1199 if (isInvalid(Loc, Invalid)) return 0;
1200 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
1201 return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
1204 unsigned SourceManager::getExpansionColumnNumber(SourceLocation Loc,
1205 bool *Invalid) const {
1206 if (isInvalid(Loc, Invalid)) return 0;
1207 std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
1208 return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
1211 unsigned SourceManager::getPresumedColumnNumber(SourceLocation Loc,
1212 bool *Invalid) const {
1213 PresumedLoc PLoc = getPresumedLoc(Loc);
1214 if (isInvalid(PLoc, Invalid)) return 0;
1215 return PLoc.getColumn();
1219 #include <emmintrin.h>
1222 static LLVM_ATTRIBUTE_NOINLINE void
1223 ComputeLineNumbers(DiagnosticsEngine &Diag, ContentCache *FI,
1224 llvm::BumpPtrAllocator &Alloc,
1225 const SourceManager &SM, bool &Invalid);
1226 static void ComputeLineNumbers(DiagnosticsEngine &Diag, ContentCache *FI,
1227 llvm::BumpPtrAllocator &Alloc,
1228 const SourceManager &SM, bool &Invalid) {
1229 // Note that calling 'getBuffer()' may lazily page in the file.
1230 const MemoryBuffer *Buffer =
1231 FI->getBuffer(Diag, SM, SourceLocation(), &Invalid);
1235 // Find the file offsets of all of the *physical* source lines. This does
1236 // not look at trigraphs, escaped newlines, or anything else tricky.
1237 SmallVector<unsigned, 256> LineOffsets;
1239 // Line #1 starts at char 0.
1240 LineOffsets.push_back(0);
1242 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
1243 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
1246 // Skip over the contents of the line.
1247 while (Buf[I] != '\n' && Buf[I] != '\r' && Buf[I] != '\0')
1250 if (Buf[I] == '\n' || Buf[I] == '\r') {
1251 // If this is \r\n, skip both characters.
1252 if (Buf[I] == '\r' && Buf[I+1] == '\n')
1255 LineOffsets.push_back(I);
1257 // Otherwise, this is a NUL. If end of file, exit.
1258 if (Buf+I == End) break;
1263 // Copy the offsets into the FileInfo structure.
1264 FI->NumLines = LineOffsets.size();
1265 FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size());
1266 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
1269 /// getLineNumber - Given a SourceLocation, return the spelling line number
1270 /// for the position indicated. This requires building and caching a table of
1271 /// line offsets for the MemoryBuffer, so this is not cheap: use only when
1272 /// about to emit a diagnostic.
1273 unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos,
1274 bool *Invalid) const {
1275 if (FID.isInvalid()) {
1281 ContentCache *Content;
1282 if (LastLineNoFileIDQuery == FID)
1283 Content = LastLineNoContentCache;
1285 bool MyInvalid = false;
1286 const SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
1287 if (MyInvalid || !Entry.isFile()) {
1293 Content = const_cast<ContentCache*>(Entry.getFile().getContentCache());
1296 // If this is the first use of line information for this buffer, compute the
1297 /// SourceLineCache for it on demand.
1298 if (!Content->SourceLineCache) {
1299 bool MyInvalid = false;
1300 ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid);
1302 *Invalid = MyInvalid;
1308 // Okay, we know we have a line number table. Do a binary search to find the
1309 // line number that this character position lands on.
1310 unsigned *SourceLineCache = Content->SourceLineCache;
1311 unsigned *SourceLineCacheStart = SourceLineCache;
1312 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
1314 unsigned QueriedFilePos = FilePos+1;
1316 // FIXME: I would like to be convinced that this code is worth being as
1317 // complicated as it is, binary search isn't that slow.
1319 // If it is worth being optimized, then in my opinion it could be more
1320 // performant, simpler, and more obviously correct by just "galloping" outward
1321 // from the queried file position. In fact, this could be incorporated into a
1322 // generic algorithm such as lower_bound_with_hint.
1324 // If someone gives me a test case where this matters, and I will do it! - DWD
1326 // If the previous query was to the same file, we know both the file pos from
1327 // that query and the line number returned. This allows us to narrow the
1328 // search space from the entire file to something near the match.
1329 if (LastLineNoFileIDQuery == FID) {
1330 if (QueriedFilePos >= LastLineNoFilePos) {
1331 // FIXME: Potential overflow?
1332 SourceLineCache = SourceLineCache+LastLineNoResult-1;
1334 // The query is likely to be nearby the previous one. Here we check to
1335 // see if it is within 5, 10 or 20 lines. It can be far away in cases
1336 // where big comment blocks and vertical whitespace eat up lines but
1337 // contribute no tokens.
1338 if (SourceLineCache+5 < SourceLineCacheEnd) {
1339 if (SourceLineCache[5] > QueriedFilePos)
1340 SourceLineCacheEnd = SourceLineCache+5;
1341 else if (SourceLineCache+10 < SourceLineCacheEnd) {
1342 if (SourceLineCache[10] > QueriedFilePos)
1343 SourceLineCacheEnd = SourceLineCache+10;
1344 else if (SourceLineCache+20 < SourceLineCacheEnd) {
1345 if (SourceLineCache[20] > QueriedFilePos)
1346 SourceLineCacheEnd = SourceLineCache+20;
1351 if (LastLineNoResult < Content->NumLines)
1352 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
1357 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
1358 unsigned LineNo = Pos-SourceLineCacheStart;
1360 LastLineNoFileIDQuery = FID;
1361 LastLineNoContentCache = Content;
1362 LastLineNoFilePos = QueriedFilePos;
1363 LastLineNoResult = LineNo;
1367 unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc,
1368 bool *Invalid) const {
1369 if (isInvalid(Loc, Invalid)) return 0;
1370 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
1371 return getLineNumber(LocInfo.first, LocInfo.second);
1373 unsigned SourceManager::getExpansionLineNumber(SourceLocation Loc,
1374 bool *Invalid) const {
1375 if (isInvalid(Loc, Invalid)) return 0;
1376 std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
1377 return getLineNumber(LocInfo.first, LocInfo.second);
1379 unsigned SourceManager::getPresumedLineNumber(SourceLocation Loc,
1380 bool *Invalid) const {
1381 PresumedLoc PLoc = getPresumedLoc(Loc);
1382 if (isInvalid(PLoc, Invalid)) return 0;
1383 return PLoc.getLine();
1386 /// getFileCharacteristic - return the file characteristic of the specified
1387 /// source location, indicating whether this is a normal file, a system
1388 /// header, or an "implicit extern C" system header.
1390 /// This state can be modified with flags on GNU linemarker directives like:
1392 /// which changes all source locations in the current file after that to be
1393 /// considered to be from a system header.
1394 SrcMgr::CharacteristicKind
1395 SourceManager::getFileCharacteristic(SourceLocation Loc) const {
1396 assert(Loc.isValid() && "Can't get file characteristic of invalid loc!");
1397 std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
1398 bool Invalid = false;
1399 const SLocEntry &SEntry = getSLocEntry(LocInfo.first, &Invalid);
1400 if (Invalid || !SEntry.isFile())
1403 const SrcMgr::FileInfo &FI = SEntry.getFile();
1405 // If there are no #line directives in this file, just return the whole-file
1407 if (!FI.hasLineDirectives())
1408 return FI.getFileCharacteristic();
1410 assert(LineTable && "Can't have linetable entries without a LineTable!");
1411 // See if there is a #line directive before the location.
1412 const LineEntry *Entry =
1413 LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second);
1415 // If this is before the first line marker, use the file characteristic.
1417 return FI.getFileCharacteristic();
1419 return Entry->FileKind;
1422 /// Return the filename or buffer identifier of the buffer the location is in.
1423 /// Note that this name does not respect \#line directives. Use getPresumedLoc
1424 /// for normal clients.
1425 StringRef SourceManager::getBufferName(SourceLocation Loc,
1426 bool *Invalid) const {
1427 if (isInvalid(Loc, Invalid)) return "<invalid loc>";
1429 return getBuffer(getFileID(Loc), Invalid)->getBufferIdentifier();
1432 /// getPresumedLoc - This method returns the "presumed" location of a
1433 /// SourceLocation specifies. A "presumed location" can be modified by \#line
1434 /// or GNU line marker directives. This provides a view on the data that a
1435 /// user should see in diagnostics, for example.
1437 /// Note that a presumed location is always given as the expansion point of an
1438 /// expansion location, not at the spelling location.
1439 PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc,
1440 bool UseLineDirectives) const {
1441 if (Loc.isInvalid()) return PresumedLoc();
1443 // Presumed locations are always for expansion points.
1444 std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
1446 bool Invalid = false;
1447 const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid);
1448 if (Invalid || !Entry.isFile())
1449 return PresumedLoc();
1451 const SrcMgr::FileInfo &FI = Entry.getFile();
1452 const SrcMgr::ContentCache *C = FI.getContentCache();
1454 // To get the source name, first consult the FileEntry (if one exists)
1455 // before the MemBuffer as this will avoid unnecessarily paging in the
1457 FileID FID = LocInfo.first;
1460 Filename = C->OrigEntry->getName();
1462 Filename = C->getBuffer(Diag, *this)->getBufferIdentifier();
1464 unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second, &Invalid);
1466 return PresumedLoc();
1467 unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second, &Invalid);
1469 return PresumedLoc();
1471 SourceLocation IncludeLoc = FI.getIncludeLoc();
1473 // If we have #line directives in this file, update and overwrite the physical
1474 // location info if appropriate.
1475 if (UseLineDirectives && FI.hasLineDirectives()) {
1476 assert(LineTable && "Can't have linetable entries without a LineTable!");
1477 // See if there is a #line directive before this. If so, get it.
1478 if (const LineEntry *Entry =
1479 LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second)) {
1480 // If the LineEntry indicates a filename, use it.
1481 if (Entry->FilenameID != -1) {
1482 Filename = LineTable->getFilename(Entry->FilenameID);
1483 // The contents of files referenced by #line are not in the
1485 FID = FileID::get(0);
1488 // Use the line number specified by the LineEntry. This line number may
1489 // be multiple lines down from the line entry. Add the difference in
1490 // physical line numbers from the query point and the line marker to the
1492 unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset);
1493 LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1);
1495 // Note that column numbers are not molested by line markers.
1497 // Handle virtual #include manipulation.
1498 if (Entry->IncludeOffset) {
1499 IncludeLoc = getLocForStartOfFile(LocInfo.first);
1500 IncludeLoc = IncludeLoc.getLocWithOffset(Entry->IncludeOffset);
1505 return PresumedLoc(Filename.data(), FID, LineNo, ColNo, IncludeLoc);
1508 /// Returns whether the PresumedLoc for a given SourceLocation is
1509 /// in the main file.
1511 /// This computes the "presumed" location for a SourceLocation, then checks
1512 /// whether it came from a file other than the main file. This is different
1513 /// from isWrittenInMainFile() because it takes line marker directives into
1515 bool SourceManager::isInMainFile(SourceLocation Loc) const {
1516 if (Loc.isInvalid()) return false;
1518 // Presumed locations are always for expansion points.
1519 std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
1521 bool Invalid = false;
1522 const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid);
1523 if (Invalid || !Entry.isFile())
1526 const SrcMgr::FileInfo &FI = Entry.getFile();
1528 // Check if there is a line directive for this location.
1529 if (FI.hasLineDirectives())
1530 if (const LineEntry *Entry =
1531 LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second))
1532 if (Entry->IncludeOffset)
1535 return FI.getIncludeLoc().isInvalid();
1538 /// The size of the SLocEntry that \p FID represents.
1539 unsigned SourceManager::getFileIDSize(FileID FID) const {
1540 bool Invalid = false;
1541 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1546 unsigned NextOffset;
1547 if ((ID > 0 && unsigned(ID+1) == local_sloc_entry_size()))
1548 NextOffset = getNextLocalOffset();
1549 else if (ID+1 == -1)
1550 NextOffset = MaxLoadedOffset;
1552 NextOffset = getSLocEntry(FileID::get(ID+1)).getOffset();
1554 return NextOffset - Entry.getOffset() - 1;
1557 //===----------------------------------------------------------------------===//
1558 // Other miscellaneous methods.
1559 //===----------------------------------------------------------------------===//
1561 /// Retrieve the inode for the given file entry, if possible.
1563 /// This routine involves a system call, and therefore should only be used
1564 /// in non-performance-critical code.
1565 static Optional<llvm::sys::fs::UniqueID>
1566 getActualFileUID(const FileEntry *File) {
1570 llvm::sys::fs::UniqueID ID;
1571 if (llvm::sys::fs::getUniqueID(File->getName(), ID))
1577 /// Get the source location for the given file:line:col triplet.
1579 /// If the source file is included multiple times, the source location will
1580 /// be based upon an arbitrary inclusion.
1581 SourceLocation SourceManager::translateFileLineCol(const FileEntry *SourceFile,
1583 unsigned Col) const {
1584 assert(SourceFile && "Null source file!");
1585 assert(Line && Col && "Line and column should start from 1!");
1587 FileID FirstFID = translateFile(SourceFile);
1588 return translateLineCol(FirstFID, Line, Col);
1591 /// Get the FileID for the given file.
1593 /// If the source file is included multiple times, the FileID will be the
1594 /// first inclusion.
1595 FileID SourceManager::translateFile(const FileEntry *SourceFile) const {
1596 assert(SourceFile && "Null source file!");
1598 // Find the first file ID that corresponds to the given file.
1601 // First, check the main file ID, since it is common to look for a
1602 // location in the main file.
1603 Optional<llvm::sys::fs::UniqueID> SourceFileUID;
1604 Optional<StringRef> SourceFileName;
1605 if (MainFileID.isValid()) {
1606 bool Invalid = false;
1607 const SLocEntry &MainSLoc = getSLocEntry(MainFileID, &Invalid);
1611 if (MainSLoc.isFile()) {
1612 const ContentCache *MainContentCache
1613 = MainSLoc.getFile().getContentCache();
1614 if (!MainContentCache || !MainContentCache->OrigEntry) {
1615 // Can't do anything
1616 } else if (MainContentCache->OrigEntry == SourceFile) {
1617 FirstFID = MainFileID;
1619 // Fall back: check whether we have the same base name and inode
1620 // as the main file.
1621 const FileEntry *MainFile = MainContentCache->OrigEntry;
1622 SourceFileName = llvm::sys::path::filename(SourceFile->getName());
1623 if (*SourceFileName == llvm::sys::path::filename(MainFile->getName())) {
1624 SourceFileUID = getActualFileUID(SourceFile);
1625 if (SourceFileUID) {
1626 if (Optional<llvm::sys::fs::UniqueID> MainFileUID =
1627 getActualFileUID(MainFile)) {
1628 if (*SourceFileUID == *MainFileUID) {
1629 FirstFID = MainFileID;
1630 SourceFile = MainFile;
1639 if (FirstFID.isInvalid()) {
1640 // The location we're looking for isn't in the main file; look
1641 // through all of the local source locations.
1642 for (unsigned I = 0, N = local_sloc_entry_size(); I != N; ++I) {
1643 bool Invalid = false;
1644 const SLocEntry &SLoc = getLocalSLocEntry(I, &Invalid);
1648 if (SLoc.isFile() &&
1649 SLoc.getFile().getContentCache() &&
1650 SLoc.getFile().getContentCache()->OrigEntry == SourceFile) {
1651 FirstFID = FileID::get(I);
1655 // If that still didn't help, try the modules.
1656 if (FirstFID.isInvalid()) {
1657 for (unsigned I = 0, N = loaded_sloc_entry_size(); I != N; ++I) {
1658 const SLocEntry &SLoc = getLoadedSLocEntry(I);
1659 if (SLoc.isFile() &&
1660 SLoc.getFile().getContentCache() &&
1661 SLoc.getFile().getContentCache()->OrigEntry == SourceFile) {
1662 FirstFID = FileID::get(-int(I) - 2);
1669 // If we haven't found what we want yet, try again, but this time stat()
1670 // each of the files in case the files have changed since we originally
1672 if (FirstFID.isInvalid() &&
1674 (SourceFileName = llvm::sys::path::filename(SourceFile->getName()))) &&
1675 (SourceFileUID || (SourceFileUID = getActualFileUID(SourceFile)))) {
1676 bool Invalid = false;
1677 for (unsigned I = 0, N = local_sloc_entry_size(); I != N; ++I) {
1680 const SLocEntry &SLoc = getSLocEntry(IFileID, &Invalid);
1684 if (SLoc.isFile()) {
1685 const ContentCache *FileContentCache
1686 = SLoc.getFile().getContentCache();
1687 const FileEntry *Entry = FileContentCache ? FileContentCache->OrigEntry
1690 *SourceFileName == llvm::sys::path::filename(Entry->getName())) {
1691 if (Optional<llvm::sys::fs::UniqueID> EntryUID =
1692 getActualFileUID(Entry)) {
1693 if (*SourceFileUID == *EntryUID) {
1694 FirstFID = FileID::get(I);
1708 /// Get the source location in \arg FID for the given line:col.
1709 /// Returns null location if \arg FID is not a file SLocEntry.
1710 SourceLocation SourceManager::translateLineCol(FileID FID,
1712 unsigned Col) const {
1713 // Lines are used as a one-based index into a zero-based array. This assert
1714 // checks for possible buffer underruns.
1715 assert(Line && Col && "Line and column should start from 1!");
1717 if (FID.isInvalid())
1718 return SourceLocation();
1720 bool Invalid = false;
1721 const SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1723 return SourceLocation();
1725 if (!Entry.isFile())
1726 return SourceLocation();
1728 SourceLocation FileLoc = SourceLocation::getFileLoc(Entry.getOffset());
1730 if (Line == 1 && Col == 1)
1733 ContentCache *Content
1734 = const_cast<ContentCache *>(Entry.getFile().getContentCache());
1736 return SourceLocation();
1738 // If this is the first use of line information for this buffer, compute the
1739 // SourceLineCache for it on demand.
1740 if (!Content->SourceLineCache) {
1741 bool MyInvalid = false;
1742 ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid);
1744 return SourceLocation();
1747 if (Line > Content->NumLines) {
1748 unsigned Size = Content->getBuffer(Diag, *this)->getBufferSize();
1751 return FileLoc.getLocWithOffset(Size);
1754 const llvm::MemoryBuffer *Buffer = Content->getBuffer(Diag, *this);
1755 unsigned FilePos = Content->SourceLineCache[Line - 1];
1756 const char *Buf = Buffer->getBufferStart() + FilePos;
1757 unsigned BufLength = Buffer->getBufferSize() - FilePos;
1759 return FileLoc.getLocWithOffset(FilePos);
1763 // Check that the given column is valid.
1764 while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r')
1766 return FileLoc.getLocWithOffset(FilePos + i);
1769 /// Compute a map of macro argument chunks to their expanded source
1770 /// location. Chunks that are not part of a macro argument will map to an
1771 /// invalid source location. e.g. if a file contains one macro argument at
1772 /// offset 100 with length 10, this is how the map will be formed:
1773 /// 0 -> SourceLocation()
1774 /// 100 -> Expanded macro arg location
1775 /// 110 -> SourceLocation()
1776 void SourceManager::computeMacroArgsCache(MacroArgsMap &MacroArgsCache,
1778 assert(FID.isValid());
1780 // Initially no macro argument chunk is present.
1781 MacroArgsCache.insert(std::make_pair(0, SourceLocation()));
1786 // Stop if there are no more FileIDs to check.
1788 if (unsigned(ID) >= local_sloc_entry_size())
1790 } else if (ID == -1) {
1794 bool Invalid = false;
1795 const SrcMgr::SLocEntry &Entry = getSLocEntryByID(ID, &Invalid);
1798 if (Entry.isFile()) {
1799 SourceLocation IncludeLoc = Entry.getFile().getIncludeLoc();
1800 if (IncludeLoc.isInvalid())
1802 if (!isInFileID(IncludeLoc, FID))
1803 return; // No more files/macros that may be "contained" in this file.
1805 // Skip the files/macros of the #include'd file, we only care about macros
1806 // that lexed macro arguments from our file.
1807 if (Entry.getFile().NumCreatedFIDs)
1808 ID += Entry.getFile().NumCreatedFIDs - 1/*because of next ++ID*/;
1812 const ExpansionInfo &ExpInfo = Entry.getExpansion();
1814 if (ExpInfo.getExpansionLocStart().isFileID()) {
1815 if (!isInFileID(ExpInfo.getExpansionLocStart(), FID))
1816 return; // No more files/macros that may be "contained" in this file.
1819 if (!ExpInfo.isMacroArgExpansion())
1822 associateFileChunkWithMacroArgExp(MacroArgsCache, FID,
1823 ExpInfo.getSpellingLoc(),
1824 SourceLocation::getMacroLoc(Entry.getOffset()),
1825 getFileIDSize(FileID::get(ID)));
1829 void SourceManager::associateFileChunkWithMacroArgExp(
1830 MacroArgsMap &MacroArgsCache,
1832 SourceLocation SpellLoc,
1833 SourceLocation ExpansionLoc,
1834 unsigned ExpansionLength) const {
1835 if (!SpellLoc.isFileID()) {
1836 unsigned SpellBeginOffs = SpellLoc.getOffset();
1837 unsigned SpellEndOffs = SpellBeginOffs + ExpansionLength;
1839 // The spelling range for this macro argument expansion can span multiple
1840 // consecutive FileID entries. Go through each entry contained in the
1841 // spelling range and if one is itself a macro argument expansion, recurse
1842 // and associate the file chunk that it represents.
1844 FileID SpellFID; // Current FileID in the spelling range.
1845 unsigned SpellRelativeOffs;
1846 std::tie(SpellFID, SpellRelativeOffs) = getDecomposedLoc(SpellLoc);
1848 const SLocEntry &Entry = getSLocEntry(SpellFID);
1849 unsigned SpellFIDBeginOffs = Entry.getOffset();
1850 unsigned SpellFIDSize = getFileIDSize(SpellFID);
1851 unsigned SpellFIDEndOffs = SpellFIDBeginOffs + SpellFIDSize;
1852 const ExpansionInfo &Info = Entry.getExpansion();
1853 if (Info.isMacroArgExpansion()) {
1854 unsigned CurrSpellLength;
1855 if (SpellFIDEndOffs < SpellEndOffs)
1856 CurrSpellLength = SpellFIDSize - SpellRelativeOffs;
1858 CurrSpellLength = ExpansionLength;
1859 associateFileChunkWithMacroArgExp(MacroArgsCache, FID,
1860 Info.getSpellingLoc().getLocWithOffset(SpellRelativeOffs),
1861 ExpansionLoc, CurrSpellLength);
1864 if (SpellFIDEndOffs >= SpellEndOffs)
1865 return; // we covered all FileID entries in the spelling range.
1867 // Move to the next FileID entry in the spelling range.
1868 unsigned advance = SpellFIDSize - SpellRelativeOffs + 1;
1869 ExpansionLoc = ExpansionLoc.getLocWithOffset(advance);
1870 ExpansionLength -= advance;
1872 SpellRelativeOffs = 0;
1876 assert(SpellLoc.isFileID());
1879 if (!isInFileID(SpellLoc, FID, &BeginOffs))
1882 unsigned EndOffs = BeginOffs + ExpansionLength;
1884 // Add a new chunk for this macro argument. A previous macro argument chunk
1885 // may have been lexed again, so e.g. if the map is
1886 // 0 -> SourceLocation()
1887 // 100 -> Expanded loc #1
1888 // 110 -> SourceLocation()
1889 // and we found a new macro FileID that lexed from offset 105 with length 3,
1890 // the new map will be:
1891 // 0 -> SourceLocation()
1892 // 100 -> Expanded loc #1
1893 // 105 -> Expanded loc #2
1894 // 108 -> Expanded loc #1
1895 // 110 -> SourceLocation()
1897 // Since re-lexed macro chunks will always be the same size or less of
1898 // previous chunks, we only need to find where the ending of the new macro
1899 // chunk is mapped to and update the map with new begin/end mappings.
1901 MacroArgsMap::iterator I = MacroArgsCache.upper_bound(EndOffs);
1903 SourceLocation EndOffsMappedLoc = I->second;
1904 MacroArgsCache[BeginOffs] = ExpansionLoc;
1905 MacroArgsCache[EndOffs] = EndOffsMappedLoc;
1908 /// If \arg Loc points inside a function macro argument, the returned
1909 /// location will be the macro location in which the argument was expanded.
1910 /// If a macro argument is used multiple times, the expanded location will
1911 /// be at the first expansion of the argument.
1915 /// Passing a file location pointing at 'foo', will yield a macro location
1916 /// where 'foo' was expanded into.
1918 SourceManager::getMacroArgExpandedLocation(SourceLocation Loc) const {
1919 if (Loc.isInvalid() || !Loc.isFileID())
1924 std::tie(FID, Offset) = getDecomposedLoc(Loc);
1925 if (FID.isInvalid())
1928 std::unique_ptr<MacroArgsMap> &MacroArgsCache = MacroArgsCacheMap[FID];
1929 if (!MacroArgsCache) {
1930 MacroArgsCache = llvm::make_unique<MacroArgsMap>();
1931 computeMacroArgsCache(*MacroArgsCache, FID);
1934 assert(!MacroArgsCache->empty());
1935 MacroArgsMap::iterator I = MacroArgsCache->upper_bound(Offset);
1938 unsigned MacroArgBeginOffs = I->first;
1939 SourceLocation MacroArgExpandedLoc = I->second;
1940 if (MacroArgExpandedLoc.isValid())
1941 return MacroArgExpandedLoc.getLocWithOffset(Offset - MacroArgBeginOffs);
1946 std::pair<FileID, unsigned>
1947 SourceManager::getDecomposedIncludedLoc(FileID FID) const {
1948 if (FID.isInvalid())
1949 return std::make_pair(FileID(), 0);
1951 // Uses IncludedLocMap to retrieve/cache the decomposed loc.
1953 using DecompTy = std::pair<FileID, unsigned>;
1954 auto InsertOp = IncludedLocMap.try_emplace(FID);
1955 DecompTy &DecompLoc = InsertOp.first->second;
1956 if (!InsertOp.second)
1957 return DecompLoc; // already in map.
1959 SourceLocation UpperLoc;
1960 bool Invalid = false;
1961 const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
1963 if (Entry.isExpansion())
1964 UpperLoc = Entry.getExpansion().getExpansionLocStart();
1966 UpperLoc = Entry.getFile().getIncludeLoc();
1969 if (UpperLoc.isValid())
1970 DecompLoc = getDecomposedLoc(UpperLoc);
1975 /// Given a decomposed source location, move it up the include/expansion stack
1976 /// to the parent source location. If this is possible, return the decomposed
1977 /// version of the parent in Loc and return false. If Loc is the top-level
1978 /// entry, return true and don't modify it.
1979 static bool MoveUpIncludeHierarchy(std::pair<FileID, unsigned> &Loc,
1980 const SourceManager &SM) {
1981 std::pair<FileID, unsigned> UpperLoc = SM.getDecomposedIncludedLoc(Loc.first);
1982 if (UpperLoc.first.isInvalid())
1983 return true; // We reached the top.
1989 /// Return the cache entry for comparing the given file IDs
1990 /// for isBeforeInTranslationUnit.
1991 InBeforeInTUCacheEntry &SourceManager::getInBeforeInTUCache(FileID LFID,
1992 FileID RFID) const {
1993 // This is a magic number for limiting the cache size. It was experimentally
1994 // derived from a small Objective-C project (where the cache filled
1995 // out to ~250 items). We can make it larger if necessary.
1996 enum { MagicCacheSize = 300 };
1997 IsBeforeInTUCacheKey Key(LFID, RFID);
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
2003 if (IBTUCache.size() < MagicCacheSize)
2004 return IBTUCache[Key];
2006 // Otherwise, do a lookup that will not construct a new value.
2007 InBeforeInTUCache::iterator I = IBTUCache.find(Key);
2008 if (I != IBTUCache.end())
2011 // Fall back to the overflow value.
2012 return IBTUCacheOverflow;
2015 /// Determines the order of 2 source locations in the translation unit.
2017 /// \returns true if LHS source location comes before RHS, false otherwise.
2018 bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS,
2019 SourceLocation RHS) const {
2020 assert(LHS.isValid() && RHS.isValid() && "Passed invalid source location!");
2024 std::pair<FileID, unsigned> LOffs = getDecomposedLoc(LHS);
2025 std::pair<FileID, unsigned> ROffs = getDecomposedLoc(RHS);
2027 // getDecomposedLoc may have failed to return a valid FileID because, e.g. it
2028 // is a serialized one referring to a file that was removed after we loaded
2030 if (LOffs.first.isInvalid() || ROffs.first.isInvalid())
2031 return LOffs.first.isInvalid() && !ROffs.first.isInvalid();
2033 std::pair<bool, bool> InSameTU = isInTheSameTranslationUnit(LOffs, ROffs);
2035 return InSameTU.second;
2037 // If we arrived here, the location is either in a built-ins buffer or
2038 // associated with global inline asm. PR5662 and PR22576 are examples.
2040 StringRef LB = getBuffer(LOffs.first)->getBufferIdentifier();
2041 StringRef RB = getBuffer(ROffs.first)->getBufferIdentifier();
2042 bool LIsBuiltins = LB == "<built-in>";
2043 bool RIsBuiltins = RB == "<built-in>";
2044 // Sort built-in before non-built-in.
2045 if (LIsBuiltins || RIsBuiltins) {
2046 if (LIsBuiltins != RIsBuiltins)
2048 // Both are in built-in buffers, but from different files. We just claim that
2049 // lower IDs come first.
2050 return LOffs.first < ROffs.first;
2052 bool LIsAsm = LB == "<inline asm>";
2053 bool RIsAsm = RB == "<inline asm>";
2054 // Sort assembler after built-ins, but before the rest.
2055 if (LIsAsm || RIsAsm) {
2056 if (LIsAsm != RIsAsm)
2058 assert(LOffs.first == ROffs.first);
2061 bool LIsScratch = LB == "<scratch space>";
2062 bool RIsScratch = RB == "<scratch space>";
2063 // Sort scratch after inline asm, but before the rest.
2064 if (LIsScratch || RIsScratch) {
2065 if (LIsScratch != RIsScratch)
2067 return LOffs.second < ROffs.second;
2069 llvm_unreachable("Unsortable locations found");
2072 std::pair<bool, bool> SourceManager::isInTheSameTranslationUnit(
2073 std::pair<FileID, unsigned> &LOffs,
2074 std::pair<FileID, unsigned> &ROffs) const {
2075 // If the source locations are in the same file, just compare offsets.
2076 if (LOffs.first == ROffs.first)
2077 return std::make_pair(true, LOffs.second < ROffs.second);
2079 // If we are comparing a source location with multiple locations in the same
2080 // file, we get a big win by caching the result.
2081 InBeforeInTUCacheEntry &IsBeforeInTUCache =
2082 getInBeforeInTUCache(LOffs.first, ROffs.first);
2084 // If we are comparing a source location with multiple locations in the same
2085 // file, we get a big win by caching the result.
2086 if (IsBeforeInTUCache.isCacheValid(LOffs.first, ROffs.first))
2087 return std::make_pair(
2088 true, IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second));
2090 // Okay, we missed in the cache, start updating the cache for this query.
2091 IsBeforeInTUCache.setQueryFIDs(LOffs.first, ROffs.first,
2092 /*isLFIDBeforeRFID=*/LOffs.first.ID < ROffs.first.ID);
2094 // We need to find the common ancestor. The only way of doing this is to
2095 // build the complete include chain for one and then walking up the chain
2096 // of the other looking for a match.
2097 // We use a map from FileID to Offset to store the chain. Easier than writing
2098 // a custom set hash info that only depends on the first part of a pair.
2099 using LocSet = llvm::SmallDenseMap<FileID, unsigned, 16>;
2102 LChain.insert(LOffs);
2103 // We catch the case where LOffs is in a file included by ROffs and
2104 // quit early. The other way round unfortunately remains suboptimal.
2105 } while (LOffs.first != ROffs.first && !MoveUpIncludeHierarchy(LOffs, *this));
2107 while((I = LChain.find(ROffs.first)) == LChain.end()) {
2108 if (MoveUpIncludeHierarchy(ROffs, *this))
2109 break; // Met at topmost file.
2111 if (I != LChain.end())
2114 // If we exited because we found a nearest common ancestor, compare the
2115 // locations within the common file and cache them.
2116 if (LOffs.first == ROffs.first) {
2117 IsBeforeInTUCache.setCommonLoc(LOffs.first, LOffs.second, ROffs.second);
2118 return std::make_pair(
2119 true, IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second));
2121 // Clear the lookup cache, it depends on a common location.
2122 IsBeforeInTUCache.clear();
2123 return std::make_pair(false, false);
2126 void SourceManager::PrintStats() const {
2127 llvm::errs() << "\n*** Source Manager Stats:\n";
2128 llvm::errs() << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
2129 << " mem buffers mapped.\n";
2130 llvm::errs() << LocalSLocEntryTable.size() << " local SLocEntry's allocated ("
2131 << llvm::capacity_in_bytes(LocalSLocEntryTable)
2132 << " bytes of capacity), "
2133 << NextLocalOffset << "B of Sloc address space used.\n";
2134 llvm::errs() << LoadedSLocEntryTable.size()
2135 << " loaded SLocEntries allocated, "
2136 << MaxLoadedOffset - CurrentLoadedOffset
2137 << "B of Sloc address space used.\n";
2139 unsigned NumLineNumsComputed = 0;
2140 unsigned NumFileBytesMapped = 0;
2141 for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){
2142 NumLineNumsComputed += I->second->SourceLineCache != nullptr;
2143 NumFileBytesMapped += I->second->getSizeBytesMapped();
2145 unsigned NumMacroArgsComputed = MacroArgsCacheMap.size();
2147 llvm::errs() << NumFileBytesMapped << " bytes of files mapped, "
2148 << NumLineNumsComputed << " files with line #'s computed, "
2149 << NumMacroArgsComputed << " files with macro args computed.\n";
2150 llvm::errs() << "FileID scans: " << NumLinearScans << " linear, "
2151 << NumBinaryProbes << " binary.\n";
2154 LLVM_DUMP_METHOD void SourceManager::dump() const {
2155 llvm::raw_ostream &out = llvm::errs();
2157 auto DumpSLocEntry = [&](int ID, const SrcMgr::SLocEntry &Entry,
2158 llvm::Optional<unsigned> NextStart) {
2159 out << "SLocEntry <FileID " << ID << "> " << (Entry.isFile() ? "file" : "expansion")
2160 << " <SourceLocation " << Entry.getOffset() << ":";
2162 out << *NextStart << ">\n";
2165 if (Entry.isFile()) {
2166 auto &FI = Entry.getFile();
2167 if (FI.NumCreatedFIDs)
2168 out << " covers <FileID " << ID << ":" << int(ID + FI.NumCreatedFIDs)
2170 if (FI.getIncludeLoc().isValid())
2171 out << " included from " << FI.getIncludeLoc().getOffset() << "\n";
2172 if (auto *CC = FI.getContentCache()) {
2173 out << " for " << (CC->OrigEntry ? CC->OrigEntry->getName() : "<none>")
2175 if (CC->BufferOverridden)
2176 out << " contents overridden\n";
2177 if (CC->ContentsEntry != CC->OrigEntry) {
2178 out << " contents from "
2179 << (CC->ContentsEntry ? CC->ContentsEntry->getName() : "<none>")
2184 auto &EI = Entry.getExpansion();
2185 out << " spelling from " << EI.getSpellingLoc().getOffset() << "\n";
2186 out << " macro " << (EI.isMacroArgExpansion() ? "arg" : "body")
2187 << " range <" << EI.getExpansionLocStart().getOffset() << ":"
2188 << EI.getExpansionLocEnd().getOffset() << ">\n";
2192 // Dump local SLocEntries.
2193 for (unsigned ID = 0, NumIDs = LocalSLocEntryTable.size(); ID != NumIDs; ++ID) {
2194 DumpSLocEntry(ID, LocalSLocEntryTable[ID],
2195 ID == NumIDs - 1 ? NextLocalOffset
2196 : LocalSLocEntryTable[ID + 1].getOffset());
2198 // Dump loaded SLocEntries.
2199 llvm::Optional<unsigned> NextStart;
2200 for (unsigned Index = 0; Index != LoadedSLocEntryTable.size(); ++Index) {
2201 int ID = -(int)Index - 2;
2202 if (SLocEntryLoaded[Index]) {
2203 DumpSLocEntry(ID, LoadedSLocEntryTable[Index], NextStart);
2204 NextStart = LoadedSLocEntryTable[Index].getOffset();
2211 ExternalSLocEntrySource::~ExternalSLocEntrySource() = default;
2213 /// Return the amount of memory used by memory buffers, breaking down
2214 /// by heap-backed versus mmap'ed memory.
2215 SourceManager::MemoryBufferSizes SourceManager::getMemoryBufferSizes() const {
2216 size_t malloc_bytes = 0;
2217 size_t mmap_bytes = 0;
2219 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i)
2220 if (size_t sized_mapped = MemBufferInfos[i]->getSizeBytesMapped())
2221 switch (MemBufferInfos[i]->getMemoryBufferKind()) {
2222 case llvm::MemoryBuffer::MemoryBuffer_MMap:
2223 mmap_bytes += sized_mapped;
2225 case llvm::MemoryBuffer::MemoryBuffer_Malloc:
2226 malloc_bytes += sized_mapped;
2230 return MemoryBufferSizes(malloc_bytes, mmap_bytes);
2233 size_t SourceManager::getDataStructureSizes() const {
2234 size_t size = llvm::capacity_in_bytes(MemBufferInfos)
2235 + llvm::capacity_in_bytes(LocalSLocEntryTable)
2236 + llvm::capacity_in_bytes(LoadedSLocEntryTable)
2237 + llvm::capacity_in_bytes(SLocEntryLoaded)
2238 + llvm::capacity_in_bytes(FileInfos);
2240 if (OverriddenFilesInfo)
2241 size += llvm::capacity_in_bytes(OverriddenFilesInfo->OverriddenFiles);
2246 SourceManagerForFile::SourceManagerForFile(StringRef FileName,
2247 StringRef Content) {
2248 // This is referenced by `FileMgr` and will be released by `FileMgr` when it
2250 IntrusiveRefCntPtr<llvm::vfs::InMemoryFileSystem> InMemoryFileSystem(
2251 new llvm::vfs::InMemoryFileSystem);
2252 InMemoryFileSystem->addFile(
2254 llvm::MemoryBuffer::getMemBuffer(Content, FileName,
2255 /*RequiresNullTerminator=*/false));
2256 // This is passed to `SM` as reference, so the pointer has to be referenced
2257 // in `Environment` so that `FileMgr` can out-live this function scope.
2259 llvm::make_unique<FileManager>(FileSystemOptions(), InMemoryFileSystem);
2260 // This is passed to `SM` as reference, so the pointer has to be referenced
2261 // by `Environment` due to the same reason above.
2262 Diagnostics = llvm::make_unique<DiagnosticsEngine>(
2263 IntrusiveRefCntPtr<DiagnosticIDs>(new DiagnosticIDs),
2264 new DiagnosticOptions);
2265 SourceMgr = llvm::make_unique<SourceManager>(*Diagnostics, *FileMgr);
2266 FileID ID = SourceMgr->createFileID(FileMgr->getFile(FileName),
2267 SourceLocation(), clang::SrcMgr::C_User);
2268 assert(ID.isValid());
2269 SourceMgr->setMainFileID(ID);