Skip to content

Commit a8c5f57

Browse files
author
Thomas Kiley
committed
Pull out the common logic for default step types
Previously the JSON and XML trace printing was duplicating the logic for determing whether a trace step is a loop head
1 parent 20f964f commit a8c5f57

File tree

6 files changed

+92
-18
lines changed

6 files changed

+92
-18
lines changed

src/goto-programs/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ SRC = adjust_float_expressions.cpp \
6464
slice_global_inits.cpp \
6565
string_abstraction.cpp \
6666
string_instrumentation.cpp \
67+
structured_trace_util.cpp \
6768
system_library_symbols.cpp \
6869
validate_goto_model.cpp \
6970
vcd_goto_trace.cpp \

src/goto-programs/json_goto_trace.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,15 +284,17 @@ void convert_return(
284284
/// \param conversion_dependencies: A structure
285285
/// that contains information the conversion function
286286
/// needs.
287+
/// \param step_kind: The kind of default step we are printing.
288+
/// See \ref default_step_kind
287289
void convert_default(
288290
json_objectt &json_location_only,
289291
const conversion_dependenciest &conversion_dependencies,
290-
const std::string &step_type)
292+
const default_step_kindt &step_kind)
291293
{
292294
const goto_trace_stept &step = conversion_dependencies.step;
293295
const jsont &location = conversion_dependencies.location;
294296

295-
json_location_only["stepType"] = json_stringt(step_type);
297+
json_location_only["stepType"] = json_stringt(default_step_name(step_kind));
296298
json_location_only["hidden"] = jsont::json_boolean(step.hidden);
297299
json_location_only["thread"] = json_numbert(std::to_string(step.thread_nr));
298300
json_location_only["sourceLocation"] = location;

src/goto-programs/json_goto_trace.h

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Date: November 2005
1515
#define CPROVER_GOTO_PROGRAMS_JSON_GOTO_TRACE_H
1616

1717
#include "goto_trace.h"
18+
#include "structured_trace_util.h"
1819

1920
#include <algorithm>
2021
#include <util/invariant.h>
@@ -101,7 +102,7 @@ void convert_return(
101102
void convert_default(
102103
json_objectt &json_location_only,
103104
const conversion_dependenciest &conversion_dependencies,
104-
const std::string &step_type);
105+
const default_step_kindt &step_kind);
105106

106107
/// Templated version of the conversion method.
107108
/// Works by dispatching to the more specialised
@@ -188,17 +189,14 @@ void convert(
188189
case goto_trace_stept::typet::SHARED_WRITE:
189190
case goto_trace_stept::typet::CONSTRAINT:
190191
case goto_trace_stept::typet::NONE:
191-
const bool is_loophead = std::any_of(
192-
step.pc->incoming_edges.begin(),
193-
step.pc->incoming_edges.end(),
194-
[](goto_programt::targett t) { return t->is_backwards_goto(); });
195-
if(source_location != previous_source_location || is_loophead)
192+
const auto default_step_kind = ::default_step_kind(*step.pc);
193+
if(
194+
source_location != previous_source_location ||
195+
default_step_kind == default_step_kindt::LOOP_HEAD)
196196
{
197197
json_objectt &json_location_only = dest_array.push_back().make_object();
198198
convert_default(
199-
json_location_only,
200-
conversion_dependencies,
201-
is_loophead ? "loop-head" : "location-only");
199+
json_location_only, conversion_dependencies, default_step_kind);
202200
}
203201
}
204202

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*******************************************************************\
2+
3+
Author: Diffblue
4+
5+
\*******************************************************************/
6+
7+
/// \file
8+
/// Utilities for printing location info steps in the trace in a format
9+
/// agnostic way
10+
11+
#include "structured_trace_util.h"
12+
#include <algorithm>
13+
14+
default_step_kindt
15+
default_step_kind(const goto_programt::instructiont &instruction)
16+
{
17+
const bool is_loophead = std::any_of(
18+
instruction.incoming_edges.begin(),
19+
instruction.incoming_edges.end(),
20+
[](goto_programt::targett t) { return t->is_backwards_goto(); });
21+
22+
return is_loophead ? default_step_kindt::LOOP_HEAD
23+
: default_step_kindt::LOCATION_ONLY;
24+
}
25+
std::string default_step_name(const default_step_kindt &step_type)
26+
{
27+
switch(step_type)
28+
{
29+
case default_step_kindt::LOCATION_ONLY:
30+
return "location-only";
31+
case default_step_kindt::LOOP_HEAD:
32+
return "loop-head";
33+
}
34+
UNREACHABLE;
35+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*******************************************************************\
2+
3+
Author: Diffblue
4+
5+
\*******************************************************************/
6+
7+
/// \file
8+
/// Utilities for printing location info steps in the trace in a format
9+
/// agnostic way
10+
11+
#ifndef CPROVER_GOTO_PROGRAMS_STRUCTURED_TRACE_UTIL_H
12+
#define CPROVER_GOTO_PROGRAMS_STRUCTURED_TRACE_UTIL_H
13+
14+
#include "goto_program.h"
15+
#include <string>
16+
17+
/// There are two kinds of step for location markers - location-only and
18+
/// loop-head (for locations associated with the first step of a loop).
19+
enum class default_step_kindt
20+
{
21+
LOCATION_ONLY,
22+
LOOP_HEAD
23+
};
24+
25+
/// Identify for a given instruction whether it is a loophead or just a location
26+
///
27+
/// Loopheads are determined by whether there is backwards jump to them. This
28+
/// matches the loop detection used for loop IDs
29+
/// \param instruction: The instruction to inspect.
30+
/// \return LOOP_HEAD if this is a loop head, otherwise LOCATION_ONLY
31+
default_step_kindt
32+
default_step_kind(const goto_programt::instructiont &instruction);
33+
34+
/// Turns a \ref default_step_kindt into a string that can be used in the trace
35+
/// \param step_type: The kind of step, deduced from \ref default_step_kind
36+
/// \return Either "loop-head" or "location-only"
37+
std::string default_step_name(const default_step_kindt &step_type);
38+
39+
#endif // CPROVER_GOTO_PROGRAMS_STRUCTURED_TRACE_UTIL_H

src/goto-programs/xml_goto_trace.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,11 @@ Author: Daniel Kroening
1818
#include <util/symbol.h>
1919
#include <util/xml_irep.h>
2020

21-
#include <algorithm>
2221
#include <langapi/language_util.h>
2322
#include <util/arith_tools.h>
2423

2524
#include "printf_formatter.h"
25+
#include "structured_trace_util.h"
2626
#include "xml_expr.h"
2727

2828
bool full_lhs_value_includes_binary(
@@ -245,16 +245,15 @@ void convert(
245245
// they might come from different loop iterations. If we suppressed
246246
// them it would be impossible to know in which loop iteration
247247
// we are in.
248-
const bool is_loophead = std::any_of(
249-
step.pc->incoming_edges.begin(),
250-
step.pc->incoming_edges.end(),
251-
[](goto_programt::targett t) { return t->is_backwards_goto(); });
252-
if(source_location != previous_source_location || is_loophead)
248+
const auto default_step_kind = ::default_step_kind(*step.pc);
249+
if(
250+
source_location != previous_source_location ||
251+
default_step_kind == default_step_kindt::LOOP_HEAD)
253252
{
254253
if(!xml_location.name.empty())
255254
{
256255
xmlt &xml_location_only =
257-
dest.new_element(is_loophead ? "loop-head" : "location-only");
256+
dest.new_element(default_step_name(default_step_kind));
258257

259258
xml_location_only.set_attribute_bool("hidden", step.hidden);
260259
xml_location_only.set_attribute(

0 commit comments

Comments
 (0)