Skip to content

Commit 8101a98

Browse files
tautschnigDaniel Kroening
authored and
Daniel Kroening
committed
C++ front-end: Remove internal symbols before linking
This mirrors what the C front-end already does, but requires changes to constant propagation in the C++ front-end: we previously used macros, which serve a very different purpose otherwise. Fixes: #1490
1 parent 955f166 commit 8101a98

10 files changed

+74
-19
lines changed

regression/goto-cc-cbmc/chain.sh

+6-5
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@ is_windows=$3
66

77
options=${*:4:$#-4}
88
name=${*:$#}
9-
name=${name%.c}
9+
base_name=${name%.c}
10+
base_name=${base_name%.cpp}
1011

1112
if [[ "${is_windows}" == "true" ]]; then
12-
"${goto_cc}" "${name}.c"
13-
mv "${name}.exe" "${name}.gb"
13+
"${goto_cc}" "${name}"
14+
mv "${base_name}.exe" "${base_name}.gb"
1415
else
15-
"${goto_cc}" "${name}.c" -o "${name}.gb"
16+
"${goto_cc}" "${name}" -o "${base_name}.gb"
1617
fi
1718

18-
"${cbmc}" "${name}.gb" ${options}
19+
"${cbmc}" "${base_name}.gb" ${options}

regression/goto-cc-cbmc/cpp/main.cpp

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
int main()
2+
{
3+
return 0;
4+
}

regression/goto-cc-cbmc/cpp/test.desc

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
CORE
2+
main.cpp
3+
4+
^EXIT=0$
5+
^SIGNAL=0$
6+
^VERIFICATION SUCCESSFUL$
7+
--
8+
^warning: ignoring

src/cpp/cpp_declarator_converter.cpp

-5
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,6 @@ symbolt &cpp_declarator_convertert::convert_new_symbol(
460460
symbol.is_macro=is_typedef && !is_template_parameter;
461461
symbol.pretty_name=pretty_name;
462462

463-
// Constant? These are propagated.
464-
if(symbol.type.get_bool(ID_C_constant) &&
465-
symbol.value.is_not_nil())
466-
symbol.is_macro=true;
467-
468463
if(is_code && !symbol.is_type)
469464
{
470465
// it is a function

src/cpp/cpp_instantiate_template.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,16 @@ std::string cpp_typecheckt::template_suffix(
5454
else // expression
5555
{
5656
exprt e=expr;
57+
58+
if(e.id() == ID_symbol)
59+
{
60+
const symbol_exprt &s = to_symbol_expr(e);
61+
const symbolt &symbol = lookup(s.get_identifier());
62+
63+
if(cpp_is_pod(symbol.type) && symbol.type.get_bool(ID_C_constant))
64+
e = symbol.value;
65+
}
66+
5767
make_constant(e);
5868

5969
// this must be a constant, which includes true/false

src/cpp/cpp_language.cpp

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ Author: Daniel Kroening, [email protected]
2020
#include <util/get_base_name.h>
2121

2222
#include <linking/linking.h>
23+
#include <linking/remove_internal_symbols.h>
2324

2425
#include <ansi-c/ansi_c_entry_point.h>
2526
#include <ansi-c/c_preprocess.h>
@@ -132,6 +133,8 @@ bool cpp_languaget::typecheck(
132133
cpp_parse_tree, new_symbol_table, module, get_message_handler()))
133134
return true;
134135

136+
remove_internal_symbols(new_symbol_table, get_message_handler(), false);
137+
135138
return linking(symbol_table, new_symbol_table, get_message_handler());
136139
}
137140

src/cpp/cpp_typecheck_compound_type.cpp

+11-7
Original file line numberDiff line numberDiff line change
@@ -733,13 +733,6 @@ void cpp_typecheckt::typecheck_compound_declarator(
733733
{
734734
new_symbol->value.swap(value);
735735
c_typecheck_baset::do_initializer(*new_symbol);
736-
737-
// these are macros if they are PODs and come with a (constant) value
738-
if(new_symbol->type.get_bool(ID_C_constant))
739-
{
740-
simplify(new_symbol->value, *this);
741-
new_symbol->is_macro=true;
742-
}
743736
}
744737
else
745738
{
@@ -771,7 +764,18 @@ void cpp_typecheckt::check_fixed_size_array(typet &type)
771764
array_typet &array_type=to_array_type(type);
772765

773766
if(array_type.size().is_not_nil())
767+
{
768+
if(array_type.size().id() == ID_symbol)
769+
{
770+
const symbol_exprt &s = to_symbol_expr(array_type.size());
771+
const symbolt &symbol = lookup(s.get_identifier());
772+
773+
if(cpp_is_pod(symbol.type) && symbol.type.get_bool(ID_C_constant))
774+
array_type.size() = symbol.value;
775+
}
776+
774777
make_constant_index(array_type.size());
778+
}
775779

776780
// recursive call for multi-dimensional arrays
777781
check_fixed_size_array(array_type.subtype());

src/cpp/cpp_typecheck_enum_type.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,8 @@ void cpp_typecheckt::typecheck_enum_body(symbolt &enum_symbol)
6363
symbol.type=enum_tag_type;
6464
symbol.is_type=false;
6565
symbol.is_macro=true;
66+
symbol.is_file_local = true;
67+
symbol.is_thread_local = true;
6668

6769
symbolt *new_symbol;
6870
if(symbol_table.move(symbol, new_symbol))

src/cpp/cpp_typecheck_resolve.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ Author: Daniel Kroening, [email protected]
2222
#include <util/c_types.h>
2323
#include <util/mathematical_types.h>
2424
#include <util/prefix.h>
25+
#include <util/simplify_expr.h>
2526
#include <util/std_expr.h>
2627
#include <util/std_types.h>
2728
#include <util/string_constant.h>
@@ -1046,6 +1047,26 @@ struct_tag_typet cpp_typecheck_resolvet::disambiguate_template_classes(
10461047
source_location,
10471048
primary_template_symbol,
10481049
full_template_args);
1050+
1051+
for(auto &arg : full_template_args_tc.arguments())
1052+
{
1053+
if(arg.id() == ID_type)
1054+
continue;
1055+
if(arg.id() == ID_symbol)
1056+
{
1057+
const symbol_exprt &s = to_symbol_expr(arg);
1058+
const symbolt &symbol = cpp_typecheck.lookup(s.get_identifier());
1059+
1060+
if(
1061+
cpp_typecheck.cpp_is_pod(symbol.type) &&
1062+
symbol.type.get_bool(ID_C_constant))
1063+
{
1064+
arg = symbol.value;
1065+
}
1066+
}
1067+
simplify(arg, cpp_typecheck);
1068+
}
1069+
10491070
// go back to where we used to be
10501071
}
10511072

src/linking/remove_internal_symbols.cpp

+9-2
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ void remove_internal_symbols(
9595
special.insert(CPROVER_PREFIX "deallocated");
9696
special.insert(CPROVER_PREFIX "dead_object");
9797
special.insert(CPROVER_PREFIX "rounding_mode");
98+
special.insert("__new");
99+
special.insert("__new_array");
100+
special.insert("__placement_new");
101+
special.insert("__placement_new_array");
102+
special.insert("__delete");
103+
special.insert("__delete_array");
98104

99105
for(symbol_tablet::symbolst::const_iterator
100106
it=symbol_table.symbols.begin();
@@ -139,8 +145,9 @@ void remove_internal_symbols(
139145
{
140146
// body? not local (i.e., "static")?
141147
if(
142-
has_body && (!is_file_local || (config.main.has_value() &&
143-
symbol.name == config.main.value())))
148+
has_body &&
149+
(!is_file_local ||
150+
(config.main.has_value() && symbol.base_name == config.main.value())))
144151
{
145152
get_symbols(ns, symbol, exported);
146153
}

0 commit comments

Comments
 (0)