]> git.ipfire.org Git - thirdparty/git.git/commitdiff
cocci: add a "coccicheck-test" target and test *.cocci rules
authorÆvar Arnfjörð Bjarmason <avarab@gmail.com>
Tue, 5 Jul 2022 13:46:57 +0000 (15:46 +0200)
committerJunio C Hamano <gitster@pobox.com>
Wed, 6 Jul 2022 19:24:43 +0000 (12:24 -0700)
Add a "coccicheck-test" target to test our *.cocci rules, and as a
demonstration add tests for the rules added in 39ea59a2570 (remove
unnecessary NULL check before free(3), 2016-10-08) and
1b83d1251ed (coccinelle: add a rule to make "expression" code use
FREE_AND_NULL(), 2017-06-15).

I considered making use of the "spatch --test" option, and the choice
of a "tests" over a "t" directory is to make these tests compatible
with such a future change.

Unfortunately "spatch --test" doesn't return meaningful exit codes,
AFAICT you need to "grep" its output to see if the *.res is what you
expect. There's "--test-okfailed", but I didn't find a way to sensibly
integrate those (it relies on some in-between status files, but
doesn't help with the status codes).

Instead let's use a "--sp-file" pattern similar to the main
"coccicheck" rule, with the difference that we use and compare the
two *.res files with cmp(1).

The --very-quiet and --no-show-diff options ensure that we don't need
to pipe stdout and stderr somewhere. Unlike the "%.cocci.patch" rule
we're not using the diff.

The "cmp || git diff" is optimistically giving us better output on
failure, but even if we only have POSIX cmp and no system git
installed we'll still fail with the "cmp", just with an error message
that isn't as friendly. The "2>/dev/null" is in case we don't have a
"git" installed.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
Signed-off-by: Junio C Hamano <gitster@pobox.com>
Makefile
contrib/coccinelle/tests/free.c [new file with mode: 0644]
contrib/coccinelle/tests/free.res [new file with mode: 0644]
shared.mak

index 1ccf13595deb0f2b1ca518e8920e24944350e9e9..c4064f8394f4cd2c0e7493af36c7ff7e100c848b 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -3123,6 +3123,8 @@ check: $(GENERATED_H)
                exit 1; \
        fi
 
+COCCI_TEST_RES = $(wildcard contrib/coccinelle/tests/*.res)
+
 %.cocci.patch: %.cocci $(COCCI_SOURCES)
        $(QUIET_SPATCH) \
        if test $(SPATCH_BATCH_SIZE) = 0; then \
@@ -3143,6 +3145,22 @@ check: $(GENERATED_H)
        then \
                echo '    ' SPATCH result: $@; \
        fi
+
+COCCI_TEST_RES_GEN = $(addprefix .build/,$(COCCI_TEST_RES))
+$(COCCI_TEST_RES_GEN): .build/%.res : %.c
+$(COCCI_TEST_RES_GEN): .build/%.res : %.res
+$(COCCI_TEST_RES_GEN): .build/contrib/coccinelle/tests/%.res : contrib/coccinelle/%.cocci
+       $(call mkdir_p_parent_template)
+       $(QUIET_SPATCH_T)$(SPATCH) $(SPATCH_FLAGS) \
+               --very-quiet --no-show-diff \
+               --sp-file $< -o $@ \
+               $(@:.build/%.res=%.c) && \
+       cmp $(@:.build/%=%) $@ || \
+       git -P diff --no-index $(@:.build/%=%) $@ 2>/dev/null; \
+
+.PHONY: coccicheck-test
+coccicheck-test: $(COCCI_TEST_RES_GEN)
+
 coccicheck: $(addsuffix .patch,$(filter-out %.pending.cocci,$(wildcard contrib/coccinelle/*.cocci)))
 
 # See contrib/coccinelle/README
@@ -3404,6 +3422,7 @@ profile-clean:
        $(RM) $(addsuffix *.gcno,$(addprefix $(PROFILE_DIR)/, $(object_dirs)))
 
 cocciclean:
+       $(RM) -r .build/contrib/coccinelle
        $(RM) contrib/coccinelle/*.cocci.patch*
 
 clean: profile-clean coverage-clean cocciclean
diff --git a/contrib/coccinelle/tests/free.c b/contrib/coccinelle/tests/free.c
new file mode 100644 (file)
index 0000000..96d4abc
--- /dev/null
@@ -0,0 +1,11 @@
+int use_FREE_AND_NULL(int *v)
+{
+       free(*v);
+       *v = NULL;
+}
+
+int need_no_if(int *v)
+{
+       if (v)
+               free(v);
+}
diff --git a/contrib/coccinelle/tests/free.res b/contrib/coccinelle/tests/free.res
new file mode 100644 (file)
index 0000000..f90fd9f
--- /dev/null
@@ -0,0 +1,9 @@
+int use_FREE_AND_NULL(int *v)
+{
+       FREE_AND_NULL(*v);
+}
+
+int need_no_if(int *v)
+{
+       free(v);
+}
index 4330192e9c363df8103e27d5ccb06769adfbc0b8..33f43edbf9a6b2e156f15628869c1d6c1755cd6f 100644 (file)
@@ -70,6 +70,7 @@ ifndef V
        QUIET_HDR      = @echo '   ' HDR $(<:hcc=h);
        QUIET_RC       = @echo '   ' RC $@;
        QUIET_SPATCH   = @echo '   ' SPATCH $<;
+       QUIET_SPATCH_T = @echo '   ' SPATCH TEST $(@:.build/%=%);
 
 ## Used in "Documentation/Makefile"
        QUIET_ASCIIDOC  = @echo '   ' ASCIIDOC $@;