From 76d156766fc9364ed2ce1b07f0867cbbdc5932f4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Thu, 15 Sep 2016 20:30:52 +0200 Subject: contrib/coccinelle: fix semantic patch for oid_to_hex_r() Both sha1_to_hex_r() and oid_to_hex_r() take two parameters, so use two expressions in the semantic patch for transforming calls of the former to the latter one. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/contrib/coccinelle/object_id.cocci b/contrib/coccinelle/object_id.cocci index 8ccdbb5..0307624 100644 --- a/contrib/coccinelle/object_id.cocci +++ b/contrib/coccinelle/object_id.cocci @@ -23,16 +23,16 @@ expression E1; + oid_to_hex(E1) @@ -expression E1; +expression E1, E2; @@ -- sha1_to_hex_r(E1.hash) -+ oid_to_hex_r(&E1) +- sha1_to_hex_r(E1, E2.hash) ++ oid_to_hex_r(E1, &E2) @@ -expression E1; +expression E1, E2; @@ -- sha1_to_hex_r(E1->hash) -+ oid_to_hex_r(E1) +- sha1_to_hex_r(E1, E2->hash) ++ oid_to_hex_r(E1, E2) @@ expression E1; -- cgit v0.10.2-6-g49f6 From 63f0a758a06f9af81717683185016275cb201190 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Thu, 15 Sep 2016 20:30:56 +0200 Subject: add coccicheck make target Provide a simple way to run Coccinelle against all source files, in the form of a Makefile target. Running "make coccicheck" applies each .cocci file in contrib/coccinelle/ on all source files. It generates a .patch file for each .cocci file, containing the actual changes for effecting the transformations described by the semantic patches. Non-empty .patch files are reported. They can be applied to the work tree using "patch -p0", but should be checked to e.g. make sure they don't screw up formatting or create circular references. Coccinelle's diagnostic output (stderr) is piped into .log files. Linux has a much more elaborate make target of the same name; let's start nice and easy. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/Makefile b/Makefile index d96ecb7..4b5b1a9 100644 --- a/Makefile +++ b/Makefile @@ -456,6 +456,7 @@ CURL_CONFIG = curl-config PTHREAD_LIBS = -lpthread PTHREAD_CFLAGS = GCOV = gcov +SPATCH = spatch export TCL_PATH TCLTK_PATH @@ -2296,6 +2297,18 @@ check: common-cmds.h exit 1; \ fi +C_SOURCES = $(patsubst %.o,%.c,$(C_OBJ)) +%.cocci.patch: %.cocci $(C_SOURCES) + @echo ' ' SPATCH $<; \ + for f in $(C_SOURCES); do \ + $(SPATCH) --sp-file $< $$f; \ + done >$@ 2>$@.log; \ + if test -s $@; \ + then \ + echo ' ' SPATCH result: $@; \ + fi +coccicheck: $(patsubst %.cocci,%.cocci.patch,$(wildcard contrib/coccinelle/*.cocci)) + ### Installation rules ifneq ($(filter /%,$(firstword $(template_dir))),) @@ -2487,6 +2500,7 @@ clean: profile-clean coverage-clean $(RM) -r $(GIT_TARNAME) .doc-tmp-dir $(RM) $(GIT_TARNAME).tar.gz git-core_$(GIT_VERSION)-*.tar.gz $(RM) $(htmldocs).tar.gz $(manpages).tar.gz + $(RM) contrib/coccinelle/*.cocci.patch* $(MAKE) -C Documentation/ clean ifndef NO_PERL $(MAKE) -C gitweb clean -- cgit v0.10.2-6-g49f6 From a22ae753cb297cb8a1e3ae950ae4415190cd51d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Thu, 15 Sep 2016 20:31:00 +0200 Subject: use strbuf_addstr() for adding constant strings to a strbuf, part 2 Replace uses of strbuf_addf() for adding strings with more lightweight strbuf_addstr() calls. This makes the intent clearer and avoids potential issues with printf format specifiers. 02962d36845b89145cd69f8bc65e015d78ae3434 already converted six cases, this patch covers eleven more. A semantic patch for Coccinelle is included for easier checking for new cases that might be introduced in the future. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/builtin/fmt-merge-msg.c b/builtin/fmt-merge-msg.c index ac84e99..dc2e9e4 100644 --- a/builtin/fmt-merge-msg.c +++ b/builtin/fmt-merge-msg.c @@ -395,7 +395,7 @@ static void shortlog(const char *name, for (i = 0; i < subjects.nr; i++) if (i >= limit) - strbuf_addf(out, " ...\n"); + strbuf_addstr(out, " ...\n"); else strbuf_addf(out, " %s\n", subjects.items[i].string); diff --git a/builtin/merge.c b/builtin/merge.c index 0ae099f..a8b57c7 100644 --- a/builtin/merge.c +++ b/builtin/merge.c @@ -940,7 +940,7 @@ static void write_merge_state(struct commit_list *remoteheads) strbuf_reset(&buf); if (fast_forward == FF_NO) - strbuf_addf(&buf, "no-ff"); + strbuf_addstr(&buf, "no-ff"); write_file_buf(git_path_merge_mode(), buf.buf, buf.len); } diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index e79790f..8807539 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -749,8 +749,9 @@ static int update_clone_get_next_task(struct child_process *child, ce = suc->failed_clones[index]; if (!prepare_to_clone_next_submodule(ce, child, suc, err)) { suc->current ++; - strbuf_addf(err, "BUG: submodule considered for cloning," - "doesn't need cloning any more?\n"); + strbuf_addstr(err, "BUG: submodule considered for " + "cloning, doesn't need cloning " + "any more?\n"); return 0; } p = xmalloc(sizeof(*p)); diff --git a/contrib/coccinelle/strbuf.cocci b/contrib/coccinelle/strbuf.cocci new file mode 100644 index 0000000..7932d48 --- /dev/null +++ b/contrib/coccinelle/strbuf.cocci @@ -0,0 +1,5 @@ +@@ +expression E1, E2; +@@ +- strbuf_addf(E1, E2); ++ strbuf_addstr(E1, E2); diff --git a/merge-recursive.c b/merge-recursive.c index e349126..d2b191b 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -206,7 +206,7 @@ static void output_commit_title(struct merge_options *o, struct commit *commit) find_unique_abbrev(commit->object.oid.hash, DEFAULT_ABBREV)); if (parse_commit(commit) != 0) - strbuf_addf(&o->obuf, _("(bad commit)\n")); + strbuf_addstr(&o->obuf, _("(bad commit)\n")); else { const char *title; const char *msg = get_commit_buffer(commit, NULL); diff --git a/remote.c b/remote.c index d29850a..ad6c542 100644 --- a/remote.c +++ b/remote.c @@ -2073,7 +2073,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb) _("Your branch is based on '%s', but the upstream is gone.\n"), base); if (advice_status_hints) - strbuf_addf(sb, + strbuf_addstr(sb, _(" (use \"git branch --unset-upstream\" to fixup)\n")); } else if (!ours && !theirs) { strbuf_addf(sb, @@ -2086,7 +2086,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb) ours), base, ours); if (advice_status_hints) - strbuf_addf(sb, + strbuf_addstr(sb, _(" (use \"git push\" to publish your local commits)\n")); } else if (!ours) { strbuf_addf(sb, @@ -2097,7 +2097,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb) theirs), base, theirs); if (advice_status_hints) - strbuf_addf(sb, + strbuf_addstr(sb, _(" (use \"git pull\" to update your local branch)\n")); } else { strbuf_addf(sb, @@ -2110,7 +2110,7 @@ int format_tracking_info(struct branch *branch, struct strbuf *sb) ours + theirs), base, ours, theirs); if (advice_status_hints) - strbuf_addf(sb, + strbuf_addstr(sb, _(" (use \"git pull\" to merge the remote branch into yours)\n")); } free(base); diff --git a/wt-status.c b/wt-status.c index 6225a2d..39470da 100644 --- a/wt-status.c +++ b/wt-status.c @@ -367,11 +367,11 @@ static void wt_status_print_change_data(struct wt_status *s, if (d->new_submodule_commits || d->dirty_submodule) { strbuf_addstr(&extra, " ("); if (d->new_submodule_commits) - strbuf_addf(&extra, _("new commits, ")); + strbuf_addstr(&extra, _("new commits, ")); if (d->dirty_submodule & DIRTY_SUBMODULE_MODIFIED) - strbuf_addf(&extra, _("modified content, ")); + strbuf_addstr(&extra, _("modified content, ")); if (d->dirty_submodule & DIRTY_SUBMODULE_UNTRACKED) - strbuf_addf(&extra, _("untracked content, ")); + strbuf_addstr(&extra, _("untracked content, ")); strbuf_setlen(&extra, extra.len - 2); strbuf_addch(&extra, ')'); } -- cgit v0.10.2-6-g49f6 From 7f2817daef616e764afaaf302f85beb7a13e56dc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Tue, 27 Sep 2016 22:12:33 +0200 Subject: gitignore: ignore output files of coccicheck make target MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Helped-by: Jakub Narębski Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/contrib/coccinelle/.gitignore b/contrib/coccinelle/.gitignore new file mode 100644 index 0000000..d3f2964 --- /dev/null +++ b/contrib/coccinelle/.gitignore @@ -0,0 +1 @@ +*.patch* -- cgit v0.10.2-6-g49f6 From 92d52fab3af8b1d5231285ac13b364f008d1a886 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Tue, 27 Sep 2016 21:08:21 +0200 Subject: use strbuf_addstr() instead of strbuf_addf() with "%s", part 2 Replace uses of strbuf_addf() for adding strings with more lightweight strbuf_addstr() calls. This is shorter and makes the intent clearer. bc57b9c0cc5a123365a922fa1831177e3fd607ed already converted three cases, this patch covers two more. A semantic patch for Coccinelle is included for easier checking for new cases that might be introduced in the future. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/builtin/submodule--helper.c b/builtin/submodule--helper.c index 8807539..dbe5699 100644 --- a/builtin/submodule--helper.c +++ b/builtin/submodule--helper.c @@ -637,7 +637,7 @@ static int prepare_to_clone_next_submodule(const struct cache_entry *ce, if (suc->recursive_prefix) strbuf_addf(&sb, "%s/%s", suc->recursive_prefix, ce->name); else - strbuf_addf(&sb, "%s", ce->name); + strbuf_addstr(&sb, ce->name); strbuf_addf(out, _("Skipping unmerged submodule %s"), sb.buf); strbuf_addch(out, '\n'); goto cleanup; diff --git a/contrib/coccinelle/strbuf.cocci b/contrib/coccinelle/strbuf.cocci index 7932d48..4b7553f 100644 --- a/contrib/coccinelle/strbuf.cocci +++ b/contrib/coccinelle/strbuf.cocci @@ -3,3 +3,9 @@ expression E1, E2; @@ - strbuf_addf(E1, E2); + strbuf_addstr(E1, E2); + +@@ +expression E1, E2; +@@ +- strbuf_addf(E1, "%s", E2); ++ strbuf_addstr(E1, E2); diff --git a/submodule.c b/submodule.c index 1b5cdfb..95a7ac5 100644 --- a/submodule.c +++ b/submodule.c @@ -374,7 +374,7 @@ void show_submodule_summary(FILE *f, const char *path, find_unique_abbrev(one, DEFAULT_ABBREV)); if (!fast_backward && !fast_forward) strbuf_addch(&sb, '.'); - strbuf_addf(&sb, "%s", find_unique_abbrev(two, DEFAULT_ABBREV)); + strbuf_addstr(&sb, find_unique_abbrev(two, DEFAULT_ABBREV)); if (message) strbuf_addf(&sb, " %s%s\n", message, reset); else -- cgit v0.10.2-6-g49f6 From f937d78553ce22505543580ae7958d9f5ffeeb89 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Tue, 27 Sep 2016 21:11:58 +0200 Subject: use strbuf_add_unique_abbrev() for adding short hashes, part 2 Call strbuf_add_unique_abbrev() to add abbreviated hashes to strbufs instead of taking detours through find_unique_abbrev() and its static buffer. This is shorter and a bit more efficient. 1eb47f167d65d1d305b9c196a1bb40eb96117cb1 already converted six cases, this patch covers three more. A semantic patch for Coccinelle is included for easier checking for new cases that might be introduced in the future. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/contrib/coccinelle/strbuf.cocci b/contrib/coccinelle/strbuf.cocci index 4b7553f..1e24298 100644 --- a/contrib/coccinelle/strbuf.cocci +++ b/contrib/coccinelle/strbuf.cocci @@ -9,3 +9,9 @@ expression E1, E2; @@ - strbuf_addf(E1, "%s", E2); + strbuf_addstr(E1, E2); + +@@ +expression E1, E2, E3; +@@ +- strbuf_addstr(E1, find_unique_abbrev(E2, E3)); ++ strbuf_add_unique_abbrev(E1, E2, E3); diff --git a/diff.c b/diff.c index 534c12e..ceaac82 100644 --- a/diff.c +++ b/diff.c @@ -3086,7 +3086,7 @@ static void fill_metainfo(struct strbuf *msg, } strbuf_addf(msg, "%s%sindex %s..", line_prefix, set, find_unique_abbrev(one->oid.hash, abbrev)); - strbuf_addstr(msg, find_unique_abbrev(two->oid.hash, abbrev)); + strbuf_add_unique_abbrev(msg, two->oid.hash, abbrev); if (one->mode == two->mode) strbuf_addf(msg, " %06o", one->mode); strbuf_addf(msg, "%s\n", reset); diff --git a/submodule.c b/submodule.c index 95a7ac5..1e28437 100644 --- a/submodule.c +++ b/submodule.c @@ -374,7 +374,7 @@ void show_submodule_summary(FILE *f, const char *path, find_unique_abbrev(one, DEFAULT_ABBREV)); if (!fast_backward && !fast_forward) strbuf_addch(&sb, '.'); - strbuf_addstr(&sb, find_unique_abbrev(two, DEFAULT_ABBREV)); + strbuf_add_unique_abbrev(&sb, two, DEFAULT_ABBREV); if (message) strbuf_addf(&sb, " %s%s\n", message, reset); else diff --git a/wt-status.c b/wt-status.c index 39470da..fed83e5 100644 --- a/wt-status.c +++ b/wt-status.c @@ -1326,8 +1326,7 @@ static int grab_1st_switch(unsigned char *osha1, unsigned char *nsha1, if (!strcmp(cb->buf.buf, "HEAD")) { /* HEAD is relative. Resolve it to the right reflog entry. */ strbuf_reset(&cb->buf); - strbuf_addstr(&cb->buf, - find_unique_abbrev(nsha1, DEFAULT_ABBREV)); + strbuf_add_unique_abbrev(&cb->buf, nsha1, DEFAULT_ABBREV); } return 1; } -- cgit v0.10.2-6-g49f6 From 353d84c537485500989b190c5af93ad224264e2c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Mon, 3 Oct 2016 00:58:21 +0200 Subject: coccicheck: make transformation for strbuf_addf(sb, "...") more precise We can replace strbuf_addf() calls that just add a simple string with calls to strbuf_addstr() to make the intent clearer. We need to be careful if that string contains printf format specifications like %%, though, as a simple replacement would change the output. Add checks to the semantic patch to make sure we only perform the transformation if the second argument is a string constant (possibly translated) that doesn't contain any percent signs. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/contrib/coccinelle/strbuf.cocci b/contrib/coccinelle/strbuf.cocci index 1e24298..63995f2 100644 --- a/contrib/coccinelle/strbuf.cocci +++ b/contrib/coccinelle/strbuf.cocci @@ -1,8 +1,31 @@ +@ strbuf_addf_with_format_only @ +expression E; +constant fmt; @@ -expression E1, E2; + strbuf_addf(E, +( + fmt +| + _(fmt) +) + ); + +@ script:python @ +fmt << strbuf_addf_with_format_only.fmt; @@ -- strbuf_addf(E1, E2); -+ strbuf_addstr(E1, E2); +cocci.include_match("%" not in fmt) + +@ extends strbuf_addf_with_format_only @ +@@ +- strbuf_addf ++ strbuf_addstr + (E, +( + fmt +| + _(fmt) +) + ); @@ expression E1, E2; -- cgit v0.10.2-6-g49f6 From 39ea59a2570547166834ceeff9ae0c0c05748f35 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 8 Oct 2016 16:14:57 +0200 Subject: remove unnecessary NULL check before free(3) free(3) handles NULL pointers just fine. Add a semantic patch for removing unnecessary NULL checks before calling this function, and apply it on the code base. Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/contrib/coccinelle/free.cocci b/contrib/coccinelle/free.cocci new file mode 100644 index 0000000..e282131 --- /dev/null +++ b/contrib/coccinelle/free.cocci @@ -0,0 +1,5 @@ +@@ +expression E; +@@ +- if (E) + free(E); diff --git a/parse-options-cb.c b/parse-options-cb.c index 9667bc7..1681883 100644 --- a/parse-options-cb.c +++ b/parse-options-cb.c @@ -199,8 +199,7 @@ int parse_opt_passthru(const struct option *opt, const char *arg, int unset) if (recreate_opt(&sb, opt, arg, unset) < 0) return -1; - if (*opt_value) - free(*opt_value); + free(*opt_value); *opt_value = strbuf_detach(&sb, NULL); -- cgit v0.10.2-6-g49f6 From a94bb683970a111b467a36590ca36e52754ad504 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ren=C3=A9=20Scharfe?= Date: Sat, 8 Oct 2016 17:38:47 +0200 Subject: use strbuf_add_unique_abbrev() for adding short hashes, part 3 Call strbuf_add_unique_abbrev() to add abbreviated hashes to strbufs instead of taking detours through find_unique_abbrev() and its static buffer. This is shorter in most cases and a bit more efficient. The changes here are not easily handled by a semantic patch because they involve removing temporary variables and deconstructing format strings for strbuf_addf(). Signed-off-by: Rene Scharfe Signed-off-by: Junio C Hamano diff --git a/merge-recursive.c b/merge-recursive.c index d2b191b..aa92e30 100644 --- a/merge-recursive.c +++ b/merge-recursive.c @@ -202,9 +202,9 @@ static void output_commit_title(struct merge_options *o, struct commit *commit) strbuf_addf(&o->obuf, "virtual %s\n", merge_remote_util(commit)->name); else { - strbuf_addf(&o->obuf, "%s ", - find_unique_abbrev(commit->object.oid.hash, - DEFAULT_ABBREV)); + strbuf_add_unique_abbrev(&o->obuf, commit->object.oid.hash, + DEFAULT_ABBREV); + strbuf_addch(&o->obuf, ' '); if (parse_commit(commit) != 0) strbuf_addstr(&o->obuf, _("(bad commit)\n")); else { diff --git a/pretty.c b/pretty.c index 9609afb..19089b3 100644 --- a/pretty.c +++ b/pretty.c @@ -544,15 +544,13 @@ static void add_merge_info(const struct pretty_print_context *pp, strbuf_addstr(sb, "Merge:"); while (parent) { - struct commit *p = parent->item; - const char *hex = NULL; + struct object_id *oidp = &parent->item->object.oid; + strbuf_addch(sb, ' '); if (pp->abbrev) - hex = find_unique_abbrev(p->object.oid.hash, pp->abbrev); - if (!hex) - hex = oid_to_hex(&p->object.oid); + strbuf_add_unique_abbrev(sb, oidp->hash, pp->abbrev); + else + strbuf_addstr(sb, oid_to_hex(oidp)); parent = parent->next; - - strbuf_addf(sb, " %s", hex); } strbuf_addch(sb, '\n'); } diff --git a/submodule.c b/submodule.c index 1e28437..f2ba4a2 100644 --- a/submodule.c +++ b/submodule.c @@ -370,10 +370,9 @@ void show_submodule_summary(FILE *f, const char *path, return; } - strbuf_addf(&sb, "%s%sSubmodule %s %s..", line_prefix, meta, path, - find_unique_abbrev(one, DEFAULT_ABBREV)); - if (!fast_backward && !fast_forward) - strbuf_addch(&sb, '.'); + strbuf_addf(&sb, "%s%sSubmodule %s ", line_prefix, meta, path); + strbuf_add_unique_abbrev(&sb, one, DEFAULT_ABBREV); + strbuf_addstr(&sb, (fast_backward || fast_forward) ? ".." : "..."); strbuf_add_unique_abbrev(&sb, two, DEFAULT_ABBREV); if (message) strbuf_addf(&sb, " %s%s\n", message, reset); diff --git a/wt-status.c b/wt-status.c index fed83e5..7004a2d 100644 --- a/wt-status.c +++ b/wt-status.c @@ -1053,7 +1053,6 @@ static void abbrev_sha1_in_line(struct strbuf *line) split = strbuf_split_max(line, ' ', 3); if (split[0] && split[1]) { unsigned char sha1[20]; - const char *abbrev; /* * strbuf_split_max left a space. Trim it and re-add @@ -1061,9 +1060,10 @@ static void abbrev_sha1_in_line(struct strbuf *line) */ strbuf_trim(split[1]); if (!get_sha1(split[1]->buf, sha1)) { - abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV); strbuf_reset(split[1]); - strbuf_addf(split[1], "%s ", abbrev); + strbuf_add_unique_abbrev(split[1], sha1, + DEFAULT_ABBREV); + strbuf_addch(split[1], ' '); strbuf_reset(line); for (i = 0; split[i]; i++) strbuf_addbuf(line, split[i]); @@ -1286,10 +1286,8 @@ static char *get_branch(const struct worktree *wt, const char *path) else if (starts_with(sb.buf, "refs/")) ; else if (!get_sha1_hex(sb.buf, sha1)) { - const char *abbrev; - abbrev = find_unique_abbrev(sha1, DEFAULT_ABBREV); strbuf_reset(&sb); - strbuf_addstr(&sb, abbrev); + strbuf_add_unique_abbrev(&sb, sha1, DEFAULT_ABBREV); } else if (!strcmp(sb.buf, "detached HEAD")) /* rebase */ goto got_nothing; else /* bisect */ -- cgit v0.10.2-6-g49f6