ad98fba9baf6c2991138c303a868872157fed9cb
[oota-llvm.git] / lib / TableGen / Error.cpp
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
19 #include <cstdlib>
20
21 namespace llvm {
22
23 SourceMgr SrcMgr;
24
25 static void PrintMessage(ArrayRef<SMLoc> Loc, SourceMgr::DiagKind Kind,
26                          const Twine &Msg) {
27   SMLoc NullLoc;
28   if (Loc.empty())
29     Loc = NullLoc;
30   SrcMgr.PrintMessage(Loc.front(), Kind, Msg);
31   for (unsigned i = 1; i < Loc.size(); ++i)
32     SrcMgr.PrintMessage(Loc[i], SourceMgr::DK_Note,
33                         "instantiated from multiclass");
34 }
35
36 void PrintWarning(ArrayRef<SMLoc> WarningLoc, const Twine &Msg) {
37   PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg);
38 }
39
40 void PrintWarning(const char *Loc, const Twine &Msg) {
41   SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Warning, Msg);
42 }
43
44 void PrintWarning(const Twine &Msg) {
45   errs() << "warning:" << Msg << "\n";
46 }
47
48 void PrintWarning(const TGError &Warning) {
49   PrintWarning(Warning.getLoc(), Warning.getMessage());
50 }
51
52 void PrintError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) {
53   PrintMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
54 }
55
56 void PrintError(const char *Loc, const Twine &Msg) {
57   SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
58 }
59
60 void PrintError(const Twine &Msg) {
61   errs() << "error:" << Msg << "\n";
62 }
63
64 void PrintError(const TGError &Error) {
65   PrintError(Error.getLoc(), Error.getMessage());
66 }
67
68 void PrintFatalError(const std::string &Msg) {
69   PrintError(Twine(Msg));
70   std::exit(1);
71 }
72
73 void PrintFatalError(ArrayRef<SMLoc> ErrorLoc, const std::string &Msg) {
74   PrintError(ErrorLoc, Msg);
75   std::exit(1);
76 }
77
78 } // end namespace llvm