zcov: / lib/Frontend/GeneratePCH.cpp


Files: 1 Branches Taken: 37.5% 3 / 8
Generated: 2010-02-10 01:31 Branches Executed: 75.0% 6 / 8
Line Coverage: 94.7% 18 / 19


Programs: 1 Runs 2897


       1                 : //===--- GeneratePCH.cpp - AST Consumer for PCH Generation ------*- C++ -*-===//
       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 file defines the CreatePCHGenerate function, which creates an
      11                 : //  ASTConsume that generates a PCH file.
      12                 : //
      13                 : //===----------------------------------------------------------------------===//
      14                 : 
      15                 : #include "clang/Frontend/ASTConsumers.h"
      16                 : #include "clang/Frontend/PCHWriter.h"
      17                 : #include "clang/Sema/SemaConsumer.h"
      18                 : #include "clang/AST/ASTContext.h"
      19                 : #include "clang/AST/ASTConsumer.h"
      20                 : #include "clang/Lex/Preprocessor.h"
      21                 : #include "clang/Basic/FileManager.h"
      22                 : #include "llvm/Bitcode/BitstreamWriter.h"
      23                 : #include "llvm/Support/raw_ostream.h"
      24                 : #include <string>
      25                 : 
      26                 : using namespace clang;
      27                 : 
      28                 : namespace {
                       39: branch 1 taken
                        0: branch 2 not taken
                        0: branch 5 not taken
                        0: branch 6 not taken
      29               39:   class PCHGenerator : public SemaConsumer {
      30                 :     const Preprocessor &PP;
      31                 :     const char *isysroot;
      32                 :     llvm::raw_ostream *Out;
      33                 :     Sema *SemaPtr;
      34                 :     MemorizeStatCalls *StatCalls; // owned by the FileManager
      35                 : 
      36                 :   public:
      37                 :     explicit PCHGenerator(const Preprocessor &PP,
      38                 :                           const char *isysroot,
      39                 :                           llvm::raw_ostream *Out);
      40               44:     virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
      41                 :     virtual void HandleTranslationUnit(ASTContext &Ctx);
      42                 :   };
      43                 : }
      44                 : 
      45                 : PCHGenerator::PCHGenerator(const Preprocessor &PP,
      46                 :                            const char *isysroot,
      47               44:                            llvm::raw_ostream *OS)
      48               44:   : PP(PP), isysroot(isysroot), Out(OS), SemaPtr(0), StatCalls(0) {
      49                 : 
      50                 :   // Install a stat() listener to keep track of all of the stat()
      51                 :   // calls.
      52               44:   StatCalls = new MemorizeStatCalls;
      53               44:   PP.getFileManager().addStatCache(StatCalls, /*AtBeginning=*/true);
      54               44: }
      55                 : 
      56               44: void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
                        0: branch 2 not taken
                       44: branch 3 taken
      57               44:   if (PP.getDiagnostics().hasErrorOccurred())
      58                0:     return;
      59                 : 
      60                 :   // Write the PCH contents into a buffer
      61               44:   std::vector<unsigned char> Buffer;
      62               44:   llvm::BitstreamWriter Stream(Buffer);
      63               44:   PCHWriter Writer(Stream);
      64                 : 
      65                 :   // Emit the PCH file
                        0: branch 0 not taken
                       44: branch 1 taken
      66               44:   assert(SemaPtr && "No Sema?");
      67               44:   Writer.WritePCH(*SemaPtr, StatCalls, isysroot);
      68                 : 
      69                 :   // Write the generated bitstream to "Out".
      70               44:   Out->write((char *)&Buffer.front(), Buffer.size());
      71                 : 
      72                 :   // Make sure it hits disk now.
      73               44:   Out->flush();
      74                 : }
      75                 : 
      76                 : ASTConsumer *clang::CreatePCHGenerator(const Preprocessor &PP,
      77                 :                                        llvm::raw_ostream *OS,
      78               44:                                        const char *isysroot) {
      79               44:   return new PCHGenerator(PP, isysroot, OS);
      80                 : }

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