Skip to content

Remove goto_programt::instructiont::type_nonconst #6419

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 1 commit into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 2 additions & 9 deletions src/goto-programs/goto_program.h
Original file line number Diff line number Diff line change
Expand Up @@ -345,16 +345,9 @@ class goto_programt
return _type;
}

/// Set the kind of the instruction.
/// This method is best avoided to prevent mal-formed instructions.
/// Consider using the goto_programt::make_X methods instead.
goto_program_instruction_typet &type_nonconst()
{
return _type;
}

protected:
// Use type() and type_nonconst() to access.
// Use type() to access. To prevent malformed instructions, no non-const
// access method is provided.
goto_program_instruction_typet _type;

public:
Expand Down
19 changes: 12 additions & 7 deletions src/goto-programs/read_bin_goto_object.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,17 +97,20 @@ static bool read_bin_goto_object(
for(std::size_t ins_index = 0; ins_index < ins_count; ++ins_index)
{
goto_programt::targett itarget = f.body.add_instruction();
goto_programt::instructiont &instruction=*itarget;

instruction.code_nonconst() =
// take copies as references into irepconverter are not stable
codet code =
static_cast<const codet &>(irepconverter.reference_convert(in));
instruction.source_location_nonconst() =
static_cast<const source_locationt &>(
irepconverter.reference_convert(in));
instruction.type_nonconst() =
source_locationt source_location = static_cast<const source_locationt &>(
irepconverter.reference_convert(in));
goto_program_instruction_typet instruction_type =
(goto_program_instruction_typet)irepconverter.read_gb_word(in);
instruction.guard =
exprt guard =
static_cast<const exprt &>(irepconverter.reference_convert(in));

goto_programt::instructiont instruction{
code, source_location, instruction_type, guard, {}};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The order of evaluation of function arguments is unspecified in C++ -- i.e., this may read the entries of the binary in the wrong order.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed!

instruction.target_number = irepconverter.read_gb_word(in);
if(instruction.is_target() &&
rev_target_map.insert(
Expand All @@ -131,6 +134,8 @@ static bool read_bin_goto_object(
// The above info is also held in the goto_functiont object, and could
// be stored in the binary.
}

itarget->swap(instruction);
}

// Resolve targets
Expand Down
18 changes: 6 additions & 12 deletions unit/goto-programs/structured_trace_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,6 @@ Author: Diffblue
#include <goto-programs/goto_trace.h>
#include <goto-programs/structured_trace_util.h>

void link_edges(goto_programt::targett source, goto_programt::targett target)
{
source->targets.push_back(target);
target->incoming_edges.insert(source);
}

source_locationt simple_location(const std::string &file, unsigned line)
{
source_locationt location;
Expand Down Expand Up @@ -53,20 +47,20 @@ TEST_CASE("structured_trace_util", "[core][util][trace]")
// 0 # normal_location
add_instruction(basic_location, instructions);
// 1 # loop_head
add_instruction(loop_head_location, instructions);
auto loop_head = add_instruction(loop_head_location, instructions);
// 2: goto 1 # back_edge
const auto back_edge = add_instruction(back_edge_location, instructions);
back_edge->type_nonconst() = GOTO;
instructions.emplace_back(
goto_programt::make_goto(loop_head, back_edge_location));
auto back_edge = std::prev(instructions.end());
back_edge->location_number = 2;
loop_head->incoming_edges.insert(back_edge);
// 3: no_location
goto_programt::instructiont no_location;
no_location.location_number = 3;
instructions.push_back(no_location);
// 4: no_file
add_instruction(no_file_location, instructions);

link_edges(
std::next(instructions.begin(), 2), std::next(instructions.begin(), 1));

SECTION("location-only steps")
{
goto_trace_stept step;
Expand Down