blob: 8342359b9ffc3fba7a8ede251cf22a146a8f558f [file] [log] [blame]
Adrian Prantlbc068582015-07-08 01:00:30 +00001//===--- ObjectFilePCHContainerOperations.cpp -----------------------------===//
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#include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
11#include "CGDebugInfo.h"
12#include "CodeGenModule.h"
13#include "clang/AST/ASTContext.h"
14#include "clang/AST/DeclObjC.h"
15#include "clang/AST/Expr.h"
16#include "clang/AST/RecursiveASTVisitor.h"
17#include "clang/Basic/Diagnostic.h"
18#include "clang/Basic/TargetInfo.h"
19#include "clang/CodeGen/BackendUtil.h"
20#include "clang/Frontend/CodeGenOptions.h"
21#include "clang/Serialization/ASTWriter.h"
22#include "llvm/ADT/StringRef.h"
23#include "llvm/Bitcode/BitstreamReader.h"
24#include "llvm/DebugInfo/DWARF/DWARFContext.h"
25#include "llvm/IR/Constants.h"
26#include "llvm/IR/DataLayout.h"
27#include "llvm/IR/LLVMContext.h"
28#include "llvm/IR/Module.h"
29#include "llvm/Object/COFF.h"
30#include "llvm/Object/ObjectFile.h"
31#include "llvm/Support/TargetRegistry.h"
32#include <memory>
33using namespace clang;
34
35#define DEBUG_TYPE "pchcontainer"
36
37namespace {
Adrian Prantl5a88e1a2015-07-09 19:46:39 +000038class PCHContainerGenerator : public ASTConsumer {
Adrian Prantlbc068582015-07-08 01:00:30 +000039 DiagnosticsEngine &Diags;
40 const std::string MainFileName;
41 ASTContext *Ctx;
42 const HeaderSearchOptions &HeaderSearchOpts;
43 const PreprocessorOptions &PreprocessorOpts;
44 CodeGenOptions CodeGenOpts;
45 const TargetOptions TargetOpts;
46 const LangOptions LangOpts;
47 std::unique_ptr<llvm::LLVMContext> VMContext;
48 std::unique_ptr<llvm::Module> M;
49 std::unique_ptr<CodeGen::CodeGenModule> Builder;
50 raw_pwrite_stream *OS;
51 std::shared_ptr<PCHBuffer> Buffer;
52
53public:
Adrian Prantl5a88e1a2015-07-09 19:46:39 +000054 PCHContainerGenerator(DiagnosticsEngine &diags,
55 const HeaderSearchOptions &HSO,
56 const PreprocessorOptions &PPO, const TargetOptions &TO,
57 const LangOptions &LO, const std::string &MainFileName,
58 const std::string &OutputFileName,
59 raw_pwrite_stream *OS,
60 std::shared_ptr<PCHBuffer> Buffer)
Richard Smith0f99d6a2015-08-09 08:48:41 +000061 : Diags(diags), Ctx(nullptr), HeaderSearchOpts(HSO), PreprocessorOpts(PPO),
Adrian Prantl5a88e1a2015-07-09 19:46:39 +000062 TargetOpts(TO), LangOpts(LO), OS(OS), Buffer(Buffer) {
Adrian Prantlbc068582015-07-08 01:00:30 +000063 // The debug info output isn't affected by CodeModel and
64 // ThreadModel, but the backend expects them to be nonempty.
65 CodeGenOpts.CodeModel = "default";
66 CodeGenOpts.ThreadModel = "single";
Adrian Prantl6b21ab22015-08-27 19:46:20 +000067 CodeGenOpts.DebugTypeExtRefs = true;
Adrian Prantlbc068582015-07-08 01:00:30 +000068 CodeGenOpts.setDebugInfo(CodeGenOptions::FullDebugInfo);
69 CodeGenOpts.SplitDwarfFile = OutputFileName;
70 }
71
Adrian Prantl5a88e1a2015-07-09 19:46:39 +000072 virtual ~PCHContainerGenerator() {}
Adrian Prantlbc068582015-07-08 01:00:30 +000073
74 void Initialize(ASTContext &Context) override {
Richard Smith293534b2015-08-18 20:39:29 +000075 assert(!Ctx && "initialized multiple times");
Richard Smith0f99d6a2015-08-09 08:48:41 +000076
Adrian Prantlbc068582015-07-08 01:00:30 +000077 Ctx = &Context;
78 VMContext.reset(new llvm::LLVMContext());
79 M.reset(new llvm::Module(MainFileName, *VMContext));
Eric Christopher964a5f32015-08-05 23:48:05 +000080 M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString());
Mehdi Aminica3cf9e2015-07-24 16:04:29 +000081 Builder.reset(new CodeGen::CodeGenModule(
82 *Ctx, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags));
Adrian Prantlbc068582015-07-08 01:00:30 +000083 }
84
85 /// Emit a container holding the serialized AST.
86 void HandleTranslationUnit(ASTContext &Ctx) override {
87 assert(M && VMContext && Builder);
88 // Delete these on function exit.
89 std::unique_ptr<llvm::LLVMContext> VMContext = std::move(this->VMContext);
90 std::unique_ptr<llvm::Module> M = std::move(this->M);
91 std::unique_ptr<CodeGen::CodeGenModule> Builder = std::move(this->Builder);
92
93 if (Diags.hasErrorOccurred())
94 return;
95
96 M->setTargetTriple(Ctx.getTargetInfo().getTriple().getTriple());
Eric Christopher964a5f32015-08-05 23:48:05 +000097 M->setDataLayout(Ctx.getTargetInfo().getDataLayoutString());
Adrian Prantlbc068582015-07-08 01:00:30 +000098
99 // Finalize the Builder.
100 if (Builder)
101 Builder->Release();
102
103 // Ensure the target exists.
104 std::string Error;
105 auto Triple = Ctx.getTargetInfo().getTriple();
106 if (!llvm::TargetRegistry::lookupTarget(Triple.getTriple(), Error))
107 llvm::report_fatal_error(Error);
108
109 // Emit the serialized Clang AST into its own section.
110 assert(Buffer->IsComplete && "serialization did not complete");
111 auto &SerializedAST = Buffer->Data;
112 auto Size = SerializedAST.size();
113 auto Int8Ty = llvm::Type::getInt8Ty(*VMContext);
114 auto *Ty = llvm::ArrayType::get(Int8Ty, Size);
Adrian Prantl5a88e1a2015-07-09 19:46:39 +0000115 auto *Data = llvm::ConstantDataArray::getString(
116 *VMContext, StringRef(SerializedAST.data(), Size),
117 /*AddNull=*/false);
Adrian Prantlbc068582015-07-08 01:00:30 +0000118 auto *ASTSym = new llvm::GlobalVariable(
119 *M, Ty, /*constant*/ true, llvm::GlobalVariable::InternalLinkage, Data,
120 "__clang_ast");
121 // The on-disk hashtable needs to be aligned.
122 ASTSym->setAlignment(8);
123
124 // Mach-O also needs a segment name.
125 if (Triple.isOSBinFormatMachO())
126 ASTSym->setSection("__CLANG,__clangast");
127 // COFF has an eight character length limit.
128 else if (Triple.isOSBinFormatCOFF())
129 ASTSym->setSection("clangast");
130 else
131 ASTSym->setSection("__clangast");
132
133 DEBUG({
Adrian Prantl5a88e1a2015-07-09 19:46:39 +0000134 // Print the IR for the PCH container to the debug output.
135 llvm::SmallString<0> Buffer;
136 llvm::raw_svector_ostream OS(Buffer);
137 clang::EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
Eric Christopher964a5f32015-08-05 23:48:05 +0000138 Ctx.getTargetInfo().getDataLayoutString(),
Adrian Prantl5a88e1a2015-07-09 19:46:39 +0000139 M.get(), BackendAction::Backend_EmitLL, &OS);
Adrian Prantl5a88e1a2015-07-09 19:46:39 +0000140 llvm::dbgs() << Buffer;
141 });
Adrian Prantlbc068582015-07-08 01:00:30 +0000142
143 // Use the LLVM backend to emit the pch container.
144 clang::EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
Eric Christopher964a5f32015-08-05 23:48:05 +0000145 Ctx.getTargetInfo().getDataLayoutString(),
Adrian Prantlbc068582015-07-08 01:00:30 +0000146 M.get(), BackendAction::Backend_EmitObj, OS);
147
148 // Make sure the pch container hits disk.
149 OS->flush();
150
151 // Free the memory for the temporary buffer.
152 llvm::SmallVector<char, 0> Empty;
153 SerializedAST = std::move(Empty);
154 }
155};
Adrian Prantl5a88e1a2015-07-09 19:46:39 +0000156
157} // namespace
Adrian Prantlbc068582015-07-08 01:00:30 +0000158
159std::unique_ptr<ASTConsumer>
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000160ObjectFilePCHContainerWriter::CreatePCHContainerGenerator(
Adrian Prantlbc068582015-07-08 01:00:30 +0000161 DiagnosticsEngine &Diags, const HeaderSearchOptions &HSO,
162 const PreprocessorOptions &PPO, const TargetOptions &TO,
163 const LangOptions &LO, const std::string &MainFileName,
164 const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
165 std::shared_ptr<PCHBuffer> Buffer) const {
Adrian Prantl5a88e1a2015-07-09 19:46:39 +0000166 return llvm::make_unique<PCHContainerGenerator>(
167 Diags, HSO, PPO, TO, LO, MainFileName, OutputFileName, OS, Buffer);
Adrian Prantlbc068582015-07-08 01:00:30 +0000168}
169
Adrian Prantlfb2398d2015-07-17 01:19:54 +0000170void ObjectFilePCHContainerReader::ExtractPCH(
Adrian Prantlbc068582015-07-08 01:00:30 +0000171 llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const {
172 if (auto OF = llvm::object::ObjectFile::createObjectFile(Buffer)) {
173 auto *Obj = OF.get().get();
174 bool IsCOFF = isa<llvm::object::COFFObjectFile>(Obj);
175 // Find the clang AST section in the container.
176 for (auto &Section : OF->get()->sections()) {
177 StringRef Name;
178 Section.getName(Name);
179 if ((!IsCOFF && Name == "__clangast") ||
180 ( IsCOFF && Name == "clangast")) {
181 StringRef Buf;
182 Section.getContents(Buf);
183 StreamFile.init((const unsigned char *)Buf.begin(),
184 (const unsigned char *)Buf.end());
185 return;
186 }
187 }
188 }
189
190 // As a fallback, treat the buffer as a raw AST.
191 StreamFile.init((const unsigned char *)Buffer.getBufferStart(),
192 (const unsigned char *)Buffer.getBufferEnd());
193 return;
194}