LLVM API Documentation

ErrorHandling.h

Go to the documentation of this file.
00001 //===- llvm/Support/ErrorHandling.h - Callbacks for errors ------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 // This file defines an API used to indicate error conditions.
00011 // Callbacks can be registered for these errors through this API.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_SUPPORT_ERRORHANDLING_H
00016 #define LLVM_SUPPORT_ERRORHANDLING_H
00017 
00018 #include "llvm/Support/Compiler.h"
00019 #include <string>
00020 
00021 namespace llvm {
00022   class Twine;
00023 
00024   /// An error handler callback.
00025   typedef void (*llvm_error_handler_t)(void *user_data,
00026                                        const std::string& reason);
00027 
00028   /// llvm_instal_error_handler - Installs a new error handler to be used
00029   /// whenever a serious (non-recoverable) error is encountered by LLVM.
00030   ///
00031   /// If you are using llvm_start_multithreaded, you should register the handler
00032   /// before doing that.
00033   ///
00034   /// If no error handler is installed the default is to print the error message
00035   /// to stderr, and call exit(1).  If an error handler is installed then it is
00036   /// the handler's responsibility to log the message, it will no longer be
00037   /// printed to stderr.  If the error handler returns, then exit(1) will be
00038   /// called.
00039   ///
00040   /// It is dangerous to naively use an error handler which throws an exception.
00041   /// Even though some applications desire to gracefully recover from arbitrary
00042   /// faults, blindly throwing exceptions through unfamiliar code isn't a way to
00043   /// achieve this.
00044   ///
00045   /// \param user_data - An argument which will be passed to the install error
00046   /// handler.
00047   void llvm_install_error_handler(llvm_error_handler_t handler,
00048                                   void *user_data = 0);
00049 
00050   /// Restores default error handling behaviour.
00051   /// This must not be called between llvm_start_multithreaded() and
00052   /// llvm_stop_multithreaded().
00053   void llvm_remove_error_handler();
00054 
00055   /// Reports a serious error, calling any installed error handler. These
00056   /// functions are intended to be used for error conditions which are outside
00057   /// the control of the compiler (I/O errors, invalid user input, etc.)
00058   ///
00059   /// If no error handler is installed the default is to print the message to
00060   /// standard error, followed by a newline.
00061   /// After the error handler is called this function will call exit(1), it 
00062   /// does not return.
00063   void llvm_report_error(const char *reason) NORETURN;
00064   void llvm_report_error(const std::string &reason) NORETURN;
00065   void llvm_report_error(const Twine &reason) NORETURN;
00066 
00067   /// This function calls abort(), and prints the optional message to stderr.
00068   /// Use the llvm_unreachable macro (that adds location info), instead of
00069   /// calling this function directly.
00070   void llvm_unreachable_internal(const char *msg=0, const char *file=0,
00071                                  unsigned line=0) NORETURN;
00072 }
00073 
00074 /// Prints the message and location info to stderr in !NDEBUG builds.
00075 /// This is intended to be used for "impossible" situations that imply
00076 /// a bug in the compiler.
00077 ///
00078 /// In NDEBUG mode it only prints "UNREACHABLE executed".
00079 /// Use this instead of assert(0), so that the compiler knows this path
00080 /// is not reachable even for NDEBUG builds.
00081 #ifndef NDEBUG
00082 #define llvm_unreachable(msg) llvm_unreachable_internal(msg, __FILE__, __LINE__)
00083 #else
00084 #define llvm_unreachable(msg) llvm_unreachable_internal()
00085 #endif
00086 
00087 #endif