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; |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 58 | std::unique_ptr<raw_pwrite_stream> OS; |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 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) { |
Adrian Prantl | 1b88ace | 2018-01-03 18:31:04 +0000 | [diff] [blame] | 74 | if (!D->getImportedOwningModule()) |
| 75 | DI.EmitImportDecl(*D); |
Adrian Prantl | 85d938a | 2015-09-21 17:48:37 +0000 | [diff] [blame] | 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; |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 107 | for (auto i : D->parameters()) |
Adrian Prantl | 748a6cd | 2015-09-08 20:41:52 +0000 | [diff] [blame] | 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()); |
David Majnemer | 59f7792 | 2016-06-24 04:05:48 +0000 | [diff] [blame] | 126 | for (auto i : D->parameters()) |
Adrian Prantl | 748a6cd | 2015-09-08 20:41:52 +0000 | [diff] [blame] | 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, |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 140 | std::unique_ptr<raw_pwrite_stream> OS, |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 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()), |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 147 | TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()), |
| 148 | OS(std::move(OS)), Buffer(std::move(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; |
Adrian Prantl | 9a1a1aa | 2017-07-18 23:58:34 +0000 | [diff] [blame] | 154 | // When building a module MainFileName is the name of the modulemap file. |
| 155 | CodeGenOpts.MainFileName = |
| 156 | LangOpts.CurrentModule.empty() ? MainFileName : LangOpts.CurrentModule; |
Benjamin Kramer | 8c30592 | 2016-02-02 11:06:51 +0000 | [diff] [blame] | 157 | CodeGenOpts.setDebugInfo(codegenoptions::FullDebugInfo); |
David Blaikie | af09f4a | 2016-05-03 23:06:40 +0000 | [diff] [blame] | 158 | CodeGenOpts.setDebuggerTuning(CI.getCodeGenOpts().getDebuggerTuning()); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 161 | ~PCHContainerGenerator() override = default; |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 162 | |
| 163 | void Initialize(ASTContext &Context) override { |
Richard Smith | 293534b | 2015-08-18 20:39:29 +0000 | [diff] [blame] | 164 | assert(!Ctx && "initialized multiple times"); |
Richard Smith | 0f99d6a | 2015-08-09 08:48:41 +0000 | [diff] [blame] | 165 | |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 166 | Ctx = &Context; |
| 167 | VMContext.reset(new llvm::LLVMContext()); |
| 168 | M.reset(new llvm::Module(MainFileName, *VMContext)); |
James Y Knight | b214cbc | 2016-03-04 19:00:41 +0000 | [diff] [blame] | 169 | M->setDataLayout(Ctx->getTargetInfo().getDataLayout()); |
Mehdi Amini | ca3cf9e | 2015-07-24 16:04:29 +0000 | [diff] [blame] | 170 | Builder.reset(new CodeGen::CodeGenModule( |
| 171 | *Ctx, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags)); |
Adrian Prantl | 3a2d494 | 2016-01-22 23:30:56 +0000 | [diff] [blame] | 172 | |
| 173 | // Prepare CGDebugInfo to emit debug info for a clang module. |
| 174 | auto *DI = Builder->getModuleDebugInfo(); |
| 175 | StringRef ModuleName = llvm::sys::path::filename(MainFileName); |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 176 | DI->setPCHDescriptor({ModuleName, "", OutputFileName, |
| 177 | ASTFileSignature{{{~0U, ~0U, ~0U, ~0U, ~1U}}}}); |
Adrian Prantl | 3a2d494 | 2016-01-22 23:30:56 +0000 | [diff] [blame] | 178 | DI->setModuleMap(MMap); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 179 | } |
| 180 | |
Adrian Prantl | 4aa2b3a | 2015-09-08 19:20:27 +0000 | [diff] [blame] | 181 | bool HandleTopLevelDecl(DeclGroupRef D) override { |
Adrian Prantl | abdd6fc | 2015-10-23 16:51:32 +0000 | [diff] [blame] | 182 | if (Diags.hasErrorOccurred()) |
Adrian Prantl | 4aa2b3a | 2015-09-08 19:20:27 +0000 | [diff] [blame] | 183 | return true; |
| 184 | |
| 185 | // Collect debug info for all decls in this group. |
| 186 | for (auto *I : D) |
| 187 | if (!I->isFromASTFile()) { |
Adrian Prantl | cd97501 | 2016-01-19 23:42:44 +0000 | [diff] [blame] | 188 | DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx); |
Adrian Prantl | 4aa2b3a | 2015-09-08 19:20:27 +0000 | [diff] [blame] | 189 | DTV.TraverseDecl(I); |
| 190 | } |
| 191 | return true; |
| 192 | } |
| 193 | |
Adrian Prantl | d43fe0b | 2015-10-23 17:02:22 +0000 | [diff] [blame] | 194 | void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override { |
| 195 | HandleTopLevelDecl(D); |
| 196 | } |
| 197 | |
Adrian Prantl | 4aa2b3a | 2015-09-08 19:20:27 +0000 | [diff] [blame] | 198 | void HandleTagDeclDefinition(TagDecl *D) override { |
| 199 | if (Diags.hasErrorOccurred()) |
| 200 | return; |
| 201 | |
Adrian Prantl | b3b821f | 2016-01-06 19:22:19 +0000 | [diff] [blame] | 202 | if (D->isFromASTFile()) |
| 203 | return; |
| 204 | |
Adrian Prantl | e5238d2 | 2016-01-19 18:02:47 +0000 | [diff] [blame] | 205 | // Anonymous tag decls are deferred until we are building their declcontext. |
| 206 | if (D->getName().empty()) |
| 207 | return; |
| 208 | |
Adrian Prantl | 5a9a427 | 2016-03-07 20:58:52 +0000 | [diff] [blame] | 209 | // Defer tag decls until their declcontext is complete. |
| 210 | auto *DeclCtx = D->getDeclContext(); |
| 211 | while (DeclCtx) { |
| 212 | if (auto *D = dyn_cast<TagDecl>(DeclCtx)) |
| 213 | if (!D->isCompleteDefinition()) |
| 214 | return; |
| 215 | DeclCtx = DeclCtx->getParent(); |
| 216 | } |
| 217 | |
Adrian Prantl | cd97501 | 2016-01-19 23:42:44 +0000 | [diff] [blame] | 218 | DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx); |
Adrian Prantl | b3b821f | 2016-01-06 19:22:19 +0000 | [diff] [blame] | 219 | DTV.TraverseDecl(D); |
Adrian Prantl | 4aa2b3a | 2015-09-08 19:20:27 +0000 | [diff] [blame] | 220 | Builder->UpdateCompletedType(D); |
| 221 | } |
| 222 | |
| 223 | void HandleTagDeclRequiredDefinition(const TagDecl *D) override { |
| 224 | if (Diags.hasErrorOccurred()) |
| 225 | return; |
| 226 | |
Adrian Prantl | 8bd4c13 | 2015-09-19 00:10:25 +0000 | [diff] [blame] | 227 | if (const RecordDecl *RD = dyn_cast<RecordDecl>(D)) |
| 228 | Builder->getModuleDebugInfo()->completeRequiredType(RD); |
Adrian Prantl | 4aa2b3a | 2015-09-08 19:20:27 +0000 | [diff] [blame] | 229 | } |
| 230 | |
Adrian Prantl | c5e3647 | 2018-01-03 19:10:21 +0000 | [diff] [blame] | 231 | void HandleImplicitImportDecl(ImportDecl *D) override { |
| 232 | if (!D->getImportedOwningModule()) |
| 233 | Builder->getModuleDebugInfo()->EmitImportDecl(*D); |
| 234 | } |
| 235 | |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 236 | /// Emit a container holding the serialized AST. |
| 237 | void HandleTranslationUnit(ASTContext &Ctx) override { |
| 238 | assert(M && VMContext && Builder); |
| 239 | // Delete these on function exit. |
| 240 | std::unique_ptr<llvm::LLVMContext> VMContext = std::move(this->VMContext); |
| 241 | std::unique_ptr<llvm::Module> M = std::move(this->M); |
| 242 | std::unique_ptr<CodeGen::CodeGenModule> Builder = std::move(this->Builder); |
| 243 | |
| 244 | if (Diags.hasErrorOccurred()) |
| 245 | return; |
| 246 | |
| 247 | M->setTargetTriple(Ctx.getTargetInfo().getTriple().getTriple()); |
James Y Knight | b214cbc | 2016-03-04 19:00:41 +0000 | [diff] [blame] | 248 | M->setDataLayout(Ctx.getTargetInfo().getDataLayout()); |
Adrian Prantl | c96da8f | 2016-01-22 17:43:43 +0000 | [diff] [blame] | 249 | |
| 250 | // PCH files don't have a signature field in the control block, |
| 251 | // but LLVM detects DWO CUs by looking for a non-zero DWO id. |
Duncan P. N. Exon Smith | 60fa288 | 2017-03-13 18:45:08 +0000 | [diff] [blame] | 252 | // We use the lower 64 bits for debug info. |
| 253 | uint64_t Signature = |
| 254 | Buffer->Signature |
| 255 | ? (uint64_t)Buffer->Signature[1] << 32 | Buffer->Signature[0] |
| 256 | : ~1ULL; |
Adrian Prantl | c96da8f | 2016-01-22 17:43:43 +0000 | [diff] [blame] | 257 | Builder->getModuleDebugInfo()->setDwoId(Signature); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 258 | |
| 259 | // Finalize the Builder. |
| 260 | if (Builder) |
| 261 | Builder->Release(); |
| 262 | |
| 263 | // Ensure the target exists. |
| 264 | std::string Error; |
| 265 | auto Triple = Ctx.getTargetInfo().getTriple(); |
| 266 | if (!llvm::TargetRegistry::lookupTarget(Triple.getTriple(), Error)) |
| 267 | llvm::report_fatal_error(Error); |
| 268 | |
| 269 | // Emit the serialized Clang AST into its own section. |
| 270 | assert(Buffer->IsComplete && "serialization did not complete"); |
| 271 | auto &SerializedAST = Buffer->Data; |
| 272 | auto Size = SerializedAST.size(); |
| 273 | auto Int8Ty = llvm::Type::getInt8Ty(*VMContext); |
| 274 | auto *Ty = llvm::ArrayType::get(Int8Ty, Size); |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 275 | auto *Data = llvm::ConstantDataArray::getString( |
| 276 | *VMContext, StringRef(SerializedAST.data(), Size), |
| 277 | /*AddNull=*/false); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 278 | auto *ASTSym = new llvm::GlobalVariable( |
| 279 | *M, Ty, /*constant*/ true, llvm::GlobalVariable::InternalLinkage, Data, |
| 280 | "__clang_ast"); |
| 281 | // The on-disk hashtable needs to be aligned. |
| 282 | ASTSym->setAlignment(8); |
| 283 | |
| 284 | // Mach-O also needs a segment name. |
| 285 | if (Triple.isOSBinFormatMachO()) |
| 286 | ASTSym->setSection("__CLANG,__clangast"); |
| 287 | // COFF has an eight character length limit. |
| 288 | else if (Triple.isOSBinFormatCOFF()) |
| 289 | ASTSym->setSection("clangast"); |
| 290 | else |
| 291 | ASTSym->setSection("__clangast"); |
| 292 | |
| 293 | DEBUG({ |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 294 | // Print the IR for the PCH container to the debug output. |
| 295 | llvm::SmallString<0> Buffer; |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 296 | clang::EmitBackendOutput( |
Saleem Abdulrasool | 888e289 | 2017-01-05 16:02:32 +0000 | [diff] [blame] | 297 | Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts, LangOpts, |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 298 | Ctx.getTargetInfo().getDataLayout(), M.get(), |
| 299 | BackendAction::Backend_EmitLL, |
| 300 | llvm::make_unique<llvm::raw_svector_ostream>(Buffer)); |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 301 | llvm::dbgs() << Buffer; |
| 302 | }); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 303 | |
| 304 | // Use the LLVM backend to emit the pch container. |
Saleem Abdulrasool | 888e289 | 2017-01-05 16:02:32 +0000 | [diff] [blame] | 305 | clang::EmitBackendOutput(Diags, HeaderSearchOpts, CodeGenOpts, TargetOpts, |
| 306 | LangOpts, Ctx.getTargetInfo().getDataLayout(), |
| 307 | M.get(), BackendAction::Backend_EmitObj, |
| 308 | std::move(OS)); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 309 | |
| 310 | // Free the memory for the temporary buffer. |
| 311 | llvm::SmallVector<char, 0> Empty; |
| 312 | SerializedAST = std::move(Empty); |
| 313 | } |
| 314 | }; |
Adrian Prantl | 5a88e1a | 2015-07-09 19:46:39 +0000 | [diff] [blame] | 315 | |
Hans Wennborg | 7eb5464 | 2015-09-10 17:07:54 +0000 | [diff] [blame] | 316 | } // anonymous namespace |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 317 | |
| 318 | std::unique_ptr<ASTConsumer> |
Adrian Prantl | fb2398d | 2015-07-17 01:19:54 +0000 | [diff] [blame] | 319 | ObjectFilePCHContainerWriter::CreatePCHContainerGenerator( |
Adrian Prantl | 1e63b2b | 2015-09-19 21:42:52 +0000 | [diff] [blame] | 320 | CompilerInstance &CI, const std::string &MainFileName, |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 321 | const std::string &OutputFileName, |
| 322 | std::unique_ptr<llvm::raw_pwrite_stream> OS, |
Adrian Prantl | 1e63b2b | 2015-09-19 21:42:52 +0000 | [diff] [blame] | 323 | std::shared_ptr<PCHBuffer> Buffer) const { |
Peter Collingbourne | 03f8907 | 2016-07-15 00:55:40 +0000 | [diff] [blame] | 324 | return llvm::make_unique<PCHContainerGenerator>( |
| 325 | CI, MainFileName, OutputFileName, std::move(OS), Buffer); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 326 | } |
| 327 | |
Peter Collingbourne | 77c89b6 | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 328 | StringRef |
| 329 | ObjectFilePCHContainerReader::ExtractPCH(llvm::MemoryBufferRef Buffer) const { |
| 330 | StringRef PCH; |
Adrian Prantl | 576b2db | 2016-08-17 23:13:53 +0000 | [diff] [blame] | 331 | auto OFOrErr = llvm::object::ObjectFile::createObjectFile(Buffer); |
| 332 | if (OFOrErr) { |
| 333 | auto &OF = OFOrErr.get(); |
| 334 | bool IsCOFF = isa<llvm::object::COFFObjectFile>(*OF); |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 335 | // Find the clang AST section in the container. |
Adrian Prantl | 576b2db | 2016-08-17 23:13:53 +0000 | [diff] [blame] | 336 | for (auto &Section : OF->sections()) { |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 337 | StringRef Name; |
| 338 | Section.getName(Name); |
Adrian Prantl | 576b2db | 2016-08-17 23:13:53 +0000 | [diff] [blame] | 339 | if ((!IsCOFF && Name == "__clangast") || (IsCOFF && Name == "clangast")) { |
Peter Collingbourne | 77c89b6 | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 340 | Section.getContents(PCH); |
| 341 | return PCH; |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 342 | } |
| 343 | } |
| 344 | } |
Adrian Prantl | 576b2db | 2016-08-17 23:13:53 +0000 | [diff] [blame] | 345 | handleAllErrors(OFOrErr.takeError(), [&](const llvm::ErrorInfoBase &EIB) { |
| 346 | if (EIB.convertToErrorCode() == |
| 347 | llvm::object::object_error::invalid_file_type) |
| 348 | // As a fallback, treat the buffer as a raw AST. |
Peter Collingbourne | 77c89b6 | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 349 | PCH = Buffer.getBuffer(); |
Adrian Prantl | 576b2db | 2016-08-17 23:13:53 +0000 | [diff] [blame] | 350 | else |
| 351 | EIB.log(llvm::errs()); |
| 352 | }); |
Peter Collingbourne | 77c89b6 | 2016-11-08 04:17:11 +0000 | [diff] [blame] | 353 | return PCH; |
Adrian Prantl | bc06858 | 2015-07-08 01:00:30 +0000 | [diff] [blame] | 354 | } |