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