use QSORT
Apply the semantic patch contrib/coccinelle/qsort.cocci to the code base, replacing calls of qsort(3) with QSORT. The resulting code is shorter and supports empty arrays with NULL pointers. Signed-off-by: Rene Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
René Scharfe committed
Sep 29, 2016 at 17:27 UTC
9ed0d8d6e6de7737fe9a658446318b86e57c6fad
30 files changed
+44
-65
bisect.c
+1
-1
@@ -215,7 +215,7 @@ static struct commit_list *best_bisection_sorted(struct commit_list *list, int n
215
array[cnt].distance = distance;
216
cnt++;
217
}
218
- qsort(array, cnt, sizeof(*array), compare_commit_dist);
218
+ QSORT(array, cnt, compare_commit_dist);
219
for (p = list, i = 0; i < cnt; i++) {
220
char buf[100]; /* enough for dist=%d */
221
struct object *obj = &(array[i].commit->object);
builtin/describe.c
+1
-1
@@ -352,7 +352,7 @@ static void describe(const char *arg, int last_one)
352
oid_to_hex(oid));
353
}
354
355
- qsort(all_matches, match_cnt, sizeof(all_matches[0]), compare_pt);
355
+ QSORT(all_matches, match_cnt, compare_pt);
356
357
if (gave_up_on) {
358
commit_list_insert_by_date(gave_up_on, &list);
builtin/fast-export.c
+1
-1
@@ -347,7 +347,7 @@ static void show_filemodify(struct diff_queue_struct *q,
347
* Handle files below a directory first, in case they are all deleted
348
* and the directory changes to a file or symlink.
349
*/
350
- qsort(q->queue, q->nr, sizeof(q->queue[0]), depth_first);
350
+ QSORT(q->queue, q->nr, depth_first);
351
352
for (i = 0; i < q->nr; i++) {
353
struct diff_filespec *ospec = q->queue[i]->one;
builtin/fmt-merge-msg.c
+2
-4
@@ -315,12 +315,10 @@ static void add_people_info(struct strbuf *out,
315
struct string_list *committers)
316
{
317
if (authors->nr)
318
- qsort(authors->items,
319
- authors->nr, sizeof(authors->items[0]),
318
+ QSORT(authors->items, authors->nr,
319
cmp_string_list_util_as_integral);
320
if (committers->nr)
322
- qsort(committers->items,
323
- committers->nr, sizeof(committers->items[0]),
321
+ QSORT(committers->items, committers->nr,
322
cmp_string_list_util_as_integral);
323
324
credit_people(out, authors, 'a');
builtin/index-pack.c
+3
-5
@@ -1190,10 +1190,8 @@ static void resolve_deltas(void)
1190
return;
1191
1192
/* 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);
1195
1196
if (verbose || show_resolving_progress)
1197
progress = start_progress(_("Resolving deltas"),
@@ -1356,7 +1354,7 @@ static void fix_unresolved_deltas(struct sha1file *f)
1354
ALLOC_ARRAY(sorted_by_pos, nr_ref_deltas);
1355
for (i = 0; i < nr_ref_deltas; i++)
1356
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);
1358
1359
for (i = 0; i < nr_ref_deltas; i++) {
1360
struct ref_delta_entry *d = sorted_by_pos[i];
builtin/mktree.c
+1
-1
@@ -46,7 +46,7 @@ static void write_tree(unsigned char *sha1)
46
size_t size;
47
int i;
48
49
- qsort(entries, used, sizeof(*entries), ent_compare);
49
+ QSORT(entries, used, ent_compare);
50
for (size = i = 0; i < used; i++)
51
size += 32 + entries[i]->len;
52
builtin/name-rev.c
+1
-2
@@ -195,8 +195,7 @@ static const char *get_exact_ref_match(const struct object *o)
195
return NULL;
196
197
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);
199
tip_table.sorted = 1;
200
}
201
builtin/pack-objects.c
+3
-4
@@ -1535,7 +1535,7 @@ static void get_object_details(void)
1535
sorted_by_offset = xcalloc(to_pack.nr_objects, sizeof(struct object_entry *));
1536
for (i = 0; i < to_pack.nr_objects; i++)
1537
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);
1539
1540
for (i = 0; i < to_pack.nr_objects; i++) {
1541
struct object_entry *entry = sorted_by_offset[i];
@@ -2257,7 +2257,7 @@ static void prepare_pack(int window, int depth)
2257
if (progress)
2258
progress_state = start_progress(_("Compressing objects"),
2259
nr_deltas);
2260
- qsort(delta_list, n, sizeof(*delta_list), type_size_sort);
2260
+ QSORT(delta_list, n, type_size_sort);
2261
ll_find_deltas(delta_list, n, window+1, depth, &nr_done);
2262
stop_progress(&progress_state);
2263
if (nr_done != nr_deltas)
@@ -2449,8 +2449,7 @@ static void add_objects_in_unpacked_packs(struct rev_info *revs)
2449
}
2450
2451
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);
2453
for (i = 0; i < in_pack.nr; i++) {
2454
struct object *o = in_pack.array[i].object;
2455
add_object_entry(o->oid.hash, o->type, "", 0);
builtin/remote.c
+1
-2
@@ -1197,8 +1197,7 @@ static int show(int argc, const char **argv)
1197
1198
info.width = info.width2 = 0;
1199
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);
1201
if (info.list->nr)
1202
printf_ln(Q_(" Local ref configured for 'git push'%s:",
1203
" Local refs configured for 'git push'%s:",
diff.c
+3
-3
@@ -2019,7 +2019,7 @@ found_damage:
2019
return;
2020
2021
/* 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);
2023
gather_dirstat(options, &dir, changed, "", 0);
2024
}
2025
@@ -2063,7 +2063,7 @@ static void show_dirstat_by_line(struct diffstat_t *data, struct diff_options *o
2063
return;
2064
2065
/* 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);
2067
gather_dirstat(options, &dir, changed, "", 0);
2068
}
2069
@@ -4923,7 +4923,7 @@ static int diffnamecmp(const void *a_, const void *b_)
4923
void diffcore_fix_diff_index(struct diff_options *options)
4924
{
4925
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);
4927
}
4928
4929
void diffcore_std(struct diff_options *options)
diffcore-delta.c
+1
-4
@@ -158,10 +158,7 @@ static struct spanhash_top *hash_chars(struct diff_filespec *one)
158
n = 0;
159
accum1 = accum2 = 0;
160
}
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);
162
return hash;
163
}
164
diffcore-order.c
+1
-1
@@ -101,7 +101,7 @@ void order_objects(const char *orderfile, obj_path_fn_t obj_path,
101
objs[i].orig_order = i;
102
objs[i].order = match_order(obj_path(objs[i].obj));
103
}
104
- qsort(objs, nr, sizeof(*objs), compare_objs_order);
104
+ QSORT(objs, nr, compare_objs_order);
105
}
106
107
static const char *pair_pathtwo(void *obj)
diffcore-rename.c
+1
-1
@@ -580,7 +580,7 @@ void diffcore_rename(struct diff_options *options)
580
stop_progress(&progress);
581
582
/* 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);
584
585
rename_count += find_renames(mx, dst_cnt, minimum_score, 0);
586
if (detect_rename == DIFF_DETECT_COPY)
dir.c
+2
-2
@@ -2005,8 +2005,8 @@ int read_directory(struct dir_struct *dir, const char *path, int len, const stru
2005
if (!len || treat_leading_path(dir, path, len, simplify))
2006
read_directory_recursive(dir, path, len, untracked, 0, simplify);
2007
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);
2010
if (dir->untracked) {
2011
static struct trace_key trace_untracked_stats = TRACE_KEY_INIT(UNTRACKED_STATS);
2012
trace_printf_key(&trace_untracked_stats,
fast-import.c
+2
-2
@@ -1460,9 +1460,9 @@ static void mktree(struct tree_content *t, int v, struct strbuf *b)
1460
unsigned int i;
1461
1462
if (!v)
1463
- qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp0);
1463
+ QSORT(t->entries, t->entry_count, tecmp0);
1464
else
1465
- qsort(t->entries,t->entry_count,sizeof(t->entries[0]),tecmp1);
1465
+ QSORT(t->entries, t->entry_count, tecmp1);
1466
1467
for (i = 0; i < t->entry_count; i++) {
1468
if (t->entries[i]->versions[v].mode)
fetch-pack.c
+1
-1
@@ -812,7 +812,7 @@ static struct ref *do_fetch_pack(struct fetch_pack_args *args,
812
int agent_len;
813
814
sort_ref_list(&ref, ref_compare_name);
815
- qsort(sought, nr_sought, sizeof(*sought), cmp_ref_by_name);
815
+ QSORT(sought, nr_sought, cmp_ref_by_name);
816
817
if ((args->depth > 0 || is_repository_shallow()) && !server_supports("shallow"))
818
die("Server does not support shallow clients");
help.c
+5
-10
@@ -170,8 +170,7 @@ void load_command_list(const char *prefix,
170
171
if (exec_path) {
172
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);
174
uniq(main_cmds);
175
}
176
@@ -190,8 +189,7 @@ void load_command_list(const char *prefix,
189
}
190
free(paths);
191
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);
193
uniq(other_cmds);
194
}
195
exclude_cmds(other_cmds, main_cmds);
@@ -238,8 +236,7 @@ void list_common_cmds_help(void)
236
longest = strlen(common_cmds[i].name);
237
}
238
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);
240
241
puts(_("These are common Git commands used in various situations:"));
242
@@ -324,8 +321,7 @@ const char *help_unknown_cmd(const char *cmd)
321
322
add_cmd_list(&main_cmds, &aliases);
323
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);
325
uniq(&main_cmds);
326
327
/* This abuses cmdname->len for levenshtein distance */
@@ -359,8 +355,7 @@ const char *help_unknown_cmd(const char *cmd)
355
levenshtein(cmd, candidate, 0, 2, 1, 3) + 1;
356
}
357
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);
359
360
if (!main_cmds.cnt)
361
die(_("Uh oh. Your system reports no Git commands at all."));
line-log.c
+1
-1
@@ -113,7 +113,7 @@ void sort_and_merge_range_set(struct range_set *rs)
113
int i;
114
int o = 0; /* output cursor */
115
116
- qsort(rs->ranges, rs->nr, sizeof(struct range), range_cmp);
116
+ QSORT(rs->ranges, rs->nr, range_cmp);
117
118
for (i = 0; i < rs->nr; i++) {
119
if (rs->ranges[i].start == rs->ranges[i].end)
pack-bitmap-write.c
+1
-2
@@ -385,8 +385,7 @@ void bitmap_writer_select_commits(struct commit **indexed_commits,
385
{
386
unsigned int i = 0, j, next;
387
388
- qsort(indexed_commits, indexed_commits_nr, sizeof(indexed_commits[0]),
389
- date_compare);
388
+ QSORT(indexed_commits, indexed_commits_nr, date_compare);
389
390
if (writer.show_progress)
391
writer.progress = start_progress("Selecting bitmap commits", 0);
pack-check.c
+1
-1
@@ -99,7 +99,7 @@ static int verify_packfile(struct packed_git *p,
99
entries[i].offset = nth_packed_object_offset(p, i);
100
entries[i].nr = i;
101
}
102
- qsort(entries, nr_objects, sizeof(*entries), compare_entries);
102
+ QSORT(entries, nr_objects, compare_entries);
103
104
for (i = 0; i < nr_objects; i++) {
105
void *data;
pack-write.c
+1
-2
@@ -61,8 +61,7 @@ const char *write_idx_file(const char *index_name, struct pack_idx_entry **objec
61
if (objects[i]->offset > last_obj_offset)
62
last_obj_offset = objects[i]->offset;
63
}
64
- qsort(sorted_by_sha, nr_objects, sizeof(sorted_by_sha[0]),
65
- sha1_compare);
64
+ QSORT(sorted_by_sha, nr_objects, sha1_compare);
65
}
66
else
67
sorted_by_sha = list = last = NULL;
pathspec.c
+1
-2
@@ -446,8 +446,7 @@ void parse_pathspec(struct pathspec *pathspec,
446
if (pathspec->magic & PATHSPEC_MAXDEPTH) {
447
if (flags & PATHSPEC_KEEP_ORDER)
448
die("BUG: PATHSPEC_MAXDEPTH_VALID and PATHSPEC_KEEP_ORDER are incompatible");
449
- qsort(pathspec->items, pathspec->nr,
450
- sizeof(struct pathspec_item), pathspec_item_cmp);
449
+ QSORT(pathspec->items, pathspec->nr, pathspec_item_cmp);
450
}
451
}
452
ref-filter.c
+1
-1
@@ -1573,7 +1573,7 @@ static int compare_refs(const void *a_, const void *b_)
1573
void ref_array_sort(struct ref_sorting *sorting, struct ref_array *array)
1574
{
1575
ref_sorting = sorting;
1576
- qsort(array->items, array->nr, sizeof(struct ref_array_item *), compare_refs);
1576
+ QSORT(array->items, array->nr, compare_refs);
1577
}
1578
1579
static void append_literal(const char *cp, const char *ep, struct ref_formatting_state *state)
refs/files-backend.c
+1
-1
@@ -501,7 +501,7 @@ static void sort_ref_dir(struct ref_dir *dir)
501
if (dir->sorted == dir->nr)
502
return;
503
504
- qsort(dir->entries, dir->nr, sizeof(*dir->entries), ref_entry_cmp);
504
+ QSORT(dir->entries, dir->nr, ref_entry_cmp);
505
506
/* Remove any duplicates: */
507
for (i = 0, j = 0; j < dir->nr; j++) {
server-info.c
+1
-1
@@ -229,7 +229,7 @@ static void init_pack_info(const char *infofile, int force)
229
}
230
231
/* renumber them */
232
- qsort(info, num_pack, sizeof(info[0]), compare_info);
232
+ QSORT(info, num_pack, compare_info);
233
for (i = 0; i < num_pack; i++)
234
info[i]->new_num = i;
235
}
sh-i18n--envsubst.c
+1
-1
@@ -231,7 +231,7 @@ static inline void
231
string_list_sort (string_list_ty *slp)
232
{
233
if (slp->nitems > 0)
234
- qsort (slp->item, slp->nitems, sizeof (slp->item[0]), cmp_string);
234
+ QSORT(slp->item, slp->nitems, cmp_string);
235
}
236
237
/* Test whether a sorted string list contains a given string. */
sha1-array.c
+1
-1
@@ -16,7 +16,7 @@ static int void_hashcmp(const void *a, const void *b)
16
17
static void sha1_array_sort(struct sha1_array *array)
18
{
19
- qsort(array->sha1, array->nr, sizeof(*array->sha1), void_hashcmp);
19
+ QSORT(array->sha1, array->nr, void_hashcmp);
20
array->sorted = 1;
21
}
22
string-list.c
+1
-1
@@ -225,7 +225,7 @@ static int cmp_items(const void *a, const void *b)
225
void string_list_sort(struct string_list *list)
226
{
227
compare_for_qsort = list->cmp ? list->cmp : strcmp;
228
- qsort(list->items, list->nr, sizeof(*list->items), cmp_items);
228
+ QSORT(list->items, list->nr, cmp_items);
229
}
230
231
struct string_list_item *unsorted_string_list_lookup(struct string_list *list,
t/helper/test-dump-untracked-cache.c
+2
-4
@@ -18,10 +18,8 @@ static int compare_dir(const void *a_, const void *b_)
18
static void dump(struct untracked_cache_dir *ucd, struct strbuf *base)
19
{
20
int i, len;
21
- qsort(ucd->untracked, ucd->untracked_nr, sizeof(*ucd->untracked),
22
- compare_untracked);
23
- qsort(ucd->dirs, ucd->dirs_nr, sizeof(*ucd->dirs),
24
- compare_dir);
21
+ QSORT(ucd->untracked, ucd->untracked_nr, compare_untracked);
22
+ QSORT(ucd->dirs, ucd->dirs_nr, compare_dir);
23
len = base->len;
24
strbuf_addf(base, "%s/", ucd->name);
25
printf("%s %s", base->buf,
tree.c
+1
-2
@@ -180,8 +180,7 @@ int read_tree(struct tree *tree, int stage, struct pathspec *match)
180
* Sort the cache entry -- we need to nuke the cache tree, though.
181
*/
182
cache_tree_free(&active_cache_tree);
183
- qsort(active_cache, active_nr, sizeof(active_cache[0]),
184
- cmp_cache_name_compare);
183
+ QSORT(active_cache, active_nr, cmp_cache_name_compare);
184
return 0;
185
}
186