0001 // SPDX-License-Identifier: GPL-2.0-only
0002 ///
0003 /// Check for opencoded swap() implementation.
0004 ///
0005 // Confidence: High
0006 // Copyright: (C) 2021 Denis Efremov ISPRAS
0007 // Options: --no-includes --include-headers
0008 //
0009 // Keywords: swap
0010 //
0011
0012 virtual patch
0013 virtual org
0014 virtual report
0015 virtual context
0016
0017 @rvar depends on !patch@
0018 identifier tmp;
0019 expression a, b;
0020 type T;
0021 position p;
0022 @@
0023
0024 (
0025 * T tmp;
0026 |
0027 * T tmp = 0;
0028 |
0029 * T *tmp = NULL;
0030 )
0031 ... when != tmp
0032 * tmp = a;
0033 * a = b;@p
0034 * b = tmp;
0035 ... when != tmp
0036
0037 @r depends on !patch@
0038 identifier tmp;
0039 expression a, b;
0040 position p != rvar.p;
0041 @@
0042
0043 * tmp = a;
0044 * a = b;@p
0045 * b = tmp;
0046
0047 @rpvar depends on patch@
0048 identifier tmp;
0049 expression a, b;
0050 type T;
0051 @@
0052
0053 (
0054 - T tmp;
0055 |
0056 - T tmp = 0;
0057 |
0058 - T *tmp = NULL;
0059 )
0060 ... when != tmp
0061 - tmp = a;
0062 - a = b;
0063 - b = tmp
0064 + swap(a, b)
0065 ;
0066 ... when != tmp
0067
0068 @rp depends on patch@
0069 identifier tmp;
0070 expression a, b;
0071 @@
0072
0073 - tmp = a;
0074 - a = b;
0075 - b = tmp
0076 + swap(a, b)
0077 ;
0078
0079 @depends on patch && (rpvar || rp)@
0080 @@
0081
0082 (
0083 for (...;...;...)
0084 - {
0085 swap(...);
0086 - }
0087 |
0088 while (...)
0089 - {
0090 swap(...);
0091 - }
0092 |
0093 if (...)
0094 - {
0095 swap(...);
0096 - }
0097 )
0098
0099
0100 @script:python depends on report@
0101 p << r.p;
0102 @@
0103
0104 coccilib.report.print_report(p[0], "WARNING opportunity for swap()")
0105
0106 @script:python depends on org@
0107 p << r.p;
0108 @@
0109
0110 coccilib.org.print_todo(p[0], "WARNING opportunity for swap()")
0111
0112 @script:python depends on report@
0113 p << rvar.p;
0114 @@
0115
0116 coccilib.report.print_report(p[0], "WARNING opportunity for swap()")
0117
0118 @script:python depends on org@
0119 p << rvar.p;
0120 @@
0121
0122 coccilib.org.print_todo(p[0], "WARNING opportunity for swap()")