blob: 9be103e06eb0bc13c566ebceeefd091a8555f343 [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;
Sebastian Redla93e3b52010-07-08 22:01:51 +000031 const PCHReader *Chain;
Douglas Gregore650c8c2009-07-07 00:12:59 +000032 const char *isysroot;
Eli Friedman66d6f042009-05-18 22:20:00 +000033 llvm::raw_ostream *Out;
Douglas Gregore7785042009-04-20 15:53:59 +000034 Sema *SemaPtr;
Douglas Gregor4fed3f42009-04-27 18:38:38 +000035 MemorizeStatCalls *StatCalls; // owned by the FileManager
Mike Stump1eb44332009-09-09 15:08:12 +000036
Douglas Gregor2cf26342009-04-09 22:27:44 +000037 public:
Mike Stump1eb44332009-09-09 15:08:12 +000038 explicit PCHGenerator(const Preprocessor &PP,
Sebastian Redla93e3b52010-07-08 22:01:51 +000039 const PCHReader *Chain,
Douglas Gregore650c8c2009-07-07 00:12:59 +000040 const char *isysroot,
41 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);
44 };
45}
46
Mike Stump1eb44332009-09-09 15:08:12 +000047PCHGenerator::PCHGenerator(const Preprocessor &PP,
Sebastian Redla93e3b52010-07-08 22:01:51 +000048 const PCHReader *Chain,
Douglas Gregore650c8c2009-07-07 00:12:59 +000049 const char *isysroot,
50 llvm::raw_ostream *OS)
Sebastian Redla93e3b52010-07-08 22:01:51 +000051 : PP(PP), Chain(Chain), isysroot(isysroot), Out(OS), SemaPtr(0),
52 StatCalls(0) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +000053
54 // Install a stat() listener to keep track of all of the stat()
55 // calls.
56 StatCalls = new MemorizeStatCalls;
Douglas Gregor52e71082009-10-16 18:18:30 +000057 PP.getFileManager().addStatCache(StatCalls, /*AtBeginning=*/true);
Douglas Gregor4fed3f42009-04-27 18:38:38 +000058}
59
Douglas Gregor2cf26342009-04-09 22:27:44 +000060void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
Chris Lattner0b1fb982009-04-10 17:15:23 +000061 if (PP.getDiagnostics().hasErrorOccurred())
Douglas Gregor2cf26342009-04-09 22:27:44 +000062 return;
63
Douglas Gregore650c8c2009-07-07 00:12:59 +000064 // Write the PCH contents into a buffer
Douglas Gregor2cf26342009-04-09 22:27:44 +000065 std::vector<unsigned char> Buffer;
Benjamin Kramerbd218282009-11-28 10:07:24 +000066 llvm::BitstreamWriter Stream(Buffer);
Douglas Gregor2cf26342009-04-09 22:27:44 +000067 PCHWriter Writer(Stream);
68
69 // Emit the PCH file
Douglas Gregore7785042009-04-20 15:53:59 +000070 assert(SemaPtr && "No Sema?");
Sebastian Redla93e3b52010-07-08 22:01:51 +000071 Writer.WritePCH(*SemaPtr, StatCalls, Chain, isysroot);
Douglas Gregor2cf26342009-04-09 22:27:44 +000072
Douglas Gregor2cf26342009-04-09 22:27:44 +000073 // Write the generated bitstream to "Out".
Eli Friedman66d6f042009-05-18 22:20:00 +000074 Out->write((char *)&Buffer.front(), Buffer.size());
Douglas Gregor2cf26342009-04-09 22:27:44 +000075
76 // Make sure it hits disk now.
Eli Friedman66d6f042009-05-18 22:20:00 +000077 Out->flush();
Douglas Gregor2cf26342009-04-09 22:27:44 +000078}
79
Chris Lattnerdf961c22009-04-10 18:08:30 +000080ASTConsumer *clang::CreatePCHGenerator(const Preprocessor &PP,
Douglas Gregore650c8c2009-07-07 00:12:59 +000081 llvm::raw_ostream *OS,
Sebastian Redla93e3b52010-07-08 22:01:51 +000082 const PCHReader *Chain,
Douglas Gregore650c8c2009-07-07 00:12:59 +000083 const char *isysroot) {
Sebastian Redla93e3b52010-07-08 22:01:51 +000084 return new PCHGenerator(PP, Chain, isysroot, OS);
Douglas Gregor2cf26342009-04-09 22:27:44 +000085}