LLVM  3.7.0
Parser.cpp
Go to the documentation of this file.
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/ADT/STLExtras.h"
17 #include "llvm/IR/Module.h"
19 #include "llvm/Support/SourceMgr.h"
21 #include <cstring>
22 #include <system_error>
23 using namespace llvm;
24 
26  SlotMapping *Slots) {
27  SourceMgr SM;
28  std::unique_ptr<MemoryBuffer> Buf = MemoryBuffer::getMemBuffer(F);
29  SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
30 
31  return LLParser(F.getBuffer(), SM, Err, &M, Slots).Run();
32 }
33 
34 std::unique_ptr<Module> llvm::parseAssembly(MemoryBufferRef F,
35  SMDiagnostic &Err,
36  LLVMContext &Context,
37  SlotMapping *Slots) {
38  std::unique_ptr<Module> M =
39  make_unique<Module>(F.getBufferIdentifier(), Context);
40 
41  if (parseAssemblyInto(F, *M, Err, Slots))
42  return nullptr;
43 
44  return M;
45 }
46 
47 std::unique_ptr<Module> llvm::parseAssemblyFile(StringRef Filename,
48  SMDiagnostic &Err,
49  LLVMContext &Context,
50  SlotMapping *Slots) {
53  if (std::error_code EC = FileOrErr.getError()) {
54  Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
55  "Could not open input file: " + EC.message());
56  return nullptr;
57  }
58 
59  return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots);
60 }
61 
62 std::unique_ptr<Module> llvm::parseAssemblyString(StringRef AsmString,
63  SMDiagnostic &Err,
64  LLVMContext &Context,
65  SlotMapping *Slots) {
66  MemoryBufferRef F(AsmString, "<string>");
67  return parseAssembly(F, Err, Context, Slots);
68 }
std::error_code getError() const
Definition: ErrorOr.h:178
Represents either an error or a value T.
Definition: ErrorOr.h:82
A Module instance is used to store all the information related to an LLVM module. ...
Definition: Module.h:114
static std::unique_ptr< MemoryBuffer > getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
Open the specified memory range as a MemoryBuffer.
F(f)
unsigned AddNewSourceBuffer(std::unique_ptr< MemoryBuffer > F, SMLoc IncludeLoc)
Add a new source buffer to this source manager.
Definition: SourceMgr.h:123
std::unique_ptr< Module > parseAssembly(MemoryBufferRef F, SMDiagnostic &Err, LLVMContext &Context, SlotMapping *Slots=nullptr)
parseAssemblyFile and parseAssemblyString are wrappers around this function.
Definition: Parser.cpp:34
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:41
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling...
Definition: SourceMgr.h:35
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFileOrSTDIN(const Twine &Filename, int64_t FileSize=-1)
Open the specified file as a MemoryBuffer, or open stdin if the Filename is "-".
StringRef getBuffer() const
Definition: MemoryBuffer.h:157
Module.h This file contains the declarations for the Module class.
bool parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err, SlotMapping *Slots=nullptr)
This function is the low-level interface to the LLVM Assembly Parser.
Definition: Parser.cpp:25
This struct contains the mapping from the slot numbers to unnamed metadata nodes and global values...
Definition: SlotMapping.h:27
std::unique_ptr< Module > parseAssemblyFile(StringRef Filename, SMDiagnostic &Error, LLVMContext &Context, SlotMapping *Slots=nullptr)
This function is the main interface to the LLVM Assembly Parser.
Definition: Parser.cpp:47
StringRef getBufferIdentifier() const
Definition: MemoryBuffer.h:159
std::unique_ptr< Module > parseAssemblyString(StringRef AsmString, SMDiagnostic &Error, LLVMContext &Context, SlotMapping *Slots=nullptr)
The function is a secondary interface to the LLVM Assembly Parser.
Definition: Parser.cpp:62
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:40
Represents a location in source code.
Definition: SMLoc.h:23
reference get()
Definition: ErrorOr.h:175
Instances of this class encapsulate one diagnostic report, allowing printing to a raw_ostream as a ca...
Definition: SourceMgr.h:233