blob: 561a68a6ef6a0f372ec7c71008e721635e71e90f [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"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "llvm/Support/raw_ostream.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000024#include <string>
25
26using namespace clang;
Douglas Gregor2cf26342009-04-09 22:27:44 +000027
28namespace {
Benjamin Kramerbd218282009-11-28 10:07:24 +000029 class PCHGenerator : public SemaConsumer {
Chris Lattnerdf961c22009-04-10 18:08:30 +000030 const Preprocessor &PP;
Douglas Gregore650c8c2009-07-07 00:12:59 +000031 const char *isysroot;
Eli Friedman66d6f042009-05-18 22:20:00 +000032 llvm::raw_ostream *Out;
Douglas Gregore7785042009-04-20 15:53:59 +000033 Sema *SemaPtr;
Douglas Gregor4fed3f42009-04-27 18:38:38 +000034 MemorizeStatCalls *StatCalls; // owned by the FileManager
Sebastian Redl30c514c2010-07-14 23:45:08 +000035 std::vector<unsigned char> Buffer;
36 llvm::BitstreamWriter Stream;
37 PCHWriter Writer;
Mike Stump1eb44332009-09-09 15:08:12 +000038
Douglas Gregor2cf26342009-04-09 22:27:44 +000039 public:
Sebastian Redlffaab3e2010-07-30 00:29:29 +000040 PCHGenerator(const Preprocessor &PP, bool Chaining,
Sebastian Redl30c514c2010-07-14 23:45:08 +000041 const char *isysroot, llvm::raw_ostream *Out);
Douglas Gregore7785042009-04-20 15:53:59 +000042 virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
Douglas Gregor2cf26342009-04-09 22:27:44 +000043 virtual void HandleTranslationUnit(ASTContext &Ctx);
Sebastian Redlffaab3e2010-07-30 00:29:29 +000044 virtual PCHDeserializationListener *GetPCHDeserializationListener();
Douglas Gregor2cf26342009-04-09 22:27:44 +000045 };
46}
47
Mike Stump1eb44332009-09-09 15:08:12 +000048PCHGenerator::PCHGenerator(const Preprocessor &PP,
Sebastian Redlffaab3e2010-07-30 00:29:29 +000049 bool Chaining,
Douglas Gregore650c8c2009-07-07 00:12:59 +000050 const char *isysroot,
51 llvm::raw_ostream *OS)
Sebastian Redlffaab3e2010-07-30 00:29:29 +000052 : PP(PP), isysroot(isysroot), Out(OS), SemaPtr(0),
53 StatCalls(0), Stream(Buffer), Writer(Stream) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +000054
55 // Install a stat() listener to keep track of all of the stat()
56 // calls.
57 StatCalls = new MemorizeStatCalls;
Sebastian Redl1476ed42010-07-16 16:36:56 +000058 // If we have a chain, we want new stat calls only, so install the memorizer
59 // *after* the already installed PCHReader's stat cache.
60 PP.getFileManager().addStatCache(StatCalls,
Sebastian Redlffaab3e2010-07-30 00:29:29 +000061 /*AtBeginning=*/!Chaining);
Douglas Gregor4fed3f42009-04-27 18:38:38 +000062}
63
Douglas Gregor2cf26342009-04-09 22:27:44 +000064void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
Chris Lattner0b1fb982009-04-10 17:15:23 +000065 if (PP.getDiagnostics().hasErrorOccurred())
Douglas Gregor2cf26342009-04-09 22:27:44 +000066 return;
67
Douglas Gregor2cf26342009-04-09 22:27:44 +000068 // Emit the PCH file
Douglas Gregore7785042009-04-20 15:53:59 +000069 assert(SemaPtr && "No Sema?");
Sebastian Redl30c514c2010-07-14 23:45:08 +000070 Writer.WritePCH(*SemaPtr, StatCalls, isysroot);
Douglas Gregor2cf26342009-04-09 22:27:44 +000071
Douglas Gregor2cf26342009-04-09 22:27:44 +000072 // Write the generated bitstream to "Out".
Eli Friedman66d6f042009-05-18 22:20:00 +000073 Out->write((char *)&Buffer.front(), Buffer.size());
Douglas Gregor2cf26342009-04-09 22:27:44 +000074
75 // Make sure it hits disk now.
Eli Friedman66d6f042009-05-18 22:20:00 +000076 Out->flush();
Sebastian Redl30c514c2010-07-14 23:45:08 +000077
78 // Free up some memory, in case the process is kept alive.
79 Buffer.clear();
Douglas Gregor2cf26342009-04-09 22:27:44 +000080}
81
Sebastian Redlffaab3e2010-07-30 00:29:29 +000082PCHDeserializationListener *PCHGenerator::GetPCHDeserializationListener() {
83 return &Writer;
84}
85
Chris Lattnerdf961c22009-04-10 18:08:30 +000086ASTConsumer *clang::CreatePCHGenerator(const Preprocessor &PP,
Douglas Gregore650c8c2009-07-07 00:12:59 +000087 llvm::raw_ostream *OS,
Sebastian Redlffaab3e2010-07-30 00:29:29 +000088 bool Chaining,
Douglas Gregore650c8c2009-07-07 00:12:59 +000089 const char *isysroot) {
Sebastian Redlffaab3e2010-07-30 00:29:29 +000090 return new PCHGenerator(PP, Chaining, isysroot, OS);
Douglas Gregor2cf26342009-04-09 22:27:44 +000091}