blob: 56a171cb7d374d37d0f6ae74b03ab0d4c3170474 [file] [log] [blame]
Chris Lattnerb0094c22010-04-07 23:12:29 +00001//===- lib/Support/ErrorHandling.cpp - Callbacks for errors ---------------===//
Torok Edwin31e24662009-07-07 17:32:34 +00002//
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//
Chris Lattnerb0094c22010-04-07 23:12:29 +000010// This file defines an API used to indicate fatal error conditions. Non-fatal
11// errors (most of them) should be handled through LLVMContext.
12//
Torok Edwin31e24662009-07-07 17:32:34 +000013//===----------------------------------------------------------------------===//
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>
Torok Edwin31e24662009-07-07 17:32:34 +000022using namespace llvm;
23using namespace std;
24
Chris Lattnerb0094c22010-04-07 23:12:29 +000025static fatal_error_handler_t ErrorHandler = 0;
Daniel Dunbarca15f3d2009-08-10 03:36:26 +000026static void *ErrorHandlerUserData = 0;
27
Chris Lattnerb0094c22010-04-07 23:12:29 +000028void llvm::install_fatal_error_handler(fatal_error_handler_t handler,
29 void *user_data) {
Torok Edwin31e24662009-07-07 17:32:34 +000030 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;
Daniel Dunbarca15f3d2009-08-10 03:36:26 +000034 ErrorHandlerUserData = user_data;
Torok Edwin31e24662009-07-07 17:32:34 +000035}
36
Chris Lattnerb0094c22010-04-07 23:12:29 +000037void llvm::remove_fatal_error_handler() {
Torok Edwin31e24662009-07-07 17:32:34 +000038 ErrorHandler = 0;
39}
40
Chris Lattnerb0094c22010-04-07 23:12:29 +000041void llvm::report_fatal_error(const char *reason) {
Chris Lattner75361b62010-04-07 22:58:41 +000042 report_fatal_error(Twine(reason));
Daniel Dunbar82a29b62009-07-24 07:58:10 +000043}
44
Chris Lattnerb0094c22010-04-07 23:12:29 +000045void llvm::report_fatal_error(const std::string &reason) {
Chris Lattner75361b62010-04-07 22:58:41 +000046 report_fatal_error(Twine(reason));
Daniel Dunbar82a29b62009-07-24 07:58:10 +000047}
48
Chris Lattnerb0094c22010-04-07 23:12:29 +000049void llvm::report_fatal_error(const Twine &reason) {
Torok Edwin31e24662009-07-07 17:32:34 +000050 if (!ErrorHandler) {
51 errs() << "LLVM ERROR: " << reason << "\n";
52 } else {
Daniel Dunbarca15f3d2009-08-10 03:36:26 +000053 ErrorHandler(ErrorHandlerUserData, reason.str());
Torok Edwin31e24662009-07-07 17:32:34 +000054 }
55 exit(1);
56}
57
Chris Lattnerb0094c22010-04-07 23:12:29 +000058void llvm::llvm_unreachable_internal(const char *msg, const char *file,
59 unsigned line) {
Dan Gohman073f5b62009-08-20 17:15:19 +000060 // 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.
Torok Edwinc25e7582009-07-11 20:10:48 +000063 if (msg)
David Greeneaf6c8cc2010-01-05 01:28:29 +000064 dbgs() << msg << "\n";
65 dbgs() << "UNREACHABLE executed";
Torok Edwin93990d72009-07-14 12:49:22 +000066 if (file)
David Greeneaf6c8cc2010-01-05 01:28:29 +000067 dbgs() << " at " << file << ":" << line;
68 dbgs() << "!\n";
Torok Edwin31e24662009-07-07 17:32:34 +000069 abort();
70}