From 8ca1b4472ce97ae1d120608f9f02a86fa33d4187 Mon Sep 17 00:00:00 2001 From: Patrick Steinhardt Date: Thu, 19 Mar 2026 06:33:21 +0100 Subject: contrib: move "coccinelle/" directory into "tools/" The Coccinelle tool is an ingrained part of our build infrastructure. It is executed by our CI to detect antipatterns and is used to detect misuses of certain interfaces. It's presence in "contrib/" is thus rather misleading. Promote the configuration into the new "tools/" directory. Signed-off-by: Patrick Steinhardt Signed-off-by: Junio C Hamano --- Makefile | 40 ++-- ci/run-static-analysis.sh | 2 +- contrib/coccinelle/.gitignore | 1 - contrib/coccinelle/README | 124 ---------- contrib/coccinelle/array.cocci | 147 ------------ contrib/coccinelle/commit.cocci | 53 ----- contrib/coccinelle/config_fn_ctx.pending.cocci | 144 ------------ contrib/coccinelle/equals-null.cocci | 30 --- contrib/coccinelle/flex_alloc.cocci | 13 -- contrib/coccinelle/free.cocci | 45 ---- contrib/coccinelle/git_config_number.cocci | 27 --- contrib/coccinelle/hashmap.cocci | 16 -- contrib/coccinelle/index-compatibility.cocci | 157 ------------- contrib/coccinelle/meson.build | 85 ------- contrib/coccinelle/object_id.cocci | 75 ------ contrib/coccinelle/preincr.cocci | 5 - contrib/coccinelle/qsort.cocci | 37 --- contrib/coccinelle/refs.cocci | 103 --------- contrib/coccinelle/spatchcache | 304 ------------------------- contrib/coccinelle/strbuf.cocci | 62 ----- contrib/coccinelle/swap.cocci | 28 --- contrib/coccinelle/tests/free.c | 11 - contrib/coccinelle/tests/free.res | 9 - contrib/coccinelle/the_repository.cocci | 18 -- contrib/coccinelle/xcalloc.cocci | 10 - contrib/coccinelle/xopen.cocci | 19 -- contrib/coccinelle/xstrdup_or_null.cocci | 5 - contrib/coccinelle/xstrncmpz.cocci | 28 --- contrib/meson.build | 1 - tools/coccinelle/.gitignore | 1 + tools/coccinelle/README | 124 ++++++++++ tools/coccinelle/array.cocci | 147 ++++++++++++ tools/coccinelle/commit.cocci | 53 +++++ tools/coccinelle/config_fn_ctx.pending.cocci | 144 ++++++++++++ tools/coccinelle/equals-null.cocci | 30 +++ tools/coccinelle/flex_alloc.cocci | 13 ++ tools/coccinelle/free.cocci | 45 ++++ tools/coccinelle/git_config_number.cocci | 27 +++ tools/coccinelle/hashmap.cocci | 16 ++ tools/coccinelle/index-compatibility.cocci | 157 +++++++++++++ tools/coccinelle/meson.build | 85 +++++++ tools/coccinelle/object_id.cocci | 75 ++++++ tools/coccinelle/preincr.cocci | 5 + tools/coccinelle/qsort.cocci | 37 +++ tools/coccinelle/refs.cocci | 103 +++++++++ tools/coccinelle/spatchcache | 304 +++++++++++++++++++++++++ tools/coccinelle/strbuf.cocci | 62 +++++ tools/coccinelle/swap.cocci | 28 +++ tools/coccinelle/tests/free.c | 11 + tools/coccinelle/tests/free.res | 9 + tools/coccinelle/the_repository.cocci | 18 ++ tools/coccinelle/xcalloc.cocci | 10 + tools/coccinelle/xopen.cocci | 19 ++ tools/coccinelle/xstrdup_or_null.cocci | 5 + tools/coccinelle/xstrncmpz.cocci | 28 +++ tools/meson.build | 1 + 56 files changed, 1578 insertions(+), 1578 deletions(-) delete mode 100644 contrib/coccinelle/.gitignore delete mode 100644 contrib/coccinelle/README delete mode 100644 contrib/coccinelle/array.cocci delete mode 100644 contrib/coccinelle/commit.cocci delete mode 100644 contrib/coccinelle/config_fn_ctx.pending.cocci delete mode 100644 contrib/coccinelle/equals-null.cocci delete mode 100644 contrib/coccinelle/flex_alloc.cocci delete mode 100644 contrib/coccinelle/free.cocci delete mode 100644 contrib/coccinelle/git_config_number.cocci delete mode 100644 contrib/coccinelle/hashmap.cocci delete mode 100644 contrib/coccinelle/index-compatibility.cocci delete mode 100644 contrib/coccinelle/meson.build delete mode 100644 contrib/coccinelle/object_id.cocci delete mode 100644 contrib/coccinelle/preincr.cocci delete mode 100644 contrib/coccinelle/qsort.cocci delete mode 100644 contrib/coccinelle/refs.cocci delete mode 100755 contrib/coccinelle/spatchcache delete mode 100644 contrib/coccinelle/strbuf.cocci delete mode 100644 contrib/coccinelle/swap.cocci delete mode 100644 contrib/coccinelle/tests/free.c delete mode 100644 contrib/coccinelle/tests/free.res delete mode 100644 contrib/coccinelle/the_repository.cocci delete mode 100644 contrib/coccinelle/xcalloc.cocci delete mode 100644 contrib/coccinelle/xopen.cocci delete mode 100644 contrib/coccinelle/xstrdup_or_null.cocci delete mode 100644 contrib/coccinelle/xstrncmpz.cocci create mode 100644 tools/coccinelle/.gitignore create mode 100644 tools/coccinelle/README create mode 100644 tools/coccinelle/array.cocci create mode 100644 tools/coccinelle/commit.cocci create mode 100644 tools/coccinelle/config_fn_ctx.pending.cocci create mode 100644 tools/coccinelle/equals-null.cocci create mode 100644 tools/coccinelle/flex_alloc.cocci create mode 100644 tools/coccinelle/free.cocci create mode 100644 tools/coccinelle/git_config_number.cocci create mode 100644 tools/coccinelle/hashmap.cocci create mode 100644 tools/coccinelle/index-compatibility.cocci create mode 100644 tools/coccinelle/meson.build create mode 100644 tools/coccinelle/object_id.cocci create mode 100644 tools/coccinelle/preincr.cocci create mode 100644 tools/coccinelle/qsort.cocci create mode 100644 tools/coccinelle/refs.cocci create mode 100755 tools/coccinelle/spatchcache create mode 100644 tools/coccinelle/strbuf.cocci create mode 100644 tools/coccinelle/swap.cocci create mode 100644 tools/coccinelle/tests/free.c create mode 100644 tools/coccinelle/tests/free.res create mode 100644 tools/coccinelle/the_repository.cocci create mode 100644 tools/coccinelle/xcalloc.cocci create mode 100644 tools/coccinelle/xopen.cocci create mode 100644 tools/coccinelle/xstrdup_or_null.cocci create mode 100644 tools/coccinelle/xstrncmpz.cocci diff --git a/Makefile b/Makefile index c7cedbcd7c..8564b1be36 100644 --- a/Makefile +++ b/Makefile @@ -1005,8 +1005,8 @@ SPATCH_TEST_FLAGS = # COMPUTE_HEADER_DEPENDENCIES=no this will be unset too. SPATCH_USE_O_DEPENDENCIES = YesPlease -# Set SPATCH_CONCAT_COCCI to concatenate the contrib/cocci/*.cocci -# files into a single contrib/cocci/ALL.cocci before running +# Set SPATCH_CONCAT_COCCI to concatenate the tools/coccinelle/*.cocci +# files into a single tools/coccinelle/ALL.cocci before running # "coccicheck". # # Pros: @@ -1025,7 +1025,7 @@ SPATCH_USE_O_DEPENDENCIES = YesPlease # generate a specific patch, e.g. this will always use strbuf.cocci, # not ALL.cocci: # -# make contrib/coccinelle/strbuf.cocci.patch +# make tools/coccinelle/strbuf.cocci.patch SPATCH_CONCAT_COCCI = YesPlease # Rebuild 'coccicheck' if $(SPATCH), its flags etc. change @@ -3457,15 +3457,15 @@ check: exit 1; \ fi -COCCI_GEN_ALL = .build/contrib/coccinelle/ALL.cocci -COCCI_GLOB = $(wildcard contrib/coccinelle/*.cocci) +COCCI_GEN_ALL = .build/tools/coccinelle/ALL.cocci +COCCI_GLOB = $(wildcard tools/coccinelle/*.cocci) COCCI_RULES_TRACKED = $(COCCI_GLOB:%=.build/%) COCCI_RULES_TRACKED_NO_PENDING = $(filter-out %.pending.cocci,$(COCCI_RULES_TRACKED)) COCCI_RULES = COCCI_RULES += $(COCCI_GEN_ALL) COCCI_RULES += $(COCCI_RULES_TRACKED) COCCI_NAMES = -COCCI_NAMES += $(COCCI_RULES:.build/contrib/coccinelle/%.cocci=%) +COCCI_NAMES += $(COCCI_RULES:.build/tools/coccinelle/%.cocci=%) COCCICHECK_PENDING = $(filter %.pending.cocci,$(COCCI_RULES)) COCCICHECK = $(filter-out $(COCCICHECK_PENDING),$(COCCI_RULES)) @@ -3480,20 +3480,20 @@ COCCICHECK_PATCHES_PENDING_INTREE = $(COCCICHECK_PATCHES_PENDING:.build/%=%) # on $(MAKECMDGOALS) that match these $(COCCI_RULES) COCCI_RULES_GLOB = COCCI_RULES_GLOB += cocci% -COCCI_RULES_GLOB += .build/contrib/coccinelle/% +COCCI_RULES_GLOB += .build/tools/coccinelle/% COCCI_RULES_GLOB += $(COCCICHECK_PATCHES) COCCI_RULES_GLOB += $(COCCICHEC_PATCHES_PENDING) COCCI_RULES_GLOB += $(COCCICHECK_PATCHES_INTREE) COCCI_RULES_GLOB += $(COCCICHECK_PATCHES_PENDING_INTREE) COCCI_GOALS = $(filter $(COCCI_RULES_GLOB),$(MAKECMDGOALS)) -COCCI_TEST_RES = $(wildcard contrib/coccinelle/tests/*.res) +COCCI_TEST_RES = $(wildcard tools/coccinelle/tests/*.res) $(COCCI_RULES_TRACKED): .build/% : % $(call mkdir_p_parent_template) $(QUIET_CP)cp $< $@ -.build/contrib/coccinelle/FOUND_H_SOURCES: $(FOUND_H_SOURCES) +.build/tools/coccinelle/FOUND_H_SOURCES: $(FOUND_H_SOURCES) $(call mkdir_p_parent_template) $(QUIET_GEN) >$@ @@ -3507,12 +3507,12 @@ endif define cocci-rule ## Rule for .build/$(1).patch/$(2); Params: -# $(1) = e.g. ".build/contrib/coccinelle/free.cocci" +# $(1) = e.g. ".build/tools/coccinelle/free.cocci" # $(2) = e.g. "grep.c" # $(3) = e.g. "grep.o" -COCCI_$(1:.build/contrib/coccinelle/%.cocci=%) += $(1).d/$(2).patch +COCCI_$(1:.build/tools/coccinelle/%.cocci=%) += $(1).d/$(2).patch $(1).d/$(2).patch: GIT-SPATCH-DEFINES -$(1).d/$(2).patch: $(if $(and $(SPATCH_USE_O_DEPENDENCIES),$(wildcard $(3))),$(3),.build/contrib/coccinelle/FOUND_H_SOURCES) +$(1).d/$(2).patch: $(if $(and $(SPATCH_USE_O_DEPENDENCIES),$(wildcard $(3))),$(3),.build/tools/coccinelle/FOUND_H_SOURCES) $(1).d/$(2).patch: $(1) $(1).d/$(2).patch: $(1).d/%.patch : % $$(call mkdir_p_parent_template) @@ -3538,13 +3538,13 @@ endif define spatch-rule -.build/contrib/coccinelle/$(1).cocci.patch: $$(COCCI_$(1)) +.build/tools/coccinelle/$(1).cocci.patch: $$(COCCI_$(1)) $$(QUIET_SPATCH_CAT)cat $$^ >$$@ && \ if test -s $$@; \ then \ echo ' ' SPATCH result: $$@; \ fi -contrib/coccinelle/$(1).cocci.patch: .build/contrib/coccinelle/$(1).cocci.patch +tools/coccinelle/$(1).cocci.patch: .build/tools/coccinelle/$(1).cocci.patch $$(QUIET_CP)cp $$< $$@ endef @@ -3558,9 +3558,9 @@ $(COCCI_TEST_RES_GEN): GIT-SPATCH-DEFINES $(COCCI_TEST_RES_GEN): .build/%.res : %.c $(COCCI_TEST_RES_GEN): .build/%.res : %.res ifdef SPATCH_CONCAT_COCCI -$(COCCI_TEST_RES_GEN): .build/contrib/coccinelle/tests/%.res : $(COCCI_GEN_ALL) +$(COCCI_TEST_RES_GEN): .build/tools/coccinelle/tests/%.res : $(COCCI_GEN_ALL) else -$(COCCI_TEST_RES_GEN): .build/contrib/coccinelle/tests/%.res : contrib/coccinelle/%.cocci +$(COCCI_TEST_RES_GEN): .build/tools/coccinelle/tests/%.res : tools/coccinelle/%.cocci endif $(call mkdir_p_parent_template) $(QUIET_SPATCH_TEST)$(SPATCH) $(SPATCH_TEST_FLAGS) \ @@ -3576,14 +3576,14 @@ coccicheck-test: $(COCCI_TEST_RES_GEN) coccicheck: coccicheck-test ifdef SPATCH_CONCAT_COCCI -COCCICHECK_PATCH_MUST_BE_EMPTY_FILES = contrib/coccinelle/ALL.cocci.patch +COCCICHECK_PATCH_MUST_BE_EMPTY_FILES = tools/coccinelle/ALL.cocci.patch else COCCICHECK_PATCH_MUST_BE_EMPTY_FILES = $(COCCICHECK_PATCHES_INTREE) endif coccicheck: $(COCCICHECK_PATCH_MUST_BE_EMPTY_FILES) ! grep ^ $(COCCICHECK_PATCH_MUST_BE_EMPTY_FILES) /dev/null -# See contrib/coccinelle/README +# See tools/coccinelle/README coccicheck-pending: coccicheck-test coccicheck-pending: $(COCCICHECK_PATCHES_PENDING_INTREE) @@ -3857,8 +3857,8 @@ profile-clean: cocciclean: $(RM) GIT-SPATCH-DEFINES - $(RM) -r .build/contrib/coccinelle - $(RM) contrib/coccinelle/*.cocci.patch + $(RM) -r .build/tools/coccinelle + $(RM) tools/coccinelle/*.cocci.patch clean: profile-clean coverage-clean cocciclean $(RM) -r .build $(UNIT_TEST_BIN) diff --git a/ci/run-static-analysis.sh b/ci/run-static-analysis.sh index 9e9c72681d..ba67e80b4d 100755 --- a/ci/run-static-analysis.sh +++ b/ci/run-static-analysis.sh @@ -10,7 +10,7 @@ make coccicheck set +x fail= -for cocci_patch in contrib/coccinelle/*.patch +for cocci_patch in tools/coccinelle/*.patch do if test -s "$cocci_patch" then diff --git a/contrib/coccinelle/.gitignore b/contrib/coccinelle/.gitignore deleted file mode 100644 index 1d45c0a40c..0000000000 --- a/contrib/coccinelle/.gitignore +++ /dev/null @@ -1 +0,0 @@ -*.patch diff --git a/contrib/coccinelle/README b/contrib/coccinelle/README deleted file mode 100644 index 055ad0e06a..0000000000 --- a/contrib/coccinelle/README +++ /dev/null @@ -1,124 +0,0 @@ -= coccinelle - -This directory provides Coccinelle (http://coccinelle.lip6.fr/) semantic patches -that might be useful to developers. - -== Types of semantic patches - - * Using the semantic transformation to check for bad patterns in the code; - The target 'make coccicheck' is designed to check for these patterns and - it is expected that any resulting patch indicates a regression. - The patches resulting from 'make coccicheck' are small and infrequent, - so once they are found, they can be sent to the mailing list as per usual. - - Example for introducing new patterns: - 67947c34ae (convert "hashcmp() != 0" to "!hasheq()", 2018-08-28) - b84c783882 (fsck: s/++i > 1/i++/, 2018-10-24) - - Example of fixes using this approach: - 248f66ed8e (run-command: use strbuf_addstr() for adding a string to - a strbuf, 2018-03-25) - f919ffebed (Use MOVE_ARRAY, 2018-01-22) - - These types of semantic patches are usually part of testing, c.f. - 0860a7641b (travis-ci: fail if Coccinelle static analysis found something - to transform, 2018-07-23) - - * Using semantic transformations in large scale refactorings throughout - the code base. - - When applying the semantic patch into a real patch, sending it to the - mailing list in the usual way, such a patch would be expected to have a - lot of textual and semantic conflicts as such large scale refactorings - change function signatures that are used widely in the code base. - A textual conflict would arise if surrounding code near any call of such - function changes. A semantic conflict arises when other patch series in - flight introduce calls to such functions. - - So to aid these large scale refactorings, semantic patches can be used. - However we do not want to store them in the same place as the checks for - bad patterns, as then automated builds would fail. - That is why semantic patches 'contrib/coccinelle/*.pending.cocci' - are ignored for checks, and can be applied using 'make coccicheck-pending'. - - This allows to expose plans of pending large scale refactorings without - impacting the bad pattern checks. - -== Git-specific tips & things to know about how we run "spatch": - - * The "make coccicheck" will piggy-back on - "COMPUTE_HEADER_DEPENDENCIES". If you've built a given object file - the "coccicheck" target will consider its depednency to decide if - it needs to re-run on the corresponding source file. - - This means that a "make coccicheck" will re-compile object files - before running. This might be unexpected, but speeds up the run in - the common case, as e.g. a change to "column.h" won't require all - coccinelle rules to be re-run against "grep.c" (or another file - that happens not to use "column.h"). - - To disable this behavior use the "SPATCH_USE_O_DEPENDENCIES=NoThanks" - flag. - - * To speed up our rules the "make coccicheck" target will by default - concatenate all of the *.cocci files here into an "ALL.cocci", and - apply it to each source file. - - This makes the run faster, as we don't need to run each rule - against each source file. See the Makefile for further discussion, - this behavior can be disabled with "SPATCH_CONCAT_COCCI=". - - But since they're concatenated any in the (e.g. "@ - my_name", v.s. anonymous "@@") needs to be unique across all our - *.cocci files. You should only need to name rules if other rules - depend on them (currently only one rule is named). - - * To speed up incremental runs even more use the "spatchcache" tool - in this directory as your "SPATCH". It aimns to be a "ccache" for - coccinelle, and piggy-backs on "COMPUTE_HEADER_DEPENDENCIES". - - It caches in Redis by default, see it source for a how-to. - - In one setup with a primed cache "make coccicheck" followed by a - "make clean && make" takes around 10s to run, but 2m30s with the - default of "SPATCH_CONCAT_COCCI=Y". - - With "SPATCH_CONCAT_COCCI=" the total runtime is around ~6m, sped - up to ~1m with "spatchcache". - - Most of the 10s (or ~1m) being spent on re-running "spatch" on - files we couldn't cache, as we didn't compile them (in contrib/* - and compat/* mostly). - - The absolute times will differ for you, but the relative speedup - from caching should be on that order. - -== Authoring and reviewing coccinelle changes - -* When a .cocci is made, both the Git changes and .cocci file should be - reviewed. When reviewing such a change, do your best to understand the .cocci - changes (e.g. by asking the author to explain the change) and be explicit - about your understanding of the changes. This helps us decide whether input - from coccinelle experts is needed or not. If you aren't sure of the cocci - changes, indicate what changes you actively endorse and leave an Acked-by - (instead of Reviewed-by). - -* Authors should consider that reviewers may not be coccinelle experts, thus the - the .cocci changes may not be self-evident. A plain text description of the - changes is strongly encouraged, especially when using more esoteric features - of the language. - -* .cocci rules should target only the problem it is trying to solve; "collateral - damage" is not allowed. Reviewers should look out and flag overly-broad rules. - -* Consider the cost-benefit ratio of .cocci changes. In particular, consider the - effect on the runtime of "make coccicheck", and how often your .cocci check - will catch something valuable. As a rule of thumb, rules that can bail early - if a file doesn't have a particular token will have a small impact on runtime, - and vice-versa. - -* .cocci files used for refactoring should be temporarily kept in-tree to aid - the refactoring of out-of-tree code (e.g. in-flight topics). Periodically - evaluate the cost-benefit ratio to determine when the file should be removed. - For example, consider how many out-of-tree users are left and how much this - slows down "make coccicheck". diff --git a/contrib/coccinelle/array.cocci b/contrib/coccinelle/array.cocci deleted file mode 100644 index e71baea00b..0000000000 --- a/contrib/coccinelle/array.cocci +++ /dev/null @@ -1,147 +0,0 @@ -@@ -type T; -T *dst_ptr; -T *src_ptr; -expression n; -@@ -- memcpy(dst_ptr, src_ptr, (n) * \( sizeof(T) -- \| sizeof(*(dst_ptr)) -- \| sizeof(*(src_ptr)) -- \| sizeof(dst_ptr[...]) -- \| sizeof(src_ptr[...]) -- \) ) -+ COPY_ARRAY(dst_ptr, src_ptr, n) - -@@ -type T; -T *dst_ptr; -T[] src_arr; -expression n; -@@ -- memcpy(dst_ptr, src_arr, (n) * \( sizeof(T) -- \| sizeof(*(dst_ptr)) -- \| sizeof(*(src_arr)) -- \| sizeof(dst_ptr[...]) -- \| sizeof(src_arr[...]) -- \) ) -+ COPY_ARRAY(dst_ptr, src_arr, n) - -@@ -type T; -T[] dst_arr; -T *src_ptr; -expression n; -@@ -- memcpy(dst_arr, src_ptr, (n) * \( sizeof(T) -- \| sizeof(*(dst_arr)) -- \| sizeof(*(src_ptr)) -- \| sizeof(dst_arr[...]) -- \| sizeof(src_ptr[...]) -- \) ) -+ COPY_ARRAY(dst_arr, src_ptr, n) - -@@ -type T; -T[] dst_arr; -T[] src_arr; -expression n; -@@ -- memcpy(dst_arr, src_arr, (n) * \( sizeof(T) -- \| sizeof(*(dst_arr)) -- \| sizeof(*(src_arr)) -- \| sizeof(dst_arr[...]) -- \| sizeof(src_arr[...]) -- \) ) -+ COPY_ARRAY(dst_arr, src_arr, n) - -@@ -type T; -T *dst; -T *src; -expression n; -@@ -( -- memmove(dst, src, (n) * sizeof(*dst)); -+ MOVE_ARRAY(dst, src, n); -| -- memmove(dst, src, (n) * sizeof(*src)); -+ MOVE_ARRAY(dst, src, n); -| -- memmove(dst, src, (n) * sizeof(T)); -+ MOVE_ARRAY(dst, src, n); -) - -@@ -type T; -T *ptr; -expression n; -@@ -- ptr = xmalloc((n) * sizeof(*ptr)); -+ ALLOC_ARRAY(ptr, n); - -@@ -type T; -T *ptr; -expression n; -@@ -- ptr = xmalloc((n) * sizeof(T)); -+ ALLOC_ARRAY(ptr, n); - -@@ -type T; -T *ptr; -expression n != 1; -@@ -- ptr = xcalloc(n, \( sizeof(*ptr) \| sizeof(T) \) ) -+ CALLOC_ARRAY(ptr, n) - -@@ -expression dst, src, n; -@@ --ALLOC_ARRAY(dst, n); --COPY_ARRAY(dst, src, n); -+DUP_ARRAY(dst, src, n); - -@@ -type T; -T *ptr; -expression n; -@@ -- memset(ptr, \( 0 \| '\0' \), \( (n) \| n \) * \( sizeof(T) -- \| sizeof(ptr[...]) -- \| sizeof(*ptr) -- \) ) -+ MEMZERO_ARRAY(ptr, n) - -@@ -type T; -T *ptr; -expression n; -@@ -- memset(ptr, \( 0 \| '\0' \), \( sizeof(T) -- \| sizeof(ptr[...]) -- \| sizeof(*ptr) -- \) * \( (n) \| n \) ) -+ MEMZERO_ARRAY(ptr, n) - -@@ -type T; -T[] ptr; -expression n; -@@ -- memset(ptr, \( 0 \| '\0' \), \( (n) \| n \) * \( sizeof(T) -- \| sizeof(ptr[...]) -- \| sizeof(*ptr) -- \) ) -+ MEMZERO_ARRAY(ptr, n) - -@@ -type T; -T[] ptr; -expression n; -@@ -- memset(ptr, \( 0 \| '\0' \), \( sizeof(T) -- \| sizeof(ptr[...]) -- \| sizeof(*ptr) -- \) * \( (n) \| n \) ) -+ MEMZERO_ARRAY(ptr, n) diff --git a/contrib/coccinelle/commit.cocci b/contrib/coccinelle/commit.cocci deleted file mode 100644 index 42725161e9..0000000000 --- a/contrib/coccinelle/commit.cocci +++ /dev/null @@ -1,53 +0,0 @@ -@@ -expression c; -@@ -- &c->maybe_tree->object.oid -+ get_commit_tree_oid(c) - -@@ -expression c; -@@ -- c->maybe_tree->object.oid.hash -+ get_commit_tree_oid(c)->hash - -@@ -identifier f !~ "^set_commit_tree$"; -expression c; -expression s; -@@ - f(...) {<... -- c->maybe_tree = s -+ set_commit_tree(c, s) - ...>} - -// These excluded functions must access c->maybe_tree directly. -// Note that if c->maybe_tree is written somewhere outside of these -// functions, then the recommended transformation will be bogus with -// repo_get_commit_tree() on the LHS. -@@ -identifier f != { repo_get_commit_tree, get_commit_tree_in_graph_one, - load_tree_for_commit, set_commit_tree, repo_parse_commit_no_graph }; -expression c; -@@ - f(...) {<... -- c->maybe_tree -+ repo_get_commit_tree(specify_the_right_repo_here, c) - ...>} - -@@ -struct commit *c; -expression E; -@@ -( -- c->generation = E; -+ commit_graph_data_at(c)->generation = E; -| -- c->graph_pos = E; -+ commit_graph_data_at(c)->graph_pos = E; -| -- c->generation -+ commit_graph_generation(c) -| -- c->graph_pos -+ commit_graph_position(c) -) diff --git a/contrib/coccinelle/config_fn_ctx.pending.cocci b/contrib/coccinelle/config_fn_ctx.pending.cocci deleted file mode 100644 index 54f09fcbcd..0000000000 --- a/contrib/coccinelle/config_fn_ctx.pending.cocci +++ /dev/null @@ -1,144 +0,0 @@ -@ get_fn @ -identifier fn, R; -@@ -( -( -git_config_from_file -| -git_config_from_file_with_options -| -git_config_from_mem -| -git_config_from_blob_oid -| -read_early_config -| -read_very_early_config -| -config_with_options -| -git_config -| -git_protected_config -| -config_from_gitmodules -) - (fn, ...) -| -repo_config(R, fn, ...) -) - -@ extends get_fn @ -identifier C1, C2, D; -@@ -int fn(const char *C1, const char *C2, -+ const struct config_context *ctx, - void *D); - -@ extends get_fn @ -@@ -int fn(const char *, const char *, -+ const struct config_context *, - void *); - -@ extends get_fn @ -// Don't change fns that look like callback fns but aren't -identifier fn2 != tar_filter_config && != git_diff_heuristic_config && - != git_default_submodule_config && != git_color_config && - != bundle_list_update && != parse_object_filter_config; -identifier C1, C2, D1, D2, S; -attribute name UNUSED; -@@ -int fn(const char *C1, const char *C2, -+ const struct config_context *ctx, - void *D1) { -<+... -( -fn2(C1, C2 -+ , ctx -, D2); -| -if(fn2(C1, C2 -+ , ctx -, D2) < 0) { ... } -| -return fn2(C1, C2 -+ , ctx -, D2); -| -S = fn2(C1, C2 -+ , ctx -, D2); -) -...+> - } - -@ extends get_fn@ -identifier C1, C2, D; -attribute name UNUSED; -@@ -int fn(const char *C1, const char *C2, -+ const struct config_context *ctx UNUSED, - void *D) {...} - - -// The previous rules don't catch all callbacks, especially if they're defined -// in a separate file from the repo_config() call. Fix these manually. -@@ -identifier C1, C2, D; -attribute name UNUSED; -@@ -int -( -git_ident_config -| -urlmatch_collect_fn -| -write_one_config -| -forbid_remote_url -| -credential_config_callback -) - (const char *C1, const char *C2, -+ const struct config_context *ctx UNUSED, - void *D) {...} - -@@ -identifier C1, C2, D, D2, S, fn2; -@@ -int -( -http_options -| -git_status_config -| -git_commit_config -| -git_default_core_config -| -grep_config -) - (const char *C1, const char *C2, -+ const struct config_context *ctx, - void *D) { -<+... -( -fn2(C1, C2 -+ , ctx -, D2); -| -if(fn2(C1, C2 -+ , ctx -, D2) < 0) { ... } -| -return fn2(C1, C2 -+ , ctx -, D2); -| -S = fn2(C1, C2 -+ , ctx -, D2); -) -...+> - } diff --git a/contrib/coccinelle/equals-null.cocci b/contrib/coccinelle/equals-null.cocci deleted file mode 100644 index 92c7054013..0000000000 --- a/contrib/coccinelle/equals-null.cocci +++ /dev/null @@ -1,30 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1-or-later */ -@@ -expression e; -statement s; -@@ -if ( -( -!e -| -- e == NULL -+ !e -) - ) - {...} -else s - -@@ -expression e; -statement s; -@@ -if ( -( -e -| -- e != NULL -+ e -) - ) - {...} -else s diff --git a/contrib/coccinelle/flex_alloc.cocci b/contrib/coccinelle/flex_alloc.cocci deleted file mode 100644 index e9f7f6d861..0000000000 --- a/contrib/coccinelle/flex_alloc.cocci +++ /dev/null @@ -1,13 +0,0 @@ -@@ -expression str; -identifier x, flexname; -@@ -- FLEX_ALLOC_MEM(x, flexname, str, strlen(str)); -+ FLEX_ALLOC_STR(x, flexname, str); - -@@ -expression str; -identifier x, ptrname; -@@ -- FLEXPTR_ALLOC_MEM(x, ptrname, str, strlen(str)); -+ FLEXPTR_ALLOC_STR(x, ptrname, str); diff --git a/contrib/coccinelle/free.cocci b/contrib/coccinelle/free.cocci deleted file mode 100644 index 03799e1908..0000000000 --- a/contrib/coccinelle/free.cocci +++ /dev/null @@ -1,45 +0,0 @@ -@@ -expression E; -@@ -- if (E) -( - free(E); -| - commit_list_free(E); -) - -@@ -expression E; -@@ -- if (!E) -( - free(E); -| - commit_list_free(E); -) - -@@ -expression E; -@@ -- free(E); -+ FREE_AND_NULL(E); -- E = NULL; - -@@ -expression E; -@@ -- if (E) -- { - commit_list_free(E); - E = NULL; -- } - -@@ -expression E; -statement S; -@@ -- if (E) { -+ if (E) - S - commit_list_free(E); -- } diff --git a/contrib/coccinelle/git_config_number.cocci b/contrib/coccinelle/git_config_number.cocci deleted file mode 100644 index 7b57dceefe..0000000000 --- a/contrib/coccinelle/git_config_number.cocci +++ /dev/null @@ -1,27 +0,0 @@ -@@ -identifier C1, C2, C3; -@@ -( -( -git_config_int -| -git_config_int64 -| -git_config_ulong -| -git_config_ssize_t -) - (C1, C2 -+ , ctx->kvi - ) -| -( -git_configset_get_value -| -git_config_bool_or_int -) - (C1, C2 -+ , ctx->kvi - , C3 - ) -) diff --git a/contrib/coccinelle/hashmap.cocci b/contrib/coccinelle/hashmap.cocci deleted file mode 100644 index c5dbb4557b..0000000000 --- a/contrib/coccinelle/hashmap.cocci +++ /dev/null @@ -1,16 +0,0 @@ -@@ -expression E; -struct hashmap_entry HME; -@@ -- HME.hash = E; -+ hashmap_entry_init(&HME, E); - -@@ -identifier f !~ "^hashmap_entry_init$"; -expression E; -struct hashmap_entry *HMEP; -@@ - f(...) {<... -- HMEP->hash = E; -+ hashmap_entry_init(HMEP, E); - ...>} diff --git a/contrib/coccinelle/index-compatibility.cocci b/contrib/coccinelle/index-compatibility.cocci deleted file mode 100644 index 31e36cf3c4..0000000000 --- a/contrib/coccinelle/index-compatibility.cocci +++ /dev/null @@ -1,157 +0,0 @@ -// the_index.* variables -@@ -identifier AC = active_cache; -identifier AN = active_nr; -identifier ACC = active_cache_changed; -identifier ACT = active_cache_tree; -@@ -( -- AC -+ the_index.cache -| -- AN -+ the_index.cache_nr -| -- ACC -+ the_index.cache_changed -| -- ACT -+ the_index.cache_tree -) - -// "the_repository" simple cases -@@ -@@ -( -- read_cache -+ repo_read_index -| -- read_cache_unmerged -+ repo_read_index_unmerged -| -- hold_locked_index -+ repo_hold_locked_index -) - ( -+ the_repository, - ...) - -// "the_repository" special-cases -@@ -@@ -( -- read_cache_preload -+ repo_read_index_preload -) - ( -+ the_repository, - ... -+ , 0 - ) - -// "the_index" simple cases -@@ -@@ -( -- is_cache_unborn -+ is_index_unborn -| -- unmerged_cache -+ unmerged_index -| -- rename_cache_entry_at -+ rename_index_entry_at -| -- chmod_cache_entry -+ chmod_index_entry -| -- cache_file_exists -+ index_file_exists -| -- cache_name_is_other -+ index_name_is_other -| -- unmerge_cache_entry_at -+ unmerge_index_entry_at -| -- add_to_cache -+ add_to_index -| -- add_file_to_cache -+ add_file_to_index -| -- add_cache_entry -+ add_index_entry -| -- remove_file_from_cache -+ remove_file_from_index -| -- ce_match_stat -+ ie_match_stat -| -- ce_modified -+ ie_modified -| -- resolve_undo_clear -+ resolve_undo_clear_index -| -- cache_name_pos -+ index_name_pos -| -- update_main_cache_tree -+ cache_tree_update -| -- discard_cache -+ discard_index -) - ( -+ &the_index, - ...) - -@@ -@@ -( -- refresh_and_write_cache -+ repo_refresh_and_write_index -) - ( -+ the_repository, - ... -+ , NULL, NULL, NULL - ) - -// "the_index" special-cases -@@ -@@ -( -- read_cache_from -+ read_index_from -) - ( -+ &the_index, - ... -+ , get_git_dir() - ) - -@@ -@@ -( -- refresh_cache -+ refresh_index -) - ( -+ &the_index, - ... -+ , NULL, NULL, NULL - ) - -@@ -expression O; -@@ -- write_cache_as_tree -+ write_index_as_tree - ( -- O, -+ O, &the_index, get_index_file(), - ... - ) diff --git a/contrib/coccinelle/meson.build b/contrib/coccinelle/meson.build deleted file mode 100644 index ae7f5b5460..0000000000 --- a/contrib/coccinelle/meson.build +++ /dev/null @@ -1,85 +0,0 @@ -coccinelle_opt = get_option('coccinelle').require( - fs.exists(meson.project_source_root() / '.git'), - error_message: 'coccinelle can only be run from a git checkout', -) - -spatch = find_program('spatch', required: coccinelle_opt) -if not spatch.found() - subdir_done() -endif - -rules = [ - 'array.cocci', - 'commit.cocci', - 'config_fn_ctx.pending.cocci', - 'equals-null.cocci', - 'flex_alloc.cocci', - 'free.cocci', - 'git_config_number.cocci', - 'hashmap.cocci', - 'index-compatibility.cocci', - 'object_id.cocci', - 'preincr.cocci', - 'qsort.cocci', - 'refs.cocci', - 'strbuf.cocci', - 'swap.cocci', - 'the_repository.cocci', - 'xcalloc.cocci', - 'xopen.cocci', - 'xstrdup_or_null.cocci', - 'xstrncmpz.cocci', -] - -concatenated_rules = custom_target( - command: [ - 'cat', '@INPUT@', - ], - input: rules, - output: 'rules.cocci', - capture: true, -) - -coccinelle_sources = [] -foreach source : run_command(git, '-C', meson.project_source_root(), 'ls-files', '--deduplicate', '*.c', third_party_excludes, check: true).stdout().split() - coccinelle_sources += source -endforeach - -coccinelle_headers = [] -foreach header : headers_to_check - coccinelle_headers += meson.project_source_root() / header -endforeach - -coccinelle_includes = [] -foreach path : ['compat', 'ewah', 'refs', 'sha256', 'trace2', 'win32', 'xdiff'] - coccinelle_includes += ['-I', meson.project_source_root() / path] -endforeach - -patches = [ ] -foreach source : coccinelle_sources - patches += custom_target( - command: [ - spatch, - '--all-includes', - '--sp-file', concatenated_rules, - '--patch', meson.project_source_root(), - coccinelle_includes, - '@INPUT@', - ], - input: meson.project_source_root() / source, - output: source.underscorify() + '.patch', - capture: true, - depend_files: coccinelle_headers, - ) -endforeach - -concatenated_patch = custom_target( - command: [ - 'cat', '@INPUT@', - ], - input: patches, - output: 'cocci.patch', - capture: true, -) - -alias_target('coccicheck', concatenated_patch) diff --git a/contrib/coccinelle/object_id.cocci b/contrib/coccinelle/object_id.cocci deleted file mode 100644 index 01f8d6935b..0000000000 --- a/contrib/coccinelle/object_id.cocci +++ /dev/null @@ -1,75 +0,0 @@ -@@ -struct object_id OID; -@@ -- hashclr(OID.hash) -+ oidclr(&OID) - -@@ -identifier f != oidclr; -struct object_id *OIDPTR; -@@ - f(...) {<... -- hashclr(OIDPTR->hash) -+ oidclr(OIDPTR) - ...>} - -@@ -struct object_id OID1, OID2; -@@ -- hashcmp(OID1.hash, OID2.hash) -+ oidcmp(&OID1, &OID2) - -@@ -identifier f != oidcmp; -struct object_id *OIDPTR1, OIDPTR2; -@@ - f(...) {<... -- hashcmp(OIDPTR1->hash, OIDPTR2->hash) -+ oidcmp(OIDPTR1, OIDPTR2) - ...>} - -@@ -struct object_id *OIDPTR; -struct object_id OID; -@@ -- hashcmp(OIDPTR->hash, OID.hash) -+ oidcmp(OIDPTR, &OID) - -@@ -struct object_id *OIDPTR; -struct object_id OID; -@@ -- hashcmp(OID.hash, OIDPTR->hash) -+ oidcmp(&OID, OIDPTR) - -@@ -struct object_id *OIDPTR1; -struct object_id *OIDPTR2; -@@ -- oidcmp(OIDPTR1, OIDPTR2) == 0 -+ oideq(OIDPTR1, OIDPTR2) - -@@ -identifier f != hasheq; -expression E1, E2; -@@ - f(...) {<... -- hashcmp(E1, E2) == 0 -+ hasheq(E1, E2) - ...>} - -@@ -struct object_id *OIDPTR1; -struct object_id *OIDPTR2; -@@ -- oidcmp(OIDPTR1, OIDPTR2) != 0 -+ !oideq(OIDPTR1, OIDPTR2) - -@@ -identifier f != hasheq; -expression E1, E2; -@@ - f(...) {<... -- hashcmp(E1, E2) != 0 -+ !hasheq(E1, E2) - ...>} diff --git a/contrib/coccinelle/preincr.cocci b/contrib/coccinelle/preincr.cocci deleted file mode 100644 index ae42cb0730..0000000000 --- a/contrib/coccinelle/preincr.cocci +++ /dev/null @@ -1,5 +0,0 @@ -@@ -identifier i; -@@ -- ++i > 1 -+ i++ diff --git a/contrib/coccinelle/qsort.cocci b/contrib/coccinelle/qsort.cocci deleted file mode 100644 index 22b93a9966..0000000000 --- a/contrib/coccinelle/qsort.cocci +++ /dev/null @@ -1,37 +0,0 @@ -@@ -expression base, nmemb, compar; -@@ -- qsort(base, nmemb, sizeof(*base), compar); -+ QSORT(base, nmemb, compar); - -@@ -expression base, nmemb, compar; -@@ -- qsort(base, nmemb, sizeof(base[0]), compar); -+ QSORT(base, nmemb, compar); - -@@ -type T; -T *base; -expression nmemb, compar; -@@ -- qsort(base, nmemb, sizeof(T), compar); -+ QSORT(base, nmemb, compar); - -@@ -expression base, nmemb, compar; -@@ -- if (nmemb) - QSORT(base, nmemb, compar); - -@@ -expression base, nmemb, compar; -@@ -- if (nmemb > 0) - QSORT(base, nmemb, compar); - -@@ -expression base, nmemb, compar; -@@ -- if (nmemb > 1) - QSORT(base, nmemb, compar); diff --git a/contrib/coccinelle/refs.cocci b/contrib/coccinelle/refs.cocci deleted file mode 100644 index 31d9cad8f3..0000000000 --- a/contrib/coccinelle/refs.cocci +++ /dev/null @@ -1,103 +0,0 @@ -// Migrate "refs.h" to not rely on `the_repository` implicitly anymore. -@@ -@@ -( -- resolve_ref_unsafe -+ refs_resolve_ref_unsafe -| -- resolve_refdup -+ refs_resolve_refdup -| -- read_ref_full -+ refs_read_ref_full -| -- read_ref -+ refs_read_ref -| -- ref_exists -+ refs_ref_exists -| -- head_ref -+ refs_head_ref -| -- for_each_ref -+ refs_for_each_ref -| -- for_each_ref_in -+ refs_for_each_ref_in -| -- for_each_fullref_in -+ refs_for_each_fullref_in -| -- for_each_tag_ref -+ refs_for_each_tag_ref -| -- for_each_branch_ref -+ refs_for_each_branch_ref -| -- for_each_remote_ref -+ refs_for_each_remote_ref -| -- for_each_glob_ref -+ refs_for_each_glob_ref -| -- for_each_glob_ref_in -+ refs_for_each_glob_ref_in -| -- head_ref_namespaced -+ refs_head_ref_namespaced -| -- for_each_namespaced_ref -+ refs_for_each_namespaced_ref -| -- for_each_rawref -+ refs_for_each_rawref -| -- safe_create_reflog -+ refs_create_reflog -| -- reflog_exists -+ refs_reflog_exists -| -- delete_ref -+ refs_delete_ref -| -- delete_refs -+ refs_delete_refs -| -- delete_reflog -+ refs_delete_reflog -| -- for_each_reflog_ent -+ refs_for_each_reflog_ent -| -- for_each_reflog_ent_reverse -+ refs_for_each_reflog_ent_reverse -| -- for_each_reflog -+ refs_for_each_reflog -| -- shorten_unambiguous_ref -+ refs_shorten_unambiguous_ref -| -- rename_ref -+ refs_rename_ref -| -- copy_existing_ref -+ refs_copy_existing_ref -| -- create_symref -+ refs_create_symref -| -- ref_transaction_begin -+ ref_store_transaction_begin -| -- update_ref -+ refs_update_ref -| -- reflog_expire -+ refs_reflog_expire -) - ( -+ get_main_ref_store(the_repository), - ...) diff --git a/contrib/coccinelle/spatchcache b/contrib/coccinelle/spatchcache deleted file mode 100755 index 29e9352d8a..0000000000 --- a/contrib/coccinelle/spatchcache +++ /dev/null @@ -1,304 +0,0 @@ -#!/bin/sh -# -# spatchcache: a poor-man's "ccache"-alike for "spatch" in git.git -# -# This caching command relies on the peculiarities of the Makefile -# driving "spatch" in git.git, in particular if we invoke: -# -# make -# # See "spatchCache.cacheWhenStderr" for why "--very-quiet" is -# # used -# make coccicheck SPATCH_FLAGS=--very-quiet -# -# We can with COMPUTE_HEADER_DEPENDENCIES (auto-detected as true with -# "gcc" and "clang") write e.g. a .depend/grep.o.d for grep.c, when we -# compile grep.o. -# -# The .depend/grep.o.d will have the full header dependency tree of -# grep.c, and we can thus cache the output of "spatch" by: -# -# 1. Hashing all of those files -# 2. Hashing our source file, and the *.cocci rule we're -# applying -# 3. Running spatch, if suggests no changes (by far the common -# case) we invoke "spatchCache.getCmd" and -# "spatchCache.setCmd" with a hash SHA-256 to ask "does this -# ID have no changes" or "say that ID had no changes> -# 4. If no "spatchCache.{set,get}Cmd" is specified we'll use -# "redis-cli" and maintain a SET called "spatch-cache". Set -# appropriate redis memory policies to keep it from growing -# out of control. -# -# This along with the general incremental "make" support for -# "contrib/coccinelle" makes it viable to (re-)run coccicheck -# e.g. when merging integration branches. -# -# Note that the "--very-quiet" flag is currently critical. The cache -# will refuse to cache anything that has output on STDERR (which might -# be errors from spatch), but see spatchCache.cacheWhenStderr below. -# -# The STDERR (and exit code) could in principle be cached (as with -# ccache), but then the simple structure in the Redis cache would need -# to change, so just supply "--very-quiet" for now. -# -# To use this, simply set SPATCH to -# contrib/coccinelle/spatchcache. Then optionally set: -# -# [spatchCache] -# # Optional: path to a custom spatch -# spatch = ~/g/coccicheck/spatch.opt -# -# As well as this trace config (debug implies trace): -# -# cacheWhenStderr = true -# trace = false -# debug = false -# -# The ".depend/grep.o.d" can also be customized, as a string that will -# be eval'd, it has access to a "$dirname" and "$basename": -# -# [spatchCache] -# dependFormat = "$dirname/.depend/${basename%.c}.o.d" -# -# Setting "trace" to "true" allows for seeing when we have a cache HIT -# or MISS. To debug whether the cache is working do that, and run e.g.: -# -# redis-cli FLUSHALL -# -# grep -hore HIT -e MISS -e SET -e NOCACHE -e CANTCACHE .build/contrib/coccinelle | sort | uniq -c -# 600 CANTCACHE -# 7365 MISS -# 7365 SET -# -# A subsequent "make cocciclean && make coccicheck" should then have -# all "HIT"'s and "CANTCACHE"'s. -# -# The "spatchCache.cacheWhenStderr" option is critical when using -# spatchCache.{trace,debug} to debug whether something is set in the -# cache, as we'll write to the spatch logs in .build/* we'd otherwise -# always emit a NOCACHE. -# -# Reading the config can make the command much slower, to work around -# this the config can be set in the environment, with environment -# variable name corresponding to the config key. "default" can be used -# to use whatever's the script default, e.g. setting -# spatchCache.cacheWhenStderr=true and deferring to the defaults for -# the rest is: -# -# export GIT_CONTRIB_SPATCHCACHE_DEBUG=default -# export GIT_CONTRIB_SPATCHCACHE_TRACE=default -# export GIT_CONTRIB_SPATCHCACHE_CACHEWHENSTDERR=true -# export GIT_CONTRIB_SPATCHCACHE_SPATCH=default -# export GIT_CONTRIB_SPATCHCACHE_DEPENDFORMAT=default -# export GIT_CONTRIB_SPATCHCACHE_SETCMD=default -# export GIT_CONTRIB_SPATCHCACHE_GETCMD=default - -set -e - -env_or_config () { - env="$1" - shift - if test "$env" = "default" - then - # Avoid expensive "git config" invocation - return - elif test -n "$env" - then - echo "$env" - else - git config $@ || : - fi -} - -## Our own configuration & options -debug=$(env_or_config "$GIT_CONTRIB_SPATCHCACHE_DEBUG" --bool "spatchCache.debug") -if test "$debug" != "true" -then - debug= -fi -if test -n "$debug" -then - set -x -fi - -trace=$(env_or_config "$GIT_CONTRIB_SPATCHCACHE_TRACE" --bool "spatchCache.trace") -if test "$trace" != "true" -then - trace= -fi -if test -n "$debug" -then - # debug implies trace - trace=true -fi - -cacheWhenStderr=$(env_or_config "$GIT_CONTRIB_SPATCHCACHE_CACHEWHENSTDERR" --bool "spatchCache.cacheWhenStderr") -if test "$cacheWhenStderr" != "true" -then - cacheWhenStderr= -fi - -trace_it () { - if test -z "$trace" - then - return - fi - echo "$@" >&2 -} - -spatch=$(env_or_config "$GIT_CONTRIB_SPATCHCACHE_SPATCH" --path "spatchCache.spatch") -if test -n "$spatch" -then - if test -n "$debug" - then - trace_it "custom spatchCache.spatch='$spatch'" - fi -else - spatch=spatch -fi - -dependFormat='$dirname/.depend/${basename%.c}.o.d' -dependFormatCfg=$(env_or_config "$GIT_CONTRIB_SPATCHCACHE_DEPENDFORMAT" "spatchCache.dependFormat") -if test -n "$dependFormatCfg" -then - dependFormat="$dependFormatCfg" -fi - -set=$(env_or_config "$GIT_CONTRIB_SPATCHCACHE_SETCMD" "spatchCache.setCmd") -get=$(env_or_config "$GIT_CONTRIB_SPATCHCACHE_GETCMD" "spatchCache.getCmd") - -## Parse spatch()-like command-line for caching info -arg_sp= -arg_file= -args="$@" -spatch_opts() { - while test $# != 0 - do - arg_file="$1" - case "$1" in - --sp-file) - arg_sp="$2" - ;; - esac - shift - done -} -spatch_opts "$@" -if ! test -f "$arg_file" -then - arg_file= -fi - -hash_for_cache() { - # Parameters that should affect the cache - echo "args=$args" - echo "config spatchCache.spatch=$spatch" - echo "config spatchCache.debug=$debug" - echo "config spatchCache.trace=$trace" - echo "config spatchCache.cacheWhenStderr=$cacheWhenStderr" - echo - - # Our target file and its dependencies - git hash-object "$1" "$2" $(grep -E -o '^[^:]+:$' "$3" | tr -d ':') -} - -# Sanity checks -if ! test -f "$arg_sp" && ! test -f "$arg_file" -then - echo $0: no idea how to cache "$@" >&2 - exit 128 -fi - -# Main logic -dirname=$(dirname "$arg_file") -basename=$(basename "$arg_file") -eval "dep=$dependFormat" - -if ! test -f "$dep" -then - trace_it "$0: CANTCACHE have no '$dep' for '$arg_file'!" - exec "$spatch" "$@" -fi - -if test -n "$debug" -then - trace_it "$0: The full cache input for '$arg_sp' '$arg_file' '$dep'" - hash_for_cache "$arg_sp" "$arg_file" "$dep" >&2 -fi -sum=$(hash_for_cache "$arg_sp" "$arg_file" "$dep" | git hash-object --stdin) - -trace_it "$0: processing '$arg_file' with '$arg_sp' rule, and got hash '$sum' for it + '$dep'" - -getret= -if test -z "$get" -then - if test $(redis-cli SISMEMBER spatch-cache "$sum") = 1 - then - getret=0 - else - getret=1 - fi -else - $set "$sum" - getret=$? -fi - -if test "$getret" = 0 -then - trace_it "$0: HIT for '$arg_file' with '$arg_sp'" - exit 0 -else - trace_it "$0: MISS: for '$arg_file' with '$arg_sp'" -fi - -out="$(mktemp)" -err="$(mktemp)" - -set +e -"$spatch" "$@" >"$out" 2>>"$err" -ret=$? -cat "$out" -cat "$err" >&2 -set -e - -nocache= -if test $ret != 0 -then - nocache="exited non-zero: $ret" -elif test -s "$out" -then - nocache="had patch output" -elif test -z "$cacheWhenStderr" && test -s "$err" -then - nocache="had stderr (use --very-quiet or spatchCache.cacheWhenStderr=true?)" -fi - -if test -n "$nocache" -then - trace_it "$0: NOCACHE ($nocache): for '$arg_file' with '$arg_sp'" - exit "$ret" -fi - -trace_it "$0: SET: for '$arg_file' with '$arg_sp'" - -setret= -if test -z "$set" -then - if test $(redis-cli SADD spatch-cache "$sum") = 1 - then - setret=0 - else - setret=1 - fi -else - "$set" "$sum" - setret=$? -fi - -if test "$setret" != 0 -then - echo "FAILED to set '$sum' in cache!" >&2 - exit 128 -fi - -exit "$ret" diff --git a/contrib/coccinelle/strbuf.cocci b/contrib/coccinelle/strbuf.cocci deleted file mode 100644 index 5f06105df6..0000000000 --- a/contrib/coccinelle/strbuf.cocci +++ /dev/null @@ -1,62 +0,0 @@ -@@ -expression E; -constant fmt !~ "%"; -@@ -- strbuf_addf -+ strbuf_addstr - (E, -( - fmt -| - _(fmt) -) - ); - -@@ -expression E; -struct strbuf SB; -format F =~ "^s$"; -@@ -- strbuf_addf(E, "%@F@", SB.buf); -+ strbuf_addbuf(E, &SB); - -@@ -expression E; -struct strbuf *SBP; -format F =~ "^s$"; -@@ -- strbuf_addf(E, "%@F@", SBP->buf); -+ strbuf_addbuf(E, SBP); - -@@ -expression E; -struct strbuf SB; -@@ -- strbuf_addstr(E, SB.buf); -+ strbuf_addbuf(E, &SB); - -@@ -expression E; -struct strbuf *SBP; -@@ -- strbuf_addstr(E, SBP->buf); -+ strbuf_addbuf(E, SBP); - -@@ -expression E1, E2; -format F =~ "^s$"; -@@ -- strbuf_addf(E1, "%@F@", E2); -+ strbuf_addstr(E1, E2); - -@@ -expression E1, E2, E3; -@@ -- strbuf_addstr(E1, find_unique_abbrev(E2, E3)); -+ strbuf_add_unique_abbrev(E1, E2, E3); - -@@ -expression E1, E2; -@@ -- strbuf_addstr(E1, real_path(E2)); -+ strbuf_add_real_path(E1, E2); diff --git a/contrib/coccinelle/swap.cocci b/contrib/coccinelle/swap.cocci deleted file mode 100644 index 522177afb6..0000000000 --- a/contrib/coccinelle/swap.cocci +++ /dev/null @@ -1,28 +0,0 @@ -@@ -type T; -identifier tmp; -T a, b; -@@ -- T tmp = a; -+ T tmp; -+ tmp = a; - a = b; - b = tmp; - -@ swap @ -type T; -T tmp, a, b; -@@ -- tmp = a; -- a = b; -- b = tmp; -+ SWAP(a, b); - -@ extends swap @ -identifier unused; -@@ - { - ... -- T unused; - ... when != unused - } diff --git a/contrib/coccinelle/tests/free.c b/contrib/coccinelle/tests/free.c deleted file mode 100644 index 96d4abc0c7..0000000000 --- a/contrib/coccinelle/tests/free.c +++ /dev/null @@ -1,11 +0,0 @@ -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 deleted file mode 100644 index f90fd9f48e..0000000000 --- a/contrib/coccinelle/tests/free.res +++ /dev/null @@ -1,9 +0,0 @@ -int use_FREE_AND_NULL(int *v) -{ - FREE_AND_NULL(*v); -} - -int need_no_if(int *v) -{ - free(v); -} diff --git a/contrib/coccinelle/the_repository.cocci b/contrib/coccinelle/the_repository.cocci deleted file mode 100644 index f1129f7985..0000000000 --- a/contrib/coccinelle/the_repository.cocci +++ /dev/null @@ -1,18 +0,0 @@ -// Fully migrated "the_repository" additions -@@ -@@ -( -// TODO: remove the rules below and the macros from tree.h after the -// next Git release. -- parse_tree -+ repo_parse_tree -| -- parse_tree_gently -+ repo_parse_tree_gently -| -- parse_tree_indirect -+ repo_parse_tree_indirect -) - ( -+ the_repository, - ...) diff --git a/contrib/coccinelle/xcalloc.cocci b/contrib/coccinelle/xcalloc.cocci deleted file mode 100644 index c291011607..0000000000 --- a/contrib/coccinelle/xcalloc.cocci +++ /dev/null @@ -1,10 +0,0 @@ -@@ -type T; -T *ptr; -expression n; -@@ - xcalloc( -+ n, - \( sizeof(T) \| sizeof(*ptr) \) -- , n - ) diff --git a/contrib/coccinelle/xopen.cocci b/contrib/coccinelle/xopen.cocci deleted file mode 100644 index b71db67019..0000000000 --- a/contrib/coccinelle/xopen.cocci +++ /dev/null @@ -1,19 +0,0 @@ -@@ -identifier fd; -identifier die_fn =~ "^(die|die_errno)$"; -@@ - int fd = -- open -+ xopen - (...); -- if ( \( fd < 0 \| fd == -1 \) ) { die_fn(...); } - -@@ -expression fd; -identifier die_fn =~ "^(die|die_errno)$"; -@@ - fd = -- open -+ xopen - (...); -- if ( \( fd < 0 \| fd == -1 \) ) { die_fn(...); } diff --git a/contrib/coccinelle/xstrdup_or_null.cocci b/contrib/coccinelle/xstrdup_or_null.cocci deleted file mode 100644 index 9c1d2939b6..0000000000 --- a/contrib/coccinelle/xstrdup_or_null.cocci +++ /dev/null @@ -1,5 +0,0 @@ -@@ -expression E; -@@ -- xstrdup(absolute_path(E)) -+ absolute_pathdup(E) diff --git a/contrib/coccinelle/xstrncmpz.cocci b/contrib/coccinelle/xstrncmpz.cocci deleted file mode 100644 index ccb39e2bc0..0000000000 --- a/contrib/coccinelle/xstrncmpz.cocci +++ /dev/null @@ -1,28 +0,0 @@ -@@ -expression S, T, L; -@@ -( -- strncmp(S, T, L) || S[L] -+ !!xstrncmpz(S, T, L) -| -- strncmp(S, T, L) || S[L] != '\0' -+ !!xstrncmpz(S, T, L) -| -- strncmp(S, T, L) || T[L] -+ !!xstrncmpz(T, S, L) -| -- strncmp(S, T, L) || T[L] != '\0' -+ !!xstrncmpz(T, S, L) -| -- !strncmp(S, T, L) && !S[L] -+ !xstrncmpz(S, T, L) -| -- !strncmp(S, T, L) && S[L] == '\0' -+ !xstrncmpz(S, T, L) -| -- !strncmp(S, T, L) && !T[L] -+ !xstrncmpz(T, S, L) -| -- !strncmp(S, T, L) && T[L] == '\0' -+ !xstrncmpz(T, S, L) -) diff --git a/contrib/meson.build b/contrib/meson.build index a88c5dfe09..569c23ee76 100644 --- a/contrib/meson.build +++ b/contrib/meson.build @@ -2,5 +2,4 @@ foreach feature : get_option('contrib') subdir(feature) endforeach -subdir('coccinelle') subdir('credential') diff --git a/tools/coccinelle/.gitignore b/tools/coccinelle/.gitignore new file mode 100644 index 0000000000..1d45c0a40c --- /dev/null +++ b/tools/coccinelle/.gitignore @@ -0,0 +1 @@ +*.patch diff --git a/tools/coccinelle/README b/tools/coccinelle/README new file mode 100644 index 0000000000..fd0a543cc2 --- /dev/null +++ b/tools/coccinelle/README @@ -0,0 +1,124 @@ += coccinelle + +This directory provides Coccinelle (http://coccinelle.lip6.fr/) semantic patches +that might be useful to developers. + +== Types of semantic patches + + * Using the semantic transformation to check for bad patterns in the code; + The target 'make coccicheck' is designed to check for these patterns and + it is expected that any resulting patch indicates a regression. + The patches resulting from 'make coccicheck' are small and infrequent, + so once they are found, they can be sent to the mailing list as per usual. + + Example for introducing new patterns: + 67947c34ae (convert "hashcmp() != 0" to "!hasheq()", 2018-08-28) + b84c783882 (fsck: s/++i > 1/i++/, 2018-10-24) + + Example of fixes using this approach: + 248f66ed8e (run-command: use strbuf_addstr() for adding a string to + a strbuf, 2018-03-25) + f919ffebed (Use MOVE_ARRAY, 2018-01-22) + + These types of semantic patches are usually part of testing, c.f. + 0860a7641b (travis-ci: fail if Coccinelle static analysis found something + to transform, 2018-07-23) + + * Using semantic transformations in large scale refactorings throughout + the code base. + + When applying the semantic patch into a real patch, sending it to the + mailing list in the usual way, such a patch would be expected to have a + lot of textual and semantic conflicts as such large scale refactorings + change function signatures that are used widely in the code base. + A textual conflict would arise if surrounding code near any call of such + function changes. A semantic conflict arises when other patch series in + flight introduce calls to such functions. + + So to aid these large scale refactorings, semantic patches can be used. + However we do not want to store them in the same place as the checks for + bad patterns, as then automated builds would fail. + That is why semantic patches 'tools/coccinelle/*.pending.cocci' + are ignored for checks, and can be applied using 'make coccicheck-pending'. + + This allows to expose plans of pending large scale refactorings without + impacting the bad pattern checks. + +== Git-specific tips & things to know about how we run "spatch": + + * The "make coccicheck" will piggy-back on + "COMPUTE_HEADER_DEPENDENCIES". If you've built a given object file + the "coccicheck" target will consider its depednency to decide if + it needs to re-run on the corresponding source file. + + This means that a "make coccicheck" will re-compile object files + before running. This might be unexpected, but speeds up the run in + the common case, as e.g. a change to "column.h" won't require all + coccinelle rules to be re-run against "grep.c" (or another file + that happens not to use "column.h"). + + To disable this behavior use the "SPATCH_USE_O_DEPENDENCIES=NoThanks" + flag. + + * To speed up our rules the "make coccicheck" target will by default + concatenate all of the *.cocci files here into an "ALL.cocci", and + apply it to each source file. + + This makes the run faster, as we don't need to run each rule + against each source file. See the Makefile for further discussion, + this behavior can be disabled with "SPATCH_CONCAT_COCCI=". + + But since they're concatenated any in the (e.g. "@ + my_name", v.s. anonymous "@@") needs to be unique across all our + *.cocci files. You should only need to name rules if other rules + depend on them (currently only one rule is named). + + * To speed up incremental runs even more use the "spatchcache" tool + in this directory as your "SPATCH". It aimns to be a "ccache" for + coccinelle, and piggy-backs on "COMPUTE_HEADER_DEPENDENCIES". + + It caches in Redis by default, see it source for a how-to. + + In one setup with a primed cache "make coccicheck" followed by a + "make clean && make" takes around 10s to run, but 2m30s with the + default of "SPATCH_CONCAT_COCCI=Y". + + With "SPATCH_CONCAT_COCCI=" the total runtime is around ~6m, sped + up to ~1m with "spatchcache". + + Most of the 10s (or ~1m) being spent on re-running "spatch" on + files we couldn't cache, as we didn't compile them (in contrib/* + and compat/* mostly). + + The absolute times will differ for you, but the relative speedup + from caching should be on that order. + +== Authoring and reviewing coccinelle changes + +* When a .cocci is made, both the Git changes and .cocci file should be + reviewed. When reviewing such a change, do your best to understand the .cocci + changes (e.g. by asking the author to explain the change) and be explicit + about your understanding of the changes. This helps us decide whether input + from coccinelle experts is needed or not. If you aren't sure of the cocci + changes, indicate what changes you actively endorse and leave an Acked-by + (instead of Reviewed-by). + +* Authors should consider that reviewers may not be coccinelle experts, thus the + the .cocci changes may not be self-evident. A plain text description of the + changes is strongly encouraged, especially when using more esoteric features + of the language. + +* .cocci rules should target only the problem it is trying to solve; "collateral + damage" is not allowed. Reviewers should look out and flag overly-broad rules. + +* Consider the cost-benefit ratio of .cocci changes. In particular, consider the + effect on the runtime of "make coccicheck", and how often your .cocci check + will catch something valuable. As a rule of thumb, rules that can bail early + if a file doesn't have a particular token will have a small impact on runtime, + and vice-versa. + +* .cocci files used for refactoring should be temporarily kept in-tree to aid + the refactoring of out-of-tree code (e.g. in-flight topics). Periodically + evaluate the cost-benefit ratio to determine when the file should be removed. + For example, consider how many out-of-tree users are left and how much this + slows down "make coccicheck". diff --git a/tools/coccinelle/array.cocci b/tools/coccinelle/array.cocci new file mode 100644 index 0000000000..e71baea00b --- /dev/null +++ b/tools/coccinelle/array.cocci @@ -0,0 +1,147 @@ +@@ +type T; +T *dst_ptr; +T *src_ptr; +expression n; +@@ +- memcpy(dst_ptr, src_ptr, (n) * \( sizeof(T) +- \| sizeof(*(dst_ptr)) +- \| sizeof(*(src_ptr)) +- \| sizeof(dst_ptr[...]) +- \| sizeof(src_ptr[...]) +- \) ) ++ COPY_ARRAY(dst_ptr, src_ptr, n) + +@@ +type T; +T *dst_ptr; +T[] src_arr; +expression n; +@@ +- memcpy(dst_ptr, src_arr, (n) * \( sizeof(T) +- \| sizeof(*(dst_ptr)) +- \| sizeof(*(src_arr)) +- \| sizeof(dst_ptr[...]) +- \| sizeof(src_arr[...]) +- \) ) ++ COPY_ARRAY(dst_ptr, src_arr, n) + +@@ +type T; +T[] dst_arr; +T *src_ptr; +expression n; +@@ +- memcpy(dst_arr, src_ptr, (n) * \( sizeof(T) +- \| sizeof(*(dst_arr)) +- \| sizeof(*(src_ptr)) +- \| sizeof(dst_arr[...]) +- \| sizeof(src_ptr[...]) +- \) ) ++ COPY_ARRAY(dst_arr, src_ptr, n) + +@@ +type T; +T[] dst_arr; +T[] src_arr; +expression n; +@@ +- memcpy(dst_arr, src_arr, (n) * \( sizeof(T) +- \| sizeof(*(dst_arr)) +- \| sizeof(*(src_arr)) +- \| sizeof(dst_arr[...]) +- \| sizeof(src_arr[...]) +- \) ) ++ COPY_ARRAY(dst_arr, src_arr, n) + +@@ +type T; +T *dst; +T *src; +expression n; +@@ +( +- memmove(dst, src, (n) * sizeof(*dst)); ++ MOVE_ARRAY(dst, src, n); +| +- memmove(dst, src, (n) * sizeof(*src)); ++ MOVE_ARRAY(dst, src, n); +| +- memmove(dst, src, (n) * sizeof(T)); ++ MOVE_ARRAY(dst, src, n); +) + +@@ +type T; +T *ptr; +expression n; +@@ +- ptr = xmalloc((n) * sizeof(*ptr)); ++ ALLOC_ARRAY(ptr, n); + +@@ +type T; +T *ptr; +expression n; +@@ +- ptr = xmalloc((n) * sizeof(T)); ++ ALLOC_ARRAY(ptr, n); + +@@ +type T; +T *ptr; +expression n != 1; +@@ +- ptr = xcalloc(n, \( sizeof(*ptr) \| sizeof(T) \) ) ++ CALLOC_ARRAY(ptr, n) + +@@ +expression dst, src, n; +@@ +-ALLOC_ARRAY(dst, n); +-COPY_ARRAY(dst, src, n); ++DUP_ARRAY(dst, src, n); + +@@ +type T; +T *ptr; +expression n; +@@ +- memset(ptr, \( 0 \| '\0' \), \( (n) \| n \) * \( sizeof(T) +- \| sizeof(ptr[...]) +- \| sizeof(*ptr) +- \) ) ++ MEMZERO_ARRAY(ptr, n) + +@@ +type T; +T *ptr; +expression n; +@@ +- memset(ptr, \( 0 \| '\0' \), \( sizeof(T) +- \| sizeof(ptr[...]) +- \| sizeof(*ptr) +- \) * \( (n) \| n \) ) ++ MEMZERO_ARRAY(ptr, n) + +@@ +type T; +T[] ptr; +expression n; +@@ +- memset(ptr, \( 0 \| '\0' \), \( (n) \| n \) * \( sizeof(T) +- \| sizeof(ptr[...]) +- \| sizeof(*ptr) +- \) ) ++ MEMZERO_ARRAY(ptr, n) + +@@ +type T; +T[] ptr; +expression n; +@@ +- memset(ptr, \( 0 \| '\0' \), \( sizeof(T) +- \| sizeof(ptr[...]) +- \| sizeof(*ptr) +- \) * \( (n) \| n \) ) ++ MEMZERO_ARRAY(ptr, n) diff --git a/tools/coccinelle/commit.cocci b/tools/coccinelle/commit.cocci new file mode 100644 index 0000000000..42725161e9 --- /dev/null +++ b/tools/coccinelle/commit.cocci @@ -0,0 +1,53 @@ +@@ +expression c; +@@ +- &c->maybe_tree->object.oid ++ get_commit_tree_oid(c) + +@@ +expression c; +@@ +- c->maybe_tree->object.oid.hash ++ get_commit_tree_oid(c)->hash + +@@ +identifier f !~ "^set_commit_tree$"; +expression c; +expression s; +@@ + f(...) {<... +- c->maybe_tree = s ++ set_commit_tree(c, s) + ...>} + +// These excluded functions must access c->maybe_tree directly. +// Note that if c->maybe_tree is written somewhere outside of these +// functions, then the recommended transformation will be bogus with +// repo_get_commit_tree() on the LHS. +@@ +identifier f != { repo_get_commit_tree, get_commit_tree_in_graph_one, + load_tree_for_commit, set_commit_tree, repo_parse_commit_no_graph }; +expression c; +@@ + f(...) {<... +- c->maybe_tree ++ repo_get_commit_tree(specify_the_right_repo_here, c) + ...>} + +@@ +struct commit *c; +expression E; +@@ +( +- c->generation = E; ++ commit_graph_data_at(c)->generation = E; +| +- c->graph_pos = E; ++ commit_graph_data_at(c)->graph_pos = E; +| +- c->generation ++ commit_graph_generation(c) +| +- c->graph_pos ++ commit_graph_position(c) +) diff --git a/tools/coccinelle/config_fn_ctx.pending.cocci b/tools/coccinelle/config_fn_ctx.pending.cocci new file mode 100644 index 0000000000..54f09fcbcd --- /dev/null +++ b/tools/coccinelle/config_fn_ctx.pending.cocci @@ -0,0 +1,144 @@ +@ get_fn @ +identifier fn, R; +@@ +( +( +git_config_from_file +| +git_config_from_file_with_options +| +git_config_from_mem +| +git_config_from_blob_oid +| +read_early_config +| +read_very_early_config +| +config_with_options +| +git_config +| +git_protected_config +| +config_from_gitmodules +) + (fn, ...) +| +repo_config(R, fn, ...) +) + +@ extends get_fn @ +identifier C1, C2, D; +@@ +int fn(const char *C1, const char *C2, ++ const struct config_context *ctx, + void *D); + +@ extends get_fn @ +@@ +int fn(const char *, const char *, ++ const struct config_context *, + void *); + +@ extends get_fn @ +// Don't change fns that look like callback fns but aren't +identifier fn2 != tar_filter_config && != git_diff_heuristic_config && + != git_default_submodule_config && != git_color_config && + != bundle_list_update && != parse_object_filter_config; +identifier C1, C2, D1, D2, S; +attribute name UNUSED; +@@ +int fn(const char *C1, const char *C2, ++ const struct config_context *ctx, + void *D1) { +<+... +( +fn2(C1, C2 ++ , ctx +, D2); +| +if(fn2(C1, C2 ++ , ctx +, D2) < 0) { ... } +| +return fn2(C1, C2 ++ , ctx +, D2); +| +S = fn2(C1, C2 ++ , ctx +, D2); +) +...+> + } + +@ extends get_fn@ +identifier C1, C2, D; +attribute name UNUSED; +@@ +int fn(const char *C1, const char *C2, ++ const struct config_context *ctx UNUSED, + void *D) {...} + + +// The previous rules don't catch all callbacks, especially if they're defined +// in a separate file from the repo_config() call. Fix these manually. +@@ +identifier C1, C2, D; +attribute name UNUSED; +@@ +int +( +git_ident_config +| +urlmatch_collect_fn +| +write_one_config +| +forbid_remote_url +| +credential_config_callback +) + (const char *C1, const char *C2, ++ const struct config_context *ctx UNUSED, + void *D) {...} + +@@ +identifier C1, C2, D, D2, S, fn2; +@@ +int +( +http_options +| +git_status_config +| +git_commit_config +| +git_default_core_config +| +grep_config +) + (const char *C1, const char *C2, ++ const struct config_context *ctx, + void *D) { +<+... +( +fn2(C1, C2 ++ , ctx +, D2); +| +if(fn2(C1, C2 ++ , ctx +, D2) < 0) { ... } +| +return fn2(C1, C2 ++ , ctx +, D2); +| +S = fn2(C1, C2 ++ , ctx +, D2); +) +...+> + } diff --git a/tools/coccinelle/equals-null.cocci b/tools/coccinelle/equals-null.cocci new file mode 100644 index 0000000000..92c7054013 --- /dev/null +++ b/tools/coccinelle/equals-null.cocci @@ -0,0 +1,30 @@ +/* SPDX-License-Identifier: LGPL-2.1-or-later */ +@@ +expression e; +statement s; +@@ +if ( +( +!e +| +- e == NULL ++ !e +) + ) + {...} +else s + +@@ +expression e; +statement s; +@@ +if ( +( +e +| +- e != NULL ++ e +) + ) + {...} +else s diff --git a/tools/coccinelle/flex_alloc.cocci b/tools/coccinelle/flex_alloc.cocci new file mode 100644 index 0000000000..e9f7f6d861 --- /dev/null +++ b/tools/coccinelle/flex_alloc.cocci @@ -0,0 +1,13 @@ +@@ +expression str; +identifier x, flexname; +@@ +- FLEX_ALLOC_MEM(x, flexname, str, strlen(str)); ++ FLEX_ALLOC_STR(x, flexname, str); + +@@ +expression str; +identifier x, ptrname; +@@ +- FLEXPTR_ALLOC_MEM(x, ptrname, str, strlen(str)); ++ FLEXPTR_ALLOC_STR(x, ptrname, str); diff --git a/tools/coccinelle/free.cocci b/tools/coccinelle/free.cocci new file mode 100644 index 0000000000..03799e1908 --- /dev/null +++ b/tools/coccinelle/free.cocci @@ -0,0 +1,45 @@ +@@ +expression E; +@@ +- if (E) +( + free(E); +| + commit_list_free(E); +) + +@@ +expression E; +@@ +- if (!E) +( + free(E); +| + commit_list_free(E); +) + +@@ +expression E; +@@ +- free(E); ++ FREE_AND_NULL(E); +- E = NULL; + +@@ +expression E; +@@ +- if (E) +- { + commit_list_free(E); + E = NULL; +- } + +@@ +expression E; +statement S; +@@ +- if (E) { ++ if (E) + S + commit_list_free(E); +- } diff --git a/tools/coccinelle/git_config_number.cocci b/tools/coccinelle/git_config_number.cocci new file mode 100644 index 0000000000..7b57dceefe --- /dev/null +++ b/tools/coccinelle/git_config_number.cocci @@ -0,0 +1,27 @@ +@@ +identifier C1, C2, C3; +@@ +( +( +git_config_int +| +git_config_int64 +| +git_config_ulong +| +git_config_ssize_t +) + (C1, C2 ++ , ctx->kvi + ) +| +( +git_configset_get_value +| +git_config_bool_or_int +) + (C1, C2 ++ , ctx->kvi + , C3 + ) +) diff --git a/tools/coccinelle/hashmap.cocci b/tools/coccinelle/hashmap.cocci new file mode 100644 index 0000000000..c5dbb4557b --- /dev/null +++ b/tools/coccinelle/hashmap.cocci @@ -0,0 +1,16 @@ +@@ +expression E; +struct hashmap_entry HME; +@@ +- HME.hash = E; ++ hashmap_entry_init(&HME, E); + +@@ +identifier f !~ "^hashmap_entry_init$"; +expression E; +struct hashmap_entry *HMEP; +@@ + f(...) {<... +- HMEP->hash = E; ++ hashmap_entry_init(HMEP, E); + ...>} diff --git a/tools/coccinelle/index-compatibility.cocci b/tools/coccinelle/index-compatibility.cocci new file mode 100644 index 0000000000..31e36cf3c4 --- /dev/null +++ b/tools/coccinelle/index-compatibility.cocci @@ -0,0 +1,157 @@ +// the_index.* variables +@@ +identifier AC = active_cache; +identifier AN = active_nr; +identifier ACC = active_cache_changed; +identifier ACT = active_cache_tree; +@@ +( +- AC ++ the_index.cache +| +- AN ++ the_index.cache_nr +| +- ACC ++ the_index.cache_changed +| +- ACT ++ the_index.cache_tree +) + +// "the_repository" simple cases +@@ +@@ +( +- read_cache ++ repo_read_index +| +- read_cache_unmerged ++ repo_read_index_unmerged +| +- hold_locked_index ++ repo_hold_locked_index +) + ( ++ the_repository, + ...) + +// "the_repository" special-cases +@@ +@@ +( +- read_cache_preload ++ repo_read_index_preload +) + ( ++ the_repository, + ... ++ , 0 + ) + +// "the_index" simple cases +@@ +@@ +( +- is_cache_unborn ++ is_index_unborn +| +- unmerged_cache ++ unmerged_index +| +- rename_cache_entry_at ++ rename_index_entry_at +| +- chmod_cache_entry ++ chmod_index_entry +| +- cache_file_exists ++ index_file_exists +| +- cache_name_is_other ++ index_name_is_other +| +- unmerge_cache_entry_at ++ unmerge_index_entry_at +| +- add_to_cache ++ add_to_index +| +- add_file_to_cache ++ add_file_to_index +| +- add_cache_entry ++ add_index_entry +| +- remove_file_from_cache ++ remove_file_from_index +| +- ce_match_stat ++ ie_match_stat +| +- ce_modified ++ ie_modified +| +- resolve_undo_clear ++ resolve_undo_clear_index +| +- cache_name_pos ++ index_name_pos +| +- update_main_cache_tree ++ cache_tree_update +| +- discard_cache ++ discard_index +) + ( ++ &the_index, + ...) + +@@ +@@ +( +- refresh_and_write_cache ++ repo_refresh_and_write_index +) + ( ++ the_repository, + ... ++ , NULL, NULL, NULL + ) + +// "the_index" special-cases +@@ +@@ +( +- read_cache_from ++ read_index_from +) + ( ++ &the_index, + ... ++ , get_git_dir() + ) + +@@ +@@ +( +- refresh_cache ++ refresh_index +) + ( ++ &the_index, + ... ++ , NULL, NULL, NULL + ) + +@@ +expression O; +@@ +- write_cache_as_tree ++ write_index_as_tree + ( +- O, ++ O, &the_index, get_index_file(), + ... + ) diff --git a/tools/coccinelle/meson.build b/tools/coccinelle/meson.build new file mode 100644 index 0000000000..ae7f5b5460 --- /dev/null +++ b/tools/coccinelle/meson.build @@ -0,0 +1,85 @@ +coccinelle_opt = get_option('coccinelle').require( + fs.exists(meson.project_source_root() / '.git'), + error_message: 'coccinelle can only be run from a git checkout', +) + +spatch = find_program('spatch', required: coccinelle_opt) +if not spatch.found() + subdir_done() +endif + +rules = [ + 'array.cocci', + 'commit.cocci', + 'config_fn_ctx.pending.cocci', + 'equals-null.cocci', + 'flex_alloc.cocci', + 'free.cocci', + 'git_config_number.cocci', + 'hashmap.cocci', + 'index-compatibility.cocci', + 'object_id.cocci', + 'preincr.cocci', + 'qsort.cocci', + 'refs.cocci', + 'strbuf.cocci', + 'swap.cocci', + 'the_repository.cocci', + 'xcalloc.cocci', + 'xopen.cocci', + 'xstrdup_or_null.cocci', + 'xstrncmpz.cocci', +] + +concatenated_rules = custom_target( + command: [ + 'cat', '@INPUT@', + ], + input: rules, + output: 'rules.cocci', + capture: true, +) + +coccinelle_sources = [] +foreach source : run_command(git, '-C', meson.project_source_root(), 'ls-files', '--deduplicate', '*.c', third_party_excludes, check: true).stdout().split() + coccinelle_sources += source +endforeach + +coccinelle_headers = [] +foreach header : headers_to_check + coccinelle_headers += meson.project_source_root() / header +endforeach + +coccinelle_includes = [] +foreach path : ['compat', 'ewah', 'refs', 'sha256', 'trace2', 'win32', 'xdiff'] + coccinelle_includes += ['-I', meson.project_source_root() / path] +endforeach + +patches = [ ] +foreach source : coccinelle_sources + patches += custom_target( + command: [ + spatch, + '--all-includes', + '--sp-file', concatenated_rules, + '--patch', meson.project_source_root(), + coccinelle_includes, + '@INPUT@', + ], + input: meson.project_source_root() / source, + output: source.underscorify() + '.patch', + capture: true, + depend_files: coccinelle_headers, + ) +endforeach + +concatenated_patch = custom_target( + command: [ + 'cat', '@INPUT@', + ], + input: patches, + output: 'cocci.patch', + capture: true, +) + +alias_target('coccicheck', concatenated_patch) diff --git a/tools/coccinelle/object_id.cocci b/tools/coccinelle/object_id.cocci new file mode 100644 index 0000000000..01f8d6935b --- /dev/null +++ b/tools/coccinelle/object_id.cocci @@ -0,0 +1,75 @@ +@@ +struct object_id OID; +@@ +- hashclr(OID.hash) ++ oidclr(&OID) + +@@ +identifier f != oidclr; +struct object_id *OIDPTR; +@@ + f(...) {<... +- hashclr(OIDPTR->hash) ++ oidclr(OIDPTR) + ...>} + +@@ +struct object_id OID1, OID2; +@@ +- hashcmp(OID1.hash, OID2.hash) ++ oidcmp(&OID1, &OID2) + +@@ +identifier f != oidcmp; +struct object_id *OIDPTR1, OIDPTR2; +@@ + f(...) {<... +- hashcmp(OIDPTR1->hash, OIDPTR2->hash) ++ oidcmp(OIDPTR1, OIDPTR2) + ...>} + +@@ +struct object_id *OIDPTR; +struct object_id OID; +@@ +- hashcmp(OIDPTR->hash, OID.hash) ++ oidcmp(OIDPTR, &OID) + +@@ +struct object_id *OIDPTR; +struct object_id OID; +@@ +- hashcmp(OID.hash, OIDPTR->hash) ++ oidcmp(&OID, OIDPTR) + +@@ +struct object_id *OIDPTR1; +struct object_id *OIDPTR2; +@@ +- oidcmp(OIDPTR1, OIDPTR2) == 0 ++ oideq(OIDPTR1, OIDPTR2) + +@@ +identifier f != hasheq; +expression E1, E2; +@@ + f(...) {<... +- hashcmp(E1, E2) == 0 ++ hasheq(E1, E2) + ...>} + +@@ +struct object_id *OIDPTR1; +struct object_id *OIDPTR2; +@@ +- oidcmp(OIDPTR1, OIDPTR2) != 0 ++ !oideq(OIDPTR1, OIDPTR2) + +@@ +identifier f != hasheq; +expression E1, E2; +@@ + f(...) {<... +- hashcmp(E1, E2) != 0 ++ !hasheq(E1, E2) + ...>} diff --git a/tools/coccinelle/preincr.cocci b/tools/coccinelle/preincr.cocci new file mode 100644 index 0000000000..ae42cb0730 --- /dev/null +++ b/tools/coccinelle/preincr.cocci @@ -0,0 +1,5 @@ +@@ +identifier i; +@@ +- ++i > 1 ++ i++ diff --git a/tools/coccinelle/qsort.cocci b/tools/coccinelle/qsort.cocci new file mode 100644 index 0000000000..22b93a9966 --- /dev/null +++ b/tools/coccinelle/qsort.cocci @@ -0,0 +1,37 @@ +@@ +expression base, nmemb, compar; +@@ +- qsort(base, nmemb, sizeof(*base), compar); ++ QSORT(base, nmemb, compar); + +@@ +expression base, nmemb, compar; +@@ +- qsort(base, nmemb, sizeof(base[0]), compar); ++ QSORT(base, nmemb, compar); + +@@ +type T; +T *base; +expression nmemb, compar; +@@ +- qsort(base, nmemb, sizeof(T), compar); ++ QSORT(base, nmemb, compar); + +@@ +expression base, nmemb, compar; +@@ +- if (nmemb) + QSORT(base, nmemb, compar); + +@@ +expression base, nmemb, compar; +@@ +- if (nmemb > 0) + QSORT(base, nmemb, compar); + +@@ +expression base, nmemb, compar; +@@ +- if (nmemb > 1) + QSORT(base, nmemb, compar); diff --git a/tools/coccinelle/refs.cocci b/tools/coccinelle/refs.cocci new file mode 100644 index 0000000000..31d9cad8f3 --- /dev/null +++ b/tools/coccinelle/refs.cocci @@ -0,0 +1,103 @@ +// Migrate "refs.h" to not rely on `the_repository` implicitly anymore. +@@ +@@ +( +- resolve_ref_unsafe ++ refs_resolve_ref_unsafe +| +- resolve_refdup ++ refs_resolve_refdup +| +- read_ref_full ++ refs_read_ref_full +| +- read_ref ++ refs_read_ref +| +- ref_exists ++ refs_ref_exists +| +- head_ref ++ refs_head_ref +| +- for_each_ref ++ refs_for_each_ref +| +- for_each_ref_in ++ refs_for_each_ref_in +| +- for_each_fullref_in ++ refs_for_each_fullref_in +| +- for_each_tag_ref ++ refs_for_each_tag_ref +| +- for_each_branch_ref ++ refs_for_each_branch_ref +| +- for_each_remote_ref ++ refs_for_each_remote_ref +| +- for_each_glob_ref ++ refs_for_each_glob_ref +| +- for_each_glob_ref_in ++ refs_for_each_glob_ref_in +| +- head_ref_namespaced ++ refs_head_ref_namespaced +| +- for_each_namespaced_ref ++ refs_for_each_namespaced_ref +| +- for_each_rawref ++ refs_for_each_rawref +| +- safe_create_reflog ++ refs_create_reflog +| +- reflog_exists ++ refs_reflog_exists +| +- delete_ref ++ refs_delete_ref +| +- delete_refs ++ refs_delete_refs +| +- delete_reflog ++ refs_delete_reflog +| +- for_each_reflog_ent ++ refs_for_each_reflog_ent +| +- for_each_reflog_ent_reverse ++ refs_for_each_reflog_ent_reverse +| +- for_each_reflog ++ refs_for_each_reflog +| +- shorten_unambiguous_ref ++ refs_shorten_unambiguous_ref +| +- rename_ref ++ refs_rename_ref +| +- copy_existing_ref ++ refs_copy_existing_ref +| +- create_symref ++ refs_create_symref +| +- ref_transaction_begin ++ ref_store_transaction_begin +| +- update_ref ++ refs_update_ref +| +- reflog_expire ++ refs_reflog_expire +) + ( ++ get_main_ref_store(the_repository), + ...) diff --git a/tools/coccinelle/spatchcache b/tools/coccinelle/spatchcache new file mode 100755 index 0000000000..efbcbc3827 --- /dev/null +++ b/tools/coccinelle/spatchcache @@ -0,0 +1,304 @@ +#!/bin/sh +# +# spatchcache: a poor-man's "ccache"-alike for "spatch" in git.git +# +# This caching command relies on the peculiarities of the Makefile +# driving "spatch" in git.git, in particular if we invoke: +# +# make +# # See "spatchCache.cacheWhenStderr" for why "--very-quiet" is +# # used +# make coccicheck SPATCH_FLAGS=--very-quiet +# +# We can with COMPUTE_HEADER_DEPENDENCIES (auto-detected as true with +# "gcc" and "clang") write e.g. a .depend/grep.o.d for grep.c, when we +# compile grep.o. +# +# The .depend/grep.o.d will have the full header dependency tree of +# grep.c, and we can thus cache the output of "spatch" by: +# +# 1. Hashing all of those files +# 2. Hashing our source file, and the *.cocci rule we're +# applying +# 3. Running spatch, if suggests no changes (by far the common +# case) we invoke "spatchCache.getCmd" and +# "spatchCache.setCmd" with a hash SHA-256 to ask "does this +# ID have no changes" or "say that ID had no changes> +# 4. If no "spatchCache.{set,get}Cmd" is specified we'll use +# "redis-cli" and maintain a SET called "spatch-cache". Set +# appropriate redis memory policies to keep it from growing +# out of control. +# +# This along with the general incremental "make" support for +# "tools/coccinelle" makes it viable to (re-)run coccicheck +# e.g. when merging integration branches. +# +# Note that the "--very-quiet" flag is currently critical. The cache +# will refuse to cache anything that has output on STDERR (which might +# be errors from spatch), but see spatchCache.cacheWhenStderr below. +# +# The STDERR (and exit code) could in principle be cached (as with +# ccache), but then the simple structure in the Redis cache would need +# to change, so just supply "--very-quiet" for now. +# +# To use this, simply set SPATCH to +# tools/coccinelle/spatchcache. Then optionally set: +# +# [spatchCache] +# # Optional: path to a custom spatch +# spatch = ~/g/coccicheck/spatch.opt +# +# As well as this trace config (debug implies trace): +# +# cacheWhenStderr = true +# trace = false +# debug = false +# +# The ".depend/grep.o.d" can also be customized, as a string that will +# be eval'd, it has access to a "$dirname" and "$basename": +# +# [spatchCache] +# dependFormat = "$dirname/.depend/${basename%.c}.o.d" +# +# Setting "trace" to "true" allows for seeing when we have a cache HIT +# or MISS. To debug whether the cache is working do that, and run e.g.: +# +# redis-cli FLUSHALL +# +# grep -hore HIT -e MISS -e SET -e NOCACHE -e CANTCACHE .build/tools/coccinelle | sort | uniq -c +# 600 CANTCACHE +# 7365 MISS +# 7365 SET +# +# A subsequent "make cocciclean && make coccicheck" should then have +# all "HIT"'s and "CANTCACHE"'s. +# +# The "spatchCache.cacheWhenStderr" option is critical when using +# spatchCache.{trace,debug} to debug whether something is set in the +# cache, as we'll write to the spatch logs in .build/* we'd otherwise +# always emit a NOCACHE. +# +# Reading the config can make the command much slower, to work around +# this the config can be set in the environment, with environment +# variable name corresponding to the config key. "default" can be used +# to use whatever's the script default, e.g. setting +# spatchCache.cacheWhenStderr=true and deferring to the defaults for +# the rest is: +# +# export GIT_CONTRIB_SPATCHCACHE_DEBUG=default +# export GIT_CONTRIB_SPATCHCACHE_TRACE=default +# export GIT_CONTRIB_SPATCHCACHE_CACHEWHENSTDERR=true +# export GIT_CONTRIB_SPATCHCACHE_SPATCH=default +# export GIT_CONTRIB_SPATCHCACHE_DEPENDFORMAT=default +# export GIT_CONTRIB_SPATCHCACHE_SETCMD=default +# export GIT_CONTRIB_SPATCHCACHE_GETCMD=default + +set -e + +env_or_config () { + env="$1" + shift + if test "$env" = "default" + then + # Avoid expensive "git config" invocation + return + elif test -n "$env" + then + echo "$env" + else + git config $@ || : + fi +} + +## Our own configuration & options +debug=$(env_or_config "$GIT_CONTRIB_SPATCHCACHE_DEBUG" --bool "spatchCache.debug") +if test "$debug" != "true" +then + debug= +fi +if test -n "$debug" +then + set -x +fi + +trace=$(env_or_config "$GIT_CONTRIB_SPATCHCACHE_TRACE" --bool "spatchCache.trace") +if test "$trace" != "true" +then + trace= +fi +if test -n "$debug" +then + # debug implies trace + trace=true +fi + +cacheWhenStderr=$(env_or_config "$GIT_CONTRIB_SPATCHCACHE_CACHEWHENSTDERR" --bool "spatchCache.cacheWhenStderr") +if test "$cacheWhenStderr" != "true" +then + cacheWhenStderr= +fi + +trace_it () { + if test -z "$trace" + then + return + fi + echo "$@" >&2 +} + +spatch=$(env_or_config "$GIT_CONTRIB_SPATCHCACHE_SPATCH" --path "spatchCache.spatch") +if test -n "$spatch" +then + if test -n "$debug" + then + trace_it "custom spatchCache.spatch='$spatch'" + fi +else + spatch=spatch +fi + +dependFormat='$dirname/.depend/${basename%.c}.o.d' +dependFormatCfg=$(env_or_config "$GIT_CONTRIB_SPATCHCACHE_DEPENDFORMAT" "spatchCache.dependFormat") +if test -n "$dependFormatCfg" +then + dependFormat="$dependFormatCfg" +fi + +set=$(env_or_config "$GIT_CONTRIB_SPATCHCACHE_SETCMD" "spatchCache.setCmd") +get=$(env_or_config "$GIT_CONTRIB_SPATCHCACHE_GETCMD" "spatchCache.getCmd") + +## Parse spatch()-like command-line for caching info +arg_sp= +arg_file= +args="$@" +spatch_opts() { + while test $# != 0 + do + arg_file="$1" + case "$1" in + --sp-file) + arg_sp="$2" + ;; + esac + shift + done +} +spatch_opts "$@" +if ! test -f "$arg_file" +then + arg_file= +fi + +hash_for_cache() { + # Parameters that should affect the cache + echo "args=$args" + echo "config spatchCache.spatch=$spatch" + echo "config spatchCache.debug=$debug" + echo "config spatchCache.trace=$trace" + echo "config spatchCache.cacheWhenStderr=$cacheWhenStderr" + echo + + # Our target file and its dependencies + git hash-object "$1" "$2" $(grep -E -o '^[^:]+:$' "$3" | tr -d ':') +} + +# Sanity checks +if ! test -f "$arg_sp" && ! test -f "$arg_file" +then + echo $0: no idea how to cache "$@" >&2 + exit 128 +fi + +# Main logic +dirname=$(dirname "$arg_file") +basename=$(basename "$arg_file") +eval "dep=$dependFormat" + +if ! test -f "$dep" +then + trace_it "$0: CANTCACHE have no '$dep' for '$arg_file'!" + exec "$spatch" "$@" +fi + +if test -n "$debug" +then + trace_it "$0: The full cache input for '$arg_sp' '$arg_file' '$dep'" + hash_for_cache "$arg_sp" "$arg_file" "$dep" >&2 +fi +sum=$(hash_for_cache "$arg_sp" "$arg_file" "$dep" | git hash-object --stdin) + +trace_it "$0: processing '$arg_file' with '$arg_sp' rule, and got hash '$sum' for it + '$dep'" + +getret= +if test -z "$get" +then + if test $(redis-cli SISMEMBER spatch-cache "$sum") = 1 + then + getret=0 + else + getret=1 + fi +else + $set "$sum" + getret=$? +fi + +if test "$getret" = 0 +then + trace_it "$0: HIT for '$arg_file' with '$arg_sp'" + exit 0 +else + trace_it "$0: MISS: for '$arg_file' with '$arg_sp'" +fi + +out="$(mktemp)" +err="$(mktemp)" + +set +e +"$spatch" "$@" >"$out" 2>>"$err" +ret=$? +cat "$out" +cat "$err" >&2 +set -e + +nocache= +if test $ret != 0 +then + nocache="exited non-zero: $ret" +elif test -s "$out" +then + nocache="had patch output" +elif test -z "$cacheWhenStderr" && test -s "$err" +then + nocache="had stderr (use --very-quiet or spatchCache.cacheWhenStderr=true?)" +fi + +if test -n "$nocache" +then + trace_it "$0: NOCACHE ($nocache): for '$arg_file' with '$arg_sp'" + exit "$ret" +fi + +trace_it "$0: SET: for '$arg_file' with '$arg_sp'" + +setret= +if test -z "$set" +then + if test $(redis-cli SADD spatch-cache "$sum") = 1 + then + setret=0 + else + setret=1 + fi +else + "$set" "$sum" + setret=$? +fi + +if test "$setret" != 0 +then + echo "FAILED to set '$sum' in cache!" >&2 + exit 128 +fi + +exit "$ret" diff --git a/tools/coccinelle/strbuf.cocci b/tools/coccinelle/strbuf.cocci new file mode 100644 index 0000000000..5f06105df6 --- /dev/null +++ b/tools/coccinelle/strbuf.cocci @@ -0,0 +1,62 @@ +@@ +expression E; +constant fmt !~ "%"; +@@ +- strbuf_addf ++ strbuf_addstr + (E, +( + fmt +| + _(fmt) +) + ); + +@@ +expression E; +struct strbuf SB; +format F =~ "^s$"; +@@ +- strbuf_addf(E, "%@F@", SB.buf); ++ strbuf_addbuf(E, &SB); + +@@ +expression E; +struct strbuf *SBP; +format F =~ "^s$"; +@@ +- strbuf_addf(E, "%@F@", SBP->buf); ++ strbuf_addbuf(E, SBP); + +@@ +expression E; +struct strbuf SB; +@@ +- strbuf_addstr(E, SB.buf); ++ strbuf_addbuf(E, &SB); + +@@ +expression E; +struct strbuf *SBP; +@@ +- strbuf_addstr(E, SBP->buf); ++ strbuf_addbuf(E, SBP); + +@@ +expression E1, E2; +format F =~ "^s$"; +@@ +- strbuf_addf(E1, "%@F@", E2); ++ strbuf_addstr(E1, E2); + +@@ +expression E1, E2, E3; +@@ +- strbuf_addstr(E1, find_unique_abbrev(E2, E3)); ++ strbuf_add_unique_abbrev(E1, E2, E3); + +@@ +expression E1, E2; +@@ +- strbuf_addstr(E1, real_path(E2)); ++ strbuf_add_real_path(E1, E2); diff --git a/tools/coccinelle/swap.cocci b/tools/coccinelle/swap.cocci new file mode 100644 index 0000000000..522177afb6 --- /dev/null +++ b/tools/coccinelle/swap.cocci @@ -0,0 +1,28 @@ +@@ +type T; +identifier tmp; +T a, b; +@@ +- T tmp = a; ++ T tmp; ++ tmp = a; + a = b; + b = tmp; + +@ swap @ +type T; +T tmp, a, b; +@@ +- tmp = a; +- a = b; +- b = tmp; ++ SWAP(a, b); + +@ extends swap @ +identifier unused; +@@ + { + ... +- T unused; + ... when != unused + } diff --git a/tools/coccinelle/tests/free.c b/tools/coccinelle/tests/free.c new file mode 100644 index 0000000000..96d4abc0c7 --- /dev/null +++ b/tools/coccinelle/tests/free.c @@ -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/tools/coccinelle/tests/free.res b/tools/coccinelle/tests/free.res new file mode 100644 index 0000000000..f90fd9f48e --- /dev/null +++ b/tools/coccinelle/tests/free.res @@ -0,0 +1,9 @@ +int use_FREE_AND_NULL(int *v) +{ + FREE_AND_NULL(*v); +} + +int need_no_if(int *v) +{ + free(v); +} diff --git a/tools/coccinelle/the_repository.cocci b/tools/coccinelle/the_repository.cocci new file mode 100644 index 0000000000..f1129f7985 --- /dev/null +++ b/tools/coccinelle/the_repository.cocci @@ -0,0 +1,18 @@ +// Fully migrated "the_repository" additions +@@ +@@ +( +// TODO: remove the rules below and the macros from tree.h after the +// next Git release. +- parse_tree ++ repo_parse_tree +| +- parse_tree_gently ++ repo_parse_tree_gently +| +- parse_tree_indirect ++ repo_parse_tree_indirect +) + ( ++ the_repository, + ...) diff --git a/tools/coccinelle/xcalloc.cocci b/tools/coccinelle/xcalloc.cocci new file mode 100644 index 0000000000..c291011607 --- /dev/null +++ b/tools/coccinelle/xcalloc.cocci @@ -0,0 +1,10 @@ +@@ +type T; +T *ptr; +expression n; +@@ + xcalloc( ++ n, + \( sizeof(T) \| sizeof(*ptr) \) +- , n + ) diff --git a/tools/coccinelle/xopen.cocci b/tools/coccinelle/xopen.cocci new file mode 100644 index 0000000000..b71db67019 --- /dev/null +++ b/tools/coccinelle/xopen.cocci @@ -0,0 +1,19 @@ +@@ +identifier fd; +identifier die_fn =~ "^(die|die_errno)$"; +@@ + int fd = +- open ++ xopen + (...); +- if ( \( fd < 0 \| fd == -1 \) ) { die_fn(...); } + +@@ +expression fd; +identifier die_fn =~ "^(die|die_errno)$"; +@@ + fd = +- open ++ xopen + (...); +- if ( \( fd < 0 \| fd == -1 \) ) { die_fn(...); } diff --git a/tools/coccinelle/xstrdup_or_null.cocci b/tools/coccinelle/xstrdup_or_null.cocci new file mode 100644 index 0000000000..9c1d2939b6 --- /dev/null +++ b/tools/coccinelle/xstrdup_or_null.cocci @@ -0,0 +1,5 @@ +@@ +expression E; +@@ +- xstrdup(absolute_path(E)) ++ absolute_pathdup(E) diff --git a/tools/coccinelle/xstrncmpz.cocci b/tools/coccinelle/xstrncmpz.cocci new file mode 100644 index 0000000000..ccb39e2bc0 --- /dev/null +++ b/tools/coccinelle/xstrncmpz.cocci @@ -0,0 +1,28 @@ +@@ +expression S, T, L; +@@ +( +- strncmp(S, T, L) || S[L] ++ !!xstrncmpz(S, T, L) +| +- strncmp(S, T, L) || S[L] != '\0' ++ !!xstrncmpz(S, T, L) +| +- strncmp(S, T, L) || T[L] ++ !!xstrncmpz(T, S, L) +| +- strncmp(S, T, L) || T[L] != '\0' ++ !!xstrncmpz(T, S, L) +| +- !strncmp(S, T, L) && !S[L] ++ !xstrncmpz(S, T, L) +| +- !strncmp(S, T, L) && S[L] == '\0' ++ !xstrncmpz(S, T, L) +| +- !strncmp(S, T, L) && !T[L] ++ !xstrncmpz(T, S, L) +| +- !strncmp(S, T, L) && T[L] == '\0' ++ !xstrncmpz(T, S, L) +) diff --git a/tools/meson.build b/tools/meson.build index e69de29bb2..f731f74312 100644 --- a/tools/meson.build +++ b/tools/meson.build @@ -0,0 +1 @@ +subdir('coccinelle') -- cgit v1.3-5-g9baa