From 0fa37d8e00c25512c985f2dccf07f69640831adb Mon Sep 17 00:00:00 2001
From: Andre Maroneze <andre.maroneze@cea.fr>
Date: Thu, 21 Sep 2023 18:24:07 +0200
Subject: [PATCH] [tests] rewrite test sed filter in Python for better
 portability

---
 nix/kernel-tests.nix                         |  3 +-
 tests/syntax/cpp-command.c                   | 18 -------
 tests/syntax/cpp-command.t/cpp-command.c     |  8 +++
 tests/syntax/cpp-command.t/filter.py         | 22 ++++++++
 tests/syntax/cpp-command.t/run.t             | 53 ++++++++++++++++++++
 tests/syntax/dune                            |  4 ++
 tests/syntax/oracle/cpp-command.0.res.oracle |  2 -
 tests/syntax/oracle/cpp-command.1.res.oracle |  2 -
 tests/syntax/oracle/cpp-command.2.res.oracle |  2 -
 tests/syntax/oracle/cpp-command.3.res.oracle |  2 -
 tests/syntax/oracle/cpp-command.4.res.oracle |  2 -
 tests/syntax/oracle/cpp-command.5.res.oracle | 21 --------
 tests/syntax/oracle/cpp-command.6.res.oracle |  7 ---
 13 files changed, 89 insertions(+), 57 deletions(-)
 delete mode 100644 tests/syntax/cpp-command.c
 create mode 100644 tests/syntax/cpp-command.t/cpp-command.c
 create mode 100644 tests/syntax/cpp-command.t/filter.py
 create mode 100644 tests/syntax/cpp-command.t/run.t
 create mode 100644 tests/syntax/dune
 delete mode 100644 tests/syntax/oracle/cpp-command.0.res.oracle
 delete mode 100644 tests/syntax/oracle/cpp-command.1.res.oracle
 delete mode 100644 tests/syntax/oracle/cpp-command.2.res.oracle
 delete mode 100644 tests/syntax/oracle/cpp-command.3.res.oracle
 delete mode 100644 tests/syntax/oracle/cpp-command.4.res.oracle
 delete mode 100644 tests/syntax/oracle/cpp-command.5.res.oracle
 delete mode 100644 tests/syntax/oracle/cpp-command.6.res.oracle

diff --git a/nix/kernel-tests.nix b/nix/kernel-tests.nix
index 92fe1d96eb4..f243ca6a08c 100644
--- a/nix/kernel-tests.nix
+++ b/nix/kernel-tests.nix
@@ -18,6 +18,7 @@ mk_tests {
       @src/kernel_internals/parsing/tests/ptests
     dune runtest -j1 --display short \
       src/plugins/server/tests/batch \
-      tests/fc_script
+      tests/fc_script \
+      tests/syntax
   '';
 }
