LLVM API Documentation
00001 //===- Parser.cpp - Main dispatch module for the Parser library -----------===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This library implements the functionality defined in llvm/Assembly/Parser.h 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/Assembly/Parser.h" 00015 #include "LLParser.h" 00016 #include "llvm/Module.h" 00017 #include "llvm/ADT/OwningPtr.h" 00018 #include "llvm/Support/MemoryBuffer.h" 00019 #include "llvm/Support/raw_ostream.h" 00020 #include <cstring> 00021 using namespace llvm; 00022 00023 Module *llvm::ParseAssemblyFile(const std::string &Filename, ParseError &Err) { 00024 Err.setFilename(Filename); 00025 00026 std::string ErrorStr; 00027 OwningPtr<MemoryBuffer> 00028 F(MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr)); 00029 if (F == 0) { 00030 Err.setError("Could not open input file '" + Filename + "'"); 00031 return 0; 00032 } 00033 00034 OwningPtr<Module> M(new Module(Filename)); 00035 if (LLParser(F.get(), Err, M.get()).Run()) 00036 return 0; 00037 return M.take(); 00038 } 00039 00040 Module *llvm::ParseAssemblyString(const char *AsmString, Module *M, 00041 ParseError &Err) { 00042 Err.setFilename("<string>"); 00043 00044 OwningPtr<MemoryBuffer> 00045 F(MemoryBuffer::getMemBuffer(AsmString, AsmString+strlen(AsmString), 00046 "<string>")); 00047 00048 // If we are parsing into an existing module, do it. 00049 if (M) 00050 return LLParser(F.get(), Err, M).Run() ? 0 : M; 00051 00052 // Otherwise create a new module. 00053 OwningPtr<Module> M2(new Module("<string>")); 00054 if (LLParser(F.get(), Err, M2.get()).Run()) 00055 return 0; 00056 return M2.take(); 00057 } 00058 00059 00060 //===------------------------------------------------------------------------=== 00061 // ParseError Class 00062 //===------------------------------------------------------------------------=== 00063 00064 void ParseError::PrintError(const char *ProgName, raw_ostream &S) { 00065 errs() << ProgName << ": "; 00066 if (Filename == "-") 00067 errs() << "<stdin>"; 00068 else 00069 errs() << Filename; 00070 00071 if (LineNo != -1) { 00072 errs() << ':' << LineNo; 00073 if (ColumnNo != -1) 00074 errs() << ':' << (ColumnNo+1); 00075 } 00076 00077 errs() << ": " << Message << '\n'; 00078 00079 if (LineNo != -1 && ColumnNo != -1) { 00080 errs() << LineContents << '\n'; 00081 00082 // Print out spaces/tabs before the caret. 00083 for (unsigned i = 0; i != unsigned(ColumnNo); ++i) 00084 errs() << (LineContents[i] == '\t' ? '\t' : ' '); 00085 errs() << "^\n"; 00086 } 00087 }