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