From c6526dc91eba1fbc4c1997299580f00aa2c8c354 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Mon, 23 Oct 2017 16:08:19 -0400 Subject: [PATCH 07/24] FIXME: start adding selftests for cp/constexpr.c --- gcc/Makefile.in | 3 ++- gcc/cp/constexpr.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ gcc/cp/cp-lang.c | 1 + gcc/cp/cp-tree.h | 3 +++ gcc/fold-const.c | 42 ++++++++++++++++++++++++++++++++++ 5 files changed, 114 insertions(+), 1 deletion(-) diff --git a/gcc/Makefile.in b/gcc/Makefile.in index 2809619..badaa89 100644 --- a/gcc/Makefile.in +++ b/gcc/Makefile.in @@ -1924,7 +1924,8 @@ CPP_SELFTEST_DEPS = cc1plus$(exeext) $(SELFTEST_DEPS) .PHONY: selftest # By default, only run the selftests within the C frontend -selftest: s-selftest-c +# FIXME: hacking in C++ for now: +selftest: s-selftest-c s-selftest-c++ # C selftests s-selftest-c: $(C_SELFTEST_DEPS) diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 066d73d..08aaff8 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -33,6 +33,7 @@ along with GCC; see the file COPYING3. If not see #include "ubsan.h" #include "gimple-fold.h" #include "timevar.h" +#include "selftest.h" static bool verify_constant (tree, bool, bool *, bool *); #define VERIFY_CONSTANT(X) \ @@ -5962,4 +5963,69 @@ fini_constexpr (void) fundef_copies_table = NULL; } +#if CHECKING_P + +namespace selftest { + +/* FIXME. */ + +static void +test_cxx_constant_value () +{ +#if 0 + tree type = integer_type_node; + tree eight = build_int_cst (type, 8); + + ASSERT_EQ (eight, cxx_constant_value (eight, NULL_TREE)); + + const location_t some_loc = BUILTINS_LOCATION; + tree wrapped = maybe_wrap_with_location (eight, some_loc); + ASSERT_EQ (LOCATION_WRAPPER_EXPR, TREE_CODE (wrapped)); +#if 1 + ASSERT_EQ (eight, cxx_constant_value (wrapped, NULL_TREE)); +#else + ASSERT_EQ (wrapped, cxx_constant_value (wrapped, NULL_TREE)); +#endif + + hash_map map; + constexpr_ctx ctx = { NULL, &map, NULL, NULL, NULL, NULL, false, true }; + ASSERT_EQ (eight, cxx_eval_constant_expression (&ctx, eight, false, + NULL, NULL, NULL)); + + tree nop_around_wrapper = build1_loc (some_loc, NOP_EXPR, type, wrapped); + bool non_constant_p; + ASSERT_EQ (NOP_EXPR, TREE_CODE (nop_around_wrapper)); + ASSERT_EQ (eight, cxx_constant_value (nop_around_wrapper, NULL_TREE)); + ASSERT_EQ (nop_around_wrapper, + cxx_eval_constant_expression (&ctx, nop_around_wrapper, false, + &non_constant_p, NULL, NULL)); + + /* "1024 / 64", with NOP_EXPR and LOCATION_WRAPPER_EXPR around the + constants. */ + tree division = build2_loc (some_loc, TRUNC_DIV_EXPR, type, + build1_loc (some_loc, NOP_EXPR, type, + build1_loc (some_loc, LOCATION_WRAPPER_EXPR, type, + build_int_cst (type, 1024))), + build1_loc (some_loc, NOP_EXPR, type, + build1_loc (some_loc, LOCATION_WRAPPER_EXPR, type, + build_int_cst (type, 64)))); + tree cv = cxx_constant_value (division, NULL_TREE); + ASSERT_EQ (INTEGER_CST, TREE_CODE (cv)); + + // FIXME: add the above as (new) selftests for fold-const.c also (for "fold") +#endif +} + +/* Run all of the selftests within this file. */ + +void +constexpr_c_tests () +{ + test_cxx_constant_value (); +} + +} // namespace selftest + +#endif /* CHECKING_P */ + #include "gt-cp-constexpr.h" diff --git a/gcc/cp/cp-lang.c b/gcc/cp/cp-lang.c index 805319a..29e10b2 100644 --- a/gcc/cp/cp-lang.c +++ b/gcc/cp/cp-lang.c @@ -247,6 +247,7 @@ run_cp_tests (void) c_family_tests (); /* Additional C++-specific tests. */ + constexpr_c_tests (); } } // namespace selftest diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 254999a..817ccc4 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7355,6 +7355,9 @@ extern void cp_ubsan_maybe_initialize_vtbl_ptrs (tree); #if CHECKING_P namespace selftest { extern void run_cp_tests (void); + + extern void constexpr_c_tests (); + } // namespace selftest #endif /* #if CHECKING_P */ diff --git a/gcc/fold-const.c b/gcc/fold-const.c index c16959b..0346997 100644 --- a/gcc/fold-const.c +++ b/gcc/fold-const.c @@ -14394,6 +14394,47 @@ test_vector_folding () ASSERT_FALSE (integer_nonzerop (fold_build2 (NE_EXPR, res_type, one, one))); } +/* FIXME. */ + +static void +test_location_wrapper_folding () +{ +#if 0 + tree type = integer_type_node; + tree eight = build_int_cst (type, 8); + + ASSERT_EQ (eight, fold (eight)); + + const location_t some_loc = BUILTINS_LOCATION; + tree wrapped = maybe_wrap_with_location (eight, some_loc); + ASSERT_EQ (LOCATION_WRAPPER_EXPR, TREE_CODE (wrapped)); +#if 1 + ASSERT_EQ (eight, fold (wrapped)); +#else + ASSERT_EQ (wrapped, fold (wrapped)); +#endif + + tree nop_around_wrapper = build1_loc (some_loc, NOP_EXPR, type, wrapped); + ASSERT_EQ (NOP_EXPR, TREE_CODE (nop_around_wrapper)); + ASSERT_EQ (eight, fold (nop_around_wrapper)); + + /* "1024 / 64", with NOP_EXPR and LOCATION_WRAPPER_EXPR around the + constants. */ + tree division = build2_loc (some_loc, TRUNC_DIV_EXPR, type, + build1_loc (some_loc, NOP_EXPR, type, + build1_loc (some_loc, LOCATION_WRAPPER_EXPR, type, + build_int_cst (type, 1024))), + build1_loc (some_loc, NOP_EXPR, type, + build1_loc (some_loc, LOCATION_WRAPPER_EXPR, type, + build_int_cst (type, 64)))); + tree fd = fold (division); + ASSERT_EQ (INTEGER_CST, TREE_CODE (fd)); + + // FIXME: maybe integrate with constexpr.c tests?? +#endif +} + + /* Run all of the selftests within this file. */ void @@ -14401,6 +14442,7 @@ fold_const_c_tests () { test_arithmetic_folding (); test_vector_folding (); + test_location_wrapper_folding (); } } // namespace selftest -- 1.8.5.3