Coverage Report

Created: 2025-07-18 06:59

/src/keystone/llvm/lib/Support/ErrorHandling.cpp
Line
Count
Source (jump to first uncovered line)
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/SmallVector.h"
16
#include "llvm/ADT/Twine.h"
17
#include "llvm/Config/config.h"
18
#include "llvm/Support/Debug.h"
19
#include "llvm/Support/Errc.h"
20
#include "llvm/Support/WindowsError.h"
21
#include "llvm/Support/raw_ostream.h"
22
#include <cassert>
23
#include <cstdlib>
24
25
#if defined(HAVE_UNISTD_H)
26
# include <unistd.h>
27
#endif
28
#if defined(_MSC_VER)
29
# include <io.h>
30
# include <fcntl.h>
31
#endif
32
33
using namespace llvm_ks;
34
35
0
void llvm_ks::report_fatal_error(const char *Reason, bool GenCrashDiag) {
36
0
  report_fatal_error(Twine(Reason), GenCrashDiag);
37
0
}
38
39
0
void llvm_ks::report_fatal_error(const std::string &Reason, bool GenCrashDiag) {
40
0
  report_fatal_error(Twine(Reason), GenCrashDiag);
41
0
}
42
43
0
void llvm_ks::report_fatal_error(StringRef Reason, bool GenCrashDiag) {
44
0
  report_fatal_error(Twine(Reason), GenCrashDiag);
45
0
}
46
47
0
void llvm_ks::report_fatal_error(const Twine &Reason, bool GenCrashDiag) {
48
  // Blast the result out to stderr.  We don't try hard to make sure this
49
  // succeeds (e.g. handling EINTR) and we can't use errs() here because
50
  // raw ostreams can call report_fatal_error.
51
0
  SmallVector<char, 64> Buffer;
52
0
  raw_svector_ostream OS(Buffer);
53
0
  OS << "LLVM ERROR: " << Reason << "\n";
54
0
  StringRef MessageStr = OS.str();
55
0
  ssize_t written = ::write(2, MessageStr.data(), MessageStr.size());
56
0
  (void)written; // If something went wrong, we deliberately just give up.
57
58
  // If we reached here, we are failing ungracefully. Run the interrupt handlers
59
  // to make sure any special cleanups get done, in particular that we remove
60
  // files registered with RemoveFileOnSignal.
61
  //sys::RunInterruptHandlers();
62
63
0
  exit(1);
64
0
}
65
66
void llvm_ks::llvm_unreachable_internal(const char *msg, const char *file,
67
0
                                     unsigned line) {
68
  // This code intentionally doesn't call the ErrorHandler callback, because
69
  // llvm_unreachable is intended to be used to indicate "impossible"
70
  // situations, and not legitimate runtime errors.
71
0
  abort();
72
0
#ifdef LLVM_BUILTIN_UNREACHABLE
73
  // Windows systems and possibly others don't declare abort() to be noreturn,
74
  // so use the unreachable builtin to avoid a Clang self-host warning.
75
0
  LLVM_BUILTIN_UNREACHABLE;
76
0
#endif
77
0
}
78
79
#ifdef LLVM_ON_WIN32
80
81
#include <winerror.h>
82
83
// I'd rather not double the line count of the following.
84
#define MAP_ERR_TO_COND(x, y)                                                  \
85
  case x:                                                                      \
86
    return make_error_code(errc::y)
