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