This repository contains Coccinelle semantic patch that transforms explicit lock/unlock patterns into a scope-based cleanup style using guard macros.
Before running the script, you need to have Coccinelle installed on your system.
sudo apt install coccinelle
sudo dnf install coccinelle
sudo pacman -S coccinelle
spatch --version
The top of file scoped_guard includes two file, guard_goto.cocci and cleanup.cocci. The files should be run in this precedence.
spatch --sp-file path_to/scoped_guard.cocci [path_to_c_file] --very-quiet -D lock=[lock pattern] -D unlock=[unlock pattern] -D lock_type=[lock type]
spatch --sp-file ./scoped_guard.cocci file.c --very-quiet -D lock=mutex_lock -D unlock=mutex_unlock -D lock_type=mutex
To simplify running of the script one could use the provide Makefile. Before running the make file please configure
- COCCI_SCRIPT with the path to your coccinelle script
 - SOURCE_DIR with path of your test c file
 
After making above configurations simply run:-
make [target]
make spinlock
For further understanding on the inner working of the script, please refer to the blog posts below: