blob: 8be88ce38117cbf1b7543d27d555ea832010653e [file] [log] [blame]
Douglas Gregor2cf26342009-04-09 22:27:44 +00001//===--- 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
Eli Friedman39d7c4d2009-05-18 22:50:54 +000015#include "clang/Frontend/ASTConsumers.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000016#include "clang/Frontend/PCHWriter.h"
Douglas Gregore7785042009-04-20 15:53:59 +000017#include "clang/Sema/SemaConsumer.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000018#include "clang/AST/ASTContext.h"
19#include "clang/AST/ASTConsumer.h"
Chris Lattner0b1fb982009-04-10 17:15:23 +000020#include "clang/Lex/Preprocessor.h"
Douglas Gregor4fed3f42009-04-27 18:38:38 +000021#include "clang/Basic/FileManager.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000022#include "llvm/Bitcode/BitstreamWriter.h"
23#include "llvm/System/Path.h"
24#include "llvm/Support/Compiler.h"
25#include "llvm/Support/raw_ostream.h"
26#include "llvm/Support/Streams.h"
27#include <string>
28
29using namespace clang;
30using namespace llvm;
31
32namespace {
Douglas Gregore7785042009-04-20 15:53:59 +000033 class VISIBILITY_HIDDEN PCHGenerator : public SemaConsumer {
Chris Lattnerdf961c22009-04-10 18:08:30 +000034 const Preprocessor &PP;
Eli Friedman66d6f042009-05-18 22:20:00 +000035 llvm::raw_ostream *Out;
Douglas Gregore7785042009-04-20 15:53:59 +000036 Sema *SemaPtr;
Douglas Gregor4fed3f42009-04-27 18:38:38 +000037 MemorizeStatCalls *StatCalls; // owned by the FileManager
Douglas Gregor2cf26342009-04-09 22:27:44 +000038
39 public:
Eli Friedman66d6f042009-05-18 22:20:00 +000040 explicit PCHGenerator(const Preprocessor &PP, llvm::raw_ostream *Out);
Douglas Gregore7785042009-04-20 15:53:59 +000041 virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
Douglas Gregor2cf26342009-04-09 22:27:44 +000042 virtual void HandleTranslationUnit(ASTContext &Ctx);
43 };
44}
45
Eli Friedman66d6f042009-05-18 22:20:00 +000046PCHGenerator::PCHGenerator(const Preprocessor &PP, llvm::raw_ostream *OS)
47 : PP(PP), Out(OS), SemaPtr(0), StatCalls(0) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +000048
49 // Install a stat() listener to keep track of all of the stat()
50 // calls.
51 StatCalls = new MemorizeStatCalls;
52 PP.getFileManager().setStatCache(StatCalls);
53}
54
Douglas Gregor2cf26342009-04-09 22:27:44 +000055void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
Chris Lattner0b1fb982009-04-10 17:15:23 +000056 if (PP.getDiagnostics().hasErrorOccurred())
Douglas Gregor2cf26342009-04-09 22:27:44 +000057 return;
58
59 // Write the PCH contents into a buffer
60 std::vector<unsigned char> Buffer;
61 BitstreamWriter Stream(Buffer);
62 PCHWriter Writer(Stream);
63
64 // Emit the PCH file
Douglas Gregore7785042009-04-20 15:53:59 +000065 assert(SemaPtr && "No Sema?");
Douglas Gregor4fed3f42009-04-27 18:38:38 +000066 Writer.WritePCH(*SemaPtr, StatCalls);
Douglas Gregor2cf26342009-04-09 22:27:44 +000067
Douglas Gregor2cf26342009-04-09 22:27:44 +000068 // Write the generated bitstream to "Out".
Eli Friedman66d6f042009-05-18 22:20:00 +000069 Out->write((char *)&Buffer.front(), Buffer.size());
Douglas Gregor2cf26342009-04-09 22:27:44 +000070
71 // Make sure it hits disk now.
Eli Friedman66d6f042009-05-18 22:20:00 +000072 Out->flush();
Douglas Gregor2cf26342009-04-09 22:27:44 +000073}
74
Chris Lattnerdf961c22009-04-10 18:08:30 +000075ASTConsumer *clang::CreatePCHGenerator(const Preprocessor &PP,
Eli Friedman66d6f042009-05-18 22:20:00 +000076 llvm::raw_ostream *OS) {
77 return new PCHGenerator(PP, OS);
Douglas Gregor2cf26342009-04-09 22:27:44 +000078}