blob: 8bb156653a745a259a2577af45a632f289ac3d34 [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"
David Greeneaf6c8cc2010-01-05 01:28:29 +000016#include "llvm/Support/Debug.h"
Torok Edwin31e24662009-07-07 17:32:34 +000017#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
23using namespace llvm;
24using namespace std;
25
26static llvm_error_handler_t ErrorHandler = 0;
Daniel Dunbarca15f3d2009-08-10 03:36:26 +000027static void *ErrorHandlerUserData = 0;
28
Torok Edwin31e24662009-07-07 17:32:34 +000029namespace llvm {
Daniel Dunbarca15f3d2009-08-10 03:36:26 +000030void llvm_install_error_handler(llvm_error_handler_t handler,
31 void *user_data) {
Torok Edwin31e24662009-07-07 17:32:34 +000032 assert(!llvm_is_multithreaded() &&
33 "Cannot register error handlers after starting multithreaded mode!\n");
34 assert(!ErrorHandler && "Error handler already registered!\n");
35 ErrorHandler = handler;
Daniel Dunbarca15f3d2009-08-10 03:36:26 +000036 ErrorHandlerUserData = user_data;
Torok Edwin31e24662009-07-07 17:32:34 +000037}
38
Dan Gohmana9ad0412009-08-12 22:10:57 +000039void llvm_remove_error_handler() {
Torok Edwin31e24662009-07-07 17:32:34 +000040 ErrorHandler = 0;
41}
42
Daniel Dunbar82a29b62009-07-24 07:58:10 +000043void llvm_report_error(const char *reason) {
44 llvm_report_error(Twine(reason));
45}
46
Torok Edwinf3612382009-07-07 17:39:53 +000047void llvm_report_error(const std::string &reason) {
Daniel Dunbar82a29b62009-07-24 07:58:10 +000048 llvm_report_error(Twine(reason));
49}
50
51void llvm_report_error(const Twine &reason) {
Torok Edwin31e24662009-07-07 17:32:34 +000052 if (!ErrorHandler) {
53 errs() << "LLVM ERROR: " << reason << "\n";
54 } else {
Daniel Dunbarca15f3d2009-08-10 03:36:26 +000055 ErrorHandler(ErrorHandlerUserData, reason.str());
Torok Edwin31e24662009-07-07 17:32:34 +000056 }
57 exit(1);
58}
59
Daniel Dunbar82a29b62009-07-24 07:58:10 +000060void llvm_unreachable_internal(const char *msg, const char *file,
61 unsigned line) {
Dan Gohman073f5b62009-08-20 17:15:19 +000062 // This code intentionally doesn't call the ErrorHandler callback, because
63 // llvm_unreachable is intended to be used to indicate "impossible"
64 // situations, and not legitimate runtime errors.
Torok Edwinc25e7582009-07-11 20:10:48 +000065 if (msg)
David Greeneaf6c8cc2010-01-05 01:28:29 +000066 dbgs() << msg << "\n";
67 dbgs() << "UNREACHABLE executed";
Torok Edwin93990d72009-07-14 12:49:22 +000068 if (file)
David Greeneaf6c8cc2010-01-05 01:28:29 +000069 dbgs() << " at " << file << ":" << line;
70 dbgs() << "!\n";
Torok Edwin31e24662009-07-07 17:32:34 +000071 abort();
72}
73}
74