diff mbox

[5/9] Introduce selftest::locate_file (v4)

Message ID 1478898935-46932-6-git-send-email-dmalcolm@redhat.com
State New
Headers show

Commit Message

David Malcolm Nov. 11, 2016, 9:15 p.m. UTC
Link to discussion of earlier version of patch:
  https://gcc.gnu.org/ml/gcc-patches/2016-10/msg00260.html

On Wed, 2016-10-05 at 18:10 +0200, Bernd Schmidt wrote:
> On 10/05/2016 06:15 PM, David Malcolm wrote:
> >  selftest: s-selftest
> >  s-selftest: $(GCC_PASSES) cc1$(exeext) stmp-int-hdrs
> > -	$(GCC_FOR_TARGET) -xc -S -c /dev/null -fself-test
> > +	$(GCC_FOR_TARGET) -xc -S -c /dev/null -fself
> > -test=$(srcdir)
> >  	$(STAMP) $@
>
> I suspect the Makefile parts will need updating once Thomas commits
> his
> change.

Updated.

> Also,
>
> > +  return concat (path_to_src_gcc, "/testsuite/selftests/", name,
> > NULL);
>
> why hardcode directory names here? I think this should just be passed
> as
> a full pathname so only the Makefiles have knowledge of source
> directory
> structure.

Done, renaming path_to_src_gcc to path_to_selftest_files.

I also added a dep on selftest data dir to s-selftests in Makefile.in, so
that edits to the data mean re-running the selftests.

gcc/ChangeLog:
	* Makefile.in (SELFTEST_FLAGS): Add path argument to -fself-test.
	(s-selftest): Add dependency on the selftests data directory.
	* common.opt (fself-test): Rename to...
	(fself-test=): ...this, documenting the meaning of the argument.
	* selftest-run-tests.c (along): Likewise.
	* 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_c_tests): Likewise.
	(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

Comments

Bernd Schmidt Dec. 1, 2016, 1:29 p.m. UTC | #1
On 11/11/2016 10:15 PM, David Malcolm wrote:
> +  /* 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;
> +

What kind of dependency are you avoiding? If it's just one include I'd 
prefer to get rid of the extraneous variable.

Otherwise ok.


Bernd
David Malcolm Dec. 2, 2016, 1:19 a.m. UTC | #2
On Thu, 2016-12-01 at 14:29 +0100, Bernd Schmidt wrote:
> On 11/11/2016 10:15 PM, David Malcolm wrote:
> > +  /* 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;
> > +
> 
> What kind of dependency are you avoiding? If it's just one include
> I'd 
> prefer to get rid of the extraneous variable.

I was thinking more about keeping selftest.h/c modularized; I didn't
want them knowing anything about gcc option-handling, in case we want
to move the core of the selftests into some other support directory
(libiberty or similar).

Perhaps a better approach is to add the path as a param to locate_file,
to use it from callers.  That would support multiple subprojects using
selftest::locate_file (avoiding a global variable), at the slight cost
of requiring the use of flag_self_test (actually a macro) everywhere in
gcc that we use locate_file.

Should I update the patches to reflect that?

> Otherwise ok.
> 
> 
> Bernd
>
David Malcolm Dec. 8, 2016, 9:47 p.m. UTC | #3
On Thu, 2016-12-01 at 14:29 +0100, Bernd Schmidt wrote:
> On 11/11/2016 10:15 PM, David Malcolm wrote:
> > +  /* 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;
> > +
> 
> What kind of dependency are you avoiding? If it's just one include
> I'd 
> prefer to get rid of the extraneous variable.
> 
> Otherwise ok.

I attempted to eliminate the global, and include "options.h" from
selftest.c, and to use flag_self_test directly (implicitly accessing
the global_options global).

The *code* changes were relatively simple, with only one extra
#include.

However, from a linker perspective, it's more awkward.  selftest.o is
used by (amongst other things) OBJS-libcommon: libcommon.a currently
has selftest.o, but doesn't have options.o.  Various things in
libcommon.a use selftest.o for selftests and thus libcommon.a needs
selftest.o.

Hence doing so brings in a dependency on the option-handling objects
into libcommon.a, which seems wrong to me.

It seems simplest to go with either:

(A) the approach in the patch, with an extra global, keeping the
selftest code independent from gcc's option-handling code (both at
compile time and link time) or

(B) add the path as an extra param, passing it in at all callsites,
so that every
   locate_file (some_path)
in the kit becomes
   locate_file (flag_self_test, some_path)

Is (A) OK, or would you prefer (B)?   (I prefer (A) fwiw)

Thanks
Dave
Bernd Schmidt Dec. 9, 2016, 1:48 a.m. UTC | #4
On 12/08/2016 10:47 PM, David Malcolm wrote:

> Is (A) OK, or would you prefer (B)?   (I prefer (A) fwiw)

Oh well, just keep it as it is.


Bernd
diff mbox

Patch

diff --git a/gcc/Makefile.in b/gcc/Makefile.in
index 7ecd1e4..fa54215 100644
--- a/gcc/Makefile.in
+++ b/gcc/Makefile.in
@@ -1882,15 +1882,19 @@  rest.cross: specs
 # Specify -o /dev/null so the output of -S is discarded. More importantly
 # It does not try to create a file with the name "null.s" on POSIX and
 # "nul.s" on Windows. Because on Windows "nul" is a reserved file name.
-SELFTEST_FLAGS = -nostdinc -x c /dev/null -S -fself-test -o /dev/null
+# Specify the path to gcc/testsuite/selftests within the srcdir
+# as an argument to -fself-test.
+SELFTEST_FLAGS = -nostdinc -x c /dev/null -S -o /dev/null \
+	-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 314145a..e868c36 100644
--- a/gcc/common.opt
+++ b/gcc/common.opt
@@ -2131,9 +2131,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 a4cdb55..c1cd97e 100644
--- a/gcc/selftest-run-tests.c
+++ b/gcc/selftest-run-tests.c
@@ -24,6 +24,7 @@  along with GCC; see the file COPYING3.  If not see
 #include "tree.h"
 #include "target.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
@@ -38,6 +39,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 dcce474..c390873 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