aboutsummaryrefslogtreecommitdiff
path: root/toolsrc/src/vcpkg_Checks.cpp
blob: 95ef7c60f4fd424a26b98ac10989b498aaa0088c (plain)
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
#include "pch.h"
#include "vcpkg_Checks.h"
#include "vcpkg_System.h"
#include "vcpkglib.h"

namespace vcpkg
{
    std::string LineInfo::toString() const
    {
        return Strings::format("%s(%d)", this->file_name, this->line_number);
    }
}

namespace vcpkg::Checks
{
    __declspec(noreturn) void unreachable(const LineInfo& line_info)
    {
        System::println(System::color::error, "Error: Unreachable code was reached");
        System::println(System::color::error, line_info.toString()); // Always print line_info here
#ifndef NDEBUG
        std::abort();
#else
        exit(EXIT_FAILURE);
#endif
    }

    __declspec(noreturn) void exit_with_message(const char* errorMessage)
    {
        System::println(System::color::error, errorMessage);
        exit(EXIT_FAILURE);
    }

    __declspec(noreturn) void throw_with_message(const char* errorMessage)
    {
        throw std::runtime_error(errorMessage);
    }

    void check_throw(bool expression, const char* errorMessage)
    {
        if (!expression)
        {
            throw_with_message(errorMessage);
        }
    }

    void check_exit(bool expression)
    {
        if (!expression)
        {
            exit(EXIT_FAILURE);
        }
    }

    void check_exit(bool expression, const char* errorMessage)
    {
        if (!expression)
        {
            exit_with_message(errorMessage);
        }
    }
}