Skip to content

Commit 167877a

Browse files
committed
dump-c: use a custom expr2c configuration
This avoids working around expr2c internals.
1 parent 10583e9 commit 167877a

File tree

3 files changed

+43
-45
lines changed

3 files changed

+43
-45
lines changed

src/goto-instrument/dump_c.cpp

Lines changed: 38 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,48 @@ void dump_ct::cleanup_type(typet &type)
15211521
!type.get(ID_tag).empty());
15221522
}
15231523

1524+
static expr2c_configurationt expr2c_configuration()
1525+
{
1526+
static expr2c_configurationt configuration{
1527+
.include_struct_padding_components = true,
1528+
.print_struct_body_in_type = true,
1529+
.include_array_size = true,
1530+
.true_string = "1",
1531+
.false_string = "0",
1532+
.use_library_macros = true,
1533+
.print_enum_int_value = false,
1534+
.expand_typedef = false
1535+
};
1536+
1537+
return configuration;
1538+
}
1539+
15241540
std::string dump_ct::type_to_string(const typet &type)
15251541
{
15261542
std::string ret;
15271543
typet t=type;
15281544
cleanup_type(t);
1529-
language->from_type(t, ret, ns);
1530-
return ret;
1545+
1546+
if(mode == ID_C)
1547+
return type2c(t, ns, expr2c_configuration());
1548+
else if(mode == ID_cpp)
1549+
return type2cpp(t, ns);
1550+
else
1551+
UNREACHABLE;
15311552
}
15321553

15331554
std::string dump_ct::expr_to_string(const exprt &expr)
15341555
{
15351556
std::string ret;
15361557
exprt e=expr;
15371558
cleanup_expr(e);
1538-
language->from_expr(e, ret, ns);
1539-
return ret;
1559+
1560+
if(mode == ID_C)
1561+
return expr2c(e, ns, expr2c_configuration());
1562+
else if(mode == ID_cpp)
1563+
return expr2cpp(e, ns);
1564+
else
1565+
UNREACHABLE;
15401566
}
15411567

15421568
void dump_c(
@@ -1548,12 +1574,7 @@ void dump_c(
15481574
std::ostream &out)
15491575
{
15501576
dump_ct goto2c(
1551-
src,
1552-
use_system_headers,
1553-
use_all_headers,
1554-
include_harness,
1555-
ns,
1556-
new_ansi_c_language);
1577+
src, use_system_headers, use_all_headers, include_harness, ns, ID_C);
15571578
out << goto2c;
15581579
}
15591580

@@ -1566,12 +1587,7 @@ void dump_cpp(
15661587
std::ostream &out)
15671588
{
15681589
dump_ct goto2cpp(
1569-
src,
1570-
use_system_headers,
1571-
use_all_headers,
1572-
include_harness,
1573-
ns,
1574-
new_cpp_language);
1590+
src, use_system_headers, use_all_headers, include_harness, ns, ID_cpp);
15751591
out << goto2cpp;
15761592
}
15771593

@@ -1616,7 +1632,7 @@ void dump_c_type_header(
16161632
use_all_headers,
16171633
include_harness,
16181634
new_ns,
1619-
new_ansi_c_language,
1635+
ID_C,
16201636
dump_c_configurationt::type_header_configuration);
16211637
out << goto2c;
16221638
}

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)