Skip to content

Commit e95ce4e

Browse files
committed
Include exception base classes in separate file to avoid depending on internal headers
1 parent 761d62e commit e95ce4e

File tree

3 files changed

+86
-7
lines changed

3 files changed

+86
-7
lines changed

src/libcprover-rust/include/c_api.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22

33
#pragma once
44

5-
#include <util/exception_utils.h>
6-
75
#include <memory>
6+
#include <string>
87

98
// NOLINTNEXTLINE(build/include)
109
#include "rust/cxx.h"
10+
// NOLINTNEXTLINE(build/include)
11+
#include "include/c_errors.h"
1112

1213
struct api_sessiont;
1314

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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+
};

src/libcprover-rust/src/c_api.cc

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@
33
// NOLINTNEXTLINE(build/include)
44
#include "include/c_api.h"
55

6-
#include <util/invariant.h>
7-
#include <util/make_unique.h>
8-
9-
#include <libcprover-cpp/api.h>
6+
#include <cprover/api.h>
107

118
#include <algorithm>
129
#include <cassert>
@@ -29,7 +26,7 @@ _translate_vector_of_string(rust::Vec<rust::String> elements)
2926
std::back_inserter(*stdv),
3027
[](rust::String elem) { return std::string(elem); });
3128

32-
POSTCONDITION(elements.size() == stdv->size());
29+
assert(elements.size() == stdv->size());
3330
return *stdv;
3431
}
3532

0 commit comments

Comments
 (0)