From 77e2a2ff7ba21aa5db494df9852e88c29c6325be Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Wed, 10 Jun 2015 06:07:15 -0400 Subject: [PATCH 09/35] Add selftests to gimple.c Jeff approved an earlier version of this (as unittests/test-gimple.c): https://gcc.gnu.org/ml/gcc-patches/2015-10/msg03304.html > Comment indicates addition. But code actually generates a > MULT_EXPR. Please fix. Fixed > OK if/when prereqs are approved. Minor twiddling if we end > up moving it elsewhere or standardizing/reducing header files > is pre-approved. This version moves the tests into gimple.c. gcc/ChangeLog: * gimple.c: Include "selftest.h". Include "gimple-pretty-print.h". (class gimple_test): New subclass. (gimple_test, assign_single): New selftest. (gimple_test, assign_binop): New selftest. (gimple_test, nop_stmt): New selftest. (gimple_test, return_stmt): New selftest. (gimple_test, return_without_value): New selftest. --- gcc/gimple.c | 103 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 103 insertions(+) diff --git a/gcc/gimple.c b/gcc/gimple.c index d822fab..b5d7391 100644 --- a/gcc/gimple.c +++ b/gcc/gimple.c @@ -38,6 +38,8 @@ along with GCC; see the file COPYING3. If not see #include "gimple-walk.h" #include "gimplify.h" #include "target.h" +#include "selftest.h" +#include "gimple-pretty-print.h" /* All the tuples have their operand vector (if present) at the very bottom @@ -3016,3 +3018,104 @@ maybe_remove_unused_call_args (struct function *fn, gimple *stmt) update_stmt_fn (fn, stmt); } } + +#if CHECKING_P + +namespace { + +class gimple_test : public ::selftest::test +{ + protected: + void + verify_gimple_pp (const char *expected, gimple *stmt) + { + pretty_printer pp; + pp_gimple_stmt_1 (&pp, stmt, 0 /* spc */, 0 /* flags */); + EXPECT_STREQ (expected, pp_formatted_text (&pp)); + } +}; + +TEST_F (gimple_test, assign_single) +{ + /* Build "tmp = 5;" */ + tree type = integer_type_node; + tree lhs = build_decl (UNKNOWN_LOCATION, VAR_DECL, + get_identifier ("tmp"), + type); + tree rhs = build_int_cst (type, 5); + gassign *stmt = gimple_build_assign (lhs, rhs); + verify_gimple_pp ("tmp = 5;", stmt); + + EXPECT_TRUE (is_gimple_assign (stmt)); + EXPECT_EQ (lhs, gimple_assign_lhs (stmt)); + EXPECT_EQ (lhs, gimple_get_lhs (stmt)); + EXPECT_EQ (rhs, gimple_assign_rhs1 (stmt)); + EXPECT_EQ (NULL, gimple_assign_rhs2 (stmt)); + EXPECT_EQ (NULL, gimple_assign_rhs3 (stmt)); + EXPECT_TRUE (gimple_assign_single_p (stmt)); + EXPECT_EQ (INTEGER_CST, gimple_assign_rhs_code (stmt)); +} + +TEST_F (gimple_test, assign_binop) +{ + /* Build "tmp = a * b;" */ + tree type = integer_type_node; + tree lhs = build_decl (UNKNOWN_LOCATION, VAR_DECL, + get_identifier ("tmp"), + type); + tree a = build_decl (UNKNOWN_LOCATION, VAR_DECL, + get_identifier ("a"), + type); + tree b = build_decl (UNKNOWN_LOCATION, VAR_DECL, + get_identifier ("b"), + type); + gassign *stmt = gimple_build_assign (lhs, MULT_EXPR, a, b); + verify_gimple_pp ("tmp = a * b;", stmt); + + EXPECT_TRUE (is_gimple_assign (stmt)); + EXPECT_EQ (lhs, gimple_assign_lhs (stmt)); + EXPECT_EQ (lhs, gimple_get_lhs (stmt)); + EXPECT_EQ (a, gimple_assign_rhs1 (stmt)); + EXPECT_EQ (b, gimple_assign_rhs2 (stmt)); + EXPECT_EQ (NULL, gimple_assign_rhs3 (stmt)); + EXPECT_FALSE (gimple_assign_single_p (stmt)); + EXPECT_EQ (MULT_EXPR, gimple_assign_rhs_code (stmt)); +} + +TEST_F (gimple_test, nop_stmt) +{ + gimple *stmt = gimple_build_nop (); + verify_gimple_pp ("GIMPLE_NOP", stmt); + EXPECT_EQ (GIMPLE_NOP, gimple_code (stmt)); + EXPECT_EQ (NULL, gimple_get_lhs (stmt)); + EXPECT_FALSE (gimple_assign_single_p (stmt)); +} + +TEST_F (gimple_test, return_stmt) +{ + /* Build "return 7;" */ + tree type = integer_type_node; + tree val = build_int_cst (type, 7); + greturn *stmt = gimple_build_return (val); + verify_gimple_pp ("return 7;", stmt); + + EXPECT_EQ (GIMPLE_RETURN, gimple_code (stmt)); + EXPECT_EQ (NULL, gimple_get_lhs (stmt)); + EXPECT_EQ (val, gimple_return_retval (stmt)); + EXPECT_FALSE (gimple_assign_single_p (stmt)); +} + +TEST_F (gimple_test, return_without_value) +{ + greturn *stmt = gimple_build_return (NULL); + verify_gimple_pp ("return;", stmt); + + EXPECT_EQ (GIMPLE_RETURN, gimple_code (stmt)); + EXPECT_EQ (NULL, gimple_get_lhs (stmt)); + EXPECT_EQ (NULL, gimple_return_retval (stmt)); + EXPECT_FALSE (gimple_assign_single_p (stmt)); +} + +} /* anon namespace. */ + +#endif /* CHECKING_P */ -- 1.8.5.3