Skip to content

Commit 46ba4e7

Browse files
committed
C front-end: explicitly initialise nondet variables
This enables us to distinguish the case where the front-end intended a nondet initialiser from an undefined variable, such as a "#return_value" global before a call or on paths which throw an exception.
1 parent 17fa596 commit 46ba4e7

File tree

1 file changed

+10
-14
lines changed

1 file changed

+10
-14
lines changed

src/linking/static_lifetime_init.cpp

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,6 @@ static_lifetime_init(const irep_idt &identifier, symbol_tablet &symbol_table)
5353
if(type.id() == ID_code || type.id() == ID_empty)
5454
return {};
5555

56-
// We won't try to initialize any symbols that have
57-
// remained incomplete.
58-
59-
if(symbol.value.is_nil() && symbol.is_extern)
60-
// Compilers would usually complain about these
61-
// symbols being undefined.
62-
return {};
63-
6456
if(type.id() == ID_array && to_array_type(type).size().is_nil())
6557
{
6658
// C standard 6.9.2, paragraph 5
@@ -77,14 +69,18 @@ static_lifetime_init(const irep_idt &identifier, symbol_tablet &symbol_table)
7769
return {}; // do not initialize
7870
}
7971

80-
if(symbol.value.id() == ID_nondet)
81-
{
82-
return {}; // do not initialize
83-
}
84-
8572
exprt rhs;
8673

87-
if(symbol.value.is_nil())
74+
if((symbol.value.is_nil() && symbol.is_extern) ||
75+
symbol.value.id() == ID_nondet)
76+
{
77+
// Nondet initialise if not linked, or if explicitly requested.
78+
// Compilers would usually complain about the unlinked symbol case.
79+
const auto nondet = nondet_initializer(symbol.type, symbol.location, ns);
80+
CHECK_RETURN(nondet.has_value());
81+
rhs = *nondet;
82+
}
83+
else if(symbol.value.is_nil())
8884
{
8985
const auto zero = zero_initializer(symbol.type, symbol.location, ns);
9086
CHECK_RETURN(zero.has_value());

0 commit comments

Comments
 (0)