WPILibC++ 2024.3.2
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 behaviour.
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, if the platform does not support a builtin unreachable
127/// then we call an internal LLVM runtime function. Otherwise the behavior is
128/// controlled by the CMake flag
129/// -DLLVM_UNREACHABLE_OPTIMIZE
130/// * When "ON" (default) wpi_unreachable() becomes an optimizer hint
131/// that the current location is not supposed to be reachable: the hint
132/// turns such code path into undefined behavior. On compilers that don't
133/// support such hints, prints a reduced message instead and aborts the
134/// program.
135/// * When "OFF", a builtin_trap is emitted instead of an
136// optimizer hint or printing a reduced message.
137///
138/// Use this instead of assert(0). It conveys intent more clearly, suppresses
139/// diagnostics for unreachable code paths, and allows compilers to omit
140/// unnecessary code.
141#ifndef NDEBUG
142#define wpi_unreachable(msg) \
143 ::wpi::wpi_unreachable_internal(msg, __FILE__, __LINE__)
144#elif !defined(LLVM_BUILTIN_UNREACHABLE)
145#define wpi_unreachable(msg) ::wpi::wpi_unreachable_internal()
146#elif LLVM_UNREACHABLE_OPTIMIZE
147#define wpi_unreachable(msg) LLVM_BUILTIN_UNREACHABLE
148#else
149#define wpi_unreachable(msg) \
150 do { \
151 LLVM_BUILTIN_TRAP; \
152 LLVM_BUILTIN_UNREACHABLE; \
153 } while (false)
154#endif
155
156#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:501
Definition: ntcore_cpp.h:26
void remove_fatal_error_handler()
Restores default error handling behaviour.
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 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
void(* fatal_error_handler_t)(void *user_data, const char *reason, bool gen_crash_diag)
An error handler callback.
Definition: ErrorHandling.h:24