diff mbox series

[2/8] ccan/cppmagic: Fix NONEMPTY macro

Message ID 20180620053409.14538-3-alistair@popple.id.au
State Accepted
Headers show
Series Rework option parsing | expand

Commit Message

Alistair Popple June 20, 2018, 5:34 a.m. UTC
The original implementation of non-empty will fail to compile if passed an
argument of the form 'CPPMAGIC_NONEMPTY((...))'. Add a check for parentheses and
return true if passed.

Signed-off-by: Alistair Popple <alistair@popple.id.au>
---
 ccan/cppmagic/cppmagic.h | 10 ++++++++--
 1 file changed, 8 insertions(+), 2 deletions(-)
diff mbox series

Patch

diff --git a/ccan/cppmagic/cppmagic.h b/ccan/cppmagic/cppmagic.h
index f1f6868..587fe4e 100644
--- a/ccan/cppmagic/cppmagic.h
+++ b/ccan/cppmagic/cppmagic.h
@@ -68,9 +68,15 @@ 
  * CPPMAGIC_NONEMPTY(@a, ...)
  * 	expand to '1'
  */
-#define _CPPMAGIC_EOA()			0
+#define CHECK_N(x, n, ...) n
+#define CHECK(...) CHECK_N(__VA_ARGS__, , )
+#define PROBE(x) x, 1,
+#define IS_PAREN(x, ...) CHECK(IS_PAREN_PROBE x)
+#define IS_PAREN_PROBE(...) PROBE(~)
+
+#define _CPPMAGIC_EOA(a_, ...)			0
 #define CPPMAGIC_NONEMPTY(...)		\
-	CPPMAGIC_NONZERO(CPPMAGIC_1ST(_CPPMAGIC_EOA __VA_ARGS__)())
+	CPPMAGIC_NONZERO(CPPMAGIC_1ST(_CPPMAGIC_EOA IS_PAREN(__VA_ARGS__) __VA_ARGS__)())
 
 /**
  * CPPMAGIC_ISEMPTY - does the macro have no arguments?