SourceManager.cpp revision 7f94b0b0c6791013d2f72ced9b4bedd3b23673a6
1//===--- SourceManager.cpp - Track and cache source files -----------------===// 2// 3// The LLVM Compiler Infrastructure 4// 5// This file is distributed under the University of Illinois Open Source 6// License. See LICENSE.TXT for details. 7// 8//===----------------------------------------------------------------------===// 9// 10// This file implements the SourceManager interface. 11// 12//===----------------------------------------------------------------------===// 13 14#include "clang/Basic/SourceManager.h" 15#include "clang/Basic/SourceManagerInternals.h" 16#include "clang/Basic/FileManager.h" 17#include "llvm/Support/Compiler.h" 18#include "llvm/Support/MemoryBuffer.h" 19#include "llvm/System/Path.h" 20#include "llvm/Support/Streams.h" 21#include <algorithm> 22#include <iostream> 23using namespace clang; 24using namespace SrcMgr; 25using llvm::MemoryBuffer; 26 27//===----------------------------------------------------------------------===// 28// SourceManager Helper Classes 29//===----------------------------------------------------------------------===// 30 31ContentCache::~ContentCache() { 32 delete Buffer; 33} 34 35/// getSizeBytesMapped - Returns the number of bytes actually mapped for 36/// this ContentCache. This can be 0 if the MemBuffer was not actually 37/// instantiated. 38unsigned ContentCache::getSizeBytesMapped() const { 39 return Buffer ? Buffer->getBufferSize() : 0; 40} 41 42/// getSize - Returns the size of the content encapsulated by this ContentCache. 43/// This can be the size of the source file or the size of an arbitrary 44/// scratch buffer. If the ContentCache encapsulates a source file, that 45/// file is not lazily brought in from disk to satisfy this query. 46unsigned ContentCache::getSize() const { 47 return Entry ? Entry->getSize() : Buffer->getBufferSize(); 48} 49 50const llvm::MemoryBuffer *ContentCache::getBuffer() const { 51 // Lazily create the Buffer for ContentCaches that wrap files. 52 if (!Buffer && Entry) { 53 // FIXME: Should we support a way to not have to do this check over 54 // and over if we cannot open the file? 55 Buffer = MemoryBuffer::getFile(Entry->getName(), 0, Entry->getSize()); 56 } 57 return Buffer; 58} 59 60unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) { 61 // Look up the filename in the string table, returning the pre-existing value 62 // if it exists. 63 llvm::StringMapEntry<unsigned> &Entry = 64 FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U); 65 if (Entry.getValue() != ~0U) 66 return Entry.getValue(); 67 68 // Otherwise, assign this the next available ID. 69 Entry.setValue(FilenamesByID.size()); 70 FilenamesByID.push_back(&Entry); 71 return FilenamesByID.size()-1; 72} 73 74/// AddLineNote - Add a line note to the line table that indicates that there 75/// is a #line at the specified FID/Offset location which changes the presumed 76/// location to LineNo/FilenameID. 77void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset, 78 unsigned LineNo, int FilenameID) { 79 std::vector<LineEntry> &Entries = LineEntries[FID]; 80 81 assert((Entries.empty() || Entries.back().FileOffset < Offset) && 82 "Adding line entries out of order!"); 83 84 SrcMgr::CharacteristicKind Kind = SrcMgr::C_User; 85 unsigned IncludeOffset = 0; 86 87 if (!Entries.empty()) { 88 // If this is a '#line 4' after '#line 42 "foo.h"', make sure to remember 89 // that we are still in "foo.h". 90 if (FilenameID == -1) 91 FilenameID = Entries.back().FilenameID; 92 93 // If we are after a line marker that switched us to system header mode, or 94 // that set #include information, preserve it. 95 Kind = Entries.back().FileKind; 96 IncludeOffset = Entries.back().IncludeOffset; 97 } 98 99 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, Kind, 100 IncludeOffset)); 101} 102 103/// AddLineNote This is the same as the previous version of AddLineNote, but is 104/// used for GNU line markers. If EntryExit is 0, then this doesn't change the 105/// presumed #include stack. If it is 1, this is a file entry, if it is 2 then 106/// this is a file exit. FileKind specifies whether this is a system header or 107/// extern C system header. 108void LineTableInfo::AddLineNote(unsigned FID, unsigned Offset, 109 unsigned LineNo, int FilenameID, 110 unsigned EntryExit, 111 SrcMgr::CharacteristicKind FileKind) { 112 assert(FilenameID != -1 && "Unspecified filename should use other accessor"); 113 114 std::vector<LineEntry> &Entries = LineEntries[FID]; 115 116 assert((Entries.empty() || Entries.back().FileOffset < Offset) && 117 "Adding line entries out of order!"); 118 119 unsigned IncludeOffset = 0; 120 if (EntryExit == 0) { // No #include stack change. 121 IncludeOffset = Entries.empty() ? 0 : Entries.back().IncludeOffset; 122 } else if (EntryExit == 1) { 123 IncludeOffset = Offset-1; 124 } else if (EntryExit == 2) { 125 assert(!Entries.empty() && Entries.back().IncludeOffset && 126 "PPDirectives should have caught case when popping empty include stack"); 127 128 // Get the include loc of the last entries' include loc as our include loc. 129 IncludeOffset = 0; 130 if (const LineEntry *PrevEntry = 131 FindNearestLineEntry(FID, Entries.back().IncludeOffset)) 132 IncludeOffset = PrevEntry->IncludeOffset; 133 } 134 135 Entries.push_back(LineEntry::get(Offset, LineNo, FilenameID, FileKind, 136 IncludeOffset)); 137} 138 139 140/// FindNearestLineEntry - Find the line entry nearest to FID that is before 141/// it. If there is no line entry before Offset in FID, return null. 142const LineEntry *LineTableInfo::FindNearestLineEntry(unsigned FID, 143 unsigned Offset) { 144 const std::vector<LineEntry> &Entries = LineEntries[FID]; 145 assert(!Entries.empty() && "No #line entries for this FID after all!"); 146 147 // It is very common for the query to be after the last #line, check this 148 // first. 149 if (Entries.back().FileOffset <= Offset) 150 return &Entries.back(); 151 152 // Do a binary search to find the maximal element that is still before Offset. 153 std::vector<LineEntry>::const_iterator I = 154 std::upper_bound(Entries.begin(), Entries.end(), Offset); 155 if (I == Entries.begin()) return 0; 156 return &*--I; 157} 158 159/// \brief Add a new line entry that has already been encoded into 160/// the internal representation of the line table. 161void LineTableInfo::AddEntry(unsigned FID, 162 const std::vector<LineEntry> &Entries) { 163 LineEntries[FID] = Entries; 164} 165 166/// getLineTableFilenameID - Return the uniqued ID for the specified filename. 167/// 168unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) { 169 if (LineTable == 0) 170 LineTable = new LineTableInfo(); 171 return LineTable->getLineTableFilenameID(Ptr, Len); 172} 173 174 175/// AddLineNote - Add a line note to the line table for the FileID and offset 176/// specified by Loc. If FilenameID is -1, it is considered to be 177/// unspecified. 178void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo, 179 int FilenameID) { 180 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); 181 182 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile(); 183 184 // Remember that this file has #line directives now if it doesn't already. 185 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives(); 186 187 if (LineTable == 0) 188 LineTable = new LineTableInfo(); 189 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID); 190} 191 192/// AddLineNote - Add a GNU line marker to the line table. 193void SourceManager::AddLineNote(SourceLocation Loc, unsigned LineNo, 194 int FilenameID, bool IsFileEntry, 195 bool IsFileExit, bool IsSystemHeader, 196 bool IsExternCHeader) { 197 // If there is no filename and no flags, this is treated just like a #line, 198 // which does not change the flags of the previous line marker. 199 if (FilenameID == -1) { 200 assert(!IsFileEntry && !IsFileExit && !IsSystemHeader && !IsExternCHeader && 201 "Can't set flags without setting the filename!"); 202 return AddLineNote(Loc, LineNo, FilenameID); 203 } 204 205 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); 206 const SrcMgr::FileInfo &FileInfo = getSLocEntry(LocInfo.first).getFile(); 207 208 // Remember that this file has #line directives now if it doesn't already. 209 const_cast<SrcMgr::FileInfo&>(FileInfo).setHasLineDirectives(); 210 211 if (LineTable == 0) 212 LineTable = new LineTableInfo(); 213 214 SrcMgr::CharacteristicKind FileKind; 215 if (IsExternCHeader) 216 FileKind = SrcMgr::C_ExternCSystem; 217 else if (IsSystemHeader) 218 FileKind = SrcMgr::C_System; 219 else 220 FileKind = SrcMgr::C_User; 221 222 unsigned EntryExit = 0; 223 if (IsFileEntry) 224 EntryExit = 1; 225 else if (IsFileExit) 226 EntryExit = 2; 227 228 LineTable->AddLineNote(LocInfo.first.ID, LocInfo.second, LineNo, FilenameID, 229 EntryExit, FileKind); 230} 231 232LineTableInfo &SourceManager::getLineTable() { 233 if (LineTable == 0) 234 LineTable = new LineTableInfo(); 235 return *LineTable; 236} 237 238//===----------------------------------------------------------------------===// 239// Private 'Create' methods. 240//===----------------------------------------------------------------------===// 241 242SourceManager::~SourceManager() { 243 delete LineTable; 244 245 // Delete FileEntry objects corresponding to content caches. Since the actual 246 // content cache objects are bump pointer allocated, we just have to run the 247 // dtors, but we call the deallocate method for completeness. 248 for (unsigned i = 0, e = MemBufferInfos.size(); i != e; ++i) { 249 MemBufferInfos[i]->~ContentCache(); 250 ContentCacheAlloc.Deallocate(MemBufferInfos[i]); 251 } 252 for (llvm::DenseMap<const FileEntry*, SrcMgr::ContentCache*>::iterator 253 I = FileInfos.begin(), E = FileInfos.end(); I != E; ++I) { 254 I->second->~ContentCache(); 255 ContentCacheAlloc.Deallocate(I->second); 256 } 257} 258 259void SourceManager::clearIDTables() { 260 MainFileID = FileID(); 261 SLocEntryTable.clear(); 262 LastLineNoFileIDQuery = FileID(); 263 LastLineNoContentCache = 0; 264 LastFileIDLookup = FileID(); 265 266 if (LineTable) 267 LineTable->clear(); 268 269 // Use up FileID #0 as an invalid instantiation. 270 NextOffset = 0; 271 createInstantiationLoc(SourceLocation(),SourceLocation(),SourceLocation(), 1); 272} 273 274/// getOrCreateContentCache - Create or return a cached ContentCache for the 275/// specified file. 276const ContentCache * 277SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) { 278 assert(FileEnt && "Didn't specify a file entry to use?"); 279 280 // Do we already have information about this file? 281 ContentCache *&Entry = FileInfos[FileEnt]; 282 if (Entry) return Entry; 283 284 // Nope, create a new Cache entry. Make sure it is at least 8-byte aligned 285 // so that FileInfo can use the low 3 bits of the pointer for its own 286 // nefarious purposes. 287 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment; 288 EntryAlign = std::max(8U, EntryAlign); 289 Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign); 290 new (Entry) ContentCache(FileEnt); 291 return Entry; 292} 293 294 295/// createMemBufferContentCache - Create a new ContentCache for the specified 296/// memory buffer. This does no caching. 297const ContentCache* 298SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) { 299 // Add a new ContentCache to the MemBufferInfos list and return it. Make sure 300 // it is at least 8-byte aligned so that FileInfo can use the low 3 bits of 301 // the pointer for its own nefarious purposes. 302 unsigned EntryAlign = llvm::AlignOf<ContentCache>::Alignment; 303 EntryAlign = std::max(8U, EntryAlign); 304 ContentCache *Entry = ContentCacheAlloc.Allocate<ContentCache>(1, EntryAlign); 305 new (Entry) ContentCache(); 306 MemBufferInfos.push_back(Entry); 307 Entry->setBuffer(Buffer); 308 return Entry; 309} 310 311void SourceManager::PreallocateSLocEntries(ExternalSLocEntrySource *Source, 312 unsigned NumSLocEntries, 313 unsigned NextOffset) { 314 ExternalSLocEntries = Source; 315 this->NextOffset = NextOffset; 316 SLocEntryLoaded.resize(NumSLocEntries + 1); 317 SLocEntryLoaded[0] = true; 318 SLocEntryTable.resize(SLocEntryTable.size() + NumSLocEntries); 319} 320 321 322//===----------------------------------------------------------------------===// 323// Methods to create new FileID's and instantiations. 324//===----------------------------------------------------------------------===// 325 326/// createFileID - Create a new fileID for the specified ContentCache and 327/// include position. This works regardless of whether the ContentCache 328/// corresponds to a file or some other input source. 329FileID SourceManager::createFileID(const ContentCache *File, 330 SourceLocation IncludePos, 331 SrcMgr::CharacteristicKind FileCharacter, 332 unsigned PreallocatedID, 333 unsigned Offset) { 334 SLocEntry NewEntry = SLocEntry::get(NextOffset, 335 FileInfo::get(IncludePos, File, 336 FileCharacter)); 337 if (PreallocatedID) { 338 // If we're filling in a preallocated ID, just load in the file 339 // entry and return. 340 assert(PreallocatedID < SLocEntryLoaded.size() && 341 "Preallocate ID out-of-range"); 342 assert(!SLocEntryLoaded[PreallocatedID] && 343 "Source location entry already loaded"); 344 assert(Offset && "Preallocate source location cannot have zero offset"); 345 SLocEntryTable[PreallocatedID] 346 = SLocEntry::get(Offset, FileInfo::get(IncludePos, File, FileCharacter)); 347 SLocEntryLoaded[PreallocatedID] = true; 348 return LastFileIDLookup = FileID::get(PreallocatedID); 349 } 350 351 SLocEntryTable.push_back(SLocEntry::get(NextOffset, 352 FileInfo::get(IncludePos, File, 353 FileCharacter))); 354 unsigned FileSize = File->getSize(); 355 assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!"); 356 NextOffset += FileSize+1; 357 358 // Set LastFileIDLookup to the newly created file. The next getFileID call is 359 // almost guaranteed to be from that file. 360 return LastFileIDLookup = FileID::get(SLocEntryTable.size()-1); 361} 362 363/// createInstantiationLoc - Return a new SourceLocation that encodes the fact 364/// that a token from SpellingLoc should actually be referenced from 365/// InstantiationLoc. 366SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc, 367 SourceLocation ILocStart, 368 SourceLocation ILocEnd, 369 unsigned TokLength, 370 unsigned PreallocatedID, 371 unsigned Offset) { 372 InstantiationInfo II = InstantiationInfo::get(ILocStart,ILocEnd, SpellingLoc); 373 if (PreallocatedID) { 374 // If we're filling in a preallocated ID, just load in the 375 // instantiation entry and return. 376 assert(PreallocatedID < SLocEntryLoaded.size() && 377 "Preallocate ID out-of-range"); 378 assert(!SLocEntryLoaded[PreallocatedID] && 379 "Source location entry already loaded"); 380 assert(Offset && "Preallocate source location cannot have zero offset"); 381 SLocEntryTable[PreallocatedID] = SLocEntry::get(Offset, II); 382 SLocEntryLoaded[PreallocatedID] = true; 383 return SourceLocation::getMacroLoc(Offset); 384 } 385 SLocEntryTable.push_back(SLocEntry::get(NextOffset, II)); 386 assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!"); 387 NextOffset += TokLength+1; 388 return SourceLocation::getMacroLoc(NextOffset-(TokLength+1)); 389} 390 391/// getBufferData - Return a pointer to the start and end of the source buffer 392/// data for the specified FileID. 393std::pair<const char*, const char*> 394SourceManager::getBufferData(FileID FID) const { 395 const llvm::MemoryBuffer *Buf = getBuffer(FID); 396 return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd()); 397} 398 399 400//===----------------------------------------------------------------------===// 401// SourceLocation manipulation methods. 402//===----------------------------------------------------------------------===// 403 404/// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot 405/// method that is used for all SourceManager queries that start with a 406/// SourceLocation object. It is responsible for finding the entry in 407/// SLocEntryTable which contains the specified location. 408/// 409FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const { 410 assert(SLocOffset && "Invalid FileID"); 411 412 // After the first and second level caches, I see two common sorts of 413 // behavior: 1) a lot of searched FileID's are "near" the cached file location 414 // or are "near" the cached instantiation location. 2) others are just 415 // completely random and may be a very long way away. 416 // 417 // To handle this, we do a linear search for up to 8 steps to catch #1 quickly 418 // then we fall back to a less cache efficient, but more scalable, binary 419 // search to find the location. 420 421 // See if this is near the file point - worst case we start scanning from the 422 // most newly created FileID. 423 std::vector<SrcMgr::SLocEntry>::const_iterator I; 424 425 if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) { 426 // Neither loc prunes our search. 427 I = SLocEntryTable.end(); 428 } else { 429 // Perhaps it is near the file point. 430 I = SLocEntryTable.begin()+LastFileIDLookup.ID; 431 } 432 433 // Find the FileID that contains this. "I" is an iterator that points to a 434 // FileID whose offset is known to be larger than SLocOffset. 435 unsigned NumProbes = 0; 436 while (1) { 437 --I; 438 if (ExternalSLocEntries) 439 getSLocEntry(FileID::get(I - SLocEntryTable.begin())); 440 if (I->getOffset() <= SLocOffset) { 441#if 0 442 printf("lin %d -> %d [%s] %d %d\n", SLocOffset, 443 I-SLocEntryTable.begin(), 444 I->isInstantiation() ? "inst" : "file", 445 LastFileIDLookup.ID, int(SLocEntryTable.end()-I)); 446#endif 447 FileID Res = FileID::get(I-SLocEntryTable.begin()); 448 449 // If this isn't an instantiation, remember it. We have good locality 450 // across FileID lookups. 451 if (!I->isInstantiation()) 452 LastFileIDLookup = Res; 453 NumLinearScans += NumProbes+1; 454 return Res; 455 } 456 if (++NumProbes == 8) 457 break; 458 } 459 460 // Convert "I" back into an index. We know that it is an entry whose index is 461 // larger than the offset we are looking for. 462 unsigned GreaterIndex = I-SLocEntryTable.begin(); 463 // LessIndex - This is the lower bound of the range that we're searching. 464 // We know that the offset corresponding to the FileID is is less than 465 // SLocOffset. 466 unsigned LessIndex = 0; 467 NumProbes = 0; 468 while (1) { 469 unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex; 470 unsigned MidOffset = getSLocEntry(FileID::get(MiddleIndex)).getOffset(); 471 472 ++NumProbes; 473 474 // If the offset of the midpoint is too large, chop the high side of the 475 // range to the midpoint. 476 if (MidOffset > SLocOffset) { 477 GreaterIndex = MiddleIndex; 478 continue; 479 } 480 481 // If the middle index contains the value, succeed and return. 482 if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) { 483#if 0 484 printf("bin %d -> %d [%s] %d %d\n", SLocOffset, 485 I-SLocEntryTable.begin(), 486 I->isInstantiation() ? "inst" : "file", 487 LastFileIDLookup.ID, int(SLocEntryTable.end()-I)); 488#endif 489 FileID Res = FileID::get(MiddleIndex); 490 491 // If this isn't an instantiation, remember it. We have good locality 492 // across FileID lookups. 493 if (!I->isInstantiation()) 494 LastFileIDLookup = Res; 495 NumBinaryProbes += NumProbes; 496 return Res; 497 } 498 499 // Otherwise, move the low-side up to the middle index. 500 LessIndex = MiddleIndex; 501 } 502} 503 504SourceLocation SourceManager:: 505getInstantiationLocSlowCase(SourceLocation Loc) const { 506 do { 507 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); 508 Loc = getSLocEntry(LocInfo.first).getInstantiation() 509 .getInstantiationLocStart(); 510 Loc = Loc.getFileLocWithOffset(LocInfo.second); 511 } while (!Loc.isFileID()); 512 513 return Loc; 514} 515 516SourceLocation SourceManager::getSpellingLocSlowCase(SourceLocation Loc) const { 517 do { 518 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); 519 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc(); 520 Loc = Loc.getFileLocWithOffset(LocInfo.second); 521 } while (!Loc.isFileID()); 522 return Loc; 523} 524 525 526std::pair<FileID, unsigned> 527SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E, 528 unsigned Offset) const { 529 // If this is an instantiation record, walk through all the instantiation 530 // points. 531 FileID FID; 532 SourceLocation Loc; 533 do { 534 Loc = E->getInstantiation().getInstantiationLocStart(); 535 536 FID = getFileID(Loc); 537 E = &getSLocEntry(FID); 538 Offset += Loc.getOffset()-E->getOffset(); 539 } while (!Loc.isFileID()); 540 541 return std::make_pair(FID, Offset); 542} 543 544std::pair<FileID, unsigned> 545SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E, 546 unsigned Offset) const { 547 // If this is an instantiation record, walk through all the instantiation 548 // points. 549 FileID FID; 550 SourceLocation Loc; 551 do { 552 Loc = E->getInstantiation().getSpellingLoc(); 553 554 FID = getFileID(Loc); 555 E = &getSLocEntry(FID); 556 Offset += Loc.getOffset()-E->getOffset(); 557 } while (!Loc.isFileID()); 558 559 return std::make_pair(FID, Offset); 560} 561 562/// getImmediateSpellingLoc - Given a SourceLocation object, return the 563/// spelling location referenced by the ID. This is the first level down 564/// towards the place where the characters that make up the lexed token can be 565/// found. This should not generally be used by clients. 566SourceLocation SourceManager::getImmediateSpellingLoc(SourceLocation Loc) const{ 567 if (Loc.isFileID()) return Loc; 568 std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc); 569 Loc = getSLocEntry(LocInfo.first).getInstantiation().getSpellingLoc(); 570 return Loc.getFileLocWithOffset(LocInfo.second); 571} 572 573 574/// getImmediateInstantiationRange - Loc is required to be an instantiation 575/// location. Return the start/end of the instantiation information. 576std::pair<SourceLocation,SourceLocation> 577SourceManager::getImmediateInstantiationRange(SourceLocation Loc) const { 578 assert(Loc.isMacroID() && "Not an instantiation loc!"); 579 const InstantiationInfo &II = getSLocEntry(getFileID(Loc)).getInstantiation(); 580 return II.getInstantiationLocRange(); 581} 582 583/// getInstantiationRange - Given a SourceLocation object, return the 584/// range of tokens covered by the instantiation in the ultimate file. 585std::pair<SourceLocation,SourceLocation> 586SourceManager::getInstantiationRange(SourceLocation Loc) const { 587 if (Loc.isFileID()) return std::make_pair(Loc, Loc); 588 589 std::pair<SourceLocation,SourceLocation> Res = 590 getImmediateInstantiationRange(Loc); 591 592 // Fully resolve the start and end locations to their ultimate instantiation 593 // points. 594 while (!Res.first.isFileID()) 595 Res.first = getImmediateInstantiationRange(Res.first).first; 596 while (!Res.second.isFileID()) 597 Res.second = getImmediateInstantiationRange(Res.second).second; 598 return Res; 599} 600 601 602 603//===----------------------------------------------------------------------===// 604// Queries about the code at a SourceLocation. 605//===----------------------------------------------------------------------===// 606 607/// getCharacterData - Return a pointer to the start of the specified location 608/// in the appropriate MemoryBuffer. 609const char *SourceManager::getCharacterData(SourceLocation SL) const { 610 // Note that this is a hot function in the getSpelling() path, which is 611 // heavily used by -E mode. 612 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL); 613 614 // Note that calling 'getBuffer()' may lazily page in a source file. 615 return getSLocEntry(LocInfo.first).getFile().getContentCache() 616 ->getBuffer()->getBufferStart() + LocInfo.second; 617} 618 619 620/// getColumnNumber - Return the column # for the specified file position. 621/// this is significantly cheaper to compute than the line number. 622unsigned SourceManager::getColumnNumber(FileID FID, unsigned FilePos) const { 623 const char *Buf = getBuffer(FID)->getBufferStart(); 624 625 unsigned LineStart = FilePos; 626 while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r') 627 --LineStart; 628 return FilePos-LineStart+1; 629} 630 631unsigned SourceManager::getSpellingColumnNumber(SourceLocation Loc) const { 632 if (Loc.isInvalid()) return 0; 633 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc); 634 return getColumnNumber(LocInfo.first, LocInfo.second); 635} 636 637unsigned SourceManager::getInstantiationColumnNumber(SourceLocation Loc) const { 638 if (Loc.isInvalid()) return 0; 639 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); 640 return getColumnNumber(LocInfo.first, LocInfo.second); 641} 642 643 644 645static void ComputeLineNumbers(ContentCache* FI, 646 llvm::BumpPtrAllocator &Alloc) DISABLE_INLINE; 647static void ComputeLineNumbers(ContentCache* FI, llvm::BumpPtrAllocator &Alloc){ 648 // Note that calling 'getBuffer()' may lazily page in the file. 649 const MemoryBuffer *Buffer = FI->getBuffer(); 650 651 // Find the file offsets of all of the *physical* source lines. This does 652 // not look at trigraphs, escaped newlines, or anything else tricky. 653 std::vector<unsigned> LineOffsets; 654 655 // Line #1 starts at char 0. 656 LineOffsets.push_back(0); 657 658 const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart(); 659 const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd(); 660 unsigned Offs = 0; 661 while (1) { 662 // Skip over the contents of the line. 663 // TODO: Vectorize this? This is very performance sensitive for programs 664 // with lots of diagnostics and in -E mode. 665 const unsigned char *NextBuf = (const unsigned char *)Buf; 666 while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0') 667 ++NextBuf; 668 Offs += NextBuf-Buf; 669 Buf = NextBuf; 670 671 if (Buf[0] == '\n' || Buf[0] == '\r') { 672 // If this is \n\r or \r\n, skip both characters. 673 if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1]) 674 ++Offs, ++Buf; 675 ++Offs, ++Buf; 676 LineOffsets.push_back(Offs); 677 } else { 678 // Otherwise, this is a null. If end of file, exit. 679 if (Buf == End) break; 680 // Otherwise, skip the null. 681 ++Offs, ++Buf; 682 } 683 } 684 685 // Copy the offsets into the FileInfo structure. 686 FI->NumLines = LineOffsets.size(); 687 FI->SourceLineCache = Alloc.Allocate<unsigned>(LineOffsets.size()); 688 std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache); 689} 690 691/// getLineNumber - Given a SourceLocation, return the spelling line number 692/// for the position indicated. This requires building and caching a table of 693/// line offsets for the MemoryBuffer, so this is not cheap: use only when 694/// about to emit a diagnostic. 695unsigned SourceManager::getLineNumber(FileID FID, unsigned FilePos) const { 696 ContentCache *Content; 697 if (LastLineNoFileIDQuery == FID) 698 Content = LastLineNoContentCache; 699 else 700 Content = const_cast<ContentCache*>(getSLocEntry(FID) 701 .getFile().getContentCache()); 702 703 // If this is the first use of line information for this buffer, compute the 704 /// SourceLineCache for it on demand. 705 if (Content->SourceLineCache == 0) 706 ComputeLineNumbers(Content, ContentCacheAlloc); 707 708 // Okay, we know we have a line number table. Do a binary search to find the 709 // line number that this character position lands on. 710 unsigned *SourceLineCache = Content->SourceLineCache; 711 unsigned *SourceLineCacheStart = SourceLineCache; 712 unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines; 713 714 unsigned QueriedFilePos = FilePos+1; 715 716 // If the previous query was to the same file, we know both the file pos from 717 // that query and the line number returned. This allows us to narrow the 718 // search space from the entire file to something near the match. 719 if (LastLineNoFileIDQuery == FID) { 720 if (QueriedFilePos >= LastLineNoFilePos) { 721 SourceLineCache = SourceLineCache+LastLineNoResult-1; 722 723 // The query is likely to be nearby the previous one. Here we check to 724 // see if it is within 5, 10 or 20 lines. It can be far away in cases 725 // where big comment blocks and vertical whitespace eat up lines but 726 // contribute no tokens. 727 if (SourceLineCache+5 < SourceLineCacheEnd) { 728 if (SourceLineCache[5] > QueriedFilePos) 729 SourceLineCacheEnd = SourceLineCache+5; 730 else if (SourceLineCache+10 < SourceLineCacheEnd) { 731 if (SourceLineCache[10] > QueriedFilePos) 732 SourceLineCacheEnd = SourceLineCache+10; 733 else if (SourceLineCache+20 < SourceLineCacheEnd) { 734 if (SourceLineCache[20] > QueriedFilePos) 735 SourceLineCacheEnd = SourceLineCache+20; 736 } 737 } 738 } 739 } else { 740 SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1; 741 } 742 } 743 744 // If the spread is large, do a "radix" test as our initial guess, based on 745 // the assumption that lines average to approximately the same length. 746 // NOTE: This is currently disabled, as it does not appear to be profitable in 747 // initial measurements. 748 if (0 && SourceLineCacheEnd-SourceLineCache > 20) { 749 unsigned FileLen = Content->SourceLineCache[Content->NumLines-1]; 750 751 // Take a stab at guessing where it is. 752 unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen; 753 754 // Check for -10 and +10 lines. 755 unsigned LowerBound = std::max(int(ApproxPos-10), 0); 756 unsigned UpperBound = std::min(ApproxPos+10, FileLen); 757 758 // If the computed lower bound is less than the query location, move it in. 759 if (SourceLineCache < SourceLineCacheStart+LowerBound && 760 SourceLineCacheStart[LowerBound] < QueriedFilePos) 761 SourceLineCache = SourceLineCacheStart+LowerBound; 762 763 // If the computed upper bound is greater than the query location, move it. 764 if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound && 765 SourceLineCacheStart[UpperBound] >= QueriedFilePos) 766 SourceLineCacheEnd = SourceLineCacheStart+UpperBound; 767 } 768 769 unsigned *Pos 770 = std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos); 771 unsigned LineNo = Pos-SourceLineCacheStart; 772 773 LastLineNoFileIDQuery = FID; 774 LastLineNoContentCache = Content; 775 LastLineNoFilePos = QueriedFilePos; 776 LastLineNoResult = LineNo; 777 return LineNo; 778} 779 780unsigned SourceManager::getInstantiationLineNumber(SourceLocation Loc) const { 781 if (Loc.isInvalid()) return 0; 782 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); 783 return getLineNumber(LocInfo.first, LocInfo.second); 784} 785unsigned SourceManager::getSpellingLineNumber(SourceLocation Loc) const { 786 if (Loc.isInvalid()) return 0; 787 std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(Loc); 788 return getLineNumber(LocInfo.first, LocInfo.second); 789} 790 791/// getFileCharacteristic - return the file characteristic of the specified 792/// source location, indicating whether this is a normal file, a system 793/// header, or an "implicit extern C" system header. 794/// 795/// This state can be modified with flags on GNU linemarker directives like: 796/// # 4 "foo.h" 3 797/// which changes all source locations in the current file after that to be 798/// considered to be from a system header. 799SrcMgr::CharacteristicKind 800SourceManager::getFileCharacteristic(SourceLocation Loc) const { 801 assert(!Loc.isInvalid() && "Can't get file characteristic of invalid loc!"); 802 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); 803 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile(); 804 805 // If there are no #line directives in this file, just return the whole-file 806 // state. 807 if (!FI.hasLineDirectives()) 808 return FI.getFileCharacteristic(); 809 810 assert(LineTable && "Can't have linetable entries without a LineTable!"); 811 // See if there is a #line directive before the location. 812 const LineEntry *Entry = 813 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second); 814 815 // If this is before the first line marker, use the file characteristic. 816 if (!Entry) 817 return FI.getFileCharacteristic(); 818 819 return Entry->FileKind; 820} 821 822/// Return the filename or buffer identifier of the buffer the location is in. 823/// Note that this name does not respect #line directives. Use getPresumedLoc 824/// for normal clients. 825const char *SourceManager::getBufferName(SourceLocation Loc) const { 826 if (Loc.isInvalid()) return "<invalid loc>"; 827 828 return getBuffer(getFileID(Loc))->getBufferIdentifier(); 829} 830 831 832/// getPresumedLoc - This method returns the "presumed" location of a 833/// SourceLocation specifies. A "presumed location" can be modified by #line 834/// or GNU line marker directives. This provides a view on the data that a 835/// user should see in diagnostics, for example. 836/// 837/// Note that a presumed location is always given as the instantiation point 838/// of an instantiation location, not at the spelling location. 839PresumedLoc SourceManager::getPresumedLoc(SourceLocation Loc) const { 840 if (Loc.isInvalid()) return PresumedLoc(); 841 842 // Presumed locations are always for instantiation points. 843 std::pair<FileID, unsigned> LocInfo = getDecomposedInstantiationLoc(Loc); 844 845 const SrcMgr::FileInfo &FI = getSLocEntry(LocInfo.first).getFile(); 846 const SrcMgr::ContentCache *C = FI.getContentCache(); 847 848 // To get the source name, first consult the FileEntry (if one exists) 849 // before the MemBuffer as this will avoid unnecessarily paging in the 850 // MemBuffer. 851 const char *Filename = 852 C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier(); 853 unsigned LineNo = getLineNumber(LocInfo.first, LocInfo.second); 854 unsigned ColNo = getColumnNumber(LocInfo.first, LocInfo.second); 855 SourceLocation IncludeLoc = FI.getIncludeLoc(); 856 857 // If we have #line directives in this file, update and overwrite the physical 858 // location info if appropriate. 859 if (FI.hasLineDirectives()) { 860 assert(LineTable && "Can't have linetable entries without a LineTable!"); 861 // See if there is a #line directive before this. If so, get it. 862 if (const LineEntry *Entry = 863 LineTable->FindNearestLineEntry(LocInfo.first.ID, LocInfo.second)) { 864 // If the LineEntry indicates a filename, use it. 865 if (Entry->FilenameID != -1) 866 Filename = LineTable->getFilename(Entry->FilenameID); 867 868 // Use the line number specified by the LineEntry. This line number may 869 // be multiple lines down from the line entry. Add the difference in 870 // physical line numbers from the query point and the line marker to the 871 // total. 872 unsigned MarkerLineNo = getLineNumber(LocInfo.first, Entry->FileOffset); 873 LineNo = Entry->LineNo + (LineNo-MarkerLineNo-1); 874 875 // Note that column numbers are not molested by line markers. 876 877 // Handle virtual #include manipulation. 878 if (Entry->IncludeOffset) { 879 IncludeLoc = getLocForStartOfFile(LocInfo.first); 880 IncludeLoc = IncludeLoc.getFileLocWithOffset(Entry->IncludeOffset); 881 } 882 } 883 } 884 885 return PresumedLoc(Filename, LineNo, ColNo, IncludeLoc); 886} 887 888//===----------------------------------------------------------------------===// 889// Other miscellaneous methods. 890//===----------------------------------------------------------------------===// 891 892 893/// PrintStats - Print statistics to stderr. 894/// 895void SourceManager::PrintStats() const { 896 llvm::cerr << "\n*** Source Manager Stats:\n"; 897 llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size() 898 << " mem buffers mapped.\n"; 899 llvm::cerr << SLocEntryTable.size() << " SLocEntry's allocated, " 900 << NextOffset << "B of Sloc address space used.\n"; 901 902 unsigned NumLineNumsComputed = 0; 903 unsigned NumFileBytesMapped = 0; 904 for (fileinfo_iterator I = fileinfo_begin(), E = fileinfo_end(); I != E; ++I){ 905 NumLineNumsComputed += I->second->SourceLineCache != 0; 906 NumFileBytesMapped += I->second->getSizeBytesMapped(); 907 } 908 909 llvm::cerr << NumFileBytesMapped << " bytes of files mapped, " 910 << NumLineNumsComputed << " files with line #'s computed.\n"; 911 llvm::cerr << "FileID scans: " << NumLinearScans << " linear, " 912 << NumBinaryProbes << " binary.\n"; 913} 914 915ExternalSLocEntrySource::~ExternalSLocEntrySource() { } 916