Skip to content

Commit f7ff659

Browse files
avargitster
authored andcommitted
cocci: add a "coccicheck-test" target and test *.cocci rules
Add a "coccicheck-test" target to test our *.cocci rules, and as a demonstration add tests for the rules added in 39ea59a (remove unnecessary NULL check before free(3), 2016-10-08) and 1b83d12 (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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent af0aa69 commit f7ff659

File tree

4 files changed

+40
-0
lines changed

4 files changed

+40
-0
lines changed

Makefile

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3123,6 +3123,8 @@ check: $(GENERATED_H)
31233123
exit 1; \
31243124
fi
31253125

3126+
COCCI_TEST_RES = $(wildcard contrib/coccinelle/tests/*.res)
3127+
31263128
%.cocci.patch: %.cocci $(COCCI_SOURCES)
31273129
$(QUIET_SPATCH) \
31283130
if test $(SPATCH_BATCH_SIZE) = 0; then \
@@ -3143,6 +3145,22 @@ check: $(GENERATED_H)
31433145
then \
31443146
echo ' ' SPATCH result: $@; \
31453147
fi
3148+
3149+
COCCI_TEST_RES_GEN = $(addprefix .build/,$(COCCI_TEST_RES))
3150+
$(COCCI_TEST_RES_GEN): .build/%.res : %.c
3151+
$(COCCI_TEST_RES_GEN): .build/%.res : %.res
3152+
$(COCCI_TEST_RES_GEN): .build/contrib/coccinelle/tests/%.res : contrib/coccinelle/%.cocci
3153+
$(call mkdir_p_parent_template)
3154+
$(QUIET_SPATCH_T)$(SPATCH) $(SPATCH_FLAGS) \
3155+
--very-quiet --no-show-diff \
3156+
--sp-file $< -o $@ \
3157+
$(@:.build/%.res=%.c) && \
3158+
cmp $(@:.build/%=%) $@ || \
3159+
git -P diff --no-index $(@:.build/%=%) $@ 2>/dev/null; \
3160+
3161+
.PHONY: coccicheck-test
3162+
coccicheck-test: $(COCCI_TEST_RES_GEN)
3163+
31463164
coccicheck: $(addsuffix .patch,$(filter-out %.pending.cocci,$(wildcard contrib/coccinelle/*.cocci)))
31473165

31483166
# See contrib/coccinelle/README
@@ -3404,6 +3422,7 @@ profile-clean:
34043422
$(RM) $(addsuffix *.gcno,$(addprefix $(PROFILE_DIR)/, $(object_dirs)))
34053423

34063424
cocciclean:
3425+
$(RM) -r .build/contrib/coccinelle
34073426
$(RM) contrib/coccinelle/*.cocci.patch*
34083427

34093428
clean: profile-clean coverage-clean cocciclean

contrib/coccinelle/tests/free.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
int use_FREE_AND_NULL(int *v)
2+
{
3+
free(*v);
4+
*v = NULL;
5+
}
6+
7+
int need_no_if(int *v)
8+
{
9+
if (v)
10+
free(v);
11+
}

contrib/coccinelle/tests/free.res

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
int use_FREE_AND_NULL(int *v)
2+
{
3+
FREE_AND_NULL(*v);
4+
}
5+
6+
int need_no_if(int *v)
7+
{
8+
free(v);
9+
}

shared.mak

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@ ifndef V
7070
QUIET_HDR = @echo ' ' HDR $(<:hcc=h);
7171
QUIET_RC = @echo ' ' RC $@;
7272
QUIET_SPATCH = @echo ' ' SPATCH $<;
73+
QUIET_SPATCH_T = @echo ' ' SPATCH TEST $(@:.build/%=%);
7374

7475
## Used in "Documentation/Makefile"
7576
QUIET_ASCIIDOC = @echo ' ' ASCIIDOC $@;

0 commit comments

Comments
 (0)