2006-06-18 13:43:12 +08:00
|
|
|
//===--- SourceManager.cpp - Track and cache source files -----------------===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file was developed by Chris Lattner and is distributed under
|
|
|
|
// the University of Illinois Open Source License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the SourceManager interface.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "clang/Basic/SourceManager.h"
|
|
|
|
#include "clang/Basic/FileManager.h"
|
|
|
|
#include "clang/Basic/SourceBuffer.h"
|
|
|
|
#include "llvm/System/Path.h"
|
|
|
|
#include <algorithm>
|
|
|
|
#include <iostream>
|
|
|
|
using namespace llvm;
|
|
|
|
using namespace clang;
|
2006-06-20 13:02:40 +08:00
|
|
|
using namespace SrcMgr;
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
SourceManager::~SourceManager() {
|
|
|
|
for (std::map<const FileEntry *, FileInfo>::iterator I = FileInfos.begin(),
|
|
|
|
E = FileInfos.end(); I != E; ++I) {
|
|
|
|
delete I->second.Buffer;
|
|
|
|
delete[] I->second.SourceLineCache;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (std::list<InfoRec>::iterator I = MemBufferInfos.begin(),
|
|
|
|
E = MemBufferInfos.end(); I != E; ++I) {
|
|
|
|
delete I->second.Buffer;
|
|
|
|
delete[] I->second.SourceLineCache;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// getFileInfo - Create or return a cached FileInfo for the specified file.
|
|
|
|
///
|
2006-06-20 13:02:40 +08:00
|
|
|
const InfoRec *
|
2006-06-18 13:43:12 +08:00
|
|
|
SourceManager::getInfoRec(const FileEntry *FileEnt) {
|
|
|
|
assert(FileEnt && "Didn't specify a file entry to use?");
|
|
|
|
// Do we already have information about this file?
|
|
|
|
std::map<const FileEntry *, FileInfo>::iterator I =
|
|
|
|
FileInfos.lower_bound(FileEnt);
|
|
|
|
if (I != FileInfos.end() && I->first == FileEnt)
|
|
|
|
return &*I;
|
|
|
|
|
|
|
|
// Nope, get information.
|
2007-04-29 14:08:57 +08:00
|
|
|
const SourceBuffer *File = clang::SourceBuffer::getFile(FileEnt);
|
|
|
|
if (File == 0)
|
2006-06-18 13:43:12 +08:00
|
|
|
return 0;
|
|
|
|
|
|
|
|
const InfoRec &Entry =
|
|
|
|
*FileInfos.insert(I, std::make_pair(FileEnt, FileInfo()));
|
|
|
|
FileInfo &Info = const_cast<FileInfo &>(Entry.second);
|
|
|
|
|
|
|
|
Info.Buffer = File;
|
|
|
|
Info.SourceLineCache = 0;
|
|
|
|
Info.NumLines = 0;
|
|
|
|
return &Entry;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// createMemBufferInfoRec - Create a new info record for the specified memory
|
|
|
|
/// buffer. This does no caching.
|
2006-06-20 13:02:40 +08:00
|
|
|
const InfoRec *
|
2006-06-18 13:43:12 +08:00
|
|
|
SourceManager::createMemBufferInfoRec(const SourceBuffer *Buffer) {
|
|
|
|
// Add a new info record to the MemBufferInfos list and return it.
|
|
|
|
FileInfo FI;
|
|
|
|
FI.Buffer = Buffer;
|
|
|
|
FI.SourceLineCache = 0;
|
|
|
|
FI.NumLines = 0;
|
|
|
|
MemBufferInfos.push_back(InfoRec(0, FI));
|
|
|
|
return &MemBufferInfos.back();
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/// createFileID - Create a new fileID for the specified InfoRec and include
|
|
|
|
/// position. This works regardless of whether the InfoRec corresponds to a
|
|
|
|
/// file or some other input source.
|
|
|
|
unsigned SourceManager::createFileID(const InfoRec *File,
|
|
|
|
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.
|
|
|
|
unsigned FileSize = File->second.Buffer->getBufferSize();
|
|
|
|
if (FileSize+1 < (1 << SourceLocation::FilePosBits)) {
|
2006-06-20 13:02:40 +08:00
|
|
|
FileIDs.push_back(FileIDInfo::getNormalBuffer(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) {
|
2006-06-20 13:02:40 +08:00
|
|
|
FileIDs.push_back(FileIDInfo::getNormalBuffer(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.
|
|
|
|
SourceLocation SourceManager::getInstantiationLoc(SourceLocation PhysLoc,
|
|
|
|
SourceLocation InstantLoc) {
|
2006-07-17 02:05:08 +08:00
|
|
|
assert(getFIDInfo(PhysLoc.getFileID())->IDType !=
|
|
|
|
SrcMgr::FileIDInfo::MacroExpansion &&
|
|
|
|
"Location instantiated in a macro?");
|
|
|
|
|
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
|
|
|
|
|
|
|
unsigned InstantiationFileID;
|
|
|
|
// If this is the same instantiation as was requested last time, return this
|
|
|
|
// immediately.
|
|
|
|
if (PhysLoc.getFileID() == LastInstantiationLoc_MacroFID &&
|
|
|
|
InstantLoc == LastInstantiationLoc_InstantLoc) {
|
|
|
|
InstantiationFileID = LastInstantiationLoc_Result;
|
|
|
|
} else {
|
|
|
|
// Add a FileID for this. FIXME: should cache these!
|
|
|
|
FileIDs.push_back(FileIDInfo::getMacroExpansion(InstantLoc,
|
|
|
|
PhysLoc.getFileID()));
|
|
|
|
InstantiationFileID = FileIDs.size();
|
|
|
|
|
|
|
|
// Remember this in the single-entry cache for next time.
|
|
|
|
LastInstantiationLoc_MacroFID = PhysLoc.getFileID();
|
|
|
|
LastInstantiationLoc_InstantLoc = InstantLoc;
|
|
|
|
LastInstantiationLoc_Result = InstantiationFileID;
|
|
|
|
}
|
2006-06-30 14:15:08 +08:00
|
|
|
return SourceLocation(InstantiationFileID, PhysLoc.getRawFilePos());
|
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
|
2006-07-05 07:01:03 +08:00
|
|
|
/// in the appropriate SourceBuffer.
|
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.
|
|
|
|
unsigned FileID = SL.getFileID();
|
|
|
|
assert(FileID && "Invalid source location!");
|
|
|
|
|
|
|
|
return getFileInfo(FileID)->Buffer->getBufferStart() + getFilePos(SL);
|
2006-06-19 00:22:51 +08:00
|
|
|
}
|
|
|
|
|
2006-06-26 09:36:22 +08:00
|
|
|
/// getIncludeLoc - Return the location of the #include for the specified
|
|
|
|
/// FileID.
|
|
|
|
SourceLocation SourceManager::getIncludeLoc(unsigned FileID) const {
|
|
|
|
const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(FileID);
|
|
|
|
|
|
|
|
// For Macros, the physical loc is specified by the MacroTokenFileID.
|
2006-06-30 00:44:08 +08:00
|
|
|
if (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion)
|
2006-06-26 09:36:22 +08:00
|
|
|
FIDInfo = &FileIDs[FIDInfo->u.MacroTokenFileID-1];
|
|
|
|
|
|
|
|
return FIDInfo->IncludeLoc;
|
|
|
|
}
|
|
|
|
|
2006-06-19 00:22:51 +08:00
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// getColumnNumber - Return the column # for the specified include position.
|
|
|
|
/// 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 {
|
2006-06-30 00:44:08 +08:00
|
|
|
Loc = getLogicalLoc(Loc);
|
2006-06-21 12:57:09 +08:00
|
|
|
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
|
|
|
|
2006-06-21 12:57:09 +08:00
|
|
|
unsigned FilePos = getFilePos(Loc);
|
2006-06-21 11:01:55 +08:00
|
|
|
const SourceBuffer *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.
|
|
|
|
std::string SourceManager::getSourceName(SourceLocation Loc) {
|
2006-06-30 00:44:08 +08:00
|
|
|
Loc = getLogicalLoc(Loc);
|
2006-06-21 12:57:09 +08:00
|
|
|
unsigned FileID = Loc.getFileID();
|
|
|
|
if (FileID == 0) return "";
|
2006-06-30 00:44:08 +08:00
|
|
|
return getFileInfo(FileID)->Buffer->getBufferIdentifier();
|
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
|
|
|
|
/// line offsets for the SourceBuffer, so this is not cheap: use only when
|
|
|
|
/// about to emit a diagnostic.
|
2006-06-21 12:57:09 +08:00
|
|
|
unsigned SourceManager::getLineNumber(SourceLocation Loc) {
|
2006-06-30 00:44:08 +08:00
|
|
|
Loc = getLogicalLoc(Loc);
|
2006-07-03 04:07:52 +08:00
|
|
|
unsigned FileID = Loc.getFileID();
|
|
|
|
if (FileID == 0) return 0;
|
|
|
|
FileInfo *FileInfo = getFileInfo(FileID);
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
// If this is the first use of line information for this buffer, compute the
|
|
|
|
/// SourceLineCache for it on demand.
|
|
|
|
if (FileInfo->SourceLineCache == 0) {
|
|
|
|
const SourceBuffer *Buffer = FileInfo->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
|
2006-07-05 05:11:41 +08:00
|
|
|
// with lots of diagnostics and in -E mode.
|
2006-06-18 13:43:12 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
LineOffsets.push_back(Offs);
|
|
|
|
|
|
|
|
// Copy the offsets into the FileInfo structure.
|
|
|
|
FileInfo->NumLines = LineOffsets.size();
|
|
|
|
FileInfo->SourceLineCache = new unsigned[LineOffsets.size()];
|
|
|
|
std::copy(LineOffsets.begin(), LineOffsets.end(),
|
|
|
|
FileInfo->SourceLineCache);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Okay, we know we have a line number table. Do a binary search to find the
|
|
|
|
// line number that this character position lands on.
|
|
|
|
unsigned NumLines = FileInfo->NumLines;
|
|
|
|
unsigned *SourceLineCache = FileInfo->SourceLineCache;
|
|
|
|
|
|
|
|
// TODO: If this is performance sensitive, we could try doing simple radix
|
|
|
|
// type approaches to make good (tight?) initial guesses based on the
|
|
|
|
// assumption that all lines are the same average size.
|
|
|
|
unsigned *Pos = std::lower_bound(SourceLineCache, SourceLineCache+NumLines,
|
2006-06-21 12:57:09 +08:00
|
|
|
getFilePos(Loc)+1);
|
2006-06-18 13:43:12 +08:00
|
|
|
return Pos-SourceLineCache;
|
|
|
|
}
|
|
|
|
|
2006-06-26 09:48:23 +08:00
|
|
|
/// getSourceFilePos - This method returns the *logical* offset from the start
|
|
|
|
/// of the file that the specified SourceLocation represents. This returns
|
|
|
|
/// the location of the *logical* character data, not the physical file
|
|
|
|
/// position. In the case of macros, for example, this returns where the
|
|
|
|
/// macro was instantiated, not where the characters for the macro can be
|
|
|
|
/// found.
|
|
|
|
unsigned SourceManager::getSourceFilePos(SourceLocation Loc) const {
|
|
|
|
|
|
|
|
// If this is a macro, we need to get the instantiation location.
|
|
|
|
const SrcMgr::FileIDInfo *FIDInfo = getFIDInfo(Loc.getFileID());
|
2006-06-29 14:33:42 +08:00
|
|
|
while (FIDInfo->IDType == SrcMgr::FileIDInfo::MacroExpansion) {
|
|
|
|
Loc = FIDInfo->IncludeLoc;
|
|
|
|
FIDInfo = getFIDInfo(Loc.getFileID());
|
|
|
|
}
|
2006-06-26 09:48:23 +08:00
|
|
|
|
|
|
|
return getFilePos(Loc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
/// PrintStats - Print statistics to stderr.
|
|
|
|
///
|
|
|
|
void SourceManager::PrintStats() const {
|
|
|
|
std::cerr << "\n*** Source Manager Stats:\n";
|
|
|
|
std::cerr << FileInfos.size() << " files mapped, " << MemBufferInfos.size()
|
|
|
|
<< " mem buffers mapped, " << FileIDs.size()
|
|
|
|
<< " file ID's allocated.\n";
|
2006-06-21 11:01:55 +08:00
|
|
|
unsigned NumBuffers = 0, NumMacros = 0;
|
|
|
|
for (unsigned i = 0, e = FileIDs.size(); i != e; ++i) {
|
|
|
|
if (FileIDs[i].IDType == FileIDInfo::NormalBuffer)
|
|
|
|
++NumBuffers;
|
|
|
|
else if (FileIDs[i].IDType == FileIDInfo::MacroExpansion)
|
|
|
|
++NumMacros;
|
|
|
|
else
|
|
|
|
assert(0 && "Unknown FileID!");
|
|
|
|
}
|
|
|
|
std::cerr << " " << NumBuffers << " normal buffer FileID's, "
|
|
|
|
<< NumMacros << " macro expansion FileID's.\n";
|
|
|
|
|
|
|
|
|
2006-06-18 13:43:12 +08:00
|
|
|
|
|
|
|
unsigned NumLineNumsComputed = 0;
|
|
|
|
unsigned NumFileBytesMapped = 0;
|
|
|
|
for (std::map<const FileEntry *, FileInfo>::const_iterator I =
|
|
|
|
FileInfos.begin(), E = FileInfos.end(); I != E; ++I) {
|
|
|
|
NumLineNumsComputed += I->second.SourceLineCache != 0;
|
|
|
|
NumFileBytesMapped += I->second.Buffer->getBufferSize();
|
|
|
|
}
|
|
|
|
std::cerr << NumFileBytesMapped << " bytes of files mapped, "
|
|
|
|
<< NumLineNumsComputed << " files with line #'s computed.\n";
|
|
|
|
}
|