LLVM API Documentation

 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ErrorHandling.h
Go to the documentation of this file.
1 //===- llvm/Support/ErrorHandling.h - Fatal error handling ------*- C++ -*-===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines an API used to indicate fatal error conditions. Non-fatal
11 // errors (most of them) should be handled through LLVMContext.
12 //
13 //===----------------------------------------------------------------------===//
14 
15 #ifndef LLVM_SUPPORT_ERRORHANDLING_H
16 #define LLVM_SUPPORT_ERRORHANDLING_H
17 
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/Support/Compiler.h"
20 #include <string>
21 
22 namespace llvm {
23  class Twine;
24 
25  /// An error handler callback.
26  typedef void (*fatal_error_handler_t)(void *user_data,
27  const std::string& reason,
28  bool gen_crash_diag);
29 
30  /// install_fatal_error_handler - Installs a new error handler to be used
31  /// whenever a serious (non-recoverable) error is encountered by LLVM.
32  ///
33  /// If you are using llvm_start_multithreaded, you should register the handler
34  /// before doing that.
35  ///
36  /// If no error handler is installed the default is to print the error message
37  /// to stderr, and call exit(1). If an error handler is installed then it is
38  /// the handler's responsibility to log the message, it will no longer be
39  /// printed to stderr. If the error handler returns, then exit(1) will be
40  /// called.
41  ///
42  /// It is dangerous to naively use an error handler which throws an exception.
43  /// Even though some applications desire to gracefully recover from arbitrary
44  /// faults, blindly throwing exceptions through unfamiliar code isn't a way to
45  /// achieve this.
46  ///
47  /// \param user_data - An argument which will be passed to the install error
48  /// handler.
50  void *user_data = 0);
51 
52  /// Restores default error handling behaviour.
53  /// This must not be called between llvm_start_multithreaded() and
54  /// llvm_stop_multithreaded().
56 
57  /// ScopedFatalErrorHandler - This is a simple helper class which just
58  /// calls install_fatal_error_handler in its constructor and
59  /// remove_fatal_error_handler in its destructor.
62  void *user_data = 0) {
63  install_fatal_error_handler(handler, user_data);
64  }
65 
67  };
68 
69  /// Reports a serious error, calling any installed error handler. These
70  /// functions are intended to be used for error conditions which are outside
71  /// the control of the compiler (I/O errors, invalid user input, etc.)
72  ///
73  /// If no error handler is installed the default is to print the message to
74  /// standard error, followed by a newline.
75  /// After the error handler is called this function will call exit(1), it
76  /// does not return.
77  LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason,
78  bool gen_crash_diag = true);
79  LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const std::string &reason,
80  bool gen_crash_diag = true);
81  LLVM_ATTRIBUTE_NORETURN void report_fatal_error(StringRef reason,
82  bool gen_crash_diag = true);
83  LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const Twine &reason,
84  bool gen_crash_diag = true);
85 
86  /// This function calls abort(), and prints the optional message to stderr.
87  /// Use the llvm_unreachable macro (that adds location info), instead of
88  /// calling this function directly.
90  const char *file=0,
91  unsigned line=0);
92 }
93 
94 /// Marks that the current location is not supposed to be reachable.
95 /// In !NDEBUG builds, prints the message and location info to stderr.
96 /// In NDEBUG builds, becomes an optimizer hint that the current location
97 /// is not supposed to be reachable. On compilers that don't support
98 /// such hints, prints a reduced message instead.
99 ///
100 /// Use this instead of assert(0). It conveys intent more clearly and
101 /// allows compilers to omit some unnecessary code.
102 #ifndef NDEBUG
103 #define llvm_unreachable(msg) \
104  ::llvm::llvm_unreachable_internal(msg, __FILE__, __LINE__)
105 #elif defined(LLVM_BUILTIN_UNREACHABLE)
106 #define llvm_unreachable(msg) LLVM_BUILTIN_UNREACHABLE
107 #else
108 #define llvm_unreachable(msg) ::llvm::llvm_unreachable_internal()
109 #endif
110 
111 #endif
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason, bool gen_crash_diag=true)
LLVM_ATTRIBUTE_NORETURN void llvm_unreachable_internal(const char *msg=0, const char *file=0, unsigned line=0)
void(* fatal_error_handler_t)(void *user_data, const std::string &reason, bool gen_crash_diag)
An error handler callback.
Definition: ErrorHandling.h:26
void install_fatal_error_handler(fatal_error_handler_t handler, void *user_data=0)
void remove_fatal_error_handler()
ScopedFatalErrorHandler(fatal_error_handler_t handler, void *user_data=0)
Definition: ErrorHandling.h:61
#define LLVM_ATTRIBUTE_NORETURN
Definition: Compiler.h:274