LLVM  13.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"
16 #include "llvm/Support/Signals.h"
17 #include "llvm/Support/WithColor.h"
18 #include "llvm/TableGen/Error.h"
19 #include "llvm/TableGen/Record.h"
20 #include <cstdlib>
21 
22 namespace llvm {
23 
25 unsigned ErrorsPrinted = 0;
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.
32  ++ErrorsPrinted;
33 
34  SMLoc NullLoc;
35  if (Loc.empty())
36  Loc = NullLoc;
37  SrcMgr.PrintMessage(Loc.front(), Kind, Msg);
38  for (unsigned i = 1; i < Loc.size(); ++i)
40  "instantiated from multiclass");
41 }
42 
43 // Functions to print notes.
44 
45 void PrintNote(const Twine &Msg) {
46  WithColor::note() << Msg << "\n";
47 }
48 
49 void PrintNote(ArrayRef<SMLoc> NoteLoc, const Twine &Msg) {
50  PrintMessage(NoteLoc, SourceMgr::DK_Note, Msg);
51 }
52 
53 // Functions to print fatal notes.
54 
55 void PrintFatalNote(const Twine &Msg) {
56  PrintNote(Msg);
57  // The following call runs the file cleanup handlers.
59  std::exit(1);
60 }
61 
62 void PrintFatalNote(ArrayRef<SMLoc> NoteLoc, const Twine &Msg) {
63  PrintNote(NoteLoc, Msg);
64  // The following call runs the file cleanup handlers.
66  std::exit(1);
67 }
68 
69 // This method takes a Record and uses the source location
70 // stored in it.
71 void PrintFatalNote(const Record *Rec, const Twine &Msg) {
72  PrintNote(Rec->getLoc(), Msg);
73  // The following call runs the file cleanup handlers.
75  std::exit(1);
76 }
77 
78 // This method takes a RecordVal and uses the source location
79 // stored in it.
80 void PrintFatalNote(const RecordVal *RecVal, const Twine &Msg) {
81  PrintNote(RecVal->getLoc(), Msg);
82  // The following call runs the file cleanup handlers.
84  std::exit(1);
85 }
86 
87 // Functions to print warnings.
88 
89 void PrintWarning(const Twine &Msg) { WithColor::warning() << Msg << "\n"; }
90 
91 void PrintWarning(ArrayRef<SMLoc> WarningLoc, const Twine &Msg) {
92  PrintMessage(WarningLoc, SourceMgr::DK_Warning, Msg);
93 }
94 
95 void PrintWarning(const char *Loc, const Twine &Msg) {
97 }
98 
99 // Functions to print errors.
100 
101 void PrintError(const Twine &Msg) { WithColor::error() << Msg << "\n"; }
102 
103 void PrintError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) {
104  PrintMessage(ErrorLoc, SourceMgr::DK_Error, Msg);
105 }
106 
107 void PrintError(const char *Loc, const Twine &Msg) {
109 }
110 
111 // This method takes a Record and uses the source location
112 // stored in it.
113 void PrintError(const Record *Rec, const Twine &Msg) {
115 }
116 
117 // This method takes a RecordVal and uses the source location
118 // stored in it.
119 void PrintError(const RecordVal *RecVal, const Twine &Msg) {
120  PrintMessage(RecVal->getLoc(), SourceMgr::DK_Error, Msg);
121 }
122 
123 // Functions to print fatal errors.
124 
125 void PrintFatalError(const Twine &Msg) {
126  PrintError(Msg);
127  // The following call runs the file cleanup handlers.
129  std::exit(1);
130 }
131 
132 void PrintFatalError(ArrayRef<SMLoc> ErrorLoc, const Twine &Msg) {
133  PrintError(ErrorLoc, Msg);
134  // The following call runs the file cleanup handlers.
136  std::exit(1);
137 }
138 
139 // This method takes a Record and uses the source location
140 // stored in it.
141 void PrintFatalError(const Record *Rec, const Twine &Msg) {
142  PrintError(Rec->getLoc(), Msg);
143  // The following call runs the file cleanup handlers.
145  std::exit(1);
146 }
147 
148 // This method takes a RecordVal and uses the source location
149 // stored in it.
150 void PrintFatalError(const RecordVal *RecVal, const Twine &Msg) {
151  PrintError(RecVal->getLoc(), Msg);
152  // The following call runs the file cleanup handlers.
154  std::exit(1);
155 }
156 
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>(
161  Condition->convertInitializerTo(IntRecTy::get()));
162  if (!CondValue)
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());
168  else
169  PrintNote("(assert message is not a string)");
170  }
171 }
172 
173 } // end namespace llvm
i
i
Definition: README.txt:29
Signals.h
llvm
Definition: AllocatorList.h:23
llvm::PrintMessage
static void PrintMessage(ArrayRef< SMLoc > Loc, SourceMgr::DiagKind Kind, const Twine &Msg)
Definition: Error.cpp:27
llvm::Init::convertInitializerTo
virtual Init * convertInitializerTo(RecTy *Ty) const =0
Convert to a value whose type is Ty, or return null if this is not possible.
llvm::Record::getLoc
ArrayRef< SMLoc > getLoc() const
Definition: Record.h:1552
llvm::SourceMgr::DK_Note
@ DK_Note
Definition: SourceMgr.h:37
llvm::sys::RunInterruptHandlers
void RunInterruptHandlers()
This function runs all the registered interrupt handlers, including the removal of files registered b...
llvm::ArrayRef::empty
bool empty() const
empty - Check if the array is empty.
Definition: ArrayRef.h:160
llvm::WithColor::error
static raw_ostream & error()
Convenience method for printing "error: " to stderr.
Definition: WithColor.cpp:61
llvm::SMLoc
Represents a location in source code.
Definition: SMLoc.h:23
llvm::CheckAssert
void CheckAssert(SMLoc Loc, Init *Condition, Init *Message)
Definition: Error.cpp:159
Twine.h
llvm::SrcMgr
SourceMgr SrcMgr
Definition: Error.cpp:24
llvm::PrintFatalError
LLVM_ATTRIBUTE_NORETURN void PrintFatalError(const Twine &Msg)
Definition: Error.cpp:125
llvm::IntRecTy::get
static IntRecTy * get()
Definition: Record.h:152
llvm::ErrorsPrinted
unsigned ErrorsPrinted
Definition: Error.cpp:25
llvm::lltok::Kind
Kind
Definition: LLToken.h:18
Error.h
llvm::WithColor::warning
static raw_ostream & warning()
Convenience method for printing "warning: " to stderr.
Definition: WithColor.cpp:63
llvm::SourceMgr::PrintMessage
void PrintMessage(raw_ostream &OS, SMLoc Loc, DiagKind Kind, const Twine &Msg, ArrayRef< SMRange > Ranges={}, ArrayRef< SMFixIt > FixIts={}, bool ShowColors=true) const
Emit a message about the specified location with the specified string.
Definition: SourceMgr.cpp:341
llvm::SourceMgr::DK_Warning
@ DK_Warning
Definition: SourceMgr.h:35
llvm::PrintFatalNote
LLVM_ATTRIBUTE_NORETURN void PrintFatalNote(const Twine &Msg)
Definition: Error.cpp:55
llvm::Record
Definition: Record.h:1472
llvm::ArrayRef
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: APInt.h:32
llvm::SourceMgr
This owns the files read by a parser, handles include stacks, and handles diagnostic wrangling.
Definition: SourceMgr.h:31
llvm::ArrayRef::front
const T & front() const
front - Get the first element.
Definition: ArrayRef.h:168
llvm::Init
Definition: Record.h:271
llvm::SourceMgr::DiagKind
DiagKind
Definition: SourceMgr.h:33
WithColor.h
llvm::Twine
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:80
llvm::SourceMgr::DK_Error
@ DK_Error
Definition: SourceMgr.h:34
llvm::RecordVal::getLoc
const SMLoc & getLoc() const
Get the source location of the point where the field was defined.
Definition: Record.h:1434
llvm::PrintError
void PrintError(const Twine &Msg)
Definition: Error.cpp:101
exit
declare void exit(i32) noreturn nounwind This compiles into
Definition: README.txt:1072
llvm::PrintWarning
void PrintWarning(const Twine &Msg)
Definition: Error.cpp:89
llvm::ArrayRef::size
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
llvm::PrintNote
void PrintNote(const Twine &Msg)
Definition: Error.cpp:45
llvm::SMLoc::getFromPointer
static SMLoc getFromPointer(const char *Ptr)
Definition: SMLoc.h:36
llvm::WithColor::note
static raw_ostream & note()
Convenience method for printing "note: " to stderr.
Definition: WithColor.cpp:65
raw_ostream.h
llvm::RecordVal
This class represents a field in a record, including its name, type, value, and source location.
Definition: Record.h:1402
Record.h