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