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