Skip to content

Commit 3a40db1

Browse files
author
thk123
committed
Make expr2ct configurable in a number of ways
This makes the subclassing of it redundant and should instead be customsied via the configuration.
1 parent 097cf71 commit 3a40db1

File tree

3 files changed

+98
-11
lines changed

3 files changed

+98
-11
lines changed

src/ansi-c/expr2c.cpp

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,12 @@ Author: Daniel Kroening, [email protected]
3030
#include "c_qualifiers.h"
3131
#include "expr2c_class.h"
3232

33+
expr2c_configurationt expr2c_configurationt::default_configuration{true,
34+
true,
35+
true,
36+
"TRUE",
37+
"FALSE"};
38+
3339
/*
3440
3541
Precedences are as follows. Higher values mean higher precedence.
@@ -661,7 +667,12 @@ std::string expr2ct::convert_struct_type(
661667
const std::string &qualifiers_str,
662668
const std::string &declarator_str)
663669
{
664-
return convert_struct_type(src, qualifiers_str, declarator_str, true, true);
670+
return convert_struct_type(
671+
src,
672+
qualifiers_str,
673+
declarator_str,
674+
configuration.print_struct_body_in_type,
675+
configuration.include_struct_padding_components);
665676
}
666677

667678
/// To generate C-like string for declaring (or defining) the given struct
@@ -734,7 +745,8 @@ std::string expr2ct::convert_array_type(
734745
const qualifierst &qualifiers,
735746
const std::string &declarator_str)
736747
{
737-
return convert_array_type(src, qualifiers, declarator_str, true);
748+
return convert_array_type(
749+
src, qualifiers, declarator_str, configuration.include_array_size);
738750
}
739751

740752
/// To generate a C-like type declaration of an array. Optionally can include or
@@ -1996,18 +2008,18 @@ std::string expr2ct::convert_constant(
19962008
/// FALSE.
19972009
std::string expr2ct::convert_constant_bool(bool boolean_value)
19982010
{
1999-
// C doesn't really have these
20002011
if(boolean_value)
2001-
return "TRUE";
2012+
return configuration.true_string;
20022013
else
2003-
return "FALSE";
2014+
return configuration.false_string;
20042015
}
20052016

20062017
std::string expr2ct::convert_struct(
20072018
const exprt &src,
20082019
unsigned &precedence)
20092020
{
2010-
return convert_struct(src, precedence, true);
2021+
return convert_struct(
2022+
src, precedence, configuration.include_struct_padding_components);
20112023
}
20122024

20132025
/// To generate a C-like string representing a struct. Can optionally include
@@ -3947,17 +3959,33 @@ std::string expr2ct::convert(const exprt &src)
39473959
return convert_with_precedence(src, precedence);
39483960
}
39493961

3950-
std::string expr2c(const exprt &expr, const namespacet &ns)
3962+
std::string expr2c(
3963+
const exprt &expr,
3964+
const namespacet &ns,
3965+
const expr2c_configurationt &configuration)
39513966
{
39523967
std::string code;
3953-
expr2ct expr2c(ns);
3968+
expr2ct expr2c(ns, configuration);
39543969
expr2c.get_shorthands(expr);
39553970
return expr2c.convert(expr);
39563971
}
39573972

3958-
std::string type2c(const typet &type, const namespacet &ns)
3973+
std::string expr2c(const exprt &expr, const namespacet &ns)
39593974
{
3960-
expr2ct expr2c(ns);
3975+
return expr2c(expr, ns, expr2c_configurationt::default_configuration);
3976+
}
3977+
3978+
std::string type2c(
3979+
const typet &type,
3980+
const namespacet &ns,
3981+
const expr2c_configurationt &configuration)
3982+
{
3983+
expr2ct expr2c(ns, configuration);
39613984
// expr2c.get_shorthands(expr);
39623985
return expr2c.convert(type);
39633986
}
3987+
3988+
std::string type2c(const typet &type, const namespacet &ns)
3989+
{
3990+
return type2c(type, ns, expr2c_configurationt::default_configuration);
3991+
}

src/ansi-c/expr2c.h

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,57 @@ class exprt;
1616
class namespacet;
1717
class typet;
1818

19+
/// Used for configuring the behaviour of expr2c and type2c
20+
struct expr2c_configurationt final
21+
{
22+
/// When printing struct_typet or struct_exprt, include the artificial padding
23+
/// components introduced to keep the struct aligned.
24+
bool include_struct_padding_components;
25+
26+
/// When printing a struct_typet, should the components of the struct be
27+
/// printed inline.
28+
bool print_struct_body_in_type;
29+
30+
/// When printing array_typet, should the size of the array be printed
31+
bool include_array_size;
32+
33+
/// This is the string that will be printed for the true boolean expression
34+
std::string true_string;
35+
36+
/// This is the string that will be printed for the false boolean expression
37+
std::string false_string;
38+
39+
expr2c_configurationt(
40+
const bool include_struct_padding_components,
41+
const bool print_struct_body_in_type,
42+
const bool include_array_size,
43+
std::string true_string,
44+
std::string false_string)
45+
: include_struct_padding_components(include_struct_padding_components),
46+
print_struct_body_in_type(print_struct_body_in_type),
47+
include_array_size(include_array_size),
48+
true_string(std::move(true_string)),
49+
false_string(std::move(false_string))
50+
{
51+
}
52+
53+
/// This prints a human readable C like syntax that closely mirrors the
54+
/// internals of the GOTO program
55+
static expr2c_configurationt default_configuration;
56+
};
57+
1958
std::string expr2c(const exprt &expr, const namespacet &ns);
59+
60+
std::string expr2c(
61+
const exprt &expr,
62+
const namespacet &ns,
63+
const expr2c_configurationt &configuration);
64+
2065
std::string type2c(const typet &type, const namespacet &ns);
2166

67+
std::string type2c(
68+
const typet &type,
69+
const namespacet &ns,
70+
const expr2c_configurationt &configuration);
71+
2272
#endif // CPROVER_ANSI_C_EXPR2C_H

src/ansi-c/expr2c_class.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Author: Daniel Kroening, [email protected]
1010
#ifndef CPROVER_ANSI_C_EXPR2C_CLASS_H
1111
#define CPROVER_ANSI_C_EXPR2C_CLASS_H
1212

13+
#include "expr2c.h"
14+
1315
#include <string>
1416
#include <unordered_map>
1517
#include <unordered_set>
@@ -23,7 +25,13 @@ class namespacet;
2325
class expr2ct
2426
{
2527
public:
26-
explicit expr2ct(const namespacet &_ns):ns(_ns), sizeof_nesting(0) { }
28+
explicit expr2ct(
29+
const namespacet &_ns,
30+
const expr2c_configurationt &configuration =
31+
expr2c_configurationt::default_configuration)
32+
: ns(_ns), configuration(configuration), sizeof_nesting(0)
33+
{
34+
}
2735
virtual ~expr2ct() { }
2836

2937
virtual std::string convert(const typet &src);
@@ -33,6 +41,7 @@ class expr2ct
3341

3442
protected:
3543
const namespacet &ns;
44+
const expr2c_configurationt &configuration;
3645

3746
virtual std::string convert_rec(
3847
const typet &src,

0 commit comments

Comments
 (0)