Unbreak build with gcc 4.3: provide missed includes and silence most annoying warnings.
[oota-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[0], Filename.size(),
26                                                  &ErrorStr);
27   if (F == 0) {
28     if (Err)
29       Err->setError(Filename, "Could not open input file '" + Filename + "'");
30     return 0;
31   }
32   
33   TheParseError = Err;
34   Module *Result = RunVMAsmParser(F);
35   delete F;
36   return Result;
37 }
38
39 Module *llvm::ParseAssemblyString(const char *AsmString, Module *M, 
40                                   ParseError *Err) {
41   TheParseError = Err;
42   MemoryBuffer *F = MemoryBuffer::getMemBuffer(AsmString, 
43                                                AsmString+strlen(AsmString),
44                                                "<string>");
45   Module *Result = RunVMAsmParser(F);
46   delete F;
47   return Result;
48 }
49
50
51 //===------------------------------------------------------------------------===
52 //                              ParseError Class
53 //===------------------------------------------------------------------------===
54
55
56 void ParseError::setError(const std::string &filename,
57                           const std::string &message,
58                           int lineNo, int colNo) {
59   Filename = filename;
60   Message = message;
61   LineNo = lineNo;
62   colNo = colNo;
63 }
64
65 ParseError::ParseError(const ParseError &E)
66   : Filename(E.Filename), Message(E.Message) {
67   LineNo = E.LineNo;
68   ColumnNo = E.ColumnNo;
69 }
70
71 // Includes info from options
72 const std::string ParseError::getMessage() const {
73   std::string Result;
74   char Buffer[10];
75
76   if (Filename == "-")
77     Result += "<stdin>";
78   else
79     Result += Filename;
80
81   if (LineNo != -1) {
82     sprintf(Buffer, "%d", LineNo);
83     Result += std::string(":") + Buffer;
84     if (ColumnNo != -1) {
85       sprintf(Buffer, "%d", ColumnNo);
86       Result += std::string(",") + Buffer;
87     }
88   }
89
90   return Result + ": " + Message;
91 }