blob: d60dc1d2ad200b0e728279f9ba3a3cd0a68fbfb5 [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;
26namespace llvm {
27void llvm_install_error_handler(llvm_error_handler_t handler) {
28 assert(!llvm_is_multithreaded() &&
29 "Cannot register error handlers after starting multithreaded mode!\n");
30 assert(!ErrorHandler && "Error handler already registered!\n");
31 ErrorHandler = handler;
32}
33
34void llvm_remove_error_handler(void) {
35 ErrorHandler = 0;
36}
37
Daniel Dunbar82a29b62009-07-24 07:58:10 +000038void llvm_report_error(const char *reason) {
39 llvm_report_error(Twine(reason));
40}
41
Torok Edwinf3612382009-07-07 17:39:53 +000042void llvm_report_error(const std::string &reason) {
Daniel Dunbar82a29b62009-07-24 07:58:10 +000043 llvm_report_error(Twine(reason));
44}
45
46void llvm_report_error(const Twine &reason) {
Torok Edwin31e24662009-07-07 17:32:34 +000047 if (!ErrorHandler) {
48 errs() << "LLVM ERROR: " << reason << "\n";
49 } else {
Daniel Dunbar82a29b62009-07-24 07:58:10 +000050 ErrorHandler(reason.str());
Torok Edwin31e24662009-07-07 17:32:34 +000051 }
52 exit(1);
53}
54
Daniel Dunbar82a29b62009-07-24 07:58:10 +000055void llvm_unreachable_internal(const char *msg, const char *file,
56 unsigned line) {
Torok Edwinc25e7582009-07-11 20:10:48 +000057 if (msg)
58 errs() << msg << "\n";
Torok Edwin93990d72009-07-14 12:49:22 +000059 errs() << "UNREACHABLE executed";
60 if (file)
61 errs() << " at " << file << ":" << line;
62 errs() << "!\n";
Torok Edwin31e24662009-07-07 17:32:34 +000063 abort();
64}
65}
66