LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
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/Assembly/Parser.h
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "llvm/Assembly/Parser.h"
15 #include "LLParser.h"
16 #include "llvm/ADT/OwningPtr.h"
17 #include "llvm/IR/Module.h"
19 #include "llvm/Support/SourceMgr.h"
22 #include <cstring>
23 using namespace llvm;
24 
26  Module *M,
27  SMDiagnostic &Err,
28  LLVMContext &Context) {
29  SourceMgr SM;
30  SM.AddNewSourceBuffer(F, SMLoc());
31 
32  // If we are parsing into an existing module, do it.
33  if (M)
34  return LLParser(F, SM, Err, M).Run() ? 0 : M;
35 
36  // Otherwise create a new module.
37  OwningPtr<Module> M2(new Module(F->getBufferIdentifier(), Context));
38  if (LLParser(F, SM, Err, M2.get()).Run())
39  return 0;
40  return M2.take();
41 }
42 
43 Module *llvm::ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Err,
44  LLVMContext &Context) {
46  if (error_code ec = MemoryBuffer::getFileOrSTDIN(Filename, File)) {
47  Err = SMDiagnostic(Filename, SourceMgr::DK_Error,
48  "Could not open input file: " + ec.message());
49  return 0;
50  }
51 
52  return ParseAssembly(File.take(), 0, Err, Context);
53 }
54 
55 Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
56  SMDiagnostic &Err, LLVMContext &Context) {
57  MemoryBuffer *F =
58  MemoryBuffer::getMemBuffer(StringRef(AsmString, strlen(AsmString)),
59  "<string>");
60 
61  return ParseAssembly(F, M, Err, Context);
62 }
static MemoryBuffer * getMemBuffer(StringRef InputData, StringRef BufferName="", bool RequiresNullTerminator=true)
The main container class for the LLVM Intermediate Representation.
Definition: Module.h:112
F(f)
bool Run()
Run: module ::= toplevelentity*.
Definition: LLParser.cpp:38
Module * ParseAssembly(MemoryBuffer *F, Module *M, SMDiagnostic &Err, LLVMContext &Context)
Parse LLVM Assembly from a MemoryBuffer. This function always takes ownership of the MemoryBuffer...
Definition: Parser.cpp:25
virtual const char * getBufferIdentifier() const
Definition: MemoryBuffer.h:61
static error_code getFileOrSTDIN(StringRef Filename, OwningPtr< MemoryBuffer > &result, int64_t FileSize=-1)
T * get() const
Definition: OwningPtr.h:72
size_t strlen(const char *s);
Module * ParseAssemblyString(const char *AsmString, Module *M, SMDiagnostic &Error, LLVMContext &Context)
Parse LLVM Assembly from a string.
Definition: Parser.cpp:55
Module * ParseAssemblyFile(const std::string &Filename, SMDiagnostic &Error, LLVMContext &Context)
Parse LLVM Assembly from a file.
Definition: Parser.cpp:43
Represents a location in source code.
Definition: SMLoc.h:23
size_t AddNewSourceBuffer(MemoryBuffer *F, SMLoc IncludeLoc)
Definition: SourceMgr.h:112