Skip to content

Commit 4c03709

Browse files
tautschnigRemi Delmas
and
Remi Delmas
committed
local_bitvector_analysist: track whether an object may be alive
A local variable is alive if it has been declared and not yet been marked dead. Co-authored-by: Remi Delmas <[email protected]>
1 parent 9a1d2b3 commit 4c03709

File tree

4 files changed

+66
-16
lines changed

4 files changed

+66
-16
lines changed
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
int foo(int i)
2+
{
3+
int x;
4+
if(x > 0)
5+
{
6+
int i;
7+
}
8+
else
9+
{
10+
int j;
11+
}
12+
return i;
13+
}
14+
15+
int main()
16+
{
17+
foo(42);
18+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
CORE
2+
main.c
3+
--show-local-bitvector-analysis
4+
activate-multi-line-match
5+
\*\*\*\* file main.c line 7 function foo\n(\n|.)*\s+foo::i: \+unknown\n(\n|.)*\n\s+// \d+ file main.c line 7 function foo\n\s+DEAD foo::1::1::i
6+
\*\*\*\* file main.c line 7 function foo\n(\n|.)*\s+foo::1::2::j: \+unknown\n(\n|.)*\n\s+// \d+ file main.c line 7 function foo\n\s+DEAD foo::1::1::i
7+
\*\*\*\* file main.c line 7 function foo\n(\n|.)*\s+foo::1::x: \+uninitialized\+maybe_alive\n(\n|.)*\n\s+// \d+ file main.c line 7 function foo\n\s+DEAD foo::1::1::i
8+
\*\*\*\* file main.c line 7 function foo\n(\n|.)*\s+foo::1::1::i: \+uninitialized\+maybe_alive\n(\n|.)*\n\s+// \d+ file main.c line 7 function foo\n\s+DEAD foo::1::1::i
9+
\*\*\*\* file main.c line 11 function foo\n(\n|.)*\s+foo::i: \+unknown\n(\n|.)*\n\s+// \d+ file main.c line 11 function foo\n\s+DEAD foo::1::2::j
10+
\*\*\*\* file main.c line 11 function foo\n(\n|.)*\s+foo::1::2::j: \+uninitialized\+maybe_alive\n(\n|.)*\n\s+// \d+ file main.c line 11 function foo\n\s+DEAD foo::1::2::j
11+
\*\*\*\* file main.c line 11 function foo\n(\n|.)*\s+foo::1::x: \+uninitialized\+maybe_alive\n(\n|.)*\n\s+// \d+ file main.c line 11 function foo\n\s+DEAD foo::1::2::j
12+
\*\*\*\* file main.c line 11 function foo\n(\n|.)*\s+foo::1::1::i: \+unknown\n(\n|.)*\n\s+// \d+ file main.c line 11 function foo\n\s+DEAD foo::1::2::j
13+
^EXIT=0$
14+
^SIGNAL=0$
15+
--
16+
^warning: ignoring
17+
\*\*\*\* file main.c line 13 function foo\n.*maybe_alive(\n|.)*END_FUNCTION

src/analyses/local_bitvector_analysis.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ void local_bitvector_analysist::flagst::print(std::ostream &out) const
3535
out << "+static_lifetime";
3636
if(is_integer_address())
3737
out << "+integer_address";
38+
if(is_maybe_alive())
39+
out << "+maybe_alive";
3840
}
3941

4042
bool local_bitvector_analysist::merge(points_tot &a, points_tot &b)
@@ -229,6 +231,14 @@ local_bitvector_analysist::flagst local_bitvector_analysist::get_rec(
229231
else
230232
return flagst::mk_unknown();
231233
}
234+
else if(rhs.id() == ID_decl)
235+
{
236+
return flagst::mk_uninitialized() | flagst::mk_maybe_alive();
237+
}
238+
else if(rhs.id() == ID_dead)
239+
{
240+
return flagst::mk_uninitialized();
241+
}
232242
else
233243
return flagst::mk_unknown();
234244
}
@@ -274,18 +284,12 @@ void local_bitvector_analysist::build()
274284

275285
case DECL:
276286
assign_lhs(
277-
instruction.decl_symbol(),
278-
exprt(ID_uninitialized),
279-
loc_info_src,
280-
loc_info_dest);
287+
instruction.decl_symbol(), exprt(ID_decl), loc_info_src, loc_info_dest);
281288
break;
282289

283290
case DEAD:
284291
assign_lhs(
285-
instruction.dead_symbol(),
286-
exprt(ID_uninitialized),
287-
loc_info_src,
288-
loc_info_dest);
292+
instruction.dead_symbol(), exprt(ID_dead), loc_info_src, loc_info_dest);
289293
break;
290294

291295
case FUNCTION_CALL:

src/analyses/local_bitvector_analysis.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,15 @@ class local_bitvector_analysist
5959
// the bits for the "bitvector analysis"
6060
enum bitst
6161
{
62-
B_unknown=1<<0,
63-
B_uninitialized=1<<1,
64-
B_uses_offset=1<<2,
65-
B_dynamic_local=1<<3,
66-
B_dynamic_heap=1<<4,
67-
B_null=1<<5,
68-
B_static_lifetime=1<<6,
69-
B_integer_address=1<<7
62+
B_unknown = 1 << 0,
63+
B_uninitialized = 1 << 1,
64+
B_uses_offset = 1 << 2,
65+
B_dynamic_local = 1 << 3,
66+
B_dynamic_heap = 1 << 4,
67+
B_null = 1 << 5,
68+
B_static_lifetime = 1 << 6,
69+
B_integer_address = 1 << 7,
70+
B_maybe_alive = 1 << 8
7071
};
7172

7273
explicit flagst(const bitst _bits):bits(_bits)
@@ -162,6 +163,16 @@ class local_bitvector_analysist
162163
return (bits&B_integer_address)!=0;
163164
}
164165

166+
static flagst mk_maybe_alive()
167+
{
168+
return flagst(B_maybe_alive);
169+
}
170+
171+
bool is_maybe_alive() const
172+
{
173+
return (bits & B_maybe_alive) != 0;
174+
}
175+
165176
void print(std::ostream &) const;
166177

167178
flagst operator|(const flagst other) const

0 commit comments

Comments
 (0)