Bug 1369622 - Fix MOZ_FOR_EACH with an empty list. r?froydnj draft
authorMike Hommey <mh+mozilla@glandium.org>
Fri, 02 Jun 2017 14:53:54 +0900
changeset 588094 b357be8b7773a94dbf31c49974cde0da311aebeb
parent 587906 b138d2f271fdb598bf8a66c2dcb7fe391ca2a96f
child 588095 6947d24ec141c30cdc59ac98f1a6535b58853b01
push id61910
push userbmo:mh+mozilla@glandium.org
push dateFri, 02 Jun 2017 06:13:28 +0000
reviewersfroydnj
bugs1369622, 1368932
milestone55.0a1
Bug 1369622 - Fix MOZ_FOR_EACH with an empty list. r?froydnj I'm not sure how I tested MOZ_FOR_EACH in bug 1368932, but it turns out it doesn't work with an empty list, despite MOZ_PASTE_PREFIX_AND_ARG_COUNT now supporting 0 arguments. Macros can be tricky, and it ends up being easier to make things work cross-compiler with a separate macro that does the counting, and (re)building MOZ_PASTE_PREFIX_AND_ARG_COUNT on top of that. Then MOZ_FOR_EACH ends up working as expected with an empty list. So this adds a MOZ_ARG_COUNT macro that counts the number of variadic arguments it's given, and derives MOZ_PASTE_PREFIX_AND_ARG_COUNT from it. And this adds a testcase validating that MOZ_FOR_EACH works properly with an empty list as a result.
mfbt/MacroArgs.h
mfbt/tests/TestMacroArgs.cpp
mfbt/tests/TestMacroForEach.cpp
--- a/mfbt/MacroArgs.h
+++ b/mfbt/MacroArgs.h
@@ -11,65 +11,74 @@
 #ifndef mozilla_MacroArgs_h
 #define mozilla_MacroArgs_h
 
 // Concatenates pre-processor tokens in a way that can be used with __LINE__.
 #define MOZ_CONCAT2(x, y) x ## y
 #define MOZ_CONCAT(x, y) MOZ_CONCAT2(x, y)
 
 /*
+ * MOZ_ARG_COUNT(...) counts the number of variadic arguments.
+ * You must pass in between 0 and 50 (inclusive) variadic arguments.
+ * For example:
+ *
+ *   MOZ_ARG_COUNT() expands to 0
+ *   MOZ_ARG_COUNT(a) expands to 1
+ *   MOZ_ARG_COUNT(a, b) expands to 2
+ *
+ * Implementation notes:
+ * The `##__VA_ARGS__` form is a GCC extension that removes the comma if
+ * __VA_ARGS__ is empty. It is supported by Clang too. MSVC ignores ##,
+ * and its default behavior is already to strip the comma when __VA_ARGS__
+ * is empty.
+ *
+ * So MOZ_MACROARGS_ARG_COUNT_HELPER() expands to
+ *   (_, 50, 49, ...)
+ * MOZ_MACROARGS_ARG_COUNT_HELPER(a) expands to
+ *   (_, a, 50, 49, ...)
+ * etc.
+ */
+#define MOZ_ARG_COUNT(...) \
+  MOZ_MACROARGS_ARG_COUNT_HELPER2(MOZ_MACROARGS_ARG_COUNT_HELPER(__VA_ARGS__))
+
+#define MOZ_MACROARGS_ARG_COUNT_HELPER(...) (_, ##__VA_ARGS__, \
+    50, 49, 48, 47, 46, 45, 44, 43, 42, 41, \
+    40, 39, 38, 37, 36, 35, 34, 33, 32, 31, \
+    30, 29, 28, 27, 26, 25, 24, 23, 22, 21, \
+    20, 19, 18, 17, 16, 15, 14, 13, 12, 11, \
+    10, 9,  8,  7,  6,  5,  4,  3,  2,  1, 0)
+
+#define MOZ_MACROARGS_ARG_COUNT_HELPER2(aArgs) \
+  MOZ_MACROARGS_ARG_COUNT_HELPER3 aArgs
+
+#define MOZ_MACROARGS_ARG_COUNT_HELPER3(a0, \
+   a1,  a2,  a3,  a4,  a5,  a6,  a7,  a8,  a9, a10, \
+  a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, \
+  a21, a22, a23, a24, a25, a26, a27, a28, a29, a30, \
+  a31, a32, a33, a34, a35, a36, a37, a38, a39, a40, \
+  a41, a42, a43, a44, a45, a46, a47, a48, a49, a50, \
+  a51, ...) a51
+
+/*
  * MOZ_PASTE_PREFIX_AND_ARG_COUNT(aPrefix, ...) counts the number of variadic
  * arguments and prefixes it with |aPrefix|. For example:
  *
  *   MOZ_PASTE_PREFIX_AND_ARG_COUNT(, foo, 42) expands to 2
  *   MOZ_PASTE_PREFIX_AND_ARG_COUNT(A, foo, 42, bar) expands to A3
  *   MOZ_PASTE_PREFIX_AND_ARG_COUNT(A) expands to A0
  *   MOZ_PASTE_PREFIX_AND_ARG_COUNT() expands to 0, but MSVC warns there
  *   aren't enough arguments given.
  *
  * You must pass in between 0 and 50 (inclusive) variadic arguments, past
  * |aPrefix|.
- *
- * The `##__VA_ARGS__` form is a GCC extension that removes the comma if
- * __VA_ARGS__ is empty. It is supported by Clang too. MSVC ignores ##,
- * and its default behavior is already to strip the comma when __VA_ARGS__
- * is empty.
- *
- * So MOZ_MACROARGS_ARG_COUNT_HELPER(prefix) expands to
- *   (_, prefix50, prefix49, ...)
- * MOZ_MACROARGS_ARG_COUNT_HELPER(prefix, a) expands to
- *   (_, a, prefix50, prefix49, ...)
- * etc.
  */
