Skip to content

Commit 29fb151

Browse files
committed
Merge branch 'jk/error-const-return'
Help compilers' flow analysis by making it more explicit that error() always returns -1, to reduce false "variable used uninitialized" warnings. Looks somewhat ugly but not too much. * jk/error-const-return: silence some -Wuninitialized false positives make error()'s constant return value more visible
2 parents 946a5ae + a469a10 commit 29fb151

6 files changed

Lines changed: 29 additions & 9 deletions

File tree

cache.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,9 @@ extern int check_repository_format_version(const char *var, const char *value, v
11451145
extern int git_env_bool(const char *, int);
11461146
extern int git_config_system(void);
11471147
extern int config_error_nonbool(const char *);
1148+
#ifdef __GNUC__
1149+
#define config_error_nonbool(s) (config_error_nonbool(s), -1)
1150+
#endif
11481151
extern const char *get_log_output_encoding(void);
11491152
extern const char *get_commit_output_encoding(void);
11501153

config.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1662,6 +1662,7 @@ int git_config_rename_section(const char *old_name, const char *new_name)
16621662
* Call this to report error for your variable that should not
16631663
* get a boolean value (i.e. "[my] var" means "true").
16641664
*/
1665+
#undef config_error_nonbool
16651666
int config_error_nonbool(const char *var)
16661667
{
16671668
return error("Missing value for '%s'", var);

git-compat-util.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -290,6 +290,17 @@ extern NORETURN void die_errno(const char *err, ...) __attribute__((format (prin
290290
extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
291291
extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));
292292

293+
/*
294+
* Let callers be aware of the constant return value; this can help
295+
* gcc with -Wuninitialized analysis. We have to restrict this trick to
296+
* gcc, though, because of the variadic macro and the magic ## comma pasting
297+
* behavior. But since we're only trying to help gcc, anyway, it's OK; other
298+
* compilers will fall back to using the function as usual.
299+
*/
300+
#ifdef __GNUC__
301+
#define error(fmt, ...) (error((fmt), ##__VA_ARGS__), -1)
302+
#endif
303+
293304
extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params));
294305
extern void set_error_routine(void (*routine)(const char *err, va_list params));
295306

parse-options.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,15 +18,6 @@ int optbug(const struct option *opt, const char *reason)
1818
return error("BUG: switch '%c' %s", opt->short_name, reason);
1919
}
2020

21-
int opterror(const struct option *opt, const char *reason, int flags)
22-
{
23-
if (flags & OPT_SHORT)
24-
return error("switch `%c' %s", opt->short_name, reason);
25-
if (flags & OPT_UNSET)
26-
return error("option `no-%s' %s", opt->long_name, reason);
27-
return error("option `%s' %s", opt->long_name, reason);
28-
}
29-
3021
static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
3122
int flags, const char **arg)
3223
{
@@ -594,3 +585,12 @@ static int parse_options_usage(struct parse_opt_ctx_t *ctx,
594585
return usage_with_options_internal(ctx, usagestr, opts, 0, err);
595586
}
596587

588+
#undef opterror
589+
int opterror(const struct option *opt, const char *reason, int flags)
590+
{
591+
if (flags & OPT_SHORT)
592+
return error("switch `%c' %s", opt->short_name, reason);
593+
if (flags & OPT_UNSET)
594+
return error("option `no-%s' %s", opt->long_name, reason);
595+
return error("option `%s' %s", opt->long_name, reason);
596+
}

parse-options.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,10 @@ extern NORETURN void usage_msg_opt(const char *msg,
177177

178178
extern int optbug(const struct option *opt, const char *reason);
179179
extern int opterror(const struct option *opt, const char *reason, int flags);
180+
#ifdef __GNUC__
181+
#define opterror(o,r,f) (opterror((o),(r),(f)), -1)
182+
#endif
183+
180184
/*----- incremental advanced APIs -----*/
181185

182186
enum {

usage.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,7 @@ void NORETURN die_errno(const char *fmt, ...)
130130
va_end(params);
131131
}
132132

133+
#undef error
133134
int error(const char *err, ...)
134135
{
135136
va_list params;

0 commit comments

Comments
 (0)