OSDN Git Service

Change the MemoryBuffer::getFile* methods to take just a pointer to the
[android-x86/external-llvm.git] / lib / AsmParser / Parser.cpp
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 "ParserInternals.h"
15 #include "llvm/Module.h"
16 #include "llvm/Support/MemoryBuffer.h"
17 #include <cstring>
18 using namespace llvm;
19
20
21 ParseError* TheParseError = 0; /// FIXME: Not threading friendly
22
23 Module *llvm::ParseAssemblyFile(const std::string &Filename, ParseError* Err) {
24   std::string ErrorStr;
25   MemoryBuffer *F = MemoryBuffer::getFileOrSTDIN(Filename.c_str(), &ErrorStr);
26   if (F == 0) {
27     if (Err)
28       Err->setError(Filename, "Could not open input file '" + Filename + "'");
29     return 0;
30   }
31   
32   TheParseError = Err;
33   Module *Result = RunVMAsmParser(F);
34   delete F;
35   return Result;
36 }
37
38 Module *llvm::ParseAssemblyString(const char *AsmString, Module *M, 
39                                   ParseError *Err) {
40   TheParseError = Err;
41   MemoryBuffer *F = MemoryBuffer::getMemBuffer(AsmString, 
42                                                AsmString+strlen(AsmString),
43                                                "<string>");
44   Module *Result = RunVMAsmParser(F);
45   delete F;
46   return Result;
47 }
48
49
50 //===------------------------------------------------------------------------===
51 //                              ParseError Class
52 //===------------------------------------------------------------------------===
53
54
55 void ParseError::setError(const std::string &filename,
56                           const std::string &message,
57                           int lineNo, int colNo) {
58   Filename = filename;
59   Message = message;
60   LineNo = lineNo;
61   colNo = colNo;
62 }
63
64 ParseError::ParseError(const ParseError &E)
65   : Filename(E.Filename), Message(E.Message) {
66   LineNo = E.LineNo;
67   ColumnNo = E.ColumnNo;
68 }
69
70 // Includes info from options
71 const std::string ParseError::getMessage() const {
72   std::string Result;
73   char Buffer[10];
74
75   if (Filename == "-")
76     Result += "<stdin>";
77   else
78     Result += Filename;
79
80   if (LineNo != -1) {
81     sprintf(Buffer, "%d", LineNo);
82     Result += std::string(":") + Buffer;
83     if (ColumnNo != -1) {
84       sprintf(Buffer, "%d", ColumnNo);
85       Result += std::string(",") + Buffer;
86     }
87   }
88
89   return Result + ": " + Message;
90 }