LLVM API Documentation
00001 //===- Parser.cpp - Main dispatch module for the Parser library -----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This library implements the functionality defined in llvm/AsmParser/Parser.h 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/AsmParser/Parser.h" 00015 #include "LLParser.h" 00016 #include "llvm/IR/Module.h" 00017 #include "llvm/Support/MemoryBuffer.h" 00018 #include "llvm/Support/SourceMgr.h" 00019 #include "llvm/Support/raw_ostream.h" 00020 #include <cstring> 00021 #include <system_error> 00022 using namespace llvm; 00023 00024 bool llvm::parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err) { 00025 SourceMgr SM; 00026 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F, false); 00027 SM.AddNewSourceBuffer(std::move(Buf), SMLoc()); 00028 00029 return LLParser(F.getBuffer(), SM, Err, &M).Run(); 00030 } 00031 00032 std::unique_ptr<Module> llvm::parseAssembly(MemoryBufferRef F, 00033 SMDiagnostic &Err, 00034 LLVMContext &Context) { 00035 std::unique_ptr<Module> M = 00036 make_unique<Module>(F.getBufferIdentifier(), Context); 00037 00038 if (parseAssemblyInto(F, *M, Err)) 00039 return nullptr; 00040 00041 return std::move(M); 00042 } 00043 00044 std::unique_ptr<Module> llvm::parseAssemblyFile(StringRef Filename, 00045 SMDiagnostic &Err, 00046 LLVMContext &Context) { 00047 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr = 00048 MemoryBuffer::getFileOrSTDIN(Filename); 00049 if (std::error_code EC = FileOrErr.getError()) { 00050 Err = SMDiagnostic(Filename, SourceMgr::DK_Error, 00051 "Could not open input file: " + EC.message()); 00052 return nullptr; 00053 } 00054 00055 return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context); 00056 } 00057 00058 std::unique_ptr<Module> llvm::parseAssemblyString(StringRef AsmString, 00059 SMDiagnostic &Err, 00060 LLVMContext &Context) { 00061 MemoryBufferRef F(AsmString, "<string>"); 00062 return parseAssembly(F, Err, Context); 00063 }