xref: /src/contrib/llvm-project/llvm/lib/TableGen/Error.cpp (revision 5f757f3ff9144b609b3c433dfd370cc6bdc191ad)
130815c53SDimitry Andric //===- Error.cpp - tblgen error handling helper routines --------*- C++ -*-===//
230815c53SDimitry Andric //
3e6d15924SDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4e6d15924SDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5e6d15924SDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
630815c53SDimitry Andric //
730815c53SDimitry Andric //===----------------------------------------------------------------------===//
830815c53SDimitry Andric //
930815c53SDimitry Andric // This file contains error handling helper routines to pretty-print diagnostic
1030815c53SDimitry Andric // messages from tblgen.
1130815c53SDimitry Andric //
1230815c53SDimitry Andric //===----------------------------------------------------------------------===//
1330815c53SDimitry Andric 
1430815c53SDimitry Andric #include "llvm/ADT/Twine.h"
15b60736ecSDimitry Andric #include "llvm/Support/raw_ostream.h"
165a5ac124SDimitry Andric #include "llvm/Support/Signals.h"
17eb11fae6SDimitry Andric #include "llvm/Support/WithColor.h"
18b60736ecSDimitry Andric #include "llvm/TableGen/Error.h"
19b60736ecSDimitry Andric #include "llvm/TableGen/Record.h"
20522600a2SDimitry Andric #include <cstdlib>
21522600a2SDimitry Andric 
2230815c53SDimitry Andric namespace llvm {
2330815c53SDimitry Andric 
2430815c53SDimitry Andric SourceMgr SrcMgr;
254a16efa3SDimitry Andric unsigned ErrorsPrinted = 0;
2630815c53SDimitry Andric 
PrintMessage(ArrayRef<SMLoc> Loc,SourceMgr::DiagKind Kind,const Twine & Msg)27522600a2SDimitry Andric static void PrintMessage(ArrayRef<SMLoc> Loc, SourceMgr::DiagKind Kind,
28522600a2SDimitry Andric                          const Twine &Msg) {
294a16efa3SDimitry Andric   // Count the total number of errors printed.
304a16efa3SDimitry Andric   // This is used to exit with an error code if there were any errors.
314a16efa3SDimitry Andric   if (Kind == SourceMgr::DK_Error)
324a16efa3SDimitry Andric     ++ErrorsPrinted;
334a16efa3SDimitry Andric 
34522600a2SDimitry Andric   SMLoc NullLoc;
35522600a2SDimitry Andric   if (Loc.empty())
36522600a2SDimitry Andric     Loc = NullLoc;
37522600a2SDimitry Andric   SrcMgr.PrintMessage(Loc.front(), Kind, Msg);
38522600a2SDimitry Andric   for (unsigned i = 1; i < Loc.size(); ++i)
39522600a2SDimitry Andric     SrcMgr.PrintMessage(Loc[i], SourceMgr::DK_Note,
40522600a2SDimitry Andric                         "instantiated from multiclass");
41522600a2SDimitry Andric }
42522600a2SDimitry Andric 
43b60736ecSDimitry Andric // Functions to print notes.
44b60736ecSDimitry Andric 
PrintNote(const Twine & Msg)45b60736ecSDimitry Andric void PrintNote(const Twine &Msg) {
46b60736ecSDimitry Andric   WithColor::note() << Msg << "\n";
47b60736ecSDimitry Andric }
481d5ae102SDimitry Andric 
PrintNote(ArrayRef<SMLoc> NoteLoc,const Twine & Msg)49044eb2f6SDimitry Andric void PrintNote(ArrayRef<SMLoc> NoteLoc, const Twine &Msg) {
50044eb2f6SDimitry Andric   PrintMessage(NoteLoc, SourceMgr::DK_Note, Msg);
51044eb2f6SDimitry Andric }
52044eb2f6SDimitry Andric 
53b60736ecSDimitry Andric // Functions to print fatal notes.
54b60736ecSDimitry Andric 
PrintFatalNote(const Twine & Msg)55b60736ecSDimitry Andric void PrintFatalNote(const Twine &Msg) {
56b60736ecSDimitry Andric   PrintNote(Msg);
57b60736ecSDimitry Andric   // The following call runs the file cleanup handlers.
58b60736ecSDimitry Andric   sys::RunInterruptHandlers();
59b60736ecSDimitry Andric   std::exit(1);
60b60736ecSDimitry Andric }
61b60736ecSDimitry Andric 
PrintFatalNote(ArrayRef<SMLoc> NoteLoc,const Twine & Msg)62b60736ecSDimitry Andric void PrintFatalNote(ArrayRef<SMLoc> NoteLoc, const Twine &Msg) {
63b60736ecSDimitry Andric   PrintNote(NoteLoc, Msg);
64b60736ecSDimitry Andric   // The following call runs the file cleanup handlers.
65b60736ecSDimitry Andric   sys::RunInterruptHandlers();
66b60736ecSDimitry Andric   std::exit(1);
67b60736ecSDimitry Andric }
68b60736ecSDimitry Andric 
69b60736ecSDimitry Andric // This method takes a Record and uses the source location
70b60736ecSDimitry Andric // stored in it.
PrintFatalNote(const Record * Rec,const Twine & Msg)71b60736ecSDimitry Andric void PrintFatalNote(const Record *Rec, const Twine &Msg) {
72b60736ecSDimitry Andric   PrintNote(Rec->getLoc(), Msg);
73b60736ecSDimitry Andric   // The following call runs the file cleanup handlers.
74b60736ecSDimitry Andric   sys::RunInterruptHandlers();
75b60736ecSDimitry Andric   std::exit(1);
76b60736ecSDimitry Andric }
77b60736ecSDimitry Andric 
78b60736ecSDimitry Andric // This method takes a RecordVal and uses the source location
79b60736ecSDimitry Andric // stored in it.
PrintFatalNote(const RecordVal * RecVal,const Twine & Msg)80b60736ecSDimitry Andric void PrintFatalNote(const RecordVal *RecVal, const Twine &Msg) {
81b60736ecSDimitry Andric   PrintNote(RecVal->getLoc(), Msg);
82b60736ecSDimitry Andric   // The following call runs the file cleanup handlers.
83b60736ecSDimitry Andric   sys::RunInterruptHandlers();
84b60736ecSDimitry Andric   std::exit(1);
85b60736ecSDimitry Andric }
86b60736ecSDimitry Andric 
87b60736ecSDimitry Andric // Functions to print warnings.
88b60736ecSDimitry Andric 
PrintWarning(const Twine & Msg)89b60736ecSDimitry Andric void PrintWarning(const Twine &Msg) { WithColor::warning() << Msg << "\n"; }
90b60736ecSDimitry Andric 
PrintWarning(ArrayRef<SMLoc> WarningLoc,const Twine & Msg)91522600a2SDimitry Andric void PrintWarning(ArrayRef<SMLoc> WarningLoc, const Twine &Msg) {
92522600a2SDimitry Andric   PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg);
93b61ab53cSDimitry Andric }
94b61ab53cSDimitry Andric 
PrintWarning(const char * Loc,const Twine & Msg)95b61ab53cSDimitry Andric void PrintWarning(const char *Loc, const Twine &Msg) {
96b61ab53cSDimitry Andric   SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Warning, Msg);
97b61ab53cSDimitry Andric }
98b61ab53cSDimitry Andric 
99b60736ecSDimitry Andric // Functions to print errors.
100b60736ecSDimitry Andric 
PrintError(const Twine & Msg)101b60736ecSDimitry Andric void PrintError(const Twine &Msg) { WithColor::error() << Msg << "\n"; }
102b61ab53cSDimitry Andric 
PrintError(ArrayRef<SMLoc> ErrorLoc,const Twine & Msg)103522600a2SDimitry Andric void PrintError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) {
104522600a2SDimitry Andric   PrintMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
10530815c53SDimitry Andric }
10630815c53SDimitry Andric 
PrintError(const char * Loc,const Twine & Msg)10730815c53SDimitry Andric void PrintError(const char *Loc, const Twine &Msg) {
10863faed5bSDimitry Andric   SrcMgr.PrintMessage(SMLoc::getFromPointer(Loc), SourceMgr::DK_Error, Msg);
10930815c53SDimitry Andric }
11030815c53SDimitry Andric 
111b60736ecSDimitry Andric // This method takes a Record and uses the source location
112b60736ecSDimitry Andric // stored in it.
PrintError(const Record * Rec,const Twine & Msg)113b60736ecSDimitry Andric void PrintError(const Record *Rec, const Twine &Msg) {
114b60736ecSDimitry Andric   PrintMessage(Rec->getLoc(), SourceMgr::DK_Error, Msg);
115b60736ecSDimitry Andric }
116b60736ecSDimitry Andric 
117b60736ecSDimitry Andric // This method takes a RecordVal and uses the source location
118b60736ecSDimitry Andric // stored in it.
PrintError(const RecordVal * RecVal,const Twine & Msg)119b60736ecSDimitry Andric void PrintError(const RecordVal *RecVal, const Twine &Msg) {
120b60736ecSDimitry Andric   PrintMessage(RecVal->getLoc(), SourceMgr::DK_Error, Msg);
121b60736ecSDimitry Andric }
122b60736ecSDimitry Andric 
123b60736ecSDimitry Andric // Functions to print fatal errors.
12430815c53SDimitry Andric 
PrintFatalError(const Twine & Msg)1255ca98fd9SDimitry Andric void PrintFatalError(const Twine &Msg) {
1265ca98fd9SDimitry Andric   PrintError(Msg);
1275a5ac124SDimitry Andric   // The following call runs the file cleanup handlers.
1285a5ac124SDimitry Andric   sys::RunInterruptHandlers();
129522600a2SDimitry Andric   std::exit(1);
130522600a2SDimitry Andric }
131522600a2SDimitry Andric 
PrintFatalError(ArrayRef<SMLoc> ErrorLoc,const Twine & Msg)1325ca98fd9SDimitry Andric void PrintFatalError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) {
133522600a2SDimitry Andric   PrintError(ErrorLoc, Msg);
1345a5ac124SDimitry Andric   // The following call runs the file cleanup handlers.
1355a5ac124SDimitry Andric   sys::RunInterruptHandlers();
136522600a2SDimitry Andric   std::exit(1);
13730815c53SDimitry Andric }
13830815c53SDimitry Andric 
139b60736ecSDimitry Andric // This method takes a Record and uses the source location
140b60736ecSDimitry Andric // stored in it.
PrintFatalError(const Record * Rec,const Twine & Msg)141b60736ecSDimitry Andric void PrintFatalError(const Record *Rec, const Twine &Msg) {
142b60736ecSDimitry Andric   PrintError(Rec->getLoc(), Msg);
143b60736ecSDimitry Andric   // The following call runs the file cleanup handlers.
144b60736ecSDimitry Andric   sys::RunInterruptHandlers();
145b60736ecSDimitry Andric   std::exit(1);
146b60736ecSDimitry Andric }
147b60736ecSDimitry Andric 
148b60736ecSDimitry Andric // This method takes a RecordVal and uses the source location
149b60736ecSDimitry Andric // stored in it.
PrintFatalError(const RecordVal * RecVal,const Twine & Msg)150b60736ecSDimitry Andric void PrintFatalError(const RecordVal *RecVal, const Twine &Msg) {
151b60736ecSDimitry Andric   PrintError(RecVal->getLoc(), Msg);
152b60736ecSDimitry Andric   // The following call runs the file cleanup handlers.
153b60736ecSDimitry Andric   sys::RunInterruptHandlers();
154b60736ecSDimitry Andric   std::exit(1);
155b60736ecSDimitry Andric }
156b60736ecSDimitry Andric 
157344a3780SDimitry Andric // Check an assertion: Obtain the condition value and be sure it is true.
158344a3780SDimitry Andric // If not, print a nonfatal error along with the message.
CheckAssert(SMLoc Loc,Init * Condition,Init * Message)159344a3780SDimitry Andric void CheckAssert(SMLoc Loc, Init *Condition, Init *Message) {
160145449b1SDimitry Andric   auto *CondValue = dyn_cast_or_null<IntInit>(Condition->convertInitializerTo(
161145449b1SDimitry Andric       IntRecTy::get(Condition->getRecordKeeper())));
162344a3780SDimitry Andric   if (!CondValue)
163344a3780SDimitry Andric     PrintError(Loc, "assert condition must of type bit, bits, or int.");
164344a3780SDimitry Andric   else if (!CondValue->getValue()) {
165344a3780SDimitry Andric     PrintError(Loc, "assertion failed");
166344a3780SDimitry Andric     if (auto *MessageInit = dyn_cast<StringInit>(Message))
167344a3780SDimitry Andric       PrintNote(MessageInit->getValue());
168344a3780SDimitry Andric     else
169344a3780SDimitry Andric       PrintNote("(assert message is not a string)");
170344a3780SDimitry Andric   }
171344a3780SDimitry Andric }
172344a3780SDimitry Andric 
173b1c73532SDimitry Andric // Dump a message to stderr.
dumpMessage(SMLoc Loc,Init * Message)174b1c73532SDimitry Andric void dumpMessage(SMLoc Loc, Init *Message) {
175b1c73532SDimitry Andric   auto *MessageInit = dyn_cast<StringInit>(Message);
176b1c73532SDimitry Andric   assert(MessageInit && "no debug message to print");
177b1c73532SDimitry Andric   PrintNote(Loc, MessageInit->getValue());
178b1c73532SDimitry Andric }
179b1c73532SDimitry Andric 
18030815c53SDimitry Andric } // end namespace llvm
181