]> git.ipfire.org Git - thirdparty/gcc.git/commitdiff
selftest: invoke "diff" when ASSERT_STREQ fails
authorDavid Malcolm <dmalcolm@redhat.com>
Tue, 26 Nov 2024 21:09:37 +0000 (16:09 -0500)
committerDavid Malcolm <dmalcolm@redhat.com>
Tue, 26 Nov 2024 21:09:37 +0000 (16:09 -0500)
Currently when ASSERT_STREQ or ASSERT_STREQ_AT fail we print
both strings to stderr.  However it can be hard to figure out
the problem (e.g. for 1-character differences in long strings).

Extend the output by writing out the strings to tempfiles and
invoking "diff -up" on them when we have such a selftest failure,
to (I hope) simplify debugging.

gcc/ChangeLog:
* selftest.cc (selftest::print_diff): New function.
(selftest::assert_streq): Call it when we have non-equal
non-null strings.

Signed-off-by: David Malcolm <dmalcolm@redhat.com>
gcc/selftest.cc

index 15284c3359c4be8cc25ace3d343ec0525255001c..7ab7889cf8c5c12e029320543a77f88668fe3c02 100644 (file)
@@ -63,6 +63,26 @@ fail_formatted (const location &loc, const char *fmt, ...)
   abort ();
 }
 
+/* Invoke "diff" to print the difference between VAL1 and VAL2
+   on stdout.  */
+
+static void
+print_diff (const location &loc, const char *val1, const char *val2)
+{
+  temp_source_file tmpfile1 (loc, ".txt", val1);
+  temp_source_file tmpfile2 (loc, ".txt", val2);
+  const char *args[] = {"diff",
+                       "-up",
+                       tmpfile1.get_filename (),
+                       tmpfile2.get_filename (),
+                       NULL};
+  int exit_status = 0;
+  int err = 0;
+  pex_one (PEX_SEARCH | PEX_LAST,
+          args[0], CONST_CAST (char **, args),
+          NULL, NULL, NULL, &exit_status, &err);
+}
+
 /* Implementation detail of ASSERT_STREQ.
    Compare val1 and val2 with strcmp.  They ought
    to be non-NULL; fail gracefully if either or both are NULL.  */
@@ -89,8 +109,12 @@ assert_streq (const location &loc,
        if (strcmp (val1, val2) == 0)
          pass (loc, "ASSERT_STREQ");
        else
-         fail_formatted (loc, "ASSERT_STREQ (%s, %s)\n val1=\"%s\"\n val2=\"%s\"\n",
-                         desc_val1, desc_val2, val1, val2);
+         {
+           print_diff (loc, val1, val2);
+           fail_formatted
+             (loc, "ASSERT_STREQ (%s, %s)\n val1=\"%s\"\n val2=\"%s\"\n",
+              desc_val1, desc_val2, val1, val2);
+         }
       }
 }