Skip to content

Commit 69a3b75

Browse files
committed
Merge branch 'ab/c99-variadic-macros'
Remove the escape hatch we added when we introduced the weather balloon to use variadic macros unconditionally, to make it official that we now have a hard dependency on the feature. * ab/c99-variadic-macros: C99: remove hardcoded-out !HAVE_VARIADIC_MACROS code git-compat-util.h: clarify GCC v.s. C99-specific in comment
2 parents 4763ccd + 56a29d2 commit 69a3b75

File tree

8 files changed

+65
-246
lines changed

8 files changed

+65
-246
lines changed

Documentation/CodingGuidelines

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,9 @@ For C programs:
217217
. since mid 2017 with 512f41cf, we have been using designated
218218
initializers for array (e.g. "int array[10] = { [5] = 2 }").
219219

220+
. since early 2021 with 765dc168882, we have been using variadic
221+
macros, mostly for printf-like trace and debug macros.
222+
220223
These used to be forbidden, but we have not heard any breakage
221224
report, and they are assumed to be safe.
222225

banned.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,8 @@
2121

2222
#undef sprintf
2323
#undef vsprintf
24-
#ifdef HAVE_VARIADIC_MACROS
2524
#define sprintf(...) BANNED(sprintf)
2625
#define vsprintf(...) BANNED(vsprintf)
27-
#else
28-
#define sprintf(buf,fmt,arg) BANNED(sprintf)
29-
#define vsprintf(buf,fmt,arg) BANNED(vsprintf)
30-
#endif
3126

3227
#undef gmtime
3328
#define gmtime(t) BANNED(gmtime)

git-compat-util.h

Lines changed: 1 addition & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -534,9 +534,7 @@ void warning_errno(const char *err, ...) __attribute__((format (printf, 1, 2)));
534534
/*
535535
* Let callers be aware of the constant return value; this can help
536536
* gcc with -Wuninitialized analysis. We restrict this trick to gcc, though,
537-
* because some compilers may not support variadic macros. Since we're only
538-
* trying to help gcc, anyway, it's OK; other compilers will fall back to
539-
* using the function as usual.
537+
* because other compilers may be confused by this.
540538
*/
541539
#if defined(__GNUC__)
542540
static inline int const_error(void)
@@ -1258,24 +1256,12 @@ static inline int regexec_buf(const regex_t *preg, const char *buf, size_t size,
12581256
#endif
12591257
#endif
12601258

1261-
/*
1262-
* This is always defined as a first step towards making the use of variadic
1263-
* macros unconditional. If it causes compilation problems on your platform,
1264-
* please report it to the Git mailing list at [email protected].
1265-
*/
1266-
#define HAVE_VARIADIC_MACROS 1
1267-
12681259
/* usage.c: only to be used for testing BUG() implementation (see test-tool) */
12691260
extern int BUG_exit_code;
12701261

1271-
#ifdef HAVE_VARIADIC_MACROS
12721262
__attribute__((format (printf, 3, 4))) NORETURN
12731263
void BUG_fl(const char *file, int line, const char *fmt, ...);
12741264
#define BUG(...) BUG_fl(__FILE__, __LINE__, __VA_ARGS__)
1275-
#else
1276-
__attribute__((format (printf, 1, 2))) NORETURN
1277-
void BUG(const char *fmt, ...);
1278-
#endif
12791265

12801266
/*
12811267
* Preserves errno, prints a message, but gives no warning for ENOENT.

trace.c

Lines changed: 2 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -108,16 +108,11 @@ static int prepare_trace_line(const char *file, int line,
108108
gettimeofday(&tv, NULL);
109109
secs = tv.tv_sec;
110110
localtime_r(&secs, &tm);
111-
strbuf_addf(buf, "%02d:%02d:%02d.%06ld ", tm.tm_hour, tm.tm_min,
112-
tm.tm_sec, (long) tv.tv_usec);
113-
114-
#ifdef HAVE_VARIADIC_MACROS
115-
/* print file:line */
116-
strbuf_addf(buf, "%s:%d ", file, line);
111+
strbuf_addf(buf, "%02d:%02d:%02d.%06ld %s:%d", tm.tm_hour, tm.tm_min,
112+
tm.tm_sec, (long) tv.tv_usec, file, line);
117113
/* align trace output (column 40 catches most files names in git) */
118114
while (buf->len < 40)
119115
strbuf_addch(buf, ' ');
120-
#endif
121116

122117
return 1;
123118
}
@@ -229,74 +224,6 @@ static void trace_performance_vprintf_fl(const char *file, int line,
229224
strbuf_release(&buf);
230225
}
231226

