Modernize the .ll parsing interface.
[oota-llvm.git] / lib / AsmParser / Parser.cpp
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 std::unique_ptr<Module> llvm::parseAssembly(std::unique_ptr<MemoryBuffer> F,
25                                             SMDiagnostic &Err,
26                                             LLVMContext &Context) {
27   SourceMgr SM;
28   MemoryBuffer *Buf = F.get();
29   SM.AddNewSourceBuffer(F.release(), SMLoc());
30
31   std::unique_ptr<Module> M =
32       make_unique<Module>(Buf->getBufferIdentifier(), Context);
33   if (LLParser(Buf->getBuffer(), SM, Err, M.get()).Run())
34     return nullptr;
35   return std::move(M);
36 }
37
38 std::unique_ptr<Module> llvm::parseAssemblyFile(StringRef Filename,
39                                                 SMDiagnostic &Err,
40                                                 LLVMContext &Context) {
41   ErrorOr<std::unique_ptr<MemoryBuffer>> FileOrErr =
42       MemoryBuffer::getFileOrSTDIN(Filename);
43   if (std::error_code EC = FileOrErr.getError()) {
44     Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
45                        "Could not open input file: " + EC.message());
46     return nullptr;
47   }
48
49   return parseAssembly(std::move(FileOrErr.get()), Err, Context);
50 }
51
52 std::unique_ptr<Module> llvm::parseAssemblyString(StringRef AsmString,
53                                                   SMDiagnostic &Err,
54                                                   LLVMContext &Context) {
55   std::unique_ptr<MemoryBuffer> F(
56       MemoryBuffer::getMemBuffer(AsmString, "<string>"));
57
58   return parseAssembly(std::move(F), Err, Context);
59 }