blob: 4265c816f058f92e7a976ef4c4cc7f7e5dc0fe6a [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
Chris Lattner0b1fb982009-04-10 17:15:23 +000015#include "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 Gregor2cf26342009-04-09 22:27:44 +000021#include "llvm/Bitcode/BitstreamWriter.h"
22#include "llvm/System/Path.h"
23#include "llvm/Support/Compiler.h"
24#include "llvm/Support/raw_ostream.h"
25#include "llvm/Support/Streams.h"
26#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 Gregor2cf26342009-04-09 22:27:44 +000034 std::string OutFile;
Douglas Gregore7785042009-04-20 15:53:59 +000035 Sema *SemaPtr;
Douglas Gregor2cf26342009-04-09 22:27:44 +000036
37 public:
Chris Lattnerdf961c22009-04-10 18:08:30 +000038 explicit PCHGenerator(const Preprocessor &PP, const std::string &OutFile)
Douglas Gregore7785042009-04-20 15:53:59 +000039 : PP(PP), OutFile(OutFile), SemaPtr(0) { }
Douglas Gregor2cf26342009-04-09 22:27:44 +000040
Douglas Gregore7785042009-04-20 15:53:59 +000041 virtual void InitializeSema(Sema &S) { SemaPtr = &S; }
Douglas Gregor2cf26342009-04-09 22:27:44 +000042 virtual void HandleTranslationUnit(ASTContext &Ctx);
43 };
44}
45
46void PCHGenerator::HandleTranslationUnit(ASTContext &Ctx) {
Chris Lattner0b1fb982009-04-10 17:15:23 +000047 if (PP.getDiagnostics().hasErrorOccurred())
Douglas Gregor2cf26342009-04-09 22:27:44 +000048 return;
49
50 // Write the PCH contents into a buffer
51 std::vector<unsigned char> Buffer;
52 BitstreamWriter Stream(Buffer);
53 PCHWriter Writer(Stream);
54
55 // Emit the PCH file
Douglas Gregore7785042009-04-20 15:53:59 +000056 assert(SemaPtr && "No Sema?");
57 Writer.WritePCH(*SemaPtr);
Douglas Gregor2cf26342009-04-09 22:27:44 +000058
59 // Open up the PCH file.
60 std::string ErrMsg;
61 llvm::raw_fd_ostream Out(OutFile.c_str(), true, ErrMsg);
62
63 if (!ErrMsg.empty()) {
64 llvm::errs() << "PCH error: " << ErrMsg << "\n";
65 return;
66 }
67
68 // Write the generated bitstream to "Out".
69 Out.write((char *)&Buffer.front(), Buffer.size());
70
71 // Make sure it hits disk now.
72 Out.flush();
73}
74
Chris Lattnerdf961c22009-04-10 18:08:30 +000075ASTConsumer *clang::CreatePCHGenerator(const Preprocessor &PP,
Chris Lattner0b1fb982009-04-10 17:15:23 +000076 const std::string &OutFile) {
77 return new PCHGenerator(PP, OutFile);
Douglas Gregor2cf26342009-04-09 22:27:44 +000078}