LLVM API Documentation

Parser.cpp

Go to the documentation of this file.
00001 //===- Parser.cpp - Main dispatch module for the Parser library -------------===
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file was developed by the LLVM research group and is distributed under
00006 // the University of Illinois Open Source License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This library implements the functionality defined in llvm/assembly/parser.h
00011 //
00012 //===------------------------------------------------------------------------===
00013 
00014 #include "ParserInternals.h"
00015 #include "llvm/Module.h"
00016 using namespace llvm;
00017 
00018 // The useful interface defined by this file... Parse an ASCII file, and return
00019 // the internal representation in a nice slice'n'dice'able representation.
00020 //
00021 Module *llvm::ParseAssemblyFile(const std::string &Filename) {
00022   FILE *F = stdin;
00023 
00024   if (Filename != "-") {
00025     F = fopen(Filename.c_str(), "r");
00026 
00027     if (F == 0)
00028       throw ParseException(Filename, "Could not open file '" + Filename + "'");
00029   }
00030 
00031   Module *Result;
00032   try {
00033     Result = RunVMAsmParser(Filename, F);
00034   } catch (...) {
00035     if (F != stdin) fclose(F);      // Make sure to close file descriptor if an
00036     throw;                          // exception is thrown
00037   }
00038 
00039   if (F != stdin)
00040     fclose(F);
00041 
00042   return Result;
00043 }
00044 
00045 Module *llvm::ParseAssemblyString(const char * AsmString, Module * M) {
00046   return RunVMAsmParser(AsmString, M);
00047 }
00048 
00049 
00050 //===------------------------------------------------------------------------===
00051 //                              ParseException Class
00052 //===------------------------------------------------------------------------===
00053 
00054 
00055 ParseException::ParseException(const std::string &filename,
00056                                const std::string &message,
00057                                int lineNo, int colNo)
00058   : Filename(filename), Message(message) {
00059   LineNo = lineNo; ColumnNo = colNo;
00060 }
00061 
00062 ParseException::ParseException(const ParseException &E)
00063   : Filename(E.Filename), Message(E.Message) {
00064   LineNo = E.LineNo;
00065   ColumnNo = E.ColumnNo;
00066 }
00067 
00068 // Includes info from options
00069 const std::string ParseException::getMessage() const {
00070   std::string Result;
00071   char Buffer[10];
00072 
00073   if (Filename == "-")
00074     Result += "<stdin>";
00075   else
00076     Result += Filename;
00077 
00078   if (LineNo != -1) {
00079     sprintf(Buffer, "%d", LineNo);
00080     Result += std::string(":") + Buffer;
00081     if (ColumnNo != -1) {
00082       sprintf(Buffer, "%d", ColumnNo);
00083       Result += std::string(",") + Buffer;
00084     }
00085   }
00086 
00087   return Result + ": " + Message;
00088 }