1 //===- Error.cpp - tblgen error handling helper routines --------*- C++ -*-===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // This file contains error handling helper routines to pretty-print diagnostic 10 // messages from tblgen. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "llvm/ADT/Twine.h" 15 #include "llvm/Support/raw_ostream.h" 16 #include "llvm/Support/Signals.h" 17 #include "llvm/Support/WithColor.h" 18 #include "llvm/TableGen/Error.h" 19 #include "llvm/TableGen/Record.h" 20 #include <cstdlib> 21 22 namespace llvm { 23 24 SourceMgr SrcMgr; 25 unsigned ErrorsPrinted = 0; 26 27 static void PrintMessage(ArrayRef<SMLoc> Loc, SourceMgr::DiagKind Kind, 28 const Twine &Msg) { 29 // Count the total number of errors printed. 30 // This is used to exit with an error code if there were any errors. 31 if (Kind == SourceMgr::DK_Error) 32 ++ErrorsPrinted; 33 34 SMLoc NullLoc; 35 if (Loc.empty()) 36 Loc = NullLoc; 37 SrcMgr.PrintMessage(Loc.front(), Kind, Msg); 38 for (unsigned i = 1; i < Loc.size(); ++i) 39 SrcMgr.PrintMessage(Loc[i], SourceMgr::DK_Note, 40 "instantiated from multiclass"); 41 } 42 43 void PrintNote(const Twine &Msg) { WithColor::note() << Msg << "\n"; } 44 45 void PrintNote(ArrayRef<SMLoc> NoteLoc, const Twine &Msg) { 46 PrintMessage(NoteLoc, SourceMgr::DK_Note, Msg); 47 } 48 49 void PrintFatalNote(ArrayRef<SMLoc> NoteLoc, const Twine &Msg) { 50 PrintNote(NoteLoc, Msg); 51 // The following call runs the file cleanup handlers. 52 sys::RunInterruptHandlers(); 53 std::exit(1); 54 } 55 56 void PrintWarning(ArrayRef<SMLoc> WarningLoc, const Twine &Msg) { 57 PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg); 58 } 59 60 void PrintWarning(const char *Loc, const Twine &Msg) { 61 SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Warning, Msg); 62 } 63 64 void PrintWarning(const Twine &Msg) { WithColor::warning() << Msg << "\n"; } 65 66 void PrintError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) { 67 PrintMessage(ErrorLoc, SourceMgr::DK_Error, Msg); 68 } 69 70 void PrintError(const char *Loc, const Twine &Msg) { 71 SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg); 72 } 73 74 void PrintError(const Twine &Msg) { WithColor::error() << Msg << "\n"; } 75 76 void PrintFatalError(const Twine &Msg) { 77 PrintError(Msg); 78 // The following call runs the file cleanup handlers. 79 sys::RunInterruptHandlers(); 80 std::exit(1); 81 } 82 83 void PrintFatalError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) { 84 PrintError(ErrorLoc, Msg); 85 // The following call runs the file cleanup handlers. 86 sys::RunInterruptHandlers(); 87 std::exit(1); 88 } 89 90 // This method takes a Record and uses the source location 91 // stored in it. 92 void PrintFatalError(const Record *Rec, const Twine &Msg) { 93 PrintError(Rec->getLoc(), Msg); 94 // The following call runs the file cleanup handlers. 95 sys::RunInterruptHandlers(); 96 std::exit(1); 97 } 98 99 // This method takes a RecordVal and uses the source location 100 // stored in it. 101 void PrintFatalError(const RecordVal *RecVal, const Twine &Msg) { 102 PrintError(RecVal->getLoc(), Msg); 103 // The following call runs the file cleanup handlers. 104 sys::RunInterruptHandlers(); 105 std::exit(1); 106 } 107 108 } // end namespace llvm 109