blob: 0e4f83ff09f5a14bb6fdf7269cb1b1258fb1dfee [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"
Douglas Gregor2cf26342009-04-09 22:27:44 +000026#include <string>
27
28using namespace clang;
29using namespace llvm;
30
31namespace {
Douglas Gregore7785042009-04-20 15:53:59 +000032 class VISIBILITY_HIDDEN PCHGenerator : public SemaConsumer {
Chris Lattnerdf961c22009-04-10 18:08:30 +000033 const Preprocessor &PP;
Douglas Gregore650c8c2009-07-07 00:12:59 +000034 const char *isysroot;
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
Mike Stump1eb44332009-09-09 15:08:12 +000038
Douglas Gregor2cf26342009-04-09 22:27:44 +000039 public:
Mike Stump1eb44332009-09-09 15:08:12 +000040 explicit PCHGenerator(const Preprocessor &PP,
Douglas Gregore650c8c2009-07-07 00:12:59 +000041 const char *isysroot,
42 llvm::raw_ostream *Out);
Douglas Gregore7785042009-04-20 15:53:59 +000043 virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
Douglas Gregor2cf26342009-04-09 22:27:44 +000044 virtual void HandleTranslationUnit(ASTContext &Ctx);
45 };
46}
47
Mike Stump1eb44332009-09-09 15:08:12 +000048PCHGenerator::PCHGenerator(const Preprocessor &PP,
Douglas Gregore650c8c2009-07-07 00:12:59 +000049 const char *isysroot,
50 llvm::raw_ostream *OS)
Mike Stump1eb44332009-09-09 15:08:12 +000051 : PP(PP), isysroot(isysroot), Out(OS), SemaPtr(0), StatCalls(0) {
Douglas Gregor4fed3f42009-04-27 18:38:38 +000052
53 // Install a stat() listener to keep track of all of the stat()
54 // calls.
55 StatCalls = new MemorizeStatCalls;
Douglas Gregor52e71082009-10-16 18:18:30 +000056 PP.getFileManager().addStatCache(StatCalls, /*AtBeginning=*/true);
Douglas Gregor4fed3f42009-04-27 18:38:38 +000057}
58
Douglas Gregor2cf26342009-04-09 22:27:44 +000059void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
Chris Lattner0b1fb982009-04-10 17:15:23 +000060 if (PP.getDiagnostics().hasErrorOccurred())
Douglas Gregor2cf26342009-04-09 22:27:44 +000061 return;
62
Douglas Gregore650c8c2009-07-07 00:12:59 +000063 // Write the PCH contents into a buffer
Douglas Gregor2cf26342009-04-09 22:27:44 +000064 std::vector<unsigned char> Buffer;
65 BitstreamWriter Stream(Buffer);
66 PCHWriter Writer(Stream);
67
68 // Emit the PCH file
Douglas Gregore7785042009-04-20 15:53:59 +000069 assert(SemaPtr && "No Sema?");
Douglas Gregore650c8c2009-07-07 00:12:59 +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();
Douglas Gregor2cf26342009-04-09 22:27:44 +000077}
78
Chris Lattnerdf961c22009-04-10 18:08:30 +000079ASTConsumer *clang::CreatePCHGenerator(const Preprocessor &PP,
Douglas Gregore650c8c2009-07-07 00:12:59 +000080 llvm::raw_ostream *OS,
81 const char *isysroot) {
82 return new PCHGenerator(PP, isysroot, OS);
Douglas Gregor2cf26342009-04-09 22:27:44 +000083}