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