10b57cec5SDimitry Andric //===- Error.cpp - tblgen error handling helper routines --------*- C++ -*-===//
20b57cec5SDimitry Andric //
30b57cec5SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
40b57cec5SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
50b57cec5SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
60b57cec5SDimitry Andric //
70b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
80b57cec5SDimitry Andric //
90b57cec5SDimitry Andric // This file contains error handling helper routines to pretty-print diagnostic
100b57cec5SDimitry Andric // messages from tblgen.
110b57cec5SDimitry Andric //
120b57cec5SDimitry Andric //===----------------------------------------------------------------------===//
130b57cec5SDimitry Andric
140b57cec5SDimitry Andric #include "llvm/ADT/Twine.h"
15e8d8bef9SDimitry Andric #include "llvm/Support/raw_ostream.h"
160b57cec5SDimitry Andric #include "llvm/Support/Signals.h"
170b57cec5SDimitry Andric #include "llvm/Support/WithColor.h"
18e8d8bef9SDimitry Andric #include "llvm/TableGen/Error.h"
19e8d8bef9SDimitry Andric #include "llvm/TableGen/Record.h"
200b57cec5SDimitry Andric #include <cstdlib>
210b57cec5SDimitry Andric
220b57cec5SDimitry Andric namespace llvm {
230b57cec5SDimitry Andric
240b57cec5SDimitry Andric SourceMgr SrcMgr;
250b57cec5SDimitry Andric unsigned ErrorsPrinted = 0;
260b57cec5SDimitry Andric
PrintMessage(ArrayRef<SMLoc> Loc,SourceMgr::DiagKind Kind,const Twine & Msg)270b57cec5SDimitry Andric static void PrintMessage(ArrayRef<SMLoc> Loc, SourceMgr::DiagKind Kind,
280b57cec5SDimitry Andric const Twine &Msg) {
290b57cec5SDimitry Andric // Count the total number of errors printed.
300b57cec5SDimitry Andric // This is used to exit with an error code if there were any errors.
310b57cec5SDimitry Andric if (Kind == SourceMgr::DK_Error)
320b57cec5SDimitry Andric ++ErrorsPrinted;
330b57cec5SDimitry Andric
340b57cec5SDimitry Andric SMLoc NullLoc;
350b57cec5SDimitry Andric if (Loc.empty())
360b57cec5SDimitry Andric Loc = NullLoc;
370b57cec5SDimitry Andric SrcMgr.PrintMessage(Loc.front(), Kind, Msg);
380b57cec5SDimitry Andric for (unsigned i = 1; i < Loc.size(); ++i)
390b57cec5SDimitry Andric SrcMgr.PrintMessage(Loc[i], SourceMgr::DK_Note,
400b57cec5SDimitry Andric "instantiated from multiclass");
410b57cec5SDimitry Andric }
420b57cec5SDimitry Andric
43e8d8bef9SDimitry Andric // Functions to print notes.
44e8d8bef9SDimitry Andric
PrintNote(const Twine & Msg)45e8d8bef9SDimitry Andric void PrintNote(const Twine &Msg) {
46e8d8bef9SDimitry Andric WithColor::note() << Msg << "\n";
47e8d8bef9SDimitry Andric }
488bcb0991SDimitry Andric
PrintNote(ArrayRef<SMLoc> NoteLoc,const Twine & Msg)490b57cec5SDimitry Andric void PrintNote(ArrayRef<SMLoc> NoteLoc, const Twine &Msg) {
500b57cec5SDimitry Andric PrintMessage(NoteLoc, SourceMgr::DK_Note, Msg);
510b57cec5SDimitry Andric }
520b57cec5SDimitry Andric
53e8d8bef9SDimitry Andric // Functions to print fatal notes.
54e8d8bef9SDimitry Andric
PrintFatalNote(const Twine & Msg)55e8d8bef9SDimitry Andric void PrintFatalNote(const Twine &Msg) {
56e8d8bef9SDimitry Andric PrintNote(Msg);
57e8d8bef9SDimitry Andric // The following call runs the file cleanup handlers.
58e8d8bef9SDimitry Andric sys::RunInterruptHandlers();
59e8d8bef9SDimitry Andric std::exit(1);
60e8d8bef9SDimitry Andric }
61e8d8bef9SDimitry Andric
PrintFatalNote(ArrayRef<SMLoc> NoteLoc,const Twine & Msg)62e8d8bef9SDimitry Andric void PrintFatalNote(ArrayRef<SMLoc> NoteLoc, const Twine &Msg) {
63e8d8bef9SDimitry Andric PrintNote(NoteLoc, Msg);
64e8d8bef9SDimitry Andric // The following call runs the file cleanup handlers.
65e8d8bef9SDimitry Andric sys::RunInterruptHandlers();
66e8d8bef9SDimitry Andric std::exit(1);
67e8d8bef9SDimitry Andric }
68e8d8bef9SDimitry Andric
69e8d8bef9SDimitry Andric // This method takes a Record and uses the source location
70e8d8bef9SDimitry Andric // stored in it.
PrintFatalNote(const Record * Rec,const Twine & Msg)71e8d8bef9SDimitry Andric void PrintFatalNote(const Record *Rec, const Twine &Msg) {
72e8d8bef9SDimitry Andric PrintNote(Rec->getLoc(), Msg);
73e8d8bef9SDimitry Andric // The following call runs the file cleanup handlers.
74e8d8bef9SDimitry Andric sys::RunInterruptHandlers();
75e8d8bef9SDimitry Andric std::exit(1);
76e8d8bef9SDimitry Andric }
77e8d8bef9SDimitry Andric
78e8d8bef9SDimitry Andric // This method takes a RecordVal and uses the source location
79e8d8bef9SDimitry Andric // stored in it.
PrintFatalNote(const RecordVal * RecVal,const Twine & Msg)80e8d8bef9SDimitry Andric void PrintFatalNote(const RecordVal *RecVal, const Twine &Msg) {
81e8d8bef9SDimitry Andric PrintNote(RecVal->getLoc(), Msg);
82e8d8bef9SDimitry Andric // The following call runs the file cleanup handlers.
83e8d8bef9SDimitry Andric sys::RunInterruptHandlers();
84e8d8bef9SDimitry Andric std::exit(1);
85e8d8bef9SDimitry Andric }
86e8d8bef9SDimitry Andric
87e8d8bef9SDimitry Andric // Functions to print warnings.
88e8d8bef9SDimitry Andric
PrintWarning(const Twine & Msg)89e8d8bef9SDimitry Andric void PrintWarning(const Twine &Msg) { WithColor::warning() << Msg << "\n"; }
90e8d8bef9SDimitry Andric
PrintWarning(ArrayRef<SMLoc> WarningLoc,const Twine & Msg)910b57cec5SDimitry Andric void PrintWarning(ArrayRef<SMLoc> WarningLoc, const Twine &Msg) {
920b57cec5SDimitry Andric PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg);
930b57cec5SDimitry Andric }
940b57cec5SDimitry Andric
PrintWarning(const char * Loc,const Twine & Msg)950b57cec5SDimitry Andric void PrintWarning(const char *Loc, const Twine &Msg) {
960b57cec5SDimitry Andric SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Warning, Msg);
970b57cec5SDimitry Andric }
980b57cec5SDimitry Andric
99e8d8bef9SDimitry Andric // Functions to print errors.
100e8d8bef9SDimitry Andric
PrintError(const Twine & Msg)101e8d8bef9SDimitry Andric void PrintError(const Twine &Msg) { WithColor::error() << Msg << "\n"; }
1020b57cec5SDimitry Andric
PrintError(ArrayRef<SMLoc> ErrorLoc,const Twine & Msg)1030b57cec5SDimitry Andric void PrintError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) {
1040b57cec5SDimitry Andric PrintMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
1050b57cec5SDimitry Andric }
1060b57cec5SDimitry Andric
PrintError(const char * Loc,const Twine & Msg)1070b57cec5SDimitry Andric void PrintError(const char *Loc, const Twine &Msg) {
1080b57cec5SDimitry Andric SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
1090b57cec5SDimitry Andric }
1100b57cec5SDimitry Andric
111e8d8bef9SDimitry Andric // This method takes a Record and uses the source location
112e8d8bef9SDimitry Andric // stored in it.
PrintError(const Record * Rec,const Twine & Msg)113e8d8bef9SDimitry Andric void PrintError(const Record *Rec, const Twine &Msg) {
114e8d8bef9SDimitry Andric PrintMessage(Rec->getLoc(), SourceMgr::DK_Error, Msg);
115e8d8bef9SDimitry Andric }
116e8d8bef9SDimitry Andric
117e8d8bef9SDimitry Andric // This method takes a RecordVal and uses the source location
118e8d8bef9SDimitry Andric // stored in it.
PrintError(const RecordVal * RecVal,const Twine & Msg)119e8d8bef9SDimitry Andric void PrintError(const RecordVal *RecVal, const Twine &Msg) {
120e8d8bef9SDimitry Andric PrintMessage(RecVal->getLoc(), SourceMgr::DK_Error, Msg);
121e8d8bef9SDimitry Andric }
122e8d8bef9SDimitry Andric
123e8d8bef9SDimitry Andric // Functions to print fatal errors.
1240b57cec5SDimitry Andric
PrintFatalError(const Twine & Msg)1250b57cec5SDimitry Andric void PrintFatalError(const Twine &Msg) {
1260b57cec5SDimitry Andric PrintError(Msg);
1270b57cec5SDimitry Andric // The following call runs the file cleanup handlers.
1280b57cec5SDimitry Andric sys::RunInterruptHandlers();
1290b57cec5SDimitry Andric std::exit(1);
1300b57cec5SDimitry Andric }
1310b57cec5SDimitry Andric
PrintFatalError(ArrayRef<SMLoc> ErrorLoc,const Twine & Msg)1320b57cec5SDimitry Andric void PrintFatalError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) {
1330b57cec5SDimitry Andric PrintError(ErrorLoc, Msg);
1340b57cec5SDimitry Andric // The following call runs the file cleanup handlers.
1350b57cec5SDimitry Andric sys::RunInterruptHandlers();
1360b57cec5SDimitry Andric std::exit(1);
1370b57cec5SDimitry Andric }
1380b57cec5SDimitry Andric
139e8d8bef9SDimitry Andric // This method takes a Record and uses the source location
140e8d8bef9SDimitry Andric // stored in it.
PrintFatalError(const Record * Rec,const Twine & Msg)141e8d8bef9SDimitry Andric void PrintFatalError(const Record *Rec, const Twine &Msg) {
142e8d8bef9SDimitry Andric PrintError(Rec->getLoc(), Msg);
143e8d8bef9SDimitry Andric // The following call runs the file cleanup handlers.
144e8d8bef9SDimitry Andric sys::RunInterruptHandlers();
145e8d8bef9SDimitry Andric std::exit(1);
146e8d8bef9SDimitry Andric }
147e8d8bef9SDimitry Andric
148e8d8bef9SDimitry Andric // This method takes a RecordVal and uses the source location
149e8d8bef9SDimitry Andric // stored in it.
PrintFatalError(const RecordVal * RecVal,const Twine & Msg)150e8d8bef9SDimitry Andric void PrintFatalError(const RecordVal *RecVal, const Twine &Msg) {
151e8d8bef9SDimitry Andric PrintError(RecVal->getLoc(), Msg);
152e8d8bef9SDimitry Andric // The following call runs the file cleanup handlers.
153e8d8bef9SDimitry Andric sys::RunInterruptHandlers();
154e8d8bef9SDimitry Andric std::exit(1);
155e8d8bef9SDimitry Andric }
156e8d8bef9SDimitry Andric
157fe6060f1SDimitry Andric // Check an assertion: Obtain the condition value and be sure it is true.
158fe6060f1SDimitry Andric // If not, print a nonfatal error along with the message.
CheckAssert(SMLoc Loc,Init * Condition,Init * Message)159fe6060f1SDimitry Andric void CheckAssert(SMLoc Loc, Init *Condition, Init *Message) {
16081ad6265SDimitry Andric auto *CondValue = dyn_cast_or_null<IntInit>(Condition->convertInitializerTo(
16181ad6265SDimitry Andric IntRecTy::get(Condition->getRecordKeeper())));
162fe6060f1SDimitry Andric if (!CondValue)
163fe6060f1SDimitry Andric PrintError(Loc, "assert condition must of type bit, bits, or int.");
164fe6060f1SDimitry Andric else if (!CondValue->getValue()) {
165fe6060f1SDimitry Andric PrintError(Loc, "assertion failed");
166fe6060f1SDimitry Andric if (auto *MessageInit = dyn_cast<StringInit>(Message))
167fe6060f1SDimitry Andric PrintNote(MessageInit->getValue());
168fe6060f1SDimitry Andric else
169fe6060f1SDimitry Andric PrintNote("(assert message is not a string)");
170fe6060f1SDimitry Andric }
171fe6060f1SDimitry Andric }
172fe6060f1SDimitry Andric
173*5f757f3fSDimitry Andric // Dump a message to stderr.
dumpMessage(SMLoc Loc,Init * Message)174*5f757f3fSDimitry Andric void dumpMessage(SMLoc Loc, Init *Message) {
175*5f757f3fSDimitry Andric auto *MessageInit = dyn_cast<StringInit>(Message);
176*5f757f3fSDimitry Andric assert(MessageInit && "no debug message to print");
177*5f757f3fSDimitry Andric PrintNote(Loc, MessageInit->getValue());
178*5f757f3fSDimitry Andric }
179*5f757f3fSDimitry Andric
1800b57cec5SDimitry Andric } // end namespace llvm
181