Commit | Line | Data |
---|---|---|
951c7801 C |
1 | // This detects NULL tests that can only be reached when the value is known |
2 | // not to be NULL | |
3 | // | |
4 | // Confidence: High | |
5 | // Copyright: (C) Gilles Muller, Julia Lawall, EMN, DIKU. GPLv2. | |
6 | // URL: http://www.emn.fr/x-info/coccinelle/rules/notnull.html | |
7 | // Options: | |
8 | ||
9 | virtual org,diff | |
10 | ||
11 | @r exists@ | |
12 | local idexpression x; | |
13 | expression E; | |
14 | position p1,p2; | |
15 | @@ | |
16 | ||
17 | if (x@p1 == NULL || ...) { ... when forall | |
18 | return ...; } | |
19 | ... when != \(x=E\|x--\|x++\|--x\|++x\|x-=E\|x+=E\|x|=E\|x&=E\) | |
20 | when != &x | |
21 | ( | |
22 | x@p2 == NULL | |
23 | | | |
24 | x@p2 != NULL | |
25 | ) | |
26 | ||
27 | // another path to the test that is not through p1? | |
28 | ||
29 | @s exists@ | |
30 | local idexpression r.x; | |
31 | position r.p1,r.p2; | |
32 | @@ | |
33 | ||
34 | ... when != x@p1 | |
35 | ( | |
36 | x@p2 == NULL | |
37 | | | |
38 | x@p2 != NULL | |
39 | ) | |
40 | ||
41 | // another path to the test from p1? | |
42 | ||
43 | @t exists@ | |
44 | local idexpression x; | |
45 | position r.p1,r.p2; | |
46 | @@ | |
47 | ||
48 | if (x@p1 == NULL || ...) { ... x@p2 ... when any | |
49 | return ...; } | |
50 | ||
51 | // another path to the test containing an assignment? | |
52 | ||
53 | @u exists@ | |
54 | local idexpression x; | |
55 | expression E; | |
56 | position r.p1,r.p2; | |
57 | @@ | |
58 | ||
59 | if (x@p1 == NULL || ...) { ... when forall | |
60 | return ...; } | |
61 | ... | |
62 | \(x=E\|x--\|x++\|--x\|++x\|x-=E\|x+=E\|x|=E\|x&=E\|&x\) | |
63 | ... when != x@p1 | |
64 | when any | |
65 | ( | |
66 | x@p2 == NULL | |
67 | | | |
68 | x@p2 != NULL | |
69 | ) | |
70 | ||
71 | @script:python depends on !s && !t && !u && org @ | |
72 | p1 << r.p1; | |
73 | p2 << r.p2; | |
74 | @@ | |
75 | ||
76 | cocci.print_main("",p2) | |
77 | cocci.print_secs("",p1) | |
78 | ||
79 | @depends on !s && !t && !u && diff@ | |
80 | position r.p1, r.p2; | |
81 | expression x; | |
82 | @@ | |
83 | ||
84 | *x@p1 | |
85 | ... | |
86 | *x@p2 |