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" |
Adrian Prantl | 0391406 | 2015-09-18 22:10:59 +0000 | [diff] [blame] | 21 | #include "clang/Frontend/CompilerInstance.h" |
Adrian Prantl | 9402cef | 2015-09-20 16:51:35 +0000 | [diff] [blame] | 22 | #include "clang/Lex/HeaderSearch.h" |
Adrian Prantl | 3a2d494 | 2016-01-22 23:30:56 +0000 | [diff] [blame] | 23 | #include "clang/Lex/Preprocessor.h" |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 24 | #include "clang/Serialization/ASTWriter.h" |
| 25 | #include "llvm/ADT/StringRef.h" |
| 26 | #include "llvm/Bitcode/BitstreamReader.h" |
| 27 | #include "llvm/DebugInfo/DWARF/DWARFContext.h" |
| 28 | #include "llvm/IR/Constants.h" |
| 29 | #include "llvm/IR/DataLayout.h" |
| 30 | #include "llvm/IR/LLVMContext.h" |
| 31 | #include "llvm/IR/Module.h" |
| 32 | #include "llvm/Object/COFF.h" |
| 33 | #include "llvm/Object/ObjectFile.h" |
Adrian Prantl | 3a2d494 | 2016-01-22 23:30:56 +0000 | [diff] [blame] | 34 | #include "llvm/Support/Path.h" |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 35 | #include "llvm/Support/TargetRegistry.h" |
| 36 | #include <memory> |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 37 | |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 38 | using namespace clang; |
| 39 | |
| 40 | #define DEBUG_TYPE "pchcontainer" |
| 41 | |
| 42 | namespace { |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 43 | class PCHContainerGenerator : public ASTConsumer { |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 44 | DiagnosticsEngine &Diags; |
| 45 | const std::string MainFileName; |
Adrian Prantl | aa5d08d | 2016-01-22 21:14:41 +0000 | [diff] [blame] | 46 | const std::string OutputFileName; |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 47 | ASTContext *Ctx; |
Adrian Prantl | 9402cef | 2015-09-20 16:51:35 +0000 | [diff] [blame] | 48 | ModuleMap &MMap; |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 49 | const HeaderSearchOptions &HeaderSearchOpts; |
| 50 | const PreprocessorOptions &PreprocessorOpts; |
| 51 | CodeGenOptions CodeGenOpts; |
| 52 | const TargetOptions TargetOpts; |
| 53 | const LangOptions LangOpts; |
| 54 | std::unique_ptr<llvm::LLVMContext> VMContext; |
| 55 | std::unique_ptr<llvm::Module> M; |
| 56 | std::unique_ptr<CodeGen::CodeGenModule> Builder; |
| 57 | raw_pwrite_stream *OS; |
| 58 | std::shared_ptr<PCHBuffer> Buffer; |
| 59 | |
Adrian Prantl | 4aa2b3a | 2015-09-08 19:20:27 +0000 | [diff] [blame] | 60 | /// Visit every type and emit debug info for it. |
| 61 | struct DebugTypeVisitor : public RecursiveASTVisitor<DebugTypeVisitor> { |
| 62 | clang::CodeGen::CGDebugInfo &DI; |
| 63 | ASTContext &Ctx; |
Adrian Prantl | cd97501 | 2016-01-19 23:42:44 +0000 | [diff] [blame] | 64 | DebugTypeVisitor(clang::CodeGen::CGDebugInfo &DI, ASTContext &Ctx) |
| 65 | : DI(DI), Ctx(Ctx) {} |
Adrian Prantl | 4aa2b3a | 2015-09-08 19:20:27 +0000 | [diff] [blame] | 66 | |
| 67 | /// Determine whether this type can be represented in DWARF. |
| 68 | static bool CanRepresent(const Type *Ty) { |
| 69 | return !Ty->isDependentType() && !Ty->isUndeducedType(); |
| 70 | } |
| 71 | |
Adrian Prantl | 85d938a | 2015-09-21 17:48:37 +0000 | [diff] [blame] | 72 | bool VisitImportDecl(ImportDecl *D) { |
| 73 | auto *Import = cast<ImportDecl>(D); |
| 74 | if (!Import->getImportedOwningModule()) |
| 75 | DI.EmitImportDecl(*Import); |
| 76 | return true; |
| 77 | } |
| 78 | |
Adrian Prantl | 4aa2b3a | 2015-09-08 19:20:27 +0000 | [diff] [blame] | 79 | bool VisitTypeDecl(TypeDecl *D) { |
Adrian Prantl | b3b821f | 2016-01-06 19:22:19 +0000 | [diff] [blame] | 80 | // TagDecls may be deferred until after all decls have been merged and we |
| 81 | // know the complete type. Pure forward declarations will be skipped, but |
| 82 | // they don't need to be emitted into the module anyway. |
Adrian Prantl | cd97501 | 2016-01-19 23:42:44 +0000 | [diff] [blame] | 83 | if (auto *TD = dyn_cast<TagDecl>(D)) |
| 84 | if (!TD->isCompleteDefinition()) |
Adrian Prantl | b3b821f | 2016-01-06 19:22:19 +0000 | [diff] [blame] | 85 | return true; |
| 86 | |
Adrian Prantl | 4aa2b3a | 2015-09-08 19:20:27 +0000 | [diff] [blame] | 87 | QualType QualTy = Ctx.getTypeDeclType(D); |
| 88 | if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr())) |
| 89 | DI.getOrCreateStandaloneType(QualTy, D->getLocation()); |
| 90 | return true; |
| 91 | } |
| 92 | |
Adrian Prantl | 748a6cd | 2015-09-08 20:41:52 +0000 | [diff] [blame] | 93 | bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) { |
| 94 | QualType QualTy(D->getTypeForDecl(), 0); |
| 95 | if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr())) |
| 96 | DI.getOrCreateStandaloneType(QualTy, D->getLocation()); |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | bool VisitFunctionDecl(FunctionDecl *D) { |
| 101 | if (isa<CXXMethodDecl>(D)) |
| 102 | // This is not yet supported. Constructing the `this' argument |
| 103 | // mandates a CodeGenFunction. |
| 104 | return true; |
| 105 | |
| 106 | SmallVector<QualType, 16> ArgTypes; |
| 107 | for (auto i : D->params()) |
| 108 | ArgTypes.push_back(i->getType()); |
| 109 | QualType RetTy = D->getReturnType(); |
| 110 | QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes, |
| 111 | FunctionProtoType::ExtProtoInfo()); |
| 112 | if (CanRepresent(FnTy.getTypePtr())) |
| 113 | DI.EmitFunctionDecl(D, D->getLocation(), FnTy); |
| 114 | return true; |
| 115 | } |
| 116 | |
| 117 | bool VisitObjCMethodDecl(ObjCMethodDecl *D) { |
| 118 | if (!D->getClassInterface()) |
| 119 | return true; |
| 120 | |
| 121 | bool selfIsPseudoStrong, selfIsConsumed; |
| 122 | SmallVector<QualType, 16> ArgTypes; |
| 123 | ArgTypes.push_back(D->getSelfType(Ctx, D->getClassInterface(), |
| 124 | selfIsPseudoStrong, selfIsConsumed)); |
| 125 | ArgTypes.push_back(Ctx.getObjCSelType()); |
| 126 | for (auto i : D->params()) |
| 127 | ArgTypes.push_back(i->getType()); |
| 128 | QualType RetTy = D->getReturnType(); |
| 129 | QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes, |
| 130 | FunctionProtoType::ExtProtoInfo()); |
| 131 | if (CanRepresent(FnTy.getTypePtr())) |
| 132 | DI.EmitFunctionDecl(D, D->getLocation(), FnTy); |
| 133 | return true; |
| 134 | } |
Adrian Prantl | 4aa2b3a | 2015-09-08 19:20:27 +0000 | [diff] [blame] | 135 | }; |
| 136 | |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 137 | public: |
Adrian Prantl | 1e63b2b | 2015-09-19 21:42:52 +0000 | [diff] [blame] | 138 | PCHContainerGenerator(CompilerInstance &CI, const std::string &MainFileName, |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 139 | const std::string &OutputFileName, |
| 140 | raw_pwrite_stream *OS, |
| 141 | std::shared_ptr<PCHBuffer> Buffer) |
Adrian Prantl | aa5d08d | 2016-01-22 21:14:41 +0000 | [diff] [blame] | 142 | : Diags(CI.getDiagnostics()), MainFileName(MainFileName), |
| 143 | OutputFileName(OutputFileName), Ctx(nullptr), |
Adrian Prantl | 9402cef | 2015-09-20 16:51:35 +0000 | [diff] [blame] | 144 | MMap(CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()), |
Adrian Prantl | 1e63b2b | 2015-09-19 21:42:52 +0000 | [diff] [blame] | 145 | HeaderSearchOpts(CI.getHeaderSearchOpts()), |
Adrian Prantl | 0391406 | 2015-09-18 22:10:59 +0000 | [diff] [blame] | 146 | PreprocessorOpts(CI.getPreprocessorOpts()), |
| 147 | TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()), OS(OS), |
| 148 | Buffer(Buffer) { |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 149 | // The debug info output isn't affected by CodeModel and |
| 150 | // ThreadModel, but the backend expects them to be nonempty. |
| 151 | CodeGenOpts.CodeModel = "default"; |
| 152 | CodeGenOpts.ThreadModel = "single"; |
Adrian Prantl | 6b21ab2 | 2015-08-27 19:46:20 +0000 | [diff] [blame] | 153 | CodeGenOpts.DebugTypeExtRefs = true; |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 154 | CodeGenOpts.setDebugInfo(codegenoptions::FullDebugInfo); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 157 | ~PCHContainerGenerator() override = default; |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 158 | |
| 159 | void Initialize(ASTContext &Context) override { |
Richard Smith | 293534b | 2015-08-18 20:39:29 +0000 | [diff] [blame] | 160 | assert(!Ctx && "initialized multiple times"); |
Richard Smith | 0f99d6a | 2015-08-09 08:48:41 +0000 | [diff] [blame] | 161 | |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 162 | Ctx = &Context; |
| 163 | VMContext.reset(new llvm::LLVMContext()); |
| 164 | M.reset(new llvm::Module(MainFileName, *VMContext)); |
James Y Knight | b214cbc | 2016-03-04 19:00:41 +0000 | [diff] [blame] | 165 | M->setDataLayout(Ctx->getTargetInfo().getDataLayout()); |
Mehdi Amini | ca3cf9e | 2015-07-24 16:04:29 +0000 | [diff] [blame] | 166 | Builder.reset(new CodeGen::CodeGenModule( |
| 167 | *Ctx, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags)); |
Adrian Prantl | 3a2d494 | 2016-01-22 23:30:56 +0000 | [diff] [blame] | 168 | |
| 169 | // Prepare CGDebugInfo to emit debug info for a clang module. |
| 170 | auto *DI = Builder->getModuleDebugInfo(); |
| 171 | StringRef ModuleName = llvm::sys::path::filename(MainFileName); |
| 172 | DI->setPCHDescriptor({ModuleName, "", OutputFileName, ~1ULL}); |
| 173 | DI->setModuleMap(MMap); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Adrian Prantl | 4aa2b3a | 2015-09-08 19:20:27 +0000 | [diff] [blame] | 176 | bool HandleTopLevelDecl(DeclGroupRef D) override { |
Adrian Prantl | abdd6fc | 2015-10-23 16:51:32 +0000 | [diff] [blame] | 177 | if (Diags.hasErrorOccurred()) |
Adrian Prantl | 4aa2b3a | 2015-09-08 19:20:27 +0000 | [diff] [blame] | 178 | return true; |
| 179 | |
| 180 | // Collect debug info for all decls in this group. |
| 181 | for (auto *I : D) |
| 182 | if (!I->isFromASTFile()) { |
Adrian Prantl | cd97501 | 2016-01-19 23:42:44 +0000 | [diff] [blame] | 183 | DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx); |
Adrian Prantl | 4aa2b3a | 2015-09-08 19:20:27 +0000 | [diff] [blame] | 184 | DTV.TraverseDecl(I); |
| 185 | } |
| 186 | return true; |
| 187 | } |
| 188 | |
Adrian Prantl | d43fe0b | 2015-10-23 17:02:22 +0000 | [diff] [blame] | 189 | void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override { |
| 190 | HandleTopLevelDecl(D); |
| 191 | } |
| 192 | |
Adrian Prantl | 4aa2b3a | 2015-09-08 19:20:27 +0000 | [diff] [blame] | 193 | void HandleTagDeclDefinition(TagDecl *D) override { |
| 194 | if (Diags.hasErrorOccurred()) |
| 195 | return; |
| 196 | |
Adrian Prantl | b3b821f | 2016-01-06 19:22:19 +0000 | [diff] [blame] | 197 | if (D->isFromASTFile()) |
| 198 | return; |
| 199 | |
Adrian Prantl | e5238d2 | 2016-01-19 18:02:47 +0000 | [diff] [blame] | 200 | // Anonymous tag decls are deferred until we are building their declcontext. |
| 201 | if (D->getName().empty()) |
| 202 | return; |
| 203 | |
Adrian Prantl | 5a9a427 | 2016-03-07 20:58:52 +0000 | [diff] [blame^] | 204 | // Defer tag decls until their declcontext is complete. |
| 205 | auto *DeclCtx = D->getDeclContext(); |
| 206 | while (DeclCtx) { |
| 207 | if (auto *D = dyn_cast<TagDecl>(DeclCtx)) |
| 208 | if (!D->isCompleteDefinition()) |
| 209 | return; |
| 210 | DeclCtx = DeclCtx->getParent(); |
| 211 | } |
| 212 | |
Adrian Prantl | cd97501 | 2016-01-19 23:42:44 +0000 | [diff] [blame] | 213 | DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx); |
Adrian Prantl | b3b821f | 2016-01-06 19:22:19 +0000 | [diff] [blame] | 214 | DTV.TraverseDecl(D); |
Adrian Prantl | 4aa2b3a | 2015-09-08 19:20:27 +0000 | [diff] [blame] | 215 | Builder->UpdateCompletedType(D); |
| 216 | } |
| 217 | |
| 218 | void HandleTagDeclRequiredDefinition(const TagDecl *D) override { |
| 219 | if (Diags.hasErrorOccurred()) |
| 220 | return; |
| 221 | |
Adrian Prantl | 8bd4c13 | 2015-09-19 00:10:25 +0000 | [diff] [blame] | 222 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 223 | Builder->getModuleDebugInfo()->completeRequiredType(RD); |
Adrian Prantl | 4aa2b3a | 2015-09-08 19:20:27 +0000 | [diff] [blame] | 224 | } |
| 225 | |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 226 | /// Emit a container holding the serialized AST. |
| 227 | void HandleTranslationUnit(ASTContext &Ctx) override { |
| 228 | assert(M && VMContext && Builder); |
| 229 | // Delete these on function exit. |
| 230 | std::unique_ptr<llvm::LLVMContext> VMContext = std::move(this->VMContext); |
| 231 | std::unique_ptr<llvm::Module> M = std::move(this->M); |
| 232 | std::unique_ptr<CodeGen::CodeGenModule> Builder = std::move(this->Builder); |
| 233 | |
| 234 | if (Diags.hasErrorOccurred()) |
| 235 | return; |
| 236 | |
| 237 | M->setTargetTriple(Ctx.getTargetInfo().getTriple().getTriple()); |
James Y Knight | b214cbc | 2016-03-04 19:00:41 +0000 | [diff] [blame] | 238 | M->setDataLayout(Ctx.getTargetInfo().getDataLayout()); |
Adrian Prantl | c96da8f | 2016-01-22 17:43:43 +0000 | [diff] [blame] | 239 | |
| 240 | // PCH files don't have a signature field in the control block, |
| 241 | // but LLVM detects DWO CUs by looking for a non-zero DWO id. |
Adrian Prantl | 98bfc82 | 2016-01-22 19:29:41 +0000 | [diff] [blame] | 242 | uint64_t Signature = Buffer->Signature ? Buffer->Signature : ~1ULL; |
Adrian Prantl | c96da8f | 2016-01-22 17:43:43 +0000 | [diff] [blame] | 243 | Builder->getModuleDebugInfo()->setDwoId(Signature); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 244 | |
| 245 | // Finalize the Builder. |
| 246 | if (Builder) |
| 247 | Builder->Release(); |
| 248 | |
| 249 | // Ensure the target exists. |
| 250 | std::string Error; |
| 251 | auto Triple = Ctx.getTargetInfo().getTriple(); |
| 252 | if (!llvm::TargetRegistry::lookupTarget(Triple.getTriple(), Error)) |
| 253 | llvm::report_fatal_error(Error); |
| 254 | |
| 255 | // Emit the serialized Clang AST into its own section. |
| 256 | assert(Buffer->IsComplete && "serialization did not complete"); |
| 257 | auto &SerializedAST = Buffer->Data; |
| 258 | auto Size = SerializedAST.size(); |
| 259 | auto Int8Ty = llvm::Type::getInt8Ty(*VMContext); |
| 260 | auto *Ty = llvm::ArrayType::get(Int8Ty, Size); |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 261 | auto *Data = llvm::ConstantDataArray::getString( |
| 262 | *VMContext, StringRef(SerializedAST.data(), Size), |
| 263 | /*AddNull=*/false); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 264 | auto *ASTSym = new llvm::GlobalVariable( |
| 265 | *M, Ty, /*constant*/ true, llvm::GlobalVariable::InternalLinkage, Data, |
| 266 | "__clang_ast"); |
| 267 | // The on-disk hashtable needs to be aligned. |
| 268 | ASTSym->setAlignment(8); |
| 269 | |
| 270 | // Mach-O also needs a segment name. |
| 271 | if (Triple.isOSBinFormatMachO()) |
| 272 | ASTSym->setSection("__CLANG,__clangast"); |
| 273 | // COFF has an eight character length limit. |
| 274 | else if (Triple.isOSBinFormatCOFF()) |
| 275 | ASTSym->setSection("clangast"); |
| 276 | else |
| 277 | ASTSym->setSection("__clangast"); |
| 278 | |
| 279 | DEBUG({ |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 280 | // Print the IR for the PCH container to the debug output. |
| 281 | llvm::SmallString<0> Buffer; |
| 282 | llvm::raw_svector_ostream OS(Buffer); |
| 283 | clang::EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts, |
James Y Knight | b214cbc | 2016-03-04 19:00:41 +0000 | [diff] [blame] | 284 | Ctx.getTargetInfo().getDataLayout(), M.get(), |
| 285 | BackendAction::Backend_EmitLL, &OS); |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 286 | llvm::dbgs() << Buffer; |
| 287 | }); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 288 | |
| 289 | // Use the LLVM backend to emit the pch container. |
| 290 | clang::EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts, |
James Y Knight | b214cbc | 2016-03-04 19:00:41 +0000 | [diff] [blame] | 291 | Ctx.getTargetInfo().getDataLayout(), M.get(), |
| 292 | BackendAction::Backend_EmitObj, OS); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 293 | |
| 294 | // Make sure the pch container hits disk. |
| 295 | OS->flush(); |
| 296 | |
| 297 | // Free the memory for the temporary buffer. |
| 298 | llvm::SmallVector<char, 0> Empty; |
| 299 | SerializedAST = std::move(Empty); |
| 300 | } |
| 301 | }; |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 302 | |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 303 | } // anonymous namespace |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 304 | |
| 305 | std::unique_ptr<ASTConsumer> |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 306 | ObjectFilePCHContainerWriter::CreatePCHContainerGenerator( |
Adrian Prantl | 1e63b2b | 2015-09-19 21:42:52 +0000 | [diff] [blame] | 307 | CompilerInstance &CI, const std::string &MainFileName, |
| 308 | const std::string &OutputFileName, llvm::raw_pwrite_stream *OS, |
| 309 | std::shared_ptr<PCHBuffer> Buffer) const { |
| 310 | return llvm::make_unique<PCHContainerGenerator>(CI, MainFileName, |
Adrian Prantl | 0391406 | 2015-09-18 22:10:59 +0000 | [diff] [blame] | 311 | OutputFileName, OS, Buffer); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 312 | } |
| 313 | |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 314 | void ObjectFilePCHContainerReader::ExtractPCH( |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 315 | llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const { |
| 316 | if (auto OF = llvm::object::ObjectFile::createObjectFile(Buffer)) { |
| 317 | auto *Obj = OF.get().get(); |
| 318 | bool IsCOFF = isa<llvm::object::COFFObjectFile>(Obj); |
| 319 | // Find the clang AST section in the container. |
| 320 | for (auto &Section : OF->get()->sections()) { |
| 321 | StringRef Name; |
| 322 | Section.getName(Name); |
| 323 | if ((!IsCOFF && Name == "__clangast") || |
| 324 | ( IsCOFF && Name == "clangast")) { |
| 325 | StringRef Buf; |
| 326 | Section.getContents(Buf); |
| 327 | StreamFile.init((const unsigned char *)Buf.begin(), |
| 328 | (const unsigned char *)Buf.end()); |
| 329 | return; |
| 330 | } |
| 331 | } |
| 332 | } |
| 333 | |
| 334 | // As a fallback, treat the buffer as a raw AST. |
| 335 | StreamFile.init((const unsigned char *)Buffer.getBufferStart(), |
| 336 | (const unsigned char *)Buffer.getBufferEnd()); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 337 | } |