various cleanups, no functionality change

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@67883 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chris Lattner 2009-03-28 00:16:20 +00:00
Родитель 1ae0afaf14
Коммит f2390367a0
1 изменённых файлов: 28 добавлений и 33 удалений

Просмотреть файл

@ -66,7 +66,8 @@ static void Emit64(llvm::raw_ostream& Out, uint64_t V) {
static void Pad(llvm::raw_fd_ostream& Out, unsigned A) { static void Pad(llvm::raw_fd_ostream& Out, unsigned A) {
Offset off = (Offset) Out.tell(); Offset off = (Offset) Out.tell();
uint32_t n = ((uintptr_t)(off+A-1) & ~(uintptr_t)(A-1)) - off; uint32_t n = ((uintptr_t)(off+A-1) & ~(uintptr_t)(A-1)) - off;
for ( ; n ; --n ) Emit8(Out, 0); for (; n ; --n)
Emit8(Out, 0);
} }
// Bernstein hash function: // Bernstein hash function:
@ -357,8 +358,8 @@ class VISIBILITY_HIDDEN PTHWriter {
void Emit32(uint32_t V) { ::Emit32(Out, V); } void Emit32(uint32_t V) { ::Emit32(Out, V); }
void EmitBuf(const char* I, const char* E) { void EmitBuf(const char *Ptr, unsigned NumBytes) {
for ( ; I != E ; ++I) Out << *I; Out.write(Ptr, NumBytes);
} }
/// EmitIdentifierTable - Emits two tables to the PTH file. The first is /// EmitIdentifierTable - Emits two tables to the PTH file. The first is
@ -420,13 +421,11 @@ uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) {
return 0; return 0;
IDMap::iterator I = IM.find(II); IDMap::iterator I = IM.find(II);
if (I != IM.end())
if (I == IM.end()) { return I->second; // We've already added 1.
IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL.
return idcount; IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL.
} return idcount;
return I->second; // We've already added 1.
} }
void PTHWriter::EmitToken(const Token& T) { void PTHWriter::EmitToken(const Token& T) {
@ -434,7 +433,9 @@ void PTHWriter::EmitToken(const Token& T) {
Emit32(((uint32_t) T.getKind()) | ((((uint32_t) T.getFlags())) << 8)| Emit32(((uint32_t) T.getKind()) | ((((uint32_t) T.getFlags())) << 8)|
(((uint32_t) T.getLength()) << 16)); (((uint32_t) T.getLength()) << 16));
if (T.isLiteral()) { if (!T.isLiteral()) {
Emit32(ResolveID(T.getIdentifierInfo()));
} else {
// We cache *un-cleaned* spellings. This gives us 100% fidelity with the // We cache *un-cleaned* spellings. This gives us 100% fidelity with the
// source code. // source code.
const char* s = T.getLiteralData(); const char* s = T.getLiteralData();
@ -453,8 +454,6 @@ void PTHWriter::EmitToken(const Token& T) {
// Emit the relative offset into the PTH file for the spelling string. // Emit the relative offset into the PTH file for the spelling string.
Emit32(E->getValue().getOffset()); Emit32(E->getValue().getOffset());
} }
else
Emit32(ResolveID(T.getIdentifierInfo()));
// Emit the offset into the original source file of this token so that we // Emit the offset into the original source file of this token so that we
// can reconstruct its SourceLocation. // can reconstruct its SourceLocation.
@ -622,12 +621,8 @@ Offset PTHWriter::EmitCachedSpellings() {
Offset SpellingsOff = Out.tell(); Offset SpellingsOff = Out.tell();
for (std::vector<llvm::StringMapEntry<OffsetOpt>*>::iterator for (std::vector<llvm::StringMapEntry<OffsetOpt>*>::iterator
I = StrEntries.begin(), E = StrEntries.end(); I!=E; ++I) { I = StrEntries.begin(), E = StrEntries.end(); I!=E; ++I)
EmitBuf((*I)->getKeyData(), (*I)->getKeyLength()+1 /*nul included*/);
const char* data = (*I)->getKeyData();
EmitBuf(data, data + (*I)->getKeyLength());
Emit8('\0');
}
return SpellingsOff; return SpellingsOff;
} }
@ -639,14 +634,14 @@ void PTHWriter::GeneratePTH(const std::string *MainFile) {
// Leave 4 words for the prologue. // Leave 4 words for the prologue.
Offset PrologueOffset = Out.tell(); Offset PrologueOffset = Out.tell();
for (unsigned i = 0; i < 4 * sizeof(uint32_t); ++i) Emit8(0); for (unsigned i = 0; i < 4; ++i)
Emit32(0);
// Write the name of the MainFile. // Write the name of the MainFile.
if (MainFile && MainFile->length() > 0) { if (MainFile && !MainFile->empty()) {
Emit16(MainFile->length()); Emit16(MainFile->length());
EmitBuf(&((*MainFile)[0]), &((*MainFile)[0]) + MainFile->length()); EmitBuf(MainFile->data(), MainFile->length());
} } else {
else {
// String with 0 bytes. // String with 0 bytes.
Emit16(0); Emit16(0);
} }
@ -676,7 +671,7 @@ void PTHWriter::GeneratePTH(const std::string *MainFile) {
} }
// Write out the identifier table. // Write out the identifier table.
const std::pair<Offset,Offset>& IdTableOff = EmitIdentifierTable(); const std::pair<Offset,Offset> &IdTableOff = EmitIdentifierTable();
// Write out the cached strings table. // Write out the cached strings table.
Offset SpellingOff = EmitCachedSpellings(); Offset SpellingOff = EmitCachedSpellings();
@ -692,7 +687,7 @@ void PTHWriter::GeneratePTH(const std::string *MainFile) {
Emit32(SpellingOff); Emit32(SpellingOff);
} }
void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) { void clang::CacheTokens(Preprocessor &PP, const std::string &OutFile) {
// Open up the PTH file. // Open up the PTH file.
std::string ErrMsg; std::string ErrMsg;
llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg); llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg);
@ -712,8 +707,7 @@ void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) {
llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory(); llvm::sys::Path P = llvm::sys::Path::GetCurrentDirectory();
P.appendComponent(MainFilePath.toString()); P.appendComponent(MainFilePath.toString());
MainFileName = P.toString(); MainFileName = P.toString();
} } else {
else {
MainFileName = MainFilePath.toString(); MainFileName = MainFilePath.toString();
} }
@ -736,13 +730,13 @@ void clang::CacheTokens(Preprocessor& PP, const std::string& OutFile) {
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
namespace { class PTHIdKey {
class VISIBILITY_HIDDEN PTHIdKey {
public: public:
const IdentifierInfo* II; const IdentifierInfo* II;
uint32_t FileOffset; uint32_t FileOffset;
}; };
namespace {
class VISIBILITY_HIDDEN PTHIdentifierTableTrait { class VISIBILITY_HIDDEN PTHIdentifierTableTrait {
public: public:
typedef PTHIdKey* key_type; typedef PTHIdKey* key_type;
@ -787,13 +781,13 @@ std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() {
// (2) a map from (IdentifierInfo*, Offset)* -> persistent IDs // (2) a map from (IdentifierInfo*, Offset)* -> persistent IDs
// Note that we use 'calloc', so all the bytes are 0. // Note that we use 'calloc', so all the bytes are 0.
PTHIdKey* IIDMap = (PTHIdKey*) calloc(idcount, sizeof(PTHIdKey)); PTHIdKey *IIDMap = (PTHIdKey*)calloc(idcount, sizeof(PTHIdKey));
// Create the hashtable. // Create the hashtable.
OnDiskChainedHashTableGenerator<PTHIdentifierTableTrait> IIOffMap; OnDiskChainedHashTableGenerator<PTHIdentifierTableTrait> IIOffMap;
// Generate mapping from persistent IDs -> IdentifierInfo*. // Generate mapping from persistent IDs -> IdentifierInfo*.
for (IDMap::iterator I=IM.begin(), E=IM.end(); I!=E; ++I) { for (IDMap::iterator I = IM.begin(), E = IM.end(); I != E; ++I) {
// Decrement by 1 because we are using a vector for the lookup and // Decrement by 1 because we are using a vector for the lookup and
// 0 is reserved for NULL. // 0 is reserved for NULL.
assert(I->second > 0); assert(I->second > 0);
@ -815,7 +809,8 @@ std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() {
// Now emit the table mapping from persistent IDs to PTH file offsets. // Now emit the table mapping from persistent IDs to PTH file offsets.
Offset IDOff = Out.tell(); Offset IDOff = Out.tell();
Emit32(idcount); // Emit the number of identifiers. Emit32(idcount); // Emit the number of identifiers.
for (unsigned i = 0 ; i < idcount; ++i) Emit32(IIDMap[i].FileOffset); for (unsigned i = 0 ; i < idcount; ++i)
Emit32(IIDMap[i].FileOffset);
// Finally, release the inverse map. // Finally, release the inverse map.
free(IIDMap); free(IIDMap);