+#define MOZ_PASTE_PREFIX_AND_ARG_COUNT_GLUE(a, b) a b
 #define MOZ_PASTE_PREFIX_AND_ARG_COUNT(aPrefix, ...) \
-  MOZ_MACROARGS_ARG_COUNT_HELPER2( \
-    MOZ_MACROARGS_ARG_COUNT_HELPER(aPrefix, ##__VA_ARGS__))
-
-#define MOZ_MACROARGS_ARG_COUNT_HELPER(aPrefix, ...) (_, ##__VA_ARGS__, \
-    aPrefix##50, aPrefix##49, aPrefix##48, aPrefix##47, aPrefix##46, \
-    aPrefix##45, aPrefix##44, aPrefix##43, aPrefix##42, aPrefix##41, \
-    aPrefix##40, aPrefix##39, aPrefix##38, aPrefix##37, aPrefix##36, \
-    aPrefix##35, aPrefix##34, aPrefix##33, aPrefix##32, aPrefix##31, \
-    aPrefix##30, aPrefix##29, aPrefix##28, aPrefix##27, aPrefix##26, \
-    aPrefix##25, aPrefix##24, aPrefix##23, aPrefix##22, aPrefix##21, \
-    aPrefix##20, aPrefix##19, aPrefix##18, aPrefix##17, aPrefix##16, \
-    aPrefix##15, aPrefix##14, aPrefix##13, aPrefix##12, aPrefix##11, \
-    aPrefix##10, aPrefix##9,  aPrefix##8,  aPrefix##7,  aPrefix##6,  \
-    aPrefix##5,  aPrefix##4,  aPrefix##3,  aPrefix##2,  aPrefix##1, aPrefix##0)
-
-#define MOZ_MACROARGS_ARG_COUNT_HELPER2(aArgs) \
-  MOZ_MACROARGS_ARG_COUNT_HELPER3 aArgs
-
-#define MOZ_MACROARGS_ARG_COUNT_HELPER3(a0, \
-   a1,  a2,  a3,  a4,  a5,  a6,  a7,  a8,  a9, a10, \
-  a11, a12, a13, a14, a15, a16, a17, a18, a19, a20, \
-  a21, a22, a23, a24, a25, a26, a27, a28, a29, a30, \
-  a31, a32, a33, a34, a35, a36, a37, a38, a39, a40, \
-  a41, a42, a43, a44, a45, a46, a47, a48, a49, a50, \
-  a51, ...) a51
+  MOZ_PASTE_PREFIX_AND_ARG_COUNT_GLUE( \
+    MOZ_CONCAT, (aPrefix, MOZ_ARG_COUNT(__VA_ARGS__)))
 
 /*
  * MOZ_ARGS_AFTER_N expands to its arguments excluding the first |N|
  * arguments. For example:
  *
  *   MOZ_ARGS_AFTER_2(a, b, c, d) expands to: c, d
  */
 #define MOZ_ARGS_AFTER_1(a1, ...) __VA_ARGS__
--- a/mfbt/tests/TestMacroArgs.cpp
+++ b/mfbt/tests/TestMacroArgs.cpp
@@ -1,16 +1,21 @@
 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
 /* This Source Code Form is subject to the terms of the Mozilla Public
  * License, v. 2.0. If a copy of the MPL was not distributed with this
  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
 
 #include "mozilla/MacroArgs.h"
 
+static_assert(MOZ_ARG_COUNT() == 0, "");
+static_assert(MOZ_ARG_COUNT(a) == 1, "");
+static_assert(MOZ_ARG_COUNT(a, b) == 2, "");
+static_assert(MOZ_ARG_COUNT(a, b, c) == 3, "");
+
 static_assert(MOZ_PASTE_PREFIX_AND_ARG_COUNT(100) == 1000, "");
 static_assert(MOZ_PASTE_PREFIX_AND_ARG_COUNT(100, a) == 1001, "");
 static_assert(MOZ_PASTE_PREFIX_AND_ARG_COUNT(100, a, b) == 1002, "");
 static_assert(MOZ_PASTE_PREFIX_AND_ARG_COUNT(100, a, b, c) == 1003, "");
 
 static_assert(MOZ_PASTE_PREFIX_AND_ARG_COUNT(, a, b, c) == 3, "");
 static_assert(MOZ_PASTE_PREFIX_AND_ARG_COUNT(, a) == 1, "");
 static_assert(MOZ_PASTE_PREFIX_AND_ARG_COUNT(, !a) == 1, "");
--- a/mfbt/tests/TestMacroForEach.cpp
+++ b/mfbt/tests/TestMacroForEach.cpp
@@ -11,16 +11,19 @@
 #define HELPER_IDENTITY_PLUS(x) x +
 static_assert(MOZ_FOR_EACH(HELPER_IDENTITY_PLUS, (), (10)) 0 == 10, "");
 static_assert(MOZ_FOR_EACH(HELPER_IDENTITY_PLUS, (), (1, 1, 1)) 0 == 3, "");
 static_assert(MOZ_FOR_EACH_SEPARATED(HELPER_IDENTITY, (+),
                                      (), (10)) == 10, "");
 static_assert(MOZ_FOR_EACH_SEPARATED(HELPER_IDENTITY, (+),
                                      (), (1, 1, 1)) == 3, "");
 
+#define HELPER_ONE_PLUS(x) HELPER_IDENTITY_PLUS(1)
+static_assert(MOZ_FOR_EACH(HELPER_ONE_PLUS, (), ()) 0 == 0, "");
+
 #define HELPER_DEFINE_VAR(x) const int test1_##x = x;
 MOZ_FOR_EACH(HELPER_DEFINE_VAR, (), (10, 20))
 static_assert(test1_10 == 10 && test1_20 == 20, "");
 
 #define HELPER_DEFINE_VAR2(k, x) const int test2_##x = k + x;
 MOZ_FOR_EACH(HELPER_DEFINE_VAR2, (5,), (10, 20))
 static_assert(test2_10 == 15 && test2_20 == 25, "");