Skip to content

Commit a2de4f8

Browse files
author
Joel Allred
committed
Refactor format support to not use get_arg function argument
Simplifies the interface by passing that string argument directly rather than the function that retrieves it.
1 parent e63d7b4 commit a2de4f8

File tree

1 file changed

+46
-22
lines changed

1 file changed

+46
-22
lines changed

src/solvers/strings/string_constraint_generator_format.cpp

Lines changed: 46 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,11 @@ static bool check_format_string(std::string s)
186186
}
187187
#endif
188188

189+
static exprt format_arg_from_string(
190+
const array_string_exprt &string,
191+
const irep_idt &id,
192+
array_poolt &array_pool);
193+
189194
/// Helper function for parsing format strings.
190195
/// This follows the implementation in openJDK of the java.util.Formatter class:
191196
/// http://hg.openjdk.java.net/jdk7/jdk7/jdk/file/9b8c96f96a0f/src/share/classes/java/util/Formatter.java#l2660
@@ -260,8 +265,7 @@ static exprt is_null(const array_string_exprt &string, array_poolt &array_pool)
260265
/// on the format specifier.
261266
/// \param fresh_symbol: generator of fresh symbols
262267
/// \param fs: a format specifier
263-
/// \param get_arg: a function returning the possible value of the argument to
264-
/// format given their type.
268+
/// \param string_arg: format string from argument
265269
/// \param index_type: type for indexes in strings
266270
/// \param char_type: type of characters
267271
/// \param array_pool: pool of arrays representing strings
@@ -272,7 +276,7 @@ static std::pair<array_string_exprt, string_constraintst>
272276
add_axioms_for_format_specifier(
273277
symbol_generatort &fresh_symbol,
274278
const format_specifiert &fs,
275-
const std::function<exprt(const irep_idt &)> &get_arg,
279+
const array_string_exprt &string_arg,
276280
const typet &index_type,
277281
const typet &char_type,
278282
array_poolt &array_pool,
@@ -285,24 +289,41 @@ add_axioms_for_format_specifier(
285289
switch(fs.conversion)
286290
{
287291
case format_specifiert::DECIMAL_INTEGER:
288-
return_code =
289-
add_axioms_for_string_of_int(res, get_arg(ID_int), 0, ns, array_pool);
292+
return_code = add_axioms_for_string_of_int(
293+
res,
294+
format_arg_from_string(string_arg, ID_int, array_pool),
295+
0,
296+
ns,
297+
array_pool);
290298
return {res, std::move(return_code.second)};
291299
case format_specifiert::HEXADECIMAL_INTEGER:
292300
return_code = add_axioms_for_string_of_int_with_radix(
293-
res, get_arg(ID_int), from_integer(16, index_type), 16, ns, array_pool);
301+
res,
302+
format_arg_from_string(string_arg, ID_int, array_pool),
303+
from_integer(16, index_type),
304+
16,
305+
ns,
306+
array_pool);
294307
return {res, std::move(return_code.second)};
295308
case format_specifiert::SCIENTIFIC:
296309
return_code = add_axioms_from_float_scientific_notation(
297-
fresh_symbol, res, get_arg(ID_float), array_pool, ns);
310+
fresh_symbol,
311+
res,
312+
format_arg_from_string(string_arg, ID_float, array_pool),
313+
array_pool,
314+
ns);
298315
return {res, std::move(return_code.second)};
299316
case format_specifiert::DECIMAL_FLOAT:
300317
return_code = add_axioms_for_string_of_float(
301-
fresh_symbol, res, get_arg(ID_float), array_pool, ns);
318+
fresh_symbol,
319+
res,
320+
format_arg_from_string(string_arg, ID_float, array_pool),
321+
array_pool,
322+
ns);
302323
return {res, std::move(return_code.second)};
303324
case format_specifiert::CHARACTER:
304325
{
305-
exprt arg_string = get_arg(ID_char);
326+
exprt arg_string = format_arg_from_string(string_arg, ID_char, array_pool);
306327
array_string_exprt &string_expr = to_array_string_expr(arg_string);
307328
// In the case the arg is null, the result will be equal to "null" and
308329
// and otherwise we just take the 4th character of the string.
@@ -325,17 +346,24 @@ add_axioms_for_format_specifier(
325346
return {res, constraints};
326347
}
327348
case format_specifiert::BOOLEAN:
328-
return_code = add_axioms_from_bool(res, get_arg(ID_boolean), array_pool);
349+
return_code = add_axioms_from_bool(
350+
res,
351+
format_arg_from_string(string_arg, ID_boolean, array_pool),
352+
array_pool);
329353
return {res, std::move(return_code.second)};
330354
case format_specifiert::STRING:
331355
{
332-
const exprt arg_string = get_arg("string_expr");
356+
const exprt arg_string = string_arg;
333357
const array_string_exprt string_expr = to_array_string_expr(arg_string);
334358
return {std::move(string_expr), {}};
335359
}
336360
case format_specifiert::HASHCODE:
337-
return_code =
338-
add_axioms_for_string_of_int(res, get_arg("hashcode"), 0, ns, array_pool);
361+
return_code = add_axioms_for_string_of_int(
362+
res,
363+
format_arg_from_string(string_arg, "hashcode", array_pool),
364+
0,
365+
ns,
366+
array_pool);
339367
return {res, std::move(return_code.second)};
340368
case format_specifiert::LINE_SEPARATOR:
341369
// TODO: the constant should depend on the system: System.lineSeparator()
@@ -358,7 +386,7 @@ add_axioms_for_format_specifier(
358386
auto format_specifier_result = add_axioms_for_format_specifier(
359387
fresh_symbol,
360388
fs_lower,
361-
get_arg,
389+
string_arg,
362390
index_type,
363391
char_type,
364392
array_pool,
@@ -463,13 +491,13 @@ std::pair<exprt, string_constraintst> add_axioms_for_format(
463491
const typet &char_type = res.content().type().subtype();
464492
const typet &index_type = res.length_type();
465493

494+
array_string_exprt string_arg;
495+
466496
for(const format_elementt &fe : format_strings)
467497
{
468498
if(fe.is_format_specifier())
469499
{
470500
const format_specifiert &fs = fe.get_format_specifier();
471-
std::function<exprt(const irep_idt &)> get_arg =
472-
[](const irep_idt &) -> exprt { UNREACHABLE; };
473501

474502
if(
475503
fs.conversion != format_specifiert::PERCENT_SIGN &&
@@ -497,17 +525,13 @@ std::pair<exprt, string_constraintst> add_axioms_for_format(
497525
INVARIANT(
498526
is_refined_string_type(arg.type()),
499527
"arguments of format should be strings");
500-
const array_string_exprt string_arg = get_string_expr(array_pool, arg);
501-
/// REMOVE get_arg below
502-
get_arg = [&](const irep_idt &id) {
503-
return format_arg_from_string(string_arg, id, array_pool);
504-
};
528+
string_arg = get_string_expr(array_pool, arg);
505529
}
506530

507531
auto result = add_axioms_for_format_specifier(
508532
fresh_symbol,
509533
fs,
510-
get_arg,
534+
string_arg,
511535
index_type,
512536
char_type,
513537
array_pool,

0 commit comments

Comments
 (0)