@@ -100,6 +100,17 @@ static void add_to_xml(
100
100
return ;
101
101
}
102
102
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
+
103
114
static void add_to_json (
104
115
const namespacet &ns,
105
116
const irep_idt &function_identifier,
@@ -114,12 +125,9 @@ static void add_to_json(
114
125
DATA_INVARIANT (end_function->is_end_function (),
115
126
" The last instruction in a goto-program must be END_FUNCTION" );
116
127
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};
123
131
124
132
json_arrayt &dead_ins=entry[" unreachableInstructions" ].make_array ();
125
133
@@ -243,22 +251,30 @@ bool static_unreachable_instructions(
243
251
return false ;
244
252
}
245
253
254
+ static optionalt<std::string>
255
+ line_string_opt (const source_locationt &source_location)
256
+ {
257
+ const irep_idt &line = source_location.get_line ();
246
258
259
+ if (line.empty ())
260
+ return nullopt;
261
+ else
262
+ return id2string (line);
263
+ }
247
264
248
265
static void json_output_function (
249
266
const irep_idt &function,
250
267
const source_locationt &first_location,
251
268
const source_locationt &last_location,
252
269
json_arrayt &dest)
253
270
{
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};
262
278
263
279
dest.push_back (std::move (entry));
264
280
}
@@ -272,12 +288,12 @@ static void xml_output_function(
272
288
xmlt &x=dest.new_element (" function" );
273
289
274
290
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 );
281
297
}
282
298
283
299
static void list_functions (
@@ -357,12 +373,14 @@ static void list_functions(
357
373
else
358
374
{
359
375
// 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 ' ;
366
384
}
367
385
}
368
386
0 commit comments