1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
|
#include "pch.h"
#include <vcpkg/base/checks.h>
#include <vcpkg/base/stringview.h>
#include <vcpkg/base/system.debug.h>
namespace vcpkg
{
static void (*g_shutdown_handler)() = nullptr;
void Checks::register_global_shutdown_handler(void (*func)())
{
if (g_shutdown_handler)
// Setting the handler twice is a program error. Terminate.
std::abort();
g_shutdown_handler = func;
}
[[noreturn]] void Checks::final_cleanup_and_exit(const int exit_code)
{
static std::atomic<bool> have_entered{false};
if (have_entered.exchange(true))
{
#if defined(_WIN32)
::TerminateProcess(::GetCurrentProcess(), exit_code);
#else
std::terminate();
#endif
}
if (g_shutdown_handler) g_shutdown_handler();
fflush(nullptr);
#if defined(_WIN32)
::TerminateProcess(::GetCurrentProcess(), exit_code);
#endif
std::exit(exit_code);
}
[[noreturn]] void Checks::unreachable(const LineInfo& line_info)
{
System::print2(System::Color::error, "Error: Unreachable code was reached\n");
System::print2(System::Color::error, line_info, '\n'); // Always print line_info here
#ifndef NDEBUG
std::abort();
#else
final_cleanup_and_exit(EXIT_FAILURE);
#endif
}
[[noreturn]] void Checks::exit_with_code(const LineInfo& line_info, const int exit_code)
{
Debug::print(System::Color::error, line_info, '\n');
final_cleanup_and_exit(exit_code);
}
[[noreturn]] void Checks::exit_with_message(const LineInfo& line_info, StringView error_message)
{
System::print2(System::Color::error, error_message, '\n');
exit_fail(line_info);
}
void Checks::check_exit(const LineInfo& line_info, bool expression)
{
if (!expression)
{
exit_fail(line_info);
}
}
void Checks::check_exit(const LineInfo& line_info, bool expression, StringView error_message)
{
if (!expression)
{
exit_with_message(line_info, error_message);
}
}
std::string LineInfo::to_string() const
{
std::string ret;
this->to_string(ret);
return ret;
}
void LineInfo::to_string(std::string& out) const
{
out += m_file_name;
Strings::append(out, '(', m_line_number, ')');
}
namespace details
{
void exit_if_null(bool b, const LineInfo& line_info) { Checks::check_exit(line_info, b, "Value was null"); }
}
}
|