Revert "System: Add SwapByteOrder and update Support/MathExtras.h to use it."
[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 <string>
20
21 namespace llvm {
22   class Twine;
23
24   /// An error handler callback.
25   typedef void (*fatal_error_handler_t)(void *user_data,
26                                         const std::string& reason);
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 you are using llvm_start_multithreaded, you should register the handler
32   /// before doing that.
33   ///
34   /// If no error handler is installed the default is to print the error message
35   /// to stderr, and call exit(1).  If an error handler is installed then it is
36   /// the handler's responsibility to log the message, it will no longer be
37   /// printed to stderr.  If the error handler returns, then exit(1) will be
38   /// called.
39   ///
40   /// It is dangerous to naively use an error handler which throws an exception.
41   /// Even though some applications desire to gracefully recover from arbitrary
42   /// faults, blindly throwing exceptions through unfamiliar code isn't a way to
43   /// achieve this.
44   ///
45   /// \param user_data - An argument which will be passed to the install error
46   /// handler.
47   void install_fatal_error_handler(fatal_error_handler_t handler,
48                                    void *user_data = 0);
49
50   /// Restores default error handling behaviour.
51   /// This must not be called between llvm_start_multithreaded() and
52   /// llvm_stop_multithreaded().
53   void remove_fatal_error_handler();
54
55   /// ScopedFatalErrorHandler - This is a simple helper class which just
56   /// calls install_fatal_error_handler in its constructor and
57   /// remove_fatal_error_handler in its destructor.
58   struct ScopedFatalErrorHandler {
59     explicit ScopedFatalErrorHandler(fatal_error_handler_t handler,
60                                      void *user_data = 0) {
61       install_fatal_error_handler(handler, user_data);
62     }
63
64     ~ScopedFatalErrorHandler() { remove_fatal_error_handler(); }
65   };
66
67   /// Reports a serious error, calling any installed error handler. These
68   /// functions are intended to be used for error conditions which are outside
69   /// the control of the compiler (I/O errors, invalid user input, etc.)
70   ///
71   /// If no error handler is installed the default is to print the message to
72   /// standard error, followed by a newline.
73   /// After the error handler is called this function will call exit(1), it 
74   /// does not return.
75   NORETURN void report_fatal_error(const char *reason);
76   NORETURN void report_fatal_error(const std::string &reason);
77   NORETURN void report_fatal_error(const Twine &reason);
78
79   /// This function calls abort(), and prints the optional message to stderr.
80   /// Use the llvm_unreachable macro (that adds location info), instead of
81   /// calling this function directly.
82   NORETURN void llvm_unreachable_internal(const char *msg=0,
83                                           const char *file=0, unsigned line=0);
84 }
85
86 /// Prints the message and location info to stderr in !NDEBUG builds.
87 /// This is intended to be used for "impossible" situations that imply
88 /// a bug in the compiler.
89 ///
90 /// In NDEBUG mode it only prints "UNREACHABLE executed".
91 /// Use this instead of assert(0), so that the compiler knows this path
92 /// is not reachable even for NDEBUG builds.
93 #ifndef NDEBUG
94 #define llvm_unreachable(msg) \
95   ::llvm::llvm_unreachable_internal(msg, __FILE__, __LINE__)
96 #else
97 #define llvm_unreachable(msg) ::llvm::llvm_unreachable_internal()
98 #endif
99
100 #endif