Skip to content

Commit b9dbcf4

Browse files
committed
Homogenize printf formatting
1 parent a7914d1 commit b9dbcf4

File tree

6 files changed

+59
-48
lines changed

6 files changed

+59
-48
lines changed

cutils.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -180,8 +180,7 @@ int dbuf_putstr(DynBuf *s, const char *str)
180180
return dbuf_put(s, (const uint8_t *)str, strlen(str));
181181
}
182182

183-
int __attribute__((format(printf, 2, 3))) dbuf_printf(DynBuf *s,
184-
const char *fmt, ...)
183+
int JS_PRINTF_FORMAT_ATTR(2, 3) dbuf_printf(DynBuf *s, JS_PRINTF_FORMAT const char *fmt, ...)
185184
{
186185
va_list ap;
187186
char buf[128];

cutils.h

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,6 @@ extern "C" {
7171
# define __maybe_unused __attribute__((unused))
7272
#endif
7373

74-
// https://stackoverflow.com/a/6849629
75-
#undef FORMAT_STRING
76-
#if _MSC_VER >= 1400
77-
# include <sal.h>
78-
# if _MSC_VER > 1400
79-
# define FORMAT_STRING(p) _Printf_format_string_ p
80-
# else
81-
# define FORMAT_STRING(p) __format_string p
82-
# endif /* FORMAT_STRING */
83-
#else
84-
# define FORMAT_STRING(p) p
85-
#endif /* _MSC_VER */
86-
8774
#if defined(_MSC_VER) && !defined(__clang__)
8875
#include <math.h>
8976
#define INF INFINITY
@@ -113,6 +100,24 @@ extern "C" {
113100
#define minimum_length(n) static n
114101
#endif
115102

103+
/* Borrowed from Folly */
104+
#ifndef JS_PRINTF_FORMAT
105+
#ifdef _MSC_VER
106+
#include <sal.h>
107+
#define JS_PRINTF_FORMAT _Printf_format_string_
108+
#define JS_PRINTF_FORMAT_ATTR(format_param, dots_param)
109+
#else
110+
#define JS_PRINTF_FORMAT
111+
#if !defined(__clang__) && defined(__GNUC__)
112+
#define JS_PRINTF_FORMAT_ATTR(format_param, dots_param) \
113+
__attribute__((format(gnu_printf, format_param, dots_param)))
114+
#else
115+
#define JS_PRINTF_FORMAT_ATTR(format_param, dots_param) \
116+
__attribute__((format(printf, format_param, dots_param)))
117+
#endif
118+
#endif
119+
#endif
120+
116121
void js__pstrcpy(char *buf, int buf_size, const char *str);
117122
char *js__pstrcat(char *buf, int buf_size, const char *s);
118123
int js__strstart(const char *str, const char *val, const char **ptr);
@@ -445,8 +450,7 @@ static inline int dbuf_put_u64(DynBuf *s, uint64_t val)
445450
{
446451
return dbuf_put(s, (uint8_t *)&val, 8);
447452
}
448-
int __attribute__((format(printf, 2, 3))) dbuf_printf(DynBuf *s,
449-
FORMAT_STRING(const char *fmt), ...);
453+
int JS_PRINTF_FORMAT_ATTR(2, 3) dbuf_printf(DynBuf *s, JS_PRINTF_FORMAT const char *fmt, ...);
450454
void dbuf_free(DynBuf *s);
451455
static inline bool dbuf_error(DynBuf *s) {
452456
return s->error;

libregexp.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -374,7 +374,7 @@ static void re_emit_op_u16(REParseState *s, int op, uint32_t val)
374374
dbuf_put_u16(&s->byte_code, val);
375375
}
376376

377-
static int __attribute__((format(printf, 2, 3))) re_parse_error(REParseState *s, const char *fmt, ...)
377+
static int JS_PRINTF_FORMAT_ATTR(2, 3) re_parse_error(REParseState *s, const char *fmt, ...)
378378
{
379379
va_list ap;
380380
va_start(ap, fmt);

qjs.c

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -251,14 +251,7 @@ static inline unsigned long long js_trace_malloc_ptr_offset(uint8_t *ptr,
251251
return ptr - dp->base;
252252
}
253253

254-
static void
255-
#if defined(_WIN32) && !defined(__clang__)
256-
/* mingw printf is used */
257-
__attribute__((format(gnu_printf, 2, 3)))
258-
#else
259-
__attribute__((format(printf, 2, 3)))
260-
#endif
261-
js_trace_malloc_printf(void *opaque, const char *fmt, ...)
254+
static void JS_PRINTF_FORMAT_ATTR(2, 3) js_trace_malloc_printf(void *opaque, JS_PRINTF_FORMAT const char *fmt, ...)
262255
{
263256
va_list ap;
264257
int c;

quickjs.c

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1093,7 +1093,7 @@ static __exception int JS_ToArrayLengthFree(JSContext *ctx, uint32_t *plen,
10931093
JSValue val, bool is_array_ctor);
10941094
static JSValue JS_EvalObject(JSContext *ctx, JSValue this_obj,
10951095
JSValue val, int flags, int scope_idx);
1096-
JSValue __attribute__((format(printf, 2, 3))) JS_ThrowInternalError(JSContext *ctx, const char *fmt, ...);
1096+
JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowInternalError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
10971097

10981098
static __maybe_unused void JS_DumpString(JSRuntime *rt, const JSString *p);
10991099
static __maybe_unused void JS_DumpObjectHeader(JSRuntime *rt);
@@ -6882,7 +6882,7 @@ static JSValue JS_ThrowError(JSContext *ctx, JSErrorEnum error_num,
68826882
return JS_ThrowError2(ctx, error_num, fmt, ap, add_backtrace);
68836883
}
68846884

6885-
JSValue __attribute__((format(printf, 2, 3))) JS_ThrowPlainError(JSContext *ctx, const char *fmt, ...)
6885+
JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowPlainError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...)
68866886
{
68876887
JSValue val;
68886888
va_list ap;
@@ -6893,7 +6893,7 @@ JSValue __attribute__((format(printf, 2, 3))) JS_ThrowPlainError(JSContext *ctx,
68936893
return val;
68946894
}
68956895

6896-
JSValue __attribute__((format(printf, 2, 3))) JS_ThrowSyntaxError(JSContext *ctx, const char *fmt, ...)
6896+
JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowSyntaxError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...)
68976897
{
68986898
JSValue val;
68996899
va_list ap;
@@ -6904,7 +6904,7 @@ JSValue __attribute__((format(printf, 2, 3))) JS_ThrowSyntaxError(JSContext *ctx
69046904
return val;
69056905
}
69066906

6907-
JSValue __attribute__((format(printf, 2, 3))) JS_ThrowTypeError(JSContext *ctx, const char *fmt, ...)
6907+
JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowTypeError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...)
69086908
{
69096909
JSValue val;
69106910
va_list ap;
@@ -6915,7 +6915,7 @@ JSValue __attribute__((format(printf, 2, 3))) JS_ThrowTypeError(JSContext *ctx,
69156915
return val;
69166916
}
69176917

6918-
static int __attribute__((format(printf, 3, 4))) JS_ThrowTypeErrorOrFalse(JSContext *ctx, int flags, const char *fmt, ...)
6918+
static int JS_PRINTF_FORMAT_ATTR(3, 4) JS_ThrowTypeErrorOrFalse(JSContext *ctx, int flags, JS_PRINTF_FORMAT const char *fmt, ...)
69196919
{
69206920
va_list ap;
69216921

@@ -6931,15 +6931,15 @@ static int __attribute__((format(printf, 3, 4))) JS_ThrowTypeErrorOrFalse(JSCont
69316931
}
69326932

69336933
/* never use it directly */
6934-
static JSValue __attribute__((format(printf, 3, 4))) __JS_ThrowTypeErrorAtom(JSContext *ctx, JSAtom atom, const char *fmt, ...)
6934+
static JSValue JS_PRINTF_FORMAT_ATTR(3, 4) __JS_ThrowTypeErrorAtom(JSContext *ctx, JSAtom atom, JS_PRINTF_FORMAT const char *fmt, ...)
69356935
{
69366936
char buf[ATOM_GET_STR_BUF_SIZE];
69376937
return JS_ThrowTypeError(ctx, fmt,
69386938
JS_AtomGetStr(ctx, buf, sizeof(buf), atom));
69396939
}
69406940

69416941
/* never use it directly */
6942-
static JSValue __attribute__((format(printf, 3, 4))) __JS_ThrowSyntaxErrorAtom(JSContext *ctx, JSAtom atom, const char *fmt, ...)
6942+
static JSValue JS_PRINTF_FORMAT_ATTR(3, 4) __JS_ThrowSyntaxErrorAtom(JSContext *ctx, JSAtom atom, JS_PRINTF_FORMAT const char *fmt, ...)
69436943
{
69446944
char buf[ATOM_GET_STR_BUF_SIZE];
69456945
return JS_ThrowSyntaxError(ctx, fmt,
@@ -6962,7 +6962,7 @@ static int JS_ThrowTypeErrorReadOnly(JSContext *ctx, int flags, JSAtom atom)
69626962
}
69636963
}
69646964

6965-
JSValue __attribute__((format(printf, 2, 3))) JS_ThrowReferenceError(JSContext *ctx, const char *fmt, ...)
6965+
JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowReferenceError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...)
69666966
{
69676967
JSValue val;
69686968
va_list ap;
@@ -6973,7 +6973,7 @@ JSValue __attribute__((format(printf, 2, 3))) JS_ThrowReferenceError(JSContext *
69736973
return val;
69746974
}
69756975

6976-
JSValue __attribute__((format(printf, 2, 3))) JS_ThrowRangeError(JSContext *ctx, const char *fmt, ...)
6976+
JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowRangeError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...)
69776977
{
69786978
JSValue val;
69796979
va_list ap;
@@ -6984,7 +6984,7 @@ JSValue __attribute__((format(printf, 2, 3))) JS_ThrowRangeError(JSContext *ctx,
69846984
return val;
69856985
}
69866986

6987-
JSValue __attribute__((format(printf, 2, 3))) JS_ThrowInternalError(JSContext *ctx, const char *fmt, ...)
6987+
JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowInternalError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...)
69886988
{
69896989
JSValue val;
69906990
va_list ap;
@@ -19034,7 +19034,7 @@ static void __attribute((unused)) dump_token(JSParseState *s,
1903419034
}
1903519035
}
1903619036

19037-
int __attribute__((format(printf, 2, 3))) js_parse_error(JSParseState *s, const char *fmt, ...)
19037+
int JS_PRINTF_FORMAT_ATTR(2, 3) js_parse_error(JSParseState *s, JS_PRINTF_FORMAT const char *fmt, ...)
1903819038
{
1903919039
JSContext *ctx = s->ctx;
1904019040
va_list ap;
@@ -34647,7 +34647,7 @@ typedef struct BCReaderState {
3464734647
} BCReaderState;
3464834648

3464934649
#ifdef DUMP_READ_OBJECT
34650-
static void __attribute__((format(printf, 2, 3))) bc_read_trace(BCReaderState *s, const char *fmt, ...) {
34650+
static void JS_PRINTF_FORMAT_ATTR(2, 3) bc_read_trace(BCReaderState *s, JS_PRINTF_FORMAT const char *fmt, ...) {
3465134651
va_list ap;
3465234652
int i, n, n0;
3465334653

@@ -49924,7 +49924,7 @@ static int isURIReserved(int c) {
4992449924
return c < 0x100 && memchr(";/?:@&=+$,#", c, sizeof(";/?:@&=+$,#") - 1) != NULL;
4992549925
}
4992649926

49927-
static int __attribute__((format(printf, 2, 3))) js_throw_URIError(JSContext *ctx, const char *fmt, ...)
49927+
static int JS_PRINTF_FORMAT_ATTR(2, 3) js_throw_URIError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...)
4992849928
{
4992949929
va_list ap;
4993049930

quickjs.h

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,14 +39,30 @@ extern "C" {
3939

4040
#if defined(__GNUC__) || defined(__clang__)
4141
#define js_force_inline inline __attribute__((always_inline))
42-
#define __js_printf_like(f, a) __attribute__((format(printf, f, a)))
4342
#define JS_EXTERN __attribute__((visibility("default")))
4443
#else
4544
#define js_force_inline inline
46-
#define __js_printf_like(a, b)
4745
#define JS_EXTERN /* nothing */
4846
#endif
4947

48+
/* Borrowed from Folly */
49+
#ifndef JS_PRINTF_FORMAT
50+
#ifdef _MSC_VER
51+
#include <sal.h>
52+
#define JS_PRINTF_FORMAT _Printf_format_string_
53+
#define JS_PRINTF_FORMAT_ATTR(format_param, dots_param)
54+
#else
55+
#define JS_PRINTF_FORMAT
56+
#if !defined(__clang__) && defined(__GNUC__)
57+
#define JS_PRINTF_FORMAT_ATTR(format_param, dots_param) \
58+
__attribute__((format(gnu_printf, format_param, dots_param)))
59+
#else
60+
#define JS_PRINTF_FORMAT_ATTR(format_param, dots_param) \
61+
__attribute__((format(printf, format_param, dots_param)))
62+
#endif
63+
#endif
64+
#endif
65+
5066
typedef struct JSRuntime JSRuntime;
5167
typedef struct JSContext JSContext;
5268
typedef struct JSObject JSObject;
@@ -624,12 +640,12 @@ JS_EXTERN void JS_ClearUncatchableError(JSContext *ctx, JSValue val);
624640
// JS_Throw(ctx, exc);
625641
JS_EXTERN void JS_ResetUncatchableError(JSContext *ctx);
626642
JS_EXTERN JSValue JS_NewError(JSContext *ctx);
627-
JS_EXTERN JSValue __js_printf_like(2, 3) JS_ThrowPlainError(JSContext *ctx, const char *fmt, ...);
628-
JS_EXTERN JSValue __js_printf_like(2, 3) JS_ThrowSyntaxError(JSContext *ctx, const char *fmt, ...);
629-
JS_EXTERN JSValue __js_printf_like(2, 3) JS_ThrowTypeError(JSContext *ctx, const char *fmt, ...);
630-
JS_EXTERN JSValue __js_printf_like(2, 3) JS_ThrowReferenceError(JSContext *ctx, const char *fmt, ...);
631-
JS_EXTERN JSValue __js_printf_like(2, 3) JS_ThrowRangeError(JSContext *ctx, const char *fmt, ...);
632-
JS_EXTERN JSValue __js_printf_like(2, 3) JS_ThrowInternalError(JSContext *ctx, const char *fmt, ...);
643+
JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowPlainError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
644+
JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowSyntaxError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
645+
JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowTypeError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
646+
JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowReferenceError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
647+
JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowRangeError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
648+
JS_EXTERN JSValue JS_PRINTF_FORMAT_ATTR(2, 3) JS_ThrowInternalError(JSContext *ctx, JS_PRINTF_FORMAT const char *fmt, ...);
633649
JS_EXTERN JSValue JS_ThrowOutOfMemory(JSContext *ctx);
634650
JS_EXTERN void JS_FreeValue(JSContext *ctx, JSValue v);
635651
JS_EXTERN void JS_FreeValueRT(JSRuntime *rt, JSValue v);
@@ -1075,7 +1091,6 @@ JS_EXTERN uintptr_t js_std_cmd(int cmd, ...);
10751091

10761092
#undef JS_EXTERN
10771093
#undef js_force_inline
1078-
#undef __js_printf_like
10791094

10801095
#ifdef __cplusplus
10811096
} /* extern "C" { */

0 commit comments

Comments
 (0)