SourceManager.cpp revision 206084
1193326Sed//===--- SourceManager.cpp - Track and cache source files -----------------===// 2193326Sed// 3193326Sed// The LLVM Compiler Infrastructure 4193326Sed// 5193326Sed// This file is distributed under the University of Illinois Open Source 6193326Sed// License. See LICENSE.TXT for details. 7193326Sed// 8193326Sed//===----------------------------------------------------------------------===// 9193326Sed// 10193326Sed// This file implements the SourceManager interface. 11193326Sed// 12193326Sed//===----------------------------------------------------------------------===// 13193326Sed 14193326Sed#include "clang/Basic/SourceManager.h" 15193326Sed#include "clang/Basic/SourceManagerInternals.h" 16205219Srdivacky#include "clang/Basic/Diagnostic.h" 17193326Sed#include "clang/Basic/FileManager.h" 18193326Sed#include "llvm/Support/Compiler.h" 19193326Sed#include "llvm/Support/MemoryBuffer.h" 20198092Srdivacky#include "llvm/Support/raw_ostream.h" 21193326Sed#include "llvm/System/Path.h" 22193326Sed#include <algorithm> 23205219Srdivacky#include <string> 24205219Srdivacky#include <cstring> 25205219Srdivacky 26193326Sedusing namespace clang; 27193326Sedusing namespace SrcMgr; 28193326Sedusing llvm::MemoryBuffer; 29193326Sed 30193326Sed//===----------------------------------------------------------------------===// 31193326Sed// SourceManager Helper Classes 32193326Sed//===----------------------------------------------------------------------===// 33193326Sed 34193326SedContentCache::~ContentCache() { 35205408Srdivacky delete Buffer.getPointer(); 36193326Sed} 37193326Sed 38193326Sed/// getSizeBytesMapped - Returns the number of bytes actually mapped for 39193326Sed/// this ContentCache. This can be 0 if the MemBuffer was not actually 40193326Sed/// instantiated. 41193326Sedunsigned ContentCache::getSizeBytesMapped() const { 42205408Srdivacky return Buffer.getPointer() ? Buffer.getPointer()->getBufferSize() : 0; 43193326Sed} 44193326Sed 45193326Sed/// getSize - Returns the size of the content encapsulated by this ContentCache. 46193326Sed/// This can be the size of the source file or the size of an arbitrary 47193326Sed/// scratch buffer. If the ContentCache encapsulates a source file, that 48200583Srdivacky/// file is not lazily brought in from disk to satisfy this query. 49193326Sedunsigned ContentCache::getSize() const { 50205408Srdivacky return Buffer.getPointer() ? (unsigned) Buffer.getPointer()->getBufferSize() 51205408Srdivacky : (unsigned) Entry->getSize(); 52193326Sed} 53193326Sed 54200583Srdivackyvoid ContentCache::replaceBuffer(const llvm::MemoryBuffer *B) { 55205408Srdivacky assert(B != Buffer.getPointer()); 56200583Srdivacky 57205408Srdivacky delete Buffer.getPointer(); 58205408Srdivacky Buffer.setPointer(B); 59205408Srdivacky Buffer.setInt(false); 60200583Srdivacky} 61200583Srdivacky 62205219Srdivackyconst llvm::MemoryBuffer *ContentCache::getBuffer(Diagnostic &Diag, 63205219Srdivacky bool *Invalid) const { 64205219Srdivacky if (Invalid) 65205219Srdivacky *Invalid = false; 66205219Srdivacky 67193326Sed // Lazily create the Buffer for ContentCaches that wrap files. 68205408Srdivacky if (!Buffer.getPointer() && Entry) { 69205219Srdivacky std::string ErrorStr; 70205219Srdivacky struct stat FileInfo; 71205408Srdivacky Buffer.setPointer(MemoryBuffer::getFile(Entry->getName(), &ErrorStr, 72205408Srdivacky Entry->getSize(), &FileInfo)); 73205408Srdivacky Buffer.setInt(false); 74205408Srdivacky 75200583Srdivacky // If we were unable to open the file, then we are in an inconsistent 76200583Srdivacky // situation where the content cache referenced a file which no longer 77200583Srdivacky // exists. Most likely, we were using a stat cache with an invalid entry but 78200583Srdivacky // the file could also have been removed during processing. Since we can't 79200583Srdivacky // really deal with this situation, just create an empty buffer. 80200583Srdivacky // 81200583Srdivacky // FIXME: This is definitely not ideal, but our immediate clients can't 82200583Srdivacky // currently handle returning a null entry here. Ideally we should detect 83200583Srdivacky // that we are in an inconsistent situation and error out as quickly as 84200583Srdivacky // possible. 85205408Srdivacky if (!Buffer.getPointer()) { 86200583Srdivacky const llvm::StringRef FillStr("<<<MISSING SOURCE FILE>>>\n"); 87205408Srdivacky Buffer.setPointer(MemoryBuffer::getNewMemBuffer(Entry->getSize(), 88205408Srdivacky "<invalid>")); 89205408Srdivacky char *Ptr = const_cast<char*>(Buffer.getPointer()->getBufferStart()); 90200583Srdivacky for (unsigned i = 0, e = Entry->getSize(); i != e; ++i) 91200583Srdivacky Ptr[i] = FillStr[i % FillStr.size()]; 92206084Srdivacky 93206084Srdivacky if (Diag.isDiagnosticInFlight()) 94206084Srdivacky Diag.SetDelayedDiagnostic(diag::err_cannot_open_file, 95206084Srdivacky Entry->getName(), ErrorStr); 96206084Srdivacky else 97206084Srdivacky Diag.Report(diag::err_cannot_open_file) 98206084Srdivacky << Entry->getName() << ErrorStr; 99206084Srdivacky 100205408Srdivacky Buffer.setInt(true); 101205408Srdivacky } else if (FileInfo.st_size != Entry->getSize() || 102206084Srdivacky FileInfo.st_mtime != Entry->getModificationTime()) { 103205408Srdivacky // Check that the file's size, modification time, and inode are 104205408Srdivacky // the same as in the file entry (which may have come from a 105205408Srdivacky // stat cache). 106206084Srdivacky if (Diag.isDiagnosticInFlight()) 107206084Srdivacky Diag.SetDelayedDiagnostic(diag::err_file_modified, 108206084Srdivacky Entry->getName()); 109206084Srdivacky else 110206084Srdivacky Diag.Report(diag::err_file_modified) << Entry->getName(); 111206084Srdivacky 112205408Srdivacky Buffer.setInt(true); 113198092Srdivacky } 114198092Srdivacky } 115205219Srdivacky 116205408Srdivacky if (Invalid) 117205408Srdivacky *Invalid = Buffer.getInt(); 118205408Srdivacky 119205408Srdivacky return Buffer.getPointer(); 120198092Srdivacky} 121198092Srdivacky 122193326Sedunsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) { 123193326Sed // Look up the filename in the string table, returning the pre-existing value 124193326Sed // if it exists. 125198092Srdivacky llvm::StringMapEntry<unsigned> &Entry = 126193326Sed FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U); 127193326Sed if (Entry.getValue() != ~0U) 128193326Sed return Entry.getValue(); 129198092Srdivacky 130193326Sed // Otherwise, assign this the next available ID. 131193326Sed Entry.setValue(FilenamesByID.size()); 132193326Sed FilenamesByID.push_back(&Entry); 133193326Sed return FilenamesByID.size()-1; 134193326Sed} 135193326Sed 136193326Sed/// AddLineNote - Add a line note to the line table that indicates that there 137193326Sed/// is a #line at the specified FID/Offset location which changes the presumed 138193326Sed/// location to LineNo/FilenameID. 139193326Sedvoid LineTableInfo::AddLineNote(unsigned FID, unsigned Offset, 140193326Sed unsigned LineNo, int FilenameID) { 141193326Sed std::vector<LineEntry> &Entries = LineEntries[FID]; 142198092Srdivacky 143193326Sed assert((Entries.empty() || Entries.back().FileOffset < Offset) && 144193326Sed "Adding line entries out of order!"); 145198092Srdivacky 146193326Sed SrcMgr::CharacteristicKind Kind = SrcMgr::C_User; 147193326Sed unsigned IncludeOffset = 0; 148198092Srdivacky 149193326Sed if (!Entries.empty()) { 150193326Sed // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember 151193326Sed // that we are still in "foo.h". 152193326Sed if (FilenameID == -1) 153193326Sed FilenameID = Entries.back().FilenameID; 154198092Srdivacky 155193326Sed // If we are after a line marker that switched us to system header mode, or 156193326Sed // that set #include information, preserve it. 157193326Sed Kind = Entries.back().FileKind; 158193326Sed IncludeOffset = Entries.back().IncludeOffset; 159193326Sed } 160198092Srdivacky 161193326Sed Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, Kind, 162193326Sed IncludeOffset)); 163193326Sed} 164193326Sed 165193326Sed/// AddLineNote This is the same as the previous version of AddLineNote, but is 166193326Sed/// used for GNU line markers. If EntryExit is 0, then this doesn't change the 167193326Sed/// presumed #include stack. If it is 1, this is a file entry, if it is 2 then 168193326Sed/// this is a file exit. FileKind specifies whether this is a system header or 169193326Sed/// extern C system header. 170193326Sedvoid LineTableInfo::AddLineNote(unsigned FID, unsigned Offset, 171193326Sed unsigned LineNo, int FilenameID, 172193326Sed unsigned EntryExit, 173193326Sed SrcMgr::CharacteristicKind FileKind) { 174193326Sed assert(FilenameID != -1 && "Unspecified filename should use other accessor"); 175198092Srdivacky 176193326Sed std::vector<LineEntry> &Entries = LineEntries[FID]; 177198092Srdivacky 178193326Sed assert((Entries.empty() || Entries.back().FileOffset < Offset) && 179193326Sed "Adding line entries out of order!"); 180193326Sed 181193326Sed unsigned IncludeOffset = 0; 182193326Sed if (EntryExit == 0) { // No #include stack change. 183193326Sed IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset; 184193326Sed } else if (EntryExit == 1) { 185193326Sed IncludeOffset = Offset-1; 186193326Sed } else if (EntryExit == 2) { 187193326Sed assert(!Entries.empty() && Entries.back().IncludeOffset && 188193326Sed "PPDirectives should have caught case when popping empty include stack"); 189198092Srdivacky 190193326Sed // Get the include loc of the last entries' include loc as our include loc. 191193326Sed IncludeOffset = 0; 192193326Sed if (const LineEntry *PrevEntry = 193193326Sed FindNearestLineEntry(FID, Entries.back().IncludeOffset)) 194193326Sed IncludeOffset = PrevEntry->IncludeOffset; 195193326Sed } 196198092Srdivacky 197193326Sed Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind, 198193326Sed IncludeOffset)); 199193326Sed} 200193326Sed 201193326Sed 202193326Sed/// FindNearestLineEntry - Find the line entry nearest to FID that is before 203193326Sed/// it. If there is no line entry before Offset in FID, return null. 204198092Srdivackyconst LineEntry *LineTableInfo::FindNearestLineEntry(unsigned FID, 205193326Sed unsigned Offset) { 206193326Sed const std::vector<LineEntry> &Entries = LineEntries[FID]; 207193326Sed assert(!Entries.empty() && "No #line entries for this FID after all!"); 208193326Sed 209193326Sed // It is very common for the query to be after the last #line, check this 210193326Sed // first. 211193326Sed if (Entries.back().FileOffset <= Offset) 212193326Sed return &Entries.back(); 213193326Sed 214193326Sed // Do a binary search to find the maximal element that is still before Offset. 215193326Sed std::vector<LineEntry>::const_iterator I = 216193326Sed std::upper_bound(Entries.begin(), Entries.end(), Offset); 217193326Sed if (I == Entries.begin()) return 0; 218193326Sed return &*--I; 219193326Sed} 220193326Sed 221193326Sed/// \brief Add a new line entry that has already been encoded into 222193326Sed/// the internal representation of the line table. 223198092Srdivackyvoid LineTableInfo::AddEntry(unsigned FID, 224193326Sed const std::vector<LineEntry> &Entries) { 225193326Sed LineEntries[FID] = Entries; 226193326Sed} 227193326Sed 228193326Sed/// getLineTableFilenameID - Return the uniqued ID for the specified filename. 229198092Srdivacky/// 230193326Sedunsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) { 231193326Sed if (LineTable == 0) 232193326Sed LineTable = new LineTableInfo(); 233193326Sed return LineTable->getLineTableFilenameID(Ptr, Len); 234193326Sed} 235193326Sed 236193326Sed 237193326Sed/// AddLineNote - Add a line note to the line table for the FileID and offset 238193326Sed/// specified by Loc. If FilenameID is -1, it is considered to be 239193326Sed/// unspecified. 240193326Sedvoid SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo, 241193326Sed int FilenameID) { 242193326Sed std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); 243198092Srdivacky 244193326Sed const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile(); 245193326Sed 246193326Sed // Remember that this file has #line directives now if it doesn't already. 247193326Sed const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives(); 248198092Srdivacky 249193326Sed if (LineTable == 0) 250193326Sed LineTable = new LineTableInfo(); 251193326Sed LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID); 252193326Sed} 253193326Sed 254193326Sed/// AddLineNote - Add a GNU line marker to the line table. 255193326Sedvoid SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo, 256193326Sed int FilenameID, bool IsFileEntry, 257193326Sed bool IsFileExit, bool IsSystemHeader, 258193326Sed bool IsExternCHeader) { 259193326Sed // If there is no filename and no flags, this is treated just like a #line, 260193326Sed // which does not change the flags of the previous line marker. 261193326Sed if (FilenameID == -1) { 262193326Sed assert(!IsFileEntry && !IsFileExit && !IsSystemHeader && !IsExternCHeader && 263193326Sed "Can't set flags without setting the filename!"); 264193326Sed return AddLineNote(Loc, LineNo, FilenameID); 265193326Sed } 266198092Srdivacky 267193326Sed std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); 268193326Sed const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile(); 269198092Srdivacky 270193326Sed // Remember that this file has #line directives now if it doesn't already. 271193326Sed const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives(); 272198092Srdivacky 273193326Sed if (LineTable == 0) 274193326Sed LineTable = new LineTableInfo(); 275198092Srdivacky 276193326Sed SrcMgr::CharacteristicKind FileKind; 277193326Sed if (IsExternCHeader) 278193326Sed FileKind = SrcMgr::C_ExternCSystem; 279193326Sed else if (IsSystemHeader) 280193326Sed FileKind = SrcMgr::C_System; 281193326Sed else 282193326Sed FileKind = SrcMgr::C_User; 283198092Srdivacky 284193326Sed unsigned EntryExit = 0; 285193326Sed if (IsFileEntry) 286193326Sed EntryExit = 1; 287193326Sed else if (IsFileExit) 288193326Sed EntryExit = 2; 289198092Srdivacky 290193326Sed LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID, 291193326Sed EntryExit, FileKind); 292193326Sed} 293193326Sed 294193326SedLineTableInfo &SourceManager::getLineTable() { 295193326Sed if (LineTable == 0) 296193326Sed LineTable = new LineTableInfo(); 297193326Sed return *LineTable; 298193326Sed} 299193326Sed 300193326Sed//===----------------------------------------------------------------------===// 301193326Sed// Private 'Create' methods. 302193326Sed//===----------------------------------------------------------------------===// 303193326Sed 304193326SedSourceManager::~SourceManager() { 305193326Sed delete LineTable; 306198092Srdivacky 307193326Sed // Delete FileEntry objects corresponding to content caches. Since the actual 308193326Sed // content cache objects are bump pointer allocated, we just have to run the 309193326Sed // dtors, but we call the deallocate method for completeness. 310193326Sed for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) { 311193326Sed MemBufferInfos[i]->~ContentCache(); 312193326Sed ContentCacheAlloc.Deallocate(MemBufferInfos[i]); 313193326Sed } 314193326Sed for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator 315193326Sed I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) { 316193326Sed I->second->~ContentCache(); 317193326Sed ContentCacheAlloc.Deallocate(I->second); 318193326Sed } 319193326Sed} 320193326Sed 321193326Sedvoid SourceManager::clearIDTables() { 322193326Sed MainFileID = FileID(); 323193326Sed SLocEntryTable.clear(); 324193326Sed LastLineNoFileIDQuery = FileID(); 325193326Sed LastLineNoContentCache = 0; 326193326Sed LastFileIDLookup = FileID(); 327198092Srdivacky 328193326Sed if (LineTable) 329193326Sed LineTable->clear(); 330198092Srdivacky 331193326Sed // Use up FileID #0 as an invalid instantiation. 332193326Sed NextOffset = 0; 333193326Sed createInstantiationLoc(SourceLocation(),SourceLocation(),SourceLocation(), 1); 334193326Sed} 335193326Sed 336193326Sed/// getOrCreateContentCache - Create or return a cached ContentCache for the 337193326Sed/// specified file. 338193326Sedconst ContentCache * 339193326SedSourceManager::getOrCreateContentCache(const FileEntry *FileEnt) { 340193326Sed assert(FileEnt && "Didn't specify a file entry to use?"); 341198092Srdivacky 342193326Sed // Do we already have information about this file? 343193326Sed ContentCache *&Entry = FileInfos[FileEnt]; 344193326Sed if (Entry) return Entry; 345198092Srdivacky 346193326Sed // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned 347193326Sed // so that FileInfo can use the low 3 bits of the pointer for its own 348193326Sed // nefarious purposes. 349193326Sed unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment; 350193326Sed EntryAlign = std::max(8U, EntryAlign); 351193326Sed Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign); 352193326Sed new (Entry) ContentCache(FileEnt); 353193326Sed return Entry; 354193326Sed} 355193326Sed 356193326Sed 357193326Sed/// createMemBufferContentCache - Create a new ContentCache for the specified 358193326Sed/// memory buffer. This does no caching. 359193326Sedconst ContentCache* 360193326SedSourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) { 361193326Sed // Add a new ContentCache to the MemBufferInfos list and return it. Make sure 362193326Sed // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of 363193326Sed // the pointer for its own nefarious purposes. 364193326Sed unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment; 365193326Sed EntryAlign = std::max(8U, EntryAlign); 366193326Sed ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign); 367193326Sed new (Entry) ContentCache(); 368193326Sed MemBufferInfos.push_back(Entry); 369193326Sed Entry->setBuffer(Buffer); 370193326Sed return Entry; 371193326Sed} 372193326Sed 373193326Sedvoid SourceManager::PreallocateSLocEntries(ExternalSLocEntrySource *Source, 374193326Sed unsigned NumSLocEntries, 375193326Sed unsigned NextOffset) { 376193326Sed ExternalSLocEntries = Source; 377193326Sed this->NextOffset = NextOffset; 378193326Sed SLocEntryLoaded.resize(NumSLocEntries + 1); 379193326Sed SLocEntryLoaded[0] = true; 380193326Sed SLocEntryTable.resize(SLocEntryTable.size() + NumSLocEntries); 381193326Sed} 382193326Sed 383193326Sedvoid SourceManager::ClearPreallocatedSLocEntries() { 384193326Sed unsigned I = 0; 385193326Sed for (unsigned N = SLocEntryLoaded.size(); I != N; ++I) 386193326Sed if (!SLocEntryLoaded[I]) 387193326Sed break; 388193326Sed 389193326Sed // We've already loaded all preallocated source location entries. 390193326Sed if (I == SLocEntryLoaded.size()) 391193326Sed return; 392193326Sed 393193326Sed // Remove everything from location I onward. 394193326Sed SLocEntryTable.resize(I); 395193326Sed SLocEntryLoaded.clear(); 396193326Sed ExternalSLocEntries = 0; 397193326Sed} 398193326Sed 399193326Sed 400193326Sed//===----------------------------------------------------------------------===// 401193326Sed// Methods to create new FileID's and instantiations. 402193326Sed//===----------------------------------------------------------------------===// 403193326Sed 404193326Sed/// createFileID - Create a new fileID for the specified ContentCache and 405193326Sed/// include position. This works regardless of whether the ContentCache 406193326Sed/// corresponds to a file or some other input source. 407193326SedFileID SourceManager::createFileID(const ContentCache *File, 408193326Sed SourceLocation IncludePos, 409193326Sed SrcMgr::CharacteristicKind FileCharacter, 410193326Sed unsigned PreallocatedID, 411193326Sed unsigned Offset) { 412193326Sed if (PreallocatedID) { 413193326Sed // If we're filling in a preallocated ID, just load in the file 414193326Sed // entry and return. 415198092Srdivacky assert(PreallocatedID < SLocEntryLoaded.size() && 416193326Sed "Preallocate ID out-of-range"); 417198092Srdivacky assert(!SLocEntryLoaded[PreallocatedID] && 418193326Sed "Source location entry already loaded"); 419193326Sed assert(Offset && "Preallocate source location cannot have zero offset"); 420198092Srdivacky SLocEntryTable[PreallocatedID] 421193326Sed = SLocEntry::get(Offset, FileInfo::get(IncludePos, File, FileCharacter)); 422193326Sed SLocEntryLoaded[PreallocatedID] = true; 423194613Sed FileID FID = FileID::get(PreallocatedID); 424205408Srdivacky return FID; 425193326Sed } 426193326Sed 427198092Srdivacky SLocEntryTable.push_back(SLocEntry::get(NextOffset, 428193326Sed FileInfo::get(IncludePos, File, 429193326Sed FileCharacter))); 430193326Sed unsigned FileSize = File->getSize(); 431193326Sed assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!"); 432193326Sed NextOffset += FileSize+1; 433198092Srdivacky 434193326Sed // Set LastFileIDLookup to the newly created file. The next getFileID call is 435193326Sed // almost guaranteed to be from that file. 436194711Sed FileID FID = FileID::get(SLocEntryTable.size()-1); 437194711Sed return LastFileIDLookup = FID; 438193326Sed} 439193326Sed 440193326Sed/// createInstantiationLoc - Return a new SourceLocation that encodes the fact 441193326Sed/// that a token from SpellingLoc should actually be referenced from 442193326Sed/// InstantiationLoc. 443193326SedSourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc, 444193326Sed SourceLocation ILocStart, 445193326Sed SourceLocation ILocEnd, 446193326Sed unsigned TokLength, 447193326Sed unsigned PreallocatedID, 448193326Sed unsigned Offset) { 449193326Sed InstantiationInfo II = InstantiationInfo::get(ILocStart,ILocEnd, SpellingLoc); 450193326Sed if (PreallocatedID) { 451193326Sed // If we're filling in a preallocated ID, just load in the 452193326Sed // instantiation entry and return. 453198092Srdivacky assert(PreallocatedID < SLocEntryLoaded.size() && 454193326Sed "Preallocate ID out-of-range"); 455198092Srdivacky assert(!SLocEntryLoaded[PreallocatedID] && 456193326Sed "Source location entry already loaded"); 457193326Sed assert(Offset && "Preallocate source location cannot have zero offset"); 458193326Sed SLocEntryTable[PreallocatedID] = SLocEntry::get(Offset, II); 459193326Sed SLocEntryLoaded[PreallocatedID] = true; 460193326Sed return SourceLocation::getMacroLoc(Offset); 461193326Sed } 462193326Sed SLocEntryTable.push_back(SLocEntry::get(NextOffset, II)); 463193326Sed assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!"); 464193326Sed NextOffset += TokLength+1; 465193326Sed return SourceLocation::getMacroLoc(NextOffset-(TokLength+1)); 466193326Sed} 467193326Sed 468200583Srdivackyconst llvm::MemoryBuffer * 469205219SrdivackySourceManager::getMemoryBufferForFile(const FileEntry *File, 470205219Srdivacky bool *Invalid) { 471200583Srdivacky const SrcMgr::ContentCache *IR = getOrCreateContentCache(File); 472205219Srdivacky assert(IR && "getOrCreateContentCache() cannot return NULL"); 473205219Srdivacky return IR->getBuffer(Diag, Invalid); 474200583Srdivacky} 475200583Srdivacky 476200583Srdivackybool SourceManager::overrideFileContents(const FileEntry *SourceFile, 477200583Srdivacky const llvm::MemoryBuffer *Buffer) { 478200583Srdivacky const SrcMgr::ContentCache *IR = getOrCreateContentCache(SourceFile); 479200583Srdivacky if (IR == 0) 480200583Srdivacky return true; 481200583Srdivacky 482200583Srdivacky const_cast<SrcMgr::ContentCache *>(IR)->replaceBuffer(Buffer); 483200583Srdivacky return false; 484200583Srdivacky} 485200583Srdivacky 486205219Srdivackyllvm::StringRef SourceManager::getBufferData(FileID FID, bool *Invalid) const { 487205408Srdivacky bool MyInvalid = false; 488205408Srdivacky const llvm::MemoryBuffer *Buf = getBuffer(FID, &MyInvalid); 489205219Srdivacky if (Invalid) 490205408Srdivacky *Invalid = MyInvalid; 491205408Srdivacky 492205408Srdivacky if (MyInvalid) 493205408Srdivacky return ""; 494205219Srdivacky 495205219Srdivacky return Buf->getBuffer(); 496193326Sed} 497193326Sed 498193326Sed//===----------------------------------------------------------------------===// 499193326Sed// SourceLocation manipulation methods. 500193326Sed//===----------------------------------------------------------------------===// 501193326Sed 502193326Sed/// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot 503193326Sed/// method that is used for all SourceManager queries that start with a 504193326Sed/// SourceLocation object. It is responsible for finding the entry in 505193326Sed/// SLocEntryTable which contains the specified location. 506193326Sed/// 507193326SedFileID SourceManager::getFileIDSlow(unsigned SLocOffset) const { 508193326Sed assert(SLocOffset && "Invalid FileID"); 509198092Srdivacky 510193326Sed // After the first and second level caches, I see two common sorts of 511193326Sed // behavior: 1) a lot of searched FileID's are "near" the cached file location 512193326Sed // or are "near" the cached instantiation location. 2) others are just 513193326Sed // completely random and may be a very long way away. 514193326Sed // 515193326Sed // To handle this, we do a linear search for up to 8 steps to catch #1 quickly 516193326Sed // then we fall back to a less cache efficient, but more scalable, binary 517193326Sed // search to find the location. 518198092Srdivacky 519193326Sed // See if this is near the file point - worst case we start scanning from the 520193326Sed // most newly created FileID. 521193326Sed std::vector<SrcMgr::SLocEntry>::const_iterator I; 522198092Srdivacky 523193326Sed if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) { 524193326Sed // Neither loc prunes our search. 525193326Sed I = SLocEntryTable.end(); 526193326Sed } else { 527193326Sed // Perhaps it is near the file point. 528193326Sed I = SLocEntryTable.begin()+LastFileIDLookup.ID; 529193326Sed } 530193326Sed 531193326Sed // Find the FileID that contains this. "I" is an iterator that points to a 532193326Sed // FileID whose offset is known to be larger than SLocOffset. 533193326Sed unsigned NumProbes = 0; 534193326Sed while (1) { 535193326Sed --I; 536193326Sed if (ExternalSLocEntries) 537193326Sed getSLocEntry(FileID::get(I - SLocEntryTable.begin())); 538193326Sed if (I->getOffset() <= SLocOffset) { 539193326Sed#if 0 540193326Sed printf("lin %d -> %d [%s] %d %d\n", SLocOffset, 541193326Sed I-SLocEntryTable.begin(), 542193326Sed I->isInstantiation() ? "inst" : "file", 543193326Sed LastFileIDLookup.ID, int(SLocEntryTable.end()-I)); 544193326Sed#endif 545193326Sed FileID Res = FileID::get(I-SLocEntryTable.begin()); 546193326Sed 547193326Sed // If this isn't an instantiation, remember it. We have good locality 548193326Sed // across FileID lookups. 549193326Sed if (!I->isInstantiation()) 550193326Sed LastFileIDLookup = Res; 551193326Sed NumLinearScans += NumProbes+1; 552193326Sed return Res; 553193326Sed } 554193326Sed if (++NumProbes == 8) 555193326Sed break; 556193326Sed } 557198092Srdivacky 558193326Sed // Convert "I" back into an index. We know that it is an entry whose index is 559193326Sed // larger than the offset we are looking for. 560193326Sed unsigned GreaterIndex = I-SLocEntryTable.begin(); 561193326Sed // LessIndex - This is the lower bound of the range that we're searching. 562193326Sed // We know that the offset corresponding to the FileID is is less than 563193326Sed // SLocOffset. 564193326Sed unsigned LessIndex = 0; 565193326Sed NumProbes = 0; 566193326Sed while (1) { 567193326Sed unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex; 568193326Sed unsigned MidOffset = getSLocEntry(FileID::get(MiddleIndex)).getOffset(); 569198092Srdivacky 570193326Sed ++NumProbes; 571198092Srdivacky 572193326Sed // If the offset of the midpoint is too large, chop the high side of the 573193326Sed // range to the midpoint. 574193326Sed if (MidOffset > SLocOffset) { 575193326Sed GreaterIndex = MiddleIndex; 576193326Sed continue; 577193326Sed } 578198092Srdivacky 579193326Sed // If the middle index contains the value, succeed and return. 580193326Sed if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) { 581193326Sed#if 0 582193326Sed printf("bin %d -> %d [%s] %d %d\n", SLocOffset, 583193326Sed I-SLocEntryTable.begin(), 584193326Sed I->isInstantiation() ? "inst" : "file", 585193326Sed LastFileIDLookup.ID, int(SLocEntryTable.end()-I)); 586193326Sed#endif 587193326Sed FileID Res = FileID::get(MiddleIndex); 588193326Sed 589193326Sed // If this isn't an instantiation, remember it. We have good locality 590193326Sed // across FileID lookups. 591193326Sed if (!I->isInstantiation()) 592193326Sed LastFileIDLookup = Res; 593193326Sed NumBinaryProbes += NumProbes; 594193326Sed return Res; 595193326Sed } 596198092Srdivacky 597193326Sed // Otherwise, move the low-side up to the middle index. 598193326Sed LessIndex = MiddleIndex; 599193326Sed } 600193326Sed} 601193326Sed 602193326SedSourceLocation SourceManager:: 603193326SedgetInstantiationLocSlowCase(SourceLocation Loc) const { 604193326Sed do { 605203955Srdivacky // Note: If Loc indicates an offset into a token that came from a macro 606203955Srdivacky // expansion (e.g. the 5th character of the token) we do not want to add 607203955Srdivacky // this offset when going to the instantiation location. The instatiation 608203955Srdivacky // location is the macro invocation, which the offset has nothing to do 609203955Srdivacky // with. This is unlike when we get the spelling loc, because the offset 610203955Srdivacky // directly correspond to the token whose spelling we're inspecting. 611203955Srdivacky Loc = getSLocEntry(getFileID(Loc)).getInstantiation() 612193326Sed .getInstantiationLocStart(); 613193326Sed } while (!Loc.isFileID()); 614193326Sed 615193326Sed return Loc; 616193326Sed} 617193326Sed 618193326SedSourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const { 619193326Sed do { 620193326Sed std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); 621193326Sed Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc(); 622193326Sed Loc = Loc.getFileLocWithOffset(LocInfo.second); 623193326Sed } while (!Loc.isFileID()); 624193326Sed return Loc; 625193326Sed} 626193326Sed 627193326Sed 628193326Sedstd::pair<FileID, unsigned> 629193326SedSourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E, 630193326Sed unsigned Offset) const { 631193326Sed // If this is an instantiation record, walk through all the instantiation 632193326Sed // points. 633193326Sed FileID FID; 634193326Sed SourceLocation Loc; 635193326Sed do { 636193326Sed Loc = E->getInstantiation().getInstantiationLocStart(); 637198092Srdivacky 638193326Sed FID = getFileID(Loc); 639193326Sed E = &getSLocEntry(FID); 640193326Sed Offset += Loc.getOffset()-E->getOffset(); 641193326Sed } while (!Loc.isFileID()); 642198092Srdivacky 643193326Sed return std::make_pair(FID, Offset); 644193326Sed} 645193326Sed 646193326Sedstd::pair<FileID, unsigned> 647193326SedSourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, 648193326Sed unsigned Offset) const { 649193326Sed // If this is an instantiation record, walk through all the instantiation 650193326Sed // points. 651193326Sed FileID FID; 652193326Sed SourceLocation Loc; 653193326Sed do { 654193326Sed Loc = E->getInstantiation().getSpellingLoc(); 655198092Srdivacky 656193326Sed FID = getFileID(Loc); 657193326Sed E = &getSLocEntry(FID); 658193326Sed Offset += Loc.getOffset()-E->getOffset(); 659193326Sed } while (!Loc.isFileID()); 660198092Srdivacky 661193326Sed return std::make_pair(FID, Offset); 662193326Sed} 663193326Sed 664193326Sed/// getImmediateSpellingLoc - Given a SourceLocation object, return the 665193326Sed/// spelling location referenced by the ID. This is the first level down 666193326Sed/// towards the place where the characters that make up the lexed token can be 667193326Sed/// found. This should not generally be used by clients. 668193326SedSourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{ 669193326Sed if (Loc.isFileID()) return Loc; 670193326Sed std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); 671193326Sed Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc(); 672193326Sed return Loc.getFileLocWithOffset(LocInfo.second); 673193326Sed} 674193326Sed 675193326Sed 676193326Sed/// getImmediateInstantiationRange - Loc is required to be an instantiation 677193326Sed/// location. Return the start/end of the instantiation information. 678193326Sedstd::pair<SourceLocation,SourceLocation> 679193326SedSourceManager::getImmediateInstantiationRange(SourceLocation Loc) const { 680193326Sed assert(Loc.isMacroID() && "Not an instantiation loc!"); 681193326Sed const InstantiationInfo &II = getSLocEntry(getFileID(Loc)).getInstantiation(); 682193326Sed return II.getInstantiationLocRange(); 683193326Sed} 684193326Sed 685193326Sed/// getInstantiationRange - Given a SourceLocation object, return the 686193326Sed/// range of tokens covered by the instantiation in the ultimate file. 687193326Sedstd::pair<SourceLocation,SourceLocation> 688193326SedSourceManager::getInstantiationRange(SourceLocation Loc) const { 689193326Sed if (Loc.isFileID()) return std::make_pair(Loc, Loc); 690198092Srdivacky 691193326Sed std::pair<SourceLocation,SourceLocation> Res = 692193326Sed getImmediateInstantiationRange(Loc); 693198092Srdivacky 694193326Sed // Fully resolve the start and end locations to their ultimate instantiation 695193326Sed // points. 696193326Sed while (!Res.first.isFileID()) 697193326Sed Res.first = getImmediateInstantiationRange(Res.first).first; 698193326Sed while (!Res.second.isFileID()) 699193326Sed Res.second = getImmediateInstantiationRange(Res.second).second; 700193326Sed return Res; 701193326Sed} 702193326Sed 703193326Sed 704193326Sed 705193326Sed//===----------------------------------------------------------------------===// 706193326Sed// Queries about the code at a SourceLocation. 707193326Sed//===----------------------------------------------------------------------===// 708193326Sed 709193326Sed/// getCharacterData - Return a pointer to the start of the specified location 710193326Sed/// in the appropriate MemoryBuffer. 711205219Srdivackyconst char *SourceManager::getCharacterData(SourceLocation SL, 712205219Srdivacky bool *Invalid) const { 713193326Sed // Note that this is a hot function in the getSpelling() path, which is 714193326Sed // heavily used by -E mode. 715193326Sed std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL); 716198092Srdivacky 717193326Sed // Note that calling 'getBuffer()' may lazily page in a source file. 718205219Srdivacky bool CharDataInvalid = false; 719205219Srdivacky const llvm::MemoryBuffer *Buffer 720205219Srdivacky = getSLocEntry(LocInfo.first).getFile().getContentCache()->getBuffer(Diag, 721205219Srdivacky &CharDataInvalid); 722205219Srdivacky if (Invalid) 723205219Srdivacky *Invalid = CharDataInvalid; 724205219Srdivacky return Buffer->getBufferStart() + (CharDataInvalid? 0 : LocInfo.second); 725193326Sed} 726193326Sed 727193326Sed 728193326Sed/// getColumnNumber - Return the column # for the specified file position. 729193326Sed/// this is significantly cheaper to compute than the line number. 730205219Srdivackyunsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos, 731205219Srdivacky bool *Invalid) const { 732205219Srdivacky bool MyInvalid = false; 733205219Srdivacky const char *Buf = getBuffer(FID, &MyInvalid)->getBufferStart(); 734205219Srdivacky if (Invalid) 735205219Srdivacky *Invalid = MyInvalid; 736198092Srdivacky 737205219Srdivacky if (MyInvalid) 738205219Srdivacky return 1; 739205219Srdivacky 740193326Sed unsigned LineStart = FilePos; 741193326Sed while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r') 742193326Sed --LineStart; 743193326Sed return FilePos-LineStart+1; 744193326Sed} 745193326Sed 746205219Srdivackyunsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc, 747205219Srdivacky bool *Invalid) const { 748193326Sed if (Loc.isInvalid()) return 0; 749193326Sed std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc); 750205219Srdivacky return getColumnNumber(LocInfo.first, LocInfo.second, Invalid); 751193326Sed} 752193326Sed 753205219Srdivackyunsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc, 754205219Srdivacky bool *Invalid) const { 755193326Sed if (Loc.isInvalid()) return 0; 756193326Sed std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); 757205219Srdivacky return getColumnNumber(LocInfo.first, LocInfo.second, Invalid); 758193326Sed} 759193326Sed 760205219Srdivackystatic DISABLE_INLINE void ComputeLineNumbers(Diagnostic &Diag, 761205219Srdivacky ContentCache* FI, 762205219Srdivacky llvm::BumpPtrAllocator &Alloc, 763205219Srdivacky bool &Invalid); 764205219Srdivackystatic void ComputeLineNumbers(Diagnostic &Diag, ContentCache* FI, 765205219Srdivacky llvm::BumpPtrAllocator &Alloc, bool &Invalid) { 766193326Sed // Note that calling 'getBuffer()' may lazily page in the file. 767205219Srdivacky const MemoryBuffer *Buffer = FI->getBuffer(Diag, &Invalid); 768205219Srdivacky if (Invalid) 769205219Srdivacky return; 770198092Srdivacky 771193326Sed // Find the file offsets of all of the *physical* source lines. This does 772193326Sed // not look at trigraphs, escaped newlines, or anything else tricky. 773193326Sed std::vector<unsigned> LineOffsets; 774198092Srdivacky 775193326Sed // Line #1 starts at char 0. 776193326Sed LineOffsets.push_back(0); 777198092Srdivacky 778193326Sed const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart(); 779193326Sed const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd(); 780193326Sed unsigned Offs = 0; 781193326Sed while (1) { 782193326Sed // Skip over the contents of the line. 783193326Sed // TODO: Vectorize this? This is very performance sensitive for programs 784193326Sed // with lots of diagnostics and in -E mode. 785193326Sed const unsigned char *NextBuf = (const unsigned char *)Buf; 786193326Sed while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0') 787193326Sed ++NextBuf; 788193326Sed Offs += NextBuf-Buf; 789193326Sed Buf = NextBuf; 790198092Srdivacky 791193326Sed if (Buf[0] == '\n' || Buf[0] == '\r') { 792193326Sed // If this is \n\r or \r\n, skip both characters. 793193326Sed if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1]) 794193326Sed ++Offs, ++Buf; 795193326Sed ++Offs, ++Buf; 796193326Sed LineOffsets.push_back(Offs); 797193326Sed } else { 798193326Sed // Otherwise, this is a null. If end of file, exit. 799193326Sed if (Buf == End) break; 800193326Sed // Otherwise, skip the null. 801193326Sed ++Offs, ++Buf; 802193326Sed } 803193326Sed } 804198092Srdivacky 805193326Sed // Copy the offsets into the FileInfo structure. 806193326Sed FI->NumLines = LineOffsets.size(); 807193326Sed FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size()); 808193326Sed std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache); 809193326Sed} 810193326Sed 811193326Sed/// getLineNumber - Given a SourceLocation, return the spelling line number 812193326Sed/// for the position indicated. This requires building and caching a table of 813193326Sed/// line offsets for the MemoryBuffer, so this is not cheap: use only when 814193326Sed/// about to emit a diagnostic. 815205219Srdivackyunsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos, 816205219Srdivacky bool *Invalid) const { 817193326Sed ContentCache *Content; 818193326Sed if (LastLineNoFileIDQuery == FID) 819193326Sed Content = LastLineNoContentCache; 820193326Sed else 821193326Sed Content = const_cast<ContentCache*>(getSLocEntry(FID) 822193326Sed .getFile().getContentCache()); 823198092Srdivacky 824193326Sed // If this is the first use of line information for this buffer, compute the 825193326Sed /// SourceLineCache for it on demand. 826205219Srdivacky if (Content->SourceLineCache == 0) { 827205219Srdivacky bool MyInvalid = false; 828205219Srdivacky ComputeLineNumbers(Diag, Content, ContentCacheAlloc, MyInvalid); 829205219Srdivacky if (Invalid) 830205219Srdivacky *Invalid = MyInvalid; 831205219Srdivacky if (MyInvalid) 832205219Srdivacky return 1; 833205219Srdivacky } else if (Invalid) 834205219Srdivacky *Invalid = false; 835193326Sed 836193326Sed // Okay, we know we have a line number table. Do a binary search to find the 837193326Sed // line number that this character position lands on. 838193326Sed unsigned *SourceLineCache = Content->SourceLineCache; 839193326Sed unsigned *SourceLineCacheStart = SourceLineCache; 840193326Sed unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines; 841198092Srdivacky 842193326Sed unsigned QueriedFilePos = FilePos+1; 843193326Sed 844193326Sed // FIXME: I would like to be convinced that this code is worth being as 845198092Srdivacky // complicated as it is, binary search isn't that slow. 846193326Sed // 847193326Sed // If it is worth being optimized, then in my opinion it could be more 848193326Sed // performant, simpler, and more obviously correct by just "galloping" outward 849193326Sed // from the queried file position. In fact, this could be incorporated into a 850193326Sed // generic algorithm such as lower_bound_with_hint. 851193326Sed // 852193326Sed // If someone gives me a test case where this matters, and I will do it! - DWD 853193326Sed 854193326Sed // If the previous query was to the same file, we know both the file pos from 855193326Sed // that query and the line number returned. This allows us to narrow the 856193326Sed // search space from the entire file to something near the match. 857193326Sed if (LastLineNoFileIDQuery == FID) { 858193326Sed if (QueriedFilePos >= LastLineNoFilePos) { 859193326Sed // FIXME: Potential overflow? 860193326Sed SourceLineCache = SourceLineCache+LastLineNoResult-1; 861198092Srdivacky 862193326Sed // The query is likely to be nearby the previous one. Here we check to 863193326Sed // see if it is within 5, 10 or 20 lines. It can be far away in cases 864193326Sed // where big comment blocks and vertical whitespace eat up lines but 865193326Sed // contribute no tokens. 866193326Sed if (SourceLineCache+5 < SourceLineCacheEnd) { 867193326Sed if (SourceLineCache[5] > QueriedFilePos) 868193326Sed SourceLineCacheEnd = SourceLineCache+5; 869193326Sed else if (SourceLineCache+10 < SourceLineCacheEnd) { 870193326Sed if (SourceLineCache[10] > QueriedFilePos) 871193326Sed SourceLineCacheEnd = SourceLineCache+10; 872193326Sed else if (SourceLineCache+20 < SourceLineCacheEnd) { 873193326Sed if (SourceLineCache[20] > QueriedFilePos) 874193326Sed SourceLineCacheEnd = SourceLineCache+20; 875193326Sed } 876193326Sed } 877193326Sed } 878193326Sed } else { 879193326Sed if (LastLineNoResult < Content->NumLines) 880193326Sed SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1; 881193326Sed } 882193326Sed } 883198092Srdivacky 884193326Sed // If the spread is large, do a "radix" test as our initial guess, based on 885193326Sed // the assumption that lines average to approximately the same length. 886193326Sed // NOTE: This is currently disabled, as it does not appear to be profitable in 887193326Sed // initial measurements. 888193326Sed if (0 && SourceLineCacheEnd-SourceLineCache > 20) { 889193326Sed unsigned FileLen = Content->SourceLineCache[Content->NumLines-1]; 890198092Srdivacky 891193326Sed // Take a stab at guessing where it is. 892193326Sed unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen; 893198092Srdivacky 894193326Sed // Check for -10 and +10 lines. 895193326Sed unsigned LowerBound = std::max(int(ApproxPos-10), 0); 896193326Sed unsigned UpperBound = std::min(ApproxPos+10, FileLen); 897193326Sed 898193326Sed // If the computed lower bound is less than the query location, move it in. 899193326Sed if (SourceLineCache < SourceLineCacheStart+LowerBound && 900193326Sed SourceLineCacheStart[LowerBound] < QueriedFilePos) 901193326Sed SourceLineCache = SourceLineCacheStart+LowerBound; 902198092Srdivacky 903193326Sed // If the computed upper bound is greater than the query location, move it. 904193326Sed if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound && 905193326Sed SourceLineCacheStart[UpperBound] >= QueriedFilePos) 906193326Sed SourceLineCacheEnd = SourceLineCacheStart+UpperBound; 907193326Sed } 908198092Srdivacky 909193326Sed unsigned *Pos 910193326Sed = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos); 911193326Sed unsigned LineNo = Pos-SourceLineCacheStart; 912198092Srdivacky 913193326Sed LastLineNoFileIDQuery = FID; 914193326Sed LastLineNoContentCache = Content; 915193326Sed LastLineNoFilePos = QueriedFilePos; 916193326Sed LastLineNoResult = LineNo; 917193326Sed return LineNo; 918193326Sed} 919193326Sed 920205219Srdivackyunsigned SourceManager::getInstantiationLineNumber(SourceLocation Loc, 921205219Srdivacky bool *Invalid) const { 922193326Sed if (Loc.isInvalid()) return 0; 923193326Sed std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); 924193326Sed return getLineNumber(LocInfo.first, LocInfo.second); 925193326Sed} 926205219Srdivackyunsigned SourceManager::getSpellingLineNumber(SourceLocation Loc, 927205219Srdivacky bool *Invalid) const { 928193326Sed if (Loc.isInvalid()) return 0; 929193326Sed std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc); 930193326Sed return getLineNumber(LocInfo.first, LocInfo.second); 931193326Sed} 932193326Sed 933193326Sed/// getFileCharacteristic - return the file characteristic of the specified 934198092Srdivacky/// source location, indicating whether this is a normal file, a system 935193326Sed/// header, or an "implicit extern C" system header. 936193326Sed/// 937193326Sed/// This state can be modified with flags on GNU linemarker directives like: 938193326Sed/// # 4 "foo.h" 3 939193326Sed/// which changes all source locations in the current file after that to be 940193326Sed/// considered to be from a system header. 941198092SrdivackySrcMgr::CharacteristicKind 942193326SedSourceManager::getFileCharacteristic(SourceLocation Loc) const { 943193326Sed assert(!Loc.isInvalid() && "Can't get file characteristic of invalid loc!"); 944193326Sed std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); 945193326Sed const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile(); 946193326Sed 947193326Sed // If there are no #line directives in this file, just return the whole-file 948193326Sed // state. 949193326Sed if (!FI.hasLineDirectives()) 950193326Sed return FI.getFileCharacteristic(); 951198092Srdivacky 952193326Sed assert(LineTable && "Can't have linetable entries without a LineTable!"); 953193326Sed // See if there is a #line directive before the location. 954193326Sed const LineEntry *Entry = 955193326Sed LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second); 956198092Srdivacky 957193326Sed // If this is before the first line marker, use the file characteristic. 958193326Sed if (!Entry) 959193326Sed return FI.getFileCharacteristic(); 960193326Sed 961193326Sed return Entry->FileKind; 962193326Sed} 963193326Sed 964193326Sed/// Return the filename or buffer identifier of the buffer the location is in. 965193326Sed/// Note that this name does not respect #line directives. Use getPresumedLoc 966193326Sed/// for normal clients. 967205219Srdivackyconst char *SourceManager::getBufferName(SourceLocation Loc, 968205219Srdivacky bool *Invalid) const { 969193326Sed if (Loc.isInvalid()) return "<invalid loc>"; 970198092Srdivacky 971205219Srdivacky return getBuffer(getFileID(Loc), Invalid)->getBufferIdentifier(); 972193326Sed} 973193326Sed 974193326Sed 975193326Sed/// getPresumedLoc - This method returns the "presumed" location of a 976193326Sed/// SourceLocation specifies. A "presumed location" can be modified by #line 977193326Sed/// or GNU line marker directives. This provides a view on the data that a 978193326Sed/// user should see in diagnostics, for example. 979193326Sed/// 980193326Sed/// Note that a presumed location is always given as the instantiation point 981193326Sed/// of an instantiation location, not at the spelling location. 982193326SedPresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const { 983193326Sed if (Loc.isInvalid()) return PresumedLoc(); 984198092Srdivacky 985193326Sed // Presumed locations are always for instantiation points. 986193326Sed std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); 987198092Srdivacky 988193326Sed const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile(); 989193326Sed const SrcMgr::ContentCache *C = FI.getContentCache(); 990198092Srdivacky 991193326Sed // To get the source name, first consult the FileEntry (if one exists) 992193326Sed // before the MemBuffer as this will avoid unnecessarily paging in the 993193326Sed // MemBuffer. 994198092Srdivacky const char *Filename = 995205219Srdivacky C->Entry ? C->Entry->getName() : C->getBuffer(Diag)->getBufferIdentifier(); 996193326Sed unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second); 997193326Sed unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second); 998193326Sed SourceLocation IncludeLoc = FI.getIncludeLoc(); 999198092Srdivacky 1000193326Sed // If we have #line directives in this file, update and overwrite the physical 1001193326Sed // location info if appropriate. 1002193326Sed if (FI.hasLineDirectives()) { 1003193326Sed assert(LineTable && "Can't have linetable entries without a LineTable!"); 1004193326Sed // See if there is a #line directive before this. If so, get it. 1005193326Sed if (const LineEntry *Entry = 1006193326Sed LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second)) { 1007193326Sed // If the LineEntry indicates a filename, use it. 1008193326Sed if (Entry->FilenameID != -1) 1009193326Sed Filename = LineTable->getFilename(Entry->FilenameID); 1010193326Sed 1011193326Sed // Use the line number specified by the LineEntry. This line number may 1012193326Sed // be multiple lines down from the line entry. Add the difference in 1013193326Sed // physical line numbers from the query point and the line marker to the 1014193326Sed // total. 1015193326Sed unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset); 1016193326Sed LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1); 1017198092Srdivacky 1018193326Sed // Note that column numbers are not molested by line markers. 1019198092Srdivacky 1020193326Sed // Handle virtual #include manipulation. 1021193326Sed if (Entry->IncludeOffset) { 1022193326Sed IncludeLoc = getLocForStartOfFile(LocInfo.first); 1023193326Sed IncludeLoc = IncludeLoc.getFileLocWithOffset(Entry->IncludeOffset); 1024193326Sed } 1025193326Sed } 1026193326Sed } 1027193326Sed 1028193326Sed return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc); 1029193326Sed} 1030193326Sed 1031193326Sed//===----------------------------------------------------------------------===// 1032193326Sed// Other miscellaneous methods. 1033193326Sed//===----------------------------------------------------------------------===// 1034193326Sed 1035194613Sed/// \brief Get the source location for the given file:line:col triplet. 1036194613Sed/// 1037194613Sed/// If the source file is included multiple times, the source location will 1038194613Sed/// be based upon the first inclusion. 1039194613SedSourceLocation SourceManager::getLocation(const FileEntry *SourceFile, 1040194613Sed unsigned Line, unsigned Col) const { 1041194613Sed assert(SourceFile && "Null source file!"); 1042194613Sed assert(Line && Col && "Line and column should start from 1!"); 1043193326Sed 1044194613Sed fileinfo_iterator FI = FileInfos.find(SourceFile); 1045194613Sed if (FI == FileInfos.end()) 1046194613Sed return SourceLocation(); 1047194613Sed ContentCache *Content = FI->second; 1048198092Srdivacky 1049194613Sed // If this is the first use of line information for this buffer, compute the 1050194613Sed /// SourceLineCache for it on demand. 1051205219Srdivacky if (Content->SourceLineCache == 0) { 1052205219Srdivacky bool MyInvalid = false; 1053205219Srdivacky ComputeLineNumbers(Diag, Content, ContentCacheAlloc, MyInvalid); 1054205219Srdivacky if (MyInvalid) 1055205219Srdivacky return SourceLocation(); 1056205219Srdivacky } 1057194613Sed 1058200583Srdivacky // Find the first file ID that corresponds to the given file. 1059200583Srdivacky FileID FirstFID; 1060200583Srdivacky 1061200583Srdivacky // First, check the main file ID, since it is common to look for a 1062200583Srdivacky // location in the main file. 1063200583Srdivacky if (!MainFileID.isInvalid()) { 1064200583Srdivacky const SLocEntry &MainSLoc = getSLocEntry(MainFileID); 1065200583Srdivacky if (MainSLoc.isFile() && MainSLoc.getFile().getContentCache() == Content) 1066200583Srdivacky FirstFID = MainFileID; 1067200583Srdivacky } 1068200583Srdivacky 1069200583Srdivacky if (FirstFID.isInvalid()) { 1070200583Srdivacky // The location we're looking for isn't in the main file; look 1071200583Srdivacky // through all of the source locations. 1072200583Srdivacky for (unsigned I = 0, N = sloc_entry_size(); I != N; ++I) { 1073200583Srdivacky const SLocEntry &SLoc = getSLocEntry(I); 1074200583Srdivacky if (SLoc.isFile() && SLoc.getFile().getContentCache() == Content) { 1075200583Srdivacky FirstFID = FileID::get(I); 1076200583Srdivacky break; 1077200583Srdivacky } 1078200583Srdivacky } 1079200583Srdivacky } 1080200583Srdivacky 1081200583Srdivacky if (FirstFID.isInvalid()) 1082200583Srdivacky return SourceLocation(); 1083200583Srdivacky 1084204643Srdivacky if (Line > Content->NumLines) { 1085205219Srdivacky unsigned Size = Content->getBuffer(Diag)->getBufferSize(); 1086204643Srdivacky if (Size > 0) 1087204643Srdivacky --Size; 1088204643Srdivacky return getLocForStartOfFile(FirstFID).getFileLocWithOffset(Size); 1089204643Srdivacky } 1090204643Srdivacky 1091204643Srdivacky unsigned FilePos = Content->SourceLineCache[Line - 1]; 1092205219Srdivacky const char *Buf = Content->getBuffer(Diag)->getBufferStart() + FilePos; 1093205219Srdivacky unsigned BufLength = Content->getBuffer(Diag)->getBufferEnd() - Buf; 1094204643Srdivacky unsigned i = 0; 1095204643Srdivacky 1096204643Srdivacky // Check that the given column is valid. 1097204643Srdivacky while (i < BufLength-1 && i < Col-1 && Buf[i] != '\n' && Buf[i] != '\r') 1098204643Srdivacky ++i; 1099204643Srdivacky if (i < Col-1) 1100204643Srdivacky return getLocForStartOfFile(FirstFID).getFileLocWithOffset(FilePos + i); 1101204643Srdivacky 1102200583Srdivacky return getLocForStartOfFile(FirstFID).getFileLocWithOffset(FilePos + Col - 1); 1103194613Sed} 1104194613Sed 1105195099Sed/// \brief Determines the order of 2 source locations in the translation unit. 1106195099Sed/// 1107195099Sed/// \returns true if LHS source location comes before RHS, false otherwise. 1108195099Sedbool SourceManager::isBeforeInTranslationUnit(SourceLocation LHS, 1109195099Sed SourceLocation RHS) const { 1110195099Sed assert(LHS.isValid() && RHS.isValid() && "Passed invalid source location!"); 1111195099Sed if (LHS == RHS) 1112195099Sed return false; 1113198092Srdivacky 1114195099Sed std::pair<FileID, unsigned> LOffs = getDecomposedLoc(LHS); 1115195099Sed std::pair<FileID, unsigned> ROffs = getDecomposedLoc(RHS); 1116198092Srdivacky 1117195099Sed // If the source locations are in the same file, just compare offsets. 1118195099Sed if (LOffs.first == ROffs.first) 1119195099Sed return LOffs.second < ROffs.second; 1120194613Sed 1121195099Sed // If we are comparing a source location with multiple locations in the same 1122195099Sed // file, we get a big win by caching the result. 1123198092Srdivacky 1124195099Sed if (LastLFIDForBeforeTUCheck == LOffs.first && 1125195099Sed LastRFIDForBeforeTUCheck == ROffs.first) 1126195099Sed return LastResForBeforeTUCheck; 1127198092Srdivacky 1128195099Sed LastLFIDForBeforeTUCheck = LOffs.first; 1129195099Sed LastRFIDForBeforeTUCheck = ROffs.first; 1130198092Srdivacky 1131195099Sed // "Traverse" the include/instantiation stacks of both locations and try to 1132195099Sed // find a common "ancestor". 1133195099Sed // 1134195099Sed // First we traverse the stack of the right location and check each level 1135195099Sed // against the level of the left location, while collecting all levels in a 1136195099Sed // "stack map". 1137195099Sed 1138195099Sed std::map<FileID, unsigned> ROffsMap; 1139195099Sed ROffsMap[ROffs.first] = ROffs.second; 1140195099Sed 1141195099Sed while (1) { 1142195099Sed SourceLocation UpperLoc; 1143195099Sed const SrcMgr::SLocEntry &Entry = getSLocEntry(ROffs.first); 1144195099Sed if (Entry.isInstantiation()) 1145195099Sed UpperLoc = Entry.getInstantiation().getInstantiationLocStart(); 1146195099Sed else 1147195099Sed UpperLoc = Entry.getFile().getIncludeLoc(); 1148198092Srdivacky 1149195099Sed if (UpperLoc.isInvalid()) 1150195099Sed break; // We reached the top. 1151198092Srdivacky 1152195099Sed ROffs = getDecomposedLoc(UpperLoc); 1153198092Srdivacky 1154195099Sed if (LOffs.first == ROffs.first) 1155195099Sed return LastResForBeforeTUCheck = LOffs.second < ROffs.second; 1156198092Srdivacky 1157195099Sed ROffsMap[ROffs.first] = ROffs.second; 1158195099Sed } 1159195099Sed 1160195099Sed // We didn't find a common ancestor. Now traverse the stack of the left 1161195099Sed // location, checking against the stack map of the right location. 1162195099Sed 1163195099Sed while (1) { 1164195099Sed SourceLocation UpperLoc; 1165195099Sed const SrcMgr::SLocEntry &Entry = getSLocEntry(LOffs.first); 1166195099Sed if (Entry.isInstantiation()) 1167195099Sed UpperLoc = Entry.getInstantiation().getInstantiationLocStart(); 1168195099Sed else 1169195099Sed UpperLoc = Entry.getFile().getIncludeLoc(); 1170198092Srdivacky 1171195099Sed if (UpperLoc.isInvalid()) 1172195099Sed break; // We reached the top. 1173198092Srdivacky 1174195099Sed LOffs = getDecomposedLoc(UpperLoc); 1175198092Srdivacky 1176195099Sed std::map<FileID, unsigned>::iterator I = ROffsMap.find(LOffs.first); 1177195099Sed if (I != ROffsMap.end()) 1178195099Sed return LastResForBeforeTUCheck = LOffs.second < I->second; 1179195099Sed } 1180198092Srdivacky 1181200583Srdivacky // There is no common ancestor, most probably because one location is in the 1182200583Srdivacky // predefines buffer. 1183200583Srdivacky // 1184200583Srdivacky // FIXME: We should rearrange the external interface so this simply never 1185200583Srdivacky // happens; it can't conceptually happen. Also see PR5662. 1186198092Srdivacky 1187200583Srdivacky // If exactly one location is a memory buffer, assume it preceeds the other. 1188200583Srdivacky bool LIsMB = !getSLocEntry(LOffs.first).getFile().getContentCache()->Entry; 1189200583Srdivacky bool RIsMB = !getSLocEntry(ROffs.first).getFile().getContentCache()->Entry; 1190200583Srdivacky if (LIsMB != RIsMB) 1191200583Srdivacky return LastResForBeforeTUCheck = LIsMB; 1192198092Srdivacky 1193200583Srdivacky // Otherwise, just assume FileIDs were created in order. 1194200583Srdivacky return LastResForBeforeTUCheck = (LOffs.first < ROffs.first); 1195195099Sed} 1196195099Sed 1197193326Sed/// PrintStats - Print statistics to stderr. 1198193326Sed/// 1199193326Sedvoid SourceManager::PrintStats() const { 1200198092Srdivacky llvm::errs() << "\n*** Source Manager Stats:\n"; 1201198092Srdivacky llvm::errs() << FileInfos.size() << " files mapped, " << MemBufferInfos.size() 1202198092Srdivacky << " mem buffers mapped.\n"; 1203198092Srdivacky llvm::errs() << SLocEntryTable.size() << " SLocEntry's allocated, " 1204198092Srdivacky << NextOffset << "B of Sloc address space used.\n"; 1205198092Srdivacky 1206193326Sed unsigned NumLineNumsComputed = 0; 1207193326Sed unsigned NumFileBytesMapped = 0; 1208193326Sed for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){ 1209193326Sed NumLineNumsComputed += I->second->SourceLineCache != 0; 1210193326Sed NumFileBytesMapped += I->second->getSizeBytesMapped(); 1211193326Sed } 1212198092Srdivacky 1213198092Srdivacky llvm::errs() << NumFileBytesMapped << " bytes of files mapped, " 1214198092Srdivacky << NumLineNumsComputed << " files with line #'s computed.\n"; 1215198092Srdivacky llvm::errs() << "FileID scans: " << NumLinearScans << " linear, " 1216198092Srdivacky << NumBinaryProbes << " binary.\n"; 1217193326Sed} 1218193326Sed 1219193326SedExternalSLocEntrySource::~ExternalSLocEntrySource() { } 1220