Merge pull request #5121 from pks-t/pks/variadic-errors Variadic macros
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
diff --git a/src/apply.c b/src/apply.c
index 55b1a39..1ee9291 100644
--- a/src/apply.c
+++ b/src/apply.c
@@ -24,9 +24,6 @@
#include "reader.h"
#include "index.h"
-#define apply_err(...) \
- ( git_error_set(GIT_ERROR_PATCH, __VA_ARGS__), GIT_EAPPLYFAIL )
-
typedef struct {
/* The lines that we allocate ourself are allocated out of the pool.
* (Lines may have been allocated out of the diff.)
@@ -35,6 +32,18 @@ typedef struct {
git_vector lines;
} patch_image;
+static int apply_err(const char *fmt, ...) GIT_FORMAT_PRINTF(1, 2);
+static int apply_err(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ git_error_vset(GIT_ERROR_PATCH, fmt, ap);
+ va_end(ap);
+
+ return GIT_EAPPLYFAIL;
+}
+
static void patch_line_init(
git_diff_line *out,
const char *in,
diff --git a/src/errors.c b/src/errors.c
index 8ef4919..18d6c2d 100644
--- a/src/errors.c
+++ b/src/errors.c
@@ -49,9 +49,17 @@ void git_error_set_oom(void)
GIT_GLOBAL->last_error = &g_git_oom_error;
}
-void git_error_set(int error_class, const char *string, ...)
+void git_error_set(int error_class, const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ git_error_vset(error_class, fmt, ap);
+ va_end(ap);
+}
+
+void git_error_vset(int error_class, const char *fmt, va_list ap)
{
- va_list arglist;
#ifdef GIT_WIN32
DWORD win32_error_code = (error_class == GIT_ERROR_OS) ? GetLastError() : 0;
#endif
@@ -59,11 +67,8 @@ void git_error_set(int error_class, const char *string, ...)
git_buf *buf = &GIT_GLOBAL->error_buf;
git_buf_clear(buf);
- if (string) {
- va_start(arglist, string);
- git_buf_vprintf(buf, string, arglist);
- va_end(arglist);
-
+ if (fmt) {
+ git_buf_vprintf(buf, fmt, ap);
if (error_class == GIT_ERROR_OS)
git_buf_PUTS(buf, ": ");
}
diff --git a/src/errors.h b/src/errors.h
index f2af1e3..86f06f9 100644
--- a/src/errors.h
+++ b/src/errors.h
@@ -14,8 +14,8 @@
/*
* Set the error message for this thread, formatting as needed.
*/
-
-void git_error_set(int error_class, const char *string, ...) GIT_FORMAT_PRINTF(2, 3);
+void git_error_set(int error_class, const char *fmt, ...) GIT_FORMAT_PRINTF(2, 3);
+void git_error_vset(int error_class, const char *fmt, va_list ap);
/**
* Set the error message for a regex failure, using the internal regex
diff --git a/src/parse.h b/src/parse.h
index 42a2aff..188ac28 100644
--- a/src/parse.h
+++ b/src/parse.h
@@ -28,9 +28,6 @@ typedef struct {
int git_parse_ctx_init(git_parse_ctx *ctx, const char *content, size_t content_len);
void git_parse_ctx_clear(git_parse_ctx *ctx);
-#define git_parse_err(...) \
- ( git_error_set(GIT_ERROR_PATCH, __VA_ARGS__), -1 )
-
#define git_parse_ctx_contains_s(ctx, str) \
git_parse_ctx_contains(ctx, str, sizeof(str) - 1)
diff --git a/src/patch_parse.c b/src/patch_parse.c
index 29dc8b8..84953ee 100644
--- a/src/patch_parse.c
+++ b/src/patch_parse.c
@@ -33,6 +33,18 @@ typedef struct {
char *old_prefix, *new_prefix;
} git_patch_parsed;
+static int git_parse_err(const char *fmt, ...) GIT_FORMAT_PRINTF(1, 2);
+static int git_parse_err(const char *fmt, ...)
+{
+ va_list ap;
+
+ va_start(ap, fmt);
+ git_error_vset(GIT_ERROR_PATCH, fmt, ap);
+ va_end(ap);
+
+ return -1;
+}
+
static size_t header_path_len(git_patch_parse_ctx *ctx)
{
bool inquote = 0;
diff --git a/src/unix/posix.h b/src/unix/posix.h
index f969f83..d1f9024 100644
--- a/src/unix/posix.h
+++ b/src/unix/posix.h
@@ -59,7 +59,7 @@ GIT_INLINE(int) p_fsync(int fd)
#define p_strcasecmp(s1, s2) strcasecmp(s1, s2)
#define p_strncasecmp(s1, s2, c) strncasecmp(s1, s2, c)
#define p_vsnprintf(b, c, f, a) vsnprintf(b, c, f, a)
-#define p_snprintf(b, c, ...) snprintf(b, c, __VA_ARGS__)
+#define p_snprintf snprintf
#define p_mkstemp(p) mkstemp(p)
#define p_chdir(p) chdir(p)
#define p_chmod(p,m) chmod(p, m)