xref: /aosp_15_r20/external/ltp/scripts/coccinelle/run-spatch.sh (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1#!/bin/sh -eu
2# SPDX-License-Identifier: GPL-2.0-or-later
3# Copyright (c) 2021 SUSE LLC  <[email protected]>
4
5# Helper for running spatch Coccinelle scripts on the LTP source tree
6
7if [ ! -d lib ] || [ ! -d scripts/coccinelle ]; then
8    echo "$0: Can't find lib or scripts directories. Run me from top src dir"
9    exit 1
10fi
11
12do_fix=no
13
14# Run a script on the lib dir
15libltp_spatch() {
16    echo libltp_spatch $*
17
18    if [ $do_fix = yes ]; then
19	spatch --dir lib \
20	       --ignore lib/parse_opts.c \
21	       --ignore lib/newlib_tests \
22	       --ignore lib/tests \
23	       --use-gitgrep \
24	       --in-place \
25	       -D fix \
26	       --include-headers \
27	       $*
28	spatch --dir include \
29	       --use-gitgrep \
30	       --in-place \
31	       -D fix \
32	       --include-headers \
33	       $*
34    else
35	spatch --dir lib \
36	       --ignore lib/parse_opts.c \
37	       --ignore lib/newlib_tests \
38	       --ignore lib/tests \
39	       --use-gitgrep \
40	       --include-headers \
41	       $*
42	spatch --dir include \
43	       --use-gitgrep \
44	       --include-headers \
45	       $*
46    fi
47}
48
49tests_spatch() {
50        echo tests_spatch $*
51
52        if [ $do_fix = yes ]; then
53	    spatch --dir testcases \
54		   --dir lib/newlib_tests \
55		   --use-gitgrep \
56		   --in-place \
57		   -D fix \
58		   --include-headers \
59		   $*
60	else
61	    spatch --dir testcases \
62		   --dir lib/newlib_tests \
63		   --use-gitgrep \
64		   --include-headers \
65		   $*
66	fi
67}
68
69usage()
70{
71    cat <<EOF
72Usage:
73$0 [ -f ] <patch basename> [ <patch basename> [...] ]
74$0 -h
75
76Options:
77-f	Apply the semantic patch in-place to fix the code
78-h	You are reading it
79
80If run without -f then the semantic patch will only print locations
81where it matches or show a diff.
82
83EOF
84}
85
86while getopts "fh" opt; do
87    case $opt in
88	f) do_fix=yes;;
89	h|?) usage; exit $([ $opt = h ]);;
90    esac
91done
92
93shift $(($OPTIND - 1))
94
95if [ $# -eq 0 ]; then
96    echo -e "Missing semantic patch name \n"
97    usage; exit 1
98fi
99
100if [ $do_fix = yes ] && [ -n "$(git ls-files -m -d)" ]; then
101    echo "At least stage your current changes!"
102    exit 1
103fi
104
105for spatch_file in $*; do
106    case $spatch_file in
107	libltp-test-macro)
108	    libltp_spatch --sp-file scripts/coccinelle/libltp-test-macro.cocci;;
109	libltp-test-macro-vars)
110	    libltp_spatch --sp-file scripts/coccinelle/libltp-test-macro-vars.cocci \
111			  --ignore lib/tst_test.c;;
112	*)
113	    tests_spatch --sp-file scripts/coccinelle/$spatch_file.cocci;;
114    esac
115done
116
117
118
119