Skip to content

Commit 739b682

Browse files
danpoeNlightNFotis
authored andcommitted
Document json_symtab_language and add symbol following.
Add important symbol following in json_symtab_language to fix cbmc crashing after reading gnat2goto output, and document json_symtab_language.
1 parent f494350 commit 739b682

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

src/json-symtab-language/json_symtab_language.cpp

+79
Original file line numberDiff line numberDiff line change
@@ -11,21 +11,31 @@ Author: Chris Smowton, [email protected]
1111
#include "json_symtab_language.h"
1212
#include <json/json_parser.h>
1313
#include <util/json_symbol_table.h>
14+
#include <util/namespace.h>
1415

16+
/// Parse a goto program in json form.
17+
/// \param instream: The input stream
18+
/// \param path: A file path
19+
/// \return: boolean signifying success or failure of the parsing
1520
bool json_symtab_languaget::parse(
1621
std::istream &instream,
1722
const std::string &path)
1823
{
1924
return parse_json(instream, path, get_message_handler(), parsed_json_file);
2025
}
2126

27+
/// Typecheck a goto program in json form.
28+
/// \param symbol_table: The symbol table containing symbols read from file.
29+
/// \param module: A useless parameter, there for interface consistency.
30+
/// \return: boolean signifying success or failure of the typechecking.
2231
bool json_symtab_languaget::typecheck(
2332
symbol_tablet &symbol_table,
2433
const std::string &module)
2534
{
2635
try
2736
{
2837
symbol_table_from_json(parsed_json_file, symbol_table);
38+
follow_type_symbols(symbol_table);
2939
return false;
3040
}
3141
catch(const std::string &str)
@@ -35,6 +45,75 @@ bool json_symtab_languaget::typecheck(
3545
}
3646
}
3747

48+
/// Follow type symbols present in the irep using the passed irep
49+
/// as the root for this operation.
50+
/// \param irep: An irep `irep` to use as a root for the recursive following.
51+
/// \param ns: The namespace to use for symbol following.
52+
void json_symtab_languaget::follow_type_symbols(
53+
irept &irep,
54+
const namespacet &ns)
55+
{
56+
while(irep.id() == ID_symbol)
57+
{
58+
const symbolt &symbol =
59+
ns.lookup(to_symbol_expr(static_cast<exprt const &>(irep)));
60+
61+
if(symbol.is_type && !symbol.type.is_nil())
62+
{
63+
irep = symbol.type;
64+
}
65+
else
66+
{
67+
break;
68+
}
69+
}
70+
71+
for(irept &sub : irep.get_sub())
72+
{
73+
follow_type_symbols(sub, ns);
74+
}
75+
76+
for(auto &entry : irep.get_named_sub())
77+
{
78+
irept &sub = entry.second;
79+
80+
follow_type_symbols(sub, ns);
81+
}
82+
}
83+
84+
/// Follow type symbols present in every symbol in the symbol table.
85+
/// \param symbol_table: The symbol table `symbol_table` that has been
86+
/// produced as part of the parsing and the
87+
/// typechecking of the goto program in json form.
88+
void json_symtab_languaget::follow_type_symbols(symbol_tablet &symbol_table)
89+
{
90+
const namespacet ns(symbol_table);
91+
92+
std::vector<irep_idt> type_symbol_names;
93+
94+
for(auto &entry : symbol_table)
95+
{
96+
symbolt &symbol = symbol_table.get_writeable_ref(entry.first);
97+
98+
if(symbol.is_type)
99+
{
100+
type_symbol_names.push_back(symbol.name);
101+
}
102+
103+
// Modify entries in place
104+
follow_type_symbols(symbol.type, ns);
105+
follow_type_symbols(symbol.value, ns);
106+
}
107+
108+
for(const irep_idt &id : type_symbol_names)
109+
{
110+
symbol_table.remove(id);
111+
}
112+
}
113+
114+
/// Output the result of the parsed json file to the output stream
115+
/// passed as a parameter to this function.
116+
/// \param out: The stream to use to output the parsed_json_file.
38117
void json_symtab_languaget::show_parse(std::ostream &out)
39118
{
40119
parsed_json_file.output(out);

src/json-symtab-language/json_symtab_language.h

+3
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,9 @@ class json_symtab_languaget : public languaget
6060
~json_symtab_languaget() override = default;
6161

6262
protected:
63+
void follow_type_symbols(symbol_tablet &symbol_table);
64+
void follow_type_symbols(irept &irep, const namespacet &ns);
65+
6366
jsont parsed_json_file;
6467
};
6568

0 commit comments

Comments
 (0)