blob: 9cd694e05c332c90f5c794bf10cd2f76cb920eb3 [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
Sebastian Redl3c7f4132010-08-18 23:57:06 +000011// ASTConsumer that generates a PCH file.
Douglas Gregor2cf26342009-04-09 22:27:44 +000012//
13//===----------------------------------------------------------------------===//
14
Eli Friedman39d7c4d2009-05-18 22:50:54 +000015#include "clang/Frontend/ASTConsumers.h"
Sebastian Redl7faa2ec2010-08-18 23:56:37 +000016#include "clang/Serialization/ASTWriter.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"
Chris Lattner10e286a2010-11-23 19:19:34 +000022#include "clang/Basic/FileSystemStatCache.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000023#include "llvm/Bitcode/BitstreamWriter.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000024#include "llvm/Support/raw_ostream.h"
Douglas Gregor2cf26342009-04-09 22:27:44 +000025#include <string>
Douglas Gregor4947e252011-07-22 06:03:18 +000026#include <string.h>
27#include <stdlib.h>
Douglas Gregor2cf26342009-04-09 22:27:44 +000028
29using namespace clang;
Douglas Gregor2cf26342009-04-09 22:27:44 +000030
Mike Stump1eb44332009-09-09 15:08:12 +000031PCHGenerator::PCHGenerator(const Preprocessor &PP,
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +000032 const std::string &OutputFile,
Sebastian Redlffaab3e2010-07-30 00:29:29 +000033 bool Chaining,
Douglas Gregore650c8c2009-07-07 00:12:59 +000034 const char *isysroot,
35 llvm::raw_ostream *OS)
Douglas Gregor4947e252011-07-22 06:03:18 +000036 : PP(PP), OutputFile(OutputFile), isysroot(0), Out(OS), SemaPtr(0),
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000037 StatCalls(0), Stream(Buffer), Writer(Stream), Chaining(Chaining) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +000038 // Install a stat() listener to keep track of all of the stat()
39 // calls.
Chris Lattner10e286a2010-11-23 19:19:34 +000040 StatCalls = new MemorizeStatCalls();
Sebastian Redl1476ed42010-07-16 16:36:56 +000041 // If we have a chain, we want new stat calls only, so install the memorizer
Sebastian Redlc43b54c2010-08-18 23:56:43 +000042 // *after* the already installed ASTReader's stat cache.
Sebastian Redl1476ed42010-07-16 16:36:56 +000043 PP.getFileManager().addStatCache(StatCalls,
Sebastian Redlffaab3e2010-07-30 00:29:29 +000044 /*AtBeginning=*/!Chaining);
Douglas Gregor4947e252011-07-22 06:03:18 +000045
46 if (isysroot)
47 this->isysroot = strdup(isysroot);
48}
49
50PCHGenerator::~PCHGenerator() {
51 free((void*)isysroot);
Douglas Gregor4fed3f42009-04-27 18:38:38 +000052}
53
Douglas Gregor2cf26342009-04-09 22:27:44 +000054void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
Chris Lattner0b1fb982009-04-10 17:15:23 +000055 if (PP.getDiagnostics().hasErrorOccurred())
Douglas Gregor2cf26342009-04-09 22:27:44 +000056 return;
57
Douglas Gregor89d99802010-11-30 06:16:57 +000058 // Set up the serialization listener.
59 Writer.SetSerializationListener(GetASTSerializationListener());
60
Douglas Gregor2cf26342009-04-09 22:27:44 +000061 // Emit the PCH file
Douglas Gregore7785042009-04-20 15:53:59 +000062 assert(SemaPtr && "No Sema?");
Argyrios Kyrtzidis8e3df4d2011-02-15 17:54:22 +000063 Writer.WriteAST(*SemaPtr, StatCalls, OutputFile, isysroot);
Douglas Gregor2cf26342009-04-09 22:27:44 +000064
Douglas Gregor2cf26342009-04-09 22:27:44 +000065 // Write the generated bitstream to "Out".
Eli Friedman66d6f042009-05-18 22:20:00 +000066 Out->write((char *)&Buffer.front(), Buffer.size());
Douglas Gregor2cf26342009-04-09 22:27:44 +000067
68 // Make sure it hits disk now.
Eli Friedman66d6f042009-05-18 22:20:00 +000069 Out->flush();
Sebastian Redl30c514c2010-07-14 23:45:08 +000070
71 // Free up some memory, in case the process is kept alive.
72 Buffer.clear();
Douglas Gregor2cf26342009-04-09 22:27:44 +000073}
74
Argyrios Kyrtzidis7b903402010-10-24 17:26:36 +000075ASTMutationListener *PCHGenerator::GetASTMutationListener() {
76 if (Chaining)
77 return &Writer;
78 return 0;
79}
80
Douglas Gregor89d99802010-11-30 06:16:57 +000081ASTSerializationListener *PCHGenerator::GetASTSerializationListener() {
82 return 0;
83}
84
Sebastian Redl571db7f2010-08-18 23:56:56 +000085ASTDeserializationListener *PCHGenerator::GetASTDeserializationListener() {
Sebastian Redlffaab3e2010-07-30 00:29:29 +000086 return &Writer;
87}