Skip to content

Commit 1828d43

Browse files
committed
dump-c: use a custom expr2c configuration
This avoids working around expr2c internals.
1 parent 5c3179c commit 1828d43

File tree

3 files changed

+44
-45
lines changed

3 files changed

+44
-45
lines changed

src/goto-instrument/dump_c.cpp

Lines changed: 39 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ Author: Daniel Kroening, [email protected]
2020
#include <util/replace_symbol.h>
2121
#include <util/string_utils.h>
2222

23-
#include <ansi-c/ansi_c_language.h>
24-
#include <cpp/cpp_language.h>
23+
#include <ansi-c/expr2c.h>
24+
#include <cpp/expr2cpp.h>
2525

2626
#include <linking/static_lifetime_init.h>
2727

@@ -526,7 +526,7 @@ void dump_ct::convert_compound(
526526
if(t.get_width()<=config.ansi_c.long_long_int_width)
527527
struct_body << "long long int " << comp_name
528528
<< " : " << t.get_width();
529-
else if(language->id()=="cpp")
529+
else if(mode == ID_cpp)
530530
struct_body << "__signedbv<" << t.get_width() << "> "
531531
<< comp_name;
532532
else
@@ -538,7 +538,7 @@ void dump_ct::convert_compound(
538538
if(t.get_width()<=config.ansi_c.long_long_int_width)
539539
struct_body << "unsigned long long " << comp_name
540540
<< " : " << t.get_width();
541-
else if(language->id()=="cpp")
541+
else if(mode == ID_cpp)
542542
struct_body << "__unsignedbv<" << t.get_width() << "> "
543543
<< comp_name;
544544
else
@@ -573,7 +573,7 @@ void dump_ct::convert_compound(
573573
os << type_to_string(unresolved_clean);
574574
if(!base_decls.str().empty())
575575
{
576-
PRECONDITION(language->id()=="cpp");
576+
PRECONDITION(mode == ID_cpp);
577577
os << ": " << base_decls.str();
578578
}
579579
os << '\n';
@@ -1521,22 +1521,49 @@ void dump_ct::cleanup_type(typet &type)
15211521
!type.get(ID_tag).empty());
15221522
}
15231523

1524+
static expr2c_configurationt expr2c_configuration()
1525+
{
1526+
// future TODO: with C++20 we can actually use designated initializers as
1527+
// commented out below
1528+
static expr2c_configurationt configuration{
1529+
/* .include_struct_padding_components = */ true,
1530+
/* .print_struct_body_in_type = */ true,
1531+
/* .include_array_size = */ true,
1532+
/* .true_string = */ "1",
1533+
/* .false_string = */ "0",
1534+
/* .use_library_macros = */ true,
1535+
/* .print_enum_int_value = */ false,
1536+
/* .expand_typedef = */ false};
1537+
1538+
return configuration;
1539+
}
1540+
15241541
std::string dump_ct::type_to_string(const typet &type)
15251542
{
15261543
std::string ret;
15271544
typet t=type;
15281545
cleanup_type(t);
1529-
language->from_type(t, ret, ns);
1530-
return ret;
1546+
1547+
if(mode == ID_C)
1548+
return type2c(t, ns, expr2c_configuration());
1549+
else if(mode == ID_cpp)
1550+
return type2cpp(t, ns);
1551+
else
1552+
UNREACHABLE;
15311553
}
15321554

15331555
std::string dump_ct::expr_to_string(const exprt &expr)
15341556
{
15351557
std::string ret;
15361558
exprt e=expr;
15371559
cleanup_expr(e);
1538-
language->from_expr(e, ret, ns);
1539-
return ret;
1560+
1561+
if(mode == ID_C)
1562+
return expr2c(e, ns, expr2c_configuration());
1563+
else if(mode == ID_cpp)
1564+
return expr2cpp(e, ns);
1565+
else
1566+
UNREACHABLE;
15401567
}
15411568

15421569
void dump_c(
@@ -1548,12 +1575,7 @@ void dump_c(
15481575
std::ostream &out)
15491576
{
15501577
dump_ct goto2c(
1551-
src,
1552-
use_system_headers,
1553-
use_all_headers,
1554-
include_harness,
1555-
ns,
1556-
new_ansi_c_language);
1578+
src, use_system_headers, use_all_headers, include_harness, ns, ID_C);
15571579
out << goto2c;
15581580
}
15591581

@@ -1566,12 +1588,7 @@ void dump_cpp(
15661588
std::ostream &out)
15671589
{
15681590
dump_ct goto2cpp(
1569-
src,
1570-
use_system_headers,
1571-
use_all_headers,
1572-
include_harness,
1573-
ns,
1574-
new_cpp_language);
1591+
src, use_system_headers, use_all_headers, include_harness, ns, ID_cpp);
15751592
out << goto2cpp;
15761593
}
15771594

