Skip to content

Commit

Permalink
*.[ch] *_INIT macros: use { 0 } for a "zero out" idiom
Browse files Browse the repository at this point in the history
In C it isn't required to specify that all members of a struct are
zero'd out to 0, NULL or '\0', just providing a "{ 0 }" will
accomplish that.

Let's also change code that provided N zero'd fields to just
provide one, and change e.g. "{ NULL }" to "{ 0 }" for
consistency. I.e. even if the first member is a pointer let's use "0"
instead of "NULL". The point of using "0" consistently is to pick one,
and to not have the reader wonder why we're not using the same pattern
everywhere.

Signed-off-by: Ævar Arnfjörð Bjarmason <[email protected]>
Signed-off-by: Junio C Hamano <[email protected]>
  • Loading branch information
avar authored and gitster committed Sep 27, 2021
1 parent 9d444d9 commit 9865b6e
Show file tree
Hide file tree
Showing 13 changed files with 21 additions and 24 deletions.
16 changes: 8 additions & 8 deletions builtin/submodule--helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ struct module_list {
const struct cache_entry **entries;
int alloc, nr;
};
#define MODULE_LIST_INIT { NULL, 0, 0 }
#define MODULE_LIST_INIT { 0 }

static int module_list_compute(int argc, const char **argv,
const char *prefix,
Expand Down Expand Up @@ -588,7 +588,7 @@ struct init_cb {
const char *prefix;
unsigned int flags;
};
#define INIT_CB_INIT { NULL, 0 }
#define INIT_CB_INIT { 0 }

static void init_submodule(const char *path, const char *prefix,
unsigned int flags)
Expand Down Expand Up @@ -717,7 +717,7 @@ struct status_cb {
const char *prefix;
unsigned int flags;
};
#define STATUS_CB_INIT { NULL, 0 }
#define STATUS_CB_INIT { 0 }

static void print_status(unsigned int flags, char state, const char *path,
const struct object_id *oid, const char *displaypath)
Expand Down Expand Up @@ -911,13 +911,13 @@ struct module_cb {
char status;
const char *sm_path;
};
#define MODULE_CB_INIT { 0, 0, NULL, NULL, '\0', NULL }
#define MODULE_CB_INIT { 0 }

struct module_cb_list {
struct module_cb **entries;
int alloc, nr;
};
#define MODULE_CB_LIST_INIT { NULL, 0, 0 }
#define MODULE_CB_LIST_INIT { 0 }

struct summary_cb {
int argc;
Expand All @@ -928,7 +928,7 @@ struct summary_cb {
unsigned int files: 1;
int summary_limit;
};
#define SUMMARY_CB_INIT { 0, NULL, NULL, 0, 0, 0, 0 }
#define SUMMARY_CB_INIT { 0 }

enum diff_cmd {
DIFF_INDEX,
Expand Down Expand Up @@ -1334,7 +1334,7 @@ struct sync_cb {
const char *prefix;
unsigned int flags;
};
#define SYNC_CB_INIT { NULL, 0 }
#define SYNC_CB_INIT { 0 }

static void sync_submodule(const char *path, const char *prefix,
unsigned int flags)
Expand Down Expand Up @@ -1480,7 +1480,7 @@ struct deinit_cb {
const char *prefix;
unsigned int flags;
};
#define DEINIT_CB_INIT { NULL, 0 }
#define DEINIT_CB_INIT { 0 }

static void deinit_submodule(const char *path, const char *prefix,
unsigned int flags)
Expand Down
2 changes: 1 addition & 1 deletion checkout.c
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ struct tracking_name_data {
struct object_id *default_dst_oid;
};

#define TRACKING_NAME_DATA_INIT { NULL, NULL, NULL, 0, NULL, NULL, NULL }
#define TRACKING_NAME_DATA_INIT { 0 }

static int check_tracking_name(struct remote *remote, void *cb_data)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ struct credential {
char *password;
};

#define CREDENTIAL_INIT { NULL, NULL, 0, NULL, NULL, NULL }
#define CREDENTIAL_INIT { 0 }

typedef int (*credential_op_cb)(struct credential *);

Expand Down
2 changes: 1 addition & 1 deletion contrib/credential/libsecret/git-credential-libsecret.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ struct credential {
char *password;
};

#define CREDENTIAL_INIT { NULL, NULL, 0, NULL, NULL, NULL }
#define CREDENTIAL_INIT { 0 }

typedef int (*credential_op_cb)(struct credential *);

Expand Down
4 changes: 2 additions & 2 deletions diff.c
Original file line number Diff line number Diff line change
Expand Up @@ -775,13 +775,13 @@ struct emitted_diff_symbol {
int indent_width; /* The visual width of the indentation */
enum diff_symbol s;
};
#define EMITTED_DIFF_SYMBOL_INIT {NULL}
#define EMITTED_DIFF_SYMBOL_INIT { 0 }

struct emitted_diff_symbols {
struct emitted_diff_symbol *buf;
int nr, alloc;
};
#define EMITTED_DIFF_SYMBOLS_INIT {NULL, 0, 0}
#define EMITTED_DIFF_SYMBOLS_INIT { 0 }

static void append_emitted_diff_symbol(struct diff_options *o,
struct emitted_diff_symbol *e)
Expand Down
2 changes: 1 addition & 1 deletion lockfile.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ struct lock_file {
struct tempfile *tempfile;
};

#define LOCK_INIT { NULL }
#define LOCK_INIT { 0 }

/* String appended to a filename to derive the lockfile name: */
#define LOCK_SUFFIX ".lock"
Expand Down
2 changes: 1 addition & 1 deletion object-store.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,7 +371,7 @@ struct object_info {
* Initializer for a "struct object_info" that wants no items. You may
* also memset() the memory to all-zeroes.
*/
#define OBJECT_INFO_INIT {NULL}
#define OBJECT_INFO_INIT { 0 }

/* Invoke lookup_replace_object() on the given hash */
#define OBJECT_INFO_LOOKUP_REPLACE 1
Expand Down
2 changes: 1 addition & 1 deletion object.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ struct object_array {
} *objects;
};

#define OBJECT_ARRAY_INIT { 0, 0, NULL }
#define OBJECT_ARRAY_INIT { 0 }

/*
* object flag allocation:
Expand Down
2 changes: 1 addition & 1 deletion oid-array.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ struct oid_array {
int sorted;
};

#define OID_ARRAY_INIT { NULL, 0, 0, 0 }
#define OID_ARRAY_INIT { 0 }

/**
* Add an item to the set. The object ID will be placed at the end of the array
Expand Down
5 changes: 1 addition & 4 deletions path.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,7 @@ struct path_cache {
const char *shallow;
};

#define PATH_CACHE_INIT \
{ \
NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL \
}
#define PATH_CACHE_INIT { 0 }

const char *git_path_squash_msg(struct repository *r);
const char *git_path_merge_msg(struct repository *r);
Expand Down
2 changes: 1 addition & 1 deletion ref-filter.c
Original file line number Diff line number Diff line change
Expand Up @@ -633,7 +633,7 @@ static struct {
*/
};

#define REF_FORMATTING_STATE_INIT { 0, NULL }
#define REF_FORMATTING_STATE_INIT { 0 }

struct ref_formatting_stack {
struct ref_formatting_stack *prev;
Expand Down
2 changes: 1 addition & 1 deletion remote.c
Original file line number Diff line number Diff line change
Expand Up @@ -2403,7 +2403,7 @@ struct reflog_commit_array {
size_t nr, alloc;
};

#define REFLOG_COMMIT_ARRAY_INIT { NULL, 0, 0 }
#define REFLOG_COMMIT_ARRAY_INIT { 0 }

/* Append a commit to the array. */
static void append_commit(struct reflog_commit_array *arr,
Expand Down
2 changes: 1 addition & 1 deletion revision.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ struct commit_stack {
struct commit **items;
size_t nr, alloc;
};
#define COMMIT_STACK_INIT { NULL, 0, 0 }
#define COMMIT_STACK_INIT { 0 }

static void commit_stack_push(struct commit_stack *stack, struct commit *commit)
{
Expand Down

0 comments on commit 9865b6e

Please sign in to comment.