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