Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 1 | //===--- 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> |
| 33 | using namespace clang; |
| 34 | |
| 35 | #define DEBUG_TYPE "pchcontainer" |
| 36 | |
| 37 | namespace { |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 38 | class PCHContainerGenerator : public ASTConsumer { |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 39 | 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 | |
| 53 | public: |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 54 | 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 Smith | 0f99d6a | 2015-08-09 08:48:41 +0000 | [diff] [blame] | 61 | : Diags(diags), Ctx(nullptr), HeaderSearchOpts(HSO), PreprocessorOpts(PPO), |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 62 | TargetOpts(TO), LangOpts(LO), OS(OS), Buffer(Buffer) { |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 63 | // 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"; |
| 67 | CodeGenOpts.setDebugInfo(CodeGenOptions::FullDebugInfo); |
| 68 | CodeGenOpts.SplitDwarfFile = OutputFileName; |
| 69 | } |
| 70 | |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 71 | virtual ~PCHContainerGenerator() {} |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 72 | |
| 73 | void Initialize(ASTContext &Context) override { |
Richard Smith | 293534b | 2015-08-18 20:39:29 +0000 | [diff] [blame] | 74 | assert(!Ctx && "initialized multiple times"); |
Richard Smith | 0f99d6a | 2015-08-09 08:48:41 +0000 | [diff] [blame] | 75 | |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 76 | Ctx = &Context; |
| 77 | VMContext.reset(new llvm::LLVMContext()); |
| 78 | M.reset(new llvm::Module(MainFileName, *VMContext)); |
Eric Christopher | 964a5f3 | 2015-08-05 23:48:05 +0000 | [diff] [blame] | 79 | M->setDataLayout(Ctx->getTargetInfo().getDataLayoutString()); |
Mehdi Amini | ca3cf9e | 2015-07-24 16:04:29 +0000 | [diff] [blame] | 80 | Builder.reset(new CodeGen::CodeGenModule( |
| 81 | *Ctx, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags)); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | /// Emit a container holding the serialized AST. |
| 85 | void HandleTranslationUnit(ASTContext &Ctx) override { |
| 86 | assert(M && VMContext && Builder); |
| 87 | // Delete these on function exit. |
| 88 | std::unique_ptr<llvm::LLVMContext> VMContext = std::move(this->VMContext); |
| 89 | std::unique_ptr<llvm::Module> M = std::move(this->M); |
| 90 | std::unique_ptr<CodeGen::CodeGenModule> Builder = std::move(this->Builder); |
| 91 | |
| 92 | if (Diags.hasErrorOccurred()) |
| 93 | return; |
| 94 | |
| 95 | M->setTargetTriple(Ctx.getTargetInfo().getTriple().getTriple()); |
Eric Christopher | 964a5f3 | 2015-08-05 23:48:05 +0000 | [diff] [blame] | 96 | M->setDataLayout(Ctx.getTargetInfo().getDataLayoutString()); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 97 | |
| 98 | // Finalize the Builder. |
| 99 | if (Builder) |
| 100 | Builder->Release(); |
| 101 | |
| 102 | // Ensure the target exists. |
| 103 | std::string Error; |
| 104 | auto Triple = Ctx.getTargetInfo().getTriple(); |
| 105 | if (!llvm::TargetRegistry::lookupTarget(Triple.getTriple(), Error)) |
| 106 | llvm::report_fatal_error(Error); |
| 107 | |
| 108 | // Emit the serialized Clang AST into its own section. |
| 109 | assert(Buffer->IsComplete && "serialization did not complete"); |
| 110 | auto &SerializedAST = Buffer->Data; |
| 111 | auto Size = SerializedAST.size(); |
| 112 | auto Int8Ty = llvm::Type::getInt8Ty(*VMContext); |
| 113 | auto *Ty = llvm::ArrayType::get(Int8Ty, Size); |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 114 | auto *Data = llvm::ConstantDataArray::getString( |
| 115 | *VMContext, StringRef(SerializedAST.data(), Size), |
| 116 | /*AddNull=*/false); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 117 | auto *ASTSym = new llvm::GlobalVariable( |
| 118 | *M, Ty, /*constant*/ true, llvm::GlobalVariable::InternalLinkage, Data, |
| 119 | "__clang_ast"); |
| 120 | // The on-disk hashtable needs to be aligned. |
| 121 | ASTSym->setAlignment(8); |
| 122 | |
| 123 | // Mach-O also needs a segment name. |
| 124 | if (Triple.isOSBinFormatMachO()) |
| 125 | ASTSym->setSection("__CLANG,__clangast"); |
| 126 | // COFF has an eight character length limit. |
| 127 | else if (Triple.isOSBinFormatCOFF()) |
| 128 | ASTSym->setSection("clangast"); |
| 129 | else |
| 130 | ASTSym->setSection("__clangast"); |
| 131 | |
| 132 | DEBUG({ |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 133 | // Print the IR for the PCH container to the debug output. |
| 134 | llvm::SmallString<0> Buffer; |
| 135 | llvm::raw_svector_ostream OS(Buffer); |
| 136 | clang::EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts, |
Eric Christopher | 964a5f3 | 2015-08-05 23:48:05 +0000 | [diff] [blame] | 137 | Ctx.getTargetInfo().getDataLayoutString(), |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 138 | M.get(), BackendAction::Backend_EmitLL, &OS); |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 139 | llvm::dbgs() << Buffer; |
| 140 | }); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 141 | |
| 142 | // Use the LLVM backend to emit the pch container. |
| 143 | clang::EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts, |
Eric Christopher | 964a5f3 | 2015-08-05 23:48:05 +0000 | [diff] [blame] | 144 | Ctx.getTargetInfo().getDataLayoutString(), |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 145 | M.get(), BackendAction::Backend_EmitObj, OS); |
| 146 | |
| 147 | // Make sure the pch container hits disk. |
| 148 | OS->flush(); |
| 149 | |
| 150 | // Free the memory for the temporary buffer. |
| 151 | llvm::SmallVector<char, 0> Empty; |
| 152 | SerializedAST = std::move(Empty); |
| 153 | } |
| 154 | }; |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 155 | |
| 156 | } // namespace |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 157 | |
| 158 | std::unique_ptr<ASTConsumer> |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 159 | ObjectFilePCHContainerWriter::CreatePCHContainerGenerator( |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 160 | DiagnosticsEngine &Diags, const HeaderSearchOptions &HSO, |
| 161 | const PreprocessorOptions &PPO, const TargetOptions &TO, |
| 162 | const LangOptions &LO, const std::string &MainFileName, |
| 163 | const std::string &OutputFileName, llvm::raw_pwrite_stream *OS, |
| 164 | std::shared_ptr<PCHBuffer> Buffer) const { |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 165 | return llvm::make_unique<PCHContainerGenerator>( |
| 166 | Diags, HSO, PPO, TO, LO, MainFileName, OutputFileName, OS, Buffer); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 169 | void ObjectFilePCHContainerReader::ExtractPCH( |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 170 | llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const { |
| 171 | if (auto OF = llvm::object::ObjectFile::createObjectFile(Buffer)) { |
| 172 | auto *Obj = OF.get().get(); |
| 173 | bool IsCOFF = isa<llvm::object::COFFObjectFile>(Obj); |
| 174 | // Find the clang AST section in the container. |
| 175 | for (auto &Section : OF->get()->sections()) { |
| 176 | StringRef Name; |
| 177 | Section.getName(Name); |
| 178 | if ((!IsCOFF && Name == "__clangast") || |
| 179 | ( IsCOFF && Name == "clangast")) { |
| 180 | StringRef Buf; |
| 181 | Section.getContents(Buf); |
| 182 | StreamFile.init((const unsigned char *)Buf.begin(), |
| 183 | (const unsigned char *)Buf.end()); |
| 184 | return; |
| 185 | } |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | // As a fallback, treat the buffer as a raw AST. |
| 190 | StreamFile.init((const unsigned char *)Buffer.getBufferStart(), |
| 191 | (const unsigned char *)Buffer.getBufferEnd()); |
| 192 | return; |
| 193 | } |