blob: 6251bac04758550f837892f55d7e13a4e847ef14 [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
Mike Stump1eb44332009-09-09 15:08:12 +000035
Douglas Gregor2cf26342009-04-09 22:27:44 +000036 public:
Mike Stump1eb44332009-09-09 15:08:12 +000037 explicit PCHGenerator(const Preprocessor &PP,
Douglas Gregore650c8c2009-07-07 00:12:59 +000038 const char *isysroot,
39 llvm::raw_ostream *Out);
Douglas Gregore7785042009-04-20 15:53:59 +000040 virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
Douglas Gregor2cf26342009-04-09 22:27:44 +000041 virtual void HandleTranslationUnit(ASTContext &Ctx);
42 };
43}
44
Mike Stump1eb44332009-09-09 15:08:12 +000045PCHGenerator::PCHGenerator(const Preprocessor &PP,
Douglas Gregore650c8c2009-07-07 00:12:59 +000046 const char *isysroot,
47 llvm::raw_ostream *OS)
Mike Stump1eb44332009-09-09 15:08:12 +000048 : PP(PP), isysroot(isysroot), Out(OS), SemaPtr(0), StatCalls(0) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +000049
50 // Install a stat() listener to keep track of all of the stat()
51 // calls.
52 StatCalls = new MemorizeStatCalls;
Douglas Gregor52e71082009-10-16 18:18:30 +000053 PP.getFileManager().addStatCache(StatCalls, /*AtBeginning=*/true);
Douglas Gregor4fed3f42009-04-27 18:38:38 +000054}
55
Douglas Gregor2cf26342009-04-09 22:27:44 +000056void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
Chris Lattner0b1fb982009-04-10 17:15:23 +000057 if (PP.getDiagnostics().hasErrorOccurred())
Douglas Gregor2cf26342009-04-09 22:27:44 +000058 return;
59
Douglas Gregore650c8c2009-07-07 00:12:59 +000060 // Write the PCH contents into a buffer
Douglas Gregor2cf26342009-04-09 22:27:44 +000061 std::vector<unsigned char> Buffer;
Benjamin Kramerbd218282009-11-28 10:07:24 +000062 llvm::BitstreamWriter Stream(Buffer);
Douglas Gregor2cf26342009-04-09 22:27:44 +000063 PCHWriter Writer(Stream);
64
65 // Emit the PCH file
Douglas Gregore7785042009-04-20 15:53:59 +000066 assert(SemaPtr && "No Sema?");
Douglas Gregore650c8c2009-07-07 00:12:59 +000067 Writer.WritePCH(*SemaPtr, StatCalls, isysroot);
Douglas Gregor2cf26342009-04-09 22:27:44 +000068
Douglas Gregor2cf26342009-04-09 22:27:44 +000069 // Write the generated bitstream to "Out".
Eli Friedman66d6f042009-05-18 22:20:00 +000070 Out->write((char *)&Buffer.front(), Buffer.size());
Douglas Gregor2cf26342009-04-09 22:27:44 +000071
72 // Make sure it hits disk now.
Eli Friedman66d6f042009-05-18 22:20:00 +000073 Out->flush();
Douglas Gregor2cf26342009-04-09 22:27:44 +000074}
75
Chris Lattnerdf961c22009-04-10 18:08:30 +000076ASTConsumer *clang::CreatePCHGenerator(const Preprocessor &PP,
Douglas Gregore650c8c2009-07-07 00:12:59 +000077 llvm::raw_ostream *OS,
78 const char *isysroot) {
79 return new PCHGenerator(PP, isysroot, OS);
Douglas Gregor2cf26342009-04-09 22:27:44 +000080}