Skip to content

Commit f55f636

Browse files
committed
Some cleanup for restrict function pointer by name feature
1 parent 910be39 commit f55f636

File tree

4 files changed

+100
-70
lines changed

4 files changed

+100
-70
lines changed

src/goto-instrument/goto_instrument_parse_options.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1038,18 +1038,15 @@ void goto_instrument_parse_optionst::instrument_goto_program()
10381038
parse_function_pointer_restriction_options_from_cmdline(cmdline, options);
10391039

10401040
if(
1041-
!options.get_list_option(RESTRICT_FUNCTION_POINTER_OPT).empty() ||
1042-
!options.get_list_option(RESTRICT_FUNCTION_POINTER_BY_NAME_OPT).empty() ||
1043-
!options.get_list_option(RESTRICT_FUNCTION_POINTER_FROM_FILE_OPT).empty())
1041+
options.is_set(RESTRICT_FUNCTION_POINTER_OPT) ||
1042+
options.is_set(RESTRICT_FUNCTION_POINTER_BY_NAME_OPT) ||
1043+
options.is_set(RESTRICT_FUNCTION_POINTER_FROM_FILE_OPT))
10441044
{
10451045
label_function_pointer_call_sites(goto_model);
10461046

1047-
auto const by_name_restrictions =
1048-
get_function_pointer_by_name_restrictions(goto_model, options);
1049-
10501047
const auto function_pointer_restrictions =
1051-
by_name_restrictions.merge(function_pointer_restrictionst::from_options(
1052-
options, log.get_message_handler()));
1048+
function_pointer_restrictionst::from_options(
1049+
options, goto_model, log.get_message_handler());
10531050

10541051
restrict_function_pointers(goto_model, function_pointer_restrictions);
10551052
}

src/goto-programs/restrict_function_pointers.cpp

Lines changed: 67 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -215,15 +215,26 @@ void parse_function_pointer_restriction_options_from_cmdline(
215215
const cmdlinet &cmdline,
216216
optionst &options)
217217
{
218-
options.set_option(
219-
RESTRICT_FUNCTION_POINTER_OPT,
220-
cmdline.get_values(RESTRICT_FUNCTION_POINTER_OPT));
221-
options.set_option(
222-
RESTRICT_FUNCTION_POINTER_FROM_FILE_OPT,
223-
cmdline.get_values(RESTRICT_FUNCTION_POINTER_FROM_FILE_OPT));
224-
options.set_option(
225-
RESTRICT_FUNCTION_POINTER_BY_NAME_OPT,
226-
cmdline.get_values(RESTRICT_FUNCTION_POINTER_BY_NAME_OPT));
218+
if(cmdline.isset(RESTRICT_FUNCTION_POINTER_OPT))
219+
{
220+
options.set_option(
221+
RESTRICT_FUNCTION_POINTER_OPT,
222+
cmdline.get_values(RESTRICT_FUNCTION_POINTER_OPT));
223+
}
224+
225+
if(cmdline.isset(RESTRICT_FUNCTION_POINTER_FROM_FILE_OPT))
226+
{
227+
options.set_option(
228+
RESTRICT_FUNCTION_POINTER_FROM_FILE_OPT,
229+
cmdline.get_values(RESTRICT_FUNCTION_POINTER_FROM_FILE_OPT));
230+
}
231+
232+
if(cmdline.isset(RESTRICT_FUNCTION_POINTER_BY_NAME_OPT))
233+
{
234+
options.set_option(
235+
RESTRICT_FUNCTION_POINTER_BY_NAME_OPT,
236+
cmdline.get_values(RESTRICT_FUNCTION_POINTER_BY_NAME_OPT));
237+
}
227238
}
228239

229240
function_pointer_restrictionst::restrictionst
@@ -249,17 +260,17 @@ function_pointer_restrictionst::merge_function_pointer_restrictions(
249260
}
250261

