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/TableGen/Error.h" 15 #include "llvm/ADT/Twine.h" 16 #include "llvm/Support/Signals.h" 17 #include "llvm/Support/WithColor.h" 18 #include "llvm/Support/raw_ostream.h" 19 #include <cstdlib> 20 21 namespace llvm { 22 23 SourceMgr SrcMgr; 24 unsigned ErrorsPrinted = 0; 25 26 static void PrintMessage(ArrayRef<SMLoc> Loc, SourceMgr::DiagKind Kind, 27 const Twine &Msg) { 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 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 PrintNote(const Twine &Msg) { WithColor::note() << Msg << "\n"; } 43 44 void PrintNote(ArrayRef<SMLoc> NoteLoc, const Twine &Msg) { 45 PrintMessage(NoteLoc, SourceMgr::DK_Note, Msg); 46 } 47 48 void PrintFatalNote(ArrayRef<SMLoc> NoteLoc, const Twine &Msg) { 49 PrintNote(NoteLoc, Msg); 50 // The following call runs the file cleanup handlers. 51 sys::RunInterruptHandlers(); 52 std::exit(1); 53 } 54 55 void PrintWarning(ArrayRef<SMLoc> WarningLoc, const Twine &Msg) { 56 PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg); 57 } 58 59 void PrintWarning(const char *Loc, const Twine &Msg) { 60 SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Warning, Msg); 61 } 62 63 void PrintWarning(const Twine &Msg) { WithColor::warning() << Msg << "\n"; } 64 65 void PrintError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) { 66 PrintMessage(ErrorLoc, SourceMgr::DK_Error, Msg); 67 } 68 69 void PrintError(const char *Loc, const Twine &Msg) { 70 SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg); 71 } 72 73 void PrintError(const Twine &Msg) { WithColor::error() << Msg << "\n"; } 74 75 void PrintFatalError(const Twine &Msg) { 76 PrintError(Msg); 77 // The following call runs the file cleanup handlers. 78 sys::RunInterruptHandlers(); 79 std::exit(1); 80 } 81 82 void PrintFatalError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) { 83 PrintError(ErrorLoc, Msg); 84 // The following call runs the file cleanup handlers. 85 sys::RunInterruptHandlers(); 86 std::exit(1); 87 } 88 89 } // end namespace llvm 90