Skip to content

Commit 8020211

Browse files
Refactor add_axioms_for_format_specifier
This makes it accept a function as argument, which gives for possible types the corresponding value of the argument. This will allow us to adapt more easily to a change in the way the arguments of format are given: they are now given as a struct but we want to have the possibility to give them as a string.
1 parent 733a88e commit 8020211

File tree

1 file changed

+39
-28
lines changed

1 file changed

+39
-28
lines changed

src/solvers/strings/string_constraint_generator_format.cpp

Lines changed: 39 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -257,60 +257,57 @@ static exprt get_component_in_struct(const struct_exprt &expr,
257257
/// specifier.
258258
/// \param fresh_symbol: generator of fresh symbols
259259
/// \param fs: a format specifier
260-
/// \param arg: a struct containing the possible value of the argument to format
260+
/// \param get_arg: a function returning the possible value of the argument to
261+
/// format given their type.
261262
/// \param index_type: type for indexes in strings
262263
/// \param char_type: type of characters
263264
/// \param array_pool: pool of arrays representing strings
264265
/// \param message: message handler for warnings
265266
/// \param ns: namespace
266267
/// \return String expression representing the output of String.format.
267268
static std::pair<array_string_exprt, string_constraintst>
268-
add_axioms_for_format_specifier(symbol_generatort &fresh_symbol,
269-
const format_specifiert &fs,
270-
const struct_exprt &arg,
271-
const typet &index_type, const typet &char_type,
272-
array_poolt &array_pool,
273-
const messaget &message, const namespacet &ns) {
269+
add_axioms_for_format_specifier(
270+
symbol_generatort &fresh_symbol,
271+
const format_specifiert &fs,
272+
const std::function<exprt(const irep_idt &)> &get_arg,
273+
const typet &index_type,
274+
const typet &char_type,
275+
array_poolt &array_pool,
276+
const messaget &message,
277+
const namespacet &ns)
278+
{
274279
string_constraintst constraints;
275280
const array_string_exprt res = array_pool.fresh_string(index_type, char_type);
276281
std::pair<exprt, string_constraintst> return_code;
277282
switch(fs.conversion)
278283
{
279284
case format_specifiert::DECIMAL_INTEGER:
280-
return_code = add_axioms_for_string_of_int(
281-
res, get_component_in_struct(arg, ID_int), 0, ns);
285+
return_code = add_axioms_for_string_of_int(res, get_arg(ID_int), 0, ns);
282286
return {res, std::move(return_code.second)};
283287
case format_specifiert::HEXADECIMAL_INTEGER:
284-
return_code =
285-
add_axioms_from_int_hex(res, get_component_in_struct(arg, ID_int));
288+
return_code = add_axioms_from_int_hex(res, get_arg(ID_int));
286289
return {res, std::move(return_code.second)};
287290
case format_specifiert::SCIENTIFIC:
288291
return_code = add_axioms_from_float_scientific_notation(
289-
fresh_symbol, res, get_component_in_struct(arg, ID_float), array_pool,
290-
ns);
292+
fresh_symbol, res, get_arg(ID_float), array_pool, ns);
291293
return {res, std::move(return_code.second)};
292294
case format_specifiert::DECIMAL_FLOAT:
293295
return_code = add_axioms_for_string_of_float(
294-
fresh_symbol, res, get_component_in_struct(arg, ID_float), array_pool,
295-
ns);
296+
fresh_symbol, res, get_arg(ID_float), array_pool, ns);
296297
return {res, std::move(return_code.second)};
297298
case format_specifiert::CHARACTER:
298-
return_code =
299-
add_axioms_from_char(res, get_component_in_struct(arg, ID_char));
299+
return_code = add_axioms_from_char(res, get_arg(ID_char));
300300
return {res, std::move(return_code.second)};
301301
case format_specifiert::BOOLEAN:
302-
return_code =
303-
add_axioms_from_bool(res, get_component_in_struct(arg, ID_boolean));
302+
return_code = add_axioms_from_bool(res, get_arg(ID_boolean));
304303
return {res, std::move(return_code.second)};
305304
case format_specifiert::STRING:
306305
{
307-
auto string_expr = get_string_expr(
308-
array_pool, get_component_in_struct(arg, "string_expr"));
306+
auto string_expr = get_string_expr(array_pool, get_arg("string_expr"));
309307
return {std::move(string_expr), {}};
310308
}
311309
case format_specifiert::HASHCODE:
312-
return_code = add_axioms_for_string_of_int(
313-
res, get_component_in_struct(arg, "hashcode"), 0, ns);
310+
return_code = add_axioms_for_string_of_int(res, get_arg("hashcode"), 0, ns);
314311
return {res, std::move(return_code.second)};
315312
case format_specifiert::LINE_SEPARATOR:
316313
// TODO: the constant should depend on the system: System.lineSeparator()
@@ -330,9 +327,15 @@ add_axioms_for_format_specifier(symbol_generatort &fresh_symbol,
330327
{
331328
format_specifiert fs_lower = fs;
332329
fs_lower.conversion = tolower(fs.conversion);
333-
auto format_specifier_result =
334-
add_axioms_for_format_specifier(fresh_symbol, fs_lower, arg, index_type,
335-
char_type, array_pool, message, ns);
330+
auto format_specifier_result = add_axioms_for_format_specifier(
331+
fresh_symbol,
332+
fs_lower,
333+
get_arg,
334+
index_type,
335+
char_type,
336+
array_pool,
337+
message,
338+
ns);
336339

337340
const exprt return_code_upper_case =
338341
fresh_symbol("return_code_upper_case", get_return_code_type());
@@ -417,8 +420,16 @@ std::pair<exprt, string_constraintst> add_axioms_for_format(
417420
}
418421

419422
auto result = add_axioms_for_format_specifier(
420-
fresh_symbol, fs, to_struct_expr(arg), index_type, char_type,
421-
array_pool, message, ns);
423+
fresh_symbol,
424+
fs,
425+
[&](const irep_idt &id) {
426+
return get_component_in_struct(to_struct_expr(arg), id);
427+
},
428+
index_type,
429+
char_type,
430+
array_pool,
431+
message,
432+
ns);
422433
merge(constraints, std::move(result.second));
423434
intermediary_strings.push_back(result.first);
424435
}

0 commit comments

Comments
 (0)