LLVM API Documentation

TableGen/Error.cpp
Go to the documentation of this file.
00001 //===- Error.cpp - tblgen error handling helper routines --------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file contains error handling helper routines to pretty-print diagnostic
00011 // messages from tblgen.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #include "llvm/TableGen/Error.h"
00016 #include "llvm/ADT/Twine.h"
00017 #include "llvm/Support/raw_ostream.h"
00018 #include <cstdlib>
00019 
00020 namespace llvm {
00021 
00022 SourceMgr SrcMgr;
00023 unsigned ErrorsPrinted = 0;
00024 
00025 static void PrintMessage(ArrayRef<SMLoc> Loc, SourceMgr::DiagKind Kind,
00026                          const Twine &Msg) {
00027   // Count the total number of errors printed.
00028   // This is used to exit with an error code if there were any errors.
00029   if (Kind == SourceMgr::DK_Error)
00030     ++ErrorsPrinted;
00031 
00032   SMLoc NullLoc;
00033   if (Loc.empty())
00034     Loc = NullLoc;
00035   SrcMgr.PrintMessage(Loc.front(), Kind, Msg);
00036   for (unsigned i = 1; i < Loc.size(); ++i)
00037     SrcMgr.PrintMessage(Loc[i], SourceMgr::DK_Note,
00038                         "instantiated from multiclass");
00039 }
00040 
00041 void PrintWarning(ArrayRef<SMLoc> WarningLoc, const Twine &Msg) {
00042   PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg);
00043 }
00044 
00045 void PrintWarning(const char *Loc, const Twine &Msg) {
00046   SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Warning, Msg);
00047 }
00048 
00049 void PrintWarning(const Twine &Msg) {
00050   errs() << "warning:" << Msg << "\n";
00051 }
00052 
00053 void PrintError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) {
00054   PrintMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
00055 }
00056 
00057 void PrintError(const char *Loc, const Twine &Msg) {
00058   SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
00059 }
00060 
00061 void PrintError(const Twine &Msg) {
00062   errs() << "error:" << Msg << "\n";
00063 }
00064 
00065 void PrintFatalError(const Twine &Msg) {
00066   PrintError(Msg);
00067   std::exit(1);
00068 }
00069 
00070 void PrintFatalError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) {
00071   PrintError(ErrorLoc, Msg);
00072   std::exit(1);
00073 }
00074 
00075 } // end namespace llvm