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/Assembly/Parser.h
12 //===----------------------------------------------------------------------===//
14 #include "llvm/Assembly/Parser.h"
16 #include "llvm/Module.h"
17 #include "llvm/ADT/OwningPtr.h"
18 #include "llvm/Support/MemoryBuffer.h"
19 #include "llvm/Support/raw_ostream.h"
23 Module *llvm::ParseAssemblyFile(const std::string &Filename, ParseError &Err,
24 const LLVMContext& Context) {
25 Err.setFilename(Filename);
28 OwningPtr<MemoryBuffer>
29 F(MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr));
31 Err.setError("Could not open input file '" + Filename + "'");
35 OwningPtr<Module> M(new Module(Filename, Context));
36 if (LLParser(F.get(), Err, M.get()).Run())
41 Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
42 ParseError &Err, const LLVMContext& Context) {
43 Err.setFilename("<string>");
45 OwningPtr<MemoryBuffer>
46 F(MemoryBuffer::getMemBuffer(AsmString, AsmString+strlen(AsmString),
49 // If we are parsing into an existing module, do it.
51 return LLParser(F.get(), Err, M).Run() ? 0 : M;
53 // Otherwise create a new module.
54 OwningPtr<Module> M2(new Module("<string>", Context));
55 if (LLParser(F.get(), Err, M2.get()).Run())
61 //===------------------------------------------------------------------------===
63 //===------------------------------------------------------------------------===
65 void ParseError::PrintError(const char *ProgName, raw_ostream &S) {
66 errs() << ProgName << ": ";
73 errs() << ':' << LineNo;
75 errs() << ':' << (ColumnNo+1);
78 errs() << ": " << Message << '\n';
80 if (LineNo != -1 && ColumnNo != -1) {
81 errs() << LineContents << '\n';
83 // Print out spaces/tabs before the caret.
84 for (unsigned i = 0; i != unsigned(ColumnNo); ++i)
85 errs() << (LineContents[i] == '\t' ? '\t' : ' ');