zcov: / lib/Frontend/CacheTokens.cpp


Files: 1 Branches Taken: 51.7% 62 / 120
Generated: 2010-02-10 01:31 Branches Executed: 78.3% 94 / 120
Line Coverage: 80.8% 215 / 266


Programs: 1 Runs 2897


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

Generated: 2010-02-10 01:31 by zcov