@@ -1616,7 +1633,7 @@ void dump_c_type_header(
16161633
use_all_headers,
16171634
include_harness,
16181635
new_ns,
1619-
new_ansi_c_language,
1636+
ID_C,
16201637
dump_c_configurationt::type_header_configuration);
16211638
out << goto2c;
16221639
}

src/goto-instrument/dump_c_class.h

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@ Author: Daniel Kroening, [email protected]
1414

1515
#include <set>
1616
#include <string>
17-
#include <memory> // unique_ptr
18-
19-
#include <langapi/language.h>
20-
#include <langapi/mode.h>
2117

2218
#include <goto-programs/system_library_symbols.h>
2319

@@ -116,13 +112,13 @@ class dump_ct
116112
const bool use_all_headers,
117113
const bool include_harness,
118114
const namespacet &_ns,
119-
language_factoryt factory,
115+
const irep_idt &mode,
120116
const dump_c_configurationt config)
121117
: goto_functions(_goto_functions),
122118
copied_symbol_table(_ns.get_symbol_table()),
123119
ns(copied_symbol_table),
124120
dump_c_config(config),
125-
language(factory()),
121+
mode(mode),
126122
harness(include_harness),
127123
system_symbols(use_system_headers)
128124
{
@@ -135,14 +131,14 @@ class dump_ct
135131
const bool use_all_headers,
136132
const bool include_harness,
137133
const namespacet &_ns,
138-
language_factoryt factory)
134+
const irep_idt &mode)
139135
: dump_ct(
140136
_goto_functions,
141137
use_system_headers,
142138
use_all_headers,
143139
include_harness,
144140
_ns,
145-
factory,
141+
mode,
146142
dump_c_configurationt::default_configuration)
147143
{
148144
}
@@ -156,7 +152,7 @@ class dump_ct
156152
symbol_tablet copied_symbol_table;
157153
const namespacet ns;
158154
const dump_c_configurationt dump_c_config;
159-
std::unique_ptr<languaget> language;
155+
const irep_idt mode;
160156
const bool harness;
161157

162158
typedef std::unordered_set<irep_idt> convertedt;

src/goto-instrument/goto_program2code.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1893,20 +1893,6 @@ void goto_program2codet::cleanup_expr(exprt &expr, bool no_typecast)
18931893
}
18941894
else if(expr.type().id()==ID_pointer)
18951895
add_local_types(expr.type());
1896-
else if(expr.type().id() == ID_bool)
1897-
{
1898-
expr = typecast_exprt(
1899-
from_integer(
1900-
expr.is_true() ? 1 : 0, signedbv_typet(config.ansi_c.int_width)),
1901-
bool_typet());
1902-
}
1903-
else if(expr.type().id() == ID_c_bool)
1904-
{
1905-
expr = typecast_exprt(
1906-
from_integer(
1907-
expr.is_one() ? 1 : 0, signedbv_typet(config.ansi_c.int_width)),
1908-
c_bool_type());
1909-
}
19101896

19111897
const irept &c_sizeof_type=expr.find(ID_C_c_sizeof_type);
19121898

0 commit comments

Comments
 (0)