blob: 157a43b1aa2a8b91ca50eaa2c00528b6dd74d121 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- Parser.cpp - Main dispatch module for the Parser library -------------===
2//
3// The LLVM Compiler Infrastructure
4//
Chris Lattner081ce942007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Dan Gohmanf17a25c2007-07-18 16:29:46 +00007//
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"
Chris Lattner17e73c22007-11-18 08:46:26 +000016#include "llvm/Support/MemoryBuffer.h"
Anton Korobeynikov357a27d2008-02-20 11:08:44 +000017#include <cstring>
Dan Gohmanf17a25c2007-07-18 16:29:46 +000018using namespace llvm;
19
20
21ParseError* TheParseError = 0; /// FIXME: Not threading friendly
22
23Module *llvm::ParseAssemblyFile(const std::string &Filename, ParseError* Err) {
Chris Lattner17e73c22007-11-18 08:46:26 +000024 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;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031 }
Chris Lattner17e73c22007-11-18 08:46:26 +000032
Dan Gohmanf17a25c2007-07-18 16:29:46 +000033 TheParseError = Err;
Chris Lattner17e73c22007-11-18 08:46:26 +000034 Module *Result = RunVMAsmParser(F);
35 delete F;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000036 return Result;
37}
38
Chris Lattner17e73c22007-11-18 08:46:26 +000039Module *llvm::ParseAssemblyString(const char *AsmString, Module *M,
40 ParseError *Err) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000041 TheParseError = Err;
Chris Lattner17e73c22007-11-18 08:46:26 +000042 MemoryBuffer *F = MemoryBuffer::getMemBuffer(AsmString,
43 AsmString+strlen(AsmString),
44 "<string>");
45 Module *Result = RunVMAsmParser(F);
46 delete F;
47 return Result;
Dan Gohmanf17a25c2007-07-18 16:29:46 +000048}
49
50
51//===------------------------------------------------------------------------===
52// ParseError Class
53//===------------------------------------------------------------------------===
54
55
56void ParseError::setError(const std::string &filename,
Chris Lattner17e73c22007-11-18 08:46:26 +000057 const std::string &message,
58 int lineNo, int colNo) {
Dan Gohmanf17a25c2007-07-18 16:29:46 +000059 Filename = filename;
60 Message = message;
61 LineNo = lineNo;
62 colNo = colNo;
63}
64
65ParseError::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
72const 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}