1 // SPDX-License-Identifier: GPL-2.0-only
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.
8 // Copyright: (C) 2021 Denis Efremov ISPRAS
9 // Options: --no-includes --include-headers
20 @rmax depends on !patch@
23 binary operator cmp = {>, >=};
30 * ((x) cmp@p (y) ? (x) : (y))
34 @rmaxif depends on !patch@
38 binary operator cmp = {>, >=};
45 * if ((x) cmp@p (y)) {
53 @rmin depends on !patch@
56 binary operator cmp = {<, <=};
63 * ((x) cmp@p (y) ? (x) : (y))
67 @rminif depends on !patch@
71 binary operator cmp = {<, <=};
78 * if ((x) cmp@p (y)) {
86 @pmax depends on patch@
89 binary operator cmp = {>=, >};
95 - ((x) cmp (y) ? (x) : (y))
100 @pmaxif depends on patch@
104 binary operator cmp = {>=, >};
115 + max_val = max(x, y);
119 // Don't generate patches for errcode returns.
120 @errcode depends on patch@
124 binary operator cmp = {<, <=};
130 return ((x) cmp@p 0 ? (x) : 0);
134 @pmin depends on patch@
137 binary operator cmp = {<=, <};
138 position p != errcode.p;
144 - ((x) cmp@p (y) ? (x) : (y))
149 @pminif depends on patch@
153 binary operator cmp = {<=, <};
164 + min_val = min(x, y);
168 @script:python depends on report@
173 coccilib.report.print_report(p0, "WARNING opportunity for max()")
175 @script:python depends on org@
180 coccilib.org.print_todo(p0, "WARNING opportunity for max()")
182 @script:python depends on report@
187 coccilib.report.print_report(p0, "WARNING opportunity for max()")
189 @script:python depends on org@
194 coccilib.org.print_todo(p0, "WARNING opportunity for max()")
196 @script:python depends on report@
201 coccilib.report.print_report(p0, "WARNING opportunity for min()")
203 @script:python depends on org@
208 coccilib.org.print_todo(p0, "WARNING opportunity for min()")
210 @script:python depends on report@
215 coccilib.report.print_report(p0, "WARNING opportunity for min()")
217 @script:python depends on org@
222 coccilib.org.print_todo(p0, "WARNING opportunity for min()")