diff --git a/devel/Makefile.am b/devel/Makefile.am index 42f21be75e..4873dca5f0 100644 --- a/devel/Makefile.am +++ b/devel/Makefile.am @@ -1,53 +1,54 @@ # # Copyright 2020 the Pacemaker project contributors # # The version control history for this file may have further details. # # This source code is licensed under the GNU General Public License version 2 # or later (GPLv2+) WITHOUT ANY WARRANTY. # EXTRA_SCRIPTS = coccinelle/test/testrunner.sh EXTRA_DIST = $(EXTRA_SCRIPTS) \ coccinelle/ref-passed-variables-inited.cocci \ + coccinelle/string-empty.cocci \ coccinelle/test/ref-passed-variables-inited.input.c \ coccinelle/test/ref-passed-variables-inited.output # Coccinelle is a tool that takes special patch-like files (called semantic patches) and # applies them throughout a source tree. This is useful when refactoring, changing APIs, # catching dangerous or incorrect code, and other similar tasks. It's not especially # easy to write a semantic patch but most users should only be concerned about running # the target and inspecting the results. # # Documentation (including examples, which are the most useful): # http://coccinelle.lip6.fr/documentation.php # # Run the "make cocci" target to just output what would be done, or "make cocci-inplace" # to apply the changes to the source tree. # # COCCI_FILES may be set on the command line, if you want to test just a single file # while it's under development. Otherwise, it is a list of all the files that are ready # to be run. # # ref-passed-variables-inited.cocci seems to be returning some false positives around # GHashTableIters, so it is disabled for the moment. -COCCI_FILES ?= +COCCI_FILES ?= coccinelle/string-empty.cocci cocci: for f in $(COCCI_FILES); do \ for d in daemons include lib tools; do \ test $$d = "include" \ && spatch $(_SPATCH_FLAGS) --include-headers --local-includes \ --preprocess --sp-file $$f --dir ../$$d \ || spatch $(_SPATCH_FLAGS) --local-includes \ --preprocess --sp-file $$f --dir ../$$d; \ done; \ done cocci-inplace: $(MAKE) $(AM_MAKEFLAGS) _SPATCH_FLAGS=--in-place cocci cocci-test: for f in coccinelle/test/*.c; do \ coccinelle/test/testrunner.sh $$f; \ done diff --git a/devel/coccinelle/string-empty.cocci b/devel/coccinelle/string-empty.cocci new file mode 100644 index 0000000000..7abd558794 --- /dev/null +++ b/devel/coccinelle/string-empty.cocci @@ -0,0 +1,28 @@ +/* + * Copyright 2020 the Pacemaker project contributors + * + * The version control history for this file may have further details. + * + * This source code is licensed under the GNU General Public License version 2 + * or later (GPLv2+) WITHOUT ANY WARRANTY. + * + * Catch string comparisons where the pcmk__str_empty function could be used + * instead. Note that we are only catching uses involving identifiers (not + * expressions), but I think this is probably fine - we are likely not using + * the same expression multiple times in a single line of code. If some are + * found, it's easy enough to add another block here. + */ + +@ string_empty @ +identifier I; +@@ +( +- (I == NULL) || (strlen(I) == 0) ++ pcmk__str_empty(I) +| +- (I == NULL) || !strlen(I) ++ pcmk__str_empty(I) +| +- (I == NULL) || (I[0] == 0) ++ pcmk__str_empty(I) +)