1 // SPDX-License-Identifier: GPL-2.0-only 1 // SPDX-License-Identifier: GPL-2.0-only 2 /// 2 /// 3 /// Check for opencoded min(), max() implement 3 /// Check for opencoded min(), max() implementations. 4 /// Generated patches sometimes require adding 4 /// Generated patches sometimes require adding a cast to fix compile warning. 5 /// Warnings/patches scope intentionally limit 5 /// Warnings/patches scope intentionally limited to a function body. 6 /// 6 /// 7 // Confidence: Medium 7 // Confidence: Medium 8 // Copyright: (C) 2021 Denis Efremov ISPRAS 8 // Copyright: (C) 2021 Denis Efremov ISPRAS 9 // Options: --no-includes --include-headers 9 // Options: --no-includes --include-headers 10 // 10 // 11 // Keywords: min, max 11 // Keywords: min, max 12 // 12 // 13 13 14 14 15 virtual report 15 virtual report 16 virtual org 16 virtual org 17 virtual context 17 virtual context 18 virtual patch 18 virtual patch 19 19 20 @rmax depends on !patch@ 20 @rmax depends on !patch@ 21 identifier func; 21 identifier func; 22 expression x, y; 22 expression x, y; 23 binary operator cmp = {>, >=}; 23 binary operator cmp = {>, >=}; 24 position p; 24 position p; 25 @@ 25 @@ 26 26 27 func(...) 27 func(...) 28 { 28 { 29 <... 29 <... 30 * ((x) cmp@p (y) ? (x) : (y)) 30 * ((x) cmp@p (y) ? (x) : (y)) 31 ...> 31 ...> 32 } 32 } 33 33 34 @rmaxif depends on !patch@ 34 @rmaxif depends on !patch@ 35 identifier func; 35 identifier func; 36 expression x, y; 36 expression x, y; 37 expression max_val; 37 expression max_val; 38 binary operator cmp = {>, >=}; 38 binary operator cmp = {>, >=}; 39 position p; 39 position p; 40 @@ 40 @@ 41 41 42 func(...) 42 func(...) 43 { 43 { 44 <... 44 <... 45 * if ((x) cmp@p (y)) { 45 * if ((x) cmp@p (y)) { 46 * max_val = (x); 46 * max_val = (x); 47 * } else { 47 * } else { 48 * max_val = (y); 48 * max_val = (y); 49 * } 49 * } 50 ...> 50 ...> 51 } 51 } 52 52 53 // Ignore errcode returns. << 54 @errcode@ << 55 position p; << 56 identifier func; << 57 expression x; << 58 binary operator cmp = {<, <=}; << 59 @@ << 60 << 61 func(...) << 62 { << 63 <... << 64 return ((x) cmp@p 0 ? (x) : 0); << 65 ...> << 66 } << 67 << 68 @rmin depends on !patch@ 53 @rmin depends on !patch@ 69 identifier func; 54 identifier func; 70 expression x, y; 55 expression x, y; 71 binary operator cmp = {<, <=}; 56 binary operator cmp = {<, <=}; 72 position p != errcode.p; !! 57 position p; 73 @@ 58 @@ 74 59 75 func(...) 60 func(...) 76 { 61 { 77 <... 62 <... 78 * ((x) cmp@p (y) ? (x) : (y)) 63 * ((x) cmp@p (y) ? (x) : (y)) 79 ...> 64 ...> 80 } 65 } 81 66 82 @rminif depends on !patch@ 67 @rminif depends on !patch@ 83 identifier func; 68 identifier func; 84 expression x, y; 69 expression x, y; 85 expression min_val; 70 expression min_val; 86 binary operator cmp = {<, <=}; 71 binary operator cmp = {<, <=}; 87 position p; 72 position p; 88 @@ 73 @@ 89 74 90 func(...) 75 func(...) 91 { 76 { 92 <... 77 <... 93 * if ((x) cmp@p (y)) { 78 * if ((x) cmp@p (y)) { 94 * min_val = (x); 79 * min_val = (x); 95 * } else { 80 * } else { 96 * min_val = (y); 81 * min_val = (y); 97 * } 82 * } 98 ...> 83 ...> 99 } 84 } 100 85 101 @pmax depends on patch@ 86 @pmax depends on patch@ 102 identifier func; 87 identifier func; 103 expression x, y; 88 expression x, y; 104 binary operator cmp = {>=, >}; 89 binary operator cmp = {>=, >}; 105 @@ 90 @@ 106 91 107 func(...) 92 func(...) 108 { 93 { 109 <... 94 <... 110 - ((x) cmp (y) ? (x) : (y)) 95 - ((x) cmp (y) ? (x) : (y)) 111 + max(x, y) 96 + max(x, y) 112 ...> 97 ...> 113 } 98 } 114 99 115 @pmaxif depends on patch@ 100 @pmaxif depends on patch@ 116 identifier func; 101 identifier func; 117 expression x, y; 102 expression x, y; 118 expression max_val; 103 expression max_val; 119 binary operator cmp = {>=, >}; 104 binary operator cmp = {>=, >}; 120 @@ 105 @@ 121 106 122 func(...) 107 func(...) 123 { 108 { 124 <... 109 <... 125 - if ((x) cmp (y)) { 110 - if ((x) cmp (y)) { 126 - max_val = x; 111 - max_val = x; 127 - } else { 112 - } else { 128 - max_val = y; 113 - max_val = y; 129 - } 114 - } 130 + max_val = max(x, y); 115 + max_val = max(x, y); >> 116 ...> >> 117 } >> 118 >> 119 // Don't generate patches for errcode returns. >> 120 @errcode depends on patch@ >> 121 position p; >> 122 identifier func; >> 123 expression x; >> 124 binary operator cmp = {<, <=}; >> 125 @@ >> 126 >> 127 func(...) >> 128 { >> 129 <... >> 130 return ((x) cmp@p 0 ? (x) : 0); 131 ...> 131 ...> 132 } 132 } 133 133 134 @pmin depends on patch@ 134 @pmin depends on patch@ 135 identifier func; 135 identifier func; 136 expression x, y; 136 expression x, y; 137 binary operator cmp = {<=, <}; 137 binary operator cmp = {<=, <}; 138 position p != errcode.p; 138 position p != errcode.p; 139 @@ 139 @@ 140 140 141 func(...) 141 func(...) 142 { 142 { 143 <... 143 <... 144 - ((x) cmp@p (y) ? (x) : (y)) 144 - ((x) cmp@p (y) ? (x) : (y)) 145 + min(x, y) 145 + min(x, y) 146 ...> 146 ...> 147 } 147 } 148 148 149 @pminif depends on patch@ 149 @pminif depends on patch@ 150 identifier func; 150 identifier func; 151 expression x, y; 151 expression x, y; 152 expression min_val; 152 expression min_val; 153 binary operator cmp = {<=, <}; 153 binary operator cmp = {<=, <}; 154 @@ 154 @@ 155 155 156 func(...) 156 func(...) 157 { 157 { 158 <... 158 <... 159 - if ((x) cmp (y)) { 159 - if ((x) cmp (y)) { 160 - min_val = x; 160 - min_val = x; 161 - } else { 161 - } else { 162 - min_val = y; 162 - min_val = y; 163 - } 163 - } 164 + min_val = min(x, y); 164 + min_val = min(x, y); 165 ...> 165 ...> 166 } 166 } 167 167 168 @script:python depends on report@ 168 @script:python depends on report@ 169 p << rmax.p; 169 p << rmax.p; 170 @@ 170 @@ 171 171 172 for p0 in p: 172 for p0 in p: 173 coccilib.report.print_report(p0, "WARN 173 coccilib.report.print_report(p0, "WARNING opportunity for max()") 174 174 175 @script:python depends on org@ 175 @script:python depends on org@ 176 p << rmax.p; 176 p << rmax.p; 177 @@ 177 @@ 178 178 179 for p0 in p: 179 for p0 in p: 180 coccilib.org.print_todo(p0, "WARNING o 180 coccilib.org.print_todo(p0, "WARNING opportunity for max()") 181 181 182 @script:python depends on report@ 182 @script:python depends on report@ 183 p << rmaxif.p; 183 p << rmaxif.p; 184 @@ 184 @@ 185 185 186 for p0 in p: 186 for p0 in p: 187 coccilib.report.print_report(p0, "WARN 187 coccilib.report.print_report(p0, "WARNING opportunity for max()") 188 188 189 @script:python depends on org@ 189 @script:python depends on org@ 190 p << rmaxif.p; 190 p << rmaxif.p; 191 @@ 191 @@ 192 192 193 for p0 in p: 193 for p0 in p: 194 coccilib.org.print_todo(p0, "WARNING o 194 coccilib.org.print_todo(p0, "WARNING opportunity for max()") 195 195 196 @script:python depends on report@ 196 @script:python depends on report@ 197 p << rmin.p; 197 p << rmin.p; 198 @@ 198 @@ 199 199 200 for p0 in p: 200 for p0 in p: 201 coccilib.report.print_report(p0, "WARN 201 coccilib.report.print_report(p0, "WARNING opportunity for min()") 202 202 203 @script:python depends on org@ 203 @script:python depends on org@ 204 p << rmin.p; 204 p << rmin.p; 205 @@ 205 @@ 206 206 207 for p0 in p: 207 for p0 in p: 208 coccilib.org.print_todo(p0, "WARNING o 208 coccilib.org.print_todo(p0, "WARNING opportunity for min()") 209 209 210 @script:python depends on report@ 210 @script:python depends on report@ 211 p << rminif.p; 211 p << rminif.p; 212 @@ 212 @@ 213 213 214 for p0 in p: 214 for p0 in p: 215 coccilib.report.print_report(p0, "WARN 215 coccilib.report.print_report(p0, "WARNING opportunity for min()") 216 216 217 @script:python depends on org@ 217 @script:python depends on org@ 218 p << rminif.p; 218 p << rminif.p; 219 @@ 219 @@ 220 220 221 for p0 in p: 221 for p0 in p: 222 coccilib.org.print_todo(p0, "WARNING o 222 coccilib.org.print_todo(p0, "WARNING opportunity for min()")
Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.