clang API Documentation
00001 //===--- CacheTokens.cpp - Caching of lexer tokens for PTH support --------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This provides a possible implementation of PTH support for Clang that is 00011 // based on caching lexed tokens and identifiers. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "clang/Frontend/Utils.h" 00016 #include "clang/Basic/Diagnostic.h" 00017 #include "clang/Basic/FileManager.h" 00018 #include "clang/Basic/FileSystemStatCache.h" 00019 #include "clang/Basic/IdentifierTable.h" 00020 #include "clang/Basic/OnDiskHashTable.h" 00021 #include "clang/Basic/SourceManager.h" 00022 #include "clang/Lex/Lexer.h" 00023 #include "clang/Lex/Preprocessor.h" 00024 #include "llvm/ADT/StringExtras.h" 00025 #include "llvm/ADT/StringMap.h" 00026 #include "llvm/Support/FileSystem.h" 00027 #include "llvm/Support/MemoryBuffer.h" 00028 #include "llvm/Support/raw_ostream.h" 00029 #include "llvm/Support/Path.h" 00030 00031 // FIXME: put this somewhere else? 00032 #ifndef S_ISDIR 00033 #define S_ISDIR(x) (((x)&_S_IFDIR)!=0) 00034 #endif 00035 00036 using namespace clang; 00037 using namespace clang::io; 00038 00039 //===----------------------------------------------------------------------===// 00040 // PTH-specific stuff. 00041 //===----------------------------------------------------------------------===// 00042 00043 namespace { 00044 class PTHEntry { 00045 Offset TokenData, PPCondData; 00046 00047 public: 00048 PTHEntry() {} 00049 00050 PTHEntry(Offset td, Offset ppcd) 00051 : TokenData(td), PPCondData(ppcd) {} 00052 00053 Offset getTokenOffset() const { return TokenData; } 00054 Offset getPPCondTableOffset() const { return PPCondData; } 00055 }; 00056 00057 00058 class PTHEntryKeyVariant { 00059 union { const FileEntry* FE; const char* Path; }; 00060 enum { IsFE = 0x1, IsDE = 0x2, IsNoExist = 0x0 } Kind; 00061 struct stat *StatBuf; 00062 public: 00063 PTHEntryKeyVariant(const FileEntry *fe) 00064 : FE(fe), Kind(IsFE), StatBuf(0) {} 00065 00066 PTHEntryKeyVariant(struct stat* statbuf, const char* path) 00067 : Path(path), Kind(IsDE), StatBuf(new struct stat(*statbuf)) {} 00068 00069 explicit PTHEntryKeyVariant(const char* path) 00070 : Path(path), Kind(IsNoExist), StatBuf(0) {} 00071 00072 bool isFile() const { return Kind == IsFE; } 00073 00074 StringRef getString() const { 00075 return Kind == IsFE ? FE->getName() : Path; 00076 } 00077 00078 unsigned getKind() const { return (unsigned) Kind; } 00079 00080 void EmitData(raw_ostream& Out) { 00081 switch (Kind) { 00082 case IsFE: 00083 // Emit stat information. 00084 ::Emit32(Out, FE->getInode()); 00085 ::Emit32(Out, FE->getDevice()); 00086 ::Emit16(Out, FE->getFileMode()); 00087 ::Emit64(Out, FE->getModificationTime()); 00088 ::Emit64(Out, FE->getSize()); 00089 break; 00090 case IsDE: 00091 // Emit stat information. 00092 ::Emit32(Out, (uint32_t) StatBuf->st_ino); 00093 ::Emit32(Out, (uint32_t) StatBuf->st_dev); 00094 ::Emit16(Out, (uint16_t) StatBuf->st_mode); 00095 ::Emit64(Out, (uint64_t) StatBuf->st_mtime); 00096 ::Emit64(Out, (uint64_t) StatBuf->st_size); 00097 delete StatBuf; 00098 break; 00099 default: 00100 break; 00101 } 00102 } 00103 00104 unsigned getRepresentationLength() const { 00105 return Kind == IsNoExist ? 0 : 4 + 4 + 2 + 8 + 8; 00106 } 00107 }; 00108 00109 class FileEntryPTHEntryInfo { 00110 public: 00111 typedef PTHEntryKeyVariant key_type; 00112 typedef key_type key_type_ref; 00113 00114 typedef PTHEntry data_type; 00115 typedef const PTHEntry& data_type_ref; 00116 00117 static unsigned ComputeHash(PTHEntryKeyVariant V) { 00118 return llvm::HashString(V.getString()); 00119 } 00120 00121 static std::pair<unsigned,unsigned> 00122 EmitKeyDataLength(raw_ostream& Out, PTHEntryKeyVariant V, 00123 const PTHEntry& E) { 00124 00125 unsigned n = V.getString().size() + 1 + 1; 00126 ::Emit16(Out, n); 00127 00128 unsigned m = V.getRepresentationLength() + (V.isFile() ? 4 + 4 : 0); 00129 ::Emit8(Out, m); 00130 00131 return std::make_pair(n, m); 00132 } 00133 00134 static void EmitKey(raw_ostream& Out, PTHEntryKeyVariant V, unsigned n){ 00135 // Emit the entry kind. 00136 ::Emit8(Out, (unsigned) V.getKind()); 00137 // Emit the string. 00138 Out.write(V.getString().data(), n - 1); 00139 } 00140 00141 static void EmitData(raw_ostream& Out, PTHEntryKeyVariant V, 00142 const PTHEntry& E, unsigned) { 00143 00144 00145 // For file entries emit the offsets into the PTH file for token data 00146 // and the preprocessor blocks table. 00147 if (V.isFile()) { 00148 ::Emit32(Out, E.getTokenOffset()); 00149 ::Emit32(Out, E.getPPCondTableOffset()); 00150 } 00151 00152 // Emit any other data associated with the key (i.e., stat information). 00153 V.EmitData(Out); 00154 } 00155 }; 00156 00157 class OffsetOpt { 00158 bool valid; 00159 Offset off; 00160 public: 00161 OffsetOpt() : valid(false) {} 00162 bool hasOffset() const { return valid; } 00163 Offset getOffset() const { assert(valid); return off; } 00164 void setOffset(Offset o) { off = o; valid = true; } 00165 }; 00166 } // end anonymous namespace 00167 00168 typedef OnDiskChainedHashTableGenerator<FileEntryPTHEntryInfo> PTHMap; 00169 00170 namespace { 00171 class PTHWriter { 00172 typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap; 00173 typedef llvm::StringMap<OffsetOpt, llvm::BumpPtrAllocator> CachedStrsTy; 00174 00175 IDMap IM; 00176 llvm::raw_fd_ostream& Out; 00177 Preprocessor& PP; 00178 uint32_t idcount; 00179 PTHMap PM; 00180 CachedStrsTy CachedStrs; 00181 Offset CurStrOffset; 00182 std::vector<llvm::StringMapEntry<OffsetOpt>*> StrEntries; 00183 00184 //// Get the persistent id for the given IdentifierInfo*. 00185 uint32_t ResolveID(const IdentifierInfo* II); 00186 00187 /// Emit a token to the PTH file. 00188 void EmitToken(const Token& T); 00189 00190 void Emit8(uint32_t V) { ::Emit8(Out, V); } 00191 00192 void Emit16(uint32_t V) { ::Emit16(Out, V); } 00193 00194 void Emit32(uint32_t V) { ::Emit32(Out, V); } 00195 00196 void EmitBuf(const char *Ptr, unsigned NumBytes) { 00197 Out.write(Ptr, NumBytes); 00198 } 00199 00200 void EmitString(StringRef V) { 00201 ::Emit16(Out, V.size()); 00202 EmitBuf(V.data(), V.size()); 00203 } 00204 00205 /// EmitIdentifierTable - Emits two tables to the PTH file. The first is 00206 /// a hashtable mapping from identifier strings to persistent IDs. 00207 /// The second is a straight table mapping from persistent IDs to string data 00208 /// (the keys of the first table). 00209 std::pair<Offset, Offset> EmitIdentifierTable(); 00210 00211 /// EmitFileTable - Emit a table mapping from file name strings to PTH 00212 /// token data. 00213 Offset EmitFileTable() { return PM.Emit(Out); } 00214 00215 PTHEntry LexTokens(Lexer& L); 00216 Offset EmitCachedSpellings(); 00217 00218 public: 00219 PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp) 00220 : Out(out), PP(pp), idcount(0), CurStrOffset(0) {} 00221 00222 PTHMap &getPM() { return PM; } 00223 void GeneratePTH(const std::string &MainFile); 00224 }; 00225 } // end anonymous namespace 00226 00227 uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) { 00228 // Null IdentifierInfo's map to the persistent ID 0. 00229 if (!II) 00230 return 0; 00231 00232 IDMap::iterator I = IM.find(II); 00233 if (I != IM.end()) 00234 return I->second; // We've already added 1. 00235 00236 IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL. 00237 return idcount; 00238 } 00239 00240 void PTHWriter::EmitToken(const Token& T) { 00241 // Emit the token kind, flags, and length. 00242 Emit32(((uint32_t) T.getKind()) | ((((uint32_t) T.getFlags())) << 8)| 00243 (((uint32_t) T.getLength()) << 16)); 00244 00245 if (!T.isLiteral()) { 00246 Emit32(ResolveID(T.getIdentifierInfo())); 00247 } else { 00248 // We cache *un-cleaned* spellings. This gives us 100% fidelity with the 00249 // source code. 00250 StringRef s(T.getLiteralData(), T.getLength()); 00251 00252 // Get the string entry. 00253 llvm::StringMapEntry<OffsetOpt> *E = &CachedStrs.GetOrCreateValue(s); 00254 00255 // If this is a new string entry, bump the PTH offset. 00256 if (!E->getValue().hasOffset()) { 00257 E->getValue().setOffset(CurStrOffset); 00258 StrEntries.push_back(E); 00259 CurStrOffset += s.size() + 1; 00260 } 00261 00262 // Emit the relative offset into the PTH file for the spelling string. 00263 Emit32(E->getValue().getOffset()); 00264 } 00265 00266 // Emit the offset into the original source file of this token so that we 00267 // can reconstruct its SourceLocation. 00268 Emit32(PP.getSourceManager().getFileOffset(T.getLocation())); 00269 } 00270 00271 PTHEntry PTHWriter::LexTokens(Lexer& L) { 00272 // Pad 0's so that we emit tokens to a 4-byte alignment. 00273 // This speed up reading them back in. 00274 Pad(Out, 4); 00275 Offset TokenOff = (Offset) Out.tell(); 00276 00277 // Keep track of matching '#if' ... '#endif'. 00278 typedef std::vector<std::pair<Offset, unsigned> > PPCondTable; 00279 PPCondTable PPCond; 00280 std::vector<unsigned> PPStartCond; 00281 bool ParsingPreprocessorDirective = false; 00282 Token Tok; 00283 00284 do { 00285 L.LexFromRawLexer(Tok); 00286 NextToken: 00287 00288 if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) && 00289 ParsingPreprocessorDirective) { 00290 // Insert an eod token into the token cache. It has the same 00291 // position as the next token that is not on the same line as the 00292 // preprocessor directive. Observe that we continue processing 00293 // 'Tok' when we exit this branch. 00294 Token Tmp = Tok; 00295 Tmp.setKind(tok::eod); 00296 Tmp.clearFlag(Token::StartOfLine); 00297 Tmp.setIdentifierInfo(0); 00298 EmitToken(Tmp); 00299 ParsingPreprocessorDirective = false; 00300 } 00301 00302 if (Tok.is(tok::raw_identifier)) { 00303 PP.LookUpIdentifierInfo(Tok); 00304 EmitToken(Tok); 00305 continue; 00306 } 00307 00308 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) { 00309 // Special processing for #include. Store the '#' token and lex 00310 // the next token. 00311 assert(!ParsingPreprocessorDirective); 00312 Offset HashOff = (Offset) Out.tell(); 00313 00314 // Get the next token. 00315 Token NextTok; 00316 L.LexFromRawLexer(NextTok); 00317 00318 // If we see the start of line, then we had a null directive "#". In 00319 // this case, discard both tokens. 00320 if (NextTok.isAtStartOfLine()) 00321 goto NextToken; 00322 00323 // The token is the start of a directive. Emit it. 00324 EmitToken(Tok); 00325 Tok = NextTok; 00326 00327 // Did we see 'include'/'import'/'include_next'? 00328 if (Tok.isNot(tok::raw_identifier)) { 00329 EmitToken(Tok); 00330 continue; 00331 } 00332 00333 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok); 00334 tok::PPKeywordKind K = II->getPPKeywordID(); 00335 00336 ParsingPreprocessorDirective = true; 00337 00338 switch (K) { 00339 case tok::pp_not_keyword: 00340 // Invalid directives "#foo" can occur in #if 0 blocks etc, just pass 00341 // them through. 00342 default: 00343 break; 00344 00345 case tok::pp_include: 00346 case tok::pp_import: 00347 case tok::pp_include_next: { 00348 // Save the 'include' token. 00349 EmitToken(Tok); 00350 // Lex the next token as an include string. 00351 L.setParsingPreprocessorDirective(true); 00352 L.LexIncludeFilename(Tok); 00353 L.setParsingPreprocessorDirective(false); 00354 assert(!Tok.isAtStartOfLine()); 00355 if (Tok.is(tok::raw_identifier)) 00356 PP.LookUpIdentifierInfo(Tok); 00357 00358 break; 00359 } 00360 case tok::pp_if: 00361 case tok::pp_ifdef: 00362 case tok::pp_ifndef: { 00363 // Add an entry for '#if' and friends. We initially set the target 00364 // index to 0. This will get backpatched when we hit #endif. 00365 PPStartCond.push_back(PPCond.size()); 00366 PPCond.push_back(std::make_pair(HashOff, 0U)); 00367 break; 00368 } 00369 case tok::pp_endif: { 00370 // Add an entry for '#endif'. We set the target table index to itself. 00371 // This will later be set to zero when emitting to the PTH file. We 00372 // use 0 for uninitialized indices because that is easier to debug. 00373 unsigned index = PPCond.size(); 00374 // Backpatch the opening '#if' entry. 00375 assert(!PPStartCond.empty()); 00376 assert(PPCond.size() > PPStartCond.back()); 00377 assert(PPCond[PPStartCond.back()].second == 0); 00378 PPCond[PPStartCond.back()].second = index; 00379 PPStartCond.pop_back(); 00380 // Add the new entry to PPCond. 00381 PPCond.push_back(std::make_pair(HashOff, index)); 00382 EmitToken(Tok); 00383 00384 // Some files have gibberish on the same line as '#endif'. 00385 // Discard these tokens. 00386 do 00387 L.LexFromRawLexer(Tok); 00388 while (Tok.isNot(tok::eof) && !Tok.isAtStartOfLine()); 00389 // We have the next token in hand. 00390 // Don't immediately lex the next one. 00391 goto NextToken; 00392 } 00393 case tok::pp_elif: 00394 case tok::pp_else: { 00395 // Add an entry for #elif or #else. 00396 // This serves as both a closing and opening of a conditional block. 00397 // This means that its entry will get backpatched later. 00398 unsigned index = PPCond.size(); 00399 // Backpatch the previous '#if' entry. 00400 assert(!PPStartCond.empty()); 00401 assert(PPCond.size() > PPStartCond.back()); 00402 assert(PPCond[PPStartCond.back()].second == 0); 00403 PPCond[PPStartCond.back()].second = index; 00404 PPStartCond.pop_back(); 00405 // Now add '#elif' as a new block opening. 00406 PPCond.push_back(std::make_pair(HashOff, 0U)); 00407 PPStartCond.push_back(index); 00408 break; 00409 } 00410 } 00411 } 00412 00413 EmitToken(Tok); 00414 } 00415 while (Tok.isNot(tok::eof)); 00416 00417 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals."); 00418 00419 // Next write out PPCond. 00420 Offset PPCondOff = (Offset) Out.tell(); 00421 00422 // Write out the size of PPCond so that clients can identifer empty tables. 00423 Emit32(PPCond.size()); 00424 00425 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) { 00426 Emit32(PPCond[i].first - TokenOff); 00427 uint32_t x = PPCond[i].second; 00428 assert(x != 0 && "PPCond entry not backpatched."); 00429 // Emit zero for #endifs. This allows us to do checking when 00430 // we read the PTH file back in. 00431 Emit32(x == i ? 0 : x); 00432 } 00433 00434 return PTHEntry(TokenOff, PPCondOff); 00435 } 00436 00437 Offset PTHWriter::EmitCachedSpellings() { 00438 // Write each cached strings to the PTH file. 00439 Offset SpellingsOff = Out.tell(); 00440 00441 for (std::vector<llvm::StringMapEntry<OffsetOpt>*>::iterator 00442 I = StrEntries.begin(), E = StrEntries.end(); I!=E; ++I) 00443 EmitBuf((*I)->getKeyData(), (*I)->getKeyLength()+1 /*nul included*/); 00444 00445 return SpellingsOff; 00446 } 00447 00448 void PTHWriter::GeneratePTH(const std::string &MainFile) { 00449 // Generate the prologue. 00450 Out << "cfe-pth"; 00451 Emit32(PTHManager::Version); 00452 00453 // Leave 4 words for the prologue. 00454 Offset PrologueOffset = Out.tell(); 00455 for (unsigned i = 0; i < 4; ++i) 00456 Emit32(0); 00457 00458 // Write the name of the MainFile. 00459 if (!MainFile.empty()) { 00460 EmitString(MainFile); 00461 } else { 00462 // String with 0 bytes. 00463 Emit16(0); 00464 } 00465 Emit8(0); 00466 00467 // Iterate over all the files in SourceManager. Create a lexer 00468 // for each file and cache the tokens. 00469 SourceManager &SM = PP.getSourceManager(); 00470 const LangOptions &LOpts = PP.getLangOpts(); 00471 00472 for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(), 00473 E = SM.fileinfo_end(); I != E; ++I) { 00474 const SrcMgr::ContentCache &C = *I->second; 00475 const FileEntry *FE = C.OrigEntry; 00476 00477 // FIXME: Handle files with non-absolute paths. 00478 if (llvm::sys::path::is_relative(FE->getName())) 00479 continue; 00480 00481 const llvm::MemoryBuffer *B = C.getBuffer(PP.getDiagnostics(), SM); 00482 if (!B) continue; 00483 00484 FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User); 00485 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID); 00486 Lexer L(FID, FromFile, SM, LOpts); 00487 PM.insert(FE, LexTokens(L)); 00488 } 00489 00490 // Write out the identifier table. 00491 const std::pair<Offset,Offset> &IdTableOff = EmitIdentifierTable(); 00492 00493 // Write out the cached strings table. 00494 Offset SpellingOff = EmitCachedSpellings(); 00495 00496 // Write out the file table. 00497 Offset FileTableOff = EmitFileTable(); 00498 00499 // Finally, write the prologue. 00500 Out.seek(PrologueOffset); 00501 Emit32(IdTableOff.first); 00502 Emit32(IdTableOff.second); 00503 Emit32(FileTableOff); 00504 Emit32(SpellingOff); 00505 } 00506 00507 namespace { 00508 /// StatListener - A simple "interpose" object used to monitor stat calls 00509 /// invoked by FileManager while processing the original sources used 00510 /// as input to PTH generation. StatListener populates the PTHWriter's 00511 /// file map with stat information for directories as well as negative stats. 00512 /// Stat information for files are populated elsewhere. 00513 class StatListener : public FileSystemStatCache { 00514 PTHMap &PM; 00515 public: 00516 StatListener(PTHMap &pm) : PM(pm) {} 00517 ~StatListener() {} 00518 00519 LookupResult getStat(const char *Path, struct stat &StatBuf, 00520 int *FileDescriptor) { 00521 LookupResult Result = statChained(Path, StatBuf, FileDescriptor); 00522 00523 if (Result == CacheMissing) // Failed 'stat'. 00524 PM.insert(PTHEntryKeyVariant(Path), PTHEntry()); 00525 else if (S_ISDIR(StatBuf.st_mode)) { 00526 // Only cache directories with absolute paths. 00527 if (llvm::sys::path::is_relative(Path)) 00528 return Result; 00529 00530 PM.insert(PTHEntryKeyVariant(&StatBuf, Path), PTHEntry()); 00531 } 00532 00533 return Result; 00534 } 00535 }; 00536 } // end anonymous namespace 00537 00538 00539 void clang::CacheTokens(Preprocessor &PP, llvm::raw_fd_ostream* OS) { 00540 // Get the name of the main file. 00541 const SourceManager &SrcMgr = PP.getSourceManager(); 00542 const FileEntry *MainFile = SrcMgr.getFileEntryForID(SrcMgr.getMainFileID()); 00543 SmallString<128> MainFilePath(MainFile->getName()); 00544 00545 llvm::sys::fs::make_absolute(MainFilePath); 00546 00547 // Create the PTHWriter. 00548 PTHWriter PW(*OS, PP); 00549 00550 // Install the 'stat' system call listener in the FileManager. 00551 StatListener *StatCache = new StatListener(PW.getPM()); 00552 PP.getFileManager().addStatCache(StatCache, /*AtBeginning=*/true); 00553 00554 // Lex through the entire file. This will populate SourceManager with 00555 // all of the header information. 00556 Token Tok; 00557 PP.EnterMainSourceFile(); 00558 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof)); 00559 00560 // Generate the PTH file. 00561 PP.getFileManager().removeStatCache(StatCache); 00562 PW.GeneratePTH(MainFilePath.str()); 00563 } 00564 00565 //===----------------------------------------------------------------------===// 00566 00567 namespace { 00568 class PTHIdKey { 00569 public: 00570 const IdentifierInfo* II; 00571 uint32_t FileOffset; 00572 }; 00573 00574 class PTHIdentifierTableTrait { 00575 public: 00576 typedef PTHIdKey* key_type; 00577 typedef key_type key_type_ref; 00578 00579 typedef uint32_t data_type; 00580 typedef data_type data_type_ref; 00581 00582 static unsigned ComputeHash(PTHIdKey* key) { 00583 return llvm::HashString(key->II->getName()); 00584 } 00585 00586 static std::pair<unsigned,unsigned> 00587 EmitKeyDataLength(raw_ostream& Out, const PTHIdKey* key, uint32_t) { 00588 unsigned n = key->II->getLength() + 1; 00589 ::Emit16(Out, n); 00590 return std::make_pair(n, sizeof(uint32_t)); 00591 } 00592 00593 static void EmitKey(raw_ostream& Out, PTHIdKey* key, unsigned n) { 00594 // Record the location of the key data. This is used when generating 00595 // the mapping from persistent IDs to strings. 00596 key->FileOffset = Out.tell(); 00597 Out.write(key->II->getNameStart(), n); 00598 } 00599 00600 static void EmitData(raw_ostream& Out, PTHIdKey*, uint32_t pID, 00601 unsigned) { 00602 ::Emit32(Out, pID); 00603 } 00604 }; 00605 } // end anonymous namespace 00606 00607 /// EmitIdentifierTable - Emits two tables to the PTH file. The first is 00608 /// a hashtable mapping from identifier strings to persistent IDs. The second 00609 /// is a straight table mapping from persistent IDs to string data (the 00610 /// keys of the first table). 00611 /// 00612 std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() { 00613 // Build two maps: 00614 // (1) an inverse map from persistent IDs -> (IdentifierInfo*,Offset) 00615 // (2) a map from (IdentifierInfo*, Offset)* -> persistent IDs 00616 00617 // Note that we use 'calloc', so all the bytes are 0. 00618 PTHIdKey *IIDMap = (PTHIdKey*)calloc(idcount, sizeof(PTHIdKey)); 00619 00620 // Create the hashtable. 00621 OnDiskChainedHashTableGenerator<PTHIdentifierTableTrait> IIOffMap; 00622 00623 // Generate mapping from persistent IDs -> IdentifierInfo*. 00624 for (IDMap::iterator I = IM.begin(), E = IM.end(); I != E; ++I) { 00625 // Decrement by 1 because we are using a vector for the lookup and 00626 // 0 is reserved for NULL. 00627 assert(I->second > 0); 00628 assert(I->second-1 < idcount); 00629 unsigned idx = I->second-1; 00630 00631 // Store the mapping from persistent ID to IdentifierInfo* 00632 IIDMap[idx].II = I->first; 00633 00634 // Store the reverse mapping in a hashtable. 00635 IIOffMap.insert(&IIDMap[idx], I->second); 00636 } 00637 00638 // Write out the inverse map first. This causes the PCIDKey entries to 00639 // record PTH file offsets for the string data. This is used to write 00640 // the second table. 00641 Offset StringTableOffset = IIOffMap.Emit(Out); 00642 00643 // Now emit the table mapping from persistent IDs to PTH file offsets. 00644 Offset IDOff = Out.tell(); 00645 Emit32(idcount); // Emit the number of identifiers. 00646 for (unsigned i = 0 ; i < idcount; ++i) 00647 Emit32(IIDMap[i].FileOffset); 00648 00649 // Finally, release the inverse map. 00650 free(IIDMap); 00651 00652 return std::make_pair(IDOff, StringTableOffset); 00653 }