clang API Documentation

SourceManager.cpp
Go to the documentation of this file.
00001 //===--- SourceManager.cpp - Track and cache source files -----------------===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file implements the SourceManager interface.
00011 //
00012 //===----------------------------------------------------------------------===//
00013 
00014 #include "clang/Basic/SourceManager.h"
00015 #include "clang/Basic/Diagnostic.h"
00016 #include "clang/Basic/FileManager.h"
00017 #include "clang/Basic/SourceManagerInternals.h"
00018 #include "llvm/ADT/Optional.h"
00019 #include "llvm/ADT/STLExtras.h"
00020 #include "llvm/ADT/StringSwitch.h"
00021 #include "llvm/Support/Capacity.h"
00022 #include "llvm/Support/Compiler.h"
00023 #include "llvm/Support/MemoryBuffer.h"
00024 #include "llvm/Support/Path.h"
00025 #include "llvm/Support/raw_ostream.h"
00026 #include <algorithm>
00027 #include <cstring>
00028 #include <string>
00029 
00030 using namespace clang;
00031 using namespace SrcMgr;
00032 using llvm::MemoryBuffer;
00033 
00034 //===----------------------------------------------------------------------===//
00035 // SourceManager Helper Classes
00036 //===----------------------------------------------------------------------===//
00037 
00038 ContentCache::~ContentCache() {
00039   if (shouldFreeBuffer())
00040     delete Buffer.getPointer();
00041 }
00042 
00043 /// getSizeBytesMapped - Returns the number of bytes actually mapped for this
00044 /// ContentCache. This can be 0 if the MemBuffer was not actually expanded.
00045 unsigned ContentCache::getSizeBytesMapped() const {
00046   return Buffer.getPointer() ? Buffer.getPointer()->getBufferSize() : 0;
00047 }
00048 
00049 /// Returns the kind of memory used to back the memory buffer for
00050 /// this content cache.  This is used for performance analysis.
00051 llvm::MemoryBuffer::BufferKind ContentCache::getMemoryBufferKind() const {
00052   assert(Buffer.getPointer());
00053 
00054   // Should be unreachable, but keep for sanity.
00055   if (!Buffer.getPointer())
00056     return llvm::MemoryBuffer::MemoryBuffer_Malloc;
00057 
00058   llvm::MemoryBuffer *buf = Buffer.getPointer();
00059   return buf->getBufferKind();
00060 }
00061 
00062 /// getSize - Returns the size of the content encapsulated by this ContentCache.
00063 ///  This can be the size of the source file or the size of an arbitrary
00064 ///  scratch buffer.  If the ContentCache encapsulates a source file, that
00065 ///  file is not lazily brought in from disk to satisfy this query.
00066 unsigned ContentCache::getSize() const {
00067   return Buffer.getPointer() ? (unsigned) Buffer.getPointer()->getBufferSize()
00068                              : (unsigned) ContentsEntry->getSize();
00069 }
00070 
00071 void ContentCache::replaceBuffer(llvm::MemoryBuffer *B, bool DoNotFree) {
00072   if (B && B == Buffer.getPointer()) {
00073     assert(0 && "Replacing with the same buffer");
00074     Buffer.setInt(DoNotFree? DoNotFreeFlag : 0);
00075     return;
00076   }
00077   
00078   if (shouldFreeBuffer())
00079     delete Buffer.getPointer();
00080   Buffer.setPointer(B);
00081   Buffer.setInt(DoNotFree? DoNotFreeFlag : 0);
00082 }
00083 
00084 llvm::MemoryBuffer *ContentCache::getBuffer(DiagnosticsEngine &Diag,
00085                                             const SourceManager &SM,
00086                                             SourceLocation Loc,
00087                                             bool *Invalid) const {
00088   // Lazily create the Buffer for ContentCaches that wrap files.  If we already
00089   // computed it, just return what we have.
00090   if (Buffer.getPointer() || !ContentsEntry) {
00091     if (Invalid)
00092       *Invalid = isBufferInvalid();
00093     
00094     return Buffer.getPointer();
00095   }    
00096 
00097   bool isVolatile = SM.userFilesAreVolatile() && !IsSystemFile;
00098   auto BufferOrError =
00099       SM.getFileManager().getBufferForFile(ContentsEntry, isVolatile);
00100 
00101   // If we were unable to open the file, then we are in an inconsistent
00102   // situation where the content cache referenced a file which no longer
00103   // exists. Most likely, we were using a stat cache with an invalid entry but
00104   // the file could also have been removed during processing. Since we can't
00105   // really deal with this situation, just create an empty buffer.
00106   //
00107   // FIXME: This is definitely not ideal, but our immediate clients can't
00108   // currently handle returning a null entry here. Ideally we should detect
00109   // that we are in an inconsistent situation and error out as quickly as
00110   // possible.
00111   if (!BufferOrError) {
00112     StringRef FillStr("<<<MISSING SOURCE FILE>>>\n");
00113     Buffer.setPointer(MemoryBuffer::getNewMemBuffer(ContentsEntry->getSize(),
00114                                                     "<invalid>").release());
00115     char *Ptr = const_cast<char*>(Buffer.getPointer()->getBufferStart());
00116     for (unsigned i = 0, e = ContentsEntry->getSize(); i != e; ++i)
00117       Ptr[i] = FillStr[i % FillStr.size()];
00118 
00119     if (Diag.isDiagnosticInFlight())
00120       Diag.SetDelayedDiagnostic(diag::err_cannot_open_file,
00121                                 ContentsEntry->getName(),
00122                                 BufferOrError.getError().message());
00123     else
00124       Diag.Report(Loc, diag::err_cannot_open_file)
00125           << ContentsEntry->getName() << BufferOrError.getError().message();
00126 
00127     Buffer.setInt(Buffer.getInt() | InvalidFlag);
00128     
00129     if (Invalid) *Invalid = true;
00130     return Buffer.getPointer();
00131   }
00132 
00133   Buffer.setPointer(BufferOrError->release());
00134 
00135   // Check that the file's size is the same as in the file entry (which may
00136   // have come from a stat cache).
00137   if (getRawBuffer()->getBufferSize() != (size_t)ContentsEntry->getSize()) {
00138     if (Diag.isDiagnosticInFlight())
00139       Diag.SetDelayedDiagnostic(diag::err_file_modified,
00140                                 ContentsEntry->getName());
00141     else
00142       Diag.Report(Loc, diag::err_file_modified)
00143         << ContentsEntry->getName();
00144 
00145     Buffer.setInt(Buffer.getInt() | InvalidFlag);
00146     if (Invalid) *Invalid = true;
00147     return Buffer.getPointer();
00148   }
00149 
00150   // If the buffer is valid, check to see if it has a UTF Byte Order Mark
00151   // (BOM).  We only support UTF-8 with and without a BOM right now.  See
00152   // http://en.wikipedia.org/wiki/Byte_order_mark for more information.
00153   StringRef BufStr = Buffer.getPointer()->getBuffer();
00154   const char *InvalidBOM = llvm::StringSwitch<const char *>(BufStr)
00155     .StartsWith("\xFE\xFF", "UTF-16 (BE)")
00156     .StartsWith("\xFF\xFE", "UTF-16 (LE)")
00157     .StartsWith("\x00\x00\xFE\xFF", "UTF-32 (BE)")
00158     .StartsWith("\xFF\xFE\x00\x00", "UTF-32 (LE)")
00159     .StartsWith("\x2B\x2F\x76", "UTF-7")
00160     .StartsWith("\xF7\x64\x4C", "UTF-1")
00161     .StartsWith("\xDD\x73\x66\x73", "UTF-EBCDIC")
00162     .StartsWith("\x0E\xFE\xFF", "SDSU")
00163     .StartsWith("\xFB\xEE\x28", "BOCU-1")
00164     .StartsWith("\x84\x31\x95\x33", "GB-18030")
00165     .Default(nullptr);
00166 
00167   if (InvalidBOM) {
00168     Diag.Report(Loc, diag::err_unsupported_bom)
00169       << InvalidBOM << ContentsEntry->getName();
00170     Buffer.setInt(Buffer.getInt() | InvalidFlag);
00171   }
00172   
00173   if (Invalid)
00174     *Invalid = isBufferInvalid();
00175   
00176   return Buffer.getPointer();
00177 }
00178 
00179 unsigned LineTableInfo::getLineTableFilenameID(StringRef Name) {
00180   // Look up the filename in the string table, returning the pre-existing value
00181   // if it exists.
00182   llvm::StringMapEntry<unsigned> &Entry =
00183     FilenameIDs.GetOrCreateValue(Name, ~0U);
00184   if (Entry.getValue() != ~0U)
00185     return Entry.getValue();
00186 
00187   // Otherwise, assign this the next available ID.
00188   Entry.setValue(FilenamesByID.size());
00189   FilenamesByID.push_back(&Entry);
00190   return FilenamesByID.size()-1;
00191 }
00192 
00193 /// AddLineNote - Add a line note to the line table that indicates that there
00194 /// is a \#line at the specified FID/Offset location which changes the presumed
00195 /// location to LineNo/FilenameID.
00196 void LineTableInfo::AddLineNote(FileID FID, unsigned Offset,
00197                                 unsigned LineNo, int FilenameID) {
00198   std::vector<LineEntry> &Entries = LineEntries[FID];
00199 
00200   assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
00201          "Adding line entries out of order!");
00202 
00203   SrcMgr::CharacteristicKind Kind = SrcMgr::C_User;
00204   unsigned IncludeOffset = 0;
00205 
00206   if (!Entries.empty()) {
00207     // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember
00208     // that we are still in "foo.h".
00209     if (FilenameID == -1)
00210       FilenameID = Entries.back().FilenameID;
00211 
00212     // If we are after a line marker that switched us to system header mode, or
00213     // that set #include information, preserve it.
00214     Kind = Entries.back().FileKind;
00215     IncludeOffset = Entries.back().IncludeOffset;
00216   }
00217 
00218   Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, Kind,
00219                                    IncludeOffset));
00220 }
00221 
00222 /// AddLineNote This is the same as the previous version of AddLineNote, but is
00223 /// used for GNU line markers.  If EntryExit is 0, then this doesn't change the
00224 /// presumed \#include stack.  If it is 1, this is a file entry, if it is 2 then
00225 /// this is a file exit.  FileKind specifies whether this is a system header or
00226 /// extern C system header.
00227 void LineTableInfo::AddLineNote(FileID FID, unsigned Offset,
00228                                 unsigned LineNo, int FilenameID,
00229                                 unsigned EntryExit,
00230                                 SrcMgr::CharacteristicKind FileKind) {
00231   assert(FilenameID != -1 && "Unspecified filename should use other accessor");
00232 
00233   std::vector<LineEntry> &Entries = LineEntries[FID];
00234 
00235   assert((Entries.empty() || Entries.back().FileOffset < Offset) &&
00236          "Adding line entries out of order!");
00237 
00238   unsigned IncludeOffset = 0;
00239   if (EntryExit == 0) {  // No #include stack change.
00240     IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset;
00241   } else if (EntryExit == 1) {
00242     IncludeOffset = Offset-1;
00243   } else if (EntryExit == 2) {
00244     assert(!Entries.empty() && Entries.back().IncludeOffset &&
00245        "PPDirectives should have caught case when popping empty include stack");
00246 
00247     // Get the include loc of the last entries' include loc as our include loc.
00248     IncludeOffset = 0;
00249     if (const LineEntry *PrevEntry =
00250           FindNearestLineEntry(FID, Entries.back().IncludeOffset))
00251       IncludeOffset = PrevEntry->IncludeOffset;
00252   }
00253 
00254   Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind,
00255                                    IncludeOffset));
00256 }
00257 
00258 
00259 /// FindNearestLineEntry - Find the line entry nearest to FID that is before
00260 /// it.  If there is no line entry before Offset in FID, return null.
00261 const LineEntry *LineTableInfo::FindNearestLineEntry(FileID FID,
00262                                                      unsigned Offset) {
00263   const std::vector<LineEntry> &Entries = LineEntries[FID];
00264   assert(!Entries.empty() && "No #line entries for this FID after all!");
00265 
00266   // It is very common for the query to be after the last #line, check this
00267   // first.
00268   if (Entries.back().FileOffset <= Offset)
00269     return &Entries.back();
00270 
00271   // Do a binary search to find the maximal element that is still before Offset.
00272   std::vector<LineEntry>::const_iterator I =
00273     std::upper_bound(Entries.begin(), Entries.end(), Offset);
00274   if (I == Entries.begin()) return nullptr;
00275   return &*--I;
00276 }
00277 
00278 /// \brief Add a new line entry that has already been encoded into
00279 /// the internal representation of the line table.
00280 void LineTableInfo::AddEntry(FileID FID,
00281                              const std::vector<LineEntry> &Entries) {
00282   LineEntries[FID] = Entries;
00283 }
00284 
00285 /// getLineTableFilenameID - Return the uniqued ID for the specified filename.
00286 ///
00287 unsigned SourceManager::getLineTableFilenameID(StringRef Name) {
00288   if (!LineTable)
00289     LineTable = new LineTableInfo();
00290   return LineTable->getLineTableFilenameID(Name);
00291 }
00292 
00293 
00294 /// AddLineNote - Add a line note to the line table for the FileID and offset
00295 /// specified by Loc.  If FilenameID is -1, it is considered to be
00296 /// unspecified.
00297 void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
00298                                 int FilenameID) {
00299   std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
00300 
00301   bool Invalid = false;
00302   const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid);
00303   if (!Entry.isFile() || Invalid)
00304     return;
00305   
00306   const SrcMgr::FileInfo &FileInfo = Entry.getFile();
00307 
00308   // Remember that this file has #line directives now if it doesn't already.
00309   const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
00310 
00311   if (!LineTable)
00312     LineTable = new LineTableInfo();
00313   LineTable->AddLineNote(LocInfo.first, LocInfo.second, LineNo, FilenameID);
00314 }
00315 
00316 /// AddLineNote - Add a GNU line marker to the line table.
00317 void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo,
00318                                 int FilenameID, bool IsFileEntry,
00319                                 bool IsFileExit, bool IsSystemHeader,
00320                                 bool IsExternCHeader) {
00321   // If there is no filename and no flags, this is treated just like a #line,
00322   // which does not change the flags of the previous line marker.
00323   if (FilenameID == -1) {
00324     assert(!IsFileEntry && !IsFileExit && !IsSystemHeader && !IsExternCHeader &&
00325            "Can't set flags without setting the filename!");
00326     return AddLineNote(Loc, LineNo, FilenameID);
00327   }
00328 
00329   std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
00330 
00331   bool Invalid = false;
00332   const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid);
00333   if (!Entry.isFile() || Invalid)
00334     return;
00335   
00336   const SrcMgr::FileInfo &FileInfo = Entry.getFile();
00337 
00338   // Remember that this file has #line directives now if it doesn't already.
00339   const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives();
00340 
00341   if (!LineTable)
00342     LineTable = new LineTableInfo();
00343 
00344   SrcMgr::CharacteristicKind FileKind;
00345   if (IsExternCHeader)
00346     FileKind = SrcMgr::C_ExternCSystem;
00347   else if (IsSystemHeader)
00348     FileKind = SrcMgr::C_System;
00349   else
00350     FileKind = SrcMgr::C_User;
00351 
00352   unsigned EntryExit = 0;
00353   if (IsFileEntry)
00354     EntryExit = 1;
00355   else if (IsFileExit)
00356     EntryExit = 2;
00357 
00358   LineTable->AddLineNote(LocInfo.first, LocInfo.second, LineNo, FilenameID,
00359                          EntryExit, FileKind);
00360 }
00361 
00362 LineTableInfo &SourceManager::getLineTable() {
00363   if (!LineTable)
00364     LineTable = new LineTableInfo();
00365   return *LineTable;
00366 }
00367 
00368 //===----------------------------------------------------------------------===//
00369 // Private 'Create' methods.
00370 //===----------------------------------------------------------------------===//
00371 
00372 SourceManager::SourceManager(DiagnosticsEngine &Diag, FileManager &FileMgr,
00373                              bool UserFilesAreVolatile)
00374   : Diag(Diag), FileMgr(FileMgr), OverridenFilesKeepOriginalName(true),
00375     UserFilesAreVolatile(UserFilesAreVolatile),
00376     ExternalSLocEntries(nullptr), LineTable(nullptr), NumLinearScans(0),
00377     NumBinaryProbes(0) {
00378   clearIDTables();
00379   Diag.setSourceManager(this);
00380 }
00381 
00382 SourceManager::~SourceManager() {
00383   delete LineTable;
00384 
00385   // Delete FileEntry objects corresponding to content caches.  Since the actual
00386   // content cache objects are bump pointer allocated, we just have to run the
00387   // dtors, but we call the deallocate method for completeness.
00388   for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) {
00389     if (MemBufferInfos[i]) {
00390       MemBufferInfos[i]->~ContentCache();
00391       ContentCacheAlloc.Deallocate(MemBufferInfos[i]);
00392     }
00393   }
00394   for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator
00395        I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
00396     if (I->second) {
00397       I->second->~ContentCache();
00398       ContentCacheAlloc.Deallocate(I->second);
00399     }
00400   }
00401 
00402   llvm::DeleteContainerSeconds(MacroArgsCacheMap);
00403 }
00404 
00405 void SourceManager::clearIDTables() {
00406   MainFileID = FileID();
00407   LocalSLocEntryTable.clear();
00408   LoadedSLocEntryTable.clear();
00409   SLocEntryLoaded.clear();
00410   LastLineNoFileIDQuery = FileID();
00411   LastLineNoContentCache = nullptr;
00412   LastFileIDLookup = FileID();
00413 
00414   if (LineTable)
00415     LineTable->clear();
00416 
00417   // Use up FileID #0 as an invalid expansion.
00418   NextLocalOffset = 0;
00419   CurrentLoadedOffset = MaxLoadedOffset;
00420   createExpansionLoc(SourceLocation(),SourceLocation(),SourceLocation(), 1);
00421 }
00422 
00423 /// getOrCreateContentCache - Create or return a cached ContentCache for the
00424 /// specified file.
00425 const ContentCache *
00426 SourceManager::getOrCreateContentCache(const FileEntry *FileEnt,
00427                                        bool isSystemFile) {
00428   assert(FileEnt && "Didn't specify a file entry to use?");
00429 
00430   // Do we already have information about this file?
00431   ContentCache *&Entry = FileInfos[FileEnt];
00432   if (Entry) return Entry;
00433 
00434   // Nope, create a new Cache entry.
00435   Entry = ContentCacheAlloc.Allocate<ContentCache>();
00436 
00437   if (OverriddenFilesInfo) {
00438     // If the file contents are overridden with contents from another file,
00439     // pass that file to ContentCache.
00440     llvm::DenseMap<const FileEntry *, const FileEntry *>::iterator
00441         overI = OverriddenFilesInfo->OverriddenFiles.find(FileEnt);
00442     if (overI == OverriddenFilesInfo->OverriddenFiles.end())
00443       new (Entry) ContentCache(FileEnt);
00444     else
00445       new (Entry) ContentCache(OverridenFilesKeepOriginalName ? FileEnt
00446                                                               : overI->second,
00447                                overI->second);
00448   } else {
00449     new (Entry) ContentCache(FileEnt);
00450   }
00451 
00452   Entry->IsSystemFile = isSystemFile;
00453 
00454   return Entry;
00455 }
00456 
00457 
00458 /// createMemBufferContentCache - Create a new ContentCache for the specified
00459 ///  memory buffer.  This does no caching.
00460 const ContentCache *SourceManager::createMemBufferContentCache(
00461     std::unique_ptr<llvm::MemoryBuffer> Buffer) {
00462   // Add a new ContentCache to the MemBufferInfos list and return it.
00463   ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>();
00464   new (Entry) ContentCache();
00465   MemBufferInfos.push_back(Entry);
00466   Entry->setBuffer(std::move(Buffer));
00467   return Entry;
00468 }
00469 
00470 const SrcMgr::SLocEntry &SourceManager::loadSLocEntry(unsigned Index,
00471                                                       bool *Invalid) const {
00472   assert(!SLocEntryLoaded[Index]);
00473   if (ExternalSLocEntries->ReadSLocEntry(-(static_cast<int>(Index) + 2))) {
00474     if (Invalid)
00475       *Invalid = true;
00476     // If the file of the SLocEntry changed we could still have loaded it.
00477     if (!SLocEntryLoaded[Index]) {
00478       // Try to recover; create a SLocEntry so the rest of clang can handle it.
00479       LoadedSLocEntryTable[Index] = SLocEntry::get(0,
00480                                  FileInfo::get(SourceLocation(),
00481                                                getFakeContentCacheForRecovery(),
00482                                                SrcMgr::C_User));
00483     }
00484   }
00485 
00486   return LoadedSLocEntryTable[Index];
00487 }
00488 
00489 std::pair<int, unsigned>
00490 SourceManager::AllocateLoadedSLocEntries(unsigned NumSLocEntries,
00491                                          unsigned TotalSize) {
00492   assert(ExternalSLocEntries && "Don't have an external sloc source");
00493   LoadedSLocEntryTable.resize(LoadedSLocEntryTable.size() + NumSLocEntries);
00494   SLocEntryLoaded.resize(LoadedSLocEntryTable.size());
00495   CurrentLoadedOffset -= TotalSize;
00496   assert(CurrentLoadedOffset >= NextLocalOffset && "Out of source locations");
00497   int ID = LoadedSLocEntryTable.size();
00498   return std::make_pair(-ID - 1, CurrentLoadedOffset);
00499 }
00500 
00501 /// \brief As part of recovering from missing or changed content, produce a
00502 /// fake, non-empty buffer.
00503 llvm::MemoryBuffer *SourceManager::getFakeBufferForRecovery() const {
00504   if (!FakeBufferForRecovery)
00505     FakeBufferForRecovery =
00506         llvm::MemoryBuffer::getMemBuffer("<<<INVALID BUFFER>>");
00507 
00508   return FakeBufferForRecovery.get();
00509 }
00510 
00511 /// \brief As part of recovering from missing or changed content, produce a
00512 /// fake content cache.
00513 const SrcMgr::ContentCache *
00514 SourceManager::getFakeContentCacheForRecovery() const {
00515   if (!FakeContentCacheForRecovery) {
00516     FakeContentCacheForRecovery = llvm::make_unique<SrcMgr::ContentCache>();
00517     FakeContentCacheForRecovery->replaceBuffer(getFakeBufferForRecovery(),
00518                                                /*DoNotFree=*/true);
00519   }
00520   return FakeContentCacheForRecovery.get();
00521 }
00522 
00523 /// \brief Returns the previous in-order FileID or an invalid FileID if there
00524 /// is no previous one.
00525 FileID SourceManager::getPreviousFileID(FileID FID) const {
00526   if (FID.isInvalid())
00527     return FileID();
00528 
00529   int ID = FID.ID;
00530   if (ID == -1)
00531     return FileID();
00532 
00533   if (ID > 0) {
00534     if (ID-1 == 0)
00535       return FileID();
00536   } else if (unsigned(-(ID-1) - 2) >= LoadedSLocEntryTable.size()) {
00537     return FileID();
00538   }
00539 
00540   return FileID::get(ID-1);
00541 }
00542 
00543 /// \brief Returns the next in-order FileID or an invalid FileID if there is
00544 /// no next one.
00545 FileID SourceManager::getNextFileID(FileID FID) const {
00546   if (FID.isInvalid())
00547     return FileID();
00548 
00549   int ID = FID.ID;
00550   if (ID > 0) {
00551     if (unsigned(ID+1) >= local_sloc_entry_size())
00552       return FileID();
00553   } else if (ID+1 >= -1) {
00554     return FileID();
00555   }
00556 
00557   return FileID::get(ID+1);
00558 }
00559 
00560 //===----------------------------------------------------------------------===//
00561 // Methods to create new FileID's and macro expansions.
00562 //===----------------------------------------------------------------------===//
00563 
00564 /// createFileID - Create a new FileID for the specified ContentCache and
00565 /// include position.  This works regardless of whether the ContentCache
00566 /// corresponds to a file or some other input source.
00567 FileID SourceManager::createFileID(const ContentCache *File,
00568                                    SourceLocation IncludePos,
00569                                    SrcMgr::CharacteristicKind FileCharacter,
00570                                    int LoadedID, unsigned LoadedOffset) {
00571   if (LoadedID < 0) {
00572     assert(LoadedID != -1 && "Loading sentinel FileID");
00573     unsigned Index = unsigned(-LoadedID) - 2;
00574     assert(Index < LoadedSLocEntryTable.size() && "FileID out of range");
00575     assert(!SLocEntryLoaded[Index] && "FileID already loaded");
00576     LoadedSLocEntryTable[Index] = SLocEntry::get(LoadedOffset,
00577         FileInfo::get(IncludePos, File, FileCharacter));
00578     SLocEntryLoaded[Index] = true;
00579     return FileID::get(LoadedID);
00580   }
00581   LocalSLocEntryTable.push_back(SLocEntry::get(NextLocalOffset,
00582                                                FileInfo::get(IncludePos, File,
00583                                                              FileCharacter)));
00584   unsigned FileSize = File->getSize();
00585   assert(NextLocalOffset + FileSize + 1 > NextLocalOffset &&
00586          NextLocalOffset + FileSize + 1 <= CurrentLoadedOffset &&
00587          "Ran out of source locations!");
00588   // We do a +1 here because we want a SourceLocation that means "the end of the
00589   // file", e.g. for the "no newline at the end of the file" diagnostic.
00590   NextLocalOffset += FileSize + 1;
00591 
00592   // Set LastFileIDLookup to the newly created file.  The next getFileID call is
00593   // almost guaranteed to be from that file.
00594   FileID FID = FileID::get(LocalSLocEntryTable.size()-1);
00595   return LastFileIDLookup = FID;
00596 }
00597 
00598 SourceLocation
00599 SourceManager::createMacroArgExpansionLoc(SourceLocation SpellingLoc,
00600                                           SourceLocation ExpansionLoc,
00601                                           unsigned TokLength) {
00602   ExpansionInfo Info = ExpansionInfo::createForMacroArg(SpellingLoc,
00603                                                         ExpansionLoc);
00604   return createExpansionLocImpl(Info, TokLength);
00605 }
00606 
00607 SourceLocation
00608 SourceManager::createExpansionLoc(SourceLocation SpellingLoc,
00609                                   SourceLocation ExpansionLocStart,
00610                                   SourceLocation ExpansionLocEnd,
00611                                   unsigned TokLength,
00612                                   int LoadedID,
00613                                   unsigned LoadedOffset) {
00614   ExpansionInfo Info = ExpansionInfo::create(SpellingLoc, ExpansionLocStart,
00615                                              ExpansionLocEnd);
00616   return createExpansionLocImpl(Info, TokLength, LoadedID, LoadedOffset);
00617 }
00618 
00619 SourceLocation
00620 SourceManager::createExpansionLocImpl(const ExpansionInfo &Info,
00621                                       unsigned TokLength,
00622                                       int LoadedID,
00623                                       unsigned LoadedOffset) {
00624   if (LoadedID < 0) {
00625     assert(LoadedID != -1 && "Loading sentinel FileID");
00626     unsigned Index = unsigned(-LoadedID) - 2;
00627     assert(Index < LoadedSLocEntryTable.size() && "FileID out of range");
00628     assert(!SLocEntryLoaded[Index] && "FileID already loaded");
00629     LoadedSLocEntryTable[Index] = SLocEntry::get(LoadedOffset, Info);
00630     SLocEntryLoaded[Index] = true;
00631     return SourceLocation::getMacroLoc(LoadedOffset);
00632   }
00633   LocalSLocEntryTable.push_back(SLocEntry::get(NextLocalOffset, Info));
00634   assert(NextLocalOffset + TokLength + 1 > NextLocalOffset &&
00635          NextLocalOffset + TokLength + 1 <= CurrentLoadedOffset &&
00636          "Ran out of source locations!");
00637   // See createFileID for that +1.
00638   NextLocalOffset += TokLength + 1;
00639   return SourceLocation::getMacroLoc(NextLocalOffset - (TokLength + 1));
00640 }
00641 
00642 llvm::MemoryBuffer *SourceManager::getMemoryBufferForFile(const FileEntry *File,
00643                                                           bool *Invalid) {
00644   const SrcMgr::ContentCache *IR = getOrCreateContentCache(File);
00645   assert(IR && "getOrCreateContentCache() cannot return NULL");
00646   return IR->getBuffer(Diag, *this, SourceLocation(), Invalid);
00647 }
00648 
00649 void SourceManager::overrideFileContents(const FileEntry *SourceFile,
00650                                          llvm::MemoryBuffer *Buffer,
00651                                          bool DoNotFree) {
00652   const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile);
00653   assert(IR && "getOrCreateContentCache() cannot return NULL");
00654 
00655   const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(Buffer, DoNotFree);
00656   const_cast<SrcMgr::ContentCache *>(IR)->BufferOverridden = true;
00657 
00658   getOverriddenFilesInfo().OverriddenFilesWithBuffer.insert(SourceFile);
00659 }
00660 
00661 void SourceManager::overrideFileContents(const FileEntry *SourceFile,
00662                                          const FileEntry *NewFile) {
00663   assert(SourceFile->getSize() == NewFile->getSize() &&
00664          "Different sizes, use the FileManager to create a virtual file with "
00665          "the correct size");
00666   assert(FileInfos.count(SourceFile) == 0 &&
00667          "This function should be called at the initialization stage, before "
00668          "any parsing occurs.");
00669   getOverriddenFilesInfo().OverriddenFiles[SourceFile] = NewFile;
00670 }
00671 
00672 void SourceManager::disableFileContentsOverride(const FileEntry *File) {
00673   if (!isFileOverridden(File))
00674     return;
00675 
00676   const SrcMgr::ContentCache *IR = getOrCreateContentCache(File);
00677   const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(nullptr);
00678   const_cast<SrcMgr::ContentCache *>(IR)->ContentsEntry = IR->OrigEntry;
00679 
00680   assert(OverriddenFilesInfo);
00681   OverriddenFilesInfo->OverriddenFiles.erase(File);
00682   OverriddenFilesInfo->OverriddenFilesWithBuffer.erase(File);
00683 }
00684 
00685 StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const {
00686   bool MyInvalid = false;
00687   const SLocEntry &SLoc = getSLocEntry(FID, &MyInvalid);
00688   if (!SLoc.isFile() || MyInvalid) {
00689     if (Invalid) 
00690       *Invalid = true;
00691     return "<<<<<INVALID SOURCE LOCATION>>>>>";
00692   }
00693 
00694   llvm::MemoryBuffer *Buf = SLoc.getFile().getContentCache()->getBuffer(
00695       Diag, *this, SourceLocation(), &MyInvalid);
00696   if (Invalid)
00697     *Invalid = MyInvalid;
00698 
00699   if (MyInvalid)
00700     return "<<<<<INVALID SOURCE LOCATION>>>>>";
00701   
00702   return Buf->getBuffer();
00703 }
00704 
00705 //===----------------------------------------------------------------------===//
00706 // SourceLocation manipulation methods.
00707 //===----------------------------------------------------------------------===//
00708 
00709 /// \brief Return the FileID for a SourceLocation.
00710 ///
00711 /// This is the cache-miss path of getFileID. Not as hot as that function, but
00712 /// still very important. It is responsible for finding the entry in the
00713 /// SLocEntry tables that contains the specified location.
00714 FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
00715   if (!SLocOffset)
00716     return FileID::get(0);
00717 
00718   // Now it is time to search for the correct file. See where the SLocOffset
00719   // sits in the global view and consult local or loaded buffers for it.
00720   if (SLocOffset < NextLocalOffset)
00721     return getFileIDLocal(SLocOffset);
00722   return getFileIDLoaded(SLocOffset);
00723 }
00724 
00725 /// \brief Return the FileID for a SourceLocation with a low offset.
00726 ///
00727 /// This function knows that the SourceLocation is in a local buffer, not a
00728 /// loaded one.
00729 FileID SourceManager::getFileIDLocal(unsigned SLocOffset) const {
00730   assert(SLocOffset < NextLocalOffset && "Bad function choice");
00731 
00732   // After the first and second level caches, I see two common sorts of
00733   // behavior: 1) a lot of searched FileID's are "near" the cached file
00734   // location or are "near" the cached expansion location. 2) others are just
00735   // completely random and may be a very long way away.
00736   //
00737   // To handle this, we do a linear search for up to 8 steps to catch #1 quickly
00738   // then we fall back to a less cache efficient, but more scalable, binary
00739   // search to find the location.
00740 
00741   // See if this is near the file point - worst case we start scanning from the
00742   // most newly created FileID.
00743   const SrcMgr::SLocEntry *I;
00744 
00745   if (LastFileIDLookup.ID < 0 ||
00746       LocalSLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
00747     // Neither loc prunes our search.
00748     I = LocalSLocEntryTable.end();
00749   } else {
00750     // Perhaps it is near the file point.
00751     I = LocalSLocEntryTable.begin()+LastFileIDLookup.ID;
00752   }
00753 
00754   // Find the FileID that contains this.  "I" is an iterator that points to a
00755   // FileID whose offset is known to be larger than SLocOffset.
00756   unsigned NumProbes = 0;
00757   while (1) {
00758     --I;
00759     if (I->getOffset() <= SLocOffset) {
00760       FileID Res = FileID::get(int(I - LocalSLocEntryTable.begin()));
00761 
00762       // If this isn't an expansion, remember it.  We have good locality across
00763       // FileID lookups.
00764       if (!I->isExpansion())
00765         LastFileIDLookup = Res;
00766       NumLinearScans += NumProbes+1;
00767       return Res;
00768     }
00769     if (++NumProbes == 8)
00770       break;
00771   }
00772 
00773   // Convert "I" back into an index.  We know that it is an entry whose index is
00774   // larger than the offset we are looking for.
00775   unsigned GreaterIndex = I - LocalSLocEntryTable.begin();
00776   // LessIndex - This is the lower bound of the range that we're searching.
00777   // We know that the offset corresponding to the FileID is is less than
00778   // SLocOffset.
00779   unsigned LessIndex = 0;
00780   NumProbes = 0;
00781   while (1) {
00782     bool Invalid = false;
00783     unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
00784     unsigned MidOffset = getLocalSLocEntry(MiddleIndex, &Invalid).getOffset();
00785     if (Invalid)
00786       return FileID::get(0);
00787     
00788     ++NumProbes;
00789 
00790     // If the offset of the midpoint is too large, chop the high side of the
00791     // range to the midpoint.
00792     if (MidOffset > SLocOffset) {
00793       GreaterIndex = MiddleIndex;
00794       continue;
00795     }
00796 
00797     // If the middle index contains the value, succeed and return.
00798     // FIXME: This could be made faster by using a function that's aware of
00799     // being in the local area.
00800     if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
00801       FileID Res = FileID::get(MiddleIndex);
00802 
00803       // If this isn't a macro expansion, remember it.  We have good locality
00804       // across FileID lookups.
00805       if (!LocalSLocEntryTable[MiddleIndex].isExpansion())
00806         LastFileIDLookup = Res;
00807       NumBinaryProbes += NumProbes;
00808       return Res;
00809     }
00810 
00811     // Otherwise, move the low-side up to the middle index.
00812     LessIndex = MiddleIndex;
00813   }
00814 }
00815 
00816 /// \brief Return the FileID for a SourceLocation with a high offset.
00817 ///
00818 /// This function knows that the SourceLocation is in a loaded buffer, not a
00819 /// local one.
00820 FileID SourceManager::getFileIDLoaded(unsigned SLocOffset) const {
00821   // Sanity checking, otherwise a bug may lead to hanging in release build.
00822   if (SLocOffset < CurrentLoadedOffset) {
00823     assert(0 && "Invalid SLocOffset or bad function choice");
00824     return FileID();
00825   }
00826 
00827   // Essentially the same as the local case, but the loaded array is sorted
00828   // in the other direction.
00829 
00830   // First do a linear scan from the last lookup position, if possible.
00831   unsigned I;
00832   int LastID = LastFileIDLookup.ID;
00833   if (LastID >= 0 || getLoadedSLocEntryByID(LastID).getOffset() < SLocOffset)
00834     I = 0;
00835   else
00836     I = (-LastID - 2) + 1;
00837 
00838   unsigned NumProbes;
00839   for (NumProbes = 0; NumProbes < 8; ++NumProbes, ++I) {
00840     // Make sure the entry is loaded!
00841     const SrcMgr::SLocEntry &E = getLoadedSLocEntry(I);
00842     if (E.getOffset() <= SLocOffset) {
00843       FileID Res = FileID::get(-int(I) - 2);
00844 
00845       if (!E.isExpansion())
00846         LastFileIDLookup = Res;
00847       NumLinearScans += NumProbes + 1;
00848       return Res;
00849     }
00850   }
00851 
00852   // Linear scan failed. Do the binary search. Note the reverse sorting of the
00853   // table: GreaterIndex is the one where the offset is greater, which is
00854   // actually a lower index!
00855   unsigned GreaterIndex = I;
00856   unsigned LessIndex = LoadedSLocEntryTable.size();
00857   NumProbes = 0;
00858   while (1) {
00859     ++NumProbes;
00860     unsigned MiddleIndex = (LessIndex - GreaterIndex) / 2 + GreaterIndex;
00861     const SrcMgr::SLocEntry &E = getLoadedSLocEntry(MiddleIndex);
00862     if (E.getOffset() == 0)
00863       return FileID(); // invalid entry.
00864 
00865     ++NumProbes;
00866 
00867     if (E.getOffset() > SLocOffset) {
00868       // Sanity checking, otherwise a bug may lead to hanging in release build.
00869       if (GreaterIndex == MiddleIndex) {
00870         assert(0 && "binary search missed the entry");
00871         return FileID();
00872       }
00873       GreaterIndex = MiddleIndex;
00874       continue;
00875     }
00876 
00877     if (isOffsetInFileID(FileID::get(-int(MiddleIndex) - 2), SLocOffset)) {
00878       FileID Res = FileID::get(-int(MiddleIndex) - 2);
00879       if (!E.isExpansion())
00880         LastFileIDLookup = Res;
00881       NumBinaryProbes += NumProbes;
00882       return Res;
00883     }
00884 
00885     // Sanity checking, otherwise a bug may lead to hanging in release build.
00886     if (LessIndex == MiddleIndex) {
00887       assert(0 && "binary search missed the entry");
00888       return FileID();
00889     }
00890     LessIndex = MiddleIndex;
00891   }
00892 }
00893 
00894 SourceLocation SourceManager::
00895 getExpansionLocSlowCase(SourceLocation Loc) const {
00896   do {
00897     // Note: If Loc indicates an offset into a token that came from a macro
00898     // expansion (e.g. the 5th character of the token) we do not want to add
00899     // this offset when going to the expansion location.  The expansion
00900     // location is the macro invocation, which the offset has nothing to do
00901     // with.  This is unlike when we get the spelling loc, because the offset
00902     // directly correspond to the token whose spelling we're inspecting.
00903     Loc = getSLocEntry(getFileID(Loc)).getExpansion().getExpansionLocStart();
00904   } while (!Loc.isFileID());
00905 
00906   return Loc;
00907 }
00908 
00909 SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const {
00910   do {
00911     std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
00912     Loc = getSLocEntry(LocInfo.first).getExpansion().getSpellingLoc();
00913     Loc = Loc.getLocWithOffset(LocInfo.second);
00914   } while (!Loc.isFileID());
00915   return Loc;
00916 }
00917 
00918 SourceLocation SourceManager::getFileLocSlowCase(SourceLocation Loc) const {
00919   do {
00920     if (isMacroArgExpansion(Loc))
00921       Loc = getImmediateSpellingLoc(Loc);
00922     else
00923       Loc = getImmediateExpansionRange(Loc).first;
00924   } while (!Loc.isFileID());
00925   return Loc;
00926 }
00927 
00928 
00929 std::pair<FileID, unsigned>
00930 SourceManager::getDecomposedExpansionLocSlowCase(
00931                                              const SrcMgr::SLocEntry *E) const {
00932   // If this is an expansion record, walk through all the expansion points.
00933   FileID FID;
00934   SourceLocation Loc;
00935   unsigned Offset;
00936   do {
00937     Loc = E->getExpansion().getExpansionLocStart();
00938 
00939     FID = getFileID(Loc);
00940     E = &getSLocEntry(FID);
00941     Offset = Loc.getOffset()-E->getOffset();
00942   } while (!Loc.isFileID());
00943 
00944   return std::make_pair(FID, Offset);
00945 }
00946 
00947 std::pair<FileID, unsigned>
00948 SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
00949                                                 unsigned Offset) const {
00950   // If this is an expansion record, walk through all the expansion points.
00951   FileID FID;
00952   SourceLocation Loc;
00953   do {
00954     Loc = E->getExpansion().getSpellingLoc();
00955     Loc = Loc.getLocWithOffset(Offset);
00956 
00957     FID = getFileID(Loc);
00958     E = &getSLocEntry(FID);
00959     Offset = Loc.getOffset()-E->getOffset();
00960   } while (!Loc.isFileID());
00961 
00962   return std::make_pair(FID, Offset);
00963 }
00964 
00965 /// getImmediateSpellingLoc - Given a SourceLocation object, return the
00966 /// spelling location referenced by the ID.  This is the first level down
00967 /// towards the place where the characters that make up the lexed token can be
00968 /// found.  This should not generally be used by clients.
00969 SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{
00970   if (Loc.isFileID()) return Loc;
00971   std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
00972   Loc = getSLocEntry(LocInfo.first).getExpansion().getSpellingLoc();
00973   return Loc.getLocWithOffset(LocInfo.second);
00974 }
00975 
00976 
00977 /// getImmediateExpansionRange - Loc is required to be an expansion location.
00978 /// Return the start/end of the expansion information.
00979 std::pair<SourceLocation,SourceLocation>
00980 SourceManager::getImmediateExpansionRange(SourceLocation Loc) const {
00981   assert(Loc.isMacroID() && "Not a macro expansion loc!");
00982   const ExpansionInfo &Expansion = getSLocEntry(getFileID(Loc)).getExpansion();
00983   return Expansion.getExpansionLocRange();
00984 }
00985 
00986 /// getExpansionRange - Given a SourceLocation object, return the range of
00987 /// tokens covered by the expansion in the ultimate file.
00988 std::pair<SourceLocation,SourceLocation>
00989 SourceManager::getExpansionRange(SourceLocation Loc) const {
00990   if (Loc.isFileID()) return std::make_pair(Loc, Loc);
00991 
00992   std::pair<SourceLocation,SourceLocation> Res =
00993     getImmediateExpansionRange(Loc);
00994 
00995   // Fully resolve the start and end locations to their ultimate expansion
00996   // points.
00997   while (!Res.first.isFileID())
00998     Res.first = getImmediateExpansionRange(Res.first).first;
00999   while (!Res.second.isFileID())
01000     Res.second = getImmediateExpansionRange(Res.second).second;
01001   return Res;
01002 }
01003 
01004 bool SourceManager::isMacroArgExpansion(SourceLocation Loc) const {
01005   if (!Loc.isMacroID()) return false;
01006 
01007   FileID FID = getFileID(Loc);
01008   const SrcMgr::ExpansionInfo &Expansion = getSLocEntry(FID).getExpansion();
01009   return Expansion.isMacroArgExpansion();
01010 }
01011 
01012 bool SourceManager::isMacroBodyExpansion(SourceLocation Loc) const {
01013   if (!Loc.isMacroID()) return false;
01014 
01015   FileID FID = getFileID(Loc);
01016   const SrcMgr::ExpansionInfo &Expansion = getSLocEntry(FID).getExpansion();
01017   return Expansion.isMacroBodyExpansion();
01018 }
01019 
01020 bool SourceManager::isAtStartOfImmediateMacroExpansion(SourceLocation Loc,
01021                                              SourceLocation *MacroBegin) const {
01022   assert(Loc.isValid() && Loc.isMacroID() && "Expected a valid macro loc");
01023 
01024   std::pair<FileID, unsigned> DecompLoc = getDecomposedLoc(Loc);
01025   if (DecompLoc.second > 0)
01026     return false; // Does not point at the start of expansion range.
01027 
01028   bool Invalid = false;
01029   const SrcMgr::ExpansionInfo &ExpInfo =
01030       getSLocEntry(DecompLoc.first, &Invalid).getExpansion();
01031   if (Invalid)
01032     return false;
01033   SourceLocation ExpLoc = ExpInfo.getExpansionLocStart();
01034 
01035   if (ExpInfo.isMacroArgExpansion()) {
01036     // For macro argument expansions, check if the previous FileID is part of
01037     // the same argument expansion, in which case this Loc is not at the
01038     // beginning of the expansion.
01039     FileID PrevFID = getPreviousFileID(DecompLoc.first);
01040     if (!PrevFID.isInvalid()) {
01041       const SrcMgr::SLocEntry &PrevEntry = getSLocEntry(PrevFID, &Invalid);
01042       if (Invalid)
01043         return false;
01044       if (PrevEntry.isExpansion() &&
01045           PrevEntry.getExpansion().getExpansionLocStart() == ExpLoc)
01046         return false;
01047     }
01048   }
01049 
01050   if (MacroBegin)
01051     *MacroBegin = ExpLoc;
01052   return true;
01053 }
01054 
01055 bool SourceManager::isAtEndOfImmediateMacroExpansion(SourceLocation Loc,
01056                                                SourceLocation *MacroEnd) const {
01057   assert(Loc.isValid() && Loc.isMacroID() && "Expected a valid macro loc");
01058 
01059   FileID FID = getFileID(Loc);
01060   SourceLocation NextLoc = Loc.getLocWithOffset(1);
01061   if (isInFileID(NextLoc, FID))
01062     return false; // Does not point at the end of expansion range.
01063 
01064   bool Invalid = false;
01065   const SrcMgr::ExpansionInfo &ExpInfo =
01066       getSLocEntry(FID, &Invalid).getExpansion();
01067   if (Invalid)
01068     return false;
01069 
01070   if (ExpInfo.isMacroArgExpansion()) {
01071     // For macro argument expansions, check if the next FileID is part of the
01072     // same argument expansion, in which case this Loc is not at the end of the
01073     // expansion.
01074     FileID NextFID = getNextFileID(FID);
01075     if (!NextFID.isInvalid()) {
01076       const SrcMgr::SLocEntry &NextEntry = getSLocEntry(NextFID, &Invalid);
01077       if (Invalid)
01078         return false;
01079       if (NextEntry.isExpansion() &&
01080           NextEntry.getExpansion().getExpansionLocStart() ==
01081               ExpInfo.getExpansionLocStart())
01082         return false;
01083     }
01084   }
01085 
01086   if (MacroEnd)
01087     *MacroEnd = ExpInfo.getExpansionLocEnd();
01088   return true;
01089 }
01090 
01091 
01092 //===----------------------------------------------------------------------===//
01093 // Queries about the code at a SourceLocation.
01094 //===----------------------------------------------------------------------===//
01095 
01096 /// getCharacterData - Return a pointer to the start of the specified location
01097 /// in the appropriate MemoryBuffer.
01098 const char *SourceManager::getCharacterData(SourceLocation SL,
01099                                             bool *Invalid) const {
01100   // Note that this is a hot function in the getSpelling() path, which is
01101   // heavily used by -E mode.
01102   std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
01103 
01104   // Note that calling 'getBuffer()' may lazily page in a source file.
01105   bool CharDataInvalid = false;
01106   const SLocEntry &Entry = getSLocEntry(LocInfo.first, &CharDataInvalid);
01107   if (CharDataInvalid || !Entry.isFile()) {
01108     if (Invalid)
01109       *Invalid = true;
01110     
01111     return "<<<<INVALID BUFFER>>>>";
01112   }
01113   llvm::MemoryBuffer *Buffer = Entry.getFile().getContentCache()->getBuffer(
01114       Diag, *this, SourceLocation(), &CharDataInvalid);
01115   if (Invalid)
01116     *Invalid = CharDataInvalid;
01117   return Buffer->getBufferStart() + (CharDataInvalid? 0 : LocInfo.second);
01118 }
01119 
01120 
01121 /// getColumnNumber - Return the column # for the specified file position.
01122 /// this is significantly cheaper to compute than the line number.
01123 unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos,
01124                                         bool *Invalid) const {
01125   bool MyInvalid = false;
01126   llvm::MemoryBuffer *MemBuf = getBuffer(FID, &MyInvalid);
01127   if (Invalid)
01128     *Invalid = MyInvalid;
01129 
01130   if (MyInvalid)
01131     return 1;
01132 
01133   // It is okay to request a position just past the end of the buffer.
01134   if (FilePos > MemBuf->getBufferSize()) {
01135     if (Invalid)
01136       *Invalid = true;
01137     return 1;
01138   }
01139 
01140   // See if we just calculated the line number for this FilePos and can use
01141   // that to lookup the start of the line instead of searching for it.
01142   if (LastLineNoFileIDQuery == FID &&
01143       LastLineNoContentCache->SourceLineCache != nullptr &&
01144       LastLineNoResult < LastLineNoContentCache->NumLines) {
01145     unsigned *SourceLineCache = LastLineNoContentCache->SourceLineCache;
01146     unsigned LineStart = SourceLineCache[LastLineNoResult - 1];
01147     unsigned LineEnd = SourceLineCache[LastLineNoResult];
01148     if (FilePos >= LineStart && FilePos < LineEnd)
01149       return FilePos - LineStart + 1;
01150   }
01151 
01152   const char *Buf = MemBuf->getBufferStart();
01153   unsigned LineStart = FilePos;
01154   while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
01155     --LineStart;
01156   return FilePos-LineStart+1;
01157 }
01158 
01159 // isInvalid - Return the result of calling loc.isInvalid(), and
01160 // if Invalid is not null, set its value to same.
01161 static bool isInvalid(SourceLocation Loc, bool *Invalid) {
01162   bool MyInvalid = Loc.isInvalid();
01163   if (Invalid)
01164     *Invalid = MyInvalid;
01165   return MyInvalid;
01166 }
01167 
01168 unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc,
01169                                                 bool *Invalid) const {
01170   if (isInvalid(Loc, Invalid)) return 0;
01171   std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
01172   return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
01173 }
01174 
01175 unsigned SourceManager::getExpansionColumnNumber(SourceLocation Loc,
01176                                                  bool *Invalid) const {
01177   if (isInvalid(Loc, Invalid)) return 0;
01178   std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
01179   return getColumnNumber(LocInfo.first, LocInfo.second, Invalid);
01180 }
01181 
01182 unsigned SourceManager::getPresumedColumnNumber(SourceLocation Loc,
01183                                                 bool *Invalid) const {
01184   if (isInvalid(Loc, Invalid)) return 0;
01185   return getPresumedLoc(Loc).getColumn();
01186 }
01187 
01188 #ifdef __SSE2__
01189 #include <emmintrin.h>
01190 #endif
01191 
01192 static LLVM_ATTRIBUTE_NOINLINE void
01193 ComputeLineNumbers(DiagnosticsEngine &Diag, ContentCache *FI,
01194                    llvm::BumpPtrAllocator &Alloc,
01195                    const SourceManager &SM, bool &Invalid);
01196 static void ComputeLineNumbers(DiagnosticsEngine &Diag, ContentCache *FI,
01197                                llvm::BumpPtrAllocator &Alloc,
01198                                const SourceManager &SM, bool &Invalid) {
01199   // Note that calling 'getBuffer()' may lazily page in the file.
01200   MemoryBuffer *Buffer = FI->getBuffer(Diag, SM, SourceLocation(), &Invalid);
01201   if (Invalid)
01202     return;
01203 
01204   // Find the file offsets of all of the *physical* source lines.  This does
01205   // not look at trigraphs, escaped newlines, or anything else tricky.
01206   SmallVector<unsigned, 256> LineOffsets;
01207 
01208   // Line #1 starts at char 0.
01209   LineOffsets.push_back(0);
01210 
01211   const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
01212   const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
01213   unsigned Offs = 0;
01214   while (1) {
01215     // Skip over the contents of the line.
01216     const unsigned char *NextBuf = (const unsigned char *)Buf;
01217 
01218 #ifdef __SSE2__
01219     // Try to skip to the next newline using SSE instructions. This is very
01220     // performance sensitive for programs with lots of diagnostics and in -E
01221     // mode.
01222     __m128i CRs = _mm_set1_epi8('\r');
01223     __m128i LFs = _mm_set1_epi8('\n');
01224 
01225     // First fix up the alignment to 16 bytes.
01226     while (((uintptr_t)NextBuf & 0xF) != 0) {
01227       if (*NextBuf == '\n' || *NextBuf == '\r' || *NextBuf == '\0')
01228         goto FoundSpecialChar;
01229       ++NextBuf;
01230     }
01231 
01232     // Scan 16 byte chunks for '\r' and '\n'. Ignore '\0'.
01233     while (NextBuf+16 <= End) {
01234       const __m128i Chunk = *(const __m128i*)NextBuf;
01235       __m128i Cmp = _mm_or_si128(_mm_cmpeq_epi8(Chunk, CRs),
01236                                  _mm_cmpeq_epi8(Chunk, LFs));
01237       unsigned Mask = _mm_movemask_epi8(Cmp);
01238 
01239       // If we found a newline, adjust the pointer and jump to the handling code.
01240       if (Mask != 0) {
01241         NextBuf += llvm::countTrailingZeros(Mask);
01242         goto FoundSpecialChar;
01243       }
01244       NextBuf += 16;
01245     }
01246 #endif
01247 
01248     while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
01249       ++NextBuf;
01250 
01251 #ifdef __SSE2__
01252 FoundSpecialChar:
01253 #endif
01254     Offs += NextBuf-Buf;
01255     Buf = NextBuf;
01256 
01257     if (Buf[0] == '\n' || Buf[0] == '\r') {
01258       // If this is \n\r or \r\n, skip both characters.
01259       if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
01260         ++Offs, ++Buf;
01261       ++Offs, ++Buf;
01262       LineOffsets.push_back(Offs);
01263     } else {
01264       // Otherwise, this is a null.  If end of file, exit.
01265       if (Buf == End) break;
01266       // Otherwise, skip the null.
01267       ++Offs, ++Buf;
01268     }
01269   }
01270 
01271   // Copy the offsets into the FileInfo structure.
01272   FI->NumLines = LineOffsets.size();
01273   FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size());
01274   std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
01275 }
01276 
01277 /// getLineNumber - Given a SourceLocation, return the spelling line number
01278 /// for the position indicated.  This requires building and caching a table of
01279 /// line offsets for the MemoryBuffer, so this is not cheap: use only when
01280 /// about to emit a diagnostic.
01281 unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos, 
01282                                       bool *Invalid) const {
01283   if (FID.isInvalid()) {
01284     if (Invalid)
01285       *Invalid = true;
01286     return 1;
01287   }
01288 
01289   ContentCache *Content;
01290   if (LastLineNoFileIDQuery == FID)
01291     Content = LastLineNoContentCache;
01292   else {
01293     bool MyInvalid = false;
01294     const SLocEntry &Entry = getSLocEntry(FID, &MyInvalid);
01295     if (MyInvalid || !Entry.isFile()) {
01296       if (Invalid)
01297         *Invalid = true;
01298       return 1;
01299     }
01300     
01301     Content = const_cast<ContentCache*>(Entry.getFile().getContentCache());
01302   }
01303   
01304   // If this is the first use of line information for this buffer, compute the
01305   /// SourceLineCache for it on demand.
01306   if (!Content->SourceLineCache) {
01307     bool MyInvalid = false;
01308     ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid);
01309     if (Invalid)
01310       *Invalid = MyInvalid;
01311     if (MyInvalid)
01312       return 1;
01313   } else if (Invalid)
01314     *Invalid = false;
01315 
01316   // Okay, we know we have a line number table.  Do a binary search to find the
01317   // line number that this character position lands on.
01318   unsigned *SourceLineCache = Content->SourceLineCache;
01319   unsigned *SourceLineCacheStart = SourceLineCache;
01320   unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
01321 
01322   unsigned QueriedFilePos = FilePos+1;
01323 
01324   // FIXME: I would like to be convinced that this code is worth being as
01325   // complicated as it is, binary search isn't that slow.
01326   //
01327   // If it is worth being optimized, then in my opinion it could be more
01328   // performant, simpler, and more obviously correct by just "galloping" outward
01329   // from the queried file position. In fact, this could be incorporated into a
01330   // generic algorithm such as lower_bound_with_hint.
01331   //
01332   // If someone gives me a test case where this matters, and I will do it! - DWD
01333 
01334   // If the previous query was to the same file, we know both the file pos from
01335   // that query and the line number returned.  This allows us to narrow the
01336   // search space from the entire file to something near the match.
01337   if (LastLineNoFileIDQuery == FID) {
01338     if (QueriedFilePos >= LastLineNoFilePos) {
01339       // FIXME: Potential overflow?
01340       SourceLineCache = SourceLineCache+LastLineNoResult-1;
01341 
01342       // The query is likely to be nearby the previous one.  Here we check to
01343       // see if it is within 5, 10 or 20 lines.  It can be far away in cases
01344       // where big comment blocks and vertical whitespace eat up lines but
01345       // contribute no tokens.
01346       if (SourceLineCache+5 < SourceLineCacheEnd) {
01347         if (SourceLineCache[5] > QueriedFilePos)
01348           SourceLineCacheEnd = SourceLineCache+5;
01349         else if (SourceLineCache+10 < SourceLineCacheEnd) {
01350           if (SourceLineCache[10] > QueriedFilePos)
01351             SourceLineCacheEnd = SourceLineCache+10;
01352           else if (SourceLineCache+20 < SourceLineCacheEnd) {
01353             if (SourceLineCache[20] > QueriedFilePos)
01354               SourceLineCacheEnd = SourceLineCache+20;
01355           }
01356         }
01357       }
01358     } else {
01359       if (LastLineNoResult < Content->NumLines)
01360         SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
01361     }
01362   }
01363 
01364   unsigned *Pos
01365     = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
01366   unsigned LineNo = Pos-SourceLineCacheStart;
01367 
01368   LastLineNoFileIDQuery = FID;
01369   LastLineNoContentCache = Content;
01370   LastLineNoFilePos = QueriedFilePos;
01371   LastLineNoResult = LineNo;
01372   return LineNo;
01373 }
01374 
01375 unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc, 
01376                                               bool *Invalid) const {
01377   if (isInvalid(Loc, Invalid)) return 0;
01378   std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc);
01379   return getLineNumber(LocInfo.first, LocInfo.second);
01380 }
01381 unsigned SourceManager::getExpansionLineNumber(SourceLocation Loc,
01382                                                bool *Invalid) const {
01383   if (isInvalid(Loc, Invalid)) return 0;
01384   std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
01385   return getLineNumber(LocInfo.first, LocInfo.second);
01386 }
01387 unsigned SourceManager::getPresumedLineNumber(SourceLocation Loc,
01388                                               bool *Invalid) const {
01389   if (isInvalid(Loc, Invalid)) return 0;
01390   return getPresumedLoc(Loc).getLine();
01391 }
01392 
01393 /// getFileCharacteristic - return the file characteristic of the specified
01394 /// source location, indicating whether this is a normal file, a system
01395 /// header, or an "implicit extern C" system header.
01396 ///
01397 /// This state can be modified with flags on GNU linemarker directives like:
01398 ///   # 4 "foo.h" 3
01399 /// which changes all source locations in the current file after that to be
01400 /// considered to be from a system header.
01401 SrcMgr::CharacteristicKind
01402 SourceManager::getFileCharacteristic(SourceLocation Loc) const {
01403   assert(!Loc.isInvalid() && "Can't get file characteristic of invalid loc!");
01404   std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
01405   bool Invalid = false;
01406   const SLocEntry &SEntry = getSLocEntry(LocInfo.first, &Invalid);
01407   if (Invalid || !SEntry.isFile())
01408     return C_User;
01409   
01410   const SrcMgr::FileInfo &FI = SEntry.getFile();
01411 
01412   // If there are no #line directives in this file, just return the whole-file
01413   // state.
01414   if (!FI.hasLineDirectives())
01415     return FI.getFileCharacteristic();
01416 
01417   assert(LineTable && "Can't have linetable entries without a LineTable!");
01418   // See if there is a #line directive before the location.
01419   const LineEntry *Entry =
01420     LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second);
01421 
01422   // If this is before the first line marker, use the file characteristic.
01423   if (!Entry)
01424     return FI.getFileCharacteristic();
01425 
01426   return Entry->FileKind;
01427 }
01428 
01429 /// Return the filename or buffer identifier of the buffer the location is in.
01430 /// Note that this name does not respect \#line directives.  Use getPresumedLoc
01431 /// for normal clients.
01432 const char *SourceManager::getBufferName(SourceLocation Loc, 
01433                                          bool *Invalid) const {
01434   if (isInvalid(Loc, Invalid)) return "<invalid loc>";
01435 
01436   return getBuffer(getFileID(Loc), Invalid)->getBufferIdentifier();
01437 }
01438 
01439 
01440 /// getPresumedLoc - This method returns the "presumed" location of a
01441 /// SourceLocation specifies.  A "presumed location" can be modified by \#line
01442 /// or GNU line marker directives.  This provides a view on the data that a
01443 /// user should see in diagnostics, for example.
01444 ///
01445 /// Note that a presumed location is always given as the expansion point of an
01446 /// expansion location, not at the spelling location.
01447 PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc,
01448                                           bool UseLineDirectives) const {
01449   if (Loc.isInvalid()) return PresumedLoc();
01450 
01451   // Presumed locations are always for expansion points.
01452   std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
01453 
01454   bool Invalid = false;
01455   const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid);
01456   if (Invalid || !Entry.isFile())
01457     return PresumedLoc();
01458   
01459   const SrcMgr::FileInfo &FI = Entry.getFile();
01460   const SrcMgr::ContentCache *C = FI.getContentCache();
01461 
01462   // To get the source name, first consult the FileEntry (if one exists)
01463   // before the MemBuffer as this will avoid unnecessarily paging in the
01464   // MemBuffer.
01465   const char *Filename;
01466   if (C->OrigEntry)
01467     Filename = C->OrigEntry->getName();
01468   else
01469     Filename = C->getBuffer(Diag, *this)->getBufferIdentifier();
01470 
01471   unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second, &Invalid);
01472   if (Invalid)
01473     return PresumedLoc();
01474   unsigned ColNo  = getColumnNumber(LocInfo.first, LocInfo.second, &Invalid);
01475   if (Invalid)
01476     return PresumedLoc();
01477   
01478   SourceLocation IncludeLoc = FI.getIncludeLoc();
01479 
01480   // If we have #line directives in this file, update and overwrite the physical
01481   // location info if appropriate.
01482   if (UseLineDirectives && FI.hasLineDirectives()) {
01483     assert(LineTable && "Can't have linetable entries without a LineTable!");
01484     // See if there is a #line directive before this.  If so, get it.
01485     if (const LineEntry *Entry =
01486           LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second)) {
01487       // If the LineEntry indicates a filename, use it.
01488       if (Entry->FilenameID != -1)
01489         Filename = LineTable->getFilename(Entry->FilenameID);
01490 
01491       // Use the line number specified by the LineEntry.  This line number may
01492       // be multiple lines down from the line entry.  Add the difference in
01493       // physical line numbers from the query point and the line marker to the
01494       // total.
01495       unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset);
01496       LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1);
01497 
01498       // Note that column numbers are not molested by line markers.
01499 
01500       // Handle virtual #include manipulation.
01501       if (Entry->IncludeOffset) {
01502         IncludeLoc = getLocForStartOfFile(LocInfo.first);
01503         IncludeLoc = IncludeLoc.getLocWithOffset(Entry->IncludeOffset);
01504       }
01505     }
01506   }
01507 
01508   return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc);
01509 }
01510 
01511 /// \brief Returns whether the PresumedLoc for a given SourceLocation is
01512 /// in the main file.
01513 ///
01514 /// This computes the "presumed" location for a SourceLocation, then checks
01515 /// whether it came from a file other than the main file. This is different
01516 /// from isWrittenInMainFile() because it takes line marker directives into
01517 /// account.
01518 bool SourceManager::isInMainFile(SourceLocation Loc) const {
01519   if (Loc.isInvalid()) return false;
01520 
01521   // Presumed locations are always for expansion points.
01522   std::pair<FileID, unsigned> LocInfo = getDecomposedExpansionLoc(Loc);
01523 
01524   bool Invalid = false;
01525   const SLocEntry &Entry = getSLocEntry(LocInfo.first, &Invalid);
01526   if (Invalid || !Entry.isFile())
01527     return false;
01528 
01529   const SrcMgr::FileInfo &FI = Entry.getFile();
01530 
01531   // Check if there is a line directive for this location.
01532   if (FI.hasLineDirectives())
01533     if (const LineEntry *Entry =
01534             LineTable->FindNearestLineEntry(LocInfo.first, LocInfo.second))
01535       if (Entry->IncludeOffset)
01536         return false;
01537 
01538   return FI.getIncludeLoc().isInvalid();
01539 }
01540 
01541 /// \brief The size of the SLocEntry that \p FID represents.
01542 unsigned SourceManager::getFileIDSize(FileID FID) const {
01543   bool Invalid = false;
01544   const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
01545   if (Invalid)
01546     return 0;
01547 
01548   int ID = FID.ID;
01549   unsigned NextOffset;
01550   if ((ID > 0 && unsigned(ID+1) == local_sloc_entry_size()))
01551     NextOffset = getNextLocalOffset();
01552   else if (ID+1 == -1)
01553     NextOffset = MaxLoadedOffset;
01554   else
01555     NextOffset = getSLocEntry(FileID::get(ID+1)).getOffset();
01556 
01557   return NextOffset - Entry.getOffset() - 1;
01558 }
01559 
01560 //===----------------------------------------------------------------------===//
01561 // Other miscellaneous methods.
01562 //===----------------------------------------------------------------------===//
01563 
01564 /// \brief Retrieve the inode for the given file entry, if possible.
01565 ///
01566 /// This routine involves a system call, and therefore should only be used
01567 /// in non-performance-critical code.
01568 static Optional<llvm::sys::fs::UniqueID>
01569 getActualFileUID(const FileEntry *File) {
01570   if (!File)
01571     return None;
01572 
01573   llvm::sys::fs::UniqueID ID;
01574   if (llvm::sys::fs::getUniqueID(File->getName(), ID))
01575     return None;
01576 
01577   return ID;
01578 }
01579 
01580 /// \brief Get the source location for the given file:line:col triplet.
01581 ///
01582 /// If the source file is included multiple times, the source location will
01583 /// be based upon an arbitrary inclusion.
01584 SourceLocation SourceManager::translateFileLineCol(const FileEntry *SourceFile,
01585                                                   unsigned Line,
01586                                                   unsigned Col) const {
01587   assert(SourceFile && "Null source file!");
01588   assert(Line && Col && "Line and column should start from 1!");
01589 
01590   FileID FirstFID = translateFile(SourceFile);
01591   return translateLineCol(FirstFID, Line, Col);
01592 }
01593 
01594 /// \brief Get the FileID for the given file.
01595 ///
01596 /// If the source file is included multiple times, the FileID will be the
01597 /// first inclusion.
01598 FileID SourceManager::translateFile(const FileEntry *SourceFile) const {
01599   assert(SourceFile && "Null source file!");
01600 
01601   // Find the first file ID that corresponds to the given file.
01602   FileID FirstFID;
01603 
01604   // First, check the main file ID, since it is common to look for a
01605   // location in the main file.
01606   Optional<llvm::sys::fs::UniqueID> SourceFileUID;
01607   Optional<StringRef> SourceFileName;
01608   if (!MainFileID.isInvalid()) {
01609     bool Invalid = false;
01610     const SLocEntry &MainSLoc = getSLocEntry(MainFileID, &Invalid);
01611     if (Invalid)
01612       return FileID();
01613     
01614     if (MainSLoc.isFile()) {
01615       const ContentCache *MainContentCache
01616         = MainSLoc.getFile().getContentCache();
01617       if (!MainContentCache) {
01618         // Can't do anything
01619       } else if (MainContentCache->OrigEntry == SourceFile) {
01620         FirstFID = MainFileID;
01621       } else {
01622         // Fall back: check whether we have the same base name and inode
01623         // as the main file.
01624         const FileEntry *MainFile = MainContentCache->OrigEntry;
01625         SourceFileName = llvm::sys::path::filename(SourceFile->getName());
01626         if (*SourceFileName == llvm::sys::path::filename(MainFile->getName())) {
01627           SourceFileUID = getActualFileUID(SourceFile);
01628           if (SourceFileUID) {
01629             if (Optional<llvm::sys::fs::UniqueID> MainFileUID =
01630                     getActualFileUID(MainFile)) {
01631               if (*SourceFileUID == *MainFileUID) {
01632                 FirstFID = MainFileID;
01633                 SourceFile = MainFile;
01634               }
01635             }
01636           }
01637         }
01638       }
01639     }
01640   }
01641 
01642   if (FirstFID.isInvalid()) {
01643     // The location we're looking for isn't in the main file; look
01644     // through all of the local source locations.
01645     for (unsigned I = 0, N = local_sloc_entry_size(); I != N; ++I) {
01646       bool Invalid = false;
01647       const SLocEntry &SLoc = getLocalSLocEntry(I, &Invalid);
01648       if (Invalid)
01649         return FileID();
01650       
01651       if (SLoc.isFile() && 
01652           SLoc.getFile().getContentCache() &&
01653           SLoc.getFile().getContentCache()->OrigEntry == SourceFile) {
01654         FirstFID = FileID::get(I);
01655         break;
01656       }
01657     }
01658     // If that still didn't help, try the modules.
01659     if (FirstFID.isInvalid()) {
01660       for (unsigned I = 0, N = loaded_sloc_entry_size(); I != N; ++I) {
01661         const SLocEntry &SLoc = getLoadedSLocEntry(I);
01662         if (SLoc.isFile() && 
01663             SLoc.getFile().getContentCache() &&
01664             SLoc.getFile().getContentCache()->OrigEntry == SourceFile) {
01665           FirstFID = FileID::get(-int(I) - 2);
01666           break;
01667         }
01668       }
01669     }
01670   }
01671 
01672   // If we haven't found what we want yet, try again, but this time stat()
01673   // each of the files in case the files have changed since we originally 
01674   // parsed the file.
01675   if (FirstFID.isInvalid() &&
01676       (SourceFileName ||
01677        (SourceFileName = llvm::sys::path::filename(SourceFile->getName()))) &&
01678       (SourceFileUID || (SourceFileUID = getActualFileUID(SourceFile)))) {
01679     bool Invalid = false;
01680     for (unsigned I = 0, N = local_sloc_entry_size(); I != N; ++I) {
01681       FileID IFileID;
01682       IFileID.ID = I;
01683       const SLocEntry &SLoc = getSLocEntry(IFileID, &Invalid);
01684       if (Invalid)
01685         return FileID();
01686       
01687       if (SLoc.isFile()) { 
01688         const ContentCache *FileContentCache 
01689           = SLoc.getFile().getContentCache();
01690         const FileEntry *Entry = FileContentCache ? FileContentCache->OrigEntry
01691                                                   : nullptr;
01692         if (Entry && 
01693             *SourceFileName == llvm::sys::path::filename(Entry->getName())) {
01694           if (Optional<llvm::sys::fs::UniqueID> EntryUID =
01695                   getActualFileUID(Entry)) {
01696             if (*SourceFileUID == *EntryUID) {
01697               FirstFID = FileID::get(I);
01698               SourceFile = Entry;
01699               break;
01700             }
01701           }
01702         }
01703       }
01704     }      
01705   }
01706   
01707   (void) SourceFile;
01708   return FirstFID;
01709 }
01710 
01711 /// \brief Get the source location in \arg FID for the given line:col.
01712 /// Returns null location if \arg FID is not a file SLocEntry.
01713 SourceLocation SourceManager::translateLineCol(FileID FID,
01714                                                unsigned Line,
01715                                                unsigned Col) const {
01716   // Lines are used as a one-based index into a zero-based array. This assert
01717   // checks for possible buffer underruns.
01718   assert(Line != 0 && "Passed a zero-based line");
01719 
01720   if (FID.isInvalid())
01721     return SourceLocation();
01722 
01723   bool Invalid = false;
01724   const SLocEntry &Entry = getSLocEntry(FID, &Invalid);
01725   if (Invalid)
01726     return SourceLocation();
01727 
01728   if (!Entry.isFile())
01729     return SourceLocation();
01730 
01731   SourceLocation FileLoc = SourceLocation::getFileLoc(Entry.getOffset());
01732 
01733   if (Line == 1 && Col == 1)
01734     return FileLoc;
01735 
01736   ContentCache *Content
01737     = const_cast<ContentCache *>(Entry.getFile().getContentCache());
01738   if (!Content)
01739     return SourceLocation();
01740 
01741   // If this is the first use of line information for this buffer, compute the
01742   // SourceLineCache for it on demand.
01743   if (!Content->SourceLineCache) {
01744     bool MyInvalid = false;
01745     ComputeLineNumbers(Diag, Content, ContentCacheAlloc, *this, MyInvalid);
01746     if (MyInvalid)
01747       return SourceLocation();
01748   }
01749 
01750   if (Line > Content->NumLines) {
01751     unsigned Size = Content->getBuffer(Diag, *this)->getBufferSize();
01752     if (Size > 0)
01753       --Size;
01754     return FileLoc.getLocWithOffset(Size);
01755   }
01756 
01757   llvm::MemoryBuffer *Buffer = Content->getBuffer(Diag, *this);
01758   unsigned FilePos = Content->SourceLineCache[Line - 1];
01759   const char *Buf = Buffer->getBufferStart() + FilePos;
01760   unsigned BufLength = Buffer->getBufferSize() - FilePos;
01761   if (BufLength == 0)
01762     return FileLoc.getLocWithOffset(FilePos);
01763 
01764   unsigned i = 0;
01765 
01766   // Check that the given column is valid.
01767   while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r')
01768     ++i;
01769   return FileLoc.getLocWithOffset(FilePos + i);
01770 }
01771 
01772 /// \brief Compute a map of macro argument chunks to their expanded source
01773 /// location. Chunks that are not part of a macro argument will map to an
01774 /// invalid source location. e.g. if a file contains one macro argument at
01775 /// offset 100 with length 10, this is how the map will be formed:
01776 ///     0   -> SourceLocation()
01777 ///     100 -> Expanded macro arg location
01778 ///     110 -> SourceLocation()
01779 void SourceManager::computeMacroArgsCache(MacroArgsMap *&CachePtr,
01780                                           FileID FID) const {
01781   assert(!FID.isInvalid());
01782   assert(!CachePtr);
01783 
01784   CachePtr = new MacroArgsMap();
01785   MacroArgsMap &MacroArgsCache = *CachePtr;
01786   // Initially no macro argument chunk is present.
01787   MacroArgsCache.insert(std::make_pair(0, SourceLocation()));
01788 
01789   int ID = FID.ID;
01790   while (1) {
01791     ++ID;
01792     // Stop if there are no more FileIDs to check.
01793     if (ID > 0) {
01794       if (unsigned(ID) >= local_sloc_entry_size())
01795         return;
01796     } else if (ID == -1) {
01797       return;
01798     }
01799 
01800     bool Invalid = false;
01801     const SrcMgr::SLocEntry &Entry = getSLocEntryByID(ID, &Invalid);
01802     if (Invalid)
01803       return;
01804     if (Entry.isFile()) {
01805       SourceLocation IncludeLoc = Entry.getFile().getIncludeLoc();
01806       if (IncludeLoc.isInvalid())
01807         continue;
01808       if (!isInFileID(IncludeLoc, FID))
01809         return; // No more files/macros that may be "contained" in this file.
01810 
01811       // Skip the files/macros of the #include'd file, we only care about macros
01812       // that lexed macro arguments from our file.
01813       if (Entry.getFile().NumCreatedFIDs)
01814         ID += Entry.getFile().NumCreatedFIDs - 1/*because of next ++ID*/;
01815       continue;
01816     }
01817 
01818     const ExpansionInfo &ExpInfo = Entry.getExpansion();
01819 
01820     if (ExpInfo.getExpansionLocStart().isFileID()) {
01821       if (!isInFileID(ExpInfo.getExpansionLocStart(), FID))
01822         return; // No more files/macros that may be "contained" in this file.
01823     }
01824 
01825     if (!ExpInfo.isMacroArgExpansion())
01826       continue;
01827 
01828     associateFileChunkWithMacroArgExp(MacroArgsCache, FID,
01829                                  ExpInfo.getSpellingLoc(),
01830                                  SourceLocation::getMacroLoc(Entry.getOffset()),
01831                                  getFileIDSize(FileID::get(ID)));
01832   }
01833 }
01834 
01835 void SourceManager::associateFileChunkWithMacroArgExp(
01836                                          MacroArgsMap &MacroArgsCache,
01837                                          FileID FID,
01838                                          SourceLocation SpellLoc,
01839                                          SourceLocation ExpansionLoc,
01840                                          unsigned ExpansionLength) const {
01841   if (!SpellLoc.isFileID()) {
01842     unsigned SpellBeginOffs = SpellLoc.getOffset();
01843     unsigned SpellEndOffs = SpellBeginOffs + ExpansionLength;
01844 
01845     // The spelling range for this macro argument expansion can span multiple
01846     // consecutive FileID entries. Go through each entry contained in the
01847     // spelling range and if one is itself a macro argument expansion, recurse
01848     // and associate the file chunk that it represents.
01849 
01850     FileID SpellFID; // Current FileID in the spelling range.
01851     unsigned SpellRelativeOffs;
01852     std::tie(SpellFID, SpellRelativeOffs) = getDecomposedLoc(SpellLoc);
01853     while (1) {
01854       const SLocEntry &Entry = getSLocEntry(SpellFID);
01855       unsigned SpellFIDBeginOffs = Entry.getOffset();
01856       unsigned SpellFIDSize = getFileIDSize(SpellFID);
01857       unsigned SpellFIDEndOffs = SpellFIDBeginOffs + SpellFIDSize;
01858       const ExpansionInfo &Info = Entry.getExpansion();
01859       if (Info.isMacroArgExpansion()) {
01860         unsigned CurrSpellLength;
01861         if (SpellFIDEndOffs < SpellEndOffs)
01862           CurrSpellLength = SpellFIDSize - SpellRelativeOffs;
01863         else
01864           CurrSpellLength = ExpansionLength;
01865         associateFileChunkWithMacroArgExp(MacroArgsCache, FID,
01866                       Info.getSpellingLoc().getLocWithOffset(SpellRelativeOffs),
01867                       ExpansionLoc, CurrSpellLength);
01868       }
01869 
01870       if (SpellFIDEndOffs >= SpellEndOffs)
01871         return; // we covered all FileID entries in the spelling range.
01872 
01873       // Move to the next FileID entry in the spelling range.
01874       unsigned advance = SpellFIDSize - SpellRelativeOffs + 1;
01875       ExpansionLoc = ExpansionLoc.getLocWithOffset(advance);
01876       ExpansionLength -= advance;
01877       ++SpellFID.ID;
01878       SpellRelativeOffs = 0;
01879     }
01880 
01881   }
01882 
01883   assert(SpellLoc.isFileID());
01884 
01885   unsigned BeginOffs;
01886   if (!isInFileID(SpellLoc, FID, &BeginOffs))
01887     return;
01888 
01889   unsigned EndOffs = BeginOffs + ExpansionLength;
01890 
01891   // Add a new chunk for this macro argument. A previous macro argument chunk
01892   // may have been lexed again, so e.g. if the map is
01893   //     0   -> SourceLocation()
01894   //     100 -> Expanded loc #1
01895   //     110 -> SourceLocation()
01896   // and we found a new macro FileID that lexed from offet 105 with length 3,
01897   // the new map will be:
01898   //     0   -> SourceLocation()
01899   //     100 -> Expanded loc #1
01900   //     105 -> Expanded loc #2
01901   //     108 -> Expanded loc #1
01902   //     110 -> SourceLocation()
01903   //
01904   // Since re-lexed macro chunks will always be the same size or less of
01905   // previous chunks, we only need to find where the ending of the new macro
01906   // chunk is mapped to and update the map with new begin/end mappings.
01907 
01908   MacroArgsMap::iterator I = MacroArgsCache.upper_bound(EndOffs);
01909   --I;
01910   SourceLocation EndOffsMappedLoc = I->second;
01911   MacroArgsCache[BeginOffs] = ExpansionLoc;
01912   MacroArgsCache[EndOffs] = EndOffsMappedLoc;
01913 }
01914 
01915 /// \brief If \arg Loc points inside a function macro argument, the returned
01916 /// location will be the macro location in which the argument was expanded.
01917 /// If a macro argument is used multiple times, the expanded location will
01918 /// be at the first expansion of the argument.
01919 /// e.g.
01920 ///   MY_MACRO(foo);
01921 ///             ^
01922 /// Passing a file location pointing at 'foo', will yield a macro location
01923 /// where 'foo' was expanded into.
01924 SourceLocation
01925 SourceManager::getMacroArgExpandedLocation(SourceLocation Loc) const {
01926   if (Loc.isInvalid() || !Loc.isFileID())
01927     return Loc;
01928 
01929   FileID FID;
01930   unsigned Offset;
01931   std::tie(FID, Offset) = getDecomposedLoc(Loc);
01932   if (FID.isInvalid())
01933     return Loc;
01934 
01935   MacroArgsMap *&MacroArgsCache = MacroArgsCacheMap[FID];
01936   if (!MacroArgsCache)
01937     computeMacroArgsCache(MacroArgsCache, FID);
01938 
01939   assert(!MacroArgsCache->empty());
01940   MacroArgsMap::iterator I = MacroArgsCache->upper_bound(Offset);
01941   --I;
01942   
01943   unsigned MacroArgBeginOffs = I->first;
01944   SourceLocation MacroArgExpandedLoc = I->second;
01945   if (MacroArgExpandedLoc.isValid())
01946     return MacroArgExpandedLoc.getLocWithOffset(Offset - MacroArgBeginOffs);
01947 
01948   return Loc;
01949 }
01950 
01951 std::pair<FileID, unsigned>
01952 SourceManager::getDecomposedIncludedLoc(FileID FID) const {
01953   if (FID.isInvalid())
01954     return std::make_pair(FileID(), 0);
01955 
01956   // Uses IncludedLocMap to retrieve/cache the decomposed loc.
01957 
01958   typedef std::pair<FileID, unsigned> DecompTy;
01959   typedef llvm::DenseMap<FileID, DecompTy> MapTy;
01960   std::pair<MapTy::iterator, bool>
01961     InsertOp = IncludedLocMap.insert(std::make_pair(FID, DecompTy()));
01962   DecompTy &DecompLoc = InsertOp.first->second;
01963   if (!InsertOp.second)
01964     return DecompLoc; // already in map.
01965 
01966   SourceLocation UpperLoc;
01967   bool Invalid = false;
01968   const SrcMgr::SLocEntry &Entry = getSLocEntry(FID, &Invalid);
01969   if (!Invalid) {
01970     if (Entry.isExpansion())
01971       UpperLoc = Entry.getExpansion().getExpansionLocStart();
01972     else
01973       UpperLoc = Entry.getFile().getIncludeLoc();
01974   }
01975 
01976   if (UpperLoc.isValid())
01977     DecompLoc = getDecomposedLoc(UpperLoc);
01978 
01979   return DecompLoc;
01980 }
01981 
01982 /// Given a decomposed source location, move it up the include/expansion stack
01983 /// to the parent source location.  If this is possible, return the decomposed
01984 /// version of the parent in Loc and return false.  If Loc is the top-level
01985 /// entry, return true and don't modify it.
01986 static bool MoveUpIncludeHierarchy(std::pair<FileID, unsigned> &Loc,
01987                                    const SourceManager &SM) {
01988   std::pair<FileID, unsigned> UpperLoc = SM.getDecomposedIncludedLoc(Loc.first);
01989   if (UpperLoc.first.isInvalid())
01990     return true; // We reached the top.
01991 
01992   Loc = UpperLoc;
01993   return false;
01994 }
01995 
01996 /// Return the cache entry for comparing the given file IDs
01997 /// for isBeforeInTranslationUnit.
01998 InBeforeInTUCacheEntry &SourceManager::getInBeforeInTUCache(FileID LFID,
01999                                                             FileID RFID) const {
02000   // This is a magic number for limiting the cache size.  It was experimentally
02001   // derived from a small Objective-C project (where the cache filled
02002   // out to ~250 items).  We can make it larger if necessary.
02003   enum { MagicCacheSize = 300 };
02004   IsBeforeInTUCacheKey Key(LFID, RFID);
02005 
02006   // If the cache size isn't too large, do a lookup and if necessary default
02007   // construct an entry.  We can then return it to the caller for direct
02008   // use.  When they update the value, the cache will get automatically
02009   // updated as well.
02010   if (IBTUCache.size() < MagicCacheSize)
02011     return IBTUCache[Key];
02012 
02013   // Otherwise, do a lookup that will not construct a new value.
02014   InBeforeInTUCache::iterator I = IBTUCache.find(Key);
02015   if (I != IBTUCache.end())
02016     return I->second;
02017 
02018   // Fall back to the overflow value.
02019   return IBTUCacheOverflow;
02020 }
02021 
02022 /// \brief Determines the order of 2 source locations in the translation unit.
02023 ///
02024 /// \returns true if LHS source location comes before RHS, false otherwise.
02025 bool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS,
02026                                               SourceLocation RHS) const {
02027   assert(LHS.isValid() && RHS.isValid() && "Passed invalid source location!");
02028   if (LHS == RHS)
02029     return false;
02030 
02031   std::pair<FileID, unsigned> LOffs = getDecomposedLoc(LHS);
02032   std::pair<FileID, unsigned> ROffs = getDecomposedLoc(RHS);
02033 
02034   // getDecomposedLoc may have failed to return a valid FileID because, e.g. it
02035   // is a serialized one referring to a file that was removed after we loaded
02036   // the PCH.
02037   if (LOffs.first.isInvalid() || ROffs.first.isInvalid())
02038     return LOffs.first.isInvalid() && !ROffs.first.isInvalid();
02039 
02040   // If the source locations are in the same file, just compare offsets.
02041   if (LOffs.first == ROffs.first)
02042     return LOffs.second < ROffs.second;
02043 
02044   // If we are comparing a source location with multiple locations in the same
02045   // file, we get a big win by caching the result.
02046   InBeforeInTUCacheEntry &IsBeforeInTUCache =
02047     getInBeforeInTUCache(LOffs.first, ROffs.first);
02048 
02049   // If we are comparing a source location with multiple locations in the same
02050   // file, we get a big win by caching the result.
02051   if (IsBeforeInTUCache.isCacheValid(LOffs.first, ROffs.first))
02052     return IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second);
02053 
02054   // Okay, we missed in the cache, start updating the cache for this query.
02055   IsBeforeInTUCache.setQueryFIDs(LOffs.first, ROffs.first,
02056                           /*isLFIDBeforeRFID=*/LOffs.first.ID < ROffs.first.ID);
02057 
02058   // We need to find the common ancestor. The only way of doing this is to
02059   // build the complete include chain for one and then walking up the chain
02060   // of the other looking for a match.
02061   // We use a map from FileID to Offset to store the chain. Easier than writing
02062   // a custom set hash info that only depends on the first part of a pair.
02063   typedef llvm::SmallDenseMap<FileID, unsigned, 16> LocSet;
02064   LocSet LChain;
02065   do {
02066     LChain.insert(LOffs);
02067     // We catch the case where LOffs is in a file included by ROffs and
02068     // quit early. The other way round unfortunately remains suboptimal.
02069   } while (LOffs.first != ROffs.first && !MoveUpIncludeHierarchy(LOffs, *this));
02070   LocSet::iterator I;
02071   while((I = LChain.find(ROffs.first)) == LChain.end()) {
02072     if (MoveUpIncludeHierarchy(ROffs, *this))
02073       break; // Met at topmost file.
02074   }
02075   if (I != LChain.end())
02076     LOffs = *I;
02077 
02078   // If we exited because we found a nearest common ancestor, compare the
02079   // locations within the common file and cache them.
02080   if (LOffs.first == ROffs.first) {
02081     IsBeforeInTUCache.setCommonLoc(LOffs.first, LOffs.second, ROffs.second);
02082     return IsBeforeInTUCache.getCachedResult(LOffs.second, ROffs.second);
02083   }
02084 
02085   // This can happen if a location is in a built-ins buffer.
02086   // But see PR5662.
02087   // Clear the lookup cache, it depends on a common location.
02088   IsBeforeInTUCache.clear();
02089   bool LIsBuiltins = strcmp("<built-in>",
02090                             getBuffer(LOffs.first)->getBufferIdentifier()) == 0;
02091   bool RIsBuiltins = strcmp("<built-in>",
02092                             getBuffer(ROffs.first)->getBufferIdentifier()) == 0;
02093   // built-in is before non-built-in
02094   if (LIsBuiltins != RIsBuiltins)
02095     return LIsBuiltins;
02096   assert(LIsBuiltins && RIsBuiltins &&
02097          "Non-built-in locations must be rooted in the main file");
02098   // Both are in built-in buffers, but from different files. We just claim that
02099   // lower IDs come first.
02100   return LOffs.first < ROffs.first;
02101 }
02102 
02103 void SourceManager::PrintStats() const {
02104   llvm::errs() << "\n*** Source Manager Stats:\n";
02105   llvm::errs() << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
02106                << " mem buffers mapped.\n";
02107   llvm::errs() << LocalSLocEntryTable.size() << " local SLocEntry's allocated ("
02108                << llvm::capacity_in_bytes(LocalSLocEntryTable)
02109                << " bytes of capacity), "
02110                << NextLocalOffset << "B of Sloc address space used.\n";
02111   llvm::errs() << LoadedSLocEntryTable.size()
02112                << " loaded SLocEntries allocated, "
02113                << MaxLoadedOffset - CurrentLoadedOffset
02114                << "B of Sloc address space used.\n";
02115   
02116   unsigned NumLineNumsComputed = 0;
02117   unsigned NumFileBytesMapped = 0;
02118   for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){
02119     NumLineNumsComputed += I->second->SourceLineCache != nullptr;
02120     NumFileBytesMapped  += I->second->getSizeBytesMapped();
02121   }
02122   unsigned NumMacroArgsComputed = MacroArgsCacheMap.size();
02123 
02124   llvm::errs() << NumFileBytesMapped << " bytes of files mapped, "
02125                << NumLineNumsComputed << " files with line #'s computed, "
02126                << NumMacroArgsComputed << " files with macro args computed.\n";
02127   llvm::errs() << "FileID scans: " << NumLinearScans << " linear, "
02128                << NumBinaryProbes << " binary.\n";
02129 }
02130 
02131 ExternalSLocEntrySource::~ExternalSLocEntrySource() { }
02132 
02133 /// Return the amount of memory used by memory buffers, breaking down
02134 /// by heap-backed versus mmap'ed memory.
02135 SourceManager::MemoryBufferSizes SourceManager::getMemoryBufferSizes() const {
02136   size_t malloc_bytes = 0;
02137   size_t mmap_bytes = 0;
02138   
02139   for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i)
02140     if (size_t sized_mapped = MemBufferInfos[i]->getSizeBytesMapped())
02141       switch (MemBufferInfos[i]->getMemoryBufferKind()) {
02142         case llvm::MemoryBuffer::MemoryBuffer_MMap:
02143           mmap_bytes += sized_mapped;
02144           break;
02145         case llvm::MemoryBuffer::MemoryBuffer_Malloc:
02146           malloc_bytes += sized_mapped;
02147           break;
02148       }
02149   
02150   return MemoryBufferSizes(malloc_bytes, mmap_bytes);
02151 }
02152 
02153 size_t SourceManager::getDataStructureSizes() const {
02154   size_t size = llvm::capacity_in_bytes(MemBufferInfos)
02155     + llvm::capacity_in_bytes(LocalSLocEntryTable)
02156     + llvm::capacity_in_bytes(LoadedSLocEntryTable)
02157     + llvm::capacity_in_bytes(SLocEntryLoaded)
02158     + llvm::capacity_in_bytes(FileInfos);
02159   
02160   if (OverriddenFilesInfo)
02161     size += llvm::capacity_in_bytes(OverriddenFilesInfo->OverriddenFiles);
02162 
02163   return size;
02164 }