Skip to content

Commit 3ebeb02

Browse files
Call expr/type to json via languaget
1 parent 112d616 commit 3ebeb02

File tree

10 files changed

+318
-188
lines changed

10 files changed

+318
-188
lines changed

src/cbmc/bmc_cover.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -385,7 +385,7 @@ bool bmc_covert::operator()()
385385
json_input["id"]=json_stringt(id2string(step.io_id));
386386
if(step.io_args.size()==1)
387387
json_input["value"]=
388-
json(step.io_args.front(), bmc.ns, ID_unknown);
388+
json(bmc.ns, step.pc->function, step.io_args.front());
389389
json_test.push_back(json_input);
390390
}
391391
}

src/goto-programs/json_goto_trace.cpp

+29-43
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Author: Daniel Kroening
2323
#include <util/simplify_expr.h>
2424
#include <util/json_irep.h>
2525
#include <langapi/language_util.h>
26+
#include <langapi/mode.h>
2627

2728
/// Convert an ASSERT goto_trace step.
2829
/// \param [out] json_failure: The JSON object that
@@ -128,34 +129,25 @@ void convert_decl(
128129

129130
if(!ns.lookup(identifier, symbol))
130131
{
131-
base_name = symbol->base_name;
132-
display_name = symbol->display_name();
133-
if(type_string == "")
134-
type_string = from_type(ns, identifier, symbol->type);
132+
base_name=symbol->base_name;
133+
display_name=symbol->display_name();
134+
type_string = from_type(ns, identifier, symbol->type);
135135

136-
json_assignment["mode"] = json_stringt(id2string(symbol->mode));
137-
exprt simplified = simplify_expr(step.full_lhs_value, ns);
138-
139-
full_lhs_value = json(simplified, ns, symbol->mode);
140-
}
141-
else
142-
{
143-
DATA_INVARIANT(
144-
step.full_lhs_value.is_not_nil(),
145-
"full_lhs_value in assignment must not be nil");
146-
full_lhs_value = json(step.full_lhs_value, ns, ID_unknown);
136+
json_assignment["mode"]=json_stringt(id2string(symbol->mode));
147137
}
148138

149-
json_assignment["value"] = full_lhs_value;
150-
json_assignment["lhs"] = json_stringt(full_lhs_string);
139+
const exprt value_simplified = simplify_expr(step.full_lhs_value, ns);
140+
json_assignment["value"] = json(ns, identifier, value_simplified);
141+
json_assignment["lhs"]=json_stringt(full_lhs_string);
151142
if(trace_options.json_full_lhs)
152143
{
153144
// Not language specific, still mangled, fully-qualified name of lhs
154-
json_assignment["rawLhs"] = json_irept(true).convert_from_irep(simplified);
145+
json_assignment["rawLhs"] =
146+
json_irept(true).convert_from_irep(simplified);
155147
}
156-
json_assignment["hidden"] = jsont::json_boolean(step.hidden);
157-
json_assignment["internal"] = jsont::json_boolean(step.internal);
158-
json_assignment["thread"] = json_numbert(std::to_string(step.thread_nr));
148+
json_assignment["hidden"]=jsont::json_boolean(step.hidden);
149+
json_assignment["internal"]=jsont::json_boolean(step.internal);
150+
json_assignment["thread"]=json_numbert(std::to_string(step.thread_nr));
159151

160152
json_assignment["assignmentType"] = json_stringt(
161153
step.assignment_type == goto_trace_stept::assignment_typet::ACTUAL_PARAMETER
@@ -186,20 +178,17 @@ void convert_output(
186178
json_output["outputID"] = json_stringt(id2string(step.io_id));
187179

188180
// Recovering the mode from the function
189-
irep_idt mode;
190-
const symbolt *function_name;
191-
if(ns.lookup(source_location.get_function(), function_name))
192-
// Failed to find symbol
193-
mode = ID_unknown;
194-
else
195-
mode = function_name->mode;
196-
json_output["mode"] = json_stringt(id2string(mode));
197-
json_arrayt &json_values = json_output["values"].make_array();
181+
const irep_idt mode =
182+
get_mode_from_identifier(ns.get_symbol_table(), source_location.get_function());
183+
json_output["mode"]=json_stringt(id2string(mode));
184+
json_arrayt &json_values=json_output["values"].make_array();
198185

199186
for(const auto &arg : step.io_args)
200187
{
201-
arg.is_nil() ? json_values.push_back(json_stringt(""))
202-
: json_values.push_back(json(arg, ns, mode));
188+
if(arg.is_nil())
189+
json_values.push_back(json_stringt(""));
190+
else
191+
json_values.push_back(json(ns, source_location.get_function(), arg));
203192
}
204193

205194
if(!location.is_null())
@@ -229,20 +218,17 @@ void convert_input(
229218
json_input["inputID"] = json_stringt(id2string(step.io_id));
230219

231220
// Recovering the mode from the function
232-
irep_idt mode;
233-
const symbolt *function_name;
234-
if(ns.lookup(source_location.get_function(), function_name))
235-
// Failed to find symbol
236-
mode = ID_unknown;
237-
else
238-
mode = function_name->mode;
239-
json_input["mode"] = json_stringt(id2string(mode));
240-
json_arrayt &json_values = json_input["values"].make_array();
221+
const irep_idt mode =
222+
get_mode_from_identifier(ns.get_symbol_table(), source_location.get_function());
223+
json_input["mode"]=json_stringt(id2string(mode));
224+
json_arrayt &json_values=json_input["values"].make_array();
241225

242226
for(const auto &arg : step.io_args)
243227
{
244-
arg.is_nil() ? json_values.push_back(json_stringt(""))
245-
: json_values.push_back(json(arg, ns, mode));
228+
if(arg.is_nil())
229+
json_values.push_back(json_stringt(""));
230+
else
231+
json_values.push_back(json(ns, source_location.get_function(), arg));
246232
}
247233

248234
if(!location.is_null())

src/java_bytecode/java_json_expr.cpp

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*******************************************************************\
2+
3+
Module: Expressions in JSON for Java
4+
5+
Author: Peter Schrammel
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Expressions in JSON for Java
11+
12+
#include "java_json_expr.h"
13+
14+
#include <util/json_expr.h>
15+
16+
/// Output a Java source location in json.
17+
/// \param location: a source location
18+
/// \return a json object
19+
json_objectt java_json(const source_locationt &location)
20+
{
21+
json_objectt result = json(source_location);
22+
23+
if(!location.get_java_bytecode_index().empty())
24+
result["bytecodeIndex"]=
25+
json_stringt(id2string(location.get_java_bytecode_index()));
26+
27+
return result;
28+
}
29+
30+
/// Output a Java expression in json.
31+
/// \param expr: an expression
32+
/// \param ns: a namespace
33+
/// \return a json object
34+
json_objectt java_json(
35+
const exprt &expr,
36+
const namespacet &ns)
37+
{
38+
if(expr.id()==ID_constant)
39+
{
40+
json_objectt result;
41+
json(result, ns, to_constant_expr(expr), type,
42+
[](const namespacet &ns, const exprt &expr) { return from_expr(ns, ID_java, expr); },
43+
[](const namespacet &ns, const typet &type) { return from_type(ns, ID_java, type); });
44+
return result;
45+
}
46+
47+
return json(expr, ns);
48+
}
49+
}

src/java_bytecode/java_json_expr.h

+33
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
/*******************************************************************\
2+
3+
Module: Expressions in JSON for Java
4+
5+
Author: Peter Schrammel
6+
7+
\*******************************************************************/
8+
9+
/// \file
10+
/// Expressions in JSON for Java
11+
12+
#ifndef CPROVER_JAVA_BYTECODE_JAVA_JSON_EXPR_H
13+
#define CPROVER_JAVA_BYTECODE_JAVA_JSON_EXPR_H
14+
15+
#include "json.h"
16+
#include "irep.h"
17+
18+
class source_locationt;
19+
class typet;
20+
class exprt;
21+
class namespacet;
22+
23+
json_objectt java_json(
24+
const exprt &,
25+
const namespacet &);
26+
27+
json_objectt java_json(
28+
const typet &,
29+
const namespacet &);
30+
31+
json_objectt java_json(const source_locationt &);
32+
33+
#endif // CPROVER_JAVA_BYTECODE_JAVA_JSON_EXPR_H

src/langapi/language.cpp

+20
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Author: Daniel Kroening, [email protected]
1212
#include "language.h"
1313

1414
#include <util/expr.h>
15+
#include <util/json_expr.h>
1516
#include <util/symbol.h>
1617
#include <util/symbol_table.h>
1718
#include <util/prefix.h>
@@ -52,6 +53,25 @@ bool languaget::from_type(
5253
return false;
5354
}
5455

56+
json_objectt languaget::json(
57+
const exprt &expr,
58+
const namespacet &ns)
59+
{
60+
return ::json(expr, ns);
61+
}
62+
63+
json_objectt languaget::json(
64+
const typet &type,
65+
const namespacet &ns)
66+
{
67+
return ::json(type, ns);
68+
}
69+
70+
json_objectt languaget::json(const source_locationt &source_location)
71+
{
72+
return ::json(source_location);
73+
}
74+
5575
bool languaget::type_to_name(
5676
const typet &type,
5777
std::string &name,

src/langapi/language.h

+11
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class exprt;
3131
class namespacet;
3232
class typet;
3333
class cmdlinet;
34+
class json_objectt;
3435

3536
#define OPT_FUNCTIONS \
3637
"(function):"
@@ -126,6 +127,16 @@ class languaget:public messaget
126127
std::string &code,
127128
const namespacet &ns);
128129

130+
virtual json_objectt json(
131+
const exprt &,
132+
const namespacet &);
133+
134+
virtual json_objectt json(
135+
const typet &,
136+
const namespacet &);
137+
138+
virtual json_objectt json(const source_locationt &);
139+
129140
virtual bool type_to_name(
130141
const typet &type,
131142
std::string &name,

src/langapi/language_util.cpp

+23
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,29 @@ std::string from_type(
4545
return result;
4646
}
4747

48+
json_objectt json(
49+
const namespacet &ns,
50+
const irep_idt &identifier,
51+
const exprt &expr)
52+
{
53+
std::unique_ptr<languaget> p(
54+
get_language_from_identifier(ns.get_symbol_table(), identifier));
55+
56+
std::string result;
57+
return p->json(expr, ns);
58+
}
59+
60+
json_objectt json(
61+
const namespacet &ns,
62+
const irep_idt &identifier,
63+
const typet &type)
64+
{
65+
std::unique_ptr<languaget> p(
66+
get_language_from_identifier(ns.get_symbol_table(), identifier));
67+
68+
return p->json(type, ns);
69+
}
70+
4871
std::string type_to_name(
4972
const namespacet &ns,
5073
const irep_idt &identifier,

src/langapi/language_util.h

+11
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ Author: Daniel Kroening, [email protected]
1515
class exprt;
1616
class namespacet;
1717
class typet;
18+
class json_objectt;
1819

1920
std::string from_expr(
2021
const namespacet &ns,
@@ -23,13 +24,23 @@ std::string from_expr(
2324

2425
std::string from_expr(const exprt &expr);
2526

27+
json_objectt json(
28+
const namespacet &ns,
29+
const irep_idt &identifier,
30+
const exprt &expr);
31+
2632
std::string from_type(
2733
const namespacet &ns,
2834
const irep_idt &identifier,
2935
const typet &type);
3036

3137
std::string from_type(const typet &type);
3238

39+
json_objectt json(
40+
const namespacet &ns,
41+
const irep_idt &identifier,
42+
const typet &type);
43+
3344
exprt to_expr(
3445
const namespacet &ns,
3546
const irep_idt &identifier,

0 commit comments

Comments
 (0)