yum-mirror/slang

Making it easier to work with shaders

git clone https://git.yummers.dev/yum-mirror/slang

Simon KallweitAdd API for getting last internal error message (#5772)d4136c934

master
2.2 KiB87 linesraw
1#include "slang-signal.h"
2
3#include "slang-exception.h"
4#include "stdio.h"
5
6namespace Slang
7{
8
9thread_local String g_lastSignalMessage;
10
11static const char* _getSignalTypeAsText(SignalType type)
12{
13    switch (type)
14    {
15    case SignalType::AssertFailure:
16        return "assert failure";
17    case SignalType::Unimplemented:
18        return "unimplemented";
19    case SignalType::Unreachable:
20        return "hit unreachable code";
21    case SignalType::Unexpected:
22        return "unexpected";
23    case SignalType::InvalidOperation:
24        return "invalid operation";
25    case SignalType::AbortCompilation:
26        return "abort compilation";
27    default:
28        return "unhandled";
29    }
30}
31
32String _getMessage(SignalType type, char const* message)
33{
34    StringBuilder buf;
35    const char* const typeText = _getSignalTypeAsText(type);
36    buf << typeText;
37    if (message)
38    {
39        buf << ": " << message;
40    }
41
42    return buf.produceString();
43}
44
45// One point of having as a single function is a choke point both for handling (allowing different
46// handling scenarios) as well as a choke point to set a breakpoint to catch 'signal' types
47[[noreturn]] void handleSignal(SignalType type, char const* message)
48{
49    StringBuilder buf;
50    const char* const typeText = _getSignalTypeAsText(type);
51    buf << typeText << ": " << message;
52
53    // Can be useful to enable during debug when problem is on CI
54    if (false)
55    {
56        printf("%s\n", _getMessage(type, message).getBuffer());
57    }
58
59    g_lastSignalMessage = _getMessage(type, message);
60
61#if SLANG_HAS_EXCEPTIONS
62    switch (type)
63    {
64    case SignalType::InvalidOperation:
65        throw InvalidOperationException(_getMessage(type, message));
66    case SignalType::AbortCompilation:
67        throw AbortCompilationException(_getMessage(type, message));
68    default:
69        throw InternalError(_getMessage(type, message));
70    }
71#else
72    // Attempt to drop out into the debugger. If a debugger isn't attached this will likely crash -
73    // which is probably the best we can do.
74
75    SLANG_BREAKPOINT(0);
76
77    // 'panic'. Exit with an error code as we can't throw or catch.
78    exit(-1);
79#endif
80}
81
82const char* getLastSignalMessage()
83{
84    return g_lastSignalMessage.getBuffer();
85}
86
87} // namespace Slang