summaryrefslogtreecommitdiff
path: root/lib/AsmParser/Parser.cpp
blob: 57c831e9e17e581e22746ee6a3d61dc488fd2a46 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
//===- Parser.cpp - Main dispatch module for the Parser library -------------===
//
// This library implements the functionality defined in llvm/assembly/parser.h
//
//===------------------------------------------------------------------------===

#include "llvm/Analysis/Verifier.h"
#include "llvm/Module.h"
#include "ParserInternals.h"
#include <stdio.h>  // for sprintf

// The useful interface defined by this file... Parse an ascii file, and return
// the internal representation in a nice slice'n'dice'able representation.
//
Module *ParseAssemblyFile(const ToolCommandLine &Opts) throw (ParseException) {
  FILE *F = stdin;

  if (Opts.getInputFilename() != "-") 
    F = fopen(Opts.getInputFilename().c_str(), "r");

  if (F == 0) {
    throw ParseException(Opts, string("Could not open file '") + 
			 Opts.getInputFilename() + "'");
  }

  // TODO: If this throws an exception, F is not closed.
  Module *Result = RunVMAsmParser(Opts, F);

  if (F != stdin)
    fclose(F);

  if (Result) {  // Check to see that it is valid...
    vector<string> Errors;
    if (verify(Result, Errors)) {
      delete Result; Result = 0;
      string Message;

      for (unsigned i = 0; i < Errors.size(); i++)
	Message += Errors[i] + "\n";

      throw ParseException(Opts, Message);
    }
  }
  return Result;
}


//===------------------------------------------------------------------------===
//                              ParseException Class
//===------------------------------------------------------------------------===


ParseException::ParseException(const ToolCommandLine &opts, 
			       const string &message, int lineNo, int colNo) 
  : Opts(opts), Message(message) {
  LineNo = lineNo; ColumnNo = colNo;
}

ParseException::ParseException(const ParseException &E) 
  : Opts(E.Opts), Message(E.Message) {
  LineNo = E.LineNo;
  ColumnNo = E.ColumnNo;
}

const string ParseException::getMessage() const { // Includes info from options
  string Result;
  char Buffer[10];

  if (Opts.getInputFilename() == "-") 
    Result += "<stdin>";
  else
    Result += Opts.getInputFilename();

  if (LineNo != -1) {
    sprintf(Buffer, "%d", LineNo);
    Result += string(":") + Buffer;
    if (ColumnNo != -1) {
      sprintf(Buffer, "%d", ColumnNo);
      Result += string(",") + Buffer;
    }
  }
  
  return Result + ": " + Message;
}