1 //===- Parser.cpp - Main dispatch module for the Parser library -----------===// 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 // This library implements the functionality defined in llvm/AsmParser/Parser.h 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/AsmParser/Parser.h" 15 #include "LLParser.h" 16 #include "llvm/IR/Module.h" 17 #include "llvm/Support/MemoryBuffer.h" 18 #include "llvm/Support/SourceMgr.h" 19 #include "llvm/Support/raw_ostream.h" 20 #include <cstring> 21 #include <system_error> 22 using namespace llvm; 23 24 bool llvm::parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err) { 25 SourceMgr SM; 26 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F, false); 27 SM.AddNewSourceBuffer(std::move(Buf), SMLoc()); 28 29 return LLParser(F.getBuffer(), SM, Err, &M).Run(); 30 } 31 32 std::unique_ptr<Module> llvm::parseAssembly(MemoryBufferRef F, 33 SMDiagnostic &Err, 34 LLVMContext &Context) { 35 std::unique_ptr<Module> M = 36 make_unique<Module>(F.getBufferIdentifier(), Context); 37 38 if (parseAssemblyInto(F, *M, Err)) 39 return nullptr; 40 41 return std::move(M); 42 } 43 44 std::unique_ptr<Module> llvm::parseAssemblyFile(StringRef Filename, 45 SMDiagnostic &Err, 46 LLVMContext &Context) { 47 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 48 MemoryBuffer::getFileOrSTDIN(Filename); 49 if (std::error_code EC = FileOrErr.getError()) { 50 Err = SMDiagnostic(Filename, SourceMgr::DK_Error, 51 "Could not open input file: " + EC.message()); 52 return nullptr; 53 } 54 55 return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context); 56 } 57 58 std::unique_ptr<Module> llvm::parseAssemblyString(StringRef AsmString, 59 SMDiagnostic &Err, 60 LLVMContext &Context) { 61 MemoryBufferRef F(AsmString, "<string>"); 62 return parseAssembly(F, Err, Context); 63 } 64