From e162e130fcd323d144c7d7315b5a7c159b509e95 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Wed, 21 Sep 2016 15:17:39 -0400 Subject: [PATCH 11/20] FIXME: (partially-approved): Introduce selftest::locate_file FIXME: - update ChangeLog - Makefile.in: s-selftests dep fix Updated since v3: * integrate with Thomas' addition of SELFTEST_FLAGS * pass in the full path to testsuite/selftests from the Makefile, renaming path_to_src_gcc to path_to_selftest_files Jeff: > I do think we should go ahead and plan for a target subdirectory. With > that added, this should be OK once dependencies are in. Loading RTL dumps from files rather than embedding them as strings in the source requires a way to locate the relevant files. This patch adds a selftest::locate_file function for locating such files, relative to "$(SRCDIR)/gcc/testsuite/selftests". This is done via a new argument to -fself-test, which supplies the current value of "$(SRCDIR)/gcc" to cc1. I chose "$(SRCDIR)/gcc/testsuite/selftests", so as to be below gcc/testsuite, but not below any of the existing DejaGnu subdirectories, to avoid selftest-specific files from being picked up by .exp globbing patterns. (Depends on the approved-when-needed "[PATCH 2/9] Add selftest::read_file" https://gcc.gnu.org/ml/gcc-patches/2016-09/msg00476.html ). gcc/ChangeLog: * Makefile.in (SELFTEST_FLAGS): New variable. (s-selftest, selftest-gdb, selftest-valgrind): Use it. (SELFTEST_FLAGS): Add -fself-test=$(srcdir)/testsuite/selftests. * common.opt (fself-test): Rename to... (fself-test=): ...this, documenting the meaning of the argument. * selftest-run-tests.c: Include "options.h". (selftest::run_tests): Initialize selftest::path_to_selftest_files from flag_self_test. * selftest.c (selftest::path_to_selftest_files): New global. (selftest::locate_file): New function. (selftest::test_locate_file): New function. (selftest::selftest_c_tests): Call test_locate_file. * selftest.h (selftest::locate_file): New decl. (selftest::path_to_selftest_files): New decl. gcc/testsuite/ChangeLog: * gcc.dg/cpp/pr71591.c: Add a fake value for the argument of -fself-test. * selftests/example.txt: New file. --- gcc/Makefile.in | 10 +++++++--- gcc/common.opt | 6 +++--- gcc/selftest-run-tests.c | 8 ++++++++ gcc/selftest.c | 28 ++++++++++++++++++++++++++++ gcc/selftest.h | 10 ++++++++++ gcc/testsuite/gcc.dg/cpp/pr71591.c | 2 +- gcc/testsuite/selftests/example.txt | 1 + 7 files changed, 58 insertions(+), 7 deletions(-) create mode 100644 gcc/testsuite/selftests/example.txt diff --git a/gcc/Makefile.in b/gcc/Makefile.in index d6e44e4..b33836a 100644 --- a/gcc/Makefile.in +++ b/gcc/Makefile.in @@ -1876,15 +1876,19 @@ rest.cross: specs # Specify a dummy input file to placate the driver. # Specify -nostdinc to work around missing WIND_BASE environment variable # required for *-wrs-vxworks-* targets. -SELFTEST_FLAGS = -nostdinc -x c /dev/null -S -fself-test +# Specify the path to gcc/testsuite/selftests within the srcdir +# as an argument to -fself-test. +SELFTEST_FLAGS = -nostdinc -x c /dev/null -S \ + -fself-test=$(srcdir)/testsuite/selftests # Run the selftests during the build once we have a driver and a cc1, # so that self-test failures are caught as early as possible. # Use "s-selftest" to ensure that we only run the selftests if the -# driver or cc1 change. +# driver, cc1, or selftest data change. .PHONY: selftest selftest: s-selftest -s-selftest: $(GCC_PASSES) cc1$(exeext) stmp-int-hdrs +s-selftest: $(GCC_PASSES) cc1$(exeext) stmp-int-hdrs \ + $(srcdir)/testsuite/selftests $(GCC_FOR_TARGET) $(SELFTEST_FLAGS) $(STAMP) $@ diff --git a/gcc/common.opt b/gcc/common.opt index ce16a7e..b237328 100644 --- a/gcc/common.opt +++ b/gcc/common.opt @@ -2109,9 +2109,9 @@ fselective-scheduling2 Common Report Var(flag_selective_scheduling2) Optimization Run selective scheduling after reload. -fself-test -Common Undocumented Var(flag_self_test) -Run self-tests. +fself-test= +Common Undocumented Joined Var(flag_self_test) +Run self-tests, using the given path to locate test files. fsel-sched-pipelining Common Report Var(flag_sel_sched_pipelining) Init(0) Optimization diff --git a/gcc/selftest-run-tests.c b/gcc/selftest-run-tests.c index 54a9b0f..1b752d9 100644 --- a/gcc/selftest-run-tests.c +++ b/gcc/selftest-run-tests.c @@ -23,6 +23,7 @@ along with GCC; see the file COPYING3. If not see #include "selftest.h" #include "tree.h" #include "langhooks.h" +#include "options.h" /* This function needed to be split out from selftest.c as it references tests from the whole source tree, and so is within @@ -37,6 +38,13 @@ along with GCC; see the file COPYING3. If not see void selftest::run_tests () { + /* Makefile.in has -fself-test=$(srcdir)/testsuite/selftests, so that + flag_self_test contains the path to the selftest subdirectory of the + source tree (without a trailing slash). Copy it up to + path_to_selftest_files, to avoid selftest.c depending on + option-handling. */ + path_to_selftest_files = flag_self_test; + long start_time = get_run_time (); /* Run all the tests, in hand-coded order of (approximate) dependencies: diff --git a/gcc/selftest.c b/gcc/selftest.c index 2a729be..268fc36 100644 --- a/gcc/selftest.c +++ b/gcc/selftest.c @@ -198,6 +198,21 @@ read_file (const location &loc, const char *path) return result; } +/* The path of SRCDIR/testsuite/selftests. */ + +const char *path_to_selftest_files = NULL; + +/* Convert a path relative to SRCDIR/testsuite/selftests + to a real path (either absolute, or relative to pwd). + The result should be freed by the caller. */ + +char * +locate_file (const char *name) +{ + ASSERT_NE (NULL, path_to_selftest_files); + return concat (path_to_selftest_files, "/", name, NULL); +} + /* Selftests for the selftest system itself. */ /* Sanity-check the ASSERT_ macros with various passing cases. */ @@ -240,6 +255,18 @@ test_read_file () free (buf); } +/* Verify locate_file (and read_file). */ + +static void +test_locate_file () +{ + char *path = locate_file ("example.txt"); + char *buf = read_file (SELFTEST_LOCATION, path); + ASSERT_STREQ ("example of a selftest file\n", buf); + free (buf); + free (path); +} + /* Run all of the selftests within this file. */ void @@ -248,6 +275,7 @@ selftest_c_tests () test_assertions (); test_named_temp_file (); test_read_file (); + test_locate_file (); } } // namespace selftest diff --git a/gcc/selftest.h b/gcc/selftest.h index 845eb01..39284c1 100644 --- a/gcc/selftest.h +++ b/gcc/selftest.h @@ -158,6 +158,16 @@ extern char *read_file (const location &loc, const char *path); extern void forcibly_ggc_collect (); +/* Convert a path relative to SRCDIR/gcc/testsuite/selftests + to a real path (either absolute, or relative to pwd). + The result should be freed by the caller. */ + +extern char *locate_file (const char *path); + +/* The path of SRCDIR/testsuite/selftests. */ + +extern const char *path_to_selftest_files; + /* Declarations for specific families of tests (by source file), in alphabetical order. */ extern void bitmap_c_tests (); diff --git a/gcc/testsuite/gcc.dg/cpp/pr71591.c b/gcc/testsuite/gcc.dg/cpp/pr71591.c index e92cb52..0e3d7b1 100644 --- a/gcc/testsuite/gcc.dg/cpp/pr71591.c +++ b/gcc/testsuite/gcc.dg/cpp/pr71591.c @@ -1,5 +1,5 @@ /* PR rtl-optimization/71591 */ /* { dg-do preprocess } */ -/* { dg-options "-fself-test" } */ +/* { dg-options "-fself-test=fake-value" } */ /* { dg-message "self-tests incompatible with -E" "" { target *-*-* } 0 } */ diff --git a/gcc/testsuite/selftests/example.txt b/gcc/testsuite/selftests/example.txt new file mode 100644 index 0000000..fbfaa33 --- /dev/null +++ b/gcc/testsuite/selftests/example.txt @@ -0,0 +1 @@ +example of a selftest file -- 1.8.5.3