251262
function_pointer_restrictionst::restrictionst
252-
parse_function_pointer_restrictions_from_command_line(
263+
function_pointer_restrictionst::parse_function_pointer_restrictions(
253264
const std::list<std::string> &restriction_opts,
254-
const std::string &option_name)
265+
const std::string &option)
255266
{
256267
auto function_pointer_restrictions =
257268
function_pointer_restrictionst::restrictionst{};
258269

259270
for(const std::string &restriction_opt : restriction_opts)
260271
{
261272
const auto restriction =
262-
parse_function_pointer_restriction(restriction_opt);
273+
parse_function_pointer_restriction(restriction_opt, option);
263274

264275
const bool inserted = function_pointer_restrictions
265276
.emplace(restriction.first, restriction.second)
@@ -270,23 +281,34 @@ parse_function_pointer_restrictions_from_command_line(
270281
throw invalid_command_line_argument_exceptiont{
271282
"function pointer restriction for `" + id2string(restriction.first) +
272283
"' was specified twice",
273-
"--" RESTRICT_FUNCTION_POINTER_OPT};
284+
option};
274285
}
275286
}
276287

277288
return function_pointer_restrictions;
278289
}
279290

291+
292+
function_pointer_restrictionst::restrictionst
293+
function_pointer_restrictionst::
294+
parse_function_pointer_restrictions_from_command_line(
295+
const std::list<std::string> &restriction_opts)
296+
{
297+
return parse_function_pointer_restrictions(
298+
restriction_opts,
299+
"--" RESTRICT_FUNCTION_POINTER_OPT);
300+
}
301+
280302
function_pointer_restrictionst::restrictionst
281303
function_pointer_restrictionst::parse_function_pointer_restrictions_from_file(
282304
const std::list<std::string> &filenames,
283305
message_handlert &message_handler)
284306
{
285307
auto merged_restrictions = function_pointer_restrictionst::restrictionst{};
308+
286309
for(auto const &filename : filenames)
287310
{
288-
auto const restrictions =
289-
function_pointer_restrictionst::read_from_file(filename, message_handler);
311+
auto const restrictions = read_from_file(filename, message_handler);
290312

291313
merged_restrictions = merge_function_pointer_restrictions(
292314
std::move(merged_restrictions), restrictions.restrictions);
@@ -297,7 +319,8 @@ function_pointer_restrictionst::parse_function_pointer_restrictions_from_file(
297319

298320
function_pointer_restrictionst::restrictiont
299321
function_pointer_restrictionst::parse_function_pointer_restriction(
300-
const std::string &restriction_opt)
322+
const std::string &restriction_opt,
323+
const std::string &option)
301324
{
302325
// the format for restrictions is <pointer_name>/<target[,more_targets]*>
303326
// exactly one pointer and at least one target
@@ -310,23 +333,23 @@ function_pointer_restrictionst::parse_function_pointer_restriction(
310333
{
311334
throw invalid_command_line_argument_exceptiont{
312335
"couldn't find '/' in `" + restriction_opt + "'",
313-
"--" RESTRICT_FUNCTION_POINTER_OPT,
336+
option,
314337
restriction_format_message};
315338
}
316339

317340
if(pointer_name_end == restriction_opt.size())
318341
{
319342
throw invalid_command_line_argument_exceptiont{
320343
"couldn't find names of targets after '/' in `" + restriction_opt + "'",
321-
"--" RESTRICT_FUNCTION_POINTER_OPT,
344+
option,
322345
restriction_format_message};
323346
}
324347

325348
if(pointer_name_end == 0)
326349
{
327350
throw invalid_command_line_argument_exceptiont{
328351
"couldn't find target name before '/' in `" + restriction_opt + "'",
329-
"--" RESTRICT_FUNCTION_POINTER_OPT};
352+
option};
330353
}
331354

332355
auto const pointer_name = restriction_opt.substr(0, pointer_name_end);
@@ -338,7 +361,7 @@ function_pointer_restrictionst::parse_function_pointer_restriction(
338361
{
339362
throw invalid_command_line_argument_exceptiont{
340363
"missing target list for function pointer restriction " + pointer_name,
341-
"--" RESTRICT_FUNCTION_POINTER_OPT,
364+
option,
342365
restriction_format_message};
343366
}
344367

@@ -351,7 +374,7 @@ function_pointer_restrictionst::parse_function_pointer_restriction(
351374
{
352375
throw invalid_command_line_argument_exceptiont(
353376
"leading or trailing comma in restrictions for `" + pointer_name + "'",
354-
"--" RESTRICT_FUNCTION_POINTER_OPT,
377+
option,
355378
restriction_format_message);
356379
}
357380
}
@@ -361,18 +384,27 @@ function_pointer_restrictionst::parse_function_pointer_restriction(
361384

362385
function_pointer_restrictionst function_pointer_restrictionst::from_options(
363386
const optionst &options,
387+
const goto_modelt &goto_model,
364388
message_handlert &message_handler)
365389
{
366390
auto const restriction_opts =
367391
options.get_list_option(RESTRICT_FUNCTION_POINTER_OPT);
368392
auto const commandline_restrictions =
369-
parse_function_pointer_restrictions_from_command_line(
370-
restriction_opts, RESTRICT_FUNCTION_POINTER_OPT);
393+
parse_function_pointer_restrictions_from_command_line(restriction_opts);
394+
395+
auto const restriction_file_opts =
396+
options.get_list_option(RESTRICT_FUNCTION_POINTER_FROM_FILE_OPT);
371397
auto const file_restrictions = parse_function_pointer_restrictions_from_file(
372-
options.get_list_option(RESTRICT_FUNCTION_POINTER_FROM_FILE_OPT),
373-
message_handler);
398+
restriction_file_opts, message_handler);
399+
400+
auto const restriction_name_opts =
401+
options.get_list_option(RESTRICT_FUNCTION_POINTER_BY_NAME_OPT);
402+
auto const name_restrictions = get_function_pointer_by_name_restrictions(
403+
restriction_name_opts, goto_model);
404+
374405
return {merge_function_pointer_restrictions(
375-
std::move(file_restrictions), commandline_restrictions)};
406+
commandline_restrictions,
407+
merge_function_pointer_restrictions(file_restrictions, name_restrictions))};
376408
}
377409

378410
function_pointer_restrictionst
@@ -465,21 +497,16 @@ void function_pointer_restrictionst::write_to_file(
465497

466498
function_pointer_restrictions_json.output(outFile);
467499
}
468-
function_pointer_restrictionst function_pointer_restrictionst::merge(
469-
const function_pointer_restrictionst &other) const
470-
{
471-
return function_pointer_restrictionst{
472-
merge_function_pointer_restrictions(restrictions, other.restrictions)};
473-
}
474500

475-
function_pointer_restrictionst get_function_pointer_by_name_restrictions(
476-
const goto_modelt &goto_model,
477-
const optionst &options)
501+
function_pointer_restrictionst::restrictionst
502+
function_pointer_restrictionst::get_function_pointer_by_name_restrictions(
503+
const std::list<std::string> &restriction_name_opts,
504+
const goto_modelt &goto_model)
478505
{
479506
function_pointer_restrictionst::restrictionst by_name_restrictions =
480-
parse_function_pointer_restrictions_from_command_line(
481-
options.get_list_option(RESTRICT_FUNCTION_POINTER_BY_NAME_OPT),
482-
RESTRICT_FUNCTION_POINTER_BY_NAME_OPT);
507+
parse_function_pointer_restrictions(
508+
restriction_name_opts, RESTRICT_FUNCTION_POINTER_BY_NAME_OPT);
509+
483510
function_pointer_restrictionst::restrictionst restrictions;
484511
for(auto const &goto_function : goto_model.goto_functions.function_map)
485512
{
@@ -520,5 +547,6 @@ function_pointer_restrictionst get_function_pointer_by_name_restrictions(
520547
}
521548
});
522549
}
523-
return function_pointer_restrictionst{restrictions};
550+
551+
return restrictions;
524552
}

