1 //===- Error.cpp - tblgen error handling helper routines --------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file contains error handling helper routines to pretty-print diagnostic
10 // messages from tblgen.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/ADT/Twine.h"
15 #include "llvm/Support/raw_ostream.h"
16 #include "llvm/Support/Signals.h"
17 #include "llvm/Support/WithColor.h"
18 #include "llvm/TableGen/Error.h"
19 #include "llvm/TableGen/Record.h"
25 unsigned ErrorsPrinted
= 0;
27 static void PrintMessage(ArrayRef
<SMLoc
> Loc
, SourceMgr::DiagKind Kind
,
29 // Count the total number of errors printed.
30 // This is used to exit with an error code if there were any errors.
31 if (Kind
== SourceMgr::DK_Error
)
37 SrcMgr
.PrintMessage(Loc
.front(), Kind
, Msg
);
38 for (unsigned i
= 1; i
< Loc
.size(); ++i
)
39 SrcMgr
.PrintMessage(Loc
[i
], SourceMgr::DK_Note
,
40 "instantiated from multiclass");
43 // Functions to print notes.
45 void PrintNote(const Twine
&Msg
) {
46 WithColor::note() << Msg
<< "\n";
49 void PrintNote(ArrayRef
<SMLoc
> NoteLoc
, const Twine
&Msg
) {
50 PrintMessage(NoteLoc
, SourceMgr::DK_Note
, Msg
);
53 // Functions to print fatal notes.
55 void PrintFatalNote(const Twine
&Msg
) {
57 // The following call runs the file cleanup handlers.
58 sys::RunInterruptHandlers();
62 void PrintFatalNote(ArrayRef
<SMLoc
> NoteLoc
, const Twine
&Msg
) {
63 PrintNote(NoteLoc
, Msg
);
64 // The following call runs the file cleanup handlers.
65 sys::RunInterruptHandlers();
69 // This method takes a Record and uses the source location
71 void PrintFatalNote(const Record
*Rec
, const Twine
&Msg
) {
72 PrintNote(Rec
->getLoc(), Msg
);
73 // The following call runs the file cleanup handlers.
74 sys::RunInterruptHandlers();
78 // This method takes a RecordVal and uses the source location
80 void PrintFatalNote(const RecordVal
*RecVal
, const Twine
&Msg
) {
81 PrintNote(RecVal
->getLoc(), Msg
);
82 // The following call runs the file cleanup handlers.
83 sys::RunInterruptHandlers();
87 // Functions to print warnings.
89 void PrintWarning(const Twine
&Msg
) { WithColor::warning() << Msg
<< "\n"; }
91 void PrintWarning(ArrayRef
<SMLoc
> WarningLoc
, const Twine
&Msg
) {
92 PrintMessage(WarningLoc
, SourceMgr::DK_Warning
, Msg
);
95 void PrintWarning(const char *Loc
, const Twine
&Msg
) {
96 SrcMgr
.PrintMessage(SMLoc::getFromPointer(Loc
), SourceMgr::DK_Warning
, Msg
);
99 // Functions to print errors.
101 void PrintError(const Twine
&Msg
) { WithColor::error() << Msg
<< "\n"; }
103 void PrintError(ArrayRef
<SMLoc
> ErrorLoc
, const Twine
&Msg
) {
104 PrintMessage(ErrorLoc
, SourceMgr::DK_Error
, Msg
);
107 void PrintError(const char *Loc
, const Twine
&Msg
) {
108 SrcMgr
.PrintMessage(SMLoc::getFromPointer(Loc
), SourceMgr::DK_Error
, Msg
);
111 // This method takes a Record and uses the source location
113 void PrintError(const Record
*Rec
, const Twine
&Msg
) {
114 PrintMessage(Rec
->getLoc(), SourceMgr::DK_Error
, Msg
);
117 // This method takes a RecordVal and uses the source location
119 void PrintError(const RecordVal
*RecVal
, const Twine
&Msg
) {
120 PrintMessage(RecVal
->getLoc(), SourceMgr::DK_Error
, Msg
);
123 // Functions to print fatal errors.
125 void PrintFatalError(const Twine
&Msg
) {
127 // The following call runs the file cleanup handlers.
128 sys::RunInterruptHandlers();
132 void PrintFatalError(ArrayRef
<SMLoc
> ErrorLoc
, const Twine
&Msg
) {
133 PrintError(ErrorLoc
, Msg
);
134 // The following call runs the file cleanup handlers.
135 sys::RunInterruptHandlers();
139 // This method takes a Record and uses the source location
141 void PrintFatalError(const Record
*Rec
, const Twine
&Msg
) {
142 PrintError(Rec
->getLoc(), Msg
);
143 // The following call runs the file cleanup handlers.
144 sys::RunInterruptHandlers();
148 // This method takes a RecordVal and uses the source location
150 void PrintFatalError(const RecordVal
*RecVal
, const Twine
&Msg
) {
151 PrintError(RecVal
->getLoc(), Msg
);
152 // The following call runs the file cleanup handlers.
153 sys::RunInterruptHandlers();
157 // Check an assertion: Obtain the condition value and be sure it is true.
158 // If not, print a nonfatal error along with the message.
159 void CheckAssert(SMLoc Loc
, Init
*Condition
, Init
*Message
) {
160 auto *CondValue
= dyn_cast_or_null
<IntInit
>(Condition
->convertInitializerTo(
161 IntRecTy::get(Condition
->getRecordKeeper())));
163 PrintError(Loc
, "assert condition must of type bit, bits, or int.");
164 else if (!CondValue
->getValue()) {
165 PrintError(Loc
, "assertion failed");
166 if (auto *MessageInit
= dyn_cast
<StringInit
>(Message
))
167 PrintNote(MessageInit
->getValue());
169 PrintNote("(assert message is not a string)");
173 // Dump a message to stderr.
174 void dumpMessage(SMLoc Loc
, Init
*Message
) {
175 auto *MessageInit
= dyn_cast
<StringInit
>(Message
);
176 assert(MessageInit
&& "no debug message to print");
177 PrintNote(Loc
, MessageInit
->getValue());
180 } // end namespace llvm