Allow llvm_report_error to accept a Twine.
[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)(const std::string& reason);
26
27   /// Installs a new error handler: this function will be called whenever a
28   /// serious error is encountered by LLVM.
29   /// If you are using llvm_start_multithreaded, you should register the handler
30   /// before doing that.
31   ///
32   /// If no error handler is installed the default is to print the error message
33   /// to stderr, and call exit(1).
34   /// If an error handler is installed then it is the handler's responsibility
35   /// to log the message, it will no longer be printed to stderr.
36   /// If the error handler returns, then exit(1) will be called.
37   void llvm_install_error_handler(llvm_error_handler_t handler);
38
39   /// Restores default error handling behaviour.
40   /// This must not be called between llvm_start_multithreaded() and
41   /// llvm_stop_multithreaded().
42   void llvm_remove_error_handler(void);
43
44   /// Reports a serious error, calling any installed error handler.
45   /// If no error handler is installed the default is to print the message to
46   /// standard error, followed by a newline.
47   /// After the error handler is called this function will call exit(1), it 
48   /// does not return.
49   void llvm_report_error(const char *reason) NORETURN;
50   void llvm_report_error(const std::string &reason) NORETURN;
51   void llvm_report_error(const Twine &reason) NORETURN;
52
53   /// This function calls abort(), and prints the optional message to stderr.
54   /// Use the llvm_unreachable macro (that adds location info), instead of
55   /// calling this function directly.
56   void llvm_unreachable_internal(const char *msg=0, const char *file=0,
57                                  unsigned line=0) NORETURN;
58 }
59
60 /// Prints the message and location info to stderr in !NDEBUG builds.
61 /// In NDEBUG mode it only prints "UNREACHABLE executed".
62 /// Use this instead of assert(0), so that the compiler knows this path
63 /// is not reachable even for NDEBUG builds.
64 #ifndef NDEBUG
65 #define llvm_unreachable(msg) llvm_unreachable_internal(msg, __FILE__, __LINE__)
66 #else
67 #define llvm_unreachable(msg) llvm_unreachable_internal()
68 #endif
69
70 #endif
71