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 428bcb0991SDimitry Andric void PrintNote(const Twine &Msg) { WithColor::note() << Msg << "\n"; } 438bcb0991SDimitry Andric 44*0b57cec5SDimitry Andric void PrintNote(ArrayRef<SMLoc> NoteLoc, const Twine &Msg) { 45*0b57cec5SDimitry Andric PrintMessage(NoteLoc, SourceMgr::DK_Note, Msg); 46*0b57cec5SDimitry Andric } 47*0b57cec5SDimitry Andric 48*0b57cec5SDimitry Andric void PrintWarning(ArrayRef<SMLoc> WarningLoc, const Twine &Msg) { 49*0b57cec5SDimitry Andric PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg); 50*0b57cec5SDimitry Andric } 51*0b57cec5SDimitry Andric 52*0b57cec5SDimitry Andric void PrintWarning(const char *Loc, const Twine &Msg) { 53*0b57cec5SDimitry Andric SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Warning, Msg); 54*0b57cec5SDimitry Andric } 55*0b57cec5SDimitry Andric 56*0b57cec5SDimitry Andric void PrintWarning(const Twine &Msg) { WithColor::warning() << Msg << "\n"; } 57*0b57cec5SDimitry Andric 58*0b57cec5SDimitry Andric void PrintError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) { 59*0b57cec5SDimitry Andric PrintMessage(ErrorLoc, SourceMgr::DK_Error, Msg); 60*0b57cec5SDimitry Andric } 61*0b57cec5SDimitry Andric 62*0b57cec5SDimitry Andric void PrintError(const char *Loc, const Twine &Msg) { 63*0b57cec5SDimitry Andric SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg); 64*0b57cec5SDimitry Andric } 65*0b57cec5SDimitry Andric 66*0b57cec5SDimitry Andric void PrintError(const Twine &Msg) { WithColor::error() << Msg << "\n"; } 67*0b57cec5SDimitry Andric 68*0b57cec5SDimitry Andric void PrintFatalError(const Twine &Msg) { 69*0b57cec5SDimitry Andric PrintError(Msg); 70*0b57cec5SDimitry Andric // The following call runs the file cleanup handlers. 71*0b57cec5SDimitry Andric sys::RunInterruptHandlers(); 72*0b57cec5SDimitry Andric std::exit(1); 73*0b57cec5SDimitry Andric } 74*0b57cec5SDimitry Andric 75*0b57cec5SDimitry Andric void PrintFatalError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) { 76*0b57cec5SDimitry Andric PrintError(ErrorLoc, Msg); 77*0b57cec5SDimitry Andric // The following call runs the file cleanup handlers. 78*0b57cec5SDimitry Andric sys::RunInterruptHandlers(); 79*0b57cec5SDimitry Andric std::exit(1); 80*0b57cec5SDimitry Andric } 81*0b57cec5SDimitry Andric 82*0b57cec5SDimitry Andric } // end namespace llvm 83