WPILibC++ 2023.4.3-108-ge5452e3
ErrorHandling.h
Go to the documentation of this file.
1//===- llvm/Support/ErrorHandling.h - Fatal error handling ------*- 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 defines an API used to indicate fatal error conditions. Non-fatal
10// errors (most of them) should be handled through LLVMContext.
11//
12//===----------------------------------------------------------------------===//
13
14#ifndef WPIUTIL_WPI_ERRORHANDLING_H
15#define WPIUTIL_WPI_ERRORHANDLING_H
16
17#include "wpi/Compiler.h"
18#include <string>
19#include <string_view>
20
21namespace wpi {
22
23 /// An error handler callback.
24 typedef void (*fatal_error_handler_t)(void *user_data,
25 const char *reason,
26 bool gen_crash_diag);
27
28 /// install_fatal_error_handler - Installs a new error handler to be used
29 /// whenever a serious (non-recoverable) error is encountered by LLVM.
30 ///
31 /// If no error handler is installed the default is to print the error message
32 /// to stderr, and call exit(1). If an error handler is installed then it is
33 /// the handler's responsibility to log the message, it will no longer be
34 /// printed to stderr. If the error handler returns, then exit(1) will be
35 /// called.
36 ///
37 /// It is dangerous to naively use an error handler which throws an exception.
38 /// Even though some applications desire to gracefully recover from arbitrary
39 /// faults, blindly throwing exceptions through unfamiliar code isn't a way to
40 /// achieve this.
41 ///
42 /// \param user_data - An argument which will be passed to the install error
43 /// handler.
45 void *user_data = nullptr);
46
47 /// Restores default error handling behavior.
49
50 /// ScopedFatalErrorHandler - This is a simple helper class which just
51 /// calls install_fatal_error_handler in its constructor and
52 /// remove_fatal_error_handler in its destructor.
55 void *user_data = nullptr) {
56 install_fatal_error_handler(handler, user_data);
57 }
58
60 };
61
62/// Reports a serious error, calling any installed error handler. These
63/// functions are intended to be used for error conditions which are outside
64/// the control of the compiler (I/O errors, invalid user input, etc.)
65///
66/// If no error handler is installed the default is to print the message to
67/// standard error, followed by a newline.
68/// After the error handler is called this function will call abort(), it
69/// does not return.
70[[noreturn]] void report_fatal_error(const char *reason,
71 bool gen_crash_diag = true);
72[[noreturn]] void report_fatal_error(const std::string &reason,
73 bool gen_crash_diag = true);
74[[noreturn]] void report_fatal_error(std::string_view reason,
75 bool gen_crash_diag = true);
76
77/// Installs a new bad alloc error handler that should be used whenever a
78/// bad alloc error, e.g. failing malloc/calloc, is encountered by LLVM.
79///
80/// The user can install a bad alloc handler, in order to define the behavior
81/// in case of failing allocations, e.g. throwing an exception. Note that this
82/// handler must not trigger any additional allocations itself.
83///
84/// If no error handler is installed the default is to print the error message
85/// to stderr, and call exit(1). If an error handler is installed then it is
86/// the handler's responsibility to log the message, it will no longer be
87/// printed to stderr. If the error handler returns, then exit(1) will be
88/// called.
89///
90///
91/// \param user_data - An argument which will be passed to the installed error
92/// handler.
94 void *user_data = nullptr);
95
96/// Restores default bad alloc error handling behavior.
98
100
101/// Reports a bad alloc error, calling any user defined bad alloc
102/// error handler. In contrast to the generic 'report_fatal_error'
103/// functions, this function might not terminate, e.g. the user
104/// defined error handler throws an exception, but it won't return.
105///
106/// Note: When throwing an exception in the bad alloc handler, make sure that
107/// the following unwind succeeds, e.g. do not trigger additional allocations
108/// in the unwind chain.
109///
110/// If no error handler is installed (default), throws a bad_alloc exception
111/// if LLVM is compiled with exception support. Otherwise prints the error
112/// to standard error and calls abort().
113[[noreturn]] void report_bad_alloc_error(const char *Reason,
114 bool GenCrashDiag = true);
115
116/// This function calls abort(), and prints the optional message to stderr.
117/// Use the wpi_unreachable macro (that adds location info), instead of
118/// calling this function directly.
119[[noreturn]] void
120wpi_unreachable_internal(const char *msg = nullptr, const char *file = nullptr,
121 unsigned line = 0);
122}
123
124/// Marks that the current location is not supposed to be reachable.
125/// In !NDEBUG builds, prints the message and location info to stderr.
126/// In NDEBUG builds, becomes an optimizer hint that the current location
127/// is not supposed to be reachable. On compilers that don't support
128/// such hints, prints a reduced message instead and aborts the program.
129///
130/// Use this instead of assert(0). It conveys intent more clearly and
131/// allows compilers to omit some unnecessary code.
132#ifndef NDEBUG
133#define wpi_unreachable(msg) \
134 ::wpi::wpi_unreachable_internal(msg, __FILE__, __LINE__)
135#elif defined(LLVM_BUILTIN_UNREACHABLE)
136#define wpi_unreachable(msg) LLVM_BUILTIN_UNREACHABLE
137#else
138#define wpi_unreachable(msg) ::wpi::wpi_unreachable_internal()
139#endif
140
141#endif
then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file
Definition: ThirdPartyNotices.txt:192
basic_string_view< char > string_view
Definition: core.h:520
Definition: AprilTagFieldLayout.h:18
void remove_fatal_error_handler()
Restores default error handling behavior.
void wpi_unreachable_internal(const char *msg=nullptr, const char *file=nullptr, unsigned line=0)
This function calls abort(), and prints the optional message to stderr.
void install_fatal_error_handler(fatal_error_handler_t handler, void *user_data=nullptr)
install_fatal_error_handler - Installs a new error handler to be used whenever a serious (non-recover...
void report_fatal_error(const char *reason, bool gen_crash_diag=true)
Reports a serious error, calling any installed error handler.
void remove_bad_alloc_error_handler()
Restores default bad alloc error handling behavior.
void report_bad_alloc_error(const char *Reason, bool GenCrashDiag=true)
Reports a bad alloc error, calling any user defined bad alloc error handler.
void(* fatal_error_handler_t)(void *user_data, const char *reason, bool gen_crash_diag)
An error handler callback.
Definition: ErrorHandling.h:24
void install_bad_alloc_error_handler(fatal_error_handler_t handler, void *user_data=nullptr)
Installs a new bad alloc error handler that should be used whenever a bad alloc error,...
void install_out_of_memory_new_handler()
ScopedFatalErrorHandler - This is a simple helper class which just calls install_fatal_error_handler ...
Definition: ErrorHandling.h:53
~ScopedFatalErrorHandler()
Definition: ErrorHandling.h:59
ScopedFatalErrorHandler(fatal_error_handler_t handler, void *user_data=nullptr)
Definition: ErrorHandling.h:54