blob: 928b1203cd8f108ca0b4b180f56bf1f2fdebc7b9 [file] [log] [blame]
Jim Grosbach0b6a44a2011-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 Collingbourne7c788882011-10-01 16:41:13 +000015#include "llvm/TableGen/Error.h"
Jim Grosbach0b6a44a2011-06-21 22:55:50 +000016#include "llvm/ADT/Twine.h"
17#include "llvm/Support/raw_ostream.h"
Joerg Sonnenberger0eafc7f2012-10-25 16:35:18 +000018#include <cstdlib>
19
Jim Grosbach0b6a44a2011-06-21 22:55:50 +000020namespace llvm {
21
22SourceMgr SrcMgr;
Jakob Stoklund Olesenf8ea5a52013-03-20 20:43:11 +000023unsigned ErrorsPrinted = 0;
Jim Grosbach0b6a44a2011-06-21 22:55:50 +000024
Jakob Stoklund Olesen376a8a72012-08-22 23:33:58 +000025static void PrintMessage(ArrayRef<SMLoc> Loc, SourceMgr::DiagKind Kind,
26 const Twine &Msg) {
Jakob Stoklund Olesenf8ea5a52013-03-20 20:43:11 +000027 // Count the total number of errors printed.
28 // This is used to exit with an error code if there were any errors.
29 if (Kind == SourceMgr::DK_Error)
30 ++ErrorsPrinted;
31
Jakob Stoklund Olesen376a8a72012-08-22 23:33:58 +000032 SMLoc NullLoc;
33 if (Loc.empty())
34 Loc = NullLoc;
35 SrcMgr.PrintMessage(Loc.front(), Kind, Msg);
36 for (unsigned i = 1; i < Loc.size(); ++i)
37 SrcMgr.PrintMessage(Loc[i], SourceMgr::DK_Note,
38 "instantiated from multiclass");
39}
40
41void PrintWarning(ArrayRef<SMLoc> WarningLoc, const Twine &Msg) {
42 PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg);
Jim Grosbach97c02bf2012-04-18 17:46:31 +000043}
44
45void PrintWarning(const char *Loc, const Twine &Msg) {
46 SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Warning, Msg);
47}
48
49void PrintWarning(const Twine &Msg) {
Jim Grosbach28b810e2012-04-18 18:09:53 +000050 errs() << "warning:" << Msg << "\n";
Jim Grosbach97c02bf2012-04-18 17:46:31 +000051}
52
Jakob Stoklund Olesen376a8a72012-08-22 23:33:58 +000053void PrintError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) {
54 PrintMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
Jim Grosbach0b6a44a2011-06-21 22:55:50 +000055}
56
57void PrintError(const char *Loc, const Twine &Msg) {
Chris Lattner3f2d5f62011-10-16 05:43:57 +000058 SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
Jim Grosbach0b6a44a2011-06-21 22:55:50 +000059}
60
61void PrintError(const Twine &Msg) {
62 errs() << "error:" << Msg << "\n";
63}
64
Joerg Sonnenberger0eafc7f2012-10-25 16:35:18 +000065void PrintFatalError(const std::string &Msg) {
66 PrintError(Twine(Msg));
67 std::exit(1);
68}
69
70void PrintFatalError(ArrayRef<SMLoc> ErrorLoc, const std::string &Msg) {
71 PrintError(ErrorLoc, Msg);
72 std::exit(1);
73}
74
Jim Grosbach0b6a44a2011-06-21 22:55:50 +000075} // end namespace llvm