aboutsummaryrefslogtreecommitdiffstats
path: root/lib/AsmParser/Parser.cpp
blob: a99849d31b45f8a582d51b1b53a23670f17b1c98 (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
//===- 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
using std::string;

// 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 string &Filename) { // throw (ParseException)
  FILE *F = stdin;

  if (Filename != "-") {
    F = fopen(Filename.c_str(), "r");

    if (F == 0)
      throw ParseException(Filename, "Could not open file '" + Filename + "'");
  }

  Module *Result;
  try {
    Result = RunVMAsmParser(Filename, F);
  } catch (...) {
    if (F != stdin) fclose(F);      // Make sure to close file descriptor if an
    throw;                          // exception is thrown
  }

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

  if (Result) {  // Check to see that it is valid...
    if (verifyModule(Result)) {
      delete Result;
      throw ParseException(Filename, "Source file is not well formed LLVM!");
    }
  }
  return Result;
}


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


ParseException::ParseException(const string &filename, const string &message, 
			       int lineNo, int colNo) 
  : Filename(filename), Message(message) {
  LineNo = lineNo; ColumnNo = colNo;
}

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

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

  if (Filename == "-") 
    Result += "<stdin>";
  else
    Result += Filename;

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