]> git.draconx.ca Git - gob-dx.git/commitdiff
Improve str.gob testcase on non-GNU compilers.
authorNick Bowler <nbowler@draconx.ca>
Sat, 26 Feb 2022 03:55:02 +0000 (22:55 -0500)
committerNick Bowler <nbowler@draconx.ca>
Sat, 26 Feb 2022 03:55:02 +0000 (22:55 -0500)
Checking for -Wformat warnings from the compiler is not going to work
well on non-GNU toolchains, or even on old GNU toolchains where the
warning is not enabled by default, or if the user specified something
like -w in CFLAGS.

Adapt the test to check whether the compiler produces such warnings
and produce a skipped result if not.

tests/general.at

index 49fd6a2b8ff80303b45e2f66f884c7bb098dd57b..52c3fcac43209cfb096fdf8da35c3047a39e1049 100644 (file)
@@ -21,6 +21,30 @@ AT_CLEANUP
 
 AT_SETUP([str.gob])
 
+# check if compiler supports format warnings
+AT_DATA([fmt.c],
+[[#include <config.h>
+#include <glib.h>
+
+void foo(const char *, ...) G_GNUC_PRINTF(1, 2);
+void bar(void) {
+#line 99 "VERIFY"
+foo("%s", 0);
+}
+]])
+AT_DATA([fmt.awk],
+[[BEGIN { pass=0; FS=":"; }
+/%s/ && $1 == "VERIFY" && $2 == 99 { pass=1; }
+END { exit(!pass); }
+]])
+
+fmt_warnings=false
+set x $CPPFLAGS $CFLAGS $LIBGOBJECT_CFLAGS; shift
+AS_IF([$CC "$@" -c fmt.c 1>/dev/null 2>out && $AWK -f fmt.awk out],
+  [fmt_warnings=:],
+  [AS_IF([$CC "$@" -Wformat -c fmt.c 1>/dev/null 2>out && $AWK -f fmt.awk out],
+    [CFLAGS="$CFLAGS -Wformat" fmt_warnings=:])])
+
 AT_DATA([main.c],
 [[#include <config.h>
 #include "str.h"
@@ -48,25 +72,45 @@ int main(void)
 str_gob=$abs_top_srcdir/t/str.gob
 AT_CHECK([gob2 "$str_gob"])
 AT_CHECK([$HAVE_GOBJECT_PRIVATES || exit 77])
+
 TEST_COMPILE_GOBJECT([str.c], [0], [], [stderr])
 mv stderr str_stderr
+TEST_COMPILE_GOBJECT([main.c], [0], [], [stderr])
+mv stderr main_stderr
+AT_CHECK([$CC $CFLAGS $LDFLAGS $LIBGOBJECT_LIBS -o main str.o main.o])
 
-# Check for correct diagnostic messages on the target lines...
-AT_CHECK([$AWK '/want a string/ { print NR }' "$str_gob" >str_lines])
-total=0
-exec 3<str_lines
-while read l <&3; do
-  AS_VAR_ARITH([total], [1 + $total])
-  AT_CHECK([$AWK -v line="$l" -v file="$str_gob" -F : \
-    '$1 == file && $2 == line { exit 42 }' str_stderr], [42])
-done
-exec 3<&-
-AT_CHECK([test x"$total" = x"2"])
+AT_DATA([str.awk],
+[[/want a string/ { lines[NR] = 1; }
+END {
+  FS=":";
+  while ((rc = getline < "str_stderr") > 0) {
+    sub(/.*[\/]/, "", $1);
+    if (/%s/ && $1 == "str.gob" && $2 in lines)
+      lines[$2]--;
+  }
 
-TEST_COMPILE_GOBJECT([main.c], [0], [], [stderr])
-AT_CHECK([$AWK -F : '$1 == "main.c" && $2 == "17" { exit 42 }' stderr], [42])
+  if (rc < 0)
+    exit(1);
 
-AT_CHECK([$CC $CFLAGS $LDFLAGS $LIBGOBJECT_LIBS -o main str.o main.o])
+  count=0;
+  for (l in lines) {
+    if (lines[l])
+      exit(1);
+    count++;
+  }
+
+  exit(count != 2);
+}
+]])
+
+# Check for correct diagnostic messages on the target lines...
+AT_CHECK([$fmt_warnings || exit 77
+$AWK -f str.awk "$str_gob" || exit 1
+$AWK -f - main_stderr <<'EOF'
+BEGIN { pass=0; FS=":"; }
+/%d/ && $1 == "main.c" && $2 == 17 { pass=1; }
+END { exit(!pass); }
+EOF])
 
 AT_CLEANUP