1bc068586SAdrian Prantl //===--- ObjectFilePCHContainerOperations.cpp -----------------------------===//
2bc068586SAdrian Prantl //
3bc068586SAdrian Prantl //                     The LLVM Compiler Infrastructure
4bc068586SAdrian Prantl //
5bc068586SAdrian Prantl // This file is distributed under the University of Illinois Open Source
6bc068586SAdrian Prantl // License. See LICENSE.TXT for details.
7bc068586SAdrian Prantl //
8bc068586SAdrian Prantl //===----------------------------------------------------------------------===//
9bc068586SAdrian Prantl 
10bc068586SAdrian Prantl #include "clang/CodeGen/ObjectFilePCHContainerOperations.h"
11bc068586SAdrian Prantl #include "CGDebugInfo.h"
12bc068586SAdrian Prantl #include "CodeGenModule.h"
13bc068586SAdrian Prantl #include "clang/AST/ASTContext.h"
14bc068586SAdrian Prantl #include "clang/AST/DeclObjC.h"
15bc068586SAdrian Prantl #include "clang/AST/Expr.h"
16bc068586SAdrian Prantl #include "clang/AST/RecursiveASTVisitor.h"
17bc068586SAdrian Prantl #include "clang/Basic/Diagnostic.h"
18bc068586SAdrian Prantl #include "clang/Basic/TargetInfo.h"
19bc068586SAdrian Prantl #include "clang/CodeGen/BackendUtil.h"
20bc068586SAdrian Prantl #include "clang/Frontend/CodeGenOptions.h"
210391406eSAdrian Prantl #include "clang/Frontend/CompilerInstance.h"
229402cef0SAdrian Prantl #include "clang/Lex/HeaderSearch.h"
233a2d4947SAdrian Prantl #include "clang/Lex/Preprocessor.h"
24bc068586SAdrian Prantl #include "clang/Serialization/ASTWriter.h"
25bc068586SAdrian Prantl #include "llvm/ADT/StringRef.h"
26bc068586SAdrian Prantl #include "llvm/Bitcode/BitstreamReader.h"
27bc068586SAdrian Prantl #include "llvm/DebugInfo/DWARF/DWARFContext.h"
28bc068586SAdrian Prantl #include "llvm/IR/Constants.h"
29bc068586SAdrian Prantl #include "llvm/IR/DataLayout.h"
30bc068586SAdrian Prantl #include "llvm/IR/LLVMContext.h"
31bc068586SAdrian Prantl #include "llvm/IR/Module.h"
32bc068586SAdrian Prantl #include "llvm/Object/COFF.h"
33bc068586SAdrian Prantl #include "llvm/Object/ObjectFile.h"
343a2d4947SAdrian Prantl #include "llvm/Support/Path.h"
35bc068586SAdrian Prantl #include "llvm/Support/TargetRegistry.h"
36bc068586SAdrian Prantl #include <memory>
377eb5464bSHans Wennborg 
38bc068586SAdrian Prantl using namespace clang;
39bc068586SAdrian Prantl 
40bc068586SAdrian Prantl #define DEBUG_TYPE "pchcontainer"
41bc068586SAdrian Prantl 
42bc068586SAdrian Prantl namespace {
435a88e1a8SAdrian Prantl class PCHContainerGenerator : public ASTConsumer {
44bc068586SAdrian Prantl   DiagnosticsEngine &Diags;
45bc068586SAdrian Prantl   const std::string MainFileName;
46aa5d08d0SAdrian Prantl   const std::string OutputFileName;
47bc068586SAdrian Prantl   ASTContext *Ctx;
489402cef0SAdrian Prantl   ModuleMap &MMap;
49bc068586SAdrian Prantl   const HeaderSearchOptions &HeaderSearchOpts;
50bc068586SAdrian Prantl   const PreprocessorOptions &PreprocessorOpts;
51bc068586SAdrian Prantl   CodeGenOptions CodeGenOpts;
52bc068586SAdrian Prantl   const TargetOptions TargetOpts;
53bc068586SAdrian Prantl   const LangOptions LangOpts;
54bc068586SAdrian Prantl   std::unique_ptr<llvm::LLVMContext> VMContext;
55bc068586SAdrian Prantl   std::unique_ptr<llvm::Module> M;
56bc068586SAdrian Prantl   std::unique_ptr<CodeGen::CodeGenModule> Builder;
57bc068586SAdrian Prantl   raw_pwrite_stream *OS;
58bc068586SAdrian Prantl   std::shared_ptr<PCHBuffer> Buffer;
59bc068586SAdrian Prantl 
604aa2b3a3SAdrian Prantl   /// Visit every type and emit debug info for it.
614aa2b3a3SAdrian Prantl   struct DebugTypeVisitor : public RecursiveASTVisitor<DebugTypeVisitor> {
624aa2b3a3SAdrian Prantl     clang::CodeGen::CGDebugInfo &DI;
634aa2b3a3SAdrian Prantl     ASTContext &Ctx;
64cd975018SAdrian Prantl     DebugTypeVisitor(clang::CodeGen::CGDebugInfo &DI, ASTContext &Ctx)
65cd975018SAdrian Prantl         : DI(DI), Ctx(Ctx) {}
664aa2b3a3SAdrian Prantl 
674aa2b3a3SAdrian Prantl     /// Determine whether this type can be represented in DWARF.
684aa2b3a3SAdrian Prantl     static bool CanRepresent(const Type *Ty) {
694aa2b3a3SAdrian Prantl       return !Ty->isDependentType() && !Ty->isUndeducedType();
704aa2b3a3SAdrian Prantl     }
714aa2b3a3SAdrian Prantl 
7285d938aaSAdrian Prantl     bool VisitImportDecl(ImportDecl *D) {
7385d938aaSAdrian Prantl       auto *Import = cast<ImportDecl>(D);
7485d938aaSAdrian Prantl       if (!Import->getImportedOwningModule())
7585d938aaSAdrian Prantl         DI.EmitImportDecl(*Import);
7685d938aaSAdrian Prantl       return true;
7785d938aaSAdrian Prantl     }
7885d938aaSAdrian Prantl 
794aa2b3a3SAdrian Prantl     bool VisitTypeDecl(TypeDecl *D) {
80b3b821f1SAdrian Prantl       // TagDecls may be deferred until after all decls have been merged and we
81b3b821f1SAdrian Prantl       // know the complete type. Pure forward declarations will be skipped, but
82b3b821f1SAdrian Prantl       // they don't need to be emitted into the module anyway.
83cd975018SAdrian Prantl       if (auto *TD = dyn_cast<TagDecl>(D))
84cd975018SAdrian Prantl         if (!TD->isCompleteDefinition())
85b3b821f1SAdrian Prantl           return true;
86b3b821f1SAdrian Prantl 
874aa2b3a3SAdrian Prantl       QualType QualTy = Ctx.getTypeDeclType(D);
884aa2b3a3SAdrian Prantl       if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr()))
894aa2b3a3SAdrian Prantl         DI.getOrCreateStandaloneType(QualTy, D->getLocation());
904aa2b3a3SAdrian Prantl       return true;
914aa2b3a3SAdrian Prantl     }
924aa2b3a3SAdrian Prantl 
93748a6cd1SAdrian Prantl     bool VisitObjCInterfaceDecl(ObjCInterfaceDecl *D) {
94748a6cd1SAdrian Prantl       QualType QualTy(D->getTypeForDecl(), 0);
95748a6cd1SAdrian Prantl       if (!QualTy.isNull() && CanRepresent(QualTy.getTypePtr()))
96748a6cd1SAdrian Prantl         DI.getOrCreateStandaloneType(QualTy, D->getLocation());
97748a6cd1SAdrian Prantl       return true;
98748a6cd1SAdrian Prantl     }
99748a6cd1SAdrian Prantl 
100748a6cd1SAdrian Prantl     bool VisitFunctionDecl(FunctionDecl *D) {
101748a6cd1SAdrian Prantl       if (isa<CXXMethodDecl>(D))
102748a6cd1SAdrian Prantl         // This is not yet supported. Constructing the `this' argument
103748a6cd1SAdrian Prantl         // mandates a CodeGenFunction.
104748a6cd1SAdrian Prantl         return true;
105748a6cd1SAdrian Prantl 
106748a6cd1SAdrian Prantl       SmallVector<QualType, 16> ArgTypes;
107748a6cd1SAdrian Prantl       for (auto i : D->params())
108748a6cd1SAdrian Prantl         ArgTypes.push_back(i->getType());
109748a6cd1SAdrian Prantl       QualType RetTy = D->getReturnType();
110748a6cd1SAdrian Prantl       QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes,
111748a6cd1SAdrian Prantl                                           FunctionProtoType::ExtProtoInfo());
112748a6cd1SAdrian Prantl       if (CanRepresent(FnTy.getTypePtr()))
113748a6cd1SAdrian Prantl         DI.EmitFunctionDecl(D, D->getLocation(), FnTy);
114748a6cd1SAdrian Prantl       return true;
115748a6cd1SAdrian Prantl     }
116748a6cd1SAdrian Prantl 
117748a6cd1SAdrian Prantl     bool VisitObjCMethodDecl(ObjCMethodDecl *D) {
118748a6cd1SAdrian Prantl       if (!D->getClassInterface())
119748a6cd1SAdrian Prantl         return true;
120748a6cd1SAdrian Prantl 
121748a6cd1SAdrian Prantl       bool selfIsPseudoStrong, selfIsConsumed;
122748a6cd1SAdrian Prantl       SmallVector<QualType, 16> ArgTypes;
123748a6cd1SAdrian Prantl       ArgTypes.push_back(D->getSelfType(Ctx, D->getClassInterface(),
124748a6cd1SAdrian Prantl                                         selfIsPseudoStrong, selfIsConsumed));
125748a6cd1SAdrian Prantl       ArgTypes.push_back(Ctx.getObjCSelType());
126748a6cd1SAdrian Prantl       for (auto i : D->params())
127748a6cd1SAdrian Prantl         ArgTypes.push_back(i->getType());
128748a6cd1SAdrian Prantl       QualType RetTy = D->getReturnType();
129748a6cd1SAdrian Prantl       QualType FnTy = Ctx.getFunctionType(RetTy, ArgTypes,
130748a6cd1SAdrian Prantl                                           FunctionProtoType::ExtProtoInfo());
131748a6cd1SAdrian Prantl       if (CanRepresent(FnTy.getTypePtr()))
132748a6cd1SAdrian Prantl         DI.EmitFunctionDecl(D, D->getLocation(), FnTy);
133748a6cd1SAdrian Prantl       return true;
134748a6cd1SAdrian Prantl     }
1354aa2b3a3SAdrian Prantl   };
1364aa2b3a3SAdrian Prantl 
137bc068586SAdrian Prantl public:
1381e63b2bdSAdrian Prantl   PCHContainerGenerator(CompilerInstance &CI, const std::string &MainFileName,
1395a88e1a8SAdrian Prantl                         const std::string &OutputFileName,
1405a88e1a8SAdrian Prantl                         raw_pwrite_stream *OS,
1415a88e1a8SAdrian Prantl                         std::shared_ptr<PCHBuffer> Buffer)
142aa5d08d0SAdrian Prantl       : Diags(CI.getDiagnostics()), MainFileName(MainFileName),
143aa5d08d0SAdrian Prantl         OutputFileName(OutputFileName), Ctx(nullptr),
1449402cef0SAdrian Prantl         MMap(CI.getPreprocessor().getHeaderSearchInfo().getModuleMap()),
1451e63b2bdSAdrian Prantl         HeaderSearchOpts(CI.getHeaderSearchOpts()),
1460391406eSAdrian Prantl         PreprocessorOpts(CI.getPreprocessorOpts()),
1470391406eSAdrian Prantl         TargetOpts(CI.getTargetOpts()), LangOpts(CI.getLangOpts()), OS(OS),
1480391406eSAdrian Prantl         Buffer(Buffer) {
149bc068586SAdrian Prantl     // The debug info output isn't affected by CodeModel and
150bc068586SAdrian Prantl     // ThreadModel, but the backend expects them to be nonempty.
151bc068586SAdrian Prantl     CodeGenOpts.CodeModel = "default";
152bc068586SAdrian Prantl     CodeGenOpts.ThreadModel = "single";
1536b21ab21SAdrian Prantl     CodeGenOpts.DebugTypeExtRefs = true;
1548c30592eSBenjamin Kramer     CodeGenOpts.setDebugInfo(codegenoptions::FullDebugInfo);
155bc068586SAdrian Prantl   }
156bc068586SAdrian Prantl 
1577eb5464bSHans Wennborg   ~PCHContainerGenerator() override = default;
158bc068586SAdrian Prantl 
159bc068586SAdrian Prantl   void Initialize(ASTContext &Context) override {
160293534b1SRichard Smith     assert(!Ctx && "initialized multiple times");
1610f99d6a4SRichard Smith 
162bc068586SAdrian Prantl     Ctx = &Context;
163bc068586SAdrian Prantl     VMContext.reset(new llvm::LLVMContext());
164bc068586SAdrian Prantl     M.reset(new llvm::Module(MainFileName, *VMContext));
165*b214cbc7SJames Y Knight     M->setDataLayout(Ctx->getTargetInfo().getDataLayout());
166ca3cf9e6SMehdi Amini     Builder.reset(new CodeGen::CodeGenModule(
167ca3cf9e6SMehdi Amini         *Ctx, HeaderSearchOpts, PreprocessorOpts, CodeGenOpts, *M, Diags));
1683a2d4947SAdrian Prantl 
1693a2d4947SAdrian Prantl     // Prepare CGDebugInfo to emit debug info for a clang module.
1703a2d4947SAdrian Prantl     auto *DI = Builder->getModuleDebugInfo();
1713a2d4947SAdrian Prantl     StringRef ModuleName = llvm::sys::path::filename(MainFileName);
1723a2d4947SAdrian Prantl     DI->setPCHDescriptor({ModuleName, "", OutputFileName, ~1ULL});
1733a2d4947SAdrian Prantl     DI->setModuleMap(MMap);
174bc068586SAdrian Prantl   }
175bc068586SAdrian Prantl 
1764aa2b3a3SAdrian Prantl   bool HandleTopLevelDecl(DeclGroupRef D) override {
177abdd6fc4SAdrian Prantl     if (Diags.hasErrorOccurred())
1784aa2b3a3SAdrian Prantl       return true;
1794aa2b3a3SAdrian Prantl 
1804aa2b3a3SAdrian Prantl     // Collect debug info for all decls in this group.
1814aa2b3a3SAdrian Prantl     for (auto *I : D)
1824aa2b3a3SAdrian Prantl       if (!I->isFromASTFile()) {
183cd975018SAdrian Prantl         DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx);
1844aa2b3a3SAdrian Prantl         DTV.TraverseDecl(I);
1854aa2b3a3SAdrian Prantl       }
1864aa2b3a3SAdrian Prantl     return true;
1874aa2b3a3SAdrian Prantl   }
1884aa2b3a3SAdrian Prantl 
189d43fe0bdSAdrian Prantl   void HandleTopLevelDeclInObjCContainer(DeclGroupRef D) override {
190d43fe0bdSAdrian Prantl     HandleTopLevelDecl(D);
191d43fe0bdSAdrian Prantl   }
192d43fe0bdSAdrian Prantl 
1934aa2b3a3SAdrian Prantl   void HandleTagDeclDefinition(TagDecl *D) override {
1944aa2b3a3SAdrian Prantl     if (Diags.hasErrorOccurred())
1954aa2b3a3SAdrian Prantl       return;
1964aa2b3a3SAdrian Prantl 
197b3b821f1SAdrian Prantl     if (D->isFromASTFile())
198b3b821f1SAdrian Prantl       return;
199b3b821f1SAdrian Prantl 
200e5238d2aSAdrian Prantl     // Anonymous tag decls are deferred until we are building their declcontext.
201e5238d2aSAdrian Prantl     if (D->getName().empty())
202e5238d2aSAdrian Prantl       return;
203e5238d2aSAdrian Prantl 
204cd975018SAdrian Prantl     DebugTypeVisitor DTV(*Builder->getModuleDebugInfo(), *Ctx);
205b3b821f1SAdrian Prantl     DTV.TraverseDecl(D);
2064aa2b3a3SAdrian Prantl     Builder->UpdateCompletedType(D);
2074aa2b3a3SAdrian Prantl   }
2084aa2b3a3SAdrian Prantl 
2094aa2b3a3SAdrian Prantl   void HandleTagDeclRequiredDefinition(const TagDecl *D) override {
2104aa2b3a3SAdrian Prantl     if (Diags.hasErrorOccurred())
2114aa2b3a3SAdrian Prantl       return;
2124aa2b3a3SAdrian Prantl 
2134aa2b3a3SAdrian Prantl     if (const RecordDecl *RD = dyn_cast<RecordDecl>(D))
2148bd4c13fSAdrian Prantl       Builder->getModuleDebugInfo()->completeRequiredType(RD);
2154aa2b3a3SAdrian Prantl   }
2164aa2b3a3SAdrian Prantl 
217bc068586SAdrian Prantl   /// Emit a container holding the serialized AST.
218bc068586SAdrian Prantl   void HandleTranslationUnit(ASTContext &Ctx) override {
219bc068586SAdrian Prantl     assert(M && VMContext && Builder);
220bc068586SAdrian Prantl     // Delete these on function exit.
221bc068586SAdrian Prantl     std::unique_ptr<llvm::LLVMContext> VMContext = std::move(this->VMContext);
222bc068586SAdrian Prantl     std::unique_ptr<llvm::Module> M = std::move(this->M);
223bc068586SAdrian Prantl     std::unique_ptr<CodeGen::CodeGenModule> Builder = std::move(this->Builder);
224bc068586SAdrian Prantl 
225bc068586SAdrian Prantl     if (Diags.hasErrorOccurred())
226bc068586SAdrian Prantl       return;
227bc068586SAdrian Prantl 
228bc068586SAdrian Prantl     M->setTargetTriple(Ctx.getTargetInfo().getTriple().getTriple());
229*b214cbc7SJames Y Knight     M->setDataLayout(Ctx.getTargetInfo().getDataLayout());
230c96da8faSAdrian Prantl 
231c96da8faSAdrian Prantl     // PCH files don't have a signature field in the control block,
232c96da8faSAdrian Prantl     // but LLVM detects DWO CUs by looking for a non-zero DWO id.
23398bfc82cSAdrian Prantl     uint64_t Signature = Buffer->Signature ? Buffer->Signature : ~1ULL;
234c96da8faSAdrian Prantl     Builder->getModuleDebugInfo()->setDwoId(Signature);
235bc068586SAdrian Prantl 
236bc068586SAdrian Prantl     // Finalize the Builder.
237bc068586SAdrian Prantl     if (Builder)
238bc068586SAdrian Prantl       Builder->Release();
239bc068586SAdrian Prantl 
240bc068586SAdrian Prantl     // Ensure the target exists.
241bc068586SAdrian Prantl     std::string Error;
242bc068586SAdrian Prantl     auto Triple = Ctx.getTargetInfo().getTriple();
243bc068586SAdrian Prantl     if (!llvm::TargetRegistry::lookupTarget(Triple.getTriple(), Error))
244bc068586SAdrian Prantl       llvm::report_fatal_error(Error);
245bc068586SAdrian Prantl 
246bc068586SAdrian Prantl     // Emit the serialized Clang AST into its own section.
247bc068586SAdrian Prantl     assert(Buffer->IsComplete && "serialization did not complete");
248bc068586SAdrian Prantl     auto &SerializedAST = Buffer->Data;
249bc068586SAdrian Prantl     auto Size = SerializedAST.size();
250bc068586SAdrian Prantl     auto Int8Ty = llvm::Type::getInt8Ty(*VMContext);
251bc068586SAdrian Prantl     auto *Ty = llvm::ArrayType::get(Int8Ty, Size);
2525a88e1a8SAdrian Prantl     auto *Data = llvm::ConstantDataArray::getString(
2535a88e1a8SAdrian Prantl         *VMContext, StringRef(SerializedAST.data(), Size),
254bc068586SAdrian Prantl         /*AddNull=*/false);
255bc068586SAdrian Prantl     auto *ASTSym = new llvm::GlobalVariable(
256bc068586SAdrian Prantl         *M, Ty, /*constant*/ true, llvm::GlobalVariable::InternalLinkage, Data,
257bc068586SAdrian Prantl         "__clang_ast");
258bc068586SAdrian Prantl     // The on-disk hashtable needs to be aligned.
259bc068586SAdrian Prantl     ASTSym->setAlignment(8);
260bc068586SAdrian Prantl 
261bc068586SAdrian Prantl     // Mach-O also needs a segment name.
262bc068586SAdrian Prantl     if (Triple.isOSBinFormatMachO())
263bc068586SAdrian Prantl       ASTSym->setSection("__CLANG,__clangast");
264bc068586SAdrian Prantl     // COFF has an eight character length limit.
265bc068586SAdrian Prantl     else if (Triple.isOSBinFormatCOFF())
266bc068586SAdrian Prantl       ASTSym->setSection("clangast");
267bc068586SAdrian Prantl     else
268bc068586SAdrian Prantl       ASTSym->setSection("__clangast");
269bc068586SAdrian Prantl 
270bc068586SAdrian Prantl     DEBUG({
271bc068586SAdrian Prantl       // Print the IR for the PCH container to the debug output.
272bc068586SAdrian Prantl       llvm::SmallString<0> Buffer;
273bc068586SAdrian Prantl       llvm::raw_svector_ostream OS(Buffer);
274bc068586SAdrian Prantl       clang::EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
275*b214cbc7SJames Y Knight                                Ctx.getTargetInfo().getDataLayout(), M.get(),
276*b214cbc7SJames Y Knight                                BackendAction::Backend_EmitLL, &OS);
277bc068586SAdrian Prantl       llvm::dbgs() << Buffer;
278bc068586SAdrian Prantl     });
279bc068586SAdrian Prantl 
280bc068586SAdrian Prantl     // Use the LLVM backend to emit the pch container.
281bc068586SAdrian Prantl     clang::EmitBackendOutput(Diags, CodeGenOpts, TargetOpts, LangOpts,
282*b214cbc7SJames Y Knight                              Ctx.getTargetInfo().getDataLayout(), M.get(),
283*b214cbc7SJames Y Knight                              BackendAction::Backend_EmitObj, OS);
284bc068586SAdrian Prantl 
285bc068586SAdrian Prantl     // Make sure the pch container hits disk.
286bc068586SAdrian Prantl     OS->flush();
287bc068586SAdrian Prantl 
288bc068586SAdrian Prantl     // Free the memory for the temporary buffer.
289bc068586SAdrian Prantl     llvm::SmallVector<char, 0> Empty;
290bc068586SAdrian Prantl     SerializedAST = std::move(Empty);
291bc068586SAdrian Prantl   }
292bc068586SAdrian Prantl };
2935a88e1a8SAdrian Prantl 
2947eb5464bSHans Wennborg } // anonymous namespace
295bc068586SAdrian Prantl 
296bc068586SAdrian Prantl std::unique_ptr<ASTConsumer>
297fb2398d0SAdrian Prantl ObjectFilePCHContainerWriter::CreatePCHContainerGenerator(
2981e63b2bdSAdrian Prantl     CompilerInstance &CI, const std::string &MainFileName,
2991e63b2bdSAdrian Prantl     const std::string &OutputFileName, llvm::raw_pwrite_stream *OS,
3001e63b2bdSAdrian Prantl     std::shared_ptr<PCHBuffer> Buffer) const {
3011e63b2bdSAdrian Prantl   return llvm::make_unique<PCHContainerGenerator>(CI, MainFileName,
3020391406eSAdrian Prantl                                                   OutputFileName, OS, Buffer);
303bc068586SAdrian Prantl }
304bc068586SAdrian Prantl 
305fb2398d0SAdrian Prantl void ObjectFilePCHContainerReader::ExtractPCH(
306bc068586SAdrian Prantl     llvm::MemoryBufferRef Buffer, llvm::BitstreamReader &StreamFile) const {
307bc068586SAdrian Prantl   if (auto OF = llvm::object::ObjectFile::createObjectFile(Buffer)) {
308bc068586SAdrian Prantl     auto *Obj = OF.get().get();
309bc068586SAdrian Prantl     bool IsCOFF = isa<llvm::object::COFFObjectFile>(Obj);
310bc068586SAdrian Prantl     // Find the clang AST section in the container.
311bc068586SAdrian Prantl     for (auto &Section : OF->get()->sections()) {
312bc068586SAdrian Prantl       StringRef Name;
313bc068586SAdrian Prantl       Section.getName(Name);
314bc068586SAdrian Prantl       if ((!IsCOFF && Name == "__clangast") ||
315bc068586SAdrian Prantl           ( IsCOFF && Name ==   "clangast")) {
316bc068586SAdrian Prantl         StringRef Buf;
317bc068586SAdrian Prantl         Section.getContents(Buf);
318bc068586SAdrian Prantl         StreamFile.init((const unsigned char *)Buf.begin(),
319bc068586SAdrian Prantl                         (const unsigned char *)Buf.end());
320bc068586SAdrian Prantl         return;
321bc068586SAdrian Prantl       }
322bc068586SAdrian Prantl     }
323bc068586SAdrian Prantl   }
324bc068586SAdrian Prantl 
325bc068586SAdrian Prantl   // As a fallback, treat the buffer as a raw AST.
326bc068586SAdrian Prantl   StreamFile.init((const unsigned char *)Buffer.getBufferStart(),
327bc068586SAdrian Prantl                   (const unsigned char *)Buffer.getBufferEnd());
328bc068586SAdrian Prantl }
329