blob: fd089356625404acc667f73957927be08be87699 [file] [log] [blame]
Jim Grosbach797cff02011-06-21 22:55:50 +00001//===- 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
Peter Collingbourne84c287e2011-10-01 16:41:13 +000015#include "llvm/TableGen/Error.h"
Jim Grosbach797cff02011-06-21 22:55:50 +000016#include "llvm/ADT/Twine.h"
James Y Knighte452e272015-05-11 22:17:13 +000017#include "llvm/Support/Signals.h"
Jim Grosbach797cff02011-06-21 22:55:50 +000018#include "llvm/Support/raw_ostream.h"
Joerg Sonnenberger356f7972012-10-25 16:35:18 +000019#include <cstdlib>
20
Jim Grosbach797cff02011-06-21 22:55:50 +000021namespace llvm {
22
23SourceMgr SrcMgr;
Jakob Stoklund Olesena1214e72013-03-20 20:43:11 +000024unsigned ErrorsPrinted = 0;
Jim Grosbach797cff02011-06-21 22:55:50 +000025
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +000026static void PrintMessage(ArrayRef<SMLoc> Loc, SourceMgr::DiagKind Kind,
27 const Twine &Msg) {
Jakob Stoklund Olesena1214e72013-03-20 20:43:11 +000028 // Count the total number of errors printed.
29 // This is used to exit with an error code if there were any errors.
30 if (Kind == SourceMgr::DK_Error)
31 ++ErrorsPrinted;
32
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +000033 SMLoc NullLoc;
34 if (Loc.empty())
35 Loc = NullLoc;
36 SrcMgr.PrintMessage(Loc.front(), Kind, Msg);
37 for (unsigned i = 1; i < Loc.size(); ++i)
38 SrcMgr.PrintMessage(Loc[i], SourceMgr::DK_Note,
39 "instantiated from multiclass");
40}
41
42void PrintWarning(ArrayRef<SMLoc> WarningLoc, const Twine &Msg) {
43 PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg);
Jim Grosbachcc6cbf02012-04-18 17:46:31 +000044}
45
46void PrintWarning(const char *Loc, const Twine &Msg) {
47 SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Warning, Msg);
48}
49
50void PrintWarning(const Twine &Msg) {
Jim Grosbachf30541e2012-04-18 18:09:53 +000051 errs() << "warning:" << Msg << "\n";
Jim Grosbachcc6cbf02012-04-18 17:46:31 +000052}
53
Jakob Stoklund Olesend7b66962012-08-22 23:33:58 +000054void PrintError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) {
55 PrintMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
Jim Grosbach797cff02011-06-21 22:55:50 +000056}
57
58void PrintError(const char *Loc, const Twine &Msg) {
Chris Lattner03b80a42011-10-16 05:43:57 +000059 SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
Jim Grosbach797cff02011-06-21 22:55:50 +000060}
61
62void PrintError(const Twine &Msg) {
63 errs() << "error:" << Msg << "\n";
64}
65
Benjamin Kramer48e7e852014-03-29 17:17:15 +000066void PrintFatalError(const Twine &Msg) {
67 PrintError(Msg);
James Y Knighte452e272015-05-11 22:17:13 +000068 // The following call runs the file cleanup handlers.
69 sys::RunInterruptHandlers();
Joerg Sonnenberger356f7972012-10-25 16:35:18 +000070 std::exit(1);
71}
72
Benjamin Kramer48e7e852014-03-29 17:17:15 +000073void PrintFatalError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) {
Joerg Sonnenberger356f7972012-10-25 16:35:18 +000074 PrintError(ErrorLoc, Msg);
James Y Knighte452e272015-05-11 22:17:13 +000075 // The following call runs the file cleanup handlers.
76 sys::RunInterruptHandlers();
Joerg Sonnenberger356f7972012-10-25 16:35:18 +000077 std::exit(1);
78}
79
Jim Grosbach797cff02011-06-21 22:55:50 +000080} // end namespace llvm