1 //===- Error.cpp - tblgen error handling helper routines --------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains error handling helper routines to pretty-print diagnostic
11 // messages from tblgen.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "llvm/TableGen/Error.h"
16 #include "llvm/ADT/Twine.h"
17 #include "llvm/Support/raw_ostream.h"
18 #include <cstdlib>
19
20 namespace llvm {
21
22 SourceMgr SrcMgr;
23
PrintMessage(ArrayRef<SMLoc> Loc,SourceMgr::DiagKind Kind,const Twine & Msg)24 static void PrintMessage(ArrayRef<SMLoc> Loc, SourceMgr::DiagKind Kind,
25 const Twine &Msg) {
26 SMLoc NullLoc;
27 if (Loc.empty())
28 Loc = NullLoc;
29 SrcMgr.PrintMessage(Loc.front(), Kind, Msg);
30 for (unsigned i = 1; i < Loc.size(); ++i)
31 SrcMgr.PrintMessage(Loc[i], SourceMgr::DK_Note,
32 "instantiated from multiclass");
33 }
34
PrintWarning(ArrayRef<SMLoc> WarningLoc,const Twine & Msg)35 void PrintWarning(ArrayRef<SMLoc> WarningLoc, const Twine &Msg) {
36 PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg);
37 }
38
PrintWarning(const char * Loc,const Twine & Msg)39 void PrintWarning(const char *Loc, const Twine &Msg) {
40 SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Warning, Msg);
41 }
42
PrintWarning(const Twine & Msg)43 void PrintWarning(const Twine &Msg) {
44 errs() << "warning:" << Msg << "\n";
45 }
46
PrintError(ArrayRef<SMLoc> ErrorLoc,const Twine & Msg)47 void PrintError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) {
48 PrintMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
49 }
50
PrintError(const char * Loc,const Twine & Msg)51 void PrintError(const char *Loc, const Twine &Msg) {
52 SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
53 }
54
PrintError(const Twine & Msg)55 void PrintError(const Twine &Msg) {
56 errs() << "error:" << Msg << "\n";
57 }
58
PrintFatalError(const std::string & Msg)59 void PrintFatalError(const std::string &Msg) {
60 PrintError(Twine(Msg));
61 std::exit(1);
62 }
63
PrintFatalError(ArrayRef<SMLoc> ErrorLoc,const std::string & Msg)64 void PrintFatalError(ArrayRef<SMLoc> ErrorLoc, const std::string &Msg) {
65 PrintError(ErrorLoc, Msg);
66 std::exit(1);
67 }
68
69 } // end namespace llvm
70