src/goto-programs/restrict_function_pointers.h

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,11 @@ class function_pointer_restrictionst
6666

6767
const restrictionst restrictions;
6868

69-
/// parse function pointer restrictions from command line
70-
///
71-
/// Note: These are are only syntactically checked at this stage,
72-
/// because type checking them requires a goto_modelt
73-
static function_pointer_restrictionst
74-
from_options(const optionst &options, message_handlert &message_handler);
69+
/// Parse function pointer restrictions from command line
70+
static function_pointer_restrictionst from_options(
71+
const optionst &options,
72+
const goto_modelt &goto_model,
73+
message_handlert &message_handler);
7574

7675
jsont to_json() const;
7776
static function_pointer_restrictionst from_json(const jsont &json);
@@ -82,9 +81,6 @@ class function_pointer_restrictionst
8281

8382
void write_to_file(const std::string &filename) const;
8483

85-
function_pointer_restrictionst
86-
merge(const function_pointer_restrictionst &other) const;
87-
8884
protected:
8985
static restrictionst merge_function_pointer_restrictions(
9086
restrictionst lhs,
@@ -97,13 +93,18 @@ class function_pointer_restrictionst
9793
const std::list<std::string> &filenames,
9894
message_handlert &message_handler);
9995

100-
static restrictiont
101-
parse_function_pointer_restriction(const std::string &restriction_opt);
102-
};
96+
static restrictionst parse_function_pointer_restrictions(
97+
const std::list<std::string> &restriction_opts,
98+
const std::string &option);
10399

