1 //===- Parser.cpp - Main dispatch module for the Parser library -----------===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This library implements the functionality defined in llvm/AsmParser/Parser.h
12 //===----------------------------------------------------------------------===//
14 #include "llvm/AsmParser/Parser.h"
16 #include "llvm/ADT/STLExtras.h"
17 #include "llvm/IR/Module.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/SourceMgr.h"
20 #include "llvm/Support/raw_ostream.h"
22 #include <system_error>
25 bool llvm::parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err) {
27 std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F, false);
28 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
30 return LLParser(F.getBuffer(), SM, Err, &M).Run();
33 std::unique_ptr<Module> llvm::parseAssembly(MemoryBufferRef F,
35 LLVMContext &Context) {
36 std::unique_ptr<Module> M =
37 make_unique<Module>(F.getBufferIdentifier(), Context);
39 if (parseAssemblyInto(F, *M, Err))
45 std::unique_ptr<Module> llvm::parseAssemblyFile(StringRef Filename,
47 LLVMContext &Context) {
48 ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
49 MemoryBuffer::getFileOrSTDIN(Filename);
50 if (std::error_code EC = FileOrErr.getError()) {
51 Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
52 "Could not open input file: " + EC.message());
56 return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context);
59 std::unique_ptr<Module> llvm::parseAssemblyString(StringRef AsmString,
61 LLVMContext &Context) {
62 MemoryBufferRef F(AsmString, "<string>");
63 return parseAssembly(F, Err, Context);