blob: 3b7715ad283c8a476b5ff856a5be171816930a05 [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"
26#include "llvm/Support/Streams.h"
27#include <string>
28
29using namespace clang;
30using namespace llvm;
31
32namespace {
Douglas Gregore7785042009-04-20 15:53:59 +000033 class VISIBILITY_HIDDEN PCHGenerator : public SemaConsumer {
Chris Lattnerdf961c22009-04-10 18:08:30 +000034 const Preprocessor &PP;
Douglas Gregore650c8c2009-07-07 00:12:59 +000035 const char *isysroot;
Eli Friedman66d6f042009-05-18 22:20:00 +000036 llvm::raw_ostream *Out;
Douglas Gregore7785042009-04-20 15:53:59 +000037 Sema *SemaPtr;
Douglas Gregor4fed3f42009-04-27 18:38:38 +000038 MemorizeStatCalls *StatCalls; // owned by the FileManager
Douglas Gregore650c8c2009-07-07 00:12:59 +000039
Douglas Gregor2cf26342009-04-09 22:27:44 +000040 public:
Douglas Gregore650c8c2009-07-07 00:12:59 +000041 explicit PCHGenerator(const Preprocessor &PP,
42 const char *isysroot,
43 llvm::raw_ostream *Out);
Douglas Gregore7785042009-04-20 15:53:59 +000044 virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
Douglas Gregor2cf26342009-04-09 22:27:44 +000045 virtual void HandleTranslationUnit(ASTContext &Ctx);
46 };
47}
48
Douglas Gregore650c8c2009-07-07 00:12:59 +000049PCHGenerator::PCHGenerator(const Preprocessor &PP,
50 const char *isysroot,
51 llvm::raw_ostream *OS)
52 : PP(PP), isysroot(isysroot), Out(OS), SemaPtr(0), 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;
57 PP.getFileManager().setStatCache(StatCalls);
58}
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;
66 BitstreamWriter Stream(Buffer);
67 PCHWriter Writer(Stream);
68
69 // Emit the PCH file
Douglas Gregore7785042009-04-20 15:53:59 +000070 assert(SemaPtr && "No Sema?");
Douglas Gregore650c8c2009-07-07 00:12:59 +000071 Writer.WritePCH(*SemaPtr, StatCalls, 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,
82 const char *isysroot) {
83 return new PCHGenerator(PP, isysroot, OS);
Douglas Gregor2cf26342009-04-09 22:27:44 +000084}