dccdf9b29cfcf08778b2e22056c684cb81bca204
[oota-llvm.git] / include / llvm / Support / ErrorHandling.h
1 //===- llvm/Support/ErrorHandling.h - Callbacks for errors ------*- 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 error conditions.
11 // Callbacks can be registered for these errors through this API.
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 (*llvm_error_handler_t)(void *user_data,
26                                        const std::string& reason);
27
28   /// llvm_instal_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   /// \param user_data - An argument which will be passed to the install error
41   /// handler.
42   void llvm_install_error_handler(llvm_error_handler_t handler,
43                                   void *user_data = 0);
44
45   /// Restores default error handling behaviour.
46   /// This must not be called between llvm_start_multithreaded() and
47   /// llvm_stop_multithreaded().
48   void llvm_remove_error_handler(void);
49
50   /// Reports a serious error, calling any installed error handler.
51   /// If no error handler is installed the default is to print the message to
52   /// standard error, followed by a newline.
53   /// After the error handler is called this function will call exit(1), it 
54   /// does not return.
55   void llvm_report_error(const char *reason) NORETURN;
56   void llvm_report_error(const std::string &reason) NORETURN;
57   void llvm_report_error(const Twine &reason) NORETURN;
58
59   /// This function calls abort(), and prints the optional message to stderr.
60   /// Use the llvm_unreachable macro (that adds location info), instead of
61   /// calling this function directly.
62   void llvm_unreachable_internal(const char *msg=0, const char *file=0,
63                                  unsigned line=0) NORETURN;
64 }
65
66 /// Prints the message and location info to stderr in !NDEBUG builds.
67 /// In NDEBUG mode it only prints "UNREACHABLE executed".
68 /// Use this instead of assert(0), so that the compiler knows this path
69 /// is not reachable even for NDEBUG builds.
70 #ifndef NDEBUG
71 #define llvm_unreachable(msg) llvm_unreachable_internal(msg, __FILE__, __LINE__)
72 #else
73 #define llvm_unreachable(msg) llvm_unreachable_internal()
74 #endif
75
76 #endif
77