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