Skip to content

Commit 4e1a23e

Browse files
committed
Exclude possibly shadowed variables from invariants (issue #1722)
1 parent e39b40c commit 4e1a23e

File tree

2 files changed

+14
-96
lines changed

2 files changed

+14
-96
lines changed

src/cdomain/value/domains/invariantCil.ml

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,23 @@ let exp_deep_unroll_types =
2626
let visitor = new exp_deep_unroll_types_visitor in
2727
visitCilExpr visitor
2828

29+
let var_may_be_shadowed scope vi =
30+
let vi_original_name = Cilfacade.find_original_name vi in
31+
let local_may_shadow local =
32+
not (CilType.Varinfo.equal vi local) && (* exclude self-equality by vid because the original names would always equal *)
33+
vi_original_name = Cilfacade.find_original_name local
34+
in
35+
List.exists local_may_shadow scope.sformals || List.exists local_may_shadow scope.slocals
2936

3037
let var_is_in_scope scope vi =
3138
match Cilfacade.find_scope_fundec vi with
32-
| None -> vi.vstorage <> Static (* CIL pulls static locals into globals, but they aren't syntactically in global scope *)
39+
| None ->
40+
vi.vstorage <> Static && (* CIL pulls static locals into globals, but they aren't syntactically in global scope *)
41+
not (var_may_be_shadowed scope vi)
3342
| Some fd ->
34-
if CilType.Fundec.equal fd scope then
35-
GobConfig.get_bool "witness.invariant.all-locals" || (not @@ hasAttribute "goblint_cil_nested" vi.vattr)
36-
else
37-
false
43+
CilType.Fundec.equal fd scope &&
44+
(GobConfig.get_bool "witness.invariant.all-locals" || (not @@ hasAttribute "goblint_cil_nested" vi.vattr)) &&
45+
not (var_may_be_shadowed scope vi) (* TODO: could distinguish non-nested and nested? *)
3846

3947
class exp_is_in_scope_visitor (scope: fundec) (acc: bool ref) = object
4048
inherit nopCilVisitor

tests/regression/56-witness/72-shadowing-invariant.t

Lines changed: 1 addition & 91 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
dead: 0
55
total lines: 5
66
[Info][Witness] witness generation summary:
7-
location invariants: 16
7+
location invariants: 6
88
loop invariants: 0
99
flow-insensitive invariants: 0
1010
total generation entries: 1
@@ -15,15 +15,6 @@ They can be wrong and contradicting.
1515
$ yamlWitnessStrip < witness.yml
1616
- entry_type: invariant_set
1717
content:
18-
- invariant:
19-
type: location_invariant
20-
location:
21-
file_name: 72-shadowing-invariant.c
22-
line: 6
23-
column: 3
24-
function: main
25-
value: foo == 1
26-
format: c_expression
2718
- invariant:
2819
type: location_invariant
2920
location:
@@ -33,15 +24,6 @@ They can be wrong and contradicting.
3324
function: main
3425
value: 2 == foo
3526
format: c_expression
36-
- invariant:
37-
type: location_invariant
38-
location:
39-
file_name: 72-shadowing-invariant.c
40-
line: 8
41-
column: 3
42-
function: main
43-
value: foo == 1
44-
format: c_expression
4527
- invariant:
4628
type: location_invariant
4729
location:
@@ -60,33 +42,6 @@ They can be wrong and contradicting.
6042
function: main
6143
value: 2 == foo
6244
format: c_expression
63-
- invariant:
64-
type: location_invariant
65-
location:
66-
file_name: 72-shadowing-invariant.c
67-
line: 10
68-
column: 5
69-
function: main
70-
value: 3 == bar
71-
format: c_expression
72-
- invariant:
73-
type: location_invariant
74-
location:
75-
file_name: 72-shadowing-invariant.c
76-
line: 10
77-
column: 5
78-
function: main
79-
value: bar == 3
80-
format: c_expression
81-
- invariant:
82-
type: location_invariant
83-
location:
84-
file_name: 72-shadowing-invariant.c
85-
line: 10
86-
column: 5
87-
function: main
88-
value: foo == 1
89-
format: c_expression
9045
- invariant:
9146
type: location_invariant
9247
location:
@@ -105,51 +60,6 @@ They can be wrong and contradicting.
10560
function: main
10661
value: 2 == foo
10762
format: c_expression
108-
- invariant:
109-
type: location_invariant
110-
location:
111-
file_name: 72-shadowing-invariant.c
112-
line: 11
113-
column: 5
114-
function: main
115-
value: 3 == bar
116-
format: c_expression
117-
- invariant:
118-
type: location_invariant
119-
location:
120-
file_name: 72-shadowing-invariant.c
121-
line: 11
122-
column: 5
123-
function: main
124-
value: 4 == bar
125-
format: c_expression
126-
- invariant:
127-
type: location_invariant
128-
location:
129-
file_name: 72-shadowing-invariant.c
130-
line: 11
131-
column: 5
132-
function: main
133-
value: bar == 3
134-
format: c_expression
135-
- invariant:
136-
type: location_invariant
137-
location:
138-
file_name: 72-shadowing-invariant.c
139-
line: 11
140-
column: 5
141-
function: main
142-
value: bar == 4
143-
format: c_expression
144-
- invariant:
145-
type: location_invariant
146-
location:
147-
file_name: 72-shadowing-invariant.c
148-
line: 11
149-
column: 5
150-
function: main
151-
value: foo == 1
152-
format: c_expression
15363
- invariant:
15464
type: location_invariant
15565
location:

0 commit comments

Comments
 (0)