Remove debugging code accidentally checked in!
[oota-llvm.git] / lib / AsmParser / Parser.cpp
1 //===- Parser.cpp - Main dispatch module for the Parser library -------------===
2 //
3 // This library implements the functionality defined in llvm/assembly/parser.h
4 //
5 //===------------------------------------------------------------------------===
6
7 #include "llvm/Analysis/Verifier.h"
8 #include "llvm/Module.h"
9 #include "ParserInternals.h"
10 #include <stdio.h>  // for sprintf
11 using std::string;
12
13 // The useful interface defined by this file... Parse an ascii file, and return
14 // the internal representation in a nice slice'n'dice'able representation.
15 //
16 Module *ParseAssemblyFile(const string &Filename) { // throw (ParseException)
17   FILE *F = stdin;
18
19   if (Filename != "-") {
20     F = fopen(Filename.c_str(), "r");
21
22     if (F == 0)
23       throw ParseException(Filename, "Could not open file '" + Filename + "'");
24   }
25
26   Module *Result;
27   try {
28     Result = RunVMAsmParser(Filename, F);
29   } catch (...) {
30     if (F != stdin) fclose(F);      // Make sure to close file descriptor if an
31     throw;                          // exception is thrown
32   }
33
34   if (F != stdin)
35     fclose(F);
36
37   if (Result) {  // Check to see that it is valid...
38     if (verifyModule(Result)) {
39       delete Result;
40       throw ParseException(Filename, "Source file is not well formed LLVM!");
41     }
42   }
43   return Result;
44 }
45
46
47 //===------------------------------------------------------------------------===
48 //                              ParseException Class
49 //===------------------------------------------------------------------------===
50
51
52 ParseException::ParseException(const string &filename, const string &message, 
53                                int lineNo, int colNo) 
54   : Filename(filename), Message(message) {
55   LineNo = lineNo; ColumnNo = colNo;
56 }
57
58 ParseException::ParseException(const ParseException &E) 
59   : Filename(E.Filename), Message(E.Message) {
60   LineNo = E.LineNo;
61   ColumnNo = E.ColumnNo;
62 }
63
64 const string ParseException::getMessage() const { // Includes info from options
65   string Result;
66   char Buffer[10];
67
68   if (Filename == "-") 
69     Result += "<stdin>";
70   else
71     Result += Filename;
72
73   if (LineNo != -1) {
74     sprintf(Buffer, "%d", LineNo);
75     Result += string(":") + Buffer;
76     if (ColumnNo != -1) {
77       sprintf(Buffer, "%d", ColumnNo);
78       Result += string(",") + Buffer;
79     }
80   }
81   
82   return Result + ": " + Message;
83 }