Skip to content

Commit b8688ad

Browse files
committed
Merge branch 'rs/qsort'
We call "qsort(array, nelem, sizeof(array[0]), fn)", and most of the time third parameter is redundant. A new QSORT() macro lets us omit it. * rs/qsort: show-branch: use QSORT use QSORT, part 2 coccicheck: use --all-includes by default remove unnecessary check before QSORT use QSORT add QSORT
2 parents a23ca1b + 7e65c75 commit b8688ad

35 files changed

+97
-78
lines changed

Makefile

+2-1
Original file line numberDiff line numberDiff line change
@@ -467,6 +467,7 @@ SPATCH = spatch
467467
export TCL_PATH TCLTK_PATH
468468

469469
SPARSE_FLAGS =
470+
SPATCH_FLAGS = --all-includes
470471

471472

472473

@@ -2314,7 +2315,7 @@ C_SOURCES = $(patsubst %.o,%.c,$(C_OBJ))
23142315
%.cocci.patch: %.cocci $(C_SOURCES)
23152316
@echo ' ' SPATCH $<; \
23162317
for f in $(C_SOURCES); do \
2317-
$(SPATCH) --sp-file $< $$f; \
2318+
$(SPATCH) --sp-file $< $$f $(SPATCH_FLAGS); \
23182319
done >$@ 2>$@.log; \
23192320
if test -s $@; \
23202321
then \

bisect.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ static struct commit_list *best_bisection_sorted(struct commit_list *list, int n
215215
array[cnt].distance = distance;
216216
cnt++;
217217
}
218-
qsort(array, cnt, sizeof(*array), compare_commit_dist);
218+
QSORT(array, cnt, compare_commit_dist);
219219
for (p = list, i = 0; i < cnt; i++) {
220220
char buf[100]; /* enough for dist=%d */
221221
struct object *obj = &(array[i].commit->object);

builtin/describe.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ static void describe(const char *arg, int last_one)
352352
oid_to_hex(oid));
353353
}
354354

355-
qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
355+
QSORT(all_matches, match_cnt, compare_pt);
356356

