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-09-12 23:39:04 +08:00
|
|
|
#include "llvm/Config/config.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>
|
2007-07-13 00:00:00 +08:00
|
|
|
#include <fcntl.h>
|
2006-06-18 13:43:12 +08:00
|
|
|
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
|
|
|
|
2007-10-31 05:08:08 +08:00
|
|
|
ContentCache::~ContentCache() {
|
|
|
|
delete Buffer;
|
|
|
|
delete [] SourceLineCache;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
2007-04-29 14:44:41 +08:00
|
|
|
// FIXME: REMOVE THESE
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/types.h>
|
2007-10-16 17:09:44 +08:00
|
|
|
#if !defined(_MSC_VER) && !defined(__MINGW32__)
|
2007-04-29 14:44:41 +08:00
|
|
|
#include <sys/uio.h>
|
|
|
|
#include <sys/fcntl.h>
|
2007-09-04 02:24:56 +08:00
|
|
|
#else
|
|
|
|
#include <io.h>
|
|
|
|
#endif
|
2007-04-29 14:44:41 +08:00
|
|
|
#include <cerrno>
|
|
|
|
|
2007-04-29 15:12:06 +08:00
|
|
|
static const MemoryBuffer *ReadFileFast(const FileEntry *FileEnt) {
|
2007-04-29 14:44:41 +08:00
|
|
|
#if 0
|
|
|
|
// FIXME: Reintroduce this and zap this function once the common llvm stuff
|
|
|
|
// is fast for the small case.
|
2007-04-29 15:12:06 +08:00
|
|
|
return MemoryBuffer::getFile(FileEnt->getName(), strlen(FileEnt->getName()),
|
2007-04-29 14:44:41 +08:00
|
|
|
FileEnt->getSize());
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// If the file is larger than some threshold, use 'read', otherwise use mmap.
|
2008-04-01 02:44:59 +08:00
|
|
|
if (FileEnt->getSize() >= 4096*12)
|
2007-04-29 15:12:06 +08:00
|
|
|
return MemoryBuffer::getFile(FileEnt->getName(), strlen(FileEnt->getName()),
|
2007-05-07 07:34:12 +08:00
|
|
|
0, FileEnt->getSize());
|
2007-04-29 14:44:41 +08:00
|
|
|
|
2007-04-29 15:12:06 +08:00
|
|
|
MemoryBuffer *SB = MemoryBuffer::getNewUninitMemBuffer(FileEnt->getSize(),
|
2007-04-29 14:44:41 +08:00
|
|
|
FileEnt->getName());
|
|
|
|
char *BufPtr = const_cast<char*>(SB->getBufferStart());
|
2007-09-04 02:24:56 +08:00
|
|
|
|
2007-09-12 23:39:04 +08:00
|
|
|
#if defined(LLVM_ON_WIN32)
|
2007-09-04 02:24:56 +08:00
|
|
|
int FD = ::open(FileEnt->getName(), O_RDONLY|O_BINARY);
|
|
|
|
#else
|
2007-04-29 14:44:41 +08:00
|
|
|
int FD = ::open(FileEnt->getName(), O_RDONLY);
|
2007-09-04 02:24:56 +08:00
|
|
|
#endif
|
2007-04-29 14:44:41 +08:00
|
|
|
if (FD == -1) {
|
|
|
|
delete SB;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
unsigned BytesLeft = FileEnt->getSize();
|
|
|
|
while (BytesLeft) {
|
|
|
|
ssize_t NumRead = ::read(FD, BufPtr, BytesLeft);
|
|
|
|
if (NumRead != -1) {
|
|
|
|
BytesLeft -= NumRead;
|
|
|
|
BufPtr += NumRead;
|
|
|
|
} else if (errno == EINTR) {
|
|
|
|
// try again
|
|
|
|
} else {
|
|
|
|
// error reading.
|
|
|
|
close(FD);
|
|
|
|
delete SB;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
close(FD);
|
|
|
|
|
|
|
|
return SB;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// getFileInfo - Create or return a cached FileInfo for the specified file.
|
|
|
|
///
|
2007-10-31 05:08:08 +08:00
|
|
|
const ContentCache* SourceManager::getContentCache(const FileEntry *FileEnt) {
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
assert(FileEnt && "Didn't specify a file entry to use?");
|
|
|
|
// 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;
|
|
|
|
|
|
|
|
// Nope, get information.
|
2007-04-29 15:12:06 +08:00
|
|
|
const MemoryBuffer *File = ReadFileFast(FileEnt);
|
2007-04-29 14:08:57 +08:00
|
|
|
if (File == 0)
|
2006-06-18 13:43:12 +08:00
|
|
|
return 0;
|
|
|
|
|
2007-10-31 05:08:08 +08:00
|
|
|
ContentCache& Entry = const_cast<ContentCache&>(*FileInfos.insert(I,FileEnt));
|
2006-06-18 13:43:12 +08:00
|
|
|
|
2007-10-31 05:08:08 +08:00
|
|
|
Entry.Buffer = File;
|
|
|
|
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());
|
|
|
|
Entry.Buffer = Buffer;
|
|
|
|
return &Entry;
|
2006-06-18 13:43:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2007-10-31 06:57:35 +08:00
|
|
|
/// createFileID - Create a new fileID for the specified ContentCache and
|
|
|
|
/// include position. This works regardless of whether the ContentCache
|
|
|
|
/// corresponds to a file or some other input source.
|
2007-10-31 05:08:08 +08:00
|
|
|
unsigned SourceManager::createFileID(const ContentCache *File,
|
2006-06-18 13:43:12 +08:00
|
|
|
SourceLocation IncludePos) {
|
|
|
|
// If FileEnt is really large (e.g. it's a large .i file), we may not be able
|
|
|
|
// to fit an arbitrary position in the file in the FilePos field. To handle
|
|
|
|
// this, we create one FileID for each chunk of the file that fits in a
|
|
|
|
// FilePos field.
|
2007-10-31 05:08:08 +08:00
|
|
|
unsigned FileSize = File->Buffer->getBufferSize();
|
2006-06-18 13:43:12 +08:00
|
|
|
if (FileSize+1 < (1 << SourceLocation::FilePosBits)) {
|
2007-07-21 00:37:10 +08:00
|
|
|
FileIDs.push_back(FileIDInfo::get(IncludePos, 0, File));
|
2006-10-22 14:33:42 +08:00
|
|
|
assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) &&
|
|
|
|
"Ran out of file ID's!");
|
2006-06-18 13:43:12 +08:00
|
|
|
return FileIDs.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create one FileID for each chunk of the file.
|
|
|
|
unsigned Result = FileIDs.size()+1;
|
|
|
|
|
|
|
|
unsigned ChunkNo = 0;
|
|
|
|
while (1) {
|
2007-07-21 00:37:10 +08:00
|
|
|
FileIDs.push_back(FileIDInfo::get(IncludePos, ChunkNo++, File));
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
if (FileSize+1 < (1 << SourceLocation::FilePosBits)) break;
|
|
|
|
FileSize -= (1 << SourceLocation::FilePosBits);
|
|
|
|
}
|
|
|
|
|
2006-10-22 14:33:42 +08:00
|
|
|
assert(FileIDs.size() < (1 << SourceLocation::FileIDBits) &&
|
|
|
|
"Ran out of file ID's!");
|
2006-06-18 13:43:12 +08:00
|
|
|
return Result;
|
|
|
|
}
|
|
|
|
|
2006-06-30 14:10:08 +08:00
|
|
|
/// getInstantiationLoc - Return a new SourceLocation that encodes the fact
|
|
|
|
/// that a token from physloc PhysLoc should actually be referenced from
|
|
|
|
/// InstantiationLoc.
|
2007-07-21 02:00:12 +08:00
|
|
|
SourceLocation SourceManager::getInstantiationLoc(SourceLocation PhysLoc,
|
2006-06-30 14:10:08 +08:00
|
|
|
SourceLocation InstantLoc) {
|
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.
|
2007-07-21 02:00:12 +08:00
|
|
|
PhysLoc = getPhysicalLoc(PhysLoc);
|
2006-07-17 02:05:08 +08:00
|
|
|
|
2006-06-30 14:15:08 +08:00
|
|
|
// Resolve InstantLoc down to a real logical location.
|
|
|
|
InstantLoc = getLogicalLoc(InstantLoc);
|
2006-07-20 14:48:52 +08:00
|
|
|
|
2007-07-21 02:00:12 +08:00
|
|
|
|
|
|
|
// If the last macro id is close to the currently requested location, try to
|
2007-08-02 11:55:37 +08:00
|
|
|
// reuse it. This implements a small cache.
|
|
|
|
for (int i = MacroIDs.size()-1, e = MacroIDs.size()-6; i >= 0 && i != e; --i){
|
|
|
|
MacroIDInfo &LastOne = MacroIDs[i];
|
2007-07-21 14:41:57 +08:00
|
|
|
|
2007-08-02 11:55:37 +08:00
|
|
|
// The instanitation point and source physloc have to exactly match to reuse
|
|
|
|
// (for now). We could allow "nearby" instantiations in the future.
|
2007-11-10 07:59:17 +08:00
|
|
|
if (LastOne.getVirtualLoc() != InstantLoc ||
|
2007-08-02 11:55:37 +08:00
|
|
|
LastOne.getPhysicalLoc().getFileID() != PhysLoc.getFileID())
|
|
|
|
continue;
|
|
|
|
|
|
|
|
// Check to see if the physloc of the token came from near enough to reuse.
|
|
|
|
int PhysDelta = PhysLoc.getRawFilePos() -
|
|
|
|
LastOne.getPhysicalLoc().getRawFilePos();
|
|
|
|
if (SourceLocation::isValidMacroPhysOffs(PhysDelta))
|
2008-02-03 16:24:13 +08:00
|
|
|
return SourceLocation::getMacroLoc(i, PhysDelta);
|
2007-07-21 02:00:12 +08:00
|
|
|
}
|
|
|
|
|
2007-07-21 02:26:45 +08:00
|
|
|
|
2007-07-21 00:37:10 +08:00
|
|
|
MacroIDs.push_back(MacroIDInfo::get(InstantLoc, PhysLoc));
|
2008-02-03 16:24:13 +08:00
|
|
|
return SourceLocation::getMacroLoc(MacroIDs.size()-1, 0);
|
2006-06-30 14:10:08 +08:00
|
|
|
}
|
|
|
|
|
2007-10-12 02:38:32 +08:00
|
|
|
/// getBufferData - Return a pointer to the start and end of the character
|
|
|
|
/// data for the specified FileID.
|
|
|
|
std::pair<const char*, const char*>
|
|
|
|
SourceManager::getBufferData(unsigned FileID) const {
|
|
|
|
const llvm::MemoryBuffer *Buf = getBuffer(FileID);
|
|
|
|
return std::make_pair(Buf->getBufferStart(), Buf->getBufferEnd());
|
|
|
|
}
|
2006-06-30 14:10:08 +08:00
|
|
|
|
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.
|
2007-07-21 00:37:10 +08:00
|
|
|
SL = getPhysicalLoc(SL);
|
2006-07-05 07:01:03 +08:00
|
|
|
|
2007-10-31 05:08:08 +08:00
|
|
|
return getContentCache(SL.getFileID())->Buffer->getBufferStart() +
|
2007-07-21 00:37:10 +08:00
|
|
|
getFullFilePos(SL);
|
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 {
|
|
|
|
unsigned FileID = Loc.getFileID();
|
2006-06-18 13:43:12 +08:00
|
|
|
if (FileID == 0) return 0;
|
2006-06-21 11:01:55 +08:00
|
|
|
|
2007-07-21 00:37:10 +08:00
|
|
|
unsigned FilePos = getFullFilePos(Loc);
|
2007-04-29 15:12:06 +08:00
|
|
|
const MemoryBuffer *Buffer = getBuffer(FileID);
|
2006-06-18 13:43:12 +08:00
|
|
|
const char *Buf = Buffer->getBufferStart();
|
|
|
|
|
|
|
|
unsigned LineStart = FilePos;
|
|
|
|
while (LineStart && Buf[LineStart-1] != '\n' && Buf[LineStart-1] != '\r')
|
|
|
|
--LineStart;
|
|
|
|
return FilePos-LineStart+1;
|
|
|
|
}
|
|
|
|
|
2006-06-21 12:57:09 +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.
|
2007-08-30 13:59:30 +08:00
|
|
|
const char *SourceManager::getSourceName(SourceLocation Loc) const {
|
2006-06-21 12:57:09 +08:00
|
|
|
unsigned FileID = Loc.getFileID();
|
|
|
|
if (FileID == 0) return "";
|
2007-10-31 05:08:08 +08:00
|
|
|
return getContentCache(FileID)->Buffer->getBufferIdentifier();
|
2006-06-21 12:57:09 +08:00
|
|
|
}
|
|
|
|
|
2007-10-31 05:08:08 +08:00
|
|
|
static void ComputeLineNumbers(ContentCache* FI) DISABLE_INLINE;
|
|
|
|
static void ComputeLineNumbers(ContentCache* FI) {
|
2007-07-24 13:57:19 +08:00
|
|
|
const MemoryBuffer *Buffer = FI->Buffer;
|
|
|
|
|
|
|
|
// 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
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// getLineNumber - Given a SourceLocation, return the physical line number
|
|
|
|
/// 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.
|
2006-06-21 12:57:09 +08:00
|
|
|
unsigned SourceManager::getLineNumber(SourceLocation Loc) {
|
2006-07-03 04:07:52 +08:00
|
|
|
unsigned FileID = Loc.getFileID();
|
|
|
|
if (FileID == 0) return 0;
|
2007-10-31 05:08:08 +08:00
|
|
|
|
|
|
|
ContentCache* Content;
|
2007-07-24 13:57:19 +08:00
|
|
|
|
|
|
|
if (LastLineNoFileIDQuery == FileID)
|
2007-10-31 05:08:08 +08:00
|
|
|
Content = LastLineNoContentCache;
|
2007-07-24 13:57:19 +08:00
|
|
|
else
|
2007-10-31 05:08:08 +08:00
|
|
|
Content = const_cast<ContentCache*>(getContentCache(FileID));
|
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
|
|
|
|
|
|
|
unsigned QueriedFilePos = getFullFilePos(Loc)+1;
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
if (LastLineNoFileIDQuery == FileID) {
|
|
|
|
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;
|
|
|
|
|
|
|
|
LastLineNoFileIDQuery = FileID;
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/// 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()
|
|
|
|
<< " mem buffers mapped, " << FileIDs.size()
|
|
|
|
<< " file ID's allocated.\n";
|
|
|
|
llvm::cerr << " " << FileIDs.size() << " normal buffer FileID's, "
|
|
|
|
<< MacroIDs.size() << " macro expansion FileID's.\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;
|
|
|
|
NumFileBytesMapped += I->Buffer->getBufferSize();
|
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";
|
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);
|
|
|
|
else
|
|
|
|
// Get the ContextCache object and register it with the deserializer.
|
|
|
|
D.RegisterPtr(PtrID,SMgr.getContentCache(E));
|
2007-12-05 03:39:02 +08:00
|
|
|
}
|
2007-12-05 08:14:18 +08:00
|
|
|
else {
|
|
|
|
// 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();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FileIDInfo::Emit(llvm::Serializer& S) const {
|
|
|
|
S.Emit(IncludeLoc);
|
|
|
|
S.EmitInt(ChunkNo);
|
|
|
|
S.EmitPtr(Content);
|
|
|
|
}
|
|
|
|
|
|
|
|
FileIDInfo FileIDInfo::ReadVal(llvm::Deserializer& D) {
|
|
|
|
FileIDInfo I;
|
|
|
|
I.IncludeLoc = SourceLocation::ReadVal(D);
|
|
|
|
I.ChunkNo = D.ReadInt();
|
|
|
|
D.ReadPtr(I.Content,false);
|
|
|
|
return I;
|
|
|
|
}
|
|
|
|
|
|
|
|
void MacroIDInfo::Emit(llvm::Serializer& S) const {
|
|
|
|
S.Emit(VirtualLoc);
|
|
|
|
S.Emit(PhysicalLoc);
|
|
|
|
}
|
|
|
|
|
|
|
|
MacroIDInfo MacroIDInfo::ReadVal(llvm::Deserializer& D) {
|
|
|
|
MacroIDInfo I;
|
|
|
|
I.VirtualLoc = SourceLocation::ReadVal(D);
|
|
|
|
I.PhysicalLoc = SourceLocation::ReadVal(D);
|
|
|
|
return I;
|
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);
|
2007-12-20 06:29:55 +08:00
|
|
|
S.EmitInt(MainFileID);
|
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();
|
|
|
|
|
|
|
|
// Emit: FileIDs
|
|
|
|
S.EmitInt(FileIDs.size());
|
|
|
|
std::for_each(FileIDs.begin(), FileIDs.end(), S.MakeEmitter<FileIDInfo>());
|
|
|
|
|
|
|
|
// Emit: MacroIDs
|
|
|
|
S.EmitInt(MacroIDs.size());
|
|
|
|
std::for_each(MacroIDs.begin(), MacroIDs.end(), S.MakeEmitter<MacroIDInfo>());
|
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.
|
|
|
|
M->MainFileID = D.ReadInt();
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
// Read: FileIDs.
|
|
|
|
unsigned Size = D.ReadInt();
|
2007-12-05 08:19:51 +08:00
|
|
|
M->FileIDs.reserve(Size);
|
2007-12-05 08:14:18 +08:00
|
|
|
for (; Size > 0 ; --Size)
|
2007-12-05 08:19:51 +08:00
|
|
|
M->FileIDs.push_back(FileIDInfo::ReadVal(D));
|
2007-12-05 08:14:18 +08:00
|
|
|
|
|
|
|
// Read: MacroIDs.
|
|
|
|
Size = D.ReadInt();
|
2007-12-05 08:19:51 +08:00
|
|
|
M->MacroIDs.reserve(Size);
|
2007-12-05 08:14:18 +08:00
|
|
|
for (; Size > 0 ; --Size)
|
2007-12-05 08:19:51 +08:00
|
|
|
M->MacroIDs.push_back(MacroIDInfo::ReadVal(D));
|
|
|
|
|
|
|
|
return M;
|
2007-12-11 02:01:25 +08:00
|
|
|
}
|