MC/Mach-O: Mark absolute variable's appropriately, and add Mach-O support for
[oota-llvm.git] / lib / Support / ErrorHandling.cpp
1 //===- lib/Support/ErrorHandling.cpp - Callbacks for errors ---------------===//
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 #include "llvm/ADT/Twine.h"
16 #include "llvm/Support/Debug.h"
17 #include "llvm/Support/ErrorHandling.h"
18 #include "llvm/Support/raw_ostream.h"
19 #include "llvm/System/Threading.h"
20 #include <cassert>
21 #include <cstdlib>
22 using namespace llvm;
23 using namespace std;
24
25 static fatal_error_handler_t ErrorHandler = 0;
26 static void *ErrorHandlerUserData = 0;
27
28 void llvm::install_fatal_error_handler(fatal_error_handler_t handler,
29                                        void *user_data) {
30   assert(!llvm_is_multithreaded() &&
31          "Cannot register error handlers after starting multithreaded mode!\n");
32   assert(!ErrorHandler && "Error handler already registered!\n");
33   ErrorHandler = handler;
34   ErrorHandlerUserData = user_data;
35 }
36
37 void llvm::remove_fatal_error_handler() {
38   ErrorHandler = 0;
39 }
40
41 void llvm::report_fatal_error(const char *reason) {
42   report_fatal_error(Twine(reason));
43 }
44
45 void llvm::report_fatal_error(const std::string &reason) {
46   report_fatal_error(Twine(reason));
47 }
48
49 void llvm::report_fatal_error(const Twine &reason) {
50   if (!ErrorHandler) {
51     errs() << "LLVM ERROR: " << reason << "\n";
52   } else {
53     ErrorHandler(ErrorHandlerUserData, reason.str());
54   }
55   exit(1);
56 }
57
58 void llvm::llvm_unreachable_internal(const char *msg, const char *file,
59                                      unsigned line) {
60   // This code intentionally doesn't call the ErrorHandler callback, because
61   // llvm_unreachable is intended to be used to indicate "impossible"
62   // situations, and not legitimate runtime errors.
63   if (msg)
64     dbgs() << msg << "\n";
65   dbgs() << "UNREACHABLE executed";
66   if (file)
67     dbgs() << " at " << file << ":" << line;
68   dbgs() << "!\n";
69   abort();
70 }