104-
function_pointer_restrictionst get_function_pointer_by_name_restrictions(
105-
const goto_modelt &goto_model,
106-
const optionst &options);
100+
static restrictiont parse_function_pointer_restriction(
101+
const std::string &restriction_opt,
102+
const std::string &option);
103+
104+
static restrictionst get_function_pointer_by_name_restrictions(
105+
const std::list<std::string> &restriction_name_opts,
106+
const goto_modelt &goto_model);
107+
};
107108

108109
/// Apply function pointer restrictions to a goto_model. Each restriction is a
109110
/// mapping from a pointer name to a set of possible targets. Replace calls of

unit/goto-programs/restrict_function_pointers.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -23,43 +23,47 @@ void restriction_parsing_test()
2323
{
2424
{
2525
const auto res =
26-
fp_restrictionst::parse_function_pointer_restriction("func1/func2");
26+
fp_restrictionst::parse_function_pointer_restriction(
27+
"func1/func2", "test");
2728
REQUIRE(res.first == "func1");
2829
REQUIRE(res.second.size() == 1);
2930
REQUIRE(res.second.find("func2") != res.second.end());
3031
}
3132

3233
{
3334
const auto res =
34-
fp_restrictionst::parse_function_pointer_restriction("func1/func2,func3");
35+
fp_restrictionst::parse_function_pointer_restriction(
36+
"func1/func2,func3", "test");
3537
REQUIRE(res.first == "func1");
3638
REQUIRE(res.second.size() == 2);
3739
REQUIRE(res.second.find("func2") != res.second.end());
3840
REQUIRE(res.second.find("func3") != res.second.end());
3941
}
4042

4143
REQUIRE_THROWS_AS(
42-
fp_restrictionst::parse_function_pointer_restriction("func"),
44+
fp_restrictionst::parse_function_pointer_restriction("func", "test"),
4345
invalid_command_line_argument_exceptiont);
4446

4547
REQUIRE_THROWS_AS(
46-
fp_restrictionst::parse_function_pointer_restriction("/func"),
48+
fp_restrictionst::parse_function_pointer_restriction("/func", "test"),
4749
invalid_command_line_argument_exceptiont);
4850

4951
REQUIRE_THROWS_AS(
50-
fp_restrictionst::parse_function_pointer_restriction("func/"),
52+
fp_restrictionst::parse_function_pointer_restriction("func/", "test"),
5153
invalid_command_line_argument_exceptiont);
5254

5355
REQUIRE_THROWS_AS(
54-
fp_restrictionst::parse_function_pointer_restriction("func/,"),
56+
fp_restrictionst::parse_function_pointer_restriction("func/,", "test"),
5557
invalid_command_line_argument_exceptiont);
5658

5759
REQUIRE_THROWS_AS(
58-
fp_restrictionst::parse_function_pointer_restriction("func1/func2,"),
60+
fp_restrictionst::parse_function_pointer_restriction(
61+
"func1/func2,", "test"),
5962
invalid_command_line_argument_exceptiont);
6063

6164
REQUIRE_THROWS_AS(
62-
fp_restrictionst::parse_function_pointer_restriction("func1/,func2"),
65+
fp_restrictionst::parse_function_pointer_restriction(
66+
"func1/,func2", "test"),
6367
invalid_command_line_argument_exceptiont);
6468
}
6569

0 commit comments

Comments
 (0)