4. Coverity
4.1 Context
4.2 References
-5. Miscellaneous Tools
+5. Coccinelle
+6. Miscellaneous Tools
===================
= 1. Introduction =
Early Coverity: https://wiki.lustre.org/images/8/8a/LUG2013-Lustre_Static_Code_Analysis-Bull.pdf
Coverity Scan Page: https://scan.coverity.com/projects/lustre
+=================
+= 5. Coccinelle =
+=================
+
+5.1 Context
+===========
+
+Coccinelle is a automated refactoring tool for C code. Coccinelle uses it's native
+understanding of the C language to automatically apply semantic patches (i.e Coccinelle
+scripts) to C files. This can be used to remove cruft, fix common coding mistakes,
+update APIs, and more. Coccinelle can readily obtained as a package in common Linux
+distributions.
+
+The primary tools for interacting with Coccinelle is `spatch`. A simple invocation could
+be:
+
+ spatch --sp-file test.cocci --in-place --dir lustre/
+
+This applies the `test.cocci` semantic patch directory to the Lustre codebase. The
+tool doesn't actually create patch files, so the developer must still manually
+create granular commits. Some example scripts can be found in `contrib/cocci`.
+
+For automatically generated patches, it's useful to include a message like:
+
+ The patch has been generated with the coccinelle script below.
+
+Either the full script should be added to the commit message, or a pointer should
+be provided to the source.
+
+5.2 References
+==============
+
+Official Website: https://coccinelle.gitlabpages.inria.fr/website/documentation.html
+Kernel Documentation: https://www.kernel.org/doc/html/latest/dev-tools/coccinelle.html
+
==========================
-= 5. Miscellaneous Tools =
+= 6. Miscellaneous Tools =
==========================
Other tools that might be investigated, for those interested:
--- /dev/null
+// SPDX-License-Identifier: GPL-2.0
+//
+// Copyright (c) 2024, Amazon and/or its affiliates. All rights reserved.
+// Use is subject to license terms.
+//
+// Replace macros with explicit flag manipulation. Demonstrates Python
+// scripting available with Coccinelle.
+//
+// Author: Timothy Day <timday@amazon.com>
+//
+
+@ldlm_is@
+identifier MACRO =~ "ldlm_is";
+@@
+MACRO(...)
+
+@script:python get_flag_is@
+IDENT << ldlm_is.MACRO;
+MACRO;
+FLAG;
+@@
+coccinelle.MACRO = IDENT
+
+if "granted" in IDENT:
+ cocci.include_match(False)
+
+IDENT = IDENT.split("_")
+IDENT = IDENT[2:]
+IDENT = ["LDLM", "FL"] + IDENT
+IDENT = "_".join(IDENT).upper()
+coccinelle.FLAG = IDENT
+
+print(coccinelle.MACRO, coccinelle.FLAG)
+
+@convert_is@
+identifier get_flag_is.FLAG;
+identifier get_flag_is.MACRO;
+identifier L;
+@@
+- MACRO(L)
++ (L->l_flags & FLAG)
+
+@ldlm_set@
+identifier MACRO =~ "ldlm_set";
+@@
+MACRO(...)
+
+@script:python get_flag_set@
+IDENT << ldlm_set.MACRO;
+MACRO;
+FLAG;
+@@
+coccinelle.MACRO = IDENT
+
+IDENT = IDENT.split("_")
+IDENT = IDENT[2:]
+IDENT = ["LDLM", "FL"] + IDENT
+IDENT = "_".join(IDENT).upper()
+coccinelle.FLAG = IDENT
+
+print(coccinelle.MACRO, coccinelle.FLAG)
+
+@convert_set@
+identifier get_flag_set.FLAG;
+identifier get_flag_set.MACRO;
+identifier L;
+@@
+- MACRO(L)
++ (L->l_flags |= FLAG)
+
+@ldlm_clear@
+identifier MACRO =~ "ldlm_clear";
+@@
+MACRO(...)
+
+@script:python get_flag_clear@
+IDENT << ldlm_clear.MACRO;
+MACRO;
+FLAG;
+@@
+coccinelle.MACRO = IDENT
+
+if "blocking_lock" in IDENT:
+ cocci.include_match(False)
+if "blocking_data" in IDENT:
+ cocci.include_match(False)
+
+IDENT = IDENT.split("_")
+IDENT = IDENT[2:]
+IDENT = ["LDLM", "FL"] + IDENT
+IDENT = "_".join(IDENT).upper()
+coccinelle.FLAG = IDENT
+
+print(coccinelle.MACRO, coccinelle.FLAG)
+
+@convert_clear@
+identifier get_flag_clear.FLAG;
+identifier get_flag_clear.MACRO;
+identifier L;
+@@
+- MACRO(L)
++ (L->l_flags &= ~FLAG)