2006-06-18 13:43:12 +08:00
|
|
|
//===--- SourceManager.cpp - Track and cache source files -----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-30 03:59:25 +08:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2006-06-18 13:43:12 +08:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the SourceManager interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
2007-07-24 13:57:19 +08:00
|
|
|
#include "llvm/Support/Compiler.h"
|
2007-04-29 15:12:06 +08:00
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
2006-06-18 13:43:12 +08:00
|
|
|
#include "llvm/System/Path.h"
|
2007-10-31 05:08:08 +08:00
|
|
|
#include "llvm/Bitcode/Serialize.h"
|
|
|
|
#include "llvm/Bitcode/Deserialize.h"
|
2007-12-06 06:21:13 +08:00
|
|
|
#include "llvm/Support/Streams.h"
|
2006-06-18 13:43:12 +08:00
|
|
|
#include <algorithm>
|
|
|
|
using namespace clang;
|
2006-06-20 13:02:40 +08:00
|
|
|
using namespace SrcMgr;
|
2007-06-16 07:05:46 +08:00
|
|
|
using llvm::MemoryBuffer;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2009-01-26 08:43:02 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// SourceManager Helper Classes
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
2007-10-31 05:08:08 +08:00
|
|
|
ContentCache::~ContentCache() {
|
|
|
|
delete Buffer;
|
|
|
|
delete [] SourceLineCache;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2009-01-06 09:55:26 +08:00
|
|
|
/// getSizeBytesMapped - Returns the number of bytes actually mapped for
|
|
|
|
/// this ContentCache. This can be 0 if the MemBuffer was not actually
|
|
|
|
/// instantiated.
|
|
|
|
unsigned ContentCache::getSizeBytesMapped() const {
|
|
|
|
return Buffer ? Buffer->getBufferSize() : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getSize - Returns the size of the content encapsulated by this ContentCache.
|
|
|
|
/// This can be the size of the source file or the size of an arbitrary
|
|
|
|
/// scratch buffer. If the ContentCache encapsulates a source file, that
|
|
|
|
/// file is not lazily brought in from disk to satisfy this query.
|
|
|
|
unsigned ContentCache::getSize() const {
|
|
|
|
return Entry ? Entry->getSize() : Buffer->getBufferSize();
|
|
|
|
}
|
|
|
|
|
2009-01-26 15:37:49 +08:00
|
|
|
const llvm::MemoryBuffer *ContentCache::getBuffer() const {
|
2009-01-07 06:43:04 +08:00
|
|
|
// Lazily create the Buffer for ContentCaches that wrap files.
|
|
|
|
if (!Buffer && Entry) {
|
|
|
|
// FIXME: Should we support a way to not have to do this check over
|
|
|
|
// and over if we cannot open the file?
|
2009-01-17 11:54:16 +08:00
|
|
|
Buffer = MemoryBuffer::getFile(Entry->getName(), 0, Entry->getSize());
|
2009-01-07 06:43:04 +08:00
|
|
|
}
|
2009-01-06 09:55:26 +08:00
|
|
|
return Buffer;
|
|
|
|
}
|
|
|
|
|
2009-01-26 15:57:50 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Line Table Implementation
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
namespace clang {
|
|
|
|
/// LineTableInfo - This class is used to hold and unique data used to
|
|
|
|
/// represent #line information.
|
|
|
|
class LineTableInfo {
|
|
|
|
/// FilenameIDs - This map is used to assign unique IDs to filenames in
|
|
|
|
/// #line directives. This allows us to unique the filenames that
|
|
|
|
/// frequently reoccur and reference them with indices. FilenameIDs holds
|
|
|
|
/// the mapping from string -> ID, and FilenamesByID holds the mapping of ID
|
|
|
|
/// to string.
|
|
|
|
llvm::StringMap<unsigned, llvm::BumpPtrAllocator> FilenameIDs;
|
|
|
|
std::vector<llvm::StringMapEntry<unsigned>*> FilenamesByID;
|
|
|
|
public:
|
|
|
|
LineTableInfo() {
|
|
|
|
}
|
|
|
|
|
|
|
|
void clear() {
|
|
|
|
FilenameIDs.clear();
|
|
|
|
FilenamesByID.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
~LineTableInfo() {}
|
|
|
|
|
|
|
|
unsigned getLineTableFilenameID(const char *Ptr, unsigned Len);
|
|
|
|
|
|
|
|
};
|
|
|
|
} // namespace clang
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
unsigned LineTableInfo::getLineTableFilenameID(const char *Ptr, unsigned Len) {
|
|
|
|
// Look up the filename in the string table, returning the pre-existing value
|
|
|
|
// if it exists.
|
|
|
|
llvm::StringMapEntry<unsigned> &Entry =
|
|
|
|
FilenameIDs.GetOrCreateValue(Ptr, Ptr+Len, ~0U);
|
|
|
|
if (Entry.getValue() != ~0U)
|
|
|
|
return Entry.getValue();
|
|
|
|
|
|
|
|
// Otherwise, assign this the next available ID.
|
|
|
|
Entry.setValue(FilenamesByID.size());
|
|
|
|
FilenamesByID.push_back(&Entry);
|
|
|
|
return FilenamesByID.size()-1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getLineTableFilenameID - Return the uniqued ID for the specified filename.
|
|
|
|
///
|
|
|
|
unsigned SourceManager::getLineTableFilenameID(const char *Ptr, unsigned Len) {
|
|
|
|
if (LineTable == 0)
|
|
|
|
LineTable = new LineTableInfo();
|
|
|
|
return LineTable->getLineTableFilenameID(Ptr, Len);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-26 08:43:02 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// Private 'Create' methods.
|
|
|
|
//===--------------------------------------------------------------------===//
|
2009-01-06 09:55:26 +08:00
|
|
|
|
2009-01-26 15:57:50 +08:00
|
|
|
SourceManager::~SourceManager() {
|
|
|
|
delete LineTable;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SourceManager::clearIDTables() {
|
|
|
|
MainFileID = FileID();
|
|
|
|
SLocEntryTable.clear();
|
|
|
|
LastLineNoFileIDQuery = FileID();
|
|
|
|
LastLineNoContentCache = 0;
|
|
|
|
LastFileIDLookup = FileID();
|
|
|
|
|
|
|
|
if (LineTable)
|
|
|
|
LineTable->clear();
|
|
|
|
|
|
|
|
// Use up FileID #0 as an invalid instantiation.
|
|
|
|
NextOffset = 0;
|
|
|
|
createInstantiationLoc(SourceLocation(), SourceLocation(), 1);
|
|
|
|
}
|
|
|
|
|
2009-01-26 08:43:02 +08:00
|
|
|
/// getOrCreateContentCache - Create or return a cached ContentCache for the
|
|
|
|
/// specified file.
|
|
|
|
const ContentCache *
|
|
|
|
SourceManager::getOrCreateContentCache(const FileEntry *FileEnt) {
|
2006-06-18 13:43:12 +08:00
|
|
|
assert(FileEnt && "Didn't specify a file entry to use?");
|
2009-01-26 08:43:02 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
// Do we already have information about this file?
|
2007-10-31 05:08:08 +08:00
|
|
|
std::set<ContentCache>::iterator I =
|
|
|
|
FileInfos.lower_bound(ContentCache(FileEnt));
|
|
|
|
|
|
|
|
if (I != FileInfos.end() && I->Entry == FileEnt)
|
2006-06-18 13:43:12 +08:00
|
|
|
return &*I;
|
|
|
|
|
2009-01-26 15:37:49 +08:00
|
|
|
// Nope, create a new Cache entry.
|
2007-10-31 05:08:08 +08:00
|
|
|
ContentCache& Entry = const_cast<ContentCache&>(*FileInfos.insert(I,FileEnt));
|
|
|
|
Entry.SourceLineCache = 0;
|
|
|
|
Entry.NumLines = 0;
|
2006-06-18 13:43:12 +08:00
|
|
|
return &Entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-11-01 01:53:38 +08:00
|
|
|
/// createMemBufferContentCache - Create a new ContentCache for the specified
|
|
|
|
/// memory buffer. This does no caching.
|
2007-10-31 05:08:08 +08:00
|
|
|
const ContentCache*
|
|
|
|
SourceManager::createMemBufferContentCache(const MemoryBuffer *Buffer) {
|
2007-10-31 06:57:35 +08:00
|
|
|
// Add a new ContentCache to the MemBufferInfos list and return it. We
|
|
|
|
// must default construct the object first that the instance actually
|
|
|
|
// stored within MemBufferInfos actually owns the Buffer, and not any
|
|
|
|
// temporary we would use in the call to "push_back".
|
2007-10-31 05:08:08 +08:00
|
|
|
MemBufferInfos.push_back(ContentCache());
|
|
|
|
ContentCache& Entry = const_cast<ContentCache&>(MemBufferInfos.back());
|
2009-01-06 09:55:26 +08:00
|
|
|
Entry.setBuffer(Buffer);
|
2007-10-31 05:08:08 +08:00
|
|
|
return &Entry;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2009-01-26 08:43:02 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Methods to create new FileID's and instantiations.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2008-09-29 08:25:48 +08:00
|
|
|
/// createFileID - Create a new fileID for the specified ContentCache and
|
2007-10-31 06:57:35 +08:00
|
|
|
/// include position. This works regardless of whether the ContentCache
|
|
|
|
/// corresponds to a file or some other input source.
|
2009-01-17 14:22:33 +08:00
|
|
|
FileID SourceManager::createFileID(const ContentCache *File,
|
2009-01-26 08:43:02 +08:00
|
|
|
SourceLocation IncludePos,
|
|
|
|
SrcMgr::CharacteristicKind FileCharacter) {
|
|
|
|
SLocEntryTable.push_back(SLocEntry::get(NextOffset,
|
|
|
|
FileInfo::get(IncludePos, File,
|
|
|
|
FileCharacter)));
|
2009-01-06 09:55:26 +08:00
|
|
|
unsigned FileSize = File->getSize();
|
2009-01-26 08:43:02 +08:00
|
|
|
assert(NextOffset+FileSize+1 > NextOffset && "Ran out of source locations!");
|
|
|
|
NextOffset += FileSize+1;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2009-01-26 08:43:02 +08:00
|
|
|
// Set LastFileIDLookup to the newly created file. The next getFileID call is
|
|
|
|
// almost guaranteed to be from that file.
|
|
|
|
return LastFileIDLookup = FileID::get(SLocEntryTable.size()-1);
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2009-01-26 08:43:02 +08:00
|
|
|
/// createInstantiationLoc - Return a new SourceLocation that encodes the fact
|
2009-01-16 15:00:02 +08:00
|
|
|
/// that a token from SpellingLoc should actually be referenced from
|
2006-06-30 14:10:08 +08:00
|
|
|
/// InstantiationLoc.
|
2009-01-26 08:43:02 +08:00
|
|
|
SourceLocation SourceManager::createInstantiationLoc(SourceLocation SpellingLoc,
|
|
|
|
SourceLocation InstantLoc,
|
|
|
|
unsigned TokLength) {
|
2007-07-15 14:35:27 +08:00
|
|
|
// The specified source location may be a mapped location, due to a macro
|
|
|
|
// instantiation or #line directive. Strip off this information to find out
|
|
|
|
// where the characters are actually located.
|
2009-01-16 15:00:02 +08:00
|
|
|
SpellingLoc = getSpellingLoc(SpellingLoc);
|
2006-07-17 02:05:08 +08:00
|
|
|
|
2009-01-16 15:36:28 +08:00
|
|
|
// Resolve InstantLoc down to a real instantiation location.
|
|
|
|
InstantLoc = getInstantiationLoc(InstantLoc);
|
2009-01-26 08:43:02 +08:00
|
|
|
|
|
|
|
SLocEntryTable.push_back(SLocEntry::get(NextOffset,
|
|
|
|
InstantiationInfo::get(InstantLoc,
|
|
|
|
SpellingLoc)));
|
|
|
|
assert(NextOffset+TokLength+1 > NextOffset && "Ran out of source locations!");
|
|
|
|
NextOffset += TokLength+1;
|
|
|
|
return SourceLocation::getMacroLoc(NextOffset-(TokLength+1));
|
2006-06-30 14:10:08 +08:00
|
|
|
}
|
|
|
|
|
2009-01-19 15:32:13 +08:00
|
|
|
/// getBufferData - Return a pointer to the start and end of the source buffer
|
|
|
|
/// data for the specified FileID.
|
2009-01-17 14:22:33 +08:00
|
|
|
std::pair<const char*, const char*>
|
|
|
|
SourceManager::getBufferData(FileID FID) const {
|
|
|
|
const llvm::MemoryBuffer *Buf = getBuffer(FID);
|
|
|
|
return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2009-01-26 08:43:02 +08:00
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
// SourceLocation manipulation methods.
|
|
|
|
//===--------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
/// getFileIDSlow - Return the FileID for a SourceLocation. This is a very hot
|
|
|
|
/// method that is used for all SourceManager queries that start with a
|
|
|
|
/// SourceLocation object. It is responsible for finding the entry in
|
|
|
|
/// SLocEntryTable which contains the specified location.
|
|
|
|
///
|
|
|
|
FileID SourceManager::getFileIDSlow(unsigned SLocOffset) const {
|
|
|
|
assert(SLocOffset && "Invalid FileID");
|
|
|
|
|
|
|
|
// After the first and second level caches, I see two common sorts of
|
|
|
|
// behavior: 1) a lot of searched FileID's are "near" the cached file location
|
|
|
|
// or are "near" the cached instantiation location. 2) others are just
|
|
|
|
// completely random and may be a very long way away.
|
|
|
|
//
|
|
|
|
// To handle this, we do a linear search for up to 8 steps to catch #1 quickly
|
|
|
|
// then we fall back to a less cache efficient, but more scalable, binary
|
|
|
|
// search to find the location.
|
|
|
|
|
|
|
|
// See if this is near the file point - worst case we start scanning from the
|
|
|
|
// most newly created FileID.
|
|
|
|
std::vector<SrcMgr::SLocEntry>::const_iterator I;
|
|
|
|
|
|
|
|
if (SLocEntryTable[LastFileIDLookup.ID].getOffset() < SLocOffset) {
|
|
|
|
// Neither loc prunes our search.
|
|
|
|
I = SLocEntryTable.end();
|
|
|
|
} else {
|
|
|
|
// Perhaps it is near the file point.
|
|
|
|
I = SLocEntryTable.begin()+LastFileIDLookup.ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Find the FileID that contains this. "I" is an iterator that points to a
|
|
|
|
// FileID whose offset is known to be larger than SLocOffset.
|
|
|
|
unsigned NumProbes = 0;
|
|
|
|
while (1) {
|
|
|
|
--I;
|
|
|
|
if (I->getOffset() <= SLocOffset) {
|
|
|
|
#if 0
|
|
|
|
printf("lin %d -> %d [%s] %d %d\n", SLocOffset,
|
|
|
|
I-SLocEntryTable.begin(),
|
|
|
|
I->isInstantiation() ? "inst" : "file",
|
|
|
|
LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
|
|
|
|
#endif
|
|
|
|
FileID Res = FileID::get(I-SLocEntryTable.begin());
|
|
|
|
|
|
|
|
// If this isn't an instantiation, remember it. We have good locality
|
|
|
|
// across FileID lookups.
|
|
|
|
if (!I->isInstantiation())
|
|
|
|
LastFileIDLookup = Res;
|
|
|
|
NumLinearScans += NumProbes+1;
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
if (++NumProbes == 8)
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Convert "I" back into an index. We know that it is an entry whose index is
|
|
|
|
// larger than the offset we are looking for.
|
|
|
|
unsigned GreaterIndex = I-SLocEntryTable.begin();
|
|
|
|
// LessIndex - This is the lower bound of the range that we're searching.
|
|
|
|
// We know that the offset corresponding to the FileID is is less than
|
|
|
|
// SLocOffset.
|
|
|
|
unsigned LessIndex = 0;
|
|
|
|
NumProbes = 0;
|
|
|
|
while (1) {
|
|
|
|
unsigned MiddleIndex = (GreaterIndex-LessIndex)/2+LessIndex;
|
|
|
|
unsigned MidOffset = SLocEntryTable[MiddleIndex].getOffset();
|
|
|
|
|
|
|
|
++NumProbes;
|
|
|
|
|
|
|
|
// If the offset of the midpoint is too large, chop the high side of the
|
|
|
|
// range to the midpoint.
|
|
|
|
if (MidOffset > SLocOffset) {
|
|
|
|
GreaterIndex = MiddleIndex;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the middle index contains the value, succeed and return.
|
|
|
|
if (isOffsetInFileID(FileID::get(MiddleIndex), SLocOffset)) {
|
|
|
|
#if 0
|
|
|
|
printf("bin %d -> %d [%s] %d %d\n", SLocOffset,
|
|
|
|
I-SLocEntryTable.begin(),
|
|
|
|
I->isInstantiation() ? "inst" : "file",
|
|
|
|
LastFileIDLookup.ID, int(SLocEntryTable.end()-I));
|
|
|
|
#endif
|
|
|
|
FileID Res = FileID::get(MiddleIndex);
|
|
|
|
|
|
|
|
// If this isn't an instantiation, remember it. We have good locality
|
|
|
|
// across FileID lookups.
|
|
|
|
if (!I->isInstantiation())
|
|
|
|
LastFileIDLookup = Res;
|
|
|
|
NumBinaryProbes += NumProbes;
|
|
|
|
return Res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, move the low-side up to the middle index.
|
|
|
|
LessIndex = MiddleIndex;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<FileID, unsigned>
|
|
|
|
SourceManager::getDecomposedInstantiationLocSlowCase(const SrcMgr::SLocEntry *E,
|
|
|
|
unsigned Offset) const {
|
|
|
|
// If this is an instantiation record, walk through all the instantiation
|
|
|
|
// points.
|
|
|
|
FileID FID;
|
|
|
|
SourceLocation Loc;
|
|
|
|
do {
|
|
|
|
Loc = E->getInstantiation().getInstantiationLoc();
|
|
|
|
|
|
|
|
FID = getFileID(Loc);
|
|
|
|
E = &getSLocEntry(FID);
|
|
|
|
Offset += Loc.getOffset()-E->getOffset();
|
2009-01-27 03:41:58 +08:00
|
|
|
} while (!Loc.isFileID());
|
2009-01-26 08:43:02 +08:00
|
|
|
|
|
|
|
return std::make_pair(FID, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
std::pair<FileID, unsigned>
|
|
|
|
SourceManager::getDecomposedSpellingLocSlowCase(const SrcMgr::SLocEntry *E,
|
|
|
|
unsigned Offset) const {
|
2009-01-27 03:41:58 +08:00
|
|
|
// If this is an instantiation record, walk through all the instantiation
|
|
|
|
// points.
|
|
|
|
FileID FID;
|
|
|
|
SourceLocation Loc;
|
|
|
|
do {
|
|
|
|
Loc = E->getInstantiation().getSpellingLoc();
|
|
|
|
|
|
|
|
FID = getFileID(Loc);
|
|
|
|
E = &getSLocEntry(FID);
|
|
|
|
Offset += Loc.getOffset()-E->getOffset();
|
|
|
|
} while (!Loc.isFileID());
|
|
|
|
|
2009-01-26 08:43:02 +08:00
|
|
|
return std::make_pair(FID, Offset);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Queries about the code at a SourceLocation.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2006-06-21 11:01:55 +08:00
|
|
|
|
2006-06-19 00:22:51 +08:00
|
|
|
/// getCharacterData - Return a pointer to the start of the specified location
|
2007-04-29 15:12:06 +08:00
|
|
|
/// in the appropriate MemoryBuffer.
|
2006-06-19 00:22:51 +08:00
|
|
|
const char *SourceManager::getCharacterData(SourceLocation SL) const {
|
2006-07-05 07:01:03 +08:00
|
|
|
// Note that this is a hot function in the getSpelling() path, which is
|
|
|
|
// heavily used by -E mode.
|
2009-01-26 08:43:02 +08:00
|
|
|
std::pair<FileID, unsigned> LocInfo = getDecomposedSpellingLoc(SL);
|
2009-01-17 14:22:33 +08:00
|
|
|
|
2009-01-06 09:55:26 +08:00
|
|
|
// Note that calling 'getBuffer()' may lazily page in a source file.
|
2009-01-26 08:43:02 +08:00
|
|
|
return getSLocEntry(LocInfo.first).getFile().getContentCache()
|
|
|
|
->getBuffer()->getBufferStart() + LocInfo.second;
|
2006-06-19 00:22:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-07-21 00:37:10 +08:00
|
|
|
/// getColumnNumber - Return the column # for the specified file position.
|
2006-06-18 13:43:12 +08:00
|
|
|
/// this is significantly cheaper to compute than the line number. This returns
|
|
|
|
/// zero if the column number isn't known.
|
2006-06-21 12:57:09 +08:00
|
|
|
unsigned SourceManager::getColumnNumber(SourceLocation Loc) const {
|
2009-01-26 08:43:02 +08:00
|
|
|
if (Loc.isInvalid()) return 0;
|
|
|
|
assert(Loc.isFileID() && "Don't know what part of instantiation loc to get");
|
2009-01-17 14:22:33 +08:00
|
|
|
|
2009-01-26 08:43:02 +08:00
|
|
|
std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
|
2009-01-17 14:22:33 +08:00
|
|
|
unsigned FilePos = LocInfo.second;
|
2006-06-21 11:01:55 +08:00
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
const char *Buf = getBuffer(LocInfo.first)->getBufferStart();
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
unsigned LineStart = FilePos;
|
|
|
|
while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
|
|
|
|
--LineStart;
|
|
|
|
return FilePos-LineStart+1;
|
|
|
|
}
|
|
|
|
|
2007-10-31 05:08:08 +08:00
|
|
|
static void ComputeLineNumbers(ContentCache* FI) DISABLE_INLINE;
|
2009-01-06 09:55:26 +08:00
|
|
|
static void ComputeLineNumbers(ContentCache* FI) {
|
|
|
|
// Note that calling 'getBuffer()' may lazily page in the file.
|
|
|
|
const MemoryBuffer *Buffer = FI->getBuffer();
|
2007-07-24 13:57:19 +08:00
|
|
|
|
|
|
|
// Find the file offsets of all of the *physical* source lines. This does
|
|
|
|
// not look at trigraphs, escaped newlines, or anything else tricky.
|
|
|
|
std::vector<unsigned> LineOffsets;
|
|
|
|
|
|
|
|
// Line #1 starts at char 0.
|
|
|
|
LineOffsets.push_back(0);
|
|
|
|
|
|
|
|
const unsigned char *Buf = (const unsigned char *)Buffer->getBufferStart();
|
|
|
|
const unsigned char *End = (const unsigned char *)Buffer->getBufferEnd();
|
|
|
|
unsigned Offs = 0;
|
|
|
|
while (1) {
|
|
|
|
// Skip over the contents of the line.
|
|
|
|
// TODO: Vectorize this? This is very performance sensitive for programs
|
|
|
|
// with lots of diagnostics and in -E mode.
|
|
|
|
const unsigned char *NextBuf = (const unsigned char *)Buf;
|
|
|
|
while (*NextBuf != '\n' && *NextBuf != '\r' && *NextBuf != '\0')
|
|
|
|
++NextBuf;
|
|
|
|
Offs += NextBuf-Buf;
|
|
|
|
Buf = NextBuf;
|
|
|
|
|
|
|
|
if (Buf[0] == '\n' || Buf[0] == '\r') {
|
|
|
|
// If this is \n\r or \r\n, skip both characters.
|
|
|
|
if ((Buf[1] == '\n' || Buf[1] == '\r') && Buf[0] != Buf[1])
|
|
|
|
++Offs, ++Buf;
|
|
|
|
++Offs, ++Buf;
|
|
|
|
LineOffsets.push_back(Offs);
|
|
|
|
} else {
|
|
|
|
// Otherwise, this is a null. If end of file, exit.
|
|
|
|
if (Buf == End) break;
|
|
|
|
// Otherwise, skip the null.
|
|
|
|
++Offs, ++Buf;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Copy the offsets into the FileInfo structure.
|
|
|
|
FI->NumLines = LineOffsets.size();
|
|
|
|
FI->SourceLineCache = new unsigned[LineOffsets.size()];
|
|
|
|
std::copy(LineOffsets.begin(), LineOffsets.end(), FI->SourceLineCache);
|
|
|
|
}
|
2006-06-21 12:57:09 +08:00
|
|
|
|
2009-01-16 15:00:02 +08:00
|
|
|
/// getLineNumber - Given a SourceLocation, return the spelling line number
|
2006-06-18 13:43:12 +08:00
|
|
|
/// for the position indicated. This requires building and caching a table of
|
2007-04-29 15:12:06 +08:00
|
|
|
/// line offsets for the MemoryBuffer, so this is not cheap: use only when
|
2006-06-18 13:43:12 +08:00
|
|
|
/// about to emit a diagnostic.
|
2008-11-18 14:51:15 +08:00
|
|
|
unsigned SourceManager::getLineNumber(SourceLocation Loc) const {
|
2009-01-26 08:43:02 +08:00
|
|
|
if (Loc.isInvalid()) return 0;
|
|
|
|
assert(Loc.isFileID() && "Don't know what part of instantiation loc to get");
|
2007-10-31 05:08:08 +08:00
|
|
|
|
2009-01-26 08:43:02 +08:00
|
|
|
std::pair<FileID, unsigned> LocInfo = getDecomposedLoc(Loc);
|
2007-07-24 13:57:19 +08:00
|
|
|
|
2009-01-26 08:43:02 +08:00
|
|
|
ContentCache *Content;
|
2009-01-17 14:22:33 +08:00
|
|
|
if (LastLineNoFileIDQuery == LocInfo.first)
|
2007-10-31 05:08:08 +08:00
|
|
|
Content = LastLineNoContentCache;
|
2007-07-24 13:57:19 +08:00
|
|
|
else
|
2009-01-26 08:43:02 +08:00
|
|
|
Content = const_cast<ContentCache*>(getSLocEntry(LocInfo.first)
|
|
|
|
.getFile().getContentCache());
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// If this is the first use of line information for this buffer, compute the
|
2007-07-24 13:57:19 +08:00
|
|
|
/// SourceLineCache for it on demand.
|
2007-10-31 05:08:08 +08:00
|
|
|
if (Content->SourceLineCache == 0)
|
|
|
|
ComputeLineNumbers(Content);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// Okay, we know we have a line number table. Do a binary search to find the
|
|
|
|
// line number that this character position lands on.
|
2007-10-31 05:08:08 +08:00
|
|
|
unsigned *SourceLineCache = Content->SourceLineCache;
|
2007-07-24 13:57:19 +08:00
|
|
|
unsigned *SourceLineCacheStart = SourceLineCache;
|
2007-10-31 05:08:08 +08:00
|
|
|
unsigned *SourceLineCacheEnd = SourceLineCache + Content->NumLines;
|
2007-07-24 13:57:19 +08:00
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
unsigned QueriedFilePos = LocInfo.second+1;
|
2007-07-24 13:57:19 +08:00
|
|
|
|
|
|
|
// If the previous query was to the same file, we know both the file pos from
|
|
|
|
// that query and the line number returned. This allows us to narrow the
|
|
|
|
// search space from the entire file to something near the match.
|
2009-01-17 14:22:33 +08:00
|
|
|
if (LastLineNoFileIDQuery == LocInfo.first) {
|
2007-07-24 13:57:19 +08:00
|
|
|
if (QueriedFilePos >= LastLineNoFilePos) {
|
|
|
|
SourceLineCache = SourceLineCache+LastLineNoResult-1;
|
|
|
|
|
|
|
|
// The query is likely to be nearby the previous one. Here we check to
|
|
|
|
// see if it is within 5, 10 or 20 lines. It can be far away in cases
|
|
|
|
// where big comment blocks and vertical whitespace eat up lines but
|
|
|
|
// contribute no tokens.
|
|
|
|
if (SourceLineCache+5 < SourceLineCacheEnd) {
|
|
|
|
if (SourceLineCache[5] > QueriedFilePos)
|
|
|
|
SourceLineCacheEnd = SourceLineCache+5;
|
|
|
|
else if (SourceLineCache+10 < SourceLineCacheEnd) {
|
|
|
|
if (SourceLineCache[10] > QueriedFilePos)
|
|
|
|
SourceLineCacheEnd = SourceLineCache+10;
|
|
|
|
else if (SourceLineCache+20 < SourceLineCacheEnd) {
|
|
|
|
if (SourceLineCache[20] > QueriedFilePos)
|
|
|
|
SourceLineCacheEnd = SourceLineCache+20;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
SourceLineCacheEnd = SourceLineCache+LastLineNoResult+1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-07-24 14:43:46 +08:00
|
|
|
// If the spread is large, do a "radix" test as our initial guess, based on
|
|
|
|
// the assumption that lines average to approximately the same length.
|
|
|
|
// NOTE: This is currently disabled, as it does not appear to be profitable in
|
|
|
|
// initial measurements.
|
|
|
|
if (0 && SourceLineCacheEnd-SourceLineCache > 20) {
|
2007-10-31 05:08:08 +08:00
|
|
|
unsigned FileLen = Content->SourceLineCache[Content->NumLines-1];
|
2007-07-24 14:43:46 +08:00
|
|
|
|
|
|
|
// Take a stab at guessing where it is.
|
2007-10-31 05:08:08 +08:00
|
|
|
unsigned ApproxPos = Content->NumLines*QueriedFilePos / FileLen;
|
2007-07-24 14:43:46 +08:00
|
|
|
|
|
|
|
// Check for -10 and +10 lines.
|
|
|
|
unsigned LowerBound = std::max(int(ApproxPos-10), 0);
|
|
|
|
unsigned UpperBound = std::min(ApproxPos+10, FileLen);
|
|
|
|
|
|
|
|
// If the computed lower bound is less than the query location, move it in.
|
|
|
|
if (SourceLineCache < SourceLineCacheStart+LowerBound &&
|
|
|
|
SourceLineCacheStart[LowerBound] < QueriedFilePos)
|
|
|
|
SourceLineCache = SourceLineCacheStart+LowerBound;
|
|
|
|
|
|
|
|
// If the computed upper bound is greater than the query location, move it.
|
|
|
|
if (SourceLineCacheEnd > SourceLineCacheStart+UpperBound &&
|
|
|
|
SourceLineCacheStart[UpperBound] >= QueriedFilePos)
|
|
|
|
SourceLineCacheEnd = SourceLineCacheStart+UpperBound;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned *Pos
|
|
|
|
= std::lower_bound(SourceLineCache, SourceLineCacheEnd, QueriedFilePos);
|
2007-07-24 13:57:19 +08:00
|
|
|
unsigned LineNo = Pos-SourceLineCacheStart;
|
|
|
|
|
2009-01-17 14:22:33 +08:00
|
|
|
LastLineNoFileIDQuery = LocInfo.first;
|
2007-10-31 05:08:08 +08:00
|
|
|
LastLineNoContentCache = Content;
|
2007-07-24 13:57:19 +08:00
|
|
|
LastLineNoFilePos = QueriedFilePos;
|
|
|
|
LastLineNoResult = LineNo;
|
|
|
|
return LineNo;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2009-01-26 08:43:02 +08:00
|
|
|
/// getSourceName - This method returns the name of the file or buffer that
|
|
|
|
/// the SourceLocation specifies. This can be modified with #line directives,
|
|
|
|
/// etc.
|
|
|
|
const char *SourceManager::getSourceName(SourceLocation Loc) const {
|
|
|
|
if (Loc.isInvalid()) return "";
|
|
|
|
|
|
|
|
const SrcMgr::ContentCache *C =
|
|
|
|
getSLocEntry(getFileID(getSpellingLoc(Loc))).getFile().getContentCache();
|
|
|
|
|
|
|
|
// To get the source name, first consult the FileEntry (if one exists) before
|
|
|
|
// the MemBuffer as this will avoid unnecessarily paging in the MemBuffer.
|
|
|
|
return C->Entry ? C->Entry->getName() : C->getBuffer()->getBufferIdentifier();
|
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Other miscellaneous methods.
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// PrintStats - Print statistics to stderr.
|
|
|
|
///
|
|
|
|
void SourceManager::PrintStats() const {
|
2007-12-06 06:21:13 +08:00
|
|
|
llvm::cerr << "\n*** Source Manager Stats:\n";
|
|
|
|
llvm::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
|
2009-01-26 08:43:02 +08:00
|
|
|
<< " mem buffers mapped, " << SLocEntryTable.size()
|
|
|
|
<< " SLocEntry's allocated.\n";
|
2006-06-21 11:01:55 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
unsigned NumLineNumsComputed = 0;
|
|
|
|
unsigned NumFileBytesMapped = 0;
|
2007-10-31 05:08:08 +08:00
|
|
|
for (std::set<ContentCache>::const_iterator I =
|
2006-06-18 13:43:12 +08:00
|
|
|
FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
|
2007-10-31 05:08:08 +08:00
|
|
|
NumLineNumsComputed += I->SourceLineCache != 0;
|
2009-01-06 09:55:26 +08:00
|
|
|
NumFileBytesMapped += I->getSizeBytesMapped();
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2007-10-31 05:08:08 +08:00
|
|
|
|
2007-12-06 06:21:13 +08:00
|
|
|
llvm::cerr << NumFileBytesMapped << " bytes of files mapped, "
|
|
|
|
<< NumLineNumsComputed << " files with line #'s computed.\n";
|
2009-01-26 08:43:02 +08:00
|
|
|
llvm::cerr << "FileID scans: " << NumLinearScans << " linear, "
|
|
|
|
<< NumBinaryProbes << " binary.\n";
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
2007-12-05 03:39:02 +08:00
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Serialization.
|
|
|
|
//===----------------------------------------------------------------------===//
|
2007-12-05 08:14:18 +08:00
|
|
|
|
|
|
|
void ContentCache::Emit(llvm::Serializer& S) const {
|
2007-12-05 03:39:02 +08:00
|
|
|
S.FlushRecord();
|
|
|
|
S.EmitPtr(this);
|
|
|
|
|
2007-12-19 06:12:19 +08:00
|
|
|
if (Entry) {
|
|
|
|
llvm::sys::Path Fname(Buffer->getBufferIdentifier());
|
|
|
|
|
|
|
|
if (Fname.isAbsolute())
|
|
|
|
S.EmitCStr(Fname.c_str());
|
|
|
|
else {
|
|
|
|
// Create an absolute path.
|
|
|
|
// FIXME: This will potentially contain ".." and "." in the path.
|
|
|
|
llvm::sys::Path path = llvm::sys::Path::GetCurrentDirectory();
|
|
|
|
path.appendComponent(Fname.c_str());
|
|
|
|
S.EmitCStr(path.c_str());
|
|
|
|
}
|
|
|
|
}
|
2007-12-05 08:14:18 +08:00
|
|
|
else {
|
2007-12-05 03:39:02 +08:00
|
|
|
const char* p = Buffer->getBufferStart();
|
|
|
|
const char* e = Buffer->getBufferEnd();
|
|
|
|
|
2007-12-05 08:14:18 +08:00
|
|
|
S.EmitInt(e-p);
|
|
|
|
|
2007-12-05 03:39:02 +08:00
|
|
|
for ( ; p != e; ++p)
|
2007-12-05 08:14:18 +08:00
|
|
|
S.EmitInt(*p);
|
|
|
|
}
|
|
|
|
|
|
|
|
S.FlushRecord();
|
|
|
|
}
|
2007-12-05 03:39:02 +08:00
|
|
|
|
2007-12-05 08:14:18 +08:00
|
|
|
void ContentCache::ReadToSourceManager(llvm::Deserializer& D,
|
|
|
|
SourceManager& SMgr,
|
|
|
|
FileManager* FMgr,
|
|
|
|
std::vector<char>& Buf) {
|
|
|
|
if (FMgr) {
|
|
|
|
llvm::SerializedPtrID PtrID = D.ReadPtrID();
|
|
|
|
D.ReadCStr(Buf,false);
|
|
|
|
|
|
|
|
// Create/fetch the FileEntry.
|
|
|
|
const char* start = &Buf[0];
|
|
|
|
const FileEntry* E = FMgr->getFile(start,start+Buf.size());
|
|
|
|
|
2007-12-14 02:12:10 +08:00
|
|
|
// FIXME: Ideally we want a lazy materialization of the ContentCache
|
|
|
|
// anyway, because we don't want to read in source files unless this
|
|
|
|
// is absolutely needed.
|
|
|
|
if (!E)
|
|
|
|
D.RegisterPtr(PtrID,NULL);
|
2008-09-29 08:25:48 +08:00
|
|
|
else
|
2007-12-14 02:12:10 +08:00
|
|
|
// Get the ContextCache object and register it with the deserializer.
|
2009-01-26 08:43:02 +08:00
|
|
|
D.RegisterPtr(PtrID, SMgr.getOrCreateContentCache(E));
|
|
|
|
return;
|
2007-12-05 03:39:02 +08:00
|
|
|
}
|
2009-01-26 08:43:02 +08:00
|
|
|
|
|
|
|
// Register the ContextCache object with the deserializer.
|
|
|
|
SMgr.MemBufferInfos.push_back(ContentCache());
|
|
|
|
ContentCache& Entry = const_cast<ContentCache&>(SMgr.MemBufferInfos.back());
|
|
|
|
D.RegisterPtr(&Entry);
|
|
|
|
|
|
|
|
// Create the buffer.
|
|
|
|
unsigned Size = D.ReadInt();
|
|
|
|
Entry.Buffer = MemoryBuffer::getNewUninitMemBuffer(Size);
|
|
|
|
|
|
|
|
// Read the contents of the buffer.
|
|
|
|
char* p = const_cast<char*>(Entry.Buffer->getBufferStart());
|
|
|
|
for (unsigned i = 0; i < Size ; ++i)
|
|
|
|
p[i] = D.ReadInt();
|
2007-12-05 03:39:02 +08:00
|
|
|
}
|
|
|
|
|
2007-12-05 08:14:18 +08:00
|
|
|
void SourceManager::Emit(llvm::Serializer& S) const {
|
2007-12-05 08:19:51 +08:00
|
|
|
S.EnterBlock();
|
|
|
|
S.EmitPtr(this);
|
2009-01-17 14:22:33 +08:00
|
|
|
S.EmitInt(MainFileID.getOpaqueValue());
|
2007-12-05 08:19:51 +08:00
|
|
|
|
2007-12-05 08:14:18 +08:00
|
|
|
// Emit: FileInfos. Just emit the file name.
|
|
|
|
S.EnterBlock();
|
|
|
|
|
|
|
|
std::for_each(FileInfos.begin(),FileInfos.end(),
|
|
|
|
S.MakeEmitter<ContentCache>());
|
2007-12-05 03:39:02 +08:00
|
|
|
|
2007-12-05 08:14:18 +08:00
|
|
|
S.ExitBlock();
|
2007-12-05 03:39:02 +08:00
|
|
|
|
2007-12-05 08:14:18 +08:00
|
|
|
// Emit: MemBufferInfos
|
|
|
|
S.EnterBlock();
|
|
|
|
|
|
|
|
std::for_each(MemBufferInfos.begin(), MemBufferInfos.end(),
|
|
|
|
S.MakeEmitter<ContentCache>());
|
|
|
|
|
|
|
|
S.ExitBlock();
|
|
|
|
|
2009-01-26 08:43:02 +08:00
|
|
|
// FIXME: Emit SLocEntryTable.
|
2007-12-05 08:19:51 +08:00
|
|
|
|
|
|
|
S.ExitBlock();
|
2007-12-05 03:39:02 +08:00
|
|
|
}
|
2007-12-05 08:14:18 +08:00
|
|
|
|
2007-12-05 08:19:51 +08:00
|
|
|
SourceManager*
|
|
|
|
SourceManager::CreateAndRegister(llvm::Deserializer& D, FileManager& FMgr){
|
|
|
|
SourceManager *M = new SourceManager();
|
|
|
|
D.RegisterPtr(M);
|
|
|
|
|
2007-12-20 06:29:55 +08:00
|
|
|
// Read: the FileID of the main source file of the translation unit.
|
2009-01-26 08:43:02 +08:00
|
|
|
M->MainFileID = FileID::get(D.ReadInt());
|
2007-12-20 06:29:55 +08:00
|
|
|
|
2007-12-05 08:14:18 +08:00
|
|
|
std::vector<char> Buf;
|
|
|
|
|
|
|
|
{ // Read: FileInfos.
|
|
|
|
llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
|
|
|
|
while (!D.FinishedBlock(BLoc))
|
2007-12-05 08:19:51 +08:00
|
|
|
ContentCache::ReadToSourceManager(D,*M,&FMgr,Buf);
|
2007-12-05 08:14:18 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
{ // Read: MemBufferInfos.
|
|
|
|
llvm::Deserializer::Location BLoc = D.getCurrentBlockLocation();
|
|
|
|
while (!D.FinishedBlock(BLoc))
|
2007-12-05 08:19:51 +08:00
|
|
|
ContentCache::ReadToSourceManager(D,*M,NULL,Buf);
|
2007-12-05 08:14:18 +08:00
|
|
|
}
|
|
|
|
|
2009-01-26 08:43:02 +08:00
|
|
|
// FIXME: Read SLocEntryTable.
|
2007-12-05 08:19:51 +08:00
|
|
|
|
|
|
|
return M;
|
2007-12-11 02:01:25 +08:00
|
|
|
}
|