95b01095c1b25c89ae98ad52a22cf60b6043e15f
[oota-llvm.git] / include / llvm / Support / ErrorHandling.h
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/Support/Compiler.h"
19 #include "llvm/ADT/StringRef.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
29   /// install_fatal_error_handler - Installs a new error handler to be used
30   /// whenever a serious (non-recoverable) error is encountered by LLVM.
31   ///
32   /// If you are using llvm_start_multithreaded, you should register the handler
33   /// before doing that.
34   ///
35   /// If no error handler is installed the default is to print the error message
36   /// to stderr, and call exit(1).  If an error handler is installed then it is
37   /// the handler's responsibility to log the message, it will no longer be
38   /// printed to stderr.  If the error handler returns, then exit(1) will be
39   /// called.
40   ///
41   /// It is dangerous to naively use an error handler which throws an exception.
42   /// Even though some applications desire to gracefully recover from arbitrary
43   /// faults, blindly throwing exceptions through unfamiliar code isn't a way to
44   /// achieve this.
45   ///
46   /// \param user_data - An argument which will be passed to the install error
47   /// handler.
48   void install_fatal_error_handler(fatal_error_handler_t handler,
49                                    void *user_data = 0);
50
51   /// Restores default error handling behaviour.
52   /// This must not be called between llvm_start_multithreaded() and
53   /// llvm_stop_multithreaded().
54   void remove_fatal_error_handler();
55
56   /// ScopedFatalErrorHandler - This is a simple helper class which just
57   /// calls install_fatal_error_handler in its constructor and
58   /// remove_fatal_error_handler in its destructor.
59   struct ScopedFatalErrorHandler {
60     explicit ScopedFatalErrorHandler(fatal_error_handler_t handler,
61                                      void *user_data = 0) {
62       install_fatal_error_handler(handler, user_data);
63     }
64
65     ~ScopedFatalErrorHandler() { remove_fatal_error_handler(); }
66   };
67
68   /// Reports a serious error, calling any installed error handler. These
69   /// functions are intended to be used for error conditions which are outside
70   /// the control of the compiler (I/O errors, invalid user input, etc.)
71   ///
72   /// If no error handler is installed the default is to print the message to
73   /// standard error, followed by a newline.
74   /// After the error handler is called this function will call exit(1), it 
75   /// does not return.
76   LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const char *reason);
77   LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const std::string &reason);
78   LLVM_ATTRIBUTE_NORETURN void report_fatal_error(StringRef reason);
79   LLVM_ATTRIBUTE_NORETURN void report_fatal_error(const Twine &reason);
80
81   /// This function calls abort(), and prints the optional message to stderr.
82   /// Use the llvm_unreachable macro (that adds location info), instead of
83   /// calling this function directly.
84   LLVM_ATTRIBUTE_NORETURN void llvm_unreachable_internal(const char *msg=0,
85                                                          const char *file=0,
86                                                          unsigned line=0);
87 }
88
89 /// Marks that the current location is not supposed to be reachable.
90 /// In !NDEBUG builds, prints the message and location info to stderr.
91 /// In NDEBUG builds, becomes an optimizer hint that the current location
92 /// is not supposed to be reachable.  On compilers that don't support
93 /// such hints, prints a reduced message instead.
94 ///
95 /// Use this instead of assert(0).  It conveys intent more clearly and
96 /// allows compilers to omit some unnecessary code.
97 #ifndef NDEBUG
98 #define llvm_unreachable(msg) \
99   ::llvm::llvm_unreachable_internal(msg, __FILE__, __LINE__)
100 #elif defined(LLVM_BUILTIN_UNREACHABLE)
101 #define llvm_unreachable(msg) LLVM_BUILTIN_UNREACHABLE
102 #else
103 #define llvm_unreachable(msg) ::llvm::llvm_unreachable_internal()
104 #endif
105
106 #endif