Skip to content

Commit ae6c9e2

Browse files
committed
Add an options object for the API
1 parent 33316ab commit ae6c9e2

File tree

6 files changed

+74
-7
lines changed

6 files changed

+74
-7
lines changed

cpp_api/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
# This step builds a binary driving the API (to be used for testing)
2-
add_executable(api-binary-driver call_bmc.cpp api.cpp)
2+
add_executable(api-binary-driver call_bmc.cpp api.cpp options.cpp)
33
target_link_libraries(api-binary-driver goto-programs util langapi ansi-c)
44

55
# This step builds the API in the form of a statically linked library (libcprover-api.a)
6-
add_library(cprover-api api.cpp)
6+
add_library(cprover-api api.cpp options.cpp)
77
target_link_libraries(cprover-api goto-programs util langapi ansi-c)
88

99
install(TARGETS cprover-api RUNTIME DESTINATION lib)

cpp_api/api.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
#include <langapi/mode.h>
1313

1414
#include <memory>
15+
#include <options.h>
1516

1617
extern configt config;
1718

@@ -25,6 +26,13 @@ cbmc_api::api_dependenciest::api_dependenciest()
2526
{
2627
}
2728

29+
cbmc_api::api_dependenciest::api_dependenciest(const optionst options)
30+
: // We don't print anything to the console as part of the API
31+
message_handler(new null_message_handlert()),
32+
options(util_make_unique<optionst>(options))
33+
{
34+
}
35+
2836
cbmc_api::api_dependenciest::~api_dependenciest() = default;
2937

3038
cbmc_api::cbmc_api() : dependencies(new api_dependenciest())
@@ -37,6 +45,17 @@ cbmc_api::cbmc_api() : dependencies(new api_dependenciest())
3745
register_language(new_ansi_c_language);
3846
}
3947

48+
cbmc_api::cbmc_api(const api_optionst &options)
49+
: dependencies(new api_dependenciest(options.to_engine_options()))
50+
{
51+
// Needed to initialise the language options correctly
52+
cmdlinet cmdline;
53+
// config is global in config.cpp
54+
config.set(cmdline);
55+
// Initialise C language mode
56+
register_language(new_ansi_c_language);
57+
}
58+
4059
cbmc_api::~cbmc_api() = default;
4160

4261
void cbmc_api::load_model_from_files(const std::vector<std::string> &files)

cpp_api/api.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ class goto_modelt;
66
class message_handlert;
77
class optionst;
88

9+
#include "options.h"
910

1011
// An object in the pattern of Session Facade - owning all of the memory
1112
// the API is using and being responsible for the management of that.
1213
struct cbmc_api
1314
{
1415
// Initialising constructor
1516
explicit cbmc_api();
17+
cbmc_api(const api_optionst &options);
1618
~cbmc_api();
1719

1820
/// Load a goto_model from a given vector of filenames.
@@ -25,7 +27,8 @@ struct cbmc_api
2527
// A struct containing the API dependencies (some of them, at least)
2628
struct api_dependenciest
2729
{
28-
api_dependenciest();
30+
explicit api_dependenciest();
31+
api_dependenciest(const optionst options);
2932
~api_dependenciest();
3033

3134
std::unique_ptr<message_handlert> message_handler;

cpp_api/call_bmc.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
// Test file to try loading a GOTO-model into memory and running a sample verification run on it.
2-
#include <iostream>
3-
#include <vector>
4-
52
#include <util/exception_utils.h>
63

74
#include "api.h"
5+
#include "options.h"
6+
7+
#include <iostream>
8+
#include <vector>
89

910
int main(int argc, char *argv[])
1011
{
@@ -14,8 +15,11 @@ int main(int argc, char *argv[])
1415
// Convert argv to vector of strings for initialize_goto_model
1516
std::vector<std::string> arguments(argv + 1, argv + argc);
1617

18+
// Create API options object, to pass to initialiser of API object.
19+
auto api_options = api_optionst::create().simplify(false);
20+
1721
// Initialise API dependencies and global configuration in one step.
18-
cbmc_api api;
22+
cbmc_api api(api_options);
1923

2024
// Demonstrate the loading of a goto-model from the command line arguments
2125
api.load_model_from_files(arguments);

cpp_api/options.cpp

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#include "options.h"
2+
3+
#include <util/options.h>
4+
5+
api_optionst api_optionst::create()
6+
{
7+
return api_optionst{};
8+
}
9+
10+
api_optionst &api_optionst::simplify(bool on)
11+
{
12+
simplify_enabled = on;
13+
return *this;
14+
}
15+
16+
optionst api_optionst::to_engine_options() const
17+
{
18+
optionst engine_options;
19+
engine_options.set_option("simplify", simplify_enabled);
20+
return engine_options;
21+
}

cpp_api/options.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#pragma once
2+
3+
class optionst;
4+
5+
class api_optionst
6+
{
7+
// Options for the verification engine
8+
bool simplify_enabled;
9+
10+
// Private interface methods
11+
api_optionst() = default;
12+
13+
public:
14+
~api_optionst() = default;
15+
static api_optionst create();
16+
17+
api_optionst &simplify(bool on);
18+
19+
optionst to_engine_options() const;
20+
};

0 commit comments

Comments
 (0)