-
Notifications
You must be signed in to change notification settings - Fork 276
Bug fix using format
function on code_declt
containing initialization
#5833
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
Changes from all commits
6adc45e
7b6a25d
7ea0e60
f593d18
51887aa
8a14e67
b5df13b
541cd8a
111d05f
0d0b964
dae3eca
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -420,6 +420,38 @@ class code_declt:public codet | |
return symbol().get_identifier(); | ||
} | ||
|
||
/// Returns the initial value to which the declared variable is initialized, | ||
/// or empty in the case where no initialisation is included. | ||
/// \note: Initial values may be present in the front end but they must be | ||
/// separated into a separate assignment when used in a `goto_instructiont`. | ||
optionalt<exprt> initial_value() const | ||
{ | ||
if(operands().size() < 2) | ||
return {}; | ||
return {op1()}; | ||
} | ||
|
||
/// Sets the value to which this declaration initializes the declared | ||
/// variable. Empty optional maybe passed to remove existing initialisation. | ||
/// \note: Initial values may be present in the front end but they must be | ||
/// separated into a separate assignment when used in a `goto_instructiont`. | ||
void set_initial_value(optionalt<exprt> initial_value) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If these can exist with optional There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. All the examples I have found so far were mutating after construction. I could add the facility to set this via the constructor as well. |
||
{ | ||
if(!initial_value) | ||
{ | ||
operands().resize(1); | ||
} | ||
else if(operands().size() < 2) | ||
{ | ||
PRECONDITION(operands().size() == 1); | ||
add_to_operands(std::move(*initial_value)); | ||
} | ||
else | ||
{ | ||
op1() = std::move(*initial_value); | ||
} | ||
} | ||
|
||
static void check( | ||
const codet &code, | ||
const validation_modet vm = validation_modet::INVARIANT) | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
/*******************************************************************\ | ||
|
||
Module: Unit tests for `format` function. | ||
|
||
Author: DiffBlue Limited. | ||
|
||
\*******************************************************************/ | ||
|
||
#include <util/format.h> | ||
#include <util/format_expr.h> | ||
#include <util/std_code.h> | ||
|
||
#include <testing-utils/use_catch.h> | ||
|
||
TEST_CASE("Format a declaration statement.", "[core][util][format]") | ||
{ | ||
const signedbv_typet int_type{32}; | ||
code_declt declaration{symbol_exprt{"foo", int_type}}; | ||
REQUIRE(format_to_string(declaration) == "decl signedbv[32] foo;"); | ||
declaration.set_initial_value({int_type.zero_expr()}); | ||
REQUIRE(format_to_string(declaration) == "decl signedbv[32] foo = 0;"); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
While the
INVARIANT
inset_decl
is probably the right thing, this one may better be placed ingoto_programt::instructiont::validate
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Given that this PR is now approved, I am going to merge as is and clean up this as a follow-up PR.