CacheTokens.cpp revision 263508
1//===--- CacheTokens.cpp - Caching of lexer tokens for PTH support --------===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This provides a possible implementation of PTH support for Clang that is
11// based on caching lexed tokens and identifiers.
12//
13//===----------------------------------------------------------------------===//
14
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/OnDiskHashTable.h"
21#include "clang/Basic/SourceManager.h"
22#include "clang/Lex/Lexer.h"
23#include "clang/Lex/Preprocessor.h"
24#include "llvm/ADT/StringExtras.h"
25#include "llvm/ADT/StringMap.h"
26#include "llvm/Support/FileSystem.h"
27#include "llvm/Support/MemoryBuffer.h"
28#include "llvm/Support/Path.h"
29#include "llvm/Support/raw_ostream.h"
30
31// FIXME: put this somewhere else?
32#ifndef S_ISDIR
33#define S_ISDIR(x) (((x)&_S_IFDIR)!=0)
34#endif
35
36using namespace clang;
37using namespace clang::io;
38
39//===----------------------------------------------------------------------===//
40// PTH-specific stuff.
41//===----------------------------------------------------------------------===//
42
43namespace {
44class PTHEntry {
45  Offset TokenData, PPCondData;
46
47public:
48  PTHEntry() {}
49
50  PTHEntry(Offset td, Offset ppcd)
51    : TokenData(td), PPCondData(ppcd) {}
52
53  Offset getTokenOffset() const { return TokenData; }
54  Offset getPPCondTableOffset() const { return PPCondData; }
55};
56
57
58class PTHEntryKeyVariant {
59  union { const FileEntry* FE; const char* Path; };
60  enum { IsFE = 0x1, IsDE = 0x2, IsNoExist = 0x0 } Kind;
61  FileData *Data;
62
63public:
64  PTHEntryKeyVariant(const FileEntry *fe) : FE(fe), Kind(IsFE), Data(0) {}
65
66  PTHEntryKeyVariant(FileData *Data, const char *path)
67      : Path(path), Kind(IsDE), Data(new FileData(*Data)) {}
68
69  explicit PTHEntryKeyVariant(const char *path)
70      : Path(path), Kind(IsNoExist), Data(0) {}
71
72  bool isFile() const { return Kind == IsFE; }
73
74  StringRef getString() const {
75    return Kind == IsFE ? FE->getName() : Path;
76  }
77
78  unsigned getKind() const { return (unsigned) Kind; }
79
80  void EmitData(raw_ostream& Out) {
81    switch (Kind) {
82    case IsFE: {
83      // Emit stat information.
84      llvm::sys::fs::UniqueID UID = FE->getUniqueID();
85      ::Emit64(Out, UID.getFile());
86      ::Emit64(Out, UID.getDevice());
87      ::Emit64(Out, FE->getModificationTime());
88      ::Emit64(Out, FE->getSize());
89    } break;
90    case IsDE:
91      // Emit stat information.
92      ::Emit64(Out, Data->UniqueID.getFile());
93      ::Emit64(Out, Data->UniqueID.getDevice());
94      ::Emit64(Out, Data->ModTime);
95      ::Emit64(Out, Data->Size);
96      delete Data;
97      break;
98    default:
99      break;
100    }
101  }
102
103  unsigned getRepresentationLength() const {
104    return Kind == IsNoExist ? 0 : 4 + 4 + 2 + 8 + 8;
105  }
106};
107
108class FileEntryPTHEntryInfo {
109public:
110  typedef PTHEntryKeyVariant key_type;
111  typedef key_type key_type_ref;
112
113  typedef PTHEntry data_type;
114  typedef const PTHEntry& data_type_ref;
115
116  static unsigned ComputeHash(PTHEntryKeyVariant V) {
117    return llvm::HashString(V.getString());
118  }
119
120  static std::pair<unsigned,unsigned>
121  EmitKeyDataLength(raw_ostream& Out, PTHEntryKeyVariant V,
122                    const PTHEntry& E) {
123
124    unsigned n = V.getString().size() + 1 + 1;
125    ::Emit16(Out, n);
126
127    unsigned m = V.getRepresentationLength() + (V.isFile() ? 4 + 4 : 0);
128    ::Emit8(Out, m);
129
130    return std::make_pair(n, m);
131  }
132
133  static void EmitKey(raw_ostream& Out, PTHEntryKeyVariant V, unsigned n){
134    // Emit the entry kind.
135    ::Emit8(Out, (unsigned) V.getKind());
136    // Emit the string.
137    Out.write(V.getString().data(), n - 1);
138  }
139
140  static void EmitData(raw_ostream& Out, PTHEntryKeyVariant V,
141                       const PTHEntry& E, unsigned) {
142
143
144    // For file entries emit the offsets into the PTH file for token data
145    // and the preprocessor blocks table.
146    if (V.isFile()) {
147      ::Emit32(Out, E.getTokenOffset());
148      ::Emit32(Out, E.getPPCondTableOffset());
149    }
150
151    // Emit any other data associated with the key (i.e., stat information).
152    V.EmitData(Out);
153  }
154};
155
156class OffsetOpt {
157  bool valid;
158  Offset off;
159public:
160  OffsetOpt() : valid(false) {}
161  bool hasOffset() const { return valid; }
162  Offset getOffset() const { assert(valid); return off; }
163  void setOffset(Offset o) { off = o; valid = true; }
164};
165} // end anonymous namespace
166
167typedef OnDiskChainedHashTableGenerator<FileEntryPTHEntryInfo> PTHMap;
168
169namespace {
170class PTHWriter {
171  typedef llvm::DenseMap<const IdentifierInfo*,uint32_t> IDMap;
172  typedef llvm::StringMap<OffsetOpt, llvm::BumpPtrAllocator> CachedStrsTy;
173
174  IDMap IM;
175  llvm::raw_fd_ostream& Out;
176  Preprocessor& PP;
177  uint32_t idcount;
178  PTHMap PM;
179  CachedStrsTy CachedStrs;
180  Offset CurStrOffset;
181  std::vector<llvm::StringMapEntry<OffsetOpt>*> StrEntries;
182
183  //// Get the persistent id for the given IdentifierInfo*.
184  uint32_t ResolveID(const IdentifierInfo* II);
185
186  /// Emit a token to the PTH file.
187  void EmitToken(const Token& T);
188
189  void Emit8(uint32_t V) { ::Emit8(Out, V); }
190
191  void Emit16(uint32_t V) { ::Emit16(Out, V); }
192
193  void Emit32(uint32_t V) { ::Emit32(Out, V); }
194
195  void EmitBuf(const char *Ptr, unsigned NumBytes) {
196    Out.write(Ptr, NumBytes);
197  }
198
199  void EmitString(StringRef V) {
200    ::Emit16(Out, V.size());
201    EmitBuf(V.data(), V.size());
202  }
203
204  /// EmitIdentifierTable - Emits two tables to the PTH file.  The first is
205  ///  a hashtable mapping from identifier strings to persistent IDs.
206  ///  The second is a straight table mapping from persistent IDs to string data
207  ///  (the keys of the first table).
208  std::pair<Offset, Offset> EmitIdentifierTable();
209
210  /// EmitFileTable - Emit a table mapping from file name strings to PTH
211  /// token data.
212  Offset EmitFileTable() { return PM.Emit(Out); }
213
214  PTHEntry LexTokens(Lexer& L);
215  Offset EmitCachedSpellings();
216
217public:
218  PTHWriter(llvm::raw_fd_ostream& out, Preprocessor& pp)
219    : Out(out), PP(pp), idcount(0), CurStrOffset(0) {}
220
221  PTHMap &getPM() { return PM; }
222  void GeneratePTH(const std::string &MainFile);
223};
224} // end anonymous namespace
225
226uint32_t PTHWriter::ResolveID(const IdentifierInfo* II) {
227  // Null IdentifierInfo's map to the persistent ID 0.
228  if (!II)
229    return 0;
230
231  IDMap::iterator I = IM.find(II);
232  if (I != IM.end())
233    return I->second; // We've already added 1.
234
235  IM[II] = ++idcount; // Pre-increment since '0' is reserved for NULL.
236  return idcount;
237}
238
239void PTHWriter::EmitToken(const Token& T) {
240  // Emit the token kind, flags, and length.
241  Emit32(((uint32_t) T.getKind()) | ((((uint32_t) T.getFlags())) << 8)|
242         (((uint32_t) T.getLength()) << 16));
243
244  if (!T.isLiteral()) {
245    Emit32(ResolveID(T.getIdentifierInfo()));
246  } else {
247    // We cache *un-cleaned* spellings. This gives us 100% fidelity with the
248    // source code.
249    StringRef s(T.getLiteralData(), T.getLength());
250
251    // Get the string entry.
252    llvm::StringMapEntry<OffsetOpt> *E = &CachedStrs.GetOrCreateValue(s);
253
254    // If this is a new string entry, bump the PTH offset.
255    if (!E->getValue().hasOffset()) {
256      E->getValue().setOffset(CurStrOffset);
257      StrEntries.push_back(E);
258      CurStrOffset += s.size() + 1;
259    }
260
261    // Emit the relative offset into the PTH file for the spelling string.
262    Emit32(E->getValue().getOffset());
263  }
264
265  // Emit the offset into the original source file of this token so that we
266  // can reconstruct its SourceLocation.
267  Emit32(PP.getSourceManager().getFileOffset(T.getLocation()));
268}
269
270PTHEntry PTHWriter::LexTokens(Lexer& L) {
271  // Pad 0's so that we emit tokens to a 4-byte alignment.
272  // This speed up reading them back in.
273  Pad(Out, 4);
274  Offset TokenOff = (Offset) Out.tell();
275
276  // Keep track of matching '#if' ... '#endif'.
277  typedef std::vector<std::pair<Offset, unsigned> > PPCondTable;
278  PPCondTable PPCond;
279  std::vector<unsigned> PPStartCond;
280  bool ParsingPreprocessorDirective = false;
281  Token Tok;
282
283  do {
284    L.LexFromRawLexer(Tok);
285  NextToken:
286
287    if ((Tok.isAtStartOfLine() || Tok.is(tok::eof)) &&
288        ParsingPreprocessorDirective) {
289      // Insert an eod token into the token cache.  It has the same
290      // position as the next token that is not on the same line as the
291      // preprocessor directive.  Observe that we continue processing
292      // 'Tok' when we exit this branch.
293      Token Tmp = Tok;
294      Tmp.setKind(tok::eod);
295      Tmp.clearFlag(Token::StartOfLine);
296      Tmp.setIdentifierInfo(0);
297      EmitToken(Tmp);
298      ParsingPreprocessorDirective = false;
299    }
300
301    if (Tok.is(tok::raw_identifier)) {
302      PP.LookUpIdentifierInfo(Tok);
303      EmitToken(Tok);
304      continue;
305    }
306
307    if (Tok.is(tok::hash) && Tok.isAtStartOfLine()) {
308      // Special processing for #include.  Store the '#' token and lex
309      // the next token.
310      assert(!ParsingPreprocessorDirective);
311      Offset HashOff = (Offset) Out.tell();
312
313      // Get the next token.
314      Token NextTok;
315      L.LexFromRawLexer(NextTok);
316
317      // If we see the start of line, then we had a null directive "#".  In
318      // this case, discard both tokens.
319      if (NextTok.isAtStartOfLine())
320        goto NextToken;
321
322      // The token is the start of a directive.  Emit it.
323      EmitToken(Tok);
324      Tok = NextTok;
325
326      // Did we see 'include'/'import'/'include_next'?
327      if (Tok.isNot(tok::raw_identifier)) {
328        EmitToken(Tok);
329        continue;
330      }
331
332      IdentifierInfo* II = PP.LookUpIdentifierInfo(Tok);
333      tok::PPKeywordKind K = II->getPPKeywordID();
334
335      ParsingPreprocessorDirective = true;
336
337      switch (K) {
338      case tok::pp_not_keyword:
339        // Invalid directives "#foo" can occur in #if 0 blocks etc, just pass
340        // them through.
341      default:
342        break;
343
344      case tok::pp_include:
345      case tok::pp_import:
346      case tok::pp_include_next: {
347        // Save the 'include' token.
348        EmitToken(Tok);
349        // Lex the next token as an include string.
350        L.setParsingPreprocessorDirective(true);
351        L.LexIncludeFilename(Tok);
352        L.setParsingPreprocessorDirective(false);
353        assert(!Tok.isAtStartOfLine());
354        if (Tok.is(tok::raw_identifier))
355          PP.LookUpIdentifierInfo(Tok);
356
357        break;
358      }
359      case tok::pp_if:
360      case tok::pp_ifdef:
361      case tok::pp_ifndef: {
362        // Add an entry for '#if' and friends.  We initially set the target
363        // index to 0.  This will get backpatched when we hit #endif.
364        PPStartCond.push_back(PPCond.size());
365        PPCond.push_back(std::make_pair(HashOff, 0U));
366        break;
367      }
368      case tok::pp_endif: {
369        // Add an entry for '#endif'.  We set the target table index to itself.
370        // This will later be set to zero when emitting to the PTH file.  We
371        // use 0 for uninitialized indices because that is easier to debug.
372        unsigned index = PPCond.size();
373        // Backpatch the opening '#if' entry.
374        assert(!PPStartCond.empty());
375        assert(PPCond.size() > PPStartCond.back());
376        assert(PPCond[PPStartCond.back()].second == 0);
377        PPCond[PPStartCond.back()].second = index;
378        PPStartCond.pop_back();
379        // Add the new entry to PPCond.
380        PPCond.push_back(std::make_pair(HashOff, index));
381        EmitToken(Tok);
382
383        // Some files have gibberish on the same line as '#endif'.
384        // Discard these tokens.
385        do
386          L.LexFromRawLexer(Tok);
387        while (Tok.isNot(tok::eof) && !Tok.isAtStartOfLine());
388        // We have the next token in hand.
389        // Don't immediately lex the next one.
390        goto NextToken;
391      }
392      case tok::pp_elif:
393      case tok::pp_else: {
394        // Add an entry for #elif or #else.
395        // This serves as both a closing and opening of a conditional block.
396        // This means that its entry will get backpatched later.
397        unsigned index = PPCond.size();
398        // Backpatch the previous '#if' entry.
399        assert(!PPStartCond.empty());
400        assert(PPCond.size() > PPStartCond.back());
401        assert(PPCond[PPStartCond.back()].second == 0);
402        PPCond[PPStartCond.back()].second = index;
403        PPStartCond.pop_back();
404        // Now add '#elif' as a new block opening.
405        PPCond.push_back(std::make_pair(HashOff, 0U));
406        PPStartCond.push_back(index);
407        break;
408      }
409      }
410    }
411
412    EmitToken(Tok);
413  }
414  while (Tok.isNot(tok::eof));
415
416  assert(PPStartCond.empty() && "Error: imblanced preprocessor conditionals.");
417
418  // Next write out PPCond.
419  Offset PPCondOff = (Offset) Out.tell();
420
421  // Write out the size of PPCond so that clients can identifer empty tables.
422  Emit32(PPCond.size());
423
424  for (unsigned i = 0, e = PPCond.size(); i!=e; ++i) {
425    Emit32(PPCond[i].first - TokenOff);
426    uint32_t x = PPCond[i].second;
427    assert(x != 0 && "PPCond entry not backpatched.");
428    // Emit zero for #endifs.  This allows us to do checking when
429    // we read the PTH file back in.
430    Emit32(x == i ? 0 : x);
431  }
432
433  return PTHEntry(TokenOff, PPCondOff);
434}
435
436Offset PTHWriter::EmitCachedSpellings() {
437  // Write each cached strings to the PTH file.
438  Offset SpellingsOff = Out.tell();
439
440  for (std::vector<llvm::StringMapEntry<OffsetOpt>*>::iterator
441       I = StrEntries.begin(), E = StrEntries.end(); I!=E; ++I)
442    EmitBuf((*I)->getKeyData(), (*I)->getKeyLength()+1 /*nul included*/);
443
444  return SpellingsOff;
445}
446
447void PTHWriter::GeneratePTH(const std::string &MainFile) {
448  // Generate the prologue.
449  Out << "cfe-pth" << '\0';
450  Emit32(PTHManager::Version);
451
452  // Leave 4 words for the prologue.
453  Offset PrologueOffset = Out.tell();
454  for (unsigned i = 0; i < 4; ++i)
455    Emit32(0);
456
457  // Write the name of the MainFile.
458  if (!MainFile.empty()) {
459    EmitString(MainFile);
460  } else {
461    // String with 0 bytes.
462    Emit16(0);
463  }
464  Emit8(0);
465
466  // Iterate over all the files in SourceManager.  Create a lexer
467  // for each file and cache the tokens.
468  SourceManager &SM = PP.getSourceManager();
469  const LangOptions &LOpts = PP.getLangOpts();
470
471  for (SourceManager::fileinfo_iterator I = SM.fileinfo_begin(),
472       E = SM.fileinfo_end(); I != E; ++I) {
473    const SrcMgr::ContentCache &C = *I->second;
474    const FileEntry *FE = C.OrigEntry;
475
476    // FIXME: Handle files with non-absolute paths.
477    if (llvm::sys::path::is_relative(FE->getName()))
478      continue;
479
480    const llvm::MemoryBuffer *B = C.getBuffer(PP.getDiagnostics(), SM);
481    if (!B) continue;
482
483    FileID FID = SM.createFileID(FE, SourceLocation(), SrcMgr::C_User);
484    const llvm::MemoryBuffer *FromFile = SM.getBuffer(FID);
485    Lexer L(FID, FromFile, SM, LOpts);
486    PM.insert(FE, LexTokens(L));
487  }
488
489  // Write out the identifier table.
490  const std::pair<Offset,Offset> &IdTableOff = EmitIdentifierTable();
491
492  // Write out the cached strings table.
493  Offset SpellingOff = EmitCachedSpellings();
494
495  // Write out the file table.
496  Offset FileTableOff = EmitFileTable();
497
498  // Finally, write the prologue.
499  Out.seek(PrologueOffset);
500  Emit32(IdTableOff.first);
501  Emit32(IdTableOff.second);
502  Emit32(FileTableOff);
503  Emit32(SpellingOff);
504}
505
506namespace {
507/// StatListener - A simple "interpose" object used to monitor stat calls
508/// invoked by FileManager while processing the original sources used
509/// as input to PTH generation.  StatListener populates the PTHWriter's
510/// file map with stat information for directories as well as negative stats.
511/// Stat information for files are populated elsewhere.
512class StatListener : public FileSystemStatCache {
513  PTHMap &PM;
514public:
515  StatListener(PTHMap &pm) : PM(pm) {}
516  ~StatListener() {}
517
518  LookupResult getStat(const char *Path, FileData &Data, bool isFile,
519                       int *FileDescriptor) {
520    LookupResult Result = statChained(Path, Data, isFile, FileDescriptor);
521
522    if (Result == CacheMissing) // Failed 'stat'.
523      PM.insert(PTHEntryKeyVariant(Path), PTHEntry());
524    else if (Data.IsDirectory) {
525      // Only cache directories with absolute paths.
526      if (llvm::sys::path::is_relative(Path))
527        return Result;
528
529      PM.insert(PTHEntryKeyVariant(&Data, Path), PTHEntry());
530    }
531
532    return Result;
533  }
534};
535} // end anonymous namespace
536
537
538void clang::CacheTokens(Preprocessor &PP, llvm::raw_fd_ostream* OS) {
539  // Get the name of the main file.
540  const SourceManager &SrcMgr = PP.getSourceManager();
541  const FileEntry *MainFile = SrcMgr.getFileEntryForID(SrcMgr.getMainFileID());
542  SmallString<128> MainFilePath(MainFile->getName());
543
544  llvm::sys::fs::make_absolute(MainFilePath);
545
546  // Create the PTHWriter.
547  PTHWriter PW(*OS, PP);
548
549  // Install the 'stat' system call listener in the FileManager.
550  StatListener *StatCache = new StatListener(PW.getPM());
551  PP.getFileManager().addStatCache(StatCache, /*AtBeginning=*/true);
552
553  // Lex through the entire file.  This will populate SourceManager with
554  // all of the header information.
555  Token Tok;
556  PP.EnterMainSourceFile();
557  do { PP.Lex(Tok); } while (Tok.isNot(tok::eof));
558
559  // Generate the PTH file.
560  PP.getFileManager().removeStatCache(StatCache);
561  PW.GeneratePTH(MainFilePath.str());
562}
563
564//===----------------------------------------------------------------------===//
565
566namespace {
567class PTHIdKey {
568public:
569  const IdentifierInfo* II;
570  uint32_t FileOffset;
571};
572
573class PTHIdentifierTableTrait {
574public:
575  typedef PTHIdKey* key_type;
576  typedef key_type  key_type_ref;
577
578  typedef uint32_t  data_type;
579  typedef data_type data_type_ref;
580
581  static unsigned ComputeHash(PTHIdKey* key) {
582    return llvm::HashString(key->II->getName());
583  }
584
585  static std::pair<unsigned,unsigned>
586  EmitKeyDataLength(raw_ostream& Out, const PTHIdKey* key, uint32_t) {
587    unsigned n = key->II->getLength() + 1;
588    ::Emit16(Out, n);
589    return std::make_pair(n, sizeof(uint32_t));
590  }
591
592  static void EmitKey(raw_ostream& Out, PTHIdKey* key, unsigned n) {
593    // Record the location of the key data.  This is used when generating
594    // the mapping from persistent IDs to strings.
595    key->FileOffset = Out.tell();
596    Out.write(key->II->getNameStart(), n);
597  }
598
599  static void EmitData(raw_ostream& Out, PTHIdKey*, uint32_t pID,
600                       unsigned) {
601    ::Emit32(Out, pID);
602  }
603};
604} // end anonymous namespace
605
606/// EmitIdentifierTable - Emits two tables to the PTH file.  The first is
607///  a hashtable mapping from identifier strings to persistent IDs.  The second
608///  is a straight table mapping from persistent IDs to string data (the
609///  keys of the first table).
610///
611std::pair<Offset,Offset> PTHWriter::EmitIdentifierTable() {
612  // Build two maps:
613  //  (1) an inverse map from persistent IDs -> (IdentifierInfo*,Offset)
614  //  (2) a map from (IdentifierInfo*, Offset)* -> persistent IDs
615
616  // Note that we use 'calloc', so all the bytes are 0.
617  PTHIdKey *IIDMap = (PTHIdKey*)calloc(idcount, sizeof(PTHIdKey));
618
619  // Create the hashtable.
620  OnDiskChainedHashTableGenerator<PTHIdentifierTableTrait> IIOffMap;
621
622  // Generate mapping from persistent IDs -> IdentifierInfo*.
623  for (IDMap::iterator I = IM.begin(), E = IM.end(); I != E; ++I) {
624    // Decrement by 1 because we are using a vector for the lookup and
625    // 0 is reserved for NULL.
626    assert(I->second > 0);
627    assert(I->second-1 < idcount);
628    unsigned idx = I->second-1;
629
630    // Store the mapping from persistent ID to IdentifierInfo*
631    IIDMap[idx].II = I->first;
632
633    // Store the reverse mapping in a hashtable.
634    IIOffMap.insert(&IIDMap[idx], I->second);
635  }
636
637  // Write out the inverse map first.  This causes the PCIDKey entries to
638  // record PTH file offsets for the string data.  This is used to write
639  // the second table.
640  Offset StringTableOffset = IIOffMap.Emit(Out);
641
642  // Now emit the table mapping from persistent IDs to PTH file offsets.
643  Offset IDOff = Out.tell();
644  Emit32(idcount);  // Emit the number of identifiers.
645  for (unsigned i = 0 ; i < idcount; ++i)
646    Emit32(IIDMap[i].FileOffset);
647
648  // Finally, release the inverse map.
649  free(IIDMap);
650
651  return std::make_pair(IDOff, StringTableOffset);
652}
653