LLVM 22.0.0git
Error.cpp
Go to the documentation of this file.
1//===- Error.cpp - tblgen error handling helper routines --------*- C++ -*-===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file contains error handling helper routines to pretty-print diagnostic
10// messages from tblgen.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/ADT/Twine.h"
18#include "llvm/TableGen/Error.h"
20#include <cstdlib>
21
22using namespace llvm;
23
26
28 const Twine &Msg) {
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)
33
34 SMLoc NullLoc;
35 if (Locs.empty())
36 Locs = NullLoc;
37 SrcMgr.PrintMessage(Locs.consume_front(), Kind, Msg);
38 for (SMLoc Loc : Locs)
39 SrcMgr.PrintMessage(Loc, SourceMgr::DK_Note,
40 "instantiated from multiclass");
41}
42
43// Run file cleanup handlers and then exit fatally (with non-zero exit code).
44[[noreturn]] inline static void fatal_exit() {
45 // The following call runs the file cleanup handlers.
47 std::exit(1);
48}
49
50// Functions to print notes.
51
52void llvm::PrintNote(const Twine &Msg) { WithColor::note() << Msg << "\n"; }
53
54void llvm::PrintNote(function_ref<void(raw_ostream &OS)> PrintMsg) {
55 PrintMsg(WithColor::note());
56}
57
58void llvm::PrintNote(ArrayRef<SMLoc> NoteLoc, const Twine &Msg) {
59 PrintMessage(NoteLoc, SourceMgr::DK_Note, Msg);
60}
61
62// Functions to print fatal notes.
63
64void llvm::PrintFatalNote(const Twine &Msg) {
65 PrintNote(Msg);
66 fatal_exit();
67}
68
69void llvm::PrintFatalNote(ArrayRef<SMLoc> NoteLoc, const Twine &Msg) {
70 PrintNote(NoteLoc, Msg);
71 fatal_exit();
72}
73
74// This method takes a Record and uses the source location
75// stored in it.
76void llvm::PrintFatalNote(const Record *Rec, const Twine &Msg) {
77 PrintNote(Rec->getLoc(), Msg);
78 fatal_exit();
79}
80
81// This method takes a RecordVal and uses the source location
82// stored in it.
83void llvm::PrintFatalNote(const RecordVal *RecVal, const Twine &Msg) {
84 PrintNote(RecVal->getLoc(), Msg);
85 fatal_exit();
86}
87
88// Functions to print warnings.
89
90void llvm::PrintWarning(const Twine &Msg) {
91 WithColor::warning() << Msg << "\n";
92}
93
94void llvm::PrintWarning(ArrayRef<SMLoc> WarningLoc, const Twine &Msg) {
95 PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg);
96}
97
98void llvm::PrintWarning(const char *Loc, const Twine &Msg) {
100}
101
102// Functions to print errors.
103
104void llvm::PrintError(const Twine &Msg) { WithColor::error() << Msg << "\n"; }
105
106void llvm::PrintError(function_ref<void(raw_ostream &OS)> PrintMsg) {
107 PrintMsg(WithColor::error());
108}
109
110void llvm::PrintError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) {
111 PrintMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
112}
113
114void llvm::PrintError(const char *Loc, const Twine &Msg) {
116}
117
118// This method takes a Record and uses the source location
119// stored in it.
120void llvm::PrintError(const Record *Rec, const Twine &Msg) {
122}
123
124// This method takes a RecordVal and uses the source location
125// stored in it.
126void llvm::PrintError(const RecordVal *RecVal, const Twine &Msg) {
127 PrintMessage(RecVal->getLoc(), SourceMgr::DK_Error, Msg);
128}
129
130// Functions to print fatal errors.
131
132void llvm::PrintFatalError(const Twine &Msg) {
133 PrintError(Msg);
134 fatal_exit();
135}
136
138 PrintError(PrintMsg);
139 fatal_exit();
140}
141
142void llvm::PrintFatalError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) {
143 PrintError(ErrorLoc, Msg);
144 fatal_exit();
145}
146
147// This method takes a Record and uses the source location
148// stored in it.
149void llvm::PrintFatalError(const Record *Rec, const Twine &Msg) {
150 PrintError(Rec->getLoc(), Msg);
151 fatal_exit();
152}
153
154// This method takes a RecordVal and uses the source location
155// stored in it.
156void llvm::PrintFatalError(const RecordVal *RecVal, const Twine &Msg) {
157 PrintError(RecVal->getLoc(), Msg);
158 fatal_exit();
159}
160
161// Check an assertion: Obtain the condition value and be sure it is true.
162// If not, print a nonfatal error along with the message.
163bool llvm::CheckAssert(SMLoc Loc, const Init *Condition, const Init *Message) {
164 auto *CondValue = dyn_cast_or_null<IntInit>(Condition->convertInitializerTo(
165 IntRecTy::get(Condition->getRecordKeeper())));
166 if (!CondValue) {
167 PrintError(Loc, "assert condition must of type bit, bits, or int.");
168 return true;
169 }
170 if (!CondValue->getValue()) {
171 auto *MessageInit = dyn_cast<StringInit>(Message);
172 StringRef AssertMsg = MessageInit ? MessageInit->getValue()
173 : "(assert message is not a string)";
174 PrintError(Loc, "assertion failed: " + AssertMsg);
175 return true;
176 }
177 return false;
178}
179
180// Dump a message to stderr.
181void llvm::dumpMessage(SMLoc Loc, const Init *Message) {
182 if (auto *MessageInit = dyn_cast<StringInit>(Message))
183 PrintNote(Loc, MessageInit->getValue());
184 else
185 PrintError(Loc, "dump value is not of type string");
186}
static void fatal_exit()
Definition Error.cpp:44
static void PrintMessage(ArrayRef< SMLoc > Locs, SourceMgr::DiagKind Kind, const Twine &Msg)
Definition Error.cpp:27
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
bool empty() const
empty - Check if the array is empty.
Definition ArrayRef.h:137
const T & consume_front()
consume_front() - Returns the first element and drops it from ArrayRef.
Definition ArrayRef.h:157
virtual const Init * convertInitializerTo(const RecTy *Ty) const =0
Convert to a value whose type is Ty, or return null if this is not possible.
RecordKeeper & getRecordKeeper() const
Get the record keeper that initialized this Init.
Definition Record.cpp:381
static const IntRecTy * get(RecordKeeper &RK)
Definition Record.cpp:184
This class represents a field in a record, including its name, type, value, and source location.
Definition Record.h:1541
SMLoc getLoc() const
Get the source location of the point where the field was defined.
Definition Record.h:1580
ArrayRef< SMLoc > getLoc() const
Definition Record.h:1720
Represents a location in source code.
Definition SMLoc.h:22
static SMLoc getFromPointer(const char *Ptr)
Definition SMLoc.h:35
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition SourceMgr.h:37
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
static LLVM_ABI raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
Definition WithColor.cpp:85
static LLVM_ABI raw_ostream & error()
Convenience method for printing "error: " to stderr.
Definition WithColor.cpp:83
static LLVM_ABI raw_ostream & note()
Convenience method for printing "note: " to stderr.
Definition WithColor.cpp:87
An efficient, type-erasing, non-owning reference to a callable.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
LLVM_ABI void RunInterruptHandlers()
This function runs all the registered interrupt handlers, including the removal of files registered b...
This is an optimization pass for GlobalISel generic memory operations.
Definition Types.h:26
unsigned ErrorsPrinted
Definition Error.cpp:25
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
void PrintFatalError(const Twine &Msg)
Definition Error.cpp:132
void PrintError(const Twine &Msg)
Definition Error.cpp:104
bool CheckAssert(SMLoc Loc, const Init *Condition, const Init *Message)
Definition Error.cpp:163
SourceMgr SrcMgr
Definition Error.cpp:24
auto dyn_cast_or_null(const Y &Val)
Definition Casting.h:753
void PrintWarning(const Twine &Msg)
Definition Error.cpp:90
void PrintNote(const Twine &Msg)
Definition Error.cpp:52
void PrintFatalNote(const Twine &Msg)
Definition Error.cpp:64
void dumpMessage(SMLoc Loc, const Init *Message)
Definition Error.cpp:181