From 209a1d58bfae08874961c3d46b9573fc2226018c Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sat, 27 Aug 2016 09:06:21 -0700 Subject: [PATCH] Get rid of macros --- fmt/format.h | 97 --------------------------------------------- test/CMakeLists.txt | 1 - test/format-test.cc | 13 ------ test/macro-test.cc | 76 ----------------------------------- 4 files changed, 187 deletions(-) delete mode 100644 test/macro-test.cc diff --git a/fmt/format.h b/fmt/format.h index 64dabf16..b32cb53d 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -2169,55 +2169,6 @@ class BasicFormatter : private internal::FormatterBase { const Char *format(const Char *&format_str, const internal::Arg &arg); }; -// Generates a comma-separated list with results of applying f to -// numbers 0..n-1. -# define FMT_GEN(n, f) FMT_GEN##n(f) -# define FMT_GEN1(f) f(0) -# define FMT_GEN2(f) FMT_GEN1(f), f(1) -# define FMT_GEN3(f) FMT_GEN2(f), f(2) -# define FMT_GEN4(f) FMT_GEN3(f), f(3) -# define FMT_GEN5(f) FMT_GEN4(f), f(4) -# define FMT_GEN6(f) FMT_GEN5(f), f(5) -# define FMT_GEN7(f) FMT_GEN6(f), f(6) -# define FMT_GEN8(f) FMT_GEN7(f), f(7) -# define FMT_GEN9(f) FMT_GEN8(f), f(8) -# define FMT_GEN10(f) FMT_GEN9(f), f(9) -# define FMT_GEN11(f) FMT_GEN10(f), f(10) -# define FMT_GEN12(f) FMT_GEN11(f), f(11) -# define FMT_GEN13(f) FMT_GEN12(f), f(12) -# define FMT_GEN14(f) FMT_GEN13(f), f(13) -# define FMT_GEN15(f) FMT_GEN14(f), f(14) - -# define FMT_MAKE_TEMPLATE_ARG(n) typename T##n -# define FMT_MAKE_ARG_TYPE(n) T##n -# define FMT_MAKE_ARG(n) const T##n &v##n -# define FMT_ASSIGN_char(n) \ - arr[n] = fmt::internal::MakeValue< fmt::BasicFormatter >(v##n) -# define FMT_ASSIGN_wchar_t(n) \ - arr[n] = fmt::internal::MakeValue< fmt::BasicFormatter >(v##n) - -// Generates a comma-separated list with results of applying f to pairs -// (argument, index). -#define FMT_FOR_EACH1(f, x0) f(x0, 0) -#define FMT_FOR_EACH2(f, x0, x1) \ - FMT_FOR_EACH1(f, x0), f(x1, 1) -#define FMT_FOR_EACH3(f, x0, x1, x2) \ - FMT_FOR_EACH2(f, x0 ,x1), f(x2, 2) -#define FMT_FOR_EACH4(f, x0, x1, x2, x3) \ - FMT_FOR_EACH3(f, x0, x1, x2), f(x3, 3) -#define FMT_FOR_EACH5(f, x0, x1, x2, x3, x4) \ - FMT_FOR_EACH4(f, x0, x1, x2, x3), f(x4, 4) -#define FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5) \ - FMT_FOR_EACH5(f, x0, x1, x2, x3, x4), f(x5, 5) -#define FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6) \ - FMT_FOR_EACH6(f, x0, x1, x2, x3, x4, x5), f(x6, 6) -#define FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7) \ - FMT_FOR_EACH7(f, x0, x1, x2, x3, x4, x5, x6), f(x7, 7) -#define FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8) \ - FMT_FOR_EACH8(f, x0, x1, x2, x3, x4, x5, x6, x7), f(x8, 8) -#define FMT_FOR_EACH10(f, x0, x1, x2, x3, x4, x5, x6, x7, x8, x9) \ - FMT_FOR_EACH9(f, x0, x1, x2, x3, x4, x5, x6, x7, x8), f(x9, 9) - /** An error returned by an operating system or a language runtime, for example a file opening error. @@ -3338,54 +3289,6 @@ void arg(WStringRef, const internal::NamedArg&) FMT_DELETED_OR_UNDEFINED; # pragma GCC system_header #endif -// This is used to work around VC++ bugs in handling variadic macros. -#define FMT_EXPAND(args) args - -// Returns the number of arguments. -// Based on https://groups.google.com/forum/#!topic/comp.std.c/d-6Mj5Lko_s. -#define FMT_NARG(...) FMT_NARG_(__VA_ARGS__, FMT_RSEQ_N()) -#define FMT_NARG_(...) FMT_EXPAND(FMT_ARG_N(__VA_ARGS__)) -#define FMT_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, N, ...) N -#define FMT_RSEQ_N() 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 - -#define FMT_FOR_EACH_(N, f, ...) \ - FMT_EXPAND(FMT_CONCAT(FMT_FOR_EACH, N)(f, __VA_ARGS__)) -#define FMT_FOR_EACH(f, ...) \ - FMT_EXPAND(FMT_FOR_EACH_(FMT_NARG(__VA_ARGS__), f, __VA_ARGS__)) - -#define FMT_ADD_ARG_NAME(type, index) type arg##index -#define FMT_GET_ARG_NAME(type, index) arg##index - -#define FMT_VARIADIC_(Char, ReturnType, func, call, ...) \ - template \ - ReturnType func(FMT_FOR_EACH(FMT_ADD_ARG_NAME, __VA_ARGS__), \ - const Args & ... args) { \ - auto store = fmt::internal::make_format_args< fmt::BasicFormatter >(args...); \ - call(FMT_FOR_EACH(FMT_GET_ARG_NAME, __VA_ARGS__), fmt::format_args(store)); \ - } - -#define FMT_CAPTURE_ARG_(id, index) ::fmt::arg(#id, id) - -#define FMT_CAPTURE_ARG_W_(id, index) ::fmt::arg(L###id, id) - -/** - \rst - Convenient macro to capture the arguments' names and values into several - ``fmt::arg(name, value)``. - - **Example**:: - - int x = 1, y = 2; - print("point: ({x}, {y})", FMT_CAPTURE(x, y)); - // same as: - // print("point: ({x}, {y})", arg("x", x), arg("y", y)); - - \endrst - */ -#define FMT_CAPTURE(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_, __VA_ARGS__) - -#define FMT_CAPTURE_W(...) FMT_FOR_EACH(FMT_CAPTURE_ARG_W_, __VA_ARGS__) - namespace fmt { namespace internal { template diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 95ddca55..f657337c 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -88,7 +88,6 @@ add_fmt_test(printf-test) add_fmt_test(string-test) add_fmt_test(time-test) add_fmt_test(util-test mock-allocator.h) -add_fmt_test(macro-test) add_fmt_test(custom-formatter-test) # Enable stricter options for one test to make sure that the header is free of diff --git a/test/format-test.cc b/test/format-test.cc index ae01dfe2..8a8df1a5 100644 --- a/test/format-test.cc +++ b/test/format-test.cc @@ -606,22 +606,9 @@ TEST(FormatterTest, ManyArgs) { TEST(FormatterTest, NamedArg) { EXPECT_EQ("1/a/A", format("{_1}/{a_}/{A_}", fmt::arg("a_", 'a'), fmt::arg("A_", "A"), fmt::arg("_1", 1))); - char a = 'A', b = 'B', c = 'C'; - EXPECT_EQ("BB/AA/CC", format("{1}{b}/{0}{a}/{2}{c}", FMT_CAPTURE(a, b, c))); - EXPECT_EQ(" A", format("{a:>2}", FMT_CAPTURE(a))); - EXPECT_THROW_MSG(format("{a+}", FMT_CAPTURE(a)), format_error, - "missing '}' in format string"); EXPECT_THROW_MSG(format("{a}"), format_error, "argument not found"); - EXPECT_THROW_MSG(format("{d}", FMT_CAPTURE(a, b, c)), format_error, - "argument not found"); - EXPECT_THROW_MSG(format("{a}{}", FMT_CAPTURE(a)), - format_error, "cannot switch from manual to automatic argument indexing"); - EXPECT_THROW_MSG(format("{}{a}", FMT_CAPTURE(a)), - format_error, "cannot switch from automatic to manual argument indexing"); EXPECT_EQ(" -42", format("{0:{width}}", -42, fmt::arg("width", 4))); EXPECT_EQ("st", format("{0:.{precision}}", "str", fmt::arg("precision", 2))); - int n = 100; - EXPECT_EQ(L"n=100", format(L"n={n}", FMT_CAPTURE_W(n))); } TEST(FormatterTest, AutoArgIndex) { diff --git a/test/macro-test.cc b/test/macro-test.cc deleted file mode 100644 index 7882e8c5..00000000 --- a/test/macro-test.cc +++ /dev/null @@ -1,76 +0,0 @@ -/* - Tests of variadic function emulation macros. - - Copyright (c) 2012-2014, Victor Zverovich - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions are met: - - 1. Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - 2. Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - - THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include -#include - -#define FMT_USE_VARIADIC_TEMPLATES 0 -#include "fmt/format.h" - -#define IDENTITY(x) x - -TEST(UtilTest, Gen) { - int values[] = {FMT_GEN(10, IDENTITY)}; - for (int i = 0; i < 10; ++i) - EXPECT_EQ(i, values[i]); -} - -#define MAKE_PAIR(x, y) std::make_pair(x, y) - -TEST(UtilTest, ForEach) { - std::pair values[] = { - FMT_FOR_EACH10(MAKE_PAIR, 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j') - }; - for (int i = 0; i < 10; ++i) { - EXPECT_EQ('a' + i, values[i].first); - EXPECT_EQ(i, values[i].second); - } -} - -TEST(UtilTest, NArg) { - EXPECT_EQ(1, FMT_NARG(a)); - EXPECT_EQ(2, FMT_NARG(a, b)); - EXPECT_EQ(3, FMT_NARG(a, b, c)); - EXPECT_EQ(4, FMT_NARG(a, b, c, d)); - EXPECT_EQ(5, FMT_NARG(a, b, c, d, e)); - EXPECT_EQ(6, FMT_NARG(a, b, c, d, e, f)); - EXPECT_EQ(7, FMT_NARG(a, b, c, d, e, f, g)); - EXPECT_EQ(8, FMT_NARG(a, b, c, d, e, f, g, h)); - EXPECT_EQ(9, FMT_NARG(a, b, c, d, e, f, g, h, i)); - EXPECT_EQ(10, FMT_NARG(a, b, c, d, e, f, g, h, i, j)); -} - -int result; - -#define MAKE_TEST(func) \ - void func(const char *, const fmt::format_args &args) { \ - result = 0; \ - for (unsigned i = 0; args[i].type; ++i) \ - result += args[i].int_value; \ - } - -MAKE_TEST(test_func)