87
88
std::error_code llvm_ks::mapWindowsError(unsigned EV) {
89
  switch (EV) {
90
    MAP_ERR_TO_COND(ERROR_ACCESS_DENIED, permission_denied);
91
    MAP_ERR_TO_COND(ERROR_ALREADY_EXISTS, file_exists);
92
    MAP_ERR_TO_COND(ERROR_BAD_UNIT, no_such_device);
93
    MAP_ERR_TO_COND(ERROR_BUFFER_OVERFLOW, filename_too_long);
94
    MAP_ERR_TO_COND(ERROR_BUSY, device_or_resource_busy);
95
    MAP_ERR_TO_COND(ERROR_BUSY_DRIVE, device_or_resource_busy);
96
    MAP_ERR_TO_COND(ERROR_CANNOT_MAKE, permission_denied);
97
    MAP_ERR_TO_COND(ERROR_CANTOPEN, io_error);
98
    MAP_ERR_TO_COND(ERROR_CANTREAD, io_error);
99
    MAP_ERR_TO_COND(ERROR_CANTWRITE, io_error);
100
    MAP_ERR_TO_COND(ERROR_CURRENT_DIRECTORY, permission_denied);
101
    MAP_ERR_TO_COND(ERROR_DEV_NOT_EXIST, no_such_device);
102
    MAP_ERR_TO_COND(ERROR_DEVICE_IN_USE, device_or_resource_busy);
103
    MAP_ERR_TO_COND(ERROR_DIR_NOT_EMPTY, directory_not_empty);
104
    MAP_ERR_TO_COND(ERROR_DIRECTORY, invalid_argument);
105
    MAP_ERR_TO_COND(ERROR_DISK_FULL, no_space_on_device);
106
    MAP_ERR_TO_COND(ERROR_FILE_EXISTS, file_exists);
107
    MAP_ERR_TO_COND(ERROR_FILE_NOT_FOUND, no_such_file_or_directory);
108
    MAP_ERR_TO_COND(ERROR_HANDLE_DISK_FULL, no_space_on_device);
109
    MAP_ERR_TO_COND(ERROR_INVALID_ACCESS, permission_denied);
110
    MAP_ERR_TO_COND(ERROR_INVALID_DRIVE, no_such_device);
111
    MAP_ERR_TO_COND(ERROR_INVALID_FUNCTION, function_not_supported);
112
    MAP_ERR_TO_COND(ERROR_INVALID_HANDLE, invalid_argument);
113
    MAP_ERR_TO_COND(ERROR_INVALID_NAME, invalid_argument);
114
    MAP_ERR_TO_COND(ERROR_LOCK_VIOLATION, no_lock_available);
115
    MAP_ERR_TO_COND(ERROR_LOCKED, no_lock_available);
116
    MAP_ERR_TO_COND(ERROR_NEGATIVE_SEEK, invalid_argument);
117
    MAP_ERR_TO_COND(ERROR_NOACCESS, permission_denied);
118
    MAP_ERR_TO_COND(ERROR_NOT_ENOUGH_MEMORY, not_enough_memory);
119
    MAP_ERR_TO_COND(ERROR_NOT_READY, resource_unavailable_try_again);
120
    MAP_ERR_TO_COND(ERROR_OPEN_FAILED, io_error);
121
    MAP_ERR_TO_COND(ERROR_OPEN_FILES, device_or_resource_busy);
122
    MAP_ERR_TO_COND(ERROR_OUTOFMEMORY, not_enough_memory);
123
    MAP_ERR_TO_COND(ERROR_PATH_NOT_FOUND, no_such_file_or_directory);
124
    MAP_ERR_TO_COND(ERROR_BAD_NETPATH, no_such_file_or_directory);
125
    MAP_ERR_TO_COND(ERROR_READ_FAULT, io_error);
126
    MAP_ERR_TO_COND(ERROR_RETRY, resource_unavailable_try_again);
127
    MAP_ERR_TO_COND(ERROR_SEEK, io_error);
128
    MAP_ERR_TO_COND(ERROR_SHARING_VIOLATION, permission_denied);
129
    MAP_ERR_TO_COND(ERROR_TOO_MANY_OPEN_FILES, too_many_files_open);
130
    MAP_ERR_TO_COND(ERROR_WRITE_FAULT, io_error);
131
    MAP_ERR_TO_COND(ERROR_WRITE_PROTECT, permission_denied);
132
    MAP_ERR_TO_COND(WSAEACCES, permission_denied);
133
    MAP_ERR_TO_COND(WSAEBADF, bad_file_descriptor);
134
    MAP_ERR_TO_COND(WSAEFAULT, bad_address);
135
    MAP_ERR_TO_COND(WSAEINTR, interrupted);
136
    MAP_ERR_TO_COND(WSAEINVAL, invalid_argument);
137
    MAP_ERR_TO_COND(WSAEMFILE, too_many_files_open);
138
    MAP_ERR_TO_COND(WSAENAMETOOLONG, filename_too_long);
139
  default:
140
    return std::error_code(EV, std::system_category());
141
  }
142
}
143
144
#endif