Skip to content

Commit 42bacbc

Browse files
authored
Merge pull request libgit2#5121 from pks-t/pks/variadic-errors
Variadic macros
2 parents b0692d6 + 1721ab0 commit 42bacbc

File tree

6 files changed

+39
-16
lines changed

6 files changed

+39
-16
lines changed

src/apply.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
#include "reader.h"
2525
#include "index.h"
2626

27-
#define apply_err(...) \
28-
( git_error_set(GIT_ERROR_PATCH, __VA_ARGS__), GIT_EAPPLYFAIL )
29-
3027
typedef struct {
3128
/* The lines that we allocate ourself are allocated out of the pool.
3229
* (Lines may have been allocated out of the diff.)
@@ -35,6 +32,18 @@ typedef struct {
3532
git_vector lines;
3633
} patch_image;
3734

35+
static int apply_err(const char *fmt, ...) GIT_FORMAT_PRINTF(1, 2);
36+
static int apply_err(const char *fmt, ...)
37+
{
38+
va_list ap;
39+
40+
va_start(ap, fmt);
41+
git_error_vset(GIT_ERROR_PATCH, fmt, ap);
42+
va_end(ap);
43+
44+
return GIT_EAPPLYFAIL;
45+
}
46+
3847
static void patch_line_init(
3948
git_diff_line *out,
4049
const char *in,

src/errors.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,21 +49,26 @@ void git_error_set_oom(void)
4949
GIT_GLOBAL->last_error = &g_git_oom_error;
5050
}
5151

52-
void git_error_set(int error_class, const char *string, ...)
52+
void git_error_set(int error_class, const char *fmt, ...)
53+
{
54+
va_list ap;
55+
56+
va_start(ap, fmt);
57+
git_error_vset(error_class, fmt, ap);
58+
va_end(ap);
59+
}
60+
61+
void git_error_vset(int error_class, const char *fmt, va_list ap)
5362
{
54-
va_list arglist;
5563
#ifdef GIT_WIN32
5664
DWORD win32_error_code = (error_class == GIT_ERROR_OS) ? GetLastError() : 0;
5765
#endif
5866
int error_code = (error_class == GIT_ERROR_OS) ? errno : 0;
5967
git_buf *buf = &GIT_GLOBAL->error_buf;
6068

6169
git_buf_clear(buf);
62-
if (string) {
63-
va_start(arglist, string);
64-
git_buf_vprintf(buf, string, arglist);
65-
va_end(arglist);
66-
70+
if (fmt) {
71+
git_buf_vprintf(buf, fmt, ap);
6772
if (error_class == GIT_ERROR_OS)
6873
git_buf_PUTS(buf, ": ");
6974
}

src/errors.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
/*
1515
* Set the error message for this thread, formatting as needed.
1616
*/
17-
18-
void git_error_set(int error_class, const char *string, ...) GIT_FORMAT_PRINTF(2, 3);
17+
void git_error_set(int error_class, const char *fmt, ...) GIT_FORMAT_PRINTF(2, 3);
18+
void git_error_vset(int error_class, const char *fmt, va_list ap);
1919

2020
/**
2121
* Set the error message for a regex failure, using the internal regex

src/parse.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,6 @@ typedef struct {
2828
int git_parse_ctx_init(git_parse_ctx *ctx, const char *content, size_t content_len);
2929
void git_parse_ctx_clear(git_parse_ctx *ctx);
3030

31-
#define git_parse_err(...) \
32-
( git_error_set(GIT_ERROR_PATCH, __VA_ARGS__), -1 )
33-
3431
#define git_parse_ctx_contains_s(ctx, str) \
3532
git_parse_ctx_contains(ctx, str, sizeof(str) - 1)
3633

src/patch_parse.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,18 @@ typedef struct {
3333
char *old_prefix, *new_prefix;
3434
} git_patch_parsed;
3535

36+
static int git_parse_err(const char *fmt, ...) GIT_FORMAT_PRINTF(1, 2);
37+
static int git_parse_err(const char *fmt, ...)
38+
{
39+
va_list ap;
40+
41+
va_start(ap, fmt);
42+
git_error_vset(GIT_ERROR_PATCH, fmt, ap);
43+
va_end(ap);
44+
45+
return -1;
46+
}
47+
3648
static size_t header_path_len(git_patch_parse_ctx *ctx)
3749
{
3850
bool inquote = 0;

src/unix/posix.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ GIT_INLINE(int) p_fsync(int fd)
5959
#define p_strcasecmp(s1, s2) strcasecmp(s1, s2)
6060
#define p_strncasecmp(s1, s2, c) strncasecmp(s1, s2, c)
6161
#define p_vsnprintf(b, c, f, a) vsnprintf(b, c, f, a)
62-
#define p_snprintf(b, c, ...) snprintf(b, c, __VA_ARGS__)
62+
#define p_snprintf snprintf
6363
#define p_mkstemp(p) mkstemp(p)
6464
#define p_chdir(p) chdir(p)
6565
#define p_chmod(p,m) chmod(p, m)

0 commit comments

Comments
 (0)