Jim Grosbach | 797cff0 | 2011-06-21 22:55:50 +0000 | [diff] [blame] | 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 | |
Peter Collingbourne | 84c287e | 2011-10-01 16:41:13 +0000 | [diff] [blame] | 15 | #include "llvm/TableGen/Error.h" |
Jim Grosbach | 797cff0 | 2011-06-21 22:55:50 +0000 | [diff] [blame] | 16 | #include "llvm/ADT/Twine.h" |
James Y Knight | e452e27 | 2015-05-11 22:17:13 +0000 | [diff] [blame] | 17 | #include "llvm/Support/Signals.h" |
Jim Grosbach | 797cff0 | 2011-06-21 22:55:50 +0000 | [diff] [blame] | 18 | #include "llvm/Support/raw_ostream.h" |
Joerg Sonnenberger | 356f797 | 2012-10-25 16:35:18 +0000 | [diff] [blame] | 19 | #include <cstdlib> |
| 20 | |
Jim Grosbach | 797cff0 | 2011-06-21 22:55:50 +0000 | [diff] [blame] | 21 | namespace llvm { |
| 22 | |
| 23 | SourceMgr SrcMgr; |
Jakob Stoklund Olesen | a1214e7 | 2013-03-20 20:43:11 +0000 | [diff] [blame] | 24 | unsigned ErrorsPrinted = 0; |
Jim Grosbach | 797cff0 | 2011-06-21 22:55:50 +0000 | [diff] [blame] | 25 | |
Jakob Stoklund Olesen | d7b6696 | 2012-08-22 23:33:58 +0000 | [diff] [blame] | 26 | static void PrintMessage(ArrayRef<SMLoc> Loc, SourceMgr::DiagKind Kind, |
| 27 | const Twine &Msg) { |
Jakob Stoklund Olesen | a1214e7 | 2013-03-20 20:43:11 +0000 | [diff] [blame] | 28 | // 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 Olesen | d7b6696 | 2012-08-22 23:33:58 +0000 | [diff] [blame] | 33 | 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 | |
| 42 | void PrintWarning(ArrayRef<SMLoc> WarningLoc, const Twine &Msg) { |
| 43 | PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg); |
Jim Grosbach | cc6cbf0 | 2012-04-18 17:46:31 +0000 | [diff] [blame] | 44 | } |
| 45 | |
| 46 | void PrintWarning(const char *Loc, const Twine &Msg) { |
| 47 | SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Warning, Msg); |
| 48 | } |
| 49 | |
| 50 | void PrintWarning(const Twine &Msg) { |
Jim Grosbach | f30541e | 2012-04-18 18:09:53 +0000 | [diff] [blame] | 51 | errs() << "warning:" << Msg << "\n"; |
Jim Grosbach | cc6cbf0 | 2012-04-18 17:46:31 +0000 | [diff] [blame] | 52 | } |
| 53 | |
Jakob Stoklund Olesen | d7b6696 | 2012-08-22 23:33:58 +0000 | [diff] [blame] | 54 | void PrintError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) { |
| 55 | PrintMessage(ErrorLoc, SourceMgr::DK_Error, Msg); |
Jim Grosbach | 797cff0 | 2011-06-21 22:55:50 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | void PrintError(const char *Loc, const Twine &Msg) { |
Chris Lattner | 03b80a4 | 2011-10-16 05:43:57 +0000 | [diff] [blame] | 59 | SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg); |
Jim Grosbach | 797cff0 | 2011-06-21 22:55:50 +0000 | [diff] [blame] | 60 | } |
| 61 | |
| 62 | void PrintError(const Twine &Msg) { |
| 63 | errs() << "error:" << Msg << "\n"; |
| 64 | } |
| 65 | |
Benjamin Kramer | 48e7e85 | 2014-03-29 17:17:15 +0000 | [diff] [blame] | 66 | void PrintFatalError(const Twine &Msg) { |
| 67 | PrintError(Msg); |
James Y Knight | e452e27 | 2015-05-11 22:17:13 +0000 | [diff] [blame] | 68 | // The following call runs the file cleanup handlers. |
| 69 | sys::RunInterruptHandlers(); |
Joerg Sonnenberger | 356f797 | 2012-10-25 16:35:18 +0000 | [diff] [blame] | 70 | std::exit(1); |
| 71 | } |
| 72 | |
Benjamin Kramer | 48e7e85 | 2014-03-29 17:17:15 +0000 | [diff] [blame] | 73 | void PrintFatalError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) { |
Joerg Sonnenberger | 356f797 | 2012-10-25 16:35:18 +0000 | [diff] [blame] | 74 | PrintError(ErrorLoc, Msg); |
James Y Knight | e452e27 | 2015-05-11 22:17:13 +0000 | [diff] [blame] | 75 | // The following call runs the file cleanup handlers. |
| 76 | sys::RunInterruptHandlers(); |
Joerg Sonnenberger | 356f797 | 2012-10-25 16:35:18 +0000 | [diff] [blame] | 77 | std::exit(1); |
| 78 | } |
| 79 | |
Jim Grosbach | 797cff0 | 2011-06-21 22:55:50 +0000 | [diff] [blame] | 80 | } // end namespace llvm |