-
Notifications
You must be signed in to change notification settings - Fork 273
Use temporary variable to initialise array cells #4523
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
smowton
merged 1 commit into
diffblue:develop
from
smowton:smowton/feature/use-temporary-to-init-multidimensional-array
Apr 12, 2019
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
Binary file added
BIN
+374 Bytes
jbmc/regression/jbmc/multidimensional-array-object-factory/My.class
Binary file not shown.
8 changes: 8 additions & 0 deletions
8
jbmc/regression/jbmc/multidimensional-array-object-factory/My.java
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 @@ | ||
public class My { | ||
public int func(int[][] inta) { | ||
if (inta[0][0] == 500) { | ||
return 1; | ||
} | ||
return 0; | ||
} | ||
} |
14 changes: 14 additions & 0 deletions
14
jbmc/regression/jbmc/multidimensional-array-object-factory/test.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,14 @@ | ||
CORE | ||
My.class | ||
--function My.func --unwind 3 --max-nondet-array-length 10 --show-vcc | ||
^EXIT=0$ | ||
^SIGNAL=0$ | ||
-- | ||
^warning: ignoring | ||
\$object | ||
invalid_object | ||
-- | ||
This checks that no \$object or invalid_object references are generated when creating a multidimensional | ||
array (required for My.func's input parameter, which is an int[][]). Both of the unwanted terms are used | ||
by goto-symex to describe the result of a failed or unknown pointer dereference, and the object factory's | ||
initialisation code should be clear enough that this is never necessary. |
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 |
---|---|---|
|
@@ -1207,6 +1207,29 @@ void java_object_factoryt::array_loop_init_code( | |
plus_exprt(array_init_symexpr, counter_expr, array_init_symexpr.type()), | ||
array_init_symexpr.type().subtype()); | ||
|
||
bool new_item_is_primitive = arraycellref.type().id() != ID_pointer; | ||
|
||
// Use a temporary to initialise a new, or update an existing, non-primitive. | ||
// This makes it clearer that in a sequence like | ||
// `new_array_item->x = y; new_array_item->z = w;` that all the | ||
// `new_array_item` references must alias, cf. the harder-to-analyse | ||
// `some_expr[idx]->x = y; some_expr[idx]->z = w;` | ||
exprt init_expr; | ||
if(new_item_is_primitive) | ||
{ | ||
init_expr = arraycellref; | ||
} | ||
else | ||
{ | ||
init_expr = allocate_objects.allocate_automatic_local_object( | ||
arraycellref.type(), "new_array_item"); | ||
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. Please use the same name as in the comment above (either this to |
||
|
||
// If we're updating an existing array item, read the existing object that | ||
// we (may) alter: | ||
if(update_in_place != update_in_placet::NO_UPDATE_IN_PLACE) | ||
assignments.add(code_assignt(init_expr, arraycellref)); | ||
} | ||
|
||
// MUST_UPDATE_IN_PLACE only applies to this object. | ||
// If this is a pointer to another object, offer the chance | ||
// to leave it alone by setting MAY_UPDATE_IN_PLACE instead. | ||
|
@@ -1216,16 +1239,23 @@ void java_object_factoryt::array_loop_init_code( | |
update_in_place; | ||
gen_nondet_init( | ||
assignments, | ||
arraycellref, | ||
false, // is_sub | ||
false, // skip_classid | ||
init_expr, | ||
false, // is_sub | ||
false, // skip_classid | ||
// These are variable in number, so use dynamic allocator: | ||
lifetimet::DYNAMIC, | ||
element_type, // override | ||
depth, | ||
child_update_in_place, | ||
location); | ||
|
||
if(!new_item_is_primitive) | ||
{ | ||
// We used a temporary variable to update or initialise an array item; | ||
// now write it into the array: | ||
assignments.add(code_assignt(arraycellref, init_expr)); | ||
} | ||
|
||
exprt java_one=from_integer(1, java_int_type()); | ||
code_assignt incr(counter_expr, plus_exprt(counter_expr, java_one)); | ||
|
||
|
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.
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.
💡 Any reason to not to do the primitives in the same way, just in the interest of keeping this code simpler?
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.
Just to keep it simple indeed.
array[idx] = nondet(int)
->new_array_item = nondet(int); array[idx] = new_array_item;
seems like a disservice to the reader.