[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [tor/master] lib/cc: Define the standard C macro pasting trick in one place.



commit 4541a590192a97d56c7a303492a2bc1bbeacb441
Author: Nick Mathewson <nickm@xxxxxxxxxxxxxx>
Date:   Mon Oct 28 09:05:02 2019 -0400

    lib/cc: Define the standard C macro pasting trick in one place.
    
    This file is a workaround for the issue that if you say `a ## b` to
    create a token that is the name of a macro, the C preprocessor won't
    expand that macro.  So you can't say this:
    
    #define FOO__SQUARE(x) ((x)*(x))
    #define FOO__CUBE(x)   ((x)*(x)*(x))
    #define FOO(func, x)   FOO__##func(x)
    
    Instead, the standard C trick is to add a layer of indirection:
    
    #define PASTE(a,b)     PASTE__(a,b)
    #define PASTE__(a,b)   a ## b
    
    #define FOO__SQUARE(x) ((x)*(x))
    #define FOO__CUBE(x)   ((x)*(x)*(x))
    #define FOO(func, x)   PASTE(FOO__, func)(x)
    
    We should use this kind of trick sparingly, since it gets confusing.
---
 src/lib/cc/include.am |  1 +
 src/lib/cc/tokpaste.h | 30 ++++++++++++++++++++++++++++++
 2 files changed, 31 insertions(+)

diff --git a/src/lib/cc/include.am b/src/lib/cc/include.am
index 1aa722dd8..d2a415e95 100644
--- a/src/lib/cc/include.am
+++ b/src/lib/cc/include.am
@@ -3,4 +3,5 @@
 noinst_HEADERS += \
 	src/lib/cc/compat_compiler.h \
 	src/lib/cc/ctassert.h \
+	src/lib/cc/tokpaste.h \
 	src/lib/cc/torint.h
diff --git a/src/lib/cc/tokpaste.h b/src/lib/cc/tokpaste.h
new file mode 100644
index 000000000..e7ddbffc6
--- /dev/null
+++ b/src/lib/cc/tokpaste.h
@@ -0,0 +1,30 @@
+/* Copyright (c) 2001 Matej Pfajfar.
+ * Copyright (c) 2001-2004, Roger Dingledine.
+ * Copyright (c) 2004-2006, Roger Dingledine, Nick Mathewson.
+ * Copyright (c) 2007-2019, The Tor Project, Inc. */
+/* See LICENSE for licensing information */
+
+/**
+ * @file tokpaste.h
+ * @brief Token-pasting macros.
+ **/
+
+#ifndef TOR_LIB_CC_TOKPASTE_H
+#define TOR_LIB_CC_TOKPASTE_H
+
+/**
+ * Concatenate `a` and `b` in a way that allows their result itself to be
+ * expanded by the preprocessor.
+ *
+ * Ordinarily you could just say `a ## b` in a macro definition.  But doing so
+ * results in a symbol which the preprocessor will not then expand.  If you
+ * wanted to use `a ## b` to create the name of a macro and have the
+ * preprocessor expand _that_ macro, you need to have another level of
+ * indirection, as this macro provides.
+ **/
+#define PASTE(a,b) PASTE__(a,b)
+
+/** Helper for PASTE(). */
+#define PASTE__(a,b) a ## b
+
+#endif /* !defined(TOR_LIB_CC_TOKPASTE_H) */



_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits