diff mbox

PR bootstrap/71481: fix input.c selftest

Message ID 1465509519-4165-1-git-send-email-dmalcolm@redhat.com
State New
Headers show

Commit Message

David Malcolm June 9, 2016, 9:58 p.m. UTC
input.c's selftest::test_reading_source_line attempted to read from
__FILE__, which doesn't work if the binary is run from a different
location than the build dir.

Fix it by rewriting the test to write out a tempfile, and read from
that, rather than from __FILE__.

I used make_temp_file to create the name for the temporary file, on
the grounds that that's what the driver uses for that purpose.

This is on top of the patch kit posted as:
  https://gcc.gnu.org/ml/gcc-patches/2016-06/msg00735.html

Successfully bootstrapped&regretested on x86_64-pc-linux-gnu
Successful -fself-test of stage1 on powerpc-ibm-aix7.1.3.0

OK for trunk?

gcc/ChangeLog:
	PR bootstrap/71481
	* input.c (selftest::test_reading_source_line): Avoid reading from
	__FILE__ by creating a tempfile with known content and reading
	from that instead.
---
 gcc/input.c | 30 ++++++++++++++++++++----------
 1 file changed, 20 insertions(+), 10 deletions(-)

Comments

Richard Biener June 10, 2016, 11:32 a.m. UTC | #1
On Thu, Jun 9, 2016 at 11:58 PM, David Malcolm <dmalcolm@redhat.com> wrote:
> input.c's selftest::test_reading_source_line attempted to read from
> __FILE__, which doesn't work if the binary is run from a different
> location than the build dir.
>
> Fix it by rewriting the test to write out a tempfile, and read from
> that, rather than from __FILE__.
>
> I used make_temp_file to create the name for the temporary file, on
> the grounds that that's what the driver uses for that purpose.
>
> This is on top of the patch kit posted as:
>   https://gcc.gnu.org/ml/gcc-patches/2016-06/msg00735.html
>
> Successfully bootstrapped&regretested on x86_64-pc-linux-gnu
> Successful -fself-test of stage1 on powerpc-ibm-aix7.1.3.0
>
> OK for trunk?

Ok.

Richard.

> gcc/ChangeLog:
>         PR bootstrap/71481
>         * input.c (selftest::test_reading_source_line): Avoid reading from
>         __FILE__ by creating a tempfile with known content and reading
>         from that instead.
> ---
>  gcc/input.c | 30 ++++++++++++++++++++----------
>  1 file changed, 20 insertions(+), 10 deletions(-)
>
> diff --git a/gcc/input.c b/gcc/input.c
> index 704ee75..08019c9 100644
> --- a/gcc/input.c
> +++ b/gcc/input.c
> @@ -1219,23 +1219,33 @@ test_builtins ()
>  static void
>  test_reading_source_line ()
>  {
> -  /* We will read *this* source file, using __FILE__.
> -     Here is some specific text to read and test for:
> -     The quick brown fox jumps over the lazy dog.  */
> -  const int linenum_after_test_message = __LINE__;
> -  const int linenum = linenum_after_test_message - 1;
> -
> +  /* Create a tempfile and write some text to it.  */
> +  char *filename = make_temp_file (".txt");
> +  ASSERT_NE (filename, NULL);
> +  FILE *out = fopen (filename, "w");
> +  if (!out)
> +    ::selftest::fail_formatted (SELFTEST_LOCATION,
> +                               "unable to open tempfile: %s", filename);
> +  fprintf (out,
> +          "01234567890123456789\n"
> +          "This is the test text\n"
> +          "This is the 3rd line\n");
> +  fclose (out);
> +
> +  /* Read back a specific line from the tempfile.  */
>    int line_size;
> -  const char *source_line = location_get_source_line (__FILE__, linenum, &line_size);
> +  const char *source_line = location_get_source_line (filename, 2, &line_size);
>    ASSERT_TRUE (source_line != NULL);
> -  ASSERT_EQ (53, line_size);
> -  if (!strncmp ("     The quick brown fox jumps over the lazy dog.  */",
> -              source_line, line_size))
> +  ASSERT_EQ (21, line_size);
> +  if (!strncmp ("This is the test text",
> +               source_line, line_size))
>      ::selftest::pass (SELFTEST_LOCATION,
>                       "source_line matched expected value");
>    else
>      ::selftest::fail (SELFTEST_LOCATION,
>                       "source_line did not match expected value");
> +
> +  free (filename);
>  }
>
>  /* Run all of the selftests within this file.  */
> --
> 1.8.5.3
>
Jeff Law June 13, 2016, 6:19 p.m. UTC | #2
On 06/09/2016 03:58 PM, David Malcolm wrote:
> input.c's selftest::test_reading_source_line attempted to read from
> __FILE__, which doesn't work if the binary is run from a different
> location than the build dir.
>
> Fix it by rewriting the test to write out a tempfile, and read from
> that, rather than from __FILE__.
>
> I used make_temp_file to create the name for the temporary file, on
> the grounds that that's what the driver uses for that purpose.
>
> This is on top of the patch kit posted as:
>   https://gcc.gnu.org/ml/gcc-patches/2016-06/msg00735.html
>
> Successfully bootstrapped&regretested on x86_64-pc-linux-gnu
> Successful -fself-test of stage1 on powerpc-ibm-aix7.1.3.0
>
> OK for trunk?
>
> gcc/ChangeLog:
> 	PR bootstrap/71481
> 	* input.c (selftest::test_reading_source_line): Avoid reading from
> 	__FILE__ by creating a tempfile with known content and reading
> 	from that instead.
OK.

FWIW, I think the LANG_C vs translating is your call to make.  I can see 
arguments for both directions.

Jeff
diff mbox

Patch

diff --git a/gcc/input.c b/gcc/input.c
index 704ee75..08019c9 100644
--- a/gcc/input.c
+++ b/gcc/input.c
@@ -1219,23 +1219,33 @@  test_builtins ()
 static void
 test_reading_source_line ()
 {
-  /* We will read *this* source file, using __FILE__.
-     Here is some specific text to read and test for:
-     The quick brown fox jumps over the lazy dog.  */
-  const int linenum_after_test_message = __LINE__;
-  const int linenum = linenum_after_test_message - 1;
-
+  /* Create a tempfile and write some text to it.  */
+  char *filename = make_temp_file (".txt");
+  ASSERT_NE (filename, NULL);
+  FILE *out = fopen (filename, "w");
+  if (!out)
+    ::selftest::fail_formatted (SELFTEST_LOCATION,
+				"unable to open tempfile: %s", filename);
+  fprintf (out,
+	   "01234567890123456789\n"
+	   "This is the test text\n"
+	   "This is the 3rd line\n");
+  fclose (out);
+
+  /* Read back a specific line from the tempfile.  */
   int line_size;
-  const char *source_line = location_get_source_line (__FILE__, linenum, &line_size);
+  const char *source_line = location_get_source_line (filename, 2, &line_size);
   ASSERT_TRUE (source_line != NULL);
-  ASSERT_EQ (53, line_size);
-  if (!strncmp ("     The quick brown fox jumps over the lazy dog.  */",
-	       source_line, line_size))
+  ASSERT_EQ (21, line_size);
+  if (!strncmp ("This is the test text",
+		source_line, line_size))
     ::selftest::pass (SELFTEST_LOCATION,
 		      "source_line matched expected value");
   else
     ::selftest::fail (SELFTEST_LOCATION,
 		      "source_line did not match expected value");
+
+  free (filename);
 }
 
 /* Run all of the selftests within this file.  */