Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-only
0002 /// Compare pointer-typed values to NULL rather than 0
0003 ///
0004 //# This makes an effort to choose between !x and x == NULL.  !x is used
0005 //# if it has previously been used with the function used to initialize x.
0006 //# This relies on type information.  More type information can be obtained
0007 //# using the option -all_includes and the option -I to specify an
0008 //# include path.
0009 //
0010 // Confidence: High
0011 // Copyright: (C) 2012 Julia Lawall, INRIA/LIP6.
0012 // Copyright: (C) 2012 Gilles Muller, INRIA/LiP6.
0013 // URL: https://coccinelle.gitlabpages.inria.fr/website
0014 // Requires: 1.0.0
0015 // Options:
0016 
0017 virtual patch
0018 virtual context
0019 virtual org
0020 virtual report
0021 
0022 @initialize:ocaml@
0023 @@
0024 let negtable = Hashtbl.create 101
0025 
0026 @depends on patch@
0027 expression *E;
0028 identifier f;
0029 @@
0030 
0031 (
0032   (E = f(...)) ==
0033 - 0
0034 + NULL
0035 |
0036   (E = f(...)) !=
0037 - 0
0038 + NULL
0039 |
0040 - 0
0041 + NULL
0042   == (E = f(...))
0043 |
0044 - 0
0045 + NULL
0046   != (E = f(...))
0047 )
0048 
0049 
0050 @t1 depends on !patch@
0051 expression *E;
0052 identifier f;
0053 position p;
0054 @@
0055 
0056 (
0057   (E = f(...)) ==
0058 * 0@p
0059 |
0060   (E = f(...)) !=
0061 * 0@p
0062 |
0063 * 0@p
0064   == (E = f(...))
0065 |
0066 * 0@p
0067   != (E = f(...))
0068 )
0069 
0070 @script:python depends on org@
0071 p << t1.p;
0072 @@
0073 
0074 coccilib.org.print_todo(p[0], "WARNING comparing pointer to 0")
0075 
0076 @script:python depends on report@
0077 p << t1.p;
0078 @@
0079 
0080 coccilib.report.print_report(p[0], "WARNING comparing pointer to 0")
0081 
0082 // Tests of returned values
0083 
0084 @s@
0085 identifier f;
0086 expression E,E1;
0087 @@
0088 
0089  E = f(...)
0090  ... when != E = E1
0091  !E
0092 
0093 @script:ocaml depends on s@
0094 f << s.f;
0095 @@
0096 
0097 try let _ = Hashtbl.find negtable f in ()
0098 with Not_found -> Hashtbl.add negtable f ()
0099 
0100 @ r disable is_zero,isnt_zero exists @
0101 expression *E;
0102 identifier f;
0103 @@
0104 
0105 E = f(...)
0106 ...
0107 (E == 0
0108 |E != 0
0109 |0 == E
0110 |0 != E
0111 )
0112 
0113 @script:ocaml@
0114 f << r.f;
0115 @@
0116 
0117 try let _ = Hashtbl.find negtable f in ()
0118 with Not_found -> include_match false
0119 
0120 // This rule may lead to inconsistent path problems, if E is defined in two
0121 // places
0122 @ depends on patch disable is_zero,isnt_zero @
0123 expression *E;
0124 expression E1;
0125 identifier r.f;
0126 @@
0127 
0128 E = f(...)
0129 <...
0130 (
0131 - E == 0
0132 + !E
0133 |
0134 - E != 0
0135 + E
0136 |
0137 - 0 == E
0138 + !E
0139 |
0140 - 0 != E
0141 + E
0142 )
0143 ...>
0144 ?E = E1
0145 
0146 @t2 depends on !patch disable is_zero,isnt_zero @
0147 expression *E;
0148 expression E1;
0149 identifier r.f;
0150 position p1;
0151 position p2;
0152 @@
0153 
0154 E = f(...)
0155 <...
0156 (
0157 * E == 0@p1
0158 |
0159 * E != 0@p2
0160 |
0161 * 0@p1 == E
0162 |
0163 * 0@p1 != E
0164 )
0165 ...>
0166 ?E = E1
0167 
0168 @script:python depends on org@
0169 p << t2.p1;
0170 @@
0171 
0172 coccilib.org.print_todo(p[0], "WARNING comparing pointer to 0, suggest !E")
0173 
0174 @script:python depends on org@
0175 p << t2.p2;
0176 @@
0177 
0178 coccilib.org.print_todo(p[0], "WARNING comparing pointer to 0")
0179 
0180 @script:python depends on report@
0181 p << t2.p1;
0182 @@
0183 
0184 coccilib.report.print_report(p[0], "WARNING comparing pointer to 0, suggest !E")
0185 
0186 @script:python depends on report@
0187 p << t2.p2;
0188 @@
0189 
0190 coccilib.report.print_report(p[0], "WARNING comparing pointer to 0")
0191 
0192 @ depends on patch disable is_zero,isnt_zero @
0193 expression *E;
0194 @@
0195 
0196 (
0197   E ==
0198 - 0
0199 + NULL
0200 |
0201   E !=
0202 - 0
0203 + NULL
0204 |
0205 - 0
0206 + NULL
0207   == E
0208 |
0209 - 0
0210 + NULL
0211   != E
0212 )
0213 
0214 @ t3 depends on !patch disable is_zero,isnt_zero @
0215 expression *E;
0216 position p;
0217 @@
0218 
0219 (
0220 * E == 0@p
0221 |
0222 * E != 0@p
0223 |
0224 * 0@p == E
0225 |
0226 * 0@p != E
0227 )
0228 
0229 @script:python depends on org@
0230 p << t3.p;
0231 @@
0232 
0233 coccilib.org.print_todo(p[0], "WARNING comparing pointer to 0")
0234 
0235 @script:python depends on report@
0236 p << t3.p;
0237 @@
0238 
0239 coccilib.report.print_report(p[0], "WARNING comparing pointer to 0")