diff options
| author | jsmall-nvidia <jsmall@nvidia.com> | 2021-10-04 14:15:51 -0400 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-10-04 14:15:51 -0400 |
| commit | 97bb82ebcdf8f1391b9d93b5a8d7b1dfc4e88e52 (patch) | |
| tree | f120ba282cbea96d23ed179737984a4610d3b520 /source/core/slang-signal.cpp | |
| parent | b3dfe383c6d31ff3dbd76dcfb32de8d536382f3e (diff) | |
Removing exceptions from core/compiler-core (#1953)
* #include an absolute path didn't work - because paths were taken to always be relative.
* Refactor Stream. Working on all tests.
* Split out CharEncode.
* Make method names lower camel.
m_prefix in Writer/Reader
* Tidy up around CharEncode interface.
* Small improvements around encode/decode.
* Better use of types.
* Remove readLine from TextReader.
* Remove exceptions from Stream/Text handling.
* Fix some typos.
* Fix tabbing.
* Fix missing override.
* Remove remaining exception throw/catch via using signal mechanism.
* Remove exceptions that are not used anymore.
* Document the Stream interface.
* Remove index for decoding 'get byte' function.
* Fix CharReader -> ByteReader.
Diffstat (limited to 'source/core/slang-signal.cpp')
| -rw-r--r-- | source/core/slang-signal.cpp | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/source/core/slang-signal.cpp b/source/core/slang-signal.cpp new file mode 100644 index 000000000..9641fe5f7 --- /dev/null +++ b/source/core/slang-signal.cpp @@ -0,0 +1,69 @@ +#include "slang-signal.h" + +#include "slang-exception.h" + +#include "stdio.h" + +namespace Slang +{ + +static const char* _getSignalTypeAsText(SignalType type) +{ + switch (type) + { + case SignalType::AssertFailure: return "assert failure"; + case SignalType::Unimplemented: return "unimplemented"; + case SignalType::Unreachable: return "hit unreachable code"; + case SignalType::Unexpected: return "unexpected"; + case SignalType::InvalidOperation: return "invalid operation"; + case SignalType::AbortCompilation: return "abort compilation"; + default: return "unhandled"; + } +} + +String _getMessage(SignalType type, char const* message) +{ + StringBuilder buf; + const char* const typeText = _getSignalTypeAsText(type); + buf << typeText; + if (message) + { + buf << ": " << message; + } + + return buf.ProduceString(); +} + +// One point of having as a single function is a choke point both for handling (allowing different +// handling scenarios) as well as a choke point to set a breakpoint to catch 'signal' types +SLANG_RETURN_NEVER void handleSignal(SignalType type, char const* message) +{ + StringBuilder buf; + const char*const typeText = _getSignalTypeAsText(type); + buf << typeText << ": " << message; + + // Can be useful to enable during debug when problem is on CI + if (false) + { + printf("%s\n", _getMessage(type, message).getBuffer()); + } + +#if SLANG_HAS_EXCEPTIONS + switch (type) + { + case SignalType::InvalidOperation: throw InvalidOperationException(_getMessage(type, message)); + case SignalType::AbortCompilation: throw AbortCompilationException(); + default: throw InternalError(_getMessage(type, message)); + } +#else + // Attempt to drop out into the debugger. If a debugger isn't attached this will likely crash - which is probably the best + // we can do. + + SLANG_BREAKPOINT(0); + + // 'panic'. Exit with an error code as we can't throw or catch. + exit(-1); +#endif +} + +} |
