SourceManager.cpp revision fc8f0e14ad142ed811e90fbd9a30e419e301c717
1//===--- SourceManager.cpp - Track and cache source files -----------------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file implements the SourceManager interface.
11//
12//===----------------------------------------------------------------------===//
13
14#include "clang/Basic/SourceManager.h"
15#include "clang/Basic/SourceManagerInternals.h"
16#include "clang/Basic/Diagnostic.h"
17#include "clang/Basic/FileManager.h"
18#include "llvm/ADT/StringSwitch.h"
19#include "llvm/ADT/Optional.h"
20#include "llvm/Support/Compiler.h"
21#include "llvm/Support/MemoryBuffer.h"
22#include "llvm/Support/raw_ostream.h"
23#include "llvm/Support/Path.h"
24#include <algorithm>
25#include <string>
26#include <cstring>
27#include <sys/stat.h>
28
29using namespace clang;
30using namespace SrcMgr;
31using llvm::MemoryBuffer;
32
33//===----------------------------------------------------------------------===//
34// SourceManager Helper Classes
35//===----------------------------------------------------------------------===//
36
37ContentCache::~ContentCache() {
38  if (shouldFreeBuffer())
39    delete Buffer.getPointer();
40}
41
42/// getSizeBytesMapped - Returns the number of bytes actually mapped for
43///  this ContentCache.  This can be 0 if the MemBuffer was not actually
44///  instantiated.
45unsigned ContentCache::getSizeBytesMapped() const {
46  return Buffer.getPointer() ? Buffer.getPointer()->getBufferSize() : 0;
47}
48
49/// getSize - Returns the size of the content encapsulated by this ContentCache.
50///  This can be the size of the source file or the size of an arbitrary
51///  scratch buffer.  If the ContentCache encapsulates a source file, that
52///  file is not lazily brought in from disk to satisfy this query.
53unsigned ContentCache::getSize() const {
54  return Buffer.getPointer() ? (unsigned) Buffer.getPointer()->getBufferSize()
55                             : (unsigned) ContentsEntry->getSize();
56}
57
58void ContentCache::replaceBuffer(const llvm::MemoryBuffer *B,
59                                 bool DoNotFree) {
60  assert(B != Buffer.getPointer());
61
62  if (shouldFreeBuffer())
63    delete Buffer.getPointer();
64  Buffer.setPointer(B);
65  Buffer.setInt(DoNotFree? DoNotFreeFlag : 0);
66}
67
68const llvm::MemoryBuffer *ContentCache::getBuffer(Diagnostic &Diag,
69                                                  const SourceManager &SM,
70                                                  SourceLocation Loc,
71                                                  bool *Invalid) const {
72  // Lazily create the Buffer for ContentCaches that wrap files.  If we already
73  // computed it, just return what we have.
74  if (Buffer.getPointer() || ContentsEntry == 0) {
75    if (Invalid)
76      *Invalid = isBufferInvalid();
77
78    return Buffer.getPointer();
79  }
80
81  std::string ErrorStr;
82  Buffer.setPointer(SM.getFileManager().getBufferForFile(ContentsEntry, &ErrorStr));
83
84  // If we were unable to open the file, then we are in an inconsistent
85  // situation where the content cache referenced a file which no longer
86  // exists. Most likely, we were using a stat cache with an invalid entry but
87  // the file could also have been removed during processing. Since we can't
88  // really deal with this situation, just create an empty buffer.
89  //
90  // FIXME: This is definitely not ideal, but our immediate clients can't
91  // currently handle returning a null entry here. Ideally we should detect
92  // that we are in an inconsistent situation and error out as quickly as
93  // possible.
94  if (!Buffer.getPointer()) {
95    const llvm::StringRef FillStr("<<<MISSING SOURCE FILE>>>\n");
96    Buffer.setPointer(MemoryBuffer::getNewMemBuffer(ContentsEntry->getSize(),
97                                                    "<invalid>"));
98    char *Ptr = const_cast<char*>(Buffer.getPointer()->getBufferStart());
99    for (unsigned i = 0, e = ContentsEntry->getSize(); i != e; ++i)
100      Ptr[i] = FillStr[i % FillStr.size()];
101
102    if (Diag.isDiagnosticInFlight())
103      Diag.SetDelayedDiagnostic(diag::err_cannot_open_file,
104                                ContentsEntry->getName(), ErrorStr);
105    else
106      Diag.Report(Loc, diag::err_cannot_open_file)
107        << ContentsEntry->getName() << ErrorStr;
108
109    Buffer.setInt(Buffer.getInt() | InvalidFlag);
110
111    if (Invalid) *Invalid = true;
112    return Buffer.getPointer();
113  }
114
115  // Check that the file's size is the same as in the file entry (which may
116  // have come from a stat cache).
117  if (getRawBuffer()->getBufferSize() != (size_t)ContentsEntry->getSize()) {
118    if (Diag.isDiagnosticInFlight())
119      Diag.SetDelayedDiagnostic(diag::err_file_modified,
120                                ContentsEntry->getName());
121    else
122      Diag.Report(Loc, diag::err_file_modified)
123        << ContentsEntry->getName();
124
125    Buffer.setInt(Buffer.getInt() | InvalidFlag);
126    if (Invalid) *Invalid = true;
127    return Buffer.getPointer();
128  }
129
130  // If the buffer is valid, check to see if it has a UTF Byte Order Mark
131  // (BOM).  We only support UTF-8 with and without a BOM right now.  See
132  // http://en.wikipedia.org/wiki/Byte_order_mark for more information.
133  llvm::StringRef BufStr = Buffer.getPointer()->getBuffer();
134  const char *InvalidBOM = llvm::StringSwitch<const char *>(BufStr)
135    .StartsWith("\xFE\xFF", "UTF-16 (BE)")
136    .StartsWith("\xFF\xFE", "UTF-16 (LE)")
137    .StartsWith("\x00\x00\xFE\xFF", "UTF-32 (BE)")
138    .StartsWith("\xFF\xFE\x00\x00", "UTF-32 (LE)")
139    .StartsWith("\x2B\x2F\x76", "UTF-7")
140    .StartsWith("\xF7\x64\x4C", "UTF-1")
141    .StartsWith("\xDD\x73\x66\x73", "UTF-EBCDIC")
142    .StartsWith("\x0E\xFE\xFF", "SDSU")
143    .StartsWith("\xFB\xEE\x28", "BOCU-1")
144    .StartsWith("\x84\x31\x95\x33", "GB-18030")
145    .Default(0);
146
147  if (InvalidBOM) {
148    Diag.Report(Loc, diag::err_unsupported_bom)
149      << InvalidBOM << ContentsEntry->getName();
150    Buffer.setInt(Buffer.getInt() | InvalidFlag);
151  }
152
153  if (Invalid)
154    *Invalid = isBufferInvalid();
155
156  return Buffer.getPointer();
157}
158
159unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) {
160  // Look up the filename in the string table, returning the pre-existing value
161  // if it exists.
162  llvm::StringMapEntry<unsigned> &Entry =
163    FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U);
164  if (Entry.getValue() != ~0U)
165    return Entry.getValue();
166
167  // Otherwise, assign this the next available ID.
168  Entry.setValue(FilenamesByID.size());
169  FilenamesByID.push_back(&Entry);
170  return FilenamesByID.size()-1;
171}
172
173/// AddLineNote - Add a line note to the line table that indicates that there
174/// is a #line at the specified FID/Offset location which changes the presumed
175/// location to LineNo/FilenameID.
176void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
177                                unsigned LineNo, int FilenameID) {
178  std::vector<LineEntry> &Entries = LineEntries[FID];
179
180  assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
181         "Adding line entries out of order!");
182
183  SrcMgr::CharacteristicKind Kind = SrcMgr::C_User;
184  unsigned IncludeOffset = 0;
185
186  if (!Entries.empty()) {
187    // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember
188    // that we are still in "foo.h".
189    if (FilenameID == -1)
190      FilenameID = Entries.back().FilenameID;
191
192    // If we are after a line marker that switched us to system header mode, or
193    // that set #include information, preserve it.
194    Kind = Entries.back().FileKind;
195    IncludeOffset = Entries.back().IncludeOffset;
196  }
197
198  Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, Kind,
199                                   IncludeOffset));
200}
201
202/// AddLineNote This is the same as the previous version of AddLineNote, but is
203/// used for GNU line markers.  If EntryExit is 0, then this doesn't change the
204/// presumed #include stack.  If it is 1, this is a file entry, if it is 2 then
205/// this is a file exit.  FileKind specifies whether this is a system header or
206/// extern C system header.
207void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset,
208                                unsigned LineNo, int FilenameID,
209                                unsigned EntryExit,
210                                SrcMgr::CharacteristicKind FileKind) {
211  assert(FilenameID != -1 && "Unspecified filename should use other accessor");
212
213  std::vector<LineEntry> &Entries = LineEntries[FID];
214
215  assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
216         "Adding line entries out of order!");
217
218  unsigned IncludeOffset = 0;
219  if (EntryExit == 0) {  // No #include stack change.
220    IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset;
221  } else if (EntryExit == 1) {
222    IncludeOffset = Offset-1;
223  } else if (EntryExit == 2) {
224    assert(!Entries.empty() && Entries.back().IncludeOffset &&
225       "PPDirectives should have caught case when popping empty include stack");
226
227    // Get the include loc of the last entries' include loc as our include loc.
228    IncludeOffset = 0;
229    if (const LineEntry *PrevEntry =
230          FindNearestLineEntry(FID, Entries.back().IncludeOffset))
231      IncludeOffset = PrevEntry->IncludeOffset;
232  }
233
234  Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind,
235                                   IncludeOffset));
236}
237
238
239/// FindNearestLineEntry - Find the line entry nearest to FID that is before
240/// it.  If there is no line entry before Offset in FID, return null.
241const LineEntry *LineTableInfo::FindNearestLineEntry(unsigned FID,
242                                                     unsigned Offset) {
243  const std::vector<LineEntry> &Entries = LineEntries[FID];
244  assert(!Entries.empty() && "No #line entries for this FID after all!");
245
246  // It is very common for the query to be after the last #line, check this
247  // first.
248  if (Entries.back().FileOffset <= Offset)
249    return &Entries.back();
250
251  // Do a binary search to find the maximal element that is still before Offset.
252  std::vector<LineEntry>::const_iterator I =
253    std::upper_bound(Entries.begin(), Entries.end(), Offset);
254  if (I == Entries.begin()) return 0;
255  return &*--I;
256}
257
258/// \brief Add a new line entry that has already been encoded into
259/// the internal representation of the line table.
260void LineTableInfo::AddEntry(unsigned FID,
261                             const std::vector<LineEntry> &Entries) {
262  LineEntries[FID] = Entries;
263}
264
265/// getLineTableFilenameID - Return the uniqued ID for the specified filename.
266///
267unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) {
268  if (LineTable == 0)
269    LineTable = new LineTableInfo();
270  return LineTable->getLineTableFilenameID(Ptr, Len);
271}
272
273
274/// AddLineNote - Add a line note to the line table for the FileID and offset
275/// specified by Loc.  If FilenameID is -1, it is considered to be
276/// unspecified.
277void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
278                                int FilenameID) {
279  std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
280
281  const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
282
283  // Remember that this file has #line directives now if it doesn't already.
284  const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
285
286  if (LineTable == 0)
287    LineTable = new LineTableInfo();
288  LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID);
289}
290
291/// AddLineNote - Add a GNU line marker to the line table.
292void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
293                                int FilenameID, bool IsFileEntry,
294                                bool IsFileExit, bool IsSystemHeader,
295                                bool IsExternCHeader) {
296  // If there is no filename and no flags, this is treated just like a #line,
297  // which does not change the flags of the previous line marker.
298  if (FilenameID == -1) {
299    assert(!IsFileEntry && !IsFileExit && !IsSystemHeader && !IsExternCHeader &&
300           "Can't set flags without setting the filename!");
301    return AddLineNote(Loc, LineNo, FilenameID);
302  }
303
304  std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
305  const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile();
306
307  // Remember that this file has #line directives now if it doesn't already.
308  const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
309
310  if (LineTable == 0)
311    LineTable = new LineTableInfo();
312
313  SrcMgr::CharacteristicKind FileKind;
314  if (IsExternCHeader)
315    FileKind = SrcMgr::C_ExternCSystem;
316  else if (IsSystemHeader)
317    FileKind = SrcMgr::C_System;
318  else
319    FileKind = SrcMgr::C_User;
320
321  unsigned EntryExit = 0;
322  if (IsFileEntry)
323    EntryExit = 1;
324  else if (IsFileExit)
325    EntryExit = 2;
326
327  LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID,
328                         EntryExit, FileKind);
329}
330
331LineTableInfo &SourceManager::getLineTable() {
332  if (LineTable == 0)
333    LineTable = new LineTableInfo();
334  return *LineTable;
335}
336
337//===----------------------------------------------------------------------===//
338// Private 'Create' methods.
339//===----------------------------------------------------------------------===//
340
341SourceManager::SourceManager(Diagnostic &Diag, FileManager &FileMgr)
342  : Diag(Diag), FileMgr(FileMgr), OverridenFilesKeepOriginalName(true),
343    ExternalSLocEntries(0), LineTable(0), NumLinearScans(0),
344    NumBinaryProbes(0) {
345  clearIDTables();
346  Diag.setSourceManager(this);
347}
348
349SourceManager::~SourceManager() {
350  delete LineTable;
351
352  // Delete FileEntry objects corresponding to content caches.  Since the actual
353  // content cache objects are bump pointer allocated, we just have to run the
354  // dtors, but we call the deallocate method for completeness.
355  for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) {
356    MemBufferInfos[i]->~ContentCache();
357    ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
358  }
359  for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
360       I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
361    I->second->~ContentCache();
362    ContentCacheAlloc.Deallocate(I->second);
363  }
364}
365
366void SourceManager::clearIDTables() {
367  MainFileID = FileID();
368  SLocEntryTable.clear();
369  LastLineNoFileIDQuery = FileID();
370  LastLineNoContentCache = 0;
371  LastFileIDLookup = FileID();
372
373  if (LineTable)
374    LineTable->clear();
375
376  // Use up FileID #0 as an invalid instantiation.
377  NextOffset = 0;
378  createInstantiationLoc(SourceLocation(),SourceLocation(),SourceLocation(), 1);
379}
380
381/// getOrCreateContentCache - Create or return a cached ContentCache for the
382/// specified file.
383const ContentCache *
384SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) {
385  assert(FileEnt && "Didn't specify a file entry to use?");
386
387  // Do we already have information about this file?
388  ContentCache *&Entry = FileInfos[FileEnt];
389  if (Entry) return Entry;
390
391  // Nope, create a new Cache entry.  Make sure it is at least 8-byte aligned
392  // so that FileInfo can use the low 3 bits of the pointer for its own
393  // nefarious purposes.
394  unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
395  EntryAlign = std::max(8U, EntryAlign);
396  Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
397
398  // If the file contents are overridden with contents from another file,
399  // pass that file to ContentCache.
400  llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
401      overI = OverriddenFiles.find(FileEnt);
402  if (overI == OverriddenFiles.end())
403    new (Entry) ContentCache(FileEnt);
404  else
405    new (Entry) ContentCache(OverridenFilesKeepOriginalName ? FileEnt
406                                                            : overI->second,
407                             overI->second);
408
409  return Entry;
410}
411
412
413/// createMemBufferContentCache - Create a new ContentCache for the specified
414///  memory buffer.  This does no caching.
415const ContentCache*
416SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
417  // Add a new ContentCache to the MemBufferInfos list and return it.  Make sure
418  // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of
419  // the pointer for its own nefarious purposes.
420  unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment;
421  EntryAlign = std::max(8U, EntryAlign);
422  ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign);
423  new (Entry) ContentCache();
424  MemBufferInfos.push_back(Entry);
425  Entry->setBuffer(Buffer);
426  return Entry;
427}
428
429void SourceManager::PreallocateSLocEntries(ExternalSLocEntrySource *Source,
430                                           unsigned NumSLocEntries,
431                                           unsigned NextOffset) {
432  ExternalSLocEntries = Source;
433  this->NextOffset = NextOffset;
434  unsigned CurPrealloc = SLocEntryLoaded.size();
435  // If we've ever preallocated, we must not count the dummy entry.
436  if (CurPrealloc) --CurPrealloc;
437  SLocEntryLoaded.resize(NumSLocEntries + 1);
438  SLocEntryLoaded[0] = true;
439  SLocEntryTable.resize(SLocEntryTable.size() + NumSLocEntries - CurPrealloc);
440}
441
442void SourceManager::ClearPreallocatedSLocEntries() {
443  unsigned I = 0;
444  for (unsigned N = SLocEntryLoaded.size(); I != N; ++I)
445    if (!SLocEntryLoaded[I])
446      break;
447
448  // We've already loaded all preallocated source location entries.
449  if (I == SLocEntryLoaded.size())
450    return;
451
452  // Remove everything from location I onward.
453  SLocEntryTable.resize(I);
454  SLocEntryLoaded.clear();
455  ExternalSLocEntries = 0;
456}
457
458
459//===----------------------------------------------------------------------===//
460// Methods to create new FileID's and instantiations.
461//===----------------------------------------------------------------------===//
462
463/// createFileID - Create a new FileID for the specified ContentCache and
464/// include position.  This works regardless of whether the ContentCache
465/// corresponds to a file or some other input source.
466FileID SourceManager::createFileID(const ContentCache *File,
467                                   SourceLocation IncludePos,
468                                   SrcMgr::CharacteristicKind FileCharacter,
469                                   unsigned PreallocatedID,
470                                   unsigned Offset) {
471  if (PreallocatedID) {
472    // If we're filling in a preallocated ID, just load in the file
473    // entry and return.
474    assert(PreallocatedID < SLocEntryLoaded.size() &&
475           "Preallocate ID out-of-range");
476    assert(!SLocEntryLoaded[PreallocatedID] &&
477           "Source location entry already loaded");
478    assert(Offset && "Preallocate source location cannot have zero offset");
479    SLocEntryTable[PreallocatedID]
480      = SLocEntry::get(Offset, FileInfo::get(IncludePos, File, FileCharacter));
481    SLocEntryLoaded[PreallocatedID] = true;
482    FileID FID = FileID::get(PreallocatedID);
483    return FID;
484  }
485
486  SLocEntryTable.push_back(SLocEntry::get(NextOffset,
487                                          FileInfo::get(IncludePos, File,
488                                                        FileCharacter)));
489  unsigned FileSize = File->getSize();
490  assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
491  NextOffset += FileSize+1;
492
493  // Set LastFileIDLookup to the newly created file.  The next getFileID call is
494  // almost guaranteed to be from that file.
495  FileID FID = FileID::get(SLocEntryTable.size()-1);
496  return LastFileIDLookup = FID;
497}
498
499/// createInstantiationLoc - Return a new SourceLocation that encodes the fact
500/// that a token from SpellingLoc should actually be referenced from
501/// InstantiationLoc.
502SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
503                                                     SourceLocation ILocStart,
504                                                     SourceLocation ILocEnd,
505                                                     unsigned TokLength,
506                                                     unsigned PreallocatedID,
507                                                     unsigned Offset) {
508  InstantiationInfo II = InstantiationInfo::get(ILocStart,ILocEnd, SpellingLoc);
509  if (PreallocatedID) {
510    // If we're filling in a preallocated ID, just load in the
511    // instantiation entry and return.
512    assert(PreallocatedID < SLocEntryLoaded.size() &&
513           "Preallocate ID out-of-range");
514    assert(!SLocEntryLoaded[PreallocatedID] &&
515           "Source location entry already loaded");
516    assert(Offset && "Preallocate source location cannot have zero offset");
517    SLocEntryTable[PreallocatedID] = SLocEntry::get(Offset, II);
518    SLocEntryLoaded[PreallocatedID] = true;
519    return SourceLocation::getMacroLoc(Offset);
520  }
521  SLocEntryTable.push_back(SLocEntry::get(NextOffset, II));
522  assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
523  NextOffset += TokLength+1;
524  return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
525}
526
527const llvm::MemoryBuffer *
528SourceManager::getMemoryBufferForFile(const FileEntry *File,
529                                      bool *Invalid) {
530  const SrcMgr::ContentCache *IR = getOrCreateContentCache(File);
531  assert(IR && "getOrCreateContentCache() cannot return NULL");
532  return IR->getBuffer(Diag, *this, SourceLocation(), Invalid);
533}
534
535void SourceManager::overrideFileContents(const FileEntry *SourceFile,
536                                         const llvm::MemoryBuffer *Buffer,
537                                         bool DoNotFree) {
538  const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
539  assert(IR && "getOrCreateContentCache() cannot return NULL");
540
541  const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(Buffer, DoNotFree);
542}
543
544void SourceManager::overrideFileContents(const FileEntry *SourceFile,
545                                         const FileEntry *NewFile) {
546  assert(SourceFile->getSize() == NewFile->getSize() &&
547         "Different sizes, use the FileManager to create a virtual file with "
548         "the correct size");
549  assert(FileInfos.count(SourceFile) == 0 &&
550         "This function should be called at the initialization stage, before "
551         "any parsing occurs.");
552  OverriddenFiles[SourceFile] = NewFile;
553}
554
555llvm::StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const {
556  bool MyInvalid = false;
557  const SLocEntry &SLoc = getSLocEntry(FID.ID);
558  if (!SLoc.isFile()) {
559    if (Invalid)
560      *Invalid = true;
561    return "<<<<<INVALID SOURCE LOCATION>>>>>";
562  }
563
564  const llvm::MemoryBuffer *Buf
565    = SLoc.getFile().getContentCache()->getBuffer(Diag, *this, SourceLocation(),
566                                                  &MyInvalid);
567  if (Invalid)
568    *Invalid = MyInvalid;
569
570  if (MyInvalid)
571    return "<<<<<INVALID SOURCE LOCATION>>>>>";
572
573  return Buf->getBuffer();
574}
575
576//===----------------------------------------------------------------------===//
577// SourceLocation manipulation methods.
578//===----------------------------------------------------------------------===//
579
580/// getFileIDSlow - Return the FileID for a SourceLocation.  This is a very hot
581/// method that is used for all SourceManager queries that start with a
582/// SourceLocation object.  It is responsible for finding the entry in
583/// SLocEntryTable which contains the specified location.
584///
585FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
586  assert(SLocOffset && "Invalid FileID");
587
588  // After the first and second level caches, I see two common sorts of
589  // behavior: 1) a lot of searched FileID's are "near" the cached file location
590  // or are "near" the cached instantiation location.  2) others are just
591  // completely random and may be a very long way away.
592  //
593  // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
594  // then we fall back to a less cache efficient, but more scalable, binary
595  // search to find the location.
596
597  // See if this is near the file point - worst case we start scanning from the
598  // most newly created FileID.
599  std::vector<SrcMgr::SLocEntry>::const_iterator I;
600
601  if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
602    // Neither loc prunes our search.
603    I = SLocEntryTable.end();
604  } else {
605    // Perhaps it is near the file point.
606    I = SLocEntryTable.begin()+LastFileIDLookup.ID;
607  }
608
609  // Find the FileID that contains this.  "I" is an iterator that points to a
610  // FileID whose offset is known to be larger than SLocOffset.
611  unsigned NumProbes = 0;
612  while (1) {
613    --I;
614    if (ExternalSLocEntries)
615      getSLocEntry(FileID::get(I - SLocEntryTable.begin()));
616    if (I->getOffset() <= SLocOffset) {
617#if 0
618      printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
619             I-SLocEntryTable.begin(),
620             I->isInstantiation() ? "inst" : "file",
621             LastFileIDLookup.ID,  int(SLocEntryTable.end()-I));
622#endif
623      FileID Res = FileID::get(I-SLocEntryTable.begin());
624
625      // If this isn't an instantiation, remember it.  We have good locality
626      // across FileID lookups.
627      if (!I->isInstantiation())
628        LastFileIDLookup = Res;
629      NumLinearScans += NumProbes+1;
630      return Res;
631    }
632    if (++NumProbes == 8)
633      break;
634  }
635
636  // Convert "I" back into an index.  We know that it is an entry whose index is
637  // larger than the offset we are looking for.
638  unsigned GreaterIndex = I-SLocEntryTable.begin();
639  // LessIndex - This is the lower bound of the range that we're searching.
640  // We know that the offset corresponding to the FileID is is less than
641  // SLocOffset.
642  unsigned LessIndex = 0;
643  NumProbes = 0;
644  while (1) {
645    unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
646    unsigned MidOffset = getSLocEntry(FileID::get(MiddleIndex)).getOffset();
647
648    ++NumProbes;
649
650    // If the offset of the midpoint is too large, chop the high side of the
651    // range to the midpoint.
652    if (MidOffset > SLocOffset) {
653      GreaterIndex = MiddleIndex;
654      continue;
655    }
656
657    // If the middle index contains the value, succeed and return.
658    if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
659#if 0
660      printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
661             I-SLocEntryTable.begin(),
662             I->isInstantiation() ? "inst" : "file",
663             LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
664#endif
665      FileID Res = FileID::get(MiddleIndex);
666
667      // If this isn't an instantiation, remember it.  We have good locality
668      // across FileID lookups.
669      if (!I->isInstantiation())
670        LastFileIDLookup = Res;
671      NumBinaryProbes += NumProbes;
672      return Res;
673    }
674
675    // Otherwise, move the low-side up to the middle index.
676    LessIndex = MiddleIndex;
677  }
678}
679
680SourceLocation SourceManager::
681getInstantiationLocSlowCase(SourceLocation Loc) const {
682  do {
683    // Note: If Loc indicates an offset into a token that came from a macro
684    // expansion (e.g. the 5th character of the token) we do not want to add
685    // this offset when going to the instantiation location.  The instatiation
686    // location is the macro invocation, which the offset has nothing to do
687    // with.  This is unlike when we get the spelling loc, because the offset
688    // directly correspond to the token whose spelling we're inspecting.
689    Loc = getSLocEntry(getFileID(Loc)).getInstantiation()
690                   .getInstantiationLocStart();
691  } while (!Loc.isFileID());
692
693  return Loc;
694}
695
696SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
697  do {
698    std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
699    Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
700    Loc = Loc.getFileLocWithOffset(LocInfo.second);
701  } while (!Loc.isFileID());
702  return Loc;
703}
704
705
706std::pair<FileID, unsigned>
707SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
708                                                     unsigned Offset) const {
709  // If this is an instantiation record, walk through all the instantiation
710  // points.
711  FileID FID;
712  SourceLocation Loc;
713  do {
714    Loc = E->getInstantiation().getInstantiationLocStart();
715
716    FID = getFileID(Loc);
717    E = &getSLocEntry(FID);
718    Offset += Loc.getOffset()-E->getOffset();
719  } while (!Loc.isFileID());
720
721  return std::make_pair(FID, Offset);
722}
723
724std::pair<FileID, unsigned>
725SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
726                                                unsigned Offset) const {
727  // If this is an instantiation record, walk through all the instantiation
728  // points.
729  FileID FID;
730  SourceLocation Loc;
731  do {
732    Loc = E->getInstantiation().getSpellingLoc();
733
734    FID = getFileID(Loc);
735    E = &getSLocEntry(FID);
736    Offset += Loc.getOffset()-E->getOffset();
737  } while (!Loc.isFileID());
738
739  return std::make_pair(FID, Offset);
740}
741
742/// getImmediateSpellingLoc - Given a SourceLocation object, return the
743/// spelling location referenced by the ID.  This is the first level down
744/// towards the place where the characters that make up the lexed token can be
745/// found.  This should not generally be used by clients.
746SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{
747  if (Loc.isFileID()) return Loc;
748  std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
749  Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc();
750  return Loc.getFileLocWithOffset(LocInfo.second);
751}
752
753
754/// getImmediateInstantiationRange - Loc is required to be an instantiation
755/// location.  Return the start/end of the instantiation information.
756std::pair<SourceLocation,SourceLocation>
757SourceManager::getImmediateInstantiationRange(SourceLocation Loc) const {
758  assert(Loc.isMacroID() && "Not an instantiation loc!");
759  const InstantiationInfo &II = getSLocEntry(getFileID(Loc)).getInstantiation();
760  return II.getInstantiationLocRange();
761}
762
763/// getInstantiationRange - Given a SourceLocation object, return the
764/// range of tokens covered by the instantiation in the ultimate file.
765std::pair<SourceLocation,SourceLocation>
766SourceManager::getInstantiationRange(SourceLocation Loc) const {
767  if (Loc.isFileID()) return std::make_pair(Loc, Loc);
768
769  std::pair<SourceLocation,SourceLocation> Res =
770    getImmediateInstantiationRange(Loc);
771
772  // Fully resolve the start and end locations to their ultimate instantiation
773  // points.
774  while (!Res.first.isFileID())
775    Res.first = getImmediateInstantiationRange(Res.first).first;
776  while (!Res.second.isFileID())
777    Res.second = getImmediateInstantiationRange(Res.second).second;
778  return Res;
779}
780
781
782
783//===----------------------------------------------------------------------===//
784// Queries about the code at a SourceLocation.
785//===----------------------------------------------------------------------===//
786
787/// getCharacterData - Return a pointer to the start of the specified location
788/// in the appropriate MemoryBuffer.
789const char *SourceManager::getCharacterData(SourceLocation SL,
790                                            bool *Invalid) const {
791  // Note that this is a hot function in the getSpelling() path, which is
792  // heavily used by -E mode.
793  std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
794
795  // Note that calling 'getBuffer()' may lazily page in a source file.
796  bool CharDataInvalid = false;
797  const llvm::MemoryBuffer *Buffer
798    = getSLocEntry(LocInfo.first).getFile().getContentCache()
799    ->getBuffer(Diag, *this, SourceLocation(), &CharDataInvalid);
800  if (Invalid)
801    *Invalid = CharDataInvalid;
802  return Buffer->getBufferStart() + (CharDataInvalid? 0 : LocInfo.second);
803}
804
805
806/// getColumnNumber - Return the column # for the specified file position.
807/// this is significantly cheaper to compute than the line number.
808unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos,
809                                        bool *Invalid) const {
810  bool MyInvalid = false;
811  const char *Buf = getBuffer(FID, &MyInvalid)->getBufferStart();
812  if (Invalid)
813    *Invalid = MyInvalid;
814
815  if (MyInvalid)
816    return 1;
817
818  unsigned LineStart = FilePos;
819  while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
820    --LineStart;
821  return FilePos-LineStart+1;
822}
823
824// isInvalid - Return the result of calling loc.isInvalid(), and
825// if Invalid is not null, set its value to same.
826static bool isInvalid(SourceLocation Loc, bool *Invalid) {
827  bool MyInvalid = Loc.isInvalid();
828  if (Invalid)
829    *Invalid = MyInvalid;
830  return MyInvalid;
831}
832
833unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc,
834                                                bool *Invalid) const {
835  if (isInvalid(Loc, Invalid)) return 0;
836  std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
837  return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
838}
839
840unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc,
841                                                     bool *Invalid) const {
842  if (isInvalid(Loc, Invalid)) return 0;
843  std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
844  return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
845}
846
847unsigned SourceManager::getPresumedColumnNumber(SourceLocation Loc,
848                                                bool *Invalid) const {
849  if (isInvalid(Loc, Invalid)) return 0;
850  return getPresumedLoc(Loc).getColumn();
851}
852
853static LLVM_ATTRIBUTE_NOINLINE void
854ComputeLineNumbers(Diagnostic &Diag, ContentCache *FI,
855                   llvm::BumpPtrAllocator &Alloc,
856                   const SourceManager &SM, bool &Invalid);
857static void ComputeLineNumbers(Diagnostic &Diag, ContentCache *FI,
858                               llvm::BumpPtrAllocator &Alloc,
859                               const SourceManager &SM, bool &Invalid) {
860  // Note that calling 'getBuffer()' may lazily page in the file.
861  const MemoryBuffer *Buffer = FI->getBuffer(Diag, SM, SourceLocation(),
862                                             &Invalid);
863  if (Invalid)
864    return;
865
866  // Find the file offsets of all of the *physical* source lines.  This does
867  // not look at trigraphs, escaped newlines, or anything else tricky.
868  std::vector<unsigned> LineOffsets;
869
870  // Line #1 starts at char 0.
871  LineOffsets.push_back(0);
872
873  const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
874  const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
875  unsigned Offs = 0;
876  while (1) {
877    // Skip over the contents of the line.
878    // TODO: Vectorize this?  This is very performance sensitive for programs
879    // with lots of diagnostics and in -E mode.
880    const unsigned char *NextBuf = (const unsigned char *)Buf;
881    while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
882      ++NextBuf;
883    Offs += NextBuf-Buf;
884    Buf = NextBuf;
885
886    if (Buf[0] == '\n' || Buf[0] == '\r') {
887      // If this is \n\r or \r\n, skip both characters.
888      if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
889        ++Offs, ++Buf;
890      ++Offs, ++Buf;
891      LineOffsets.push_back(Offs);
892    } else {
893      // Otherwise, this is a null.  If end of file, exit.
894      if (Buf == End) break;
895      // Otherwise, skip the null.
896      ++Offs, ++Buf;
897    }
898  }
899
900  // Copy the offsets into the FileInfo structure.
901  FI->NumLines = LineOffsets.size();
902  FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size());
903  std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
904}
905
906/// getLineNumber - Given a SourceLocation, return the spelling line number
907/// for the position indicated.  This requires building and caching a table of
908/// line offsets for the MemoryBuffer, so this is not cheap: use only when
909/// about to emit a diagnostic.
910unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos,
911                                      bool *Invalid) const {
912  ContentCache *Content;
913  if (LastLineNoFileIDQuery == FID)
914    Content = LastLineNoContentCache;
915  else
916    Content = const_cast<ContentCache*>(getSLocEntry(FID)
917                                        .getFile().getContentCache());
918
919  // If this is the first use of line information for this buffer, compute the
920  /// SourceLineCache for it on demand.
921  if (Content->SourceLineCache == 0) {
922    bool MyInvalid = false;
923    ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid);
924    if (Invalid)
925      *Invalid = MyInvalid;
926    if (MyInvalid)
927      return 1;
928  } else if (Invalid)
929    *Invalid = false;
930
931  // Okay, we know we have a line number table.  Do a binary search to find the
932  // line number that this character position lands on.
933  unsigned *SourceLineCache = Content->SourceLineCache;
934  unsigned *SourceLineCacheStart = SourceLineCache;
935  unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
936
937  unsigned QueriedFilePos = FilePos+1;
938
939  // FIXME: I would like to be convinced that this code is worth being as
940  // complicated as it is, binary search isn't that slow.
941  //
942  // If it is worth being optimized, then in my opinion it could be more
943  // performant, simpler, and more obviously correct by just "galloping" outward
944  // from the queried file position. In fact, this could be incorporated into a
945  // generic algorithm such as lower_bound_with_hint.
946  //
947  // If someone gives me a test case where this matters, and I will do it! - DWD
948
949  // If the previous query was to the same file, we know both the file pos from
950  // that query and the line number returned.  This allows us to narrow the
951  // search space from the entire file to something near the match.
952  if (LastLineNoFileIDQuery == FID) {
953    if (QueriedFilePos >= LastLineNoFilePos) {
954      // FIXME: Potential overflow?
955      SourceLineCache = SourceLineCache+LastLineNoResult-1;
956
957      // The query is likely to be nearby the previous one.  Here we check to
958      // see if it is within 5, 10 or 20 lines.  It can be far away in cases
959      // where big comment blocks and vertical whitespace eat up lines but
960      // contribute no tokens.
961      if (SourceLineCache+5 < SourceLineCacheEnd) {
962        if (SourceLineCache[5] > QueriedFilePos)
963          SourceLineCacheEnd = SourceLineCache+5;
964        else if (SourceLineCache+10 < SourceLineCacheEnd) {
965          if (SourceLineCache[10] > QueriedFilePos)
966            SourceLineCacheEnd = SourceLineCache+10;
967          else if (SourceLineCache+20 < SourceLineCacheEnd) {
968            if (SourceLineCache[20] > QueriedFilePos)
969              SourceLineCacheEnd = SourceLineCache+20;
970          }
971        }
972      }
973    } else {
974      if (LastLineNoResult < Content->NumLines)
975        SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
976    }
977  }
978
979  // If the spread is large, do a "radix" test as our initial guess, based on
980  // the assumption that lines average to approximately the same length.
981  // NOTE: This is currently disabled, as it does not appear to be profitable in
982  // initial measurements.
983  if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
984    unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
985
986    // Take a stab at guessing where it is.
987    unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
988
989    // Check for -10 and +10 lines.
990    unsigned LowerBound = std::max(int(ApproxPos-10), 0);
991    unsigned UpperBound = std::min(ApproxPos+10, FileLen);
992
993    // If the computed lower bound is less than the query location, move it in.
994    if (SourceLineCache < SourceLineCacheStart+LowerBound &&
995        SourceLineCacheStart[LowerBound] < QueriedFilePos)
996      SourceLineCache = SourceLineCacheStart+LowerBound;
997
998    // If the computed upper bound is greater than the query location, move it.
999    if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
1000        SourceLineCacheStart[UpperBound] >= QueriedFilePos)
1001      SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
1002  }
1003
1004  unsigned *Pos
1005    = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
1006  unsigned LineNo = Pos-SourceLineCacheStart;
1007
1008  LastLineNoFileIDQuery = FID;
1009  LastLineNoContentCache = Content;
1010  LastLineNoFilePos = QueriedFilePos;
1011  LastLineNoResult = LineNo;
1012  return LineNo;
1013}
1014
1015unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc,
1016                                              bool *Invalid) const {
1017  if (isInvalid(Loc, Invalid)) return 0;
1018  std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
1019  return getLineNumber(LocInfo.first, LocInfo.second);
1020}
1021unsigned SourceManager::getInstantiationLineNumber(SourceLocation Loc,
1022                                                   bool *Invalid) const {
1023  if (isInvalid(Loc, Invalid)) return 0;
1024  std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
1025  return getLineNumber(LocInfo.first, LocInfo.second);
1026}
1027unsigned SourceManager::getPresumedLineNumber(SourceLocation Loc,
1028                                              bool *Invalid) const {
1029  if (isInvalid(Loc, Invalid)) return 0;
1030  return getPresumedLoc(Loc).getLine();
1031}
1032
1033/// getFileCharacteristic - return the file characteristic of the specified
1034/// source location, indicating whether this is a normal file, a system
1035/// header, or an "implicit extern C" system header.
1036///
1037/// This state can be modified with flags on GNU linemarker directives like:
1038///   # 4 "foo.h" 3
1039/// which changes all source locations in the current file after that to be
1040/// considered to be from a system header.
1041SrcMgr::CharacteristicKind
1042SourceManager::getFileCharacteristic(SourceLocation Loc) const {
1043  assert(!Loc.isInvalid() && "Can't get file characteristic of invalid loc!");
1044  std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
1045  const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
1046
1047  // If there are no #line directives in this file, just return the whole-file
1048  // state.
1049  if (!FI.hasLineDirectives())
1050    return FI.getFileCharacteristic();
1051
1052  assert(LineTable && "Can't have linetable entries without a LineTable!");
1053  // See if there is a #line directive before the location.
1054  const LineEntry *Entry =
1055    LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second);
1056
1057  // If this is before the first line marker, use the file characteristic.
1058  if (!Entry)
1059    return FI.getFileCharacteristic();
1060
1061  return Entry->FileKind;
1062}
1063
1064/// Return the filename or buffer identifier of the buffer the location is in.
1065/// Note that this name does not respect #line directives.  Use getPresumedLoc
1066/// for normal clients.
1067const char *SourceManager::getBufferName(SourceLocation Loc,
1068                                         bool *Invalid) const {
1069  if (isInvalid(Loc, Invalid)) return "<invalid loc>";
1070
1071  return getBuffer(getFileID(Loc), Invalid)->getBufferIdentifier();
1072}
1073
1074
1075/// getPresumedLoc - This method returns the "presumed" location of a
1076/// SourceLocation specifies.  A "presumed location" can be modified by #line
1077/// or GNU line marker directives.  This provides a view on the data that a
1078/// user should see in diagnostics, for example.
1079///
1080/// Note that a presumed location is always given as the instantiation point
1081/// of an instantiation location, not at the spelling location.
1082PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const {
1083  if (Loc.isInvalid()) return PresumedLoc();
1084
1085  // Presumed locations are always for instantiation points.
1086  std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc);
1087
1088  const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile();
1089  const SrcMgr::ContentCache *C = FI.getContentCache();
1090
1091  // To get the source name, first consult the FileEntry (if one exists)
1092  // before the MemBuffer as this will avoid unnecessarily paging in the
1093  // MemBuffer.
1094  const char *Filename;
1095  if (C->OrigEntry)
1096    Filename = C->OrigEntry->getName();
1097  else
1098    Filename = C->getBuffer(Diag, *this)->getBufferIdentifier();
1099  bool Invalid = false;
1100  unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second, &Invalid);
1101  if (Invalid)
1102    return PresumedLoc();
1103  unsigned ColNo  = getColumnNumber(LocInfo.first, LocInfo.second, &Invalid);
1104  if (Invalid)
1105    return PresumedLoc();
1106
1107  SourceLocation IncludeLoc = FI.getIncludeLoc();
1108
1109  // If we have #line directives in this file, update and overwrite the physical
1110  // location info if appropriate.
1111  if (FI.hasLineDirectives()) {
1112    assert(LineTable && "Can't have linetable entries without a LineTable!");
1113    // See if there is a #line directive before this.  If so, get it.
1114    if (const LineEntry *Entry =
1115          LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second)) {
1116      // If the LineEntry indicates a filename, use it.
1117      if (Entry->FilenameID != -1)
1118        Filename = LineTable->getFilename(Entry->FilenameID);
1119
1120      // Use the line number specified by the LineEntry.  This line number may
1121      // be multiple lines down from the line entry.  Add the difference in
1122      // physical line numbers from the query point and the line marker to the
1123      // total.
1124      unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset);
1125      LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1);
1126
1127      // Note that column numbers are not molested by line markers.
1128
1129      // Handle virtual #include manipulation.
1130      if (Entry->IncludeOffset) {
1131        IncludeLoc = getLocForStartOfFile(LocInfo.first);
1132        IncludeLoc = IncludeLoc.getFileLocWithOffset(Entry->IncludeOffset);
1133      }
1134    }
1135  }
1136
1137  return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc);
1138}
1139
1140//===----------------------------------------------------------------------===//
1141// Other miscellaneous methods.
1142//===----------------------------------------------------------------------===//
1143
1144/// \brief Retrieve the inode for the given file entry, if possible.
1145///
1146/// This routine involves a system call, and therefore should only be used
1147/// in non-performance-critical code.
1148static llvm::Optional<ino_t> getActualFileInode(const FileEntry *File) {
1149  if (!File)
1150    return llvm::Optional<ino_t>();
1151
1152  struct stat StatBuf;
1153  if (::stat(File->getName(), &StatBuf))
1154    return llvm::Optional<ino_t>();
1155
1156  return StatBuf.st_ino;
1157}
1158
1159/// \brief Get the source location for the given file:line:col triplet.
1160///
1161/// If the source file is included multiple times, the source location will
1162/// be based upon the first inclusion.
1163SourceLocation SourceManager::getLocation(const FileEntry *SourceFile,
1164                                          unsigned Line, unsigned Col) {
1165  assert(SourceFile && "Null source file!");
1166  assert(Line && Col && "Line and column should start from 1!");
1167
1168  // Find the first file ID that corresponds to the given file.
1169  FileID FirstFID;
1170
1171  // First, check the main file ID, since it is common to look for a
1172  // location in the main file.
1173  llvm::Optional<ino_t> SourceFileInode;
1174  llvm::Optional<llvm::StringRef> SourceFileName;
1175  if (!MainFileID.isInvalid()) {
1176    const SLocEntry &MainSLoc = getSLocEntry(MainFileID);
1177    if (MainSLoc.isFile()) {
1178      const ContentCache *MainContentCache
1179        = MainSLoc.getFile().getContentCache();
1180      if (!MainContentCache) {
1181        // Can't do anything
1182      } else if (MainContentCache->OrigEntry == SourceFile) {
1183        FirstFID = MainFileID;
1184      } else {
1185        // Fall back: check whether we have the same base name and inode
1186        // as the main file.
1187        const FileEntry *MainFile = MainContentCache->OrigEntry;
1188        SourceFileName = llvm::sys::path::filename(SourceFile->getName());
1189        if (*SourceFileName == llvm::sys::path::filename(MainFile->getName())) {
1190          SourceFileInode = getActualFileInode(SourceFile);
1191          if (SourceFileInode) {
1192            if (llvm::Optional<ino_t> MainFileInode
1193                                               = getActualFileInode(MainFile)) {
1194              if (*SourceFileInode == *MainFileInode) {
1195                FirstFID = MainFileID;
1196                SourceFile = MainFile;
1197              }
1198            }
1199          }
1200        }
1201      }
1202    }
1203  }
1204
1205  if (FirstFID.isInvalid()) {
1206    // The location we're looking for isn't in the main file; look
1207    // through all of the source locations.
1208    for (unsigned I = 0, N = sloc_entry_size(); I != N; ++I) {
1209      const SLocEntry &SLoc = getSLocEntry(I);
1210      if (SLoc.isFile() &&
1211          SLoc.getFile().getContentCache() &&
1212          SLoc.getFile().getContentCache()->OrigEntry == SourceFile) {
1213        FirstFID = FileID::get(I);
1214        break;
1215      }
1216    }
1217  }
1218
1219  // If we haven't found what we want yet, try again, but this time stat()
1220  // each of the files in case the files have changed since we originally
1221  // parsed the file.
1222  if (FirstFID.isInvalid() &&
1223      (SourceFileName ||
1224       (SourceFileName = llvm::sys::path::filename(SourceFile->getName()))) &&
1225      (SourceFileInode ||
1226       (SourceFileInode = getActualFileInode(SourceFile)))) {
1227    for (unsigned I = 0, N = sloc_entry_size(); I != N; ++I) {
1228      const SLocEntry &SLoc = getSLocEntry(I);
1229      if (SLoc.isFile()) {
1230        const ContentCache *FileContentCache
1231          = SLoc.getFile().getContentCache();
1232      const FileEntry *Entry =FileContentCache? FileContentCache->OrigEntry : 0;
1233        if (Entry &&
1234            *SourceFileName == llvm::sys::path::filename(Entry->getName())) {
1235          if (llvm::Optional<ino_t> EntryInode = getActualFileInode(Entry)) {
1236            if (*SourceFileInode == *EntryInode) {
1237              FirstFID = FileID::get(I);
1238              SourceFile = Entry;
1239              break;
1240            }
1241          }
1242        }
1243      }
1244    }
1245  }
1246
1247  if (FirstFID.isInvalid())
1248    return SourceLocation();
1249
1250  if (Line == 1 && Col == 1)
1251    return getLocForStartOfFile(FirstFID);
1252
1253  ContentCache *Content
1254    = const_cast<ContentCache *>(getOrCreateContentCache(SourceFile));
1255  if (!Content)
1256    return SourceLocation();
1257
1258  // If this is the first use of line information for this buffer, compute the
1259  /// SourceLineCache for it on demand.
1260  if (Content->SourceLineCache == 0) {
1261    bool MyInvalid = false;
1262    ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid);
1263    if (MyInvalid)
1264      return SourceLocation();
1265  }
1266
1267  if (Line > Content->NumLines) {
1268    unsigned Size = Content->getBuffer(Diag, *this)->getBufferSize();
1269    if (Size > 0)
1270      --Size;
1271    return getLocForStartOfFile(FirstFID).getFileLocWithOffset(Size);
1272  }
1273
1274  unsigned FilePos = Content->SourceLineCache[Line - 1];
1275  const char *Buf = Content->getBuffer(Diag, *this)->getBufferStart() + FilePos;
1276  unsigned BufLength = Content->getBuffer(Diag, *this)->getBufferEnd() - Buf;
1277  unsigned i = 0;
1278
1279  // Check that the given column is valid.
1280  while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r')
1281    ++i;
1282  if (i < Col-1)
1283    return getLocForStartOfFile(FirstFID).getFileLocWithOffset(FilePos + i);
1284
1285  return getLocForStartOfFile(FirstFID).getFileLocWithOffset(FilePos + Col - 1);
1286}
1287
1288/// Given a decomposed source location, move it up the include/instantiation
1289/// stack to the parent source location.  If this is possible, return the
1290/// decomposed version of the parent in Loc and return false.  If Loc is the
1291/// top-level entry, return true and don't modify it.
1292static bool MoveUpIncludeHierarchy(std::pair<FileID, unsigned> &Loc,
1293                                   const SourceManager &SM) {
1294  SourceLocation UpperLoc;
1295  const SrcMgr::SLocEntry &Entry = SM.getSLocEntry(Loc.first);
1296  if (Entry.isInstantiation())
1297    UpperLoc = Entry.getInstantiation().getInstantiationLocStart();
1298  else
1299    UpperLoc = Entry.getFile().getIncludeLoc();
1300
1301  if (UpperLoc.isInvalid())
1302    return true; // We reached the top.
1303
1304  Loc = SM.getDecomposedLoc(UpperLoc);
1305  return false;
1306}
1307
1308
1309/// \brief Determines the order of 2 source locations in the translation unit.
1310///
1311/// \returns true if LHS source location comes before RHS, false otherwise.
1312bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS,
1313                                              SourceLocation RHS) const {
1314  assert(LHS.isValid() && RHS.isValid() && "Passed invalid source location!");
1315  if (LHS == RHS)
1316    return false;
1317
1318  // If both locations are macro instantiations, the order of their offsets
1319  // reflect the order that the tokens, pointed to by these locations, were
1320  // instantiated (during parsing each token that is instantiated by a macro,
1321  // expands the SLocEntries).
1322  if (LHS.isMacroID() && RHS.isMacroID())
1323    return LHS.getOffset() < RHS.getOffset();
1324
1325  std::pair<FileID, unsigned> LOffs = getDecomposedLoc(LHS);
1326  std::pair<FileID, unsigned> ROffs = getDecomposedLoc(RHS);
1327
1328  // If the source locations are in the same file, just compare offsets.
1329  if (LOffs.first == ROffs.first)
1330    return LOffs.second < ROffs.second;
1331
1332  // If we are comparing a source location with multiple locations in the same
1333  // file, we get a big win by caching the result.
1334  if (IsBeforeInTUCache.isCacheValid(LOffs.first, ROffs.first))
1335    return IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second);
1336
1337  // Okay, we missed in the cache, start updating the cache for this query.
1338  IsBeforeInTUCache.setQueryFIDs(LOffs.first, ROffs.first);
1339
1340  // "Traverse" the include/instantiation stacks of both locations and try to
1341  // find a common "ancestor".  FileIDs build a tree-like structure that
1342  // reflects the #include hierarchy, and this algorithm needs to find the
1343  // nearest common ancestor between the two locations.  For example, if you
1344  // have a.c that includes b.h and c.h, and are comparing a location in b.h to
1345  // a location in c.h, we need to find that their nearest common ancestor is
1346  // a.c, and compare the locations of the two #includes to find their relative
1347  // ordering.
1348  //
1349  // SourceManager assigns FileIDs in order of parsing.  This means that an
1350  // includee always has a larger FileID than an includer.  While you might
1351  // think that we could just compare the FileID's here, that doesn't work to
1352  // compare a point at the end of a.c with a point within c.h.  Though c.h has
1353  // a larger FileID, we have to compare the include point of c.h to the
1354  // location in a.c.
1355  //
1356  // Despite not being able to directly compare FileID's, we can tell that a
1357  // larger FileID is necessarily more deeply nested than a lower one and use
1358  // this information to walk up the tree to the nearest common ancestor.
1359  do {
1360    // If LOffs is larger than ROffs, then LOffs must be more deeply nested than
1361    // ROffs, walk up the #include chain.
1362    if (LOffs.first.ID > ROffs.first.ID) {
1363      if (MoveUpIncludeHierarchy(LOffs, *this))
1364        break; // We reached the top.
1365
1366    } else {
1367      // Otherwise, ROffs is larger than LOffs, so ROffs must be more deeply
1368      // nested than LOffs, walk up the #include chain.
1369      if (MoveUpIncludeHierarchy(ROffs, *this))
1370        break; // We reached the top.
1371    }
1372  } while (LOffs.first != ROffs.first);
1373
1374  // If we exited because we found a nearest common ancestor, compare the
1375  // locations within the common file and cache them.
1376  if (LOffs.first == ROffs.first) {
1377    IsBeforeInTUCache.setCommonLoc(LOffs.first, LOffs.second, ROffs.second);
1378    return IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second);
1379  }
1380
1381  // There is no common ancestor, most probably because one location is in the
1382  // predefines buffer or an AST file.
1383  // FIXME: We should rearrange the external interface so this simply never
1384  // happens; it can't conceptually happen. Also see PR5662.
1385  IsBeforeInTUCache.setQueryFIDs(FileID(), FileID()); // Don't try caching.
1386
1387  // Zip both entries up to the top level record.
1388  while (!MoveUpIncludeHierarchy(LOffs, *this)) /*empty*/;
1389  while (!MoveUpIncludeHierarchy(ROffs, *this)) /*empty*/;
1390
1391  // If exactly one location is a memory buffer, assume it precedes the other.
1392
1393  // Strip off macro instantation locations, going up to the top-level File
1394  // SLocEntry.
1395  bool LIsMB = getFileEntryForID(LOffs.first) == 0;
1396  bool RIsMB = getFileEntryForID(ROffs.first) == 0;
1397  if (LIsMB != RIsMB)
1398    return LIsMB;
1399
1400  // Otherwise, just assume FileIDs were created in order.
1401  return LOffs.first < ROffs.first;
1402}
1403
1404/// PrintStats - Print statistics to stderr.
1405///
1406void SourceManager::PrintStats() const {
1407  llvm::errs() << "\n*** Source Manager Stats:\n";
1408  llvm::errs() << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
1409               << " mem buffers mapped.\n";
1410  llvm::errs() << SLocEntryTable.size() << " SLocEntry's allocated, "
1411               << NextOffset << "B of Sloc address space used.\n";
1412
1413  unsigned NumLineNumsComputed = 0;
1414  unsigned NumFileBytesMapped = 0;
1415  for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){
1416    NumLineNumsComputed += I->second->SourceLineCache != 0;
1417    NumFileBytesMapped  += I->second->getSizeBytesMapped();
1418  }
1419
1420  llvm::errs() << NumFileBytesMapped << " bytes of files mapped, "
1421               << NumLineNumsComputed << " files with line #'s computed.\n";
1422  llvm::errs() << "FileID scans: " << NumLinearScans << " linear, "
1423               << NumBinaryProbes << " binary.\n";
1424}
1425
1426ExternalSLocEntrySource::~ExternalSLocEntrySource() { }
1427