Skip to content

Commit d54c0b5

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 05eaa8d commit d54c0b5

File tree

1 file changed

+21
-48
lines changed

1 file changed

+21
-48
lines changed

src/solvers/strings/string_constraint_generator_format.cpp

Lines changed: 21 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -258,7 +258,8 @@ get_component_in_struct(const struct_exprt &expr, irep_idt component_name)
258258
/// specifier.
259259
/// \param fresh_symbol: generator of fresh symbols
260260
/// \param fs: a format specifier
261-
/// \param arg: a struct containing the possible value of the argument to format
261+
/// \param get_arg: a function returning the possible value of the argument to
262+
/// format given their type.
262263
/// \param index_type: type for indexes in strings
263264
/// \param char_type: type of characters
264265
/// \param array_pool: pool of arrays representing strings
@@ -267,61 +268,42 @@ get_component_in_struct(const struct_exprt &expr, irep_idt component_name)
267268
/// \return String expression representing the output of String.format.
268269
static std::pair<array_string_exprt, string_constraintst>
269270
add_axioms_for_format_specifier(
270-
symbol_generatort &fresh_symbol,
271-
const format_specifiert &fs,
272-
const struct_exprt &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-
{
271+
symbol_generatort &fresh_symbol, const format_specifiert &fs,
272+
const std::function<exprt(const irep_idt &)> &get_arg,
273+
const typet &index_type, const typet &char_type, array_poolt &array_pool,
274+
const messaget &message, const namespacet &ns) {
279275
string_constraintst constraints;
280276
const array_string_exprt res = array_pool.fresh_string(index_type, char_type);
281277
std::pair<exprt, string_constraintst> return_code;
282278
switch(fs.conversion)
283279
{
284280
case format_specifiert::DECIMAL_INTEGER:
285-
return_code = add_axioms_for_string_of_int(
286-
res, get_component_in_struct(arg, ID_int), 0, ns);
281+
return_code = add_axioms_for_string_of_int(res, get_arg(ID_int), 0, ns);
287282
return {res, std::move(return_code.second)};
288283
case format_specifiert::HEXADECIMAL_INTEGER:
289-
return_code =
290-
add_axioms_from_int_hex(res, get_component_in_struct(arg, ID_int));
284+
return_code = add_axioms_from_int_hex(res, get_arg(ID_int));
291285
return {res, std::move(return_code.second)};
292286
case format_specifiert::SCIENTIFIC:
293287
return_code = add_axioms_from_float_scientific_notation(
294-
fresh_symbol,
295-
res,
296-
get_component_in_struct(arg, ID_float),
297-
array_pool,
298-
ns);
288+
fresh_symbol, res, get_arg(ID_float), array_pool, ns);
299289
return {res, std::move(return_code.second)};
300290
case format_specifiert::DECIMAL_FLOAT:
301291
return_code = add_axioms_for_string_of_float(
302-
fresh_symbol,
303-
res,
304-
get_component_in_struct(arg, ID_float),
305-
array_pool,
306-
ns);
292+
fresh_symbol, res, get_arg(ID_float), array_pool, ns);
307293
return {res, std::move(return_code.second)};
308294
case format_specifiert::CHARACTER:
309-
return_code =
310-
add_axioms_from_char(res, get_component_in_struct(arg, ID_char));
295+
return_code = add_axioms_from_char(res, get_arg(ID_char));
311296
return {res, std::move(return_code.second)};
312297
case format_specifiert::BOOLEAN:
313-
return_code =
314-
add_axioms_from_bool(res, get_component_in_struct(arg, ID_boolean));
298+
return_code = add_axioms_from_bool(res, get_arg(ID_boolean));
315299
return {res, std::move(return_code.second)};
316300
case format_specifiert::STRING:
317301
{
318-
auto string_expr =
319-
get_string_expr(array_pool, get_component_in_struct(arg, "string_expr"));
302+
auto string_expr = get_string_expr(array_pool, get_arg("string_expr"));
320303
return {std::move(string_expr), {}};
321304
}
322305
case format_specifiert::HASHCODE:
323-
return_code = add_axioms_for_string_of_int(
324-
res, get_component_in_struct(arg, "hashcode"), 0, ns);
306+
return_code = add_axioms_for_string_of_int(res, get_arg("hashcode"), 0, ns);
325307
return {res, std::move(return_code.second)};
326308
case format_specifiert::LINE_SEPARATOR:
327309
// TODO: the constant should depend on the system: System.lineSeparator()
@@ -342,14 +324,8 @@ add_axioms_for_format_specifier(
342324
format_specifiert fs_lower = fs;
343325
fs_lower.conversion = tolower(fs.conversion);
344326
auto format_specifier_result = add_axioms_for_format_specifier(
345-
fresh_symbol,
346-
fs_lower,
347-
arg,
348-
index_type,
349-
char_type,
350-
array_pool,
351-
message,
352-
ns);
327+
fresh_symbol, fs_lower, get_arg, index_type, char_type, array_pool,
328+
message, ns);
353329

354330
const exprt return_code_upper_case =
355331
fresh_symbol("return_code_upper_case", get_return_code_type());
@@ -434,14 +410,11 @@ std::pair<exprt, string_constraintst> add_axioms_for_format(
434410
}
435411

436412
auto result = add_axioms_for_format_specifier(
437-
fresh_symbol,
438-
fs,
439-
to_struct_expr(arg),
440-
index_type,
441-
char_type,
442-
array_pool,
443-
message,
444-
ns);
413+
fresh_symbol, fs,
414+
[&](const irep_idt &id) {
415+
return get_component_in_struct(to_struct_expr(arg), id);
416+
},
417+
index_type, char_type, array_pool, message, ns);
445418
merge(constraints, std::move(result.second));
446419
intermediary_strings.push_back(result.first);
447420
}

0 commit comments

Comments
 (0)