CBMC
Loading...
Searching...
No Matches
json_symbol.cpp
Go to the documentation of this file.
1/*******************************************************************\
2
3Module: JSON symbol deserialization
4
5Author: Chris Smowton, chris.smowton@diffblue.com
6
7\*******************************************************************/
8
9#include "json_symbol.h"
10
12#include <util/expr.h>
13#include <util/json_irep.h>
14#include <util/type.h>
15
16const std::string &try_get_string(const jsont &in, const std::string &key)
17{
18 if(!in.is_string())
19 {
20 throw deserialization_exceptiont{"expected string for key '" + key + "'"};
21 }
22 return in.value;
23}
24
25bool try_get_bool(const jsont &in, const std::string &key)
26{
27 if(!(in.is_true() || in.is_false()))
28 {
29 throw deserialization_exceptiont{"expected bool for key '" + key + "'"};
30 }
31 return in.is_true();
32}
33
35{
36 if(!json.is_object())
37 {
39 "source location should be encoded as an object"};
40 }
41
43
44 if(json_object.size() == 0)
45 {
46 return source_locationt::nil();
47 }
48 else if(json_object.find("id") != json_object.end())
49 {
51 irept irep = json2irep.convert_from_json(json_object);
52 return static_cast<source_locationt &>(irep);
53 }
54
55 source_locationt result;
56 for(const auto &kv : json_object)
57 {
58 if(kv.first == "workingDirectory")
59 {
61 try_get_string(kv.second, "workingDirectory"));
62 }
63 else if(kv.first == "file")
64 {
65 result.set_file(try_get_string(kv.second, "file"));
66 }
67 else if(kv.first == "line")
68 {
69 result.set_line(try_get_string(kv.second, "line"));
70 }
71 else if(kv.first == "column")
72 {
73 result.set_column(try_get_string(kv.second, "column"));
74 }
75 else if(kv.first == "function")
76 {
77 result.set_function(try_get_string(kv.second, "function"));
78 }
79 else if(kv.first == "bytecodeIndex")
80 {
82 try_get_string(kv.second, "bytecodeIndex"));
83 }
84 else if(kv.first == "propertyId")
85 {
86 result.set_property_id(try_get_string(kv.second, "propertyId"));
87 }
88 else if(kv.first == "propertyClass")
89 {
90 result.set_property_class(try_get_string(kv.second, "propertyClass"));
91 }
92 else if(kv.first == "comment")
93 {
94 result.set_comment(try_get_string(kv.second, "comment"));
95 }
96 else if(kv.first == "caseNumber")
97 {
98 result.set_case_number(try_get_string(kv.second, "caseNumber"));
99 }
100 else if(kv.first == "basicBlockSourceLines")
101 {
102 json_irept json2irep{true};
103 irept irep = json2irep.convert_from_json(kv.second);
104 result.set_basic_block_source_lines(irep);
105 }
106 else if(kv.first == "propertyFatal")
107 {
108 result.property_fatal(try_get_bool(kv.second, "propertyFatal"));
109 }
110 else if(kv.first == "hide")
111 {
112 if(try_get_bool(kv.second, "hide"))
113 result.set_hide();
114 }
115 else if(kv.first == "pragma")
116 {
117 if(!kv.second.is_array())
118 throw deserialization_exceptiont{"pragmas must be an array"};
119
120 for(const auto &pragma : to_json_array(kv.second))
121 {
122 if(!pragma.is_string())
123 throw deserialization_exceptiont{"pragma must be a string"};
124 result.add_pragma(pragma.value);
125 }
126 }
127 else
128 {
130 "try_get_source_location: unexpected key '" + kv.first + "'"};
131 }
132 }
133
134 return result;
135}
136
141{
142 if(!in.is_object())
143 throw deserialization_exceptiont("symbol_from_json takes an object");
144 symbolt result;
145 json_irept json2irep(true);
146 for(const auto &kv : to_json_object(in))
147 {
148 if(kv.first == "type")
149 {
150 irept irep = json2irep.convert_from_json(kv.second);
151 result.type = static_cast<typet &>(irep);
152 }
153 else if(kv.first == "value")
154 {
155 irept irep = json2irep.convert_from_json(kv.second);
156 result.value = static_cast<exprt &>(irep);
157 }
158 else if(kv.first == "location")
159 result.location = try_get_source_location(kv.second);
160 else if(kv.first == "name")
161 result.name = try_get_string(kv.second, "name");
162 else if(kv.first == "module")
163 result.module = try_get_string(kv.second, "module");
164 else if(kv.first == "baseName")
165 result.base_name = try_get_string(kv.second, "baseName");
166 else if(kv.first == "mode")
167 result.mode = try_get_string(kv.second, "mode");
168 else if(kv.first == "prettyName")
169 result.pretty_name = try_get_string(kv.second, "prettyName");
170 else if(kv.first == "isType")
171 result.is_type = try_get_bool(kv.second, "isType");
172 else if(kv.first == "isMacro")
173 result.is_macro = try_get_bool(kv.second, "isMacro");
174 else if(kv.first == "isExported")
175 result.is_exported = try_get_bool(kv.second, "isExported");
176 else if(kv.first == "isInput")
177 result.is_input = try_get_bool(kv.second, "isInput");
178 else if(kv.first == "isOutput")
179 result.is_output = try_get_bool(kv.second, "isOutput");
180 else if(kv.first == "isStateVar")
181 result.is_state_var = try_get_bool(kv.second, "isStateVar");
182 else if(kv.first == "isProperty")
183 result.is_property = try_get_bool(kv.second, "isProperty");
184 else if(kv.first == "isStaticLifetime")
185 result.is_static_lifetime = try_get_bool(kv.second, "isStaticLifetime");
186 else if(kv.first == "isThreadLocal")
187 result.is_thread_local = try_get_bool(kv.second, "isThreadLocal");
188 else if(kv.first == "isLvalue")
189 result.is_lvalue = try_get_bool(kv.second, "isLvalue");
190 else if(kv.first == "isFileLocal")
191 result.is_file_local = try_get_bool(kv.second, "isFileLocal");
192 else if(kv.first == "isExtern")
193 result.is_extern = try_get_bool(kv.second, "isExtern");
194 else if(kv.first == "isVolatile")
195 result.is_volatile = try_get_bool(kv.second, "isVolatile");
196 else if(kv.first == "isParameter")
197 result.is_parameter = try_get_bool(kv.second, "isParameter");
198 else if(kv.first == "isAuxiliary")
199 result.is_auxiliary = try_get_bool(kv.second, "isAuxiliary");
200 else if(kv.first == "isWeak")
201 result.is_weak = try_get_bool(kv.second, "isWeak");
202 else if(kv.first == "prettyType")
203 {
204 } // ignore
205 else if(kv.first == "prettyValue")
206 {
207 } // ignore
208 else
210 "symbol_from_json: unexpected key '" + kv.first + "'");
211 }
212
213 return result;
214}
ait supplies three of the four components needed: an abstract interpreter (in this case handling func...
Definition ai.h:562
Thrown when failing to deserialize a value from some low level format, like JSON or raw bytes.
Base class for all expressions.
Definition expr.h:56
There are a large number of kinds of tree structured or tree-like data in CPROVER.
Definition irep.h:364
Definition json.h:27
bool is_false() const
Definition json.h:76
std::string value
Definition json.h:132
bool is_string() const
Definition json.h:46
bool is_true() const
Definition json.h:71
bool is_object() const
Definition json.h:56
void set_column(const irep_idt &column)
void set_comment(const irep_idt &comment)
void set_property_id(const irep_idt &property_id)
static const source_locationt & nil()
bool property_fatal() const
void set_basic_block_source_lines(irept source_lines)
void set_working_directory(const irep_idt &cwd)
void set_property_class(const irep_idt &property_class)
void set_case_number(const irep_idt &number)
void set_java_bytecode_index(const irep_idt &index)
void add_pragma(const irep_idt &pragma)
void set_file(const irep_idt &file)
void set_line(const irep_idt &line)
void set_function(const irep_idt &function)
Symbol table entry.
Definition symbol.h:28
bool is_auxiliary
Definition symbol.h:77
bool is_volatile
Definition symbol.h:75
bool is_extern
Definition symbol.h:74
bool is_macro
Definition symbol.h:62
bool is_file_local
Definition symbol.h:73
bool is_static_lifetime
Definition symbol.h:70
bool is_property
Definition symbol.h:67
bool is_parameter
Definition symbol.h:76
bool is_thread_local
Definition symbol.h:71
bool is_type
Definition symbol.h:61
source_locationt location
Source code location of definition of symbol.
Definition symbol.h:37
bool is_state_var
Definition symbol.h:66
bool is_output
Definition symbol.h:65
typet type
Type of symbol.
Definition symbol.h:31
bool is_weak
Definition symbol.h:78
irep_idt name
The unique identifier.
Definition symbol.h:40
bool is_exported
Definition symbol.h:63
irep_idt pretty_name
Language-specific display name.
Definition symbol.h:52
irep_idt irep_idt base_name
Name of module the symbol belongs to.
Definition symbol.h:46
bool is_lvalue
Definition symbol.h:72
exprt value
Initial value of symbol.
Definition symbol.h:34
irep_idt mode
Language mode.
Definition symbol.h:49
bool is_input
Definition symbol.h:64
The type of an expression, extends irept.
Definition type.h:29
json_objectt & to_json_object(jsont &json)
Definition json.h:442
json_arrayt & to_json_array(jsont &json)
Definition json.h:424
bool try_get_bool(const jsont &in, const std::string &key)
Return boolean value for a given key if present in the json object.
source_locationt try_get_source_location(const jsont &json)
Return a source_locationt from the given JSON object.
symbolt symbol_from_json(const jsont &in)
Deserialise a json object to a symbolt.
const std::string & try_get_string(const jsont &in, const std::string &key)
Return string value for a given key if present in the json object.
static void json(json_objectT &result, const irep_idt &property_id, const property_infot &property_info)
Defines typet, type_with_subtypet and type_with_subtypest.