LLVM API Documentation
00001 //===- lib/Support/ErrorHandling.cpp - 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 for error handling, it supersedes cerr+abort(), and 00011 // cerr+exit() style error handling. 00012 // Callbacks can be registered for these errors through this API. 00013 //===----------------------------------------------------------------------===// 00014 00015 #include "llvm/ADT/Twine.h" 00016 #include "llvm/Support/ErrorHandling.h" 00017 #include "llvm/Support/raw_ostream.h" 00018 #include "llvm/System/Threading.h" 00019 #include <cassert> 00020 #include <cstdlib> 00021 00022 using namespace llvm; 00023 using namespace std; 00024 00025 static llvm_error_handler_t ErrorHandler = 0; 00026 static void *ErrorHandlerUserData = 0; 00027 00028 namespace llvm { 00029 void llvm_install_error_handler(llvm_error_handler_t handler, 00030 void *user_data) { 00031 assert(!llvm_is_multithreaded() && 00032 "Cannot register error handlers after starting multithreaded mode!\n"); 00033 assert(!ErrorHandler && "Error handler already registered!\n"); 00034 ErrorHandler = handler; 00035 ErrorHandlerUserData = user_data; 00036 } 00037 00038 void llvm_remove_error_handler() { 00039 ErrorHandler = 0; 00040 } 00041 00042 void llvm_report_error(const char *reason) { 00043 llvm_report_error(Twine(reason)); 00044 } 00045 00046 void llvm_report_error(const std::string &reason) { 00047 llvm_report_error(Twine(reason)); 00048 } 00049 00050 void llvm_report_error(const Twine &reason) { 00051 if (!ErrorHandler) { 00052 errs() << "LLVM ERROR: " << reason << "\n"; 00053 } else { 00054 ErrorHandler(ErrorHandlerUserData, reason.str()); 00055 } 00056 exit(1); 00057 } 00058 00059 void llvm_unreachable_internal(const char *msg, const char *file, 00060 unsigned line) { 00061 // This code intentionally doesn't call the ErrorHandler callback, because 00062 // llvm_unreachable is intended to be used to indicate "impossible" 00063 // situations, and not legitimate runtime errors. 00064 if (msg) 00065 errs() << msg << "\n"; 00066 errs() << "UNREACHABLE executed"; 00067 if (file) 00068 errs() << " at " << file << ":" << line; 00069 errs() << "!\n"; 00070 abort(); 00071 } 00072 } 00073