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