Parser.cpp revision 31895e73591d3c9ceae731a1274c8f56194b9616
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/Module.h" 17#include "llvm/ADT/OwningPtr.h" 18#include "llvm/Support/MemoryBuffer.h" 19#include "llvm/Support/raw_ostream.h" 20#include <cstring> 21using namespace llvm; 22 23Module *llvm::ParseAssemblyFile(const std::string &Filename, ParseError &Err, 24 const LLVMContext& Context) { 25 Err.setFilename(Filename); 26 27 std::string ErrorStr; 28 OwningPtr<MemoryBuffer> 29 F(MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr)); 30 if (F == 0) { 31 Err.setError("Could not open input file '" + Filename + "'"); 32 return 0; 33 } 34 35 OwningPtr<Module> M(new Module(Filename, Context)); 36 if (LLParser(F.get(), Err, M.get()).Run()) 37 return 0; 38 return M.take(); 39} 40 41Module *llvm::ParseAssemblyString(const char *AsmString, Module *M, 42 ParseError &Err, const LLVMContext& Context) { 43 Err.setFilename("<string>"); 44 45 OwningPtr<MemoryBuffer> 46 F(MemoryBuffer::getMemBuffer(AsmString, AsmString+strlen(AsmString), 47 "<string>")); 48 49 // If we are parsing into an existing module, do it. 50 if (M) 51 return LLParser(F.get(), Err, M).Run() ? 0 : M; 52 53 // Otherwise create a new module. 54 OwningPtr<Module> M2(new Module("<string>", Context)); 55 if (LLParser(F.get(), Err, M2.get()).Run()) 56 return 0; 57 return M2.take(); 58} 59 60 61//===------------------------------------------------------------------------=== 62// ParseError Class 63//===------------------------------------------------------------------------=== 64 65void ParseError::PrintError(const char *ProgName, raw_ostream &S) { 66 errs() << ProgName << ": "; 67 if (Filename == "-") 68 errs() << "<stdin>"; 69 else 70 errs() << Filename; 71 72 if (LineNo != -1) { 73 errs() << ':' << LineNo; 74 if (ColumnNo != -1) 75 errs() << ':' << (ColumnNo+1); 76 } 77 78 errs() << ": " << Message << '\n'; 79 80 if (LineNo != -1 && ColumnNo != -1) { 81 errs() << LineContents << '\n'; 82 83 // Print out spaces/tabs before the caret. 84 for (unsigned i = 0; i != unsigned(ColumnNo); ++i) 85 errs() << (LineContents[i] == '\t' ? '\t' : ' '); 86 errs() << "^\n"; 87 } 88} 89