[lxc-devel] [lxcfs/master] macro: fix lxcfs_{error, debug, v} build error when __VA_ARGS__ is empty

time-river on Github lxc-bot at linuxcontainers.org
Mon Aug 26 10:11:48 UTC 2019


A non-text attachment was scrubbed...
Name: not available
Type: text/x-mailbox
Size: 877 bytes
Desc: not available
URL: <http://lists.linuxcontainers.org/pipermail/lxc-devel/attachments/20190826/5d09f319/attachment.bin>
-------------- next part --------------
From 2a94c281e34f53867ed75fb4c3d00e7c814287d5 Mon Sep 17 00:00:00 2001
From: river <river at vvl.me>
Date: Mon, 26 Aug 2019 18:01:12 +0800
Subject: [PATCH] macro: fix lxcfs_{error,debug,v} build error when __VA_ARGS__
 is empty
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Originally, the compiler complained:
  macro.h:7:25: error: expected expression before ')' token
      __func__, __VA_ARGS__);

The reason is that GCC wouldn't abandon `,` when `__VA_ARGS__` is empty.
For emaple:
  #define eprintf(format, ...) fprintf (stderr, format, __VA_ARGS__)
  eprintf("success!\n", );
       → fprintf(stderr, "success!\n", );

According to GCC doc, it's okay when adding `##` before `__VA_ARGS__`:
  #define eprintf(format, ...) fprintf (stderr, format, ##__VA_ARGS__)
  eprintf ("success!\n")
       → fprintf(stderr, "success!\n");
---
 macro.h | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/macro.h b/macro.h
index 7213b8f..3e9ef82 100644
--- a/macro.h
+++ b/macro.h
@@ -4,19 +4,19 @@
 #define lxcfs_debug_stream(stream, format, ...)                                \
 	do {                                                                   \
 		fprintf(stream, "%s: %d: %s: " format, __FILE__, __LINE__,     \
-			__func__, __VA_ARGS__);                                \
+			__func__, ##__VA_ARGS__);                                \
 	} while (false)
 
-#define lxcfs_error(format, ...) lxcfs_debug_stream(stderr, format, __VA_ARGS__)
+#define lxcfs_error(format, ...) lxcfs_debug_stream(stderr, format, ##__VA_ARGS__)
 
 #ifdef DEBUG
-#define lxcfs_debug(format, ...) lxcfs_error(format, __VA_ARGS__)
+#define lxcfs_debug(format, ...) lxcfs_error(format, ##__VA_ARGS__)
 #else
 #define lxcfs_debug(format, ...)
 #endif /* DEBUG */
 
 #ifdef VERBOSE
-#define lxcfs_v(format, ...) lxcfs_error(format, __VA_ARGS__);
+#define lxcfs_v(format, ...) lxcfs_error(format, ##__VA_ARGS__);
 #else
 #define lxcfs_v(format, ...)
 #endif /* VERBOSE */


More information about the lxc-devel mailing list