diff --git a/tests/syntax/cpp-command.c b/tests/syntax/cpp-command.c
deleted file mode 100644
index 5d014018167..00000000000
--- a/tests/syntax/cpp-command.c
+++ /dev/null
@@ -1,18 +0,0 @@
-/* run.config*
-   FILTER: sed "s:/[^ ]*[/]cpp-command\.[^ ]*\.i:TMPDIR/FILE.i:g; s:[^ ]*[/]__fc_machdep.*\.dir:-ITMP_MACHDEP:g; s:$PWD/::g; s:$(realpath @FRAMAC_SHARE@)/:FRAMAC_SHARE/:g; s:@PTEST_MAKE_DIR@/result@PTEST_CONFIG@/::g; s: -m32::; s: -m64::"
-   OPT: -machdep x86_32 -cpp-frama-c-compliant -cpp-command "echo [\$(basename '%1') \$(basename '%1') \$(basename '%i') \$(basename '%input')] ['%2' '%2' '%o' '%output'] ['%args']"
-   OPT: -machdep x86_32 -cpp-frama-c-compliant -cpp-command "echo %%1 = \$(basename '%1') %%2 = '%2' %%args = '%args'"
-   OPT: -machdep x86_32 -cpp-frama-c-compliant -cpp-command "printf \"%s\n\" \"using \\% has no effect : \$(basename \"\%input\")\""
-   OPT: -machdep x86_32 -cpp-frama-c-compliant -cpp-command "echo %var is not an interpreted placeholder"
-   OPT: -machdep x86_32 -print-cpp-commands
-   OPT: -cpp-extra-args-per-file=@PTEST_FILE@:"-DPF=\\\"cp%02d_%.3f\\\"" -cpp-extra-args="-DPF2=\\\"cp%02d_%.3f\\\"" -no-autoload-plugins @PTEST_FILE@ -print
-   OPT: -cpp-extra-args-per-file=@PTEST_FILE@:"file_extra" -cpp-extra-args="global_extra" -cpp-command "echo 'extra_args: %args'" -no-autoload-plugins @PTEST_FILE@ -print
-   */
-#include <stdio.h>
-void printer(int i, float f) {
-  printf(PF, i, f);
-}
-
-int main() {
-  printer(1, 1.0);
-}
diff --git a/tests/syntax/cpp-command.t/cpp-command.c b/tests/syntax/cpp-command.t/cpp-command.c
new file mode 100644
index 00000000000..eeb024da489
--- /dev/null
+++ b/tests/syntax/cpp-command.t/cpp-command.c
@@ -0,0 +1,8 @@
+#include <stdio.h>
+void printer(int i, float f) {
+  printf(PF, i, f);
+}
+
+int main() {
+  printer(1, 1.0);
+}
diff --git a/tests/syntax/cpp-command.t/filter.py b/tests/syntax/cpp-command.t/filter.py
new file mode 100644
index 00000000000..e4b379acf65
--- /dev/null
+++ b/tests/syntax/cpp-command.t/filter.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python3
+
+""" Filter for the cpp-command test. Expects $FRAMAC_SHARE as its first argument. """
+
+import re
+import sys
+
+FRAMAC_SHARE = sys.argv[1]
+
+for line in sys.stdin:
+    # Apply several filters:
+    # - Remove preprocessed filename (randomly generated)
+    line = re.sub(r"/[^ ]*cpp-command.c......\.i", "<TMPDIR/PP>.i", line)
+    # - Remove hardcoded path to temporary __fc_machdepXXXXXX.dir
+    line = re.sub(r"-I.*__fc_machdep......\.dir", "-I<TMP_MACHDEP>", line)
+    # - Replace occurrence of FRAMAC_SHARE; re.escape is needed if the path
+    #   contains e.g. a '+' character
+    line = re.sub(re.escape(f"-I{FRAMAC_SHARE}"), "-I<FRAMAC_SHARE>", line)
+    # Remove spurious '-m32' and '-m64', which are architecture-dependent
+    line = re.sub("-m32", "", line)
+    line = re.sub("-m64", "", line)
+    print(line.strip())
diff --git a/tests/syntax/cpp-command.t/run.t b/tests/syntax/cpp-command.t/run.t
new file mode 100644
index 00000000000..df18a503c06
--- /dev/null
+++ b/tests/syntax/cpp-command.t/run.t
@@ -0,0 +1,53 @@
+  $ FRAMAC_SHARE="$(frama-c-config -print-share-path)"
+
+  $ frama-c -check -no-autoload-plugins cpp-command.c -machdep x86_32 -cpp-frama-c-compliant -cpp-command "echo [\$(basename '%1') \$(basename '%1') \$(basename '%i') \$(basename '%input')] ['%2' '%2' '%o' '%output'] ['%args']" | python3 filter.py "$FRAMAC_SHARE"
+  [kernel] Parsing cpp-command.c (with preprocessing)
+  [cpp-command.c cpp-command.c cpp-command.c cpp-command.c] [<TMPDIR/PP>.i <TMPDIR/PP>.i <TMPDIR/PP>.i <TMPDIR/PP>.i] [-I<TMP_MACHDEP> -I<FRAMAC_SHARE>/libc -D__FRAMAC__ -dD -nostdinc]
+
+  $ frama-c -check -no-autoload-plugins cpp-command.c -machdep x86_32 -cpp-frama-c-compliant -cpp-command "echo %%1 = \$(basename '%1') %%2 = '%2' %%args = '%args'" | python3 filter.py "$FRAMAC_SHARE"
+  [kernel] Parsing cpp-command.c (with preprocessing)
+  %1 = cpp-command.c %2 = <TMPDIR/PP>.i %args = -I<TMP_MACHDEP> -I<FRAMAC_SHARE>/libc -D__FRAMAC__ -dD -nostdinc
+
+  $ frama-c -check -no-autoload-plugins cpp-command.c -machdep x86_32 -cpp-frama-c-compliant -cpp-command "printf \"%s\n\" \"using \\% has no effect : \$(basename \"\%input\")\""
+  [kernel] Parsing cpp-command.c (with preprocessing)
+  using \% has no effect : cpp-command.c'
+
+  $ frama-c -check -no-autoload-plugins cpp-command.c -machdep x86_32 -cpp-frama-c-compliant -cpp-command "echo %var is not an interpreted placeholder" | python3 filter.py "$FRAMAC_SHARE"
+  [kernel] Parsing cpp-command.c (with preprocessing)
+  %var is not an interpreted placeholder
+
+  $ frama-c -check -no-autoload-plugins cpp-command.c -machdep x86_32 -print-cpp-commands | python3 filter.py "$FRAMAC_SHARE"
+  [kernel] Preprocessing command:
+  gcc -E -C -I<TMP_MACHDEP> -I<FRAMAC_SHARE>/libc -D__FRAMAC__ -dD -nostdinc -Wno-builtin-macro-redefined -Wno-unknown-warning-option '$TESTCASE_ROOT/cpp-command.c' -o '<TMPDIR/PP>.i'
+
+  $ frama-c -check -no-autoload-plugins cpp-command.c -cpp-extra-args-per-file=cpp-command.c:"-DPF=\\\"cp%02d_%.3f\\\"" -cpp-extra-args="-DPF2=\\\"cp%02d_%.3f\\\"" -print | python3 filter.py "$FRAMAC_SHARE"
+  [kernel] Parsing cpp-command.c (with preprocessing)
+  /* Generated by Frama-C */
+  #include "errno.h"
+  #include "stdarg.h"
+  #include "stddef.h"
+  #include "stdio.h"
+  void printer(int i, float f)
+  {
+  printf("cp%02d_%.3f",i,(double)f);
+  return;
+  }
+  
+  int main(void)
+  {
+  int __retres;
+  printer(1,(float)1.0);
+  __retres = 0;
+  return __retres;
+  }
+  
+  
+
+  $ frama-c -check -no-autoload-plugins cpp-command.c -cpp-extra-args-per-file=cpp-command.c:"file_extra" -cpp-extra-args="global_extra" -cpp-command "echo 'extra_args: %args'" -print | python3 filter.py "$FRAMAC_SHARE"
+  [kernel] Warning: your preprocessor is not known to handle option `-nostdinc'. If preprocessing fails because of it, please add -no-cpp-frama-c-compliant option to Frama-C's command-line. If you do not want to see this warning again, explicitly use option -cpp-frama-c-compliant.
+  [kernel] Warning: your preprocessor is not known to handle option `-dD'. If preprocessing fails because of it, please add -no-cpp-frama-c-compliant option to Frama-C's command-line. If you do not want to see this warning again, explicitly use option -cpp-frama-c-compliant.
+  [kernel] Parsing cpp-command.c (with preprocessing)
+  extra_args: -I<TMP_MACHDEP> -I<FRAMAC_SHARE>/libc -D__FRAMAC__ -dD -nostdinc file_extra global_extra
+  [kernel] Warning: trying to preprocess annotation with an unknown preprocessor.
+  /* Generated by Frama-C */
+  
diff --git a/tests/syntax/dune b/tests/syntax/dune
new file mode 100644
index 00000000000..48d4f37d214
--- /dev/null
+++ b/tests/syntax/dune
@@ -0,0 +1,4 @@
+(cram
+  (applies_to cpp-command)
+  (enabled_if %{read:../../python-3.7-available})
+)
diff --git a/tests/syntax/oracle/cpp-command.0.res.oracle b/tests/syntax/oracle/cpp-command.0.res.oracle
deleted file mode 100644
index 914862fa5a3..00000000000
--- a/tests/syntax/oracle/cpp-command.0.res.oracle
+++ /dev/null
@@ -1,2 +0,0 @@
-[kernel] Parsing cpp-command.c (with preprocessing)
-[cpp-command.c cpp-command.c cpp-command.c cpp-command.c] [TMPDIR/FILE.i TMPDIR/FILE.i TMPDIR/FILE.i TMPDIR/FILE.i] -ITMP_MACHDEP -IFRAMAC_SHARE/libc -D__FRAMAC__ -dD -nostdinc]
diff --git a/tests/syntax/oracle/cpp-command.1.res.oracle b/tests/syntax/oracle/cpp-command.1.res.oracle
deleted file mode 100644
index 6d8ee3a2017..00000000000
--- a/tests/syntax/oracle/cpp-command.1.res.oracle
+++ /dev/null
@@ -1,2 +0,0 @@
-[kernel] Parsing cpp-command.c (with preprocessing)
-%1 = cpp-command.c %2 = TMPDIR/FILE.i %args = -ITMP_MACHDEP -IFRAMAC_SHARE/libc -D__FRAMAC__ -dD -nostdinc
diff --git a/tests/syntax/oracle/cpp-command.2.res.oracle b/tests/syntax/oracle/cpp-command.2.res.oracle
deleted file mode 100644
index 0ad82e1bed4..00000000000
--- a/tests/syntax/oracle/cpp-command.2.res.oracle
+++ /dev/null
@@ -1,2 +0,0 @@
-[kernel] Parsing cpp-command.c (with preprocessing)
-using \% has no effect : cpp-command.c'
diff --git a/tests/syntax/oracle/cpp-command.3.res.oracle b/tests/syntax/oracle/cpp-command.3.res.oracle
deleted file mode 100644
index 37fd30a728f..00000000000
--- a/tests/syntax/oracle/cpp-command.3.res.oracle
+++ /dev/null
@@ -1,2 +0,0 @@
-[kernel] Parsing cpp-command.c (with preprocessing)
-%var is not an interpreted placeholder
diff --git a/tests/syntax/oracle/cpp-command.4.res.oracle b/tests/syntax/oracle/cpp-command.4.res.oracle
deleted file mode 100644
index 8a7eb85e889..00000000000
--- a/tests/syntax/oracle/cpp-command.4.res.oracle
+++ /dev/null
@@ -1,2 +0,0 @@
-[kernel] Preprocessing command:
-  gcc -E -C -I. -ITMP_MACHDEP -IFRAMAC_SHARE/libc -D__FRAMAC__ -dD -nostdinc -Wno-builtin-macro-redefined -Wno-unknown-warning-option 'cpp-command.c' -o 'TMPDIR/FILE.i'
diff --git a/tests/syntax/oracle/cpp-command.5.res.oracle b/tests/syntax/oracle/cpp-command.5.res.oracle
deleted file mode 100644
index 1c90e019287..00000000000
--- a/tests/syntax/oracle/cpp-command.5.res.oracle
+++ /dev/null
@@ -1,21 +0,0 @@
-[kernel] Parsing cpp-command.c (with preprocessing)
-/* Generated by Frama-C */
-#include "errno.h"
-#include "stdarg.h"
-#include "stddef.h"
-#include "stdio.h"
-void printer(int i, float f)
-{
-  printf("cp%02d_%.3f",i,(double)f);
-  return;
-}
-
-int main(void)
-{
-  int __retres;
-  printer(1,(float)1.0);
-  __retres = 0;
-  return __retres;
-}
-
-
diff --git a/tests/syntax/oracle/cpp-command.6.res.oracle b/tests/syntax/oracle/cpp-command.6.res.oracle
deleted file mode 100644
index f59eb94c447..00000000000
--- a/tests/syntax/oracle/cpp-command.6.res.oracle
+++ /dev/null
@@ -1,7 +0,0 @@
-[kernel] Warning: your preprocessor is not known to handle option `-nostdinc'. If preprocessing fails because of it, please add -no-cpp-frama-c-compliant option to Frama-C's command-line. If you do not want to see this warning again, explicitly use option -cpp-frama-c-compliant.
-[kernel] Warning: your preprocessor is not known to handle option `-dD'. If preprocessing fails because of it, please add -no-cpp-frama-c-compliant option to Frama-C's command-line. If you do not want to see this warning again, explicitly use option -cpp-frama-c-compliant.
-[kernel] Parsing cpp-command.c (with preprocessing)
-extra_args: -ITMP_MACHDEP -IFRAMAC_SHARE/libc -D__FRAMAC__ -dD -nostdinc file_extra global_extra
-[kernel] Warning: trying to preprocess annotation with an unknown preprocessor.
-/* Generated by Frama-C */
-
-- 
GitLab