Skip to content

Commit 3614bdc

Browse files
committed
Make symbol table JSON output and input consistent
This adapts the JSON symbol table output function show_symbol_table_json_ui() and the JSON symbol table input function symbol_table_from_json() to use a consistent format.
1 parent ffe2e58 commit 3614bdc

File tree

4 files changed

+105
-59
lines changed

4 files changed

+105
-59
lines changed

src/goto-programs/show_symbol_table.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,7 @@ static void show_symbol_table_json_ui(
196196

197197
json_objectt symbol_json(
198198
{{"prettyName", json_stringt(symbol.pretty_name)},
199+
{"name", json_stringt(symbol.name)},
199200
{"baseName", json_stringt(symbol.base_name)},
200201
{"mode", json_stringt(symbol.mode)},
201202
{"module", json_stringt(symbol.module)},

src/json-symtab-language/json_symbol.cpp

Lines changed: 42 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -69,44 +69,50 @@ symbolt symbol_from_json(const jsont &in)
6969
result.name = try_get_string(kv.second, "name");
7070
else if(kv.first == "module")
7171
result.module = try_get_string(kv.second, "module");
72-
else if(kv.first == "base_name")
73-
result.base_name = try_get_string(kv.second, "base_name");
72+
else if(kv.first == "baseName")
73+
result.base_name = try_get_string(kv.second, "baseName");
7474
else if(kv.first == "mode")
7575
result.mode = try_get_string(kv.second, "mode");
76-
else if(kv.first == "pretty_name")
77-
result.pretty_name = try_get_string(kv.second, "pretty_name");
78-
else if(kv.first == "is_type")
79-
result.is_type = try_get_bool(kv.second, "is_type");
80-
else if(kv.first == "is_macro")
81-
result.is_macro = try_get_bool(kv.second, "is_macro");
82-
else if(kv.first == "is_exported")
83-
result.is_exported = try_get_bool(kv.second, "is_exported");
84-
else if(kv.first == "is_input")
85-
result.is_input = try_get_bool(kv.second, "is_input");
86-
else if(kv.first == "is_output")
87-
result.is_output = try_get_bool(kv.second, "is_output");
88-
else if(kv.first == "is_state_var")
89-
result.is_state_var = try_get_bool(kv.second, "is_state_var");
90-
else if(kv.first == "is_property")
91-
result.is_property = try_get_bool(kv.second, "is_property");
92-
else if(kv.first == "is_static_lifetime")
93-
result.is_static_lifetime = try_get_bool(kv.second, "is_static_lifetime");
94-
else if(kv.first == "is_thread_local")
95-
result.is_thread_local = try_get_bool(kv.second, "is_thread_local");
96-
else if(kv.first == "is_lvalue")
97-
result.is_lvalue = try_get_bool(kv.second, "is_lvalue");
98-
else if(kv.first == "is_file_local")
99-
result.is_file_local = try_get_bool(kv.second, "is_file_local");
100-
else if(kv.first == "is_extern")
101-
result.is_extern = try_get_bool(kv.second, "is_extern");
102-
else if(kv.first == "is_volatile")
103-
result.is_volatile = try_get_bool(kv.second, "is_volatile");
104-
else if(kv.first == "is_parameter")
105-
result.is_parameter = try_get_bool(kv.second, "is_parameter");
106-
else if(kv.first == "is_auxiliary")
107-
result.is_auxiliary = try_get_bool(kv.second, "is_auxiliary");
108-
else if(kv.first == "is_weak")
109-
result.is_weak = try_get_bool(kv.second, "is_weak");
76+
else if(kv.first == "prettyName")
77+
result.pretty_name = try_get_string(kv.second, "prettyName");
78+
else if(kv.first == "isType")
79+
result.is_type = try_get_bool(kv.second, "isType");
80+
else if(kv.first == "isMacro")
81+
result.is_macro = try_get_bool(kv.second, "isMacro");
82+
else if(kv.first == "isExported")
83+
result.is_exported = try_get_bool(kv.second, "isExported");
84+
else if(kv.first == "isInput")
85+
result.is_input = try_get_bool(kv.second, "isInput");
86+
else if(kv.first == "isOutput")
87+
result.is_output = try_get_bool(kv.second, "isOutput");
88+
else if(kv.first == "isStateVar")
89+
result.is_state_var = try_get_bool(kv.second, "isStateVar");
90+
else if(kv.first == "isProperty")
91+
result.is_property = try_get_bool(kv.second, "isProperty");
92+
else if(kv.first == "isStaticLifetime")
93+
result.is_static_lifetime = try_get_bool(kv.second, "isStaticLifetime");
94+
else if(kv.first == "isThreadLocal")
95+
result.is_thread_local = try_get_bool(kv.second, "isThreadLocal");
96+
else if(kv.first == "isLvalue")
97+
result.is_lvalue = try_get_bool(kv.second, "isLvalue");
98+
else if(kv.first == "isFileLocal")
99+
result.is_file_local = try_get_bool(kv.second, "isFileLocal");
100+
else if(kv.first == "isExtern")
101+
result.is_extern = try_get_bool(kv.second, "isExtern");
102+
else if(kv.first == "isVolatile")
103+
result.is_volatile = try_get_bool(kv.second, "isVolatile");
104+
else if(kv.first == "isParameter")
105+
result.is_parameter = try_get_bool(kv.second, "isParameter");
106+
else if(kv.first == "isAuxiliary")
107+
result.is_auxiliary = try_get_bool(kv.second, "isAuxiliary");
108+
else if(kv.first == "isWeak")
109+
result.is_weak = try_get_bool(kv.second, "isWeak");
110+
else if(kv.first == "prettyType")
111+
{
112+
} // ignore
113+
else if(kv.first == "prettyValue")
114+
{
115+
} // ignore
110116
else
111117
throw deserialization_exceptiont(
112118
"symbol_from_json: unexpected key '" + kv.first + "'");

src/json-symtab-language/json_symbol_table.cpp

Lines changed: 30 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,15 +15,38 @@ Author: Chris Smowton, [email protected]
1515

1616
void symbol_table_from_json(const jsont &in, symbol_tablet &symbol_table)
1717
{
18-
if(!in.is_array())
18+
if(!in.is_object())
19+
{
20+
throw deserialization_exceptiont(
21+
"symbol_table_from_json: JSON input must be an object");
22+
}
23+
24+
const json_objectt &json_object = to_json_object(in);
25+
const auto it = json_object.find("symbolTable");
26+
27+
if(it == json_object.end())
28+
{
1929
throw deserialization_exceptiont(
20-
"symbol_table_from_json: JSON input must be an array");
21-
for(const auto &js_symbol : to_json_array(in))
30+
"symbol_table_from_json: JSON object must have key `symbolTable`");
31+
}
32+
33+
if(!it->second.is_object())
2234
{
23-
symbolt deserialized = symbol_from_json(js_symbol);
24-
if(symbol_table.add(deserialized))
35+
throw deserialization_exceptiont(
36+
"symbol_table_from_json: JSON symbol table must be an object");
37+
}
38+
39+
const json_objectt &json_symbol_table = to_json_object(it->second);
40+
41+
for(const auto &pair : json_symbol_table)
42+
{
43+
const jsont &json_symbol = pair.second;
44+
45+
symbolt symbol = symbol_from_json(json_symbol);
46+
47+
if(symbol_table.add(symbol))
2548
throw deserialization_exceptiont(
26-
"symbol_table_from_json: duplicate symbol name '" +
27-
id2string(deserialized.name) + "'");
49+
"symbol_table_from_json: duplicate symbol name `" +
50+
id2string(symbol.name) + "`");
2851
}
2952
}

src/util/json_irep.cpp

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,7 @@ json_objectt json_irept::convert_from_irep(const irept &irep) const
3434
{
3535
json_objectt irep_object;
3636

37-
if(irep.id()!=ID_nil)
38-
irep_object["id"]=json_stringt(irep.id_string());
37+
irep_object["id"] = json_stringt(irep.id_string());
3938

4039
convert_sub_tree("sub", irep.get_sub(), irep_object);
4140
convert_named_sub_tree("namedSub", irep.get_named_sub(), irep_object);
@@ -97,27 +96,44 @@ void json_irept::convert_named_sub_tree(
9796
/// \return result - irep equivalent of input
9897
irept json_irept::convert_from_json(const jsont &in) const
9998
{
100-
std::vector<std::string> have_keys;
101-
for(const auto &keyval : to_json_object(in))
102-
have_keys.push_back(keyval.first);
103-
std::sort(have_keys.begin(), have_keys.end());
104-
if(have_keys!=std::vector<std::string>{"comment", "id", "namedSub", "sub"})
99+
if(!in.is_object())
105100
{
106101
throw deserialization_exceptiont(
107-
"irep JSON representation is missing one of needed keys: "
108-
"'id', 'sub', 'namedSub', 'comment'");
102+
"irep JSON representation must be an object");
109103
}
110104

111-
irept out(in["id"].value);
105+
const json_objectt &json_object = to_json_object(in);
112106

113-
for(const auto &sub : to_json_array(in["sub"]))
114-
out.get_sub().push_back(convert_from_json(sub));
107+
irept out;
115108

116-
for(const auto &named_sub : to_json_object(in["namedSub"]))
117-
out.add(named_sub.first)=convert_from_json(named_sub.second);
109+
{
110+
const auto it = json_object.find("id");
111+
112+
if(it != json_object.end())
113+
{
114+
out.id(it->second.value);
115+
}
116+
}
117+
118+
{
119+
const auto it = json_object.find("sub");
120+
121+
if(it != json_object.end())
122+
{
123+
for(const auto &sub : to_json_array(it->second))
124+
out.get_sub().push_back(convert_from_json(sub));
125+
}
126+
}
127+
128+
{
129+
const auto it = json_object.find("namedSub");
118130

119-
for(const auto &comment : to_json_object(in["comment"]))
120-
out.add(comment.first)=convert_from_json(comment.second);
131+
if(it != json_object.end())
132+
{
133+
for(const auto &named_sub : to_json_object(it->second))
134+
out.add(named_sub.first) = convert_from_json(named_sub.second);
135+
}
136+
}
121137

122138
return out;
123139
}

0 commit comments

Comments
 (0)