Skip to content

Commit 4f5e148

Browse files
author
Thomas Kiley
authored
Merge pull request #2713 from thk123/dump/expr2c-configuration
Configurable expr2c
2 parents 01b7418 + 6e04213 commit 4f5e148

File tree

3 files changed

+148
-11
lines changed

3 files changed

+148
-11
lines changed

src/ansi-c/expr2c.cpp

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

33+
// clang-format off
34+
35+
expr2c_configurationt expr2c_configurationt::default_configuration
36+
{
37+
true,
38+
true,
39+
true,
40+
"TRUE",
41+
"FALSE"
42+
};
43+
44+
expr2c_configurationt expr2c_configurationt::clean_configuration
45+
{
46+
false,
47+
false,
48+
false,
49+
"1",
50+
"0"
51+
};
52+
53+
// clang-format on
3354
/*
3455
3556
Precedences are as follows. Higher values mean higher precedence.
@@ -661,7 +682,12 @@ std::string expr2ct::convert_struct_type(
661682
const std::string &qualifiers_str,
662683
const std::string &declarator_str)
663684
{
664-
return convert_struct_type(src, qualifiers_str, declarator_str, true, true);
685+
return convert_struct_type(
686+
src,
687+
qualifiers_str,
688+
declarator_str,
689+
configuration.print_struct_body_in_type,
690+
configuration.include_struct_padding_components);
665691
}
666692

667693
/// To generate C-like string for declaring (or defining) the given struct
@@ -734,7 +760,8 @@ std::string expr2ct::convert_array_type(
734760
const qualifierst &qualifiers,
735761
const std::string &declarator_str)
736762
{
737-
return convert_array_type(src, qualifiers, declarator_str, true);
763+
return convert_array_type(
764+
src, qualifiers, declarator_str, configuration.include_array_size);
738765
}
739766

740767
/// To generate a C-like type declaration of an array. Optionally can include or
@@ -1996,18 +2023,18 @@ std::string expr2ct::convert_constant(
19962023
/// FALSE.
19972024
std::string expr2ct::convert_constant_bool(bool boolean_value)
19982025
{
1999-
// C doesn't really have these
20002026
if(boolean_value)
2001-
return "TRUE";
2027+
return configuration.true_string;
20022028
else
2003-
return "FALSE";
2029+
return configuration.false_string;
20042030
}
20052031

20062032
std::string expr2ct::convert_struct(
20072033
const exprt &src,
20082034
unsigned &precedence)
20092035
{
2010-
return convert_struct(src, precedence, true);
2036+
return convert_struct(
2037+
src, precedence, configuration.include_struct_padding_components);
20112038
}
20122039

20132040
/// To generate a C-like string representing a struct. Can optionally include
@@ -3947,17 +3974,55 @@ std::string expr2ct::convert(const exprt &src)
39473974
return convert_with_precedence(src, precedence);
39483975
}
39493976

3950-
std::string expr2c(const exprt &expr, const namespacet &ns)
3977+
/// Build a declaration string, which requires converting both a type and
3978+
/// putting an identifier in the syntactically correct position.
3979+
/// \param src: the type to convert
3980+
/// \param identifier: the identifier to use as the type
3981+
/// \return A C declaration of the given type with the right identifier.
3982+
std::string expr2ct::convert_with_identifier(
3983+
const typet &src,
3984+
const std::string &identifier)
3985+
{
3986+
return convert_rec(src, c_qualifierst(), identifier);
3987+
}
3988+
3989+
std::string expr2c(
3990+
const exprt &expr,
3991+
const namespacet &ns,
3992+
const expr2c_configurationt &configuration)
39513993
{
39523994
std::string code;
3953-
expr2ct expr2c(ns);
3995+
expr2ct expr2c(ns, configuration);
39543996
expr2c.get_shorthands(expr);
39553997
return expr2c.convert(expr);
39563998
}
39573999

3958-
std::string type2c(const typet &type, const namespacet &ns)
4000+
std::string expr2c(const exprt &expr, const namespacet &ns)
4001+
{
4002+
return expr2c(expr, ns, expr2c_configurationt::default_configuration);
4003+
}
4004+
4005+
std::string type2c(
4006+
const typet &type,
4007+
const namespacet &ns,
4008+
const expr2c_configurationt &configuration)
39594009
{
3960-
expr2ct expr2c(ns);
4010+
expr2ct expr2c(ns, configuration);
39614011
// expr2c.get_shorthands(expr);
39624012
return expr2c.convert(type);
39634013
}
4014+
4015+
std::string type2c(const typet &type, const namespacet &ns)
4016+
{
4017+
return type2c(type, ns, expr2c_configurationt::default_configuration);
4018+
}
4019+
4020+
std::string type2c(
4021+
const typet &type,
4022+
const std::string &identifier,
4023+
const namespacet &ns,
4024+
const expr2c_configurationt &configuration)
4025+
{
4026+
expr2ct expr2c(ns, configuration);
4027+
return expr2c.convert_with_identifier(type, identifier);
4028+
}

src/ansi-c/expr2c.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,67 @@ 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+
/// This prints compilable C that loses some of the internal details of the
58+
/// GOTO program
59+
static expr2c_configurationt clean_configuration;
60+
};
61+
1962
std::string expr2c(const exprt &expr, const namespacet &ns);
63+
64+
std::string expr2c(
65+
const exprt &expr,
66+
const namespacet &ns,
67+
const expr2c_configurationt &configuration);
68+
2069
std::string type2c(const typet &type, const namespacet &ns);
2170

71+
std::string type2c(
72+
const typet &type,
73+
const namespacet &ns,
74+
const expr2c_configurationt &configuration);
75+
76+
std::string type2c(
77+
const typet &type,
78+
const std::string &identifier,
79+
const namespacet &ns,
80+
const expr2c_configurationt &configuration);
81+
2282
#endif // CPROVER_ANSI_C_EXPR2C_H

src/ansi-c/expr2c_class.h

Lines changed: 13 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,16 +25,26 @@ 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);
3038
virtual std::string convert(const exprt &src);
3139

3240
void get_shorthands(const exprt &expr);
3341

42+
std::string
43+
convert_with_identifier(const typet &src, const std::string &identifier);
44+
3445
protected:
3546
const namespacet &ns;
47+
const expr2c_configurationt &configuration;
3648

3749
virtual std::string convert_rec(
3850
const typet &src,

0 commit comments

Comments
 (0)