1 //===--- CacheTokens.cpp - Caching of lexer tokens for PTH support --------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This provides a possible implementation of PTH support for Clang that is
11 // based on caching lexed tokens and identifiers.
13 //===----------------------------------------------------------------------===//
15 #include "clang/Frontend/Utils.h"
16 #include "clang/Basic/Diagnostic.h"
17 #include "clang/Basic/FileManager.h"
18 #include "clang/Basic/FileSystemStatCache.h"
19 #include "clang/Basic/IdentifierTable.h"
20 #include "clang/Basic/SourceManager.h"
21 #include "clang/Lex/Lexer.h"
22 #include "clang/Lex/Preprocessor.h"
23 #include "llvm/ADT/StringExtras.h"
24 #include "llvm/ADT/StringMap.h"
25 #include "llvm/Support/EndianStream.h"
26 #include "llvm/Support/FileSystem.h"
27 #include "llvm/Support/MemoryBuffer.h"
28 #include "llvm/Support/OnDiskHashTable.h"
29 #include "llvm/Support/Path.h"
30 #include "llvm/Support/raw_ostream.h"
32 // FIXME: put this somewhere else?
34 #define S_ISDIR(x) (((x)&_S_IFDIR)!=0)
37 using namespace clang;
39 //===----------------------------------------------------------------------===//
40 // PTH-specific stuff.
41 //===----------------------------------------------------------------------===//
43 typedef uint32_t Offset;
47 Offset TokenData, PPCondData;
52 PTHEntry(Offset td, Offset ppcd)
53 : TokenData(td), PPCondData(ppcd) {}
55 Offset getTokenOffset() const { return TokenData; }
56 Offset getPPCondTableOffset() const { return PPCondData; }
60 class PTHEntryKeyVariant {
61 union { const FileEntry* FE; const char* Path; };
62 enum { IsFE = 0x1, IsDE = 0x2, IsNoExist = 0x0 } Kind;
66 PTHEntryKeyVariant(const FileEntry *fe) : FE(fe), Kind(IsFE), Data(nullptr) {}
68 PTHEntryKeyVariant(FileData *Data, const char *path)
69 : Path(path), Kind(IsDE), Data(new FileData(*Data)) {}
71 explicit PTHEntryKeyVariant(const char *path)
72 : Path(path), Kind(IsNoExist), Data(nullptr) {}
74 bool isFile() const { return Kind == IsFE; }
76 StringRef getString() const {
77 return Kind == IsFE ? FE->getName() : Path;
80 unsigned getKind() const { return (unsigned) Kind; }
82 void EmitData(raw_ostream& Out) {
83 using namespace llvm::support;
84 endian::Writer<little> LE(Out);
87 // Emit stat information.
88 llvm::sys::fs::UniqueID UID = FE->getUniqueID();
89 LE.write<uint64_t>(UID.getFile());
90 LE.write<uint64_t>(UID.getDevice());
91 LE.write<uint64_t>(FE->getModificationTime());
92 LE.write<uint64_t>(FE->getSize());
95 // Emit stat information.
96 LE.write<uint64_t>(Data->UniqueID.getFile());
97 LE.write<uint64_t>(Data->UniqueID.getDevice());
98 LE.write<uint64_t>(Data->ModTime);
99 LE.write<uint64_t>(Data->Size);
107 unsigned getRepresentationLength() const {
108 return Kind == IsNoExist ? 0 : 4 + 4 + 2 + 8 + 8;
112 class FileEntryPTHEntryInfo {
114 typedef PTHEntryKeyVariant key_type;
115 typedef key_type key_type_ref;
117 typedef PTHEntry data_type;
118 typedef const PTHEntry& data_type_ref;
120 typedef unsigned hash_value_type;
121 typedef unsigned offset_type;
123 static hash_value_type ComputeHash(PTHEntryKeyVariant V) {
124 return llvm::HashString(V.getString());
127 static std::pair<unsigned,unsigned>
128 EmitKeyDataLength(raw_ostream& Out, PTHEntryKeyVariant V,
130 using namespace llvm::support;
131 endian::Writer<little> LE(Out);
133 unsigned n = V.getString().size() + 1 + 1;
134 LE.write<uint16_t>(n);
136 unsigned m = V.getRepresentationLength() + (V.isFile() ? 4 + 4 : 0);
137 LE.write<uint8_t>(m);
139 return std::make_pair(n, m);
142 static void EmitKey(raw_ostream& Out, PTHEntryKeyVariant V, unsigned n){
143 using namespace llvm::support;
144 // Emit the entry kind.
145 endian::Writer<little>(Out).write<uint8_t>((unsigned)V.getKind());
147 Out.write(V.getString().data(), n - 1);
150 static void EmitData(raw_ostream& Out, PTHEntryKeyVariant V,
151 const PTHEntry& E, unsigned) {
152 using namespace llvm::support;
153 endian::Writer<little> LE(Out);
155 // For file entries emit the offsets into the PTH file for token data
156 // and the preprocessor blocks table.
158 LE.write<uint32_t>(E.getTokenOffset());
159 LE.write<uint32_t>(E.getPPCondTableOffset());
162 // Emit any other data associated with the key (i.e., stat information).
171 OffsetOpt() : valid(false) {}
172 bool hasOffset() const { return valid; }
173 Offset getOffset() const { assert(valid); return off; }
174 void setOffset(Offset o) { off = o; valid = true; }
176 } // end anonymous namespace
178 typedef llvm::OnDiskChainedHashTableGenerator<FileEntryPTHEntryInfo> PTHMap;
182 typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
183 typedef llvm::StringMap<OffsetOpt, llvm::BumpPtrAllocator> CachedStrsTy;
186 llvm::raw_fd_ostream& Out;
190 CachedStrsTy CachedStrs;
192 std::vector<llvm::StringMapEntry<OffsetOpt>*> StrEntries;
194 //// Get the persistent id for the given IdentifierInfo*.
195 uint32_t ResolveID(const IdentifierInfo* II);
197 /// Emit a token to the PTH file.
198 void EmitToken(const Token& T);
200 void Emit8(uint32_t V) {
201 using namespace llvm::support;
202 endian::Writer<little>(Out).write<uint8_t>(V);
205 void Emit16(uint32_t V) {
206 using namespace llvm::support;
207 endian::Writer<little>(Out).write<uint16_t>(V);
210 void Emit32(uint32_t V) {
211 using namespace llvm::support;
212 endian::Writer<little>(Out).write<uint32_t>(V);
215 void EmitBuf(const char *Ptr, unsigned NumBytes) {
216 Out.write(Ptr, NumBytes);
219 void EmitString(StringRef V) {
220 using namespace llvm::support;
221 endian::Writer<little>(Out).write<uint16_t>(V.size());
222 EmitBuf(V.data(), V.size());
225 /// EmitIdentifierTable - Emits two tables to the PTH file. The first is
226 /// a hashtable mapping from identifier strings to persistent IDs.
227 /// The second is a straight table mapping from persistent IDs to string data
228 /// (the keys of the first table).
229 std::pair<Offset, Offset> EmitIdentifierTable();
231 /// EmitFileTable - Emit a table mapping from file name strings to PTH
233 Offset EmitFileTable() { return PM.Emit(Out); }
235 PTHEntry LexTokens(Lexer& L);
236 Offset EmitCachedSpellings();
239 PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp)
240 : Out(out), PP(pp), idcount(0), CurStrOffset(0) {}
242 PTHMap &getPM() { return PM; }
243 void GeneratePTH(const std::string &MainFile);
245 } // end anonymous namespace
247 uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) {
248 // Null IdentifierInfo's map to the persistent ID 0.
252 IDMap::iterator I = IM.find(II);
254 return I->second; // We've already added 1.
256 IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL.
260 void PTHWriter::EmitToken(const Token& T) {
261 // Emit the token kind, flags, and length.
262 Emit32(((uint32_t) T.getKind()) | ((((uint32_t) T.getFlags())) << 8)|
263 (((uint32_t) T.getLength()) << 16));
265 if (!T.isLiteral()) {
266 Emit32(ResolveID(T.getIdentifierInfo()));
268 // We cache *un-cleaned* spellings. This gives us 100% fidelity with the
270 StringRef s(T.getLiteralData(), T.getLength());
272 // Get the string entry.
273 auto &E = *CachedStrs.insert(std::make_pair(s, OffsetOpt())).first;
275 // If this is a new string entry, bump the PTH offset.
276 if (!E.second.hasOffset()) {
277 E.second.setOffset(CurStrOffset);
278 StrEntries.push_back(&E);
279 CurStrOffset += s.size() + 1;
282 // Emit the relative offset into the PTH file for the spelling string.
283 Emit32(E.second.getOffset());
286 // Emit the offset into the original source file of this token so that we
287 // can reconstruct its SourceLocation.
288 Emit32(PP.getSourceManager().getFileOffset(T.getLocation()));
291 PTHEntry PTHWriter::LexTokens(Lexer& L) {
292 // Pad 0's so that we emit tokens to a 4-byte alignment.
293 // This speed up reading them back in.
294 using namespace llvm::support;
295 endian::Writer<little> LE(Out);
296 uint32_t TokenOff = Out.tell();
297 for (uint64_t N = llvm::OffsetToAlignment(TokenOff, 4); N; --N, ++TokenOff)
298 LE.write<uint8_t>(0);
300 // Keep track of matching '#if' ... '#endif'.
301 typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
303 std::vector<unsigned> PPStartCond;
304 bool ParsingPreprocessorDirective = false;
308 L.LexFromRawLexer(Tok);
311 if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
312 ParsingPreprocessorDirective) {
313 // Insert an eod token into the token cache. It has the same
314 // position as the next token that is not on the same line as the
315 // preprocessor directive. Observe that we continue processing
316 // 'Tok' when we exit this branch.
318 Tmp.setKind(tok::eod);
319 Tmp.clearFlag(Token::StartOfLine);
320 Tmp.setIdentifierInfo(nullptr);
322 ParsingPreprocessorDirective = false;
325 if (Tok.is(tok::raw_identifier)) {
326 PP.LookUpIdentifierInfo(Tok);
331 if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
332 // Special processing for #include. Store the '#' token and lex
334 assert(!ParsingPreprocessorDirective);
335 Offset HashOff = (Offset) Out.tell();
337 // Get the next token.
339 L.LexFromRawLexer(NextTok);
341 // If we see the start of line, then we had a null directive "#". In
342 // this case, discard both tokens.
343 if (NextTok.isAtStartOfLine())
346 // The token is the start of a directive. Emit it.
350 // Did we see 'include'/'import'/'include_next'?
351 if (Tok.isNot(tok::raw_identifier)) {
356 IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
357 tok::PPKeywordKind K = II->getPPKeywordID();
359 ParsingPreprocessorDirective = true;
362 case tok::pp_not_keyword:
363 // Invalid directives "#foo" can occur in #if 0 blocks etc, just pass
368 case tok::pp_include:
370 case tok::pp_include_next: {
371 // Save the 'include' token.
373 // Lex the next token as an include string.
374 L.setParsingPreprocessorDirective(true);
375 L.LexIncludeFilename(Tok);
376 L.setParsingPreprocessorDirective(false);
377 assert(!Tok.isAtStartOfLine());
378 if (Tok.is(tok::raw_identifier))
379 PP.LookUpIdentifierInfo(Tok);
385 case tok::pp_ifndef: {
386 // Add an entry for '#if' and friends. We initially set the target
387 // index to 0. This will get backpatched when we hit #endif.
388 PPStartCond.push_back(PPCond.size());
389 PPCond.push_back(std::make_pair(HashOff, 0U));
392 case tok::pp_endif: {
393 // Add an entry for '#endif'. We set the target table index to itself.
394 // This will later be set to zero when emitting to the PTH file. We
395 // use 0 for uninitialized indices because that is easier to debug.
396 unsigned index = PPCond.size();
397 // Backpatch the opening '#if' entry.
398 assert(!PPStartCond.empty());
399 assert(PPCond.size() > PPStartCond.back());
400 assert(PPCond[PPStartCond.back()].second == 0);
401 PPCond[PPStartCond.back()].second = index;
402 PPStartCond.pop_back();
403 // Add the new entry to PPCond.
404 PPCond.push_back(std::make_pair(HashOff, index));
407 // Some files have gibberish on the same line as '#endif'.
408 // Discard these tokens.
410 L.LexFromRawLexer(Tok);
411 while (Tok.isNot(tok::eof) && !Tok.isAtStartOfLine());
412 // We have the next token in hand.
413 // Don't immediately lex the next one.
418 // Add an entry for #elif or #else.
419 // This serves as both a closing and opening of a conditional block.
420 // This means that its entry will get backpatched later.
421 unsigned index = PPCond.size();
422 // Backpatch the previous '#if' entry.
423 assert(!PPStartCond.empty());
424 assert(PPCond.size() > PPStartCond.back());
425 assert(PPCond[PPStartCond.back()].second == 0);
426 PPCond[PPStartCond.back()].second = index;
427 PPStartCond.pop_back();
428 // Now add '#elif' as a new block opening.
429 PPCond.push_back(std::make_pair(HashOff, 0U));
430 PPStartCond.push_back(index);
438 while (Tok.isNot(tok::eof));
440 assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
442 // Next write out PPCond.
443 Offset PPCondOff = (Offset) Out.tell();
445 // Write out the size of PPCond so that clients can identifer empty tables.
446 Emit32(PPCond.size());
448 for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
449 Emit32(PPCond[i].first - TokenOff);
450 uint32_t x = PPCond[i].second;
451 assert(x != 0 && "PPCond entry not backpatched.");
452 // Emit zero for #endifs. This allows us to do checking when
453 // we read the PTH file back in.
454 Emit32(x == i ? 0 : x);
457 return PTHEntry(TokenOff, PPCondOff);
460 Offset PTHWriter::EmitCachedSpellings() {
461 // Write each cached strings to the PTH file.
462 Offset SpellingsOff = Out.tell();
464 for (std::vector<llvm::StringMapEntry<OffsetOpt>*>::iterator
465 I = StrEntries.begin(), E = StrEntries.end(); I!=E; ++I)
466 EmitBuf((*I)->getKeyData(), (*I)->getKeyLength()+1 /*nul included*/);
471 void PTHWriter::GeneratePTH(const std::string &MainFile) {
472 // Generate the prologue.
473 Out << "cfe-pth" << '\0';
474 Emit32(PTHManager::Version);
476 // Leave 4 words for the prologue.
477 Offset PrologueOffset = Out.tell();
478 for (unsigned i = 0; i < 4; ++i)
481 // Write the name of the MainFile.
482 if (!MainFile.empty()) {
483 EmitString(MainFile);
485 // String with 0 bytes.
490 // Iterate over all the files in SourceManager. Create a lexer
491 // for each file and cache the tokens.
492 SourceManager &SM = PP.getSourceManager();
493 const LangOptions &LOpts = PP.getLangOpts();
495 for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(),
496 E = SM.fileinfo_end(); I != E; ++I) {
497 const SrcMgr::ContentCache &C = *I->second;
498 const FileEntry *FE = C.OrigEntry;
500 // FIXME: Handle files with non-absolute paths.
501 if (llvm::sys::path::is_relative(FE->getName()))
504 const llvm::MemoryBuffer *B = C.getBuffer(PP.getDiagnostics(), SM);
507 FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
508 const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
509 Lexer L(FID, FromFile, SM, LOpts);
510 PM.insert(FE, LexTokens(L));
513 // Write out the identifier table.
514 const std::pair<Offset,Offset> &IdTableOff = EmitIdentifierTable();
516 // Write out the cached strings table.
517 Offset SpellingOff = EmitCachedSpellings();
519 // Write out the file table.
520 Offset FileTableOff = EmitFileTable();
522 // Finally, write the prologue.
523 Out.seek(PrologueOffset);
524 Emit32(IdTableOff.first);
525 Emit32(IdTableOff.second);
526 Emit32(FileTableOff);
531 /// StatListener - A simple "interpose" object used to monitor stat calls
532 /// invoked by FileManager while processing the original sources used
533 /// as input to PTH generation. StatListener populates the PTHWriter's
534 /// file map with stat information for directories as well as negative stats.
535 /// Stat information for files are populated elsewhere.
536 class StatListener : public FileSystemStatCache {
539 StatListener(PTHMap &pm) : PM(pm) {}
542 LookupResult getStat(const char *Path, FileData &Data, bool isFile,
543 std::unique_ptr<vfs::File> *F,
544 vfs::FileSystem &FS) override {
545 LookupResult Result = statChained(Path, Data, isFile, F, FS);
547 if (Result == CacheMissing) // Failed 'stat'.
548 PM.insert(PTHEntryKeyVariant(Path), PTHEntry());
549 else if (Data.IsDirectory) {
550 // Only cache directories with absolute paths.
551 if (llvm::sys::path::is_relative(Path))
554 PM.insert(PTHEntryKeyVariant(&Data, Path), PTHEntry());
560 } // end anonymous namespace
563 void clang::CacheTokens(Preprocessor &PP, llvm::raw_fd_ostream* OS) {
564 // Get the name of the main file.
565 const SourceManager &SrcMgr = PP.getSourceManager();
566 const FileEntry *MainFile = SrcMgr.getFileEntryForID(SrcMgr.getMainFileID());
567 SmallString<128> MainFilePath(MainFile->getName());
569 llvm::sys::fs::make_absolute(MainFilePath);
571 // Create the PTHWriter.
572 PTHWriter PW(*OS, PP);
574 // Install the 'stat' system call listener in the FileManager.
575 auto StatCacheOwner = llvm::make_unique<StatListener>(PW.getPM());
576 StatListener *StatCache = StatCacheOwner.get();
577 PP.getFileManager().addStatCache(std::move(StatCacheOwner),
578 /*AtBeginning=*/true);
580 // Lex through the entire file. This will populate SourceManager with
581 // all of the header information.
583 PP.EnterMainSourceFile();
584 do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
586 // Generate the PTH file.
587 PP.getFileManager().removeStatCache(StatCache);
588 PW.GeneratePTH(MainFilePath.str());
591 //===----------------------------------------------------------------------===//
596 const IdentifierInfo* II;
600 class PTHIdentifierTableTrait {
602 typedef PTHIdKey* key_type;
603 typedef key_type key_type_ref;
605 typedef uint32_t data_type;
606 typedef data_type data_type_ref;
608 typedef unsigned hash_value_type;
609 typedef unsigned offset_type;
611 static hash_value_type ComputeHash(PTHIdKey* key) {
612 return llvm::HashString(key->II->getName());
615 static std::pair<unsigned,unsigned>
616 EmitKeyDataLength(raw_ostream& Out, const PTHIdKey* key, uint32_t) {
617 using namespace llvm::support;
618 unsigned n = key->II->getLength() + 1;
619 endian::Writer<little>(Out).write<uint16_t>(n);
620 return std::make_pair(n, sizeof(uint32_t));
623 static void EmitKey(raw_ostream& Out, PTHIdKey* key, unsigned n) {
624 // Record the location of the key data. This is used when generating
625 // the mapping from persistent IDs to strings.
626 key->FileOffset = Out.tell();
627 Out.write(key->II->getNameStart(), n);
630 static void EmitData(raw_ostream& Out, PTHIdKey*, uint32_t pID,
632 using namespace llvm::support;
633 endian::Writer<little>(Out).write<uint32_t>(pID);
636 } // end anonymous namespace
638 /// EmitIdentifierTable - Emits two tables to the PTH file. The first is
639 /// a hashtable mapping from identifier strings to persistent IDs. The second
640 /// is a straight table mapping from persistent IDs to string data (the
641 /// keys of the first table).
643 std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() {
645 // (1) an inverse map from persistent IDs -> (IdentifierInfo*,Offset)
646 // (2) a map from (IdentifierInfo*, Offset)* -> persistent IDs
648 // Note that we use 'calloc', so all the bytes are 0.
649 PTHIdKey *IIDMap = (PTHIdKey*)calloc(idcount, sizeof(PTHIdKey));
651 // Create the hashtable.
652 llvm::OnDiskChainedHashTableGenerator<PTHIdentifierTableTrait> IIOffMap;
654 // Generate mapping from persistent IDs -> IdentifierInfo*.
655 for (IDMap::iterator I = IM.begin(), E = IM.end(); I != E; ++I) {
656 // Decrement by 1 because we are using a vector for the lookup and
657 // 0 is reserved for NULL.
658 assert(I->second > 0);
659 assert(I->second-1 < idcount);
660 unsigned idx = I->second-1;
662 // Store the mapping from persistent ID to IdentifierInfo*
663 IIDMap[idx].II = I->first;
665 // Store the reverse mapping in a hashtable.
666 IIOffMap.insert(&IIDMap[idx], I->second);
669 // Write out the inverse map first. This causes the PCIDKey entries to
670 // record PTH file offsets for the string data. This is used to write
672 Offset StringTableOffset = IIOffMap.Emit(Out);
674 // Now emit the table mapping from persistent IDs to PTH file offsets.
675 Offset IDOff = Out.tell();
676 Emit32(idcount); // Emit the number of identifiers.
677 for (unsigned i = 0 ; i < idcount; ++i)
678 Emit32(IIDMap[i].FileOffset);
680 // Finally, release the inverse map.
683 return std::make_pair(IDOff, StringTableOffset);