232-
#ifndef HAVE_VARIADIC_MACROS
233-
234-
void trace_printf(const char *format, ...)
235-
{
236-
va_list ap;
237-
va_start(ap, format);
238-
trace_vprintf_fl(NULL, 0, &trace_default_key, format, ap);
239-
va_end(ap);
240-
}
241-
242-
void trace_printf_key(struct trace_key *key, const char *format, ...)
243-
{
244-
va_list ap;
245-
va_start(ap, format);
246-
trace_vprintf_fl(NULL, 0, key, format, ap);
247-
va_end(ap);
248-
}
249-
250-
void trace_argv_printf(const char **argv, const char *format, ...)
251-
{
252-
va_list ap;
253-
va_start(ap, format);
254-
trace_argv_vprintf_fl(NULL, 0, argv, format, ap);
255-
va_end(ap);
256-
}
257-
258-
void trace_strbuf(struct trace_key *key, const struct strbuf *data)
259-
{
260-
trace_strbuf_fl(NULL, 0, key, data);
261-
}
262-
263-
void trace_performance(uint64_t nanos, const char *format, ...)
264-
{
265-
va_list ap;
266-
va_start(ap, format);
267-
trace_performance_vprintf_fl(NULL, 0, nanos, format, ap);
268-
va_end(ap);
269-
}
270-
271-
void trace_performance_since(uint64_t start, const char *format, ...)
272-
{
273-
va_list ap;
274-
va_start(ap, format);
275-
trace_performance_vprintf_fl(NULL, 0, getnanotime() - start,
276-
format, ap);
277-
va_end(ap);
278-
}
279-
280-
void trace_performance_leave(const char *format, ...)
281-
{
282-
va_list ap;
283-
uint64_t since;
284-
285-
if (perf_indent)
286-
perf_indent--;
287-
288-
if (!format) /* Allow callers to leave without tracing anything */
289-
return;
290-
291-
since = perf_start_times[perf_indent];
292-
va_start(ap, format);
293-
trace_performance_vprintf_fl(NULL, 0, getnanotime() - since,
294-
format, ap);
295-
va_end(ap);
296-
}
297-
298-
#else
299-
300227
void trace_printf_key_fl(const char *file, int line, struct trace_key *key,
301228
const char *format, ...)
302229
{
@@ -342,9 +269,6 @@ void trace_performance_leave_fl(const char *file, int line,
342269
va_end(ap);
343270
}
344271

345-
#endif /* HAVE_VARIADIC_MACROS */
346-
347-
348272
static const char *quote_crnl(const char *path)
349273
{
350274
static struct strbuf new_path = STRBUF_INIT;

trace.h

Lines changed: 58 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -126,71 +126,6 @@ void trace_command_performance(const char **argv);
126126
void trace_verbatim(struct trace_key *key, const void *buf, unsigned len);
127127
uint64_t trace_performance_enter(void);
128128

129-
#ifndef HAVE_VARIADIC_MACROS
130-
131-
/**
132-
* Prints a formatted message, similar to printf.
133-
*/
134-
__attribute__((format (printf, 1, 2)))
135-
void trace_printf(const char *format, ...);
136-
137-
__attribute__((format (printf, 2, 3)))
138-
void trace_printf_key(struct trace_key *key, const char *format, ...);
139-
140-
/**
141-
* Prints a formatted message, followed by a quoted list of arguments.
142-
*/
143-
__attribute__((format (printf, 2, 3)))
144-
void trace_argv_printf(const char **argv, const char *format, ...);
145-
146-
/**
147-
* Prints the strbuf, without additional formatting (i.e. doesn't
148-
* choke on `%` or even `\0`).
149-
*/
150-
void trace_strbuf(struct trace_key *key, const struct strbuf *data);
151-
152-
/**
153-
* Prints elapsed time (in nanoseconds) if GIT_TRACE_PERFORMANCE is enabled.
154-
*
155-
* Example:
156-
* ------------
157-
* uint64_t t = 0;
158-
* for (;;) {
159-
* // ignore
160-
* t -= getnanotime();
161-
* // code section to measure
162-
* t += getnanotime();
163-
* // ignore
164-
* }
165-
* trace_performance(t, "frotz");
166-
* ------------
167-
*/
168-
__attribute__((format (printf, 2, 3)))
169-
void trace_performance(uint64_t nanos, const char *format, ...);
170-
171-
/**
172-
* Prints elapsed time since 'start' if GIT_TRACE_PERFORMANCE is enabled.
173-
*
174-
* Example:
175-
* ------------
176-
* uint64_t start = getnanotime();
177-
* // code section to measure
178-
* trace_performance_since(start, "foobar");
179-
* ------------
180-
*/
181-
__attribute__((format (printf, 2, 3)))
182-
void trace_performance_since(uint64_t start, const char *format, ...);
183-
184-
__attribute__((format (printf, 1, 2)))
185-
void trace_performance_leave(const char *format, ...);
186-
187-
#else
188-
189-
/*
190-
* Macros to add file:line - see above for C-style declarations of how these
191-
* should be used.
192-
*/
193-
194129
/*
195130
* TRACE_CONTEXT may be set to __FUNCTION__ if the compiler supports it. The
196131
* default is __FILE__, as it is consistent with assert(), and static function
@@ -204,7 +139,10 @@ void trace_performance_leave(const char *format, ...);
204139
# define TRACE_CONTEXT __FILE__
205140
#endif
206141

207-
/*
142+
/**
143+
* Macros to add the file:line of the calling code, instead of that of
144+
* the trace function itself.
145+
*
208146
* Note: with C99 variadic macros, __VA_ARGS__ must include the last fixed
209147
* parameter ('format' in this case). Otherwise, a call without variable
210148
* arguments will have a surplus ','. E.g.:
@@ -220,35 +158,84 @@ void trace_performance_leave(const char *format, ...);
220158
* comma, but this is non-standard.
221159
*/
222160

161+
/**
162+
* trace_printf(), accepts "const char *format, ...".
163+
*
164+
* Prints a formatted message, similar to printf.
165+
*/
166+
#define trace_printf(...) trace_printf_key(&trace_default_key, __VA_ARGS__)
167+
168+
/**
169+
* trace_printf_key(), accepts "struct trace_key *key, const char *format, ...".
170+
*/
223171
#define trace_printf_key(key, ...) \
224172
do { \
225173
if (trace_pass_fl(key)) \
226174
trace_printf_key_fl(TRACE_CONTEXT, __LINE__, key, \
227175
__VA_ARGS__); \
228176
} while (0)
229177

230-
#define trace_printf(...) trace_printf_key(&trace_default_key, __VA_ARGS__)
231-
178+
/**
179+
* trace_argv_printf(), accepts "struct trace_key *key, const char *format, ...)".
180+
*
181+
* Prints a formatted message, followed by a quoted list of arguments.
182+
*/
232183
#define trace_argv_printf(argv, ...) \
233184
do { \
234185
if (trace_pass_fl(&trace_default_key)) \
235186
trace_argv_printf_fl(TRACE_CONTEXT, __LINE__, \
236187
argv, __VA_ARGS__); \
237188
} while (0)
238189

190+
/**
191+
* trace_strbuf(), accepts "struct trace_key *key, const struct strbuf *data".
192+
*
193+
* Prints the strbuf, without additional formatting (i.e. doesn't
194+
* choke on `%` or even `\0`).
195+
*/
239196
#define trace_strbuf(key, data) \
240197
do { \
241198
if (trace_pass_fl(key)) \
242199
trace_strbuf_fl(TRACE_CONTEXT, __LINE__, key, data);\
243200
} while (0)
244201

202+
/**
203+
* trace_performance(), accepts "uint64_t nanos, const char *format, ...".
204+
*
205+
* Prints elapsed time (in nanoseconds) if GIT_TRACE_PERFORMANCE is enabled.
206+
*
207+
* Example:
208+
* ------------
209+
* uint64_t t = 0;
210+
* for (;;) {
211+
* // ignore
212+
* t -= getnanotime();
213+
* // code section to measure
214+
* t += getnanotime();
215+
* // ignore
216+
* }
217+
* trace_performance(t, "frotz");
218+
* ------------
219+
*/
245220
#define trace_performance(nanos, ...) \
246221
do { \
247222
if (trace_pass_fl(&trace_perf_key)) \
248223
trace_performance_fl(TRACE_CONTEXT, __LINE__, nanos,\
249224
__VA_ARGS__); \
250225
} while (0)
251226

227+
/**
228+
* trace_performance_since(), accepts "uint64_t start, const char *format, ...".
229+
*
230+
* Prints elapsed time since 'start' if GIT_TRACE_PERFORMANCE is enabled.
231+
*
232+
* Example:
233+
* ------------
234+
* uint64_t start = getnanotime();
235+
* // code section to measure
236+
* trace_performance_since(start, "foobar");
237+
* ------------
238+
*/
252239
#define trace_performance_since(start, ...) \
253240
do { \
254241
if (trace_pass_fl(&trace_perf_key)) \
@@ -257,6 +244,9 @@ void trace_performance_leave(const char *format, ...);
257244
__VA_ARGS__); \
258245
} while (0)
259246

247+
/**
248+
* trace_performance_leave(), accepts "const char *format, ...".
249+
*/
260250
#define trace_performance_leave(...) \
261251
do { \
262252
if (trace_pass_fl(&trace_perf_key)) \
@@ -285,6 +275,4 @@ static inline int trace_pass_fl(struct trace_key *key)
285275
return key->fd || !key->initialized;
286276
}
287277

288-
#endif /* HAVE_VARIADIC_MACROS */
289-
290278
#endif /* TRACE_H */

0 commit comments

Comments
 (0)