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