Skip to content

Commit

Permalink
Merge branch 'jk/error-const-return'
Browse files Browse the repository at this point in the history
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
  • Loading branch information
gitster committed Jan 6, 2013
2 parents 946a5ae + a469a10 commit 29fb151
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 9 deletions.
3 changes: 3 additions & 0 deletions cache.h
Original file line number Diff line number Diff line change
Expand Up @@ -1145,6 +1145,9 @@ extern int check_repository_format_version(const char *var, const char *value, v
extern int git_env_bool(const char *, int);
extern int git_config_system(void);
extern int config_error_nonbool(const char *);
#ifdef __GNUC__
#define config_error_nonbool(s) (config_error_nonbool(s), -1)
#endif
extern const char *get_log_output_encoding(void);
extern const char *get_commit_output_encoding(void);

Expand Down
1 change: 1 addition & 0 deletions config.c
Original file line number Diff line number Diff line change
Expand Up @@ -1662,6 +1662,7 @@ int git_config_rename_section(const char *old_name, const char *new_name)
* Call this to report error for your variable that should not
* get a boolean value (i.e. "[my] var" means "true").
*/
#undef config_error_nonbool
int config_error_nonbool(const char *var)
{
return error("Missing value for '%s'", var);
Expand Down
11 changes: 11 additions & 0 deletions git-compat-util.h
Original file line number Diff line number Diff line change
Expand Up @@ -290,6 +290,17 @@ extern NORETURN void die_errno(const char *err, ...) __attribute__((format (prin
extern int error(const char *err, ...) __attribute__((format (printf, 1, 2)));
extern void warning(const char *err, ...) __attribute__((format (printf, 1, 2)));

/*
* Let callers be aware of the constant return value; this can help
* gcc with -Wuninitialized analysis. We have to restrict this trick to
* gcc, though, because of the variadic macro and the magic ## comma pasting
* behavior. But since we're only trying to help gcc, anyway, it's OK; other
* compilers will fall back to using the function as usual.
*/
#ifdef __GNUC__
#define error(fmt, ...) (error((fmt), ##__VA_ARGS__), -1)
#endif

extern void set_die_routine(NORETURN_PTR void (*routine)(const char *err, va_list params));
extern void set_error_routine(void (*routine)(const char *err, va_list params));

Expand Down
18 changes: 9 additions & 9 deletions parse-options.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,6 @@ int optbug(const struct option *opt, const char *reason)
return error("BUG: switch '%c' %s", opt->short_name, reason);
}

int opterror(const struct option *opt, const char *reason, int flags)
{
if (flags & OPT_SHORT)
return error("switch `%c' %s", opt->short_name, reason);
if (flags & OPT_UNSET)
return error("option `no-%s' %s", opt->long_name, reason);
return error("option `%s' %s", opt->long_name, reason);
}

static int get_arg(struct parse_opt_ctx_t *p, const struct option *opt,
int flags, const char **arg)
{
Expand Down Expand Up @@ -594,3 +585,12 @@ static int parse_options_usage(struct parse_opt_ctx_t *ctx,
return usage_with_options_internal(ctx, usagestr, opts, 0, err);
}

#undef opterror
int opterror(const struct option *opt, const char *reason, int flags)
{
if (flags & OPT_SHORT)
return error("switch `%c' %s", opt->short_name, reason);
if (flags & OPT_UNSET)
return error("option `no-%s' %s", opt->long_name, reason);
return error("option `%s' %s", opt->long_name, reason);
}
4 changes: 4 additions & 0 deletions parse-options.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,10 @@ extern NORETURN void usage_msg_opt(const char *msg,

extern int optbug(const struct option *opt, const char *reason);
extern int opterror(const struct option *opt, const char *reason, int flags);
#ifdef __GNUC__
#define opterror(o,r,f) (opterror((o),(r),(f)), -1)
#endif

/*----- incremental advanced APIs -----*/

enum {
Expand Down
1 change: 1 addition & 0 deletions usage.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ void NORETURN die_errno(const char *fmt, ...)
va_end(params);
}

#undef error
int error(const char *err, ...)
{
va_list params;
Expand Down

0 comments on commit 29fb151

Please sign in to comment.