-
Notifications
You must be signed in to change notification settings - Fork 274
SYNTHESIZER: Add dump-loop-contracts into loop-contracts synthesizer #7451
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 10 additions & 0 deletions
10
regression/goto-synthesizer/loop_contracts_synthesis_01/test_dump.desc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
CORE | ||
main.c | ||
--pointer-check _ --dump-loop-contracts --json-output main.c | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
\"sources"\: \[ \"main\.c\" \] | ||
\"main\"\: \[ \"loop 1 invariant 1\", \"loop 1 assigns result\,i\"\, \"loop 3 invariant 1\"\, \"loop 3 assigns result\,i\" \] | ||
\"output\"\: \"main\.c\" | ||
-- | ||
This test case checks if synthesized contracts are correctly dumped. |
9 changes: 9 additions & 0 deletions
9
regression/goto-synthesizer/loop_contracts_synthesis_02/test_dump.desc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CORE | ||
main.c | ||
--pointer-check _ --dump-loop-contracts | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
\"sources"\: \[ \"main\.c\" \] | ||
\"main\"\: \[ \"loop 1 invariant i \>\= 80u \|\| \_\_CPROVER\_POINTER\_OFFSET\(array\) | ||
-- | ||
This test case checks if synthesized contracts are correctly dumped. |
9 changes: 9 additions & 0 deletions
9
regression/goto-synthesizer/loop_contracts_synthesis_03/test_dump.desc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CORE | ||
main.c | ||
--pointer-check _ --dump-loop-contracts | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
\"sources"\: \[ \"main\.c\" \] | ||
\"main\"\: \[ \"loop 1 invariant array \=\= end | ||
-- | ||
This test case checks if synthesized contracts are correctly dumped. |
9 changes: 9 additions & 0 deletions
9
regression/goto-synthesizer/loop_contracts_synthesis_04/test_dump.desc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
CORE | ||
main.c | ||
--pointer-check _ --dump-loop-contracts | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
\"sources"\: \[ \"main\.c\" \] | ||
i \=\= .*j.*loop 1 assigns | ||
-- | ||
This test case checks if synthesized contracts are correctly dumped. |
8 changes: 8 additions & 0 deletions
8
regression/goto-synthesizer/loop_contracts_synthesis_05/test_dump.desc
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
CORE | ||
main.c | ||
--pointer-check _ --dump-loop-contracts | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
assigns \_\_CPROVER\_POINTER\_OBJECT\(b\-\>data\) | ||
-- | ||
This test case checks if synthesized contracts are correctly dumped. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
/*******************************************************************\ | ||
|
||
Module: Dump Loop Contracts as JSON | ||
|
||
Author: Qinheping Hu, [email protected] | ||
|
||
\*******************************************************************/ | ||
|
||
/// \file | ||
/// Dump Loop Contracts as JSON | ||
|
||
#include "dump_loop_contracts.h" | ||
|
||
#include <util/json_stream.h> | ||
#include <util/simplify_expr.h> | ||
|
||
#include <ansi-c/expr2c.h> | ||
|
||
void dump_loop_contracts( | ||
goto_modelt &goto_model, | ||
const std::map<loop_idt, exprt> &invariant_map, | ||
const std::map<loop_idt, std::set<exprt>> &assigns_map, | ||
const std::string &json_output_str, | ||
std::ostream &out) | ||
{ | ||
// { | ||
// "source" : [SOURCE_NAME_ARRAY], | ||
// "functions" : [{ | ||
// FUN_NAME_1 : [LOOP_CONTRACTS_ARRAY], | ||
// FUN_NAME_2 : [LOOP_CONTRACTS_ARRAY], | ||
// ... | ||
// }], | ||
// "output" : OUTPUT | ||
// } | ||
|
||
INVARIANT(!invariant_map.empty(), "No synthesized loop contracts to dump"); | ||
|
||
namespacet ns(goto_model.symbol_table); | ||
|
||
// Set of names of source files. | ||
std::set<std::string> sources_set; | ||
|
||
// Map from function name to LOOP_CONTRACTS_ARRAY json array of the function. | ||
std::map<std::string, json_arrayt> function_map; | ||
|
||
json_arrayt json_sources; | ||
|
||
for(const auto &invariant_entry : invariant_map) | ||
{ | ||
const auto head = get_loop_head( | ||
invariant_entry.first.loop_number, | ||
goto_model.goto_functions | ||
.function_map[invariant_entry.first.function_id]); | ||
|
||
const auto source_file = id2string(head->source_location().get_file()); | ||
// Add source files. | ||
if(sources_set.insert(source_file).second) | ||
json_sources.push_back(json_stringt(source_file)); | ||
|
||
// Get the LOOP_CONTRACTS_ARRAY for the function from the map. | ||
auto it_function = | ||
function_map.find(id2string(head->source_location().get_function())); | ||
if(it_function == function_map.end()) | ||
{ | ||
std::string function_name = | ||
id2string(head->source_location().get_function()); | ||
|
||
// New LOOP_CONTRACTS_ARRAY | ||
json_arrayt loop_contracts_array; | ||
it_function = | ||
function_map.emplace(function_name, loop_contracts_array).first; | ||
} | ||
json_arrayt &loop_contracts_array = it_function->second; | ||
|
||
// Adding loop invariants | ||
// The loop number in Crangler is 1-indexed instead of 0-indexed. | ||
std::string inv_string = | ||
"loop " + std::to_string(invariant_entry.first.loop_number + 1) + | ||
" invariant " + | ||
expr2c( | ||
simplify_expr(invariant_entry.second, ns), | ||
ns, | ||
expr2c_configurationt::clean_configuration); | ||
loop_contracts_array.push_back(json_stringt(inv_string)); | ||
|
||
// Adding loop assigns | ||
const auto it_assigns = assigns_map.find(invariant_entry.first); | ||
if(it_assigns != assigns_map.end()) | ||
{ | ||
std::string assign_string = | ||
"loop " + std::to_string(invariant_entry.first.loop_number + 1) + | ||
" assigns "; | ||
|
||
bool in_fisrt_iter = true; | ||
for(const auto &a : it_assigns->second) | ||
{ | ||
if(!in_fisrt_iter) | ||
{ | ||
assign_string += ","; | ||
} | ||
else | ||
{ | ||
in_fisrt_iter = false; | ||
} | ||
assign_string += expr2c( | ||
simplify_expr(a, ns), ns, expr2c_configurationt::clean_configuration); | ||
} | ||
loop_contracts_array.push_back(json_stringt(assign_string)); | ||
} | ||
} | ||
|
||
json_stream_objectt json_stream(out); | ||
json_stream.push_back("sources", json_sources); | ||
|
||
// Initialize functions object. | ||
json_arrayt json_functions; | ||
json_objectt json_functions_object; | ||
for(const auto &loop_contracts : function_map) | ||
{ | ||
json_functions_object[loop_contracts.first] = loop_contracts.second; | ||
} | ||
json_functions.push_back(json_functions_object); | ||
json_stream.push_back("functions", json_functions); | ||
|
||
// Destination of the Crangler output. | ||
json_stringt json_output(json_output_str); | ||
json_stream.push_back("output", json_output); | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
/*******************************************************************\ | ||
|
||
Module: Dump Loop Contracts as JSON | ||
|
||
Author: Qinheping Hu, [email protected] | ||
|
||
\*******************************************************************/ | ||
|
||
/// \file | ||
/// Dump Loop Contracts as JSON | ||
|
||
#ifndef CPROVER_GOTO_SYNTHESIZER_DUMP_LOOP_CONTRACTS_H | ||
#define CPROVER_GOTO_SYNTHESIZER_DUMP_LOOP_CONTRACTS_H | ||
|
||
#include "synthesizer_utils.h" | ||
|
||
#include <iosfwd> | ||
#include <string> | ||
|
||
void dump_loop_contracts( | ||
goto_modelt &goto_model, | ||
const std::map<loop_idt, exprt> &invariant_map, | ||
const std::map<loop_idt, std::set<exprt>> &assigns_map, | ||
const std::string &json_output_str, | ||
std::ostream &out); | ||
|
||
#define OPT_DUMP_LOOP_CONTRACTS "(json-output):(dump-loop-contracts)" | ||
|
||
// clang-format off | ||
#define HELP_DUMP_LOOP_CONTRACTS \ | ||
" --dump-loop-contracts dump synthesized loop contracts as JSON\n" /* NOLINT(whitespace/line_length) */ \ | ||
" --json-output <file> specify the output destination of the dumped loop contracts\n" // NOLINT(*) | ||
|
||
// clang-format on | ||
|
||
#endif // CPROVER_GOTO_SYNTHESIZER_DUMP_LOOP_CONTRACTS_H |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.