|
| 1 | +// Author: Fotis Koutoulakis for Diffblue Ltd, 2023. |
| 2 | + |
| 3 | +#pragma once |
| 4 | + |
| 5 | +// The following type is cloning two types from the `util/exception_utils.h` and |
| 6 | +// `util/invariant.h` files. |
| 7 | +// |
| 8 | +// The reason we need to do this is as follows: We have a fundamental constraint |
| 9 | +// in that we don't want to export internal headers to the clients, and our |
| 10 | +// current build system architecture on the C++ end doesn't allow us to do so. |
| 11 | +// |
| 12 | +// At the same time, we want to allow the Rust API to be able to catch at the shim |
| 13 | +// level the errors generated within CBMC, which are C++ types (and subtypes of |
| 14 | +// those), and so because of the mechanism that cxx.rs uses, we need to have the |
| 15 | +// types present at compilation time (an incomplete type won't do - I've tried). |
| 16 | +// |
| 17 | +// This is the best way that we have currently to be have the type definitions |
| 18 | +// around so that the exception handling code knows what our exceptions look |
| 19 | +// like (especially given that they don't inherit from `std::exception`), so |
| 20 | +// that our system compiles and is functional, without needing include chains |
| 21 | +// outside of the API implementation (which we can't expose as well). |
| 22 | + |
| 23 | +// This should mirror the definition in `util/invariant.h`. |
| 24 | +class invariant_failedt |
| 25 | +{ |
| 26 | + private: |
| 27 | + const std::string file; |
| 28 | + const std::string function; |
| 29 | + const int line; |
| 30 | + const std::string backtrace; |
| 31 | + const std::string condition; |
| 32 | + const std::string reason; |
| 33 | + |
| 34 | +public: |
| 35 | + virtual ~invariant_failedt() = default; |
| 36 | + |
| 37 | + virtual std::string what() const noexcept; |
| 38 | + |
| 39 | + invariant_failedt( |
| 40 | + const std::string &_file, |
| 41 | + const std::string &_function, |
| 42 | + int _line, |
| 43 | + const std::string &_backtrace, |
| 44 | + const std::string &_condition, |
| 45 | + const std::string &_reason) |
| 46 | + : file(_file), |
| 47 | + function(_function), |
| 48 | + line(_line), |
| 49 | + backtrace(_backtrace), |
| 50 | + condition(_condition), |
| 51 | + reason(_reason) |
| 52 | + { |
| 53 | + } |
| 54 | +}; |
| 55 | + |
| 56 | +// This is needed here because the original definition is in the file |
| 57 | +// <util/exception_utils.h> which is including <util/source_location.h>, which |
| 58 | +// being an `irep` is a no-go for our needs as we will need to expose internal |
| 59 | +// headers as well. |
| 60 | +class cprover_exception_baset |
| 61 | +{ |
| 62 | +public: |
| 63 | + /// A human readable description of what went wrong. |
| 64 | + /// For readability, implementors should not add a leading |
| 65 | + /// or trailing newline to this description. |
| 66 | + virtual std::string what() const; |
| 67 | + virtual ~cprover_exception_baset() = default; |
| 68 | + |
| 69 | +protected: |
| 70 | + /// This constructor is marked protected to ensure this class isn't used |
| 71 | + /// directly. Deriving classes should be used to more precisely describe the |
| 72 | + /// problem that occurred. |
| 73 | + explicit cprover_exception_baset(std::string reason) |
| 74 | + : reason(std::move(reason)) |
| 75 | + { |
| 76 | + } |
| 77 | + |
| 78 | + /// The reason this exception was generated. This is the string returned by |
| 79 | + /// `what()` unless that method is overridden |
| 80 | + std::string reason; |
| 81 | +}; |
0 commit comments