blob: 3467fe702b60259dfe21db348c10adf529259843 [file] [log] [blame]
Torok Edwin31e24662009-07-07 17:32:34 +00001//===- lib/Support/ErrorHandling.cpp - 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 for error handling, it supersedes cerr+abort(), and
11// cerr+exit() style error handling.
12// Callbacks can be registered for these errors through this API.
13//===----------------------------------------------------------------------===//
14
Daniel Dunbar82a29b62009-07-24 07:58:10 +000015#include "llvm/ADT/Twine.h"
Torok Edwin31e24662009-07-07 17:32:34 +000016#include "llvm/Support/ErrorHandling.h"
17#include "llvm/Support/raw_ostream.h"
18#include "llvm/System/Threading.h"
19#include <cassert>
20#include <cstdlib>
21
22using namespace llvm;
23using namespace std;
24
25static llvm_error_handler_t ErrorHandler = 0;
Daniel Dunbarca15f3d2009-08-10 03:36:26 +000026static void *ErrorHandlerUserData = 0;
27
Torok Edwin31e24662009-07-07 17:32:34 +000028namespace llvm {
Daniel Dunbarca15f3d2009-08-10 03:36:26 +000029void llvm_install_error_handler(llvm_error_handler_t handler,
30 void *user_data) {
Torok Edwin31e24662009-07-07 17:32:34 +000031 assert(!llvm_is_multithreaded() &&
32 "Cannot register error handlers after starting multithreaded mode!\n");
33 assert(!ErrorHandler && "Error handler already registered!\n");
34 ErrorHandler = handler;
Daniel Dunbarca15f3d2009-08-10 03:36:26 +000035 ErrorHandlerUserData = user_data;
Torok Edwin31e24662009-07-07 17:32:34 +000036}
37
Dan Gohmana9ad0412009-08-12 22:10:57 +000038void llvm_remove_error_handler() {
Torok Edwin31e24662009-07-07 17:32:34 +000039 ErrorHandler = 0;
40}
41
Daniel Dunbar82a29b62009-07-24 07:58:10 +000042void llvm_report_error(const char *reason) {
43 llvm_report_error(Twine(reason));
44}
45
Torok Edwinf3612382009-07-07 17:39:53 +000046void llvm_report_error(const std::string &reason) {
Daniel Dunbar82a29b62009-07-24 07:58:10 +000047 llvm_report_error(Twine(reason));
48}
49
50void llvm_report_error(const Twine &reason) {
Torok Edwin31e24662009-07-07 17:32:34 +000051 if (!ErrorHandler) {
52 errs() << "LLVM ERROR: " << reason << "\n";
53 } else {
Daniel Dunbarca15f3d2009-08-10 03:36:26 +000054 ErrorHandler(ErrorHandlerUserData, reason.str());
Torok Edwin31e24662009-07-07 17:32:34 +000055 }
56 exit(1);
57}
58
Daniel Dunbar82a29b62009-07-24 07:58:10 +000059void llvm_unreachable_internal(const char *msg, const char *file,
60 unsigned line) {
Torok Edwinc25e7582009-07-11 20:10:48 +000061 if (msg)
62 errs() << msg << "\n";
Torok Edwin93990d72009-07-14 12:49:22 +000063 errs() << "UNREACHABLE executed";
64 if (file)
65 errs() << " at " << file << ":" << line;
66 errs() << "!\n";
Torok Edwin31e24662009-07-07 17:32:34 +000067 abort();
68}
69}
70