357357
if (gave_up_on) {
358358
commit_list_insert_by_date(gave_up_on, &list);

builtin/fast-export.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ static void show_filemodify(struct diff_queue_struct *q,
347347
* Handle files below a directory first, in case they are all deleted
348348
* and the directory changes to a file or symlink.
349349
*/
350-
qsort(q->queue, q->nr, sizeof(q->queue[0]), depth_first);
350+
QSORT(q->queue, q->nr, depth_first);
351351

352352
for (i = 0; i < q->nr; i++) {
353353
struct diff_filespec *ospec = q->queue[i]->one;

builtin/fmt-merge-msg.c

+4-8
Original file line numberDiff line numberDiff line change
@@ -314,14 +314,10 @@ static void add_people_info(struct strbuf *out,
314314
struct string_list *authors,
315315
struct string_list *committers)
316316
{
317-
if (authors->nr)
318-
qsort(authors->items,
319-
authors->nr, sizeof(authors->items[0]),
320-
cmp_string_list_util_as_integral);
321-
if (committers->nr)
322-
qsort(committers->items,
323-
committers->nr, sizeof(committers->items[0]),
324-
cmp_string_list_util_as_integral);
317+
QSORT(authors->items, authors->nr,
318+
cmp_string_list_util_as_integral);
319+
QSORT(committers->items, committers->nr,
320+
cmp_string_list_util_as_integral);
325321

326322
credit_people(out, authors, 'a');
327323
credit_people(out, committers, 'c');

builtin/index-pack.c

+4-7
Original file line numberDiff line numberDiff line change
@@ -1190,10 +1190,8 @@ static void resolve_deltas(void)
11901190
return;
11911191

11921192
/* Sort deltas by base SHA1/offset for fast searching */
1193-
qsort(ofs_deltas, nr_ofs_deltas, sizeof(struct ofs_delta_entry),
1194-
compare_ofs_delta_entry);
1195-
qsort(ref_deltas, nr_ref_deltas, sizeof(struct ref_delta_entry),
1196-
compare_ref_delta_entry);
1193+
QSORT(ofs_deltas, nr_ofs_deltas, compare_ofs_delta_entry);
1194+
QSORT(ref_deltas, nr_ref_deltas, compare_ref_delta_entry);
11971195

11981196
if (verbose || show_resolving_progress)
11991197
progress = start_progress(_("Resolving deltas"),
@@ -1356,7 +1354,7 @@ static void fix_unresolved_deltas(struct sha1file *f)
13561354
ALLOC_ARRAY(sorted_by_pos, nr_ref_deltas);
13571355
for (i = 0; i < nr_ref_deltas; i++)
13581356
sorted_by_pos[i] = &ref_deltas[i];
1359-
qsort(sorted_by_pos, nr_ref_deltas, sizeof(*sorted_by_pos), delta_pos_compare);
1357+
QSORT(sorted_by_pos, nr_ref_deltas, delta_pos_compare);
13601358

13611359
for (i = 0; i < nr_ref_deltas; i++) {
13621360
struct ref_delta_entry *d = sorted_by_pos[i];
@@ -1533,8 +1531,7 @@ static void read_v2_anomalous_offsets(struct packed_git *p,
15331531
opts->anomaly[opts->anomaly_nr++] = ntohl(idx2[off * 2 + 1]);
15341532
}
15351533

1536-
if (1 < opts->anomaly_nr)
1537-
qsort(opts->anomaly, opts->anomaly_nr, sizeof(uint32_t), cmp_uint32);
1534+
QSORT(opts->anomaly, opts->anomaly_nr, cmp_uint32);
15381535
}
15391536

15401537
static void read_idx_option(struct pack_idx_option *opts, const char *pack_name)

builtin/mktree.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ static void write_tree(unsigned char *sha1)
4646
size_t size;
4747
int i;
4848

49-
qsort(entries, used, sizeof(*entries), ent_compare);
49+
QSORT(entries, used, ent_compare);
5050
for (size = i = 0; i < used; i++)
5151
size += 32 + entries[i]->len;
5252

builtin/name-rev.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -195,8 +195,7 @@ static const char *get_exact_ref_match(const struct object *o)
195195
return NULL;
196196

197197
if (!tip_table.sorted) {
198-
qsort(tip_table.table, tip_table.nr, sizeof(*tip_table.table),
199-
tipcmp);
198+
QSORT(tip_table.table, tip_table.nr, tipcmp);
200199
tip_table.sorted = 1;
201200
}
202201

builtin/pack-objects.c

+3-4
Original file line numberDiff line numberDiff line change
@@ -1535,7 +1535,7 @@ static void get_object_details(void)
15351535
sorted_by_offset = xcalloc(to_pack.nr_objects, sizeof(struct object_entry *));
15361536
for (i = 0; i < to_pack.nr_objects; i++)
15371537
sorted_by_offset[i] = to_pack.objects + i;
1538-
qsort(sorted_by_offset, to_pack.nr_objects, sizeof(*sorted_by_offset), pack_offset_sort);
1538+
QSORT(sorted_by_offset, to_pack.nr_objects, pack_offset_sort);
15391539

15401540
for (i = 0; i < to_pack.nr_objects; i++) {
15411541
struct object_entry *entry = sorted_by_offset[i];
@@ -2257,7 +2257,7 @@ static void prepare_pack(int window, int depth)
22572257
if (progress)
22582258
progress_state = start_progress(_("Compressing objects"),
22592259
nr_deltas);
2260-
qsort(delta_list, n, sizeof(*delta_list), type_size_sort);
2260+
QSORT(delta_list, n, type_size_sort);
22612261
ll_find_deltas(delta_list, n, window+1, depth, &nr_done);
22622262
stop_progress(&progress_state);
22632263
if (nr_done != nr_deltas)
@@ -2449,8 +2449,7 @@ static void add_objects_in_unpacked_packs(struct rev_info *revs)
24492449
}
24502450

24512451
if (in_pack.nr) {
2452-
qsort(in_pack.array, in_pack.nr, sizeof(in_pack.array[0]),
2453-
ofscmp);
2452+
QSORT(in_pack.array, in_pack.nr, ofscmp);
24542453
for (i = 0; i < in_pack.nr; i++) {
24552454
struct object *o = in_pack.array[i].object;
24562455
add_object_entry(o->oid.hash, o->type, "", 0);

builtin/remote.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -1197,8 +1197,7 @@ static int show(int argc, const char **argv)
11971197

11981198
info.width = info.width2 = 0;
11991199
for_each_string_list(&states.push, add_push_to_show_info, &info);
1200-
qsort(info.list->items, info.list->nr,
1201-
sizeof(*info.list->items), cmp_string_with_push);
1200+
QSORT(info.list->items, info.list->nr, cmp_string_with_push);
12021201
if (info.list->nr)
12031202
printf_ln(Q_(" Local ref configured for 'git push'%s:",
12041203
" Local refs configured for 'git push'%s:",

builtin/shortlog.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ void shortlog_output(struct shortlog *log)
308308
struct strbuf sb = STRBUF_INIT;
309309

310310
if (log->sort_by_number)
311-
qsort(log->list.items, log->list.nr, sizeof(struct string_list_item),
311+
QSORT(log->list.items, log->list.nr,
312312
log->summary ? compare_by_counter : compare_by_list);
313313
for (i = 0; i < log->list.nr; i++) {
314314
const struct string_list_item *item = &log->list.items[i];

builtin/show-branch.c

+2-4
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,7 @@ static int compare_ref_name(const void *a_, const void *b_)
353353

354354
static void sort_ref_range(int bottom, int top)
355355
{
356-
qsort(ref_name + bottom, top - bottom, sizeof(ref_name[0]),
357-
compare_ref_name);
356+
QSORT(ref_name + bottom, top - bottom, compare_ref_name);
358357
}
359358

360359
static int append_ref(const char *refname, const struct object_id *oid,
@@ -540,8 +539,7 @@ static void append_one_rev(const char *av)
540539
if (saved_matches == ref_name_cnt &&
541540
ref_name_cnt < MAX_REVS)
542541
error(_("no matching refs with %s"), av);
543-
if (saved_matches + 1 < ref_name_cnt)
544-
sort_ref_range(saved_matches, ref_name_cnt);
542+
sort_ref_range(saved_matches, ref_name_cnt);
545543
return;
546544
}
547545
die("bad sha1 reference %s", av);

contrib/coccinelle/qsort.cocci

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
@@
2+
expression base, nmemb, compar;
3+
@@
4+
- qsort(base, nmemb, sizeof(*base), compar);
5+
+ QSORT(base, nmemb, compar);
6+
7+
@@
8+
expression base, nmemb, compar;
9+
@@
10+
- qsort(base, nmemb, sizeof(base[0]), compar);
11+
+ QSORT(base, nmemb, compar);
12+
13+
@@
14+
type T;
15+
T *base;
16+
expression nmemb, compar;
17+
@@
18+
- qsort(base, nmemb, sizeof(T), compar);
19+
+ QSORT(base, nmemb, compar);
20+
21+
@@
22+
expression base, nmemb, compar;
23+
@@
24+
- if (nmemb)
25+
QSORT(base, nmemb, compar);
26+
27+
@@
28+
expression base, nmemb, compar;
29+
@@
30+
- if (nmemb > 0)
31+
QSORT(base, nmemb, compar);
32+
33+
@@
34+
expression base, nmemb, compar;
35+
@@
36+
- if (nmemb > 1)
37+
QSORT(base, nmemb, compar);

diff.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -2019,7 +2019,7 @@ static void show_dirstat(struct diff_options *options)
20192019
return;
20202020

20212021
/* Show all directories with more than x% of the changes */
2022-
qsort(dir.files, dir.nr, sizeof(dir.files[0]), dirstat_compare);
2022+
QSORT(dir.files, dir.nr, dirstat_compare);
20232023
gather_dirstat(options, &dir, changed, "", 0);
20242024
}
20252025

@@ -2063,7 +2063,7 @@ static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *o
20632063
return;
20642064

20652065
/* Show all directories with more than x% of the changes */
2066-
qsort(dir.files, dir.nr, sizeof(dir.files[0]), dirstat_compare);
2066+
QSORT(dir.files, dir.nr, dirstat_compare);
20672067
gather_dirstat(options, &dir, changed, "", 0);
20682068
}
20692069

@@ -4923,7 +4923,7 @@ static int diffnamecmp(const void *a_, const void *b_)
49234923
void diffcore_fix_diff_index(struct diff_options *options)
49244924
{
49254925
struct diff_queue_struct *q = &diff_queued_diff;
4926-
qsort(q->queue, q->nr, sizeof(q->queue[0]), diffnamecmp);
4926+
QSORT(q->queue, q->nr, diffnamecmp);
49274927
}
49284928

49294929
void diffcore_std(struct diff_options *options)

diffcore-delta.c

+1-4
Original file line numberDiff line numberDiff line change
@@ -158,10 +158,7 @@ static struct spanhash_top *hash_chars(struct diff_filespec *one)
158158
n = 0;
159159
accum1 = accum2 = 0;
160160
}
161-
qsort(hash->data,
162-
1ul << hash->alloc_log2,
163-
sizeof(hash->data[0]),
164-
spanhash_cmp);
161+
QSORT(hash->data, 1ul << hash->alloc_log2, spanhash_cmp);
165162
return hash;
166163
}
167164

diffcore-order.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ void order_objects(const char *orderfile, obj_path_fn_t obj_path,
101101
objs[i].orig_order = i;
102102
objs[i].order = match_order(obj_path(objs[i].obj));
103103
}
104-
qsort(objs, nr, sizeof(*objs), compare_objs_order);
104+
QSORT(objs, nr, compare_objs_order);
105105
}
106106

107107
static const char *pair_pathtwo(void *obj)

diffcore-rename.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ void diffcore_rename(struct diff_options *options)
580580
stop_progress(&progress);
581581

582582
/* cost matrix sorted by most to least similar pair */
583-
qsort(mx, dst_cnt * NUM_CANDIDATE_PER_DST, sizeof(*mx), score_compare);
583+
QSORT(mx, dst_cnt * NUM_CANDIDATE_PER_DST, score_compare);
584584

585585
rename_count += find_renames(mx, dst_cnt, minimum_score, 0);
586586
if (detect_rename == DIFF_DETECT_COPY)

dir.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -2005,8 +2005,8 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const stru
20052005
if (!len || treat_leading_path(dir, path, len, simplify))
20062006
read_directory_recursive(dir, path, len, untracked, 0, simplify);
20072007
free_simplify(simplify);
2008-
qsort(dir->entries, dir->nr, sizeof(struct dir_entry *), cmp_name);
2009-
qsort(dir->ignored, dir->ignored_nr, sizeof(struct dir_entry *), cmp_name);
2008+
QSORT(dir->entries, dir->nr, cmp_name);
2009+
QSORT(dir->ignored, dir->ignored_nr, cmp_name);
20102010
if (dir->untracked) {
20112011
static struct trace_key trace_untracked_stats = TRACE_KEY_INIT(UNTRACKED_STATS);
20122012
trace_printf_key(&trace_untracked_stats,

fast-import.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1460,9 +1460,9 @@ static void mktree(struct tree_content *t, int v, struct strbuf *b)
14601460
unsigned int i;
14611461

14621462
if (!v)
1463-
qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
1463+
QSORT(t->entries, t->entry_count, tecmp0);
14641464
else
1465-
qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
1465+
QSORT(t->entries, t->entry_count, tecmp1);
14661466

14671467
for (i = 0; i < t->entry_count; i++) {
14681468
if (t->entries[i]->versions[v].mode)

fetch-pack.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -819,7 +819,7 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
819819
int agent_len;
820820

821821
sort_ref_list(&ref, ref_compare_name);
822-
qsort(sought, nr_sought, sizeof(*sought), cmp_ref_by_name);
822+
QSORT(sought, nr_sought, cmp_ref_by_name);
823823

824824
if ((args->depth > 0 || is_repository_shallow()) && !server_supports("shallow"))
825825
die("Server does not support shallow clients");

git-compat-util.h

+8
Original file line numberDiff line numberDiff line change
@@ -985,6 +985,14 @@ void git_qsort(void *base, size_t nmemb, size_t size,
985985
#define qsort git_qsort
986986
#endif
987987

988+
#define QSORT(base, n, compar) sane_qsort((base), (n), sizeof(*(base)), compar)
989+
static inline void sane_qsort(void *base, size_t nmemb, size_t size,
990+
int(*compar)(const void *, const void *))
991+
{
992+
if (nmemb > 1)
993+
qsort(base, nmemb, size, compar);
994+
}
995+
988996
#ifndef REG_STARTEND
989997
#error "Git requires REG_STARTEND support. Compile with NO_REGEX=NeedsStartEnd"
990998
#endif

help.c

+5-10
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,7 @@ void load_command_list(const char *prefix,
170170

171171
if (exec_path) {
172172
list_commands_in_dir(main_cmds, exec_path, prefix);
173-
qsort(main_cmds->names, main_cmds->cnt,
174-
sizeof(*main_cmds->names), cmdname_compare);
173+
QSORT(main_cmds->names, main_cmds->cnt, cmdname_compare);
175174
uniq(main_cmds);
176175
}
177176

@@ -190,8 +189,7 @@ void load_command_list(const char *prefix,
190189
}
191190
free(paths);
192191

193-
qsort(other_cmds->names, other_cmds->cnt,
194-
sizeof(*other_cmds->names), cmdname_compare);
192+
QSORT(other_cmds->names, other_cmds->cnt, cmdname_compare);
195193
uniq(other_cmds);
196194
}
197195
exclude_cmds(other_cmds, main_cmds);
@@ -238,8 +236,7 @@ void list_common_cmds_help(void)
238236
longest = strlen(common_cmds[i].name);
239237
}
240238

241-
qsort(common_cmds, ARRAY_SIZE(common_cmds),
242-
sizeof(common_cmds[0]), cmd_group_cmp);
239+
QSORT(common_cmds, ARRAY_SIZE(common_cmds), cmd_group_cmp);
243240

244241
puts(_("These are common Git commands used in various situations:"));
245242

@@ -324,8 +321,7 @@ const char *help_unknown_cmd(const char *cmd)
324321

325322
add_cmd_list(&main_cmds, &aliases);
326323
add_cmd_list(&main_cmds, &other_cmds);
327-
qsort(main_cmds.names, main_cmds.cnt,
328-
sizeof(*main_cmds.names), cmdname_compare);
324+
QSORT(main_cmds.names, main_cmds.cnt, cmdname_compare);
329325
uniq(&main_cmds);
330326

331327
/* This abuses cmdname->len for levenshtein distance */
@@ -359,8 +355,7 @@ const char *help_unknown_cmd(const char *cmd)
359355
levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
360356
}
361357

362-
qsort(main_cmds.names, main_cmds.cnt,
363-
sizeof(*main_cmds.names), levenshtein_compare);
358+
QSORT(main_cmds.names, main_cmds.cnt, levenshtein_compare);
364359

365360
if (!main_cmds.cnt)
366361
die(_("Uh oh. Your system reports no Git commands at all."));

line-log.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -113,7 +113,7 @@ void sort_and_merge_range_set(struct range_set *rs)
113113
int i;
114114
int o = 0; /* output cursor */
115115

116-
qsort(rs->ranges, rs->nr, sizeof(struct range), range_cmp);
116+
QSORT(rs->ranges, rs->nr, range_cmp);
117117

118118
for (i = 0; i < rs->nr; i++) {
119119
if (rs->ranges[i].start == rs->ranges[i].end)

pack-bitmap-write.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -385,8 +385,7 @@ void bitmap_writer_select_commits(struct commit **indexed_commits,
385385
{
386386
unsigned int i = 0, j, next;
387387

388-
qsort(indexed_commits, indexed_commits_nr, sizeof(indexed_commits[0]),
389-
date_compare);
388+
QSORT(indexed_commits, indexed_commits_nr, date_compare);
390389

391390
if (writer.show_progress)
392391
writer.progress = start_progress("Selecting bitmap commits", 0);

0 commit comments

Comments
 (0)