xref: /aosp_15_r20/external/ltp/scripts/coccinelle/libltp-test-macro.cocci (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker// SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker// Copyright (c) 2021 SUSE LLC  <[email protected]>
3*49cdfc7eSAndroid Build Coastguard Worker
4*49cdfc7eSAndroid Build Coastguard Worker// Find and fix violations of rule LTP-002
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker// Set with -D fix
7*49cdfc7eSAndroid Build Coastguard Workervirtual fix
8*49cdfc7eSAndroid Build Coastguard Worker
9*49cdfc7eSAndroid Build Coastguard Worker// Find all positions where TEST is _used_.
10*49cdfc7eSAndroid Build Coastguard Worker@ depends on !fix exists @
11*49cdfc7eSAndroid Build Coastguard Worker@@
12*49cdfc7eSAndroid Build Coastguard Worker
13*49cdfc7eSAndroid Build Coastguard Worker* TEST(...);
14*49cdfc7eSAndroid Build Coastguard Worker
15*49cdfc7eSAndroid Build Coastguard Worker// Below are rules which will create a patch to replace TEST usage
16*49cdfc7eSAndroid Build Coastguard Worker// It assumes we can use the ret var without conflicts
17*49cdfc7eSAndroid Build Coastguard Worker
18*49cdfc7eSAndroid Build Coastguard Worker// Fix all references to the variables TEST modifies when they occur in a
19*49cdfc7eSAndroid Build Coastguard Worker// function where TEST was used.
20*49cdfc7eSAndroid Build Coastguard Worker@ depends on fix exists @
21*49cdfc7eSAndroid Build Coastguard Worker@@
22*49cdfc7eSAndroid Build Coastguard Worker
23*49cdfc7eSAndroid Build Coastguard Worker TEST(...)
24*49cdfc7eSAndroid Build Coastguard Worker
25*49cdfc7eSAndroid Build Coastguard Worker <...
26*49cdfc7eSAndroid Build Coastguard Worker
27*49cdfc7eSAndroid Build Coastguard Worker(
28*49cdfc7eSAndroid Build Coastguard Worker- TST_RET
29*49cdfc7eSAndroid Build Coastguard Worker+ ret
30*49cdfc7eSAndroid Build Coastguard Worker|
31*49cdfc7eSAndroid Build Coastguard Worker- TST_ERR
32*49cdfc7eSAndroid Build Coastguard Worker+ errno
33*49cdfc7eSAndroid Build Coastguard Worker|
34*49cdfc7eSAndroid Build Coastguard Worker- TTERRNO
35*49cdfc7eSAndroid Build Coastguard Worker+ TERRNO
36*49cdfc7eSAndroid Build Coastguard Worker)
37*49cdfc7eSAndroid Build Coastguard Worker
38*49cdfc7eSAndroid Build Coastguard Worker ...>
39*49cdfc7eSAndroid Build Coastguard Worker
40*49cdfc7eSAndroid Build Coastguard Worker// Replace TEST in all functions where it occurs only at the start. It
41*49cdfc7eSAndroid Build Coastguard Worker// is slightly complicated by adding a newline if a statement appears
42*49cdfc7eSAndroid Build Coastguard Worker// on the line after TEST(). It is not clear to me what the rules are
43*49cdfc7eSAndroid Build Coastguard Worker// for matching whitespace as it has no semantic meaning, but this
44*49cdfc7eSAndroid Build Coastguard Worker// appears to work.
45*49cdfc7eSAndroid Build Coastguard Worker@ depends on fix @
46*49cdfc7eSAndroid Build Coastguard Workeridentifier fn;
47*49cdfc7eSAndroid Build Coastguard Workerexpression tested_expr;
48*49cdfc7eSAndroid Build Coastguard Workerstatement st;
49*49cdfc7eSAndroid Build Coastguard Worker@@
50*49cdfc7eSAndroid Build Coastguard Worker
51*49cdfc7eSAndroid Build Coastguard Worker  fn (...)
52*49cdfc7eSAndroid Build Coastguard Worker  {
53*49cdfc7eSAndroid Build Coastguard Worker- 	TEST(tested_expr);
54*49cdfc7eSAndroid Build Coastguard Worker+	const long ret = tested_expr;
55*49cdfc7eSAndroid Build Coastguard Worker(
56*49cdfc7eSAndroid Build Coastguard Worker+
57*49cdfc7eSAndroid Build Coastguard Worker	st
58*49cdfc7eSAndroid Build Coastguard Worker|
59*49cdfc7eSAndroid Build Coastguard Worker
60*49cdfc7eSAndroid Build Coastguard Worker)
61*49cdfc7eSAndroid Build Coastguard Worker	... when != TEST(...)
62*49cdfc7eSAndroid Build Coastguard Worker  }
63*49cdfc7eSAndroid Build Coastguard Worker
64*49cdfc7eSAndroid Build Coastguard Worker// Replace TEST in all functions where it occurs at the start
65*49cdfc7eSAndroid Build Coastguard Worker// Functions where it *only* occurs at the start were handled above
66*49cdfc7eSAndroid Build Coastguard Worker@ depends on fix @
67*49cdfc7eSAndroid Build Coastguard Workeridentifier fn;
68*49cdfc7eSAndroid Build Coastguard Workerexpression tested_expr;
69*49cdfc7eSAndroid Build Coastguard Workerstatement st;
70*49cdfc7eSAndroid Build Coastguard Worker@@
71*49cdfc7eSAndroid Build Coastguard Worker
72*49cdfc7eSAndroid Build Coastguard Worker  fn (...)
73*49cdfc7eSAndroid Build Coastguard Worker  {
74*49cdfc7eSAndroid Build Coastguard Worker- 	TEST(tested_expr);
75*49cdfc7eSAndroid Build Coastguard Worker+	long ret = tested_expr;
76*49cdfc7eSAndroid Build Coastguard Worker(
77*49cdfc7eSAndroid Build Coastguard Worker+
78*49cdfc7eSAndroid Build Coastguard Worker	st
79*49cdfc7eSAndroid Build Coastguard Worker|
80*49cdfc7eSAndroid Build Coastguard Worker
81*49cdfc7eSAndroid Build Coastguard Worker)
82*49cdfc7eSAndroid Build Coastguard Worker	...
83*49cdfc7eSAndroid Build Coastguard Worker  }
84*49cdfc7eSAndroid Build Coastguard Worker
85*49cdfc7eSAndroid Build Coastguard Worker// Add ret var at the start of a function where TEST occurs and there
86*49cdfc7eSAndroid Build Coastguard Worker// is not already a ret declaration
87*49cdfc7eSAndroid Build Coastguard Worker@ depends on fix exists @
88*49cdfc7eSAndroid Build Coastguard Workeridentifier fn;
89*49cdfc7eSAndroid Build Coastguard Worker@@
90*49cdfc7eSAndroid Build Coastguard Worker
91*49cdfc7eSAndroid Build Coastguard Worker  fn (...)
92*49cdfc7eSAndroid Build Coastguard Worker  {
93*49cdfc7eSAndroid Build Coastguard Worker+	long ret;
94*49cdfc7eSAndroid Build Coastguard Worker	... when != long ret;
95*49cdfc7eSAndroid Build Coastguard Worker
96*49cdfc7eSAndroid Build Coastguard Worker	TEST(...)
97*49cdfc7eSAndroid Build Coastguard Worker	...
98*49cdfc7eSAndroid Build Coastguard Worker  }
99*49cdfc7eSAndroid Build Coastguard Worker
100*49cdfc7eSAndroid Build Coastguard Worker// Replace any remaining occurrences of TEST
101*49cdfc7eSAndroid Build Coastguard Worker@ depends on fix @
102*49cdfc7eSAndroid Build Coastguard Workerexpression tested_expr;
103*49cdfc7eSAndroid Build Coastguard Worker@@
104*49cdfc7eSAndroid Build Coastguard Worker
105*49cdfc7eSAndroid Build Coastguard Worker- 	TEST(tested_expr);
106*49cdfc7eSAndroid Build Coastguard Worker+	ret = tested_expr;
107*49cdfc7eSAndroid Build Coastguard Worker
108