Skip to content

Commit 19a8a0e

Browse files
committed
goto-contracts: Separate front-end for contracts
This introduces a new binary to handle all steps related to contracts processing, which previously was part of goto-instrument.
1 parent 198fd86 commit 19a8a0e

11 files changed

+282
-0
lines changed

CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ if("${CMAKE_CXX_COMPILER_ID}" STREQUAL "Clang" OR
154154
"$<TARGET_FILE:driver>"
155155
"$<TARGET_FILE:goto-analyzer>"
156156
"$<TARGET_FILE:goto-cc>"
157+
"$<TARGET_FILE:goto-contracts>"
157158
"$<TARGET_FILE:goto-diff>"
158159
"$<TARGET_FILE:goto-instrument>"
159160
"$<TARGET_FILE:janalyzer>"
@@ -221,6 +222,8 @@ cprover_default_properties(
221222
goto-analyzer-lib
222223
goto-cc
223224
goto-cc-lib
225+
goto-contracts
226+
goto-contracts-lib
224227
goto-checker
225228
goto-diff
226229
goto-diff-lib

src/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,7 @@ add_subdirectory(util)
108108
add_subdirectory(cbmc)
109109
add_subdirectory(crangler)
110110
add_subdirectory(goto-cc)
111+
add_subdirectory(goto-contracts)
111112
add_subdirectory(goto-instrument)
112113
add_subdirectory(goto-analyzer)
113114
add_subdirectory(goto-diff)

src/Makefile

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ DIRS = analyses \
77
crangler \
88
goto-analyzer \
99
goto-cc \
10+
goto-contracts \
1011
goto-checker \
1112
goto-diff \
1213
goto-instrument \
@@ -31,6 +32,7 @@ all: cbmc.dir \
3132
crangler.dir \
3233
goto-analyzer.dir \
3334
goto-cc.dir \
35+
goto-contracts.dir \
3436
goto-diff.dir \
3537
goto-instrument.dir \
3638
goto-harness.dir \
@@ -63,6 +65,10 @@ cpp.dir: ansi-c.dir linking.dir
6365

6466
crangler.dir: util.dir json.dir
6567

68+
goto-contracts.dir: util.dir goto-programs.dir langapi.dir \
69+
json.dir \
70+
goto-instrument.dir
71+
6672
languages: util.dir langapi.dir \
6773
cpp.dir ansi-c.dir xmllang.dir assembler.dir \
6874
jsil.dir json.dir json-symtab-language.dir statement-list.dir

src/goto-contracts/CMakeLists.txt

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Library
2+
file(GLOB_RECURSE sources "*.cpp" "*.h")
3+
list(REMOVE_ITEM sources
4+
${CMAKE_CURRENT_SOURCE_DIR}/goto_contracts_main.cpp
5+
)
6+
add_library(goto-contracts-lib ${sources})
7+
8+
generic_includes(goto-contracts-lib)
9+
10+
target_link_libraries(goto-contracts-lib
11+
ansi-c
12+
cpp
13+
big-int
14+
goto-programs
15+
langapi
16+
util
17+
json
18+
)
19+
20+
# Executable
21+
add_executable(goto-contracts goto_contracts_main.cpp)
22+
target_link_libraries(goto-contracts goto-contracts-lib)
23+
install(TARGETS goto-contracts DESTINATION ${CMAKE_INSTALL_BINDIR})

src/goto-contracts/Makefile

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
SRC = goto_contracts_languages.cpp \
2+
goto_contracts_main.cpp \
3+
goto_contracts_parse_options.cpp \
4+
# Empty last line
5+
6+
OBJ += ../ansi-c/ansi-c$(LIBEXT) \
7+
../cpp/cpp$(LIBEXT) \
8+
../big-int/big-int$(LIBEXT) \
9+
../goto-programs/goto-programs$(LIBEXT) \
10+
../langapi/langapi$(LIBEXT) \
11+
../util/util$(LIBEXT) \
12+
../json/json$(LIBEXT) \
13+
# Empty last line
14+
15+
INCLUDES= -I ..
16+
17+
LIBS =
18+
19+
CLEANFILES = goto-contracts$(EXEEXT) goto-contracts$(LIBEXT)
20+
21+
include ../config.inc
22+
include ../common
23+
24+
all: goto-contracts$(EXEEXT)
25+
26+
###############################################################################
27+
28+
goto-contracts$(EXEEXT): $(OBJ)
29+
$(LINKBIN)
30+
31+
.PHONY: goto-contracts-mac-signed
32+
33+
goto-contracts-mac-signed: goto-contracts$(EXEEXT)
34+
codesign -v -s $(OSX_IDENTITY) goto-contracts$(EXEEXT)

src/goto-contracts/README.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
\ingroup module_hidden
2+
\defgroup goto-contracts goto-contracts
3+
4+
# Folder goto-contracts
5+
6+
\author Michael Tautschnig
7+
8+
The `goto-contracts/` contains all processing of function contracts and loop
9+
invariants.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*******************************************************************\
2+
3+
Module: Language Registration
4+
5+
Author: Michael Tautschnig
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Language Registration
11+
12+
#include "goto_contracts_parse_options.h"
13+
14+
#include <langapi/mode.h>
15+
16+
#include <ansi-c/ansi_c_language.h>
17+
#include <cpp/cpp_language.h>
18+
19+
void goto_contracts_parse_optionst::register_languages()
20+
{
21+
register_language(new_ansi_c_language);
22+
register_language(new_cpp_language);
23+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*******************************************************************\
2+
3+
Module: Main Module
4+
5+
Author: Michael Tautschnig
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Main Module
11+
12+
#ifdef _MSC_VER
13+
# include <util/unicode.h>
14+
#endif
15+
16+
#include "goto_contracts_parse_options.h"
17+
18+
#ifdef _MSC_VER
19+
int wmain(int argc, const wchar_t **argv_wide)
20+
{
21+
auto vec = narrow_argv(argc, argv_wide);
22+
auto narrow = to_c_str_array(std::begin(vec), std::end(vec));
23+
auto argv = narrow.data();
24+
#else
25+
int main(int argc, const char **argv)
26+
{
27+
#endif
28+
goto_contracts_parse_optionst parse_options(argc, argv);
29+
return parse_options.main();
30+
}
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
/*******************************************************************\
2+
3+
Module: Main Module
4+
5+
Author: Michael Tautschnig
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Main Module
11+
12+
#include "goto_contracts_parse_options.h"
13+
14+
#include <util/config.h>
15+
#include <util/exit_codes.h>
16+
#include <util/version.h>
17+
18+
#include <goto-programs/read_goto_binary.h>
19+
20+
#include <iostream>
21+
22+
/// invoke main modules
23+
int goto_contracts_parse_optionst::doit()
24+
{
25+
if(cmdline.isset("version"))
26+
{
27+
std::cout << CBMC_VERSION << '\n';
28+
return CPROVER_EXIT_SUCCESS;
29+
}
30+
31+
if(cmdline.args.size() != 1 && cmdline.args.size() != 2)
32+
{
33+
help();
34+
return CPROVER_EXIT_USAGE_ERROR;
35+
}
36+
37+
messaget::eval_verbosity(
38+
cmdline.get_value("verbosity"), messaget::M_STATISTICS, ui_message_handler);
39+
40+
register_languages();
41+
42+
get_goto_program();
43+
44+
// TODO
45+
// read contracts from file, using Crangler's syntax
46+
// ASSUME no recursion, function pointers, setjmp/longjmp
47+
// create a queue of goto models
48+
// for each element in the queue:
49+
// reachability slice
50+
// remove unused functions
51+
// slice global init
52+
// compute call graph
53+
// walk call graph bottom-up
54+
// if a function contract exists:
55+
// copy goto model, mark function as top-level function
56+
// compute assigns set, possibly doing inlining first
57+
// replace all calls to this function by contract
58+
// TODO: loops, dependencies, Makefile, generate contracts stubs,
59+
// precondition, postcondition
60+
61+
help();
62+
return CPROVER_EXIT_USAGE_ERROR;
63+
}
64+
65+
void goto_contracts_parse_optionst::get_goto_program()
66+
{
67+
log.status() << "Reading GOTO program from '" << cmdline.args[0] << "'"
68+
<< messaget::eom;
69+
70+
config.set(cmdline);
71+
72+
auto result = read_goto_binary(cmdline.args[0], ui_message_handler);
73+
74+
if(!result.has_value())
75+
throw 0;
76+
77+
goto_model = std::move(result.value());
78+
79+
config.set_from_symbol_table(goto_model.symbol_table);
80+
}
81+
82+
/// display command line help
83+
void goto_contracts_parse_optionst::help()
84+
{
85+
// clang-format off
86+
std::cout << '\n' << banner_string("Goto-contracts", CBMC_VERSION) << '\n'
87+
<< align_center_with_border("Copyright (C) 2021") << '\n'
88+
<< align_center_with_border("Michael Tautschnig") << '\n'
89+
<< align_center_with_border("[email protected]") << '\n'
90+
<<
91+
"\n"
92+
"Usage: Purpose:\n"
93+
"\n"
94+
" goto-contracts [-?] [-h] [--help] show help\n"
95+
" goto-contracts in out apply contracts\n"
96+
"\n"
97+
"Main options:\n"
98+
"\n"
99+
"Other options:\n"
100+
" --version show version and exit\n"
101+
" --xml-ui use XML-formatted output\n"
102+
" --json-ui use JSON-formatted output\n"
103+
"\n";
104+
// clang-format on
105+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/*******************************************************************\
2+
3+
Module: Command Line Parsing
4+
5+
Author: Michael Tautschnig
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Command Line Parsing
11+
12+
#ifndef CPROVER_GOTO_CONTRACTS_GOTO_CONTRACTS_PARSE_OPTIONS_H
13+
#define CPROVER_GOTO_CONTRACTS_GOTO_CONTRACTS_PARSE_OPTIONS_H
14+
15+
#include <util/parse_options.h>
16+
17+
#include <goto-programs/goto_model.h>
18+
19+
// clang-format off
20+
#define GOTO_CONTRACTS_OPTIONS \
21+
"(verbosity):(version)(xml-ui)(json-ui)" \
22+
// empty last line
23+
24+
// clang-format on
25+
26+
class goto_contracts_parse_optionst : public parse_options_baset
27+
{
28+
public:
29+
int doit() override;
30+
void help() override;
31+
32+
goto_contracts_parse_optionst(int argc, const char **argv)
33+
: parse_options_baset(GOTO_CONTRACTS_OPTIONS, argc, argv, "goto-contracts")
34+
{
35+
}
36+
37+
protected:
38+
void register_languages() override;
39+
40+
void get_goto_program();
41+
42+
goto_modelt goto_model;
43+
};
44+
45+
#endif // CPROVER_GOTO_CONTRACTS_GOTO_CONTRACTS_PARSE_OPTIONS_H
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
goto-instrument
2+
goto-programs
3+
util

0 commit comments

Comments
 (0)