Skip to content

Commit 16f4f09

Browse files
authored
Merge pull request #4879 from tautschnig/valid-json
(un)reachable functions: valid JSON even without source locations
2 parents 247380f + f07b80a commit 16f4f09

File tree

2 files changed

+48
-30
lines changed

2 files changed

+48
-30
lines changed
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
CORE
22
../unreachable-instructions-basic-text/unreachable.c
33
--unreachable-functions --json -
4-
"file name": ".*unreachable.c",
5-
"first line": 3,
4+
"file": ".*unreachable.c",
5+
"firstLine": 3,
66
"function": "not_called",
7-
"last line": 6
7+
"lastLine": 6
88
^EXIT=0$
99
^SIGNAL=0$
1010
--
11-
"last line":[[:space:]]*$
11+
"lastLine":[[:space:]]*$
1212
^warning: ignoring

src/goto-analyzer/unreachable_instructions.cpp

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,17 @@ static void add_to_xml(
100100
return;
101101
}
102102

103+
static optionalt<std::string>
104+
file_name_string_opt(const source_locationt &source_location)
105+
{
106+
if(source_location.get_file().empty())
107+
return nullopt;
108+
109+
return concat_dir_file(
110+
id2string(source_location.get_working_directory()),
111+
id2string(source_location.get_file()));
112+
}
113+
103114
static void add_to_json(
104115
const namespacet &ns,
105116
const irep_idt &function_identifier,
@@ -114,12 +125,9 @@ static void add_to_json(
114125
DATA_INVARIANT(end_function->is_end_function(),
115126
"The last instruction in a goto-program must be END_FUNCTION");
116127

117-
json_objectt entry{
118-
{"function", json_stringt(function_identifier)},
119-
{"fileName",
120-
json_stringt(concat_dir_file(
121-
id2string(end_function->source_location.get_working_directory()),
122-
id2string(end_function->source_location.get_file())))}};
128+
json_objectt entry{{"function", json_stringt(function_identifier)}};
129+
if(auto file_name_opt = file_name_string_opt(end_function->source_location))
130+
entry["file"] = json_stringt{*file_name_opt};
123131

124132
json_arrayt &dead_ins=entry["unreachableInstructions"].make_array();
125133

@@ -243,22 +251,30 @@ bool static_unreachable_instructions(
243251
return false;
244252
}
245253

254+
static optionalt<std::string>
255+
line_string_opt(const source_locationt &source_location)
256+
{
257+
const irep_idt &line = source_location.get_line();
246258

259+
if(line.empty())
260+
return nullopt;
261+
else
262+
return id2string(line);
263+
}
247264

248265
static void json_output_function(
249266
const irep_idt &function,
250267
const source_locationt &first_location,
251268
const source_locationt &last_location,
252269
json_arrayt &dest)
253270
{
254-
json_objectt entry{
255-
{"function", json_stringt(function)},
256-
{"file name",
257-
json_stringt(concat_dir_file(
258-
id2string(first_location.get_working_directory()),
259-
id2string(first_location.get_file())))},
260-
{"first line", json_numbert(id2string(first_location.get_line()))},
261-
{"last line", json_numbert(id2string(last_location.get_line()))}};
271+
json_objectt entry{{"function", json_stringt(function)}};
272+
if(auto file_name_opt = file_name_string_opt(first_location))
273+
entry["file"] = json_stringt{*file_name_opt};
274+
if(auto line_opt = line_string_opt(first_location))
275+
entry["firstLine"] = json_numbert{*line_opt};
276+
if(auto line_opt = line_string_opt(last_location))
277+
entry["lastLine"] = json_numbert{*line_opt};
262278

263279
dest.push_back(std::move(entry));
264280
}
@@ -272,12 +288,12 @@ static void xml_output_function(
272288
xmlt &x=dest.new_element("function");
273289

274290
x.set_attribute("name", id2string(function));
275-
x.set_attribute("file name",
276-
concat_dir_file(
277-
id2string(first_location.get_working_directory()),
278-
id2string(first_location.get_file())));
279-
x.set_attribute("first line", id2string(first_location.get_line()));
280-
x.set_attribute("last line", id2string(last_location.get_line()));
291+
if(auto file_name_opt = file_name_string_opt(first_location))
292+
x.set_attribute("file", *file_name_opt);
293+
if(auto line_opt = line_string_opt(first_location))
294+
x.set_attribute("first_line", *line_opt);
295+
if(auto line_opt = line_string_opt(last_location))
296+
x.set_attribute("last_line", *line_opt);
281297
}
282298

283299
static void list_functions(
@@ -357,12 +373,14 @@ static void list_functions(
357373
else
358374
{
359375
// text or console
360-
os << concat_dir_file(
361-
id2string(first_location.get_working_directory()),
362-
id2string(first_location.get_file())) << " "
363-
<< decl.base_name << " "
364-
<< first_location.get_line() << " "
365-
<< last_location.get_line() << "\n";
376+
if(auto file_name_opt = file_name_string_opt(first_location))
377+
os << *file_name_opt << ' ';
378+
os << decl.base_name;
379+
if(auto line_opt = line_string_opt(first_location))
380+
os << ' ' << *line_opt;
381+
if(auto line_opt = line_string_opt(last_location))
382+
os << ' ' << *line_opt;
383+
os << '\n';
366384
}
367385
}
368386

0 commit comments

Comments
 (0)