Skip to content

Commit ff03dd5

Browse files
authored
Merge pull request #7438 from esteffin/esteffin/add-validate-drop-unused-functions-to-api
Expose two goto-instrument passes that Kani uses to C++ API
2 parents 8b6f0f5 + ca13a7a commit ff03dd5

File tree

5 files changed

+84
-7
lines changed

5 files changed

+84
-7
lines changed

regression/libcprover-cpp/call_bmc.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
#include <libcprover-cpp/api.h>
77
#include <libcprover-cpp/options.h>
88

9+
#include "goto_model.h"
10+
911
#include <iostream>
1012
#include <vector>
1113

@@ -26,7 +28,10 @@ int main(int argc, char *argv[])
2628
std::vector<std::string> arguments(argv + 1, argv + argc);
2729

2830
// Create API options object, to pass to initialiser of API object.
29-
auto api_options = api_optionst::create().simplify(false);
31+
auto api_options = api_optionst::create()
32+
.simplify(false)
33+
.validate_goto_model(true)
34+
.drop_unused_functions(true);
3035

3136
// Initialise API dependencies and global configuration in one step.
3237
api_sessiont api(api_options);
@@ -36,6 +41,19 @@ int main(int argc, char *argv[])
3641
api.load_model_from_files(arguments);
3742

3843
std::cout << "Successfully initialised goto_model" << std::endl;
44+
45+
// Demonstrate the validation of an already loaded goto-model
46+
api.validate_goto_model();
47+
48+
std::cout << "Successfully validated goto_model" << std::endl;
49+
50+
// Demonstrate the dropping of unused functions from an already loaded
51+
// goto-model
52+
api.drop_unused_functions();
53+
54+
std::cout << "Successfully dropped unused functions from goto_model"
55+
<< std::endl;
56+
3957
return 0;
4058
}
4159
catch(const invalid_command_line_argument_exceptiont &e)

src/libcprover-cpp/api.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <util/cmdline.h>
66
#include <util/config.h>
7+
#include <util/invariant.h>
78
#include <util/message.h>
89
#include <util/options.h>
910
#include <util/ui_message.h>
@@ -13,6 +14,7 @@
1314
#include <goto-programs/link_to_library.h>
1415
#include <goto-programs/process_goto_program.h>
1516
#include <goto-programs/remove_skip.h>
17+
#include <goto-programs/remove_unused_functions.h>
1618
#include <goto-programs/set_properties.h>
1719

1820
#include <ansi-c/ansi_c_language.h>
@@ -159,3 +161,32 @@ void api_sessiont::verify_model()
159161
(void)verifier();
160162
verifier.report();
161163
}
164+
165+
void api_sessiont::drop_unused_functions()
166+
{
167+
INVARIANT(
168+
implementation->model != nullptr,
169+
"Model has not been loaded. Load it first using "
170+
"api::load_model_from_files");
171+
172+
messaget log{*implementation->message_handler};
173+
log.status() << "Performing instrumentation pass: dropping unused functions"
174+
<< messaget::eom;
175+
176+
remove_unused_functions(
177+
*implementation->model, *implementation->message_handler);
178+
}
179+
180+
void api_sessiont::validate_goto_model()
181+
{
182+
INVARIANT(
183+
implementation->model != nullptr,
184+
"Model has not been loaded. Load it first using "
185+
"api::load_model_from_files");
186+
187+
messaget log{*implementation->message_handler};
188+
log.status() << "Validating consistency of goto-model supplied to API session"
189+
<< messaget::eom;
190+
191+
implementation->model->validate();
192+
}

src/libcprover-cpp/api.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,12 @@ struct api_sessiont
7171
/// Verify previously loaded model.
7272
void verify_model();
7373

74+
/// Drop unused functions from the loaded goto_model simplifying it
75+
void drop_unused_functions();
76+
77+
/// Validate the loaded goto model
78+
void validate_goto_model();
79+
7480
private:
7581
std::unique_ptr<api_session_implementationt> implementation;
7682
};

src/libcprover-cpp/options.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,6 @@ api_optionst api_optionst::create()
1414
return api_optionst{};
1515
}
1616

17-
api_optionst &api_optionst::simplify(bool on)
18-
{
19-
simplify_enabled = on;
20-
return *this;
21-
}
22-
2317
static std::unique_ptr<optionst> make_internal_default_options()
2418
{
2519
std::unique_ptr<optionst> options = util_make_unique<optionst>();
@@ -33,6 +27,24 @@ static std::unique_ptr<optionst> make_internal_default_options()
3327
return options;
3428
}
3529

30+
api_optionst &api_optionst::simplify(bool on)
31+
{
32+
simplify_enabled = on;
33+
return *this;
34+
}
35+
36+
api_optionst &api_optionst::drop_unused_functions(bool on)
37+
{
38+
drop_unused_functions_enabled = on;
39+
return *this;
40+
}
41+
42+
api_optionst &api_optionst::validate_goto_model(bool on)
43+
{
44+
validate_goto_model_enabled = on;
45+
return *this;
46+
}
47+
3648
std::unique_ptr<optionst> api_optionst::to_engine_options() const
3749
{
3850
auto engine_options = make_internal_default_options();

src/libcprover-cpp/options.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@ class api_optionst
1212
// Options for the verification engine
1313
bool simplify_enabled;
1414

15+
// Option for dropping unused function
16+
bool drop_unused_functions_enabled;
17+
18+
// Option for validating the goto model
19+
bool validate_goto_model_enabled;
20+
1521
// Private interface methods
1622
api_optionst() = default;
1723

@@ -20,6 +26,10 @@ class api_optionst
2026

2127
api_optionst &simplify(bool on);
2228

29+
api_optionst &drop_unused_functions(bool on);
30+
31+
api_optionst &validate_goto_model(bool on);
32+
2333
std::unique_ptr<optionst> to_engine_options() const;
2434
};
2535

0 commit comments

Comments
 (0)