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