Add static checker tests
authorOlivier Dion <odion@efficios.com>
Wed, 23 Oct 2024 18:46:05 +0000 (14:46 -0400)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Thu, 24 Oct 2024 20:51:26 +0000 (16:51 -0400)
The static checkers ought to work with unit tests.  Therefore, adding
`*-sc*' variants for `test.c' and `test-cxx.  This only test valid
inputs.

Invalid inputs are tested under `tests/static-checker'.  Files can be
written with invalid DSL usage and added
`tests/static-checker/run-tests'.

Change-Id: Iac5419bfb846d4272ed02ba9c5d1377467e85684
Signed-off-by: Olivier Dion <odion@efficios.com>
Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
14 files changed:
.gitignore
configure.ac
tests/Makefile.am
tests/static-checker/Makefile.am [new file with mode: 0644]
tests/static-checker/argument-array-types-incompatible.c [new file with mode: 0644]
tests/static-checker/argument-vla-types-incompatible.c [new file with mode: 0644]
tests/static-checker/duplicated-fields.c [new file with mode: 0644]
tests/static-checker/null-field.c [new file with mode: 0644]
tests/static-checker/optional-mismatch.c [new file with mode: 0644]
tests/static-checker/prologue.c [new file with mode: 0644]
tests/static-checker/run-tests [new file with mode: 0755]
tests/static-checker/static-event-call-mismatch.c [new file with mode: 0644]
tests/unit/test-sc-cxx.cpp [new file with mode: 0644]
tests/unit/test-sc.c [new file with mode: 0644]

index 4eac5b0802aa7bff0457760b8ebb2faf3eca11f2..ad46cd34bcee08b0772bb1925ae05be67d1b6ae1 100644 (file)
@@ -67,6 +67,8 @@ dkms.conf
 /tests/unit/test
 /tests/unit/test-cxx
 /tests/unit/statedump
+/tests/unit/test-sc
+/tests/unit/test-sc-cxx
 
 #automake
 /include/config.h
index 2af34dd3c209746a991f185e8b9877617efd3a23..6a040cf6c5cb510eb661e38468a8ee96c74d2761 100644 (file)
@@ -212,6 +212,7 @@ AC_CONFIG_FILES([
        src/Makefile
        src/libside.pc
        tests/Makefile
+       tests/static-checker/Makefile
        tests/utils/Makefile
 ])
 
index 9d8702c255474a97b12544304e849c19ba7cf04f..152efee8a2b94763b19bf4c73c3729bcdcc83dc7 100644 (file)
@@ -3,11 +3,10 @@
 
 AM_CPPFLAGS += -I$(top_srcdir)/tests/utils
 
-SUBDIRS = utils
+SUBDIRS = utils static-checker
 
-TEST_EXTENSIONS = .tap
-TAP_LOG_DRIVER_FLAGS = --merge --comments
-TAP_LOG_DRIVER = env AM_TAP_AWK='$(AWK)' \
+LOG_DRIVER_FLAGS = --merge --comments
+LOG_DRIVER = env AM_TAP_AWK='$(AWK)' \
        SIDE_TESTS_SRCDIR='$(abs_top_srcdir)/tests' \
        SIDE_TESTS_BUILDDIR='$(abs_top_builddir)/tests' \
        $(SHELL) $(srcdir)/utils/tap-driver.sh
@@ -16,6 +15,8 @@ noinst_PROGRAMS = \
        regression/side-rcu-test \
        unit/test \
        unit/test-cxx \
+       unit/test-sc \
+       unit/test-sc-cxx \
        unit/demo \
        unit/statedump
 
@@ -38,6 +39,18 @@ unit_test_cxx_LDADD = \
        $(top_builddir)/tests/utils/libtap.la \
        $(RSEQ_LIBS)
 
+unit_test_sc_SOURCES = unit/test-sc.c
+unit_test_sc_LDADD = \
+       $(top_builddir)/src/libside.la \
+       $(top_builddir)/tests/utils/libtap.la \
+       $(RSEQ_LIBS)
+
+unit_test_sc_cxx_SOURCES = unit/test-sc-cxx.cpp
+unit_test_sc_cxx_LDADD = \
+       $(top_builddir)/src/libside.la \
+       $(top_builddir)/tests/utils/libtap.la \
+       $(RSEQ_LIBS)
+
 unit_demo_SOURCES = unit/demo.c
 unit_demo_LDADD = \
        $(top_builddir)/src/libside.la \
@@ -51,4 +64,4 @@ unit_statedump_LDADD = \
        $(RSEQ_LIBS)
 
 # Currently no tap tests to run
-TESTS =
+TESTS =        static-checker/run-tests
diff --git a/tests/static-checker/Makefile.am b/tests/static-checker/Makefile.am
new file mode 100644 (file)
index 0000000..1634557
--- /dev/null
@@ -0,0 +1 @@
+dist_check_SCRIPTS = run-tests
diff --git a/tests/static-checker/argument-array-types-incompatible.c b/tests/static-checker/argument-array-types-incompatible.c
new file mode 100644 (file)
index 0000000..73e6df4
--- /dev/null
@@ -0,0 +1,6 @@
+int main(void)
+{
+       side_arg_define_array(my_array, side_arg_list(side_arg_u32(1), side_arg_u64(2)));
+
+       return 0;
+}
diff --git a/tests/static-checker/argument-vla-types-incompatible.c b/tests/static-checker/argument-vla-types-incompatible.c
new file mode 100644 (file)
index 0000000..bec4a92
--- /dev/null
@@ -0,0 +1,6 @@
+int main(void)
+{
+       side_arg_define_vla(my_vla, side_arg_list(side_arg_u32(1), side_arg_u64(2)));
+
+       return 0;
+}
diff --git a/tests/static-checker/duplicated-fields.c b/tests/static-checker/duplicated-fields.c
new file mode 100644 (file)
index 0000000..6730b1d
--- /dev/null
@@ -0,0 +1,8 @@
+side_static_event(event, "provider", "event", SIDE_LOGLEVEL_DEBUG,
+       side_field_list(
+               side_field_u32("a"),
+               side_field_u32("a"),
+       )
+);
+
+int main() { return 0; }
diff --git a/tests/static-checker/null-field.c b/tests/static-checker/null-field.c
new file mode 100644 (file)
index 0000000..74cec3c
--- /dev/null
@@ -0,0 +1,7 @@
+side_static_event(event, "provider", "event", SIDE_LOGLEVEL_DEBUG,
+       side_field_list(
+               side_field_u32(""),
+       )
+);
+
+int main() { return 0; }
diff --git a/tests/static-checker/optional-mismatch.c b/tests/static-checker/optional-mismatch.c
new file mode 100644 (file)
index 0000000..9195cf3
--- /dev/null
@@ -0,0 +1,18 @@
+side_static_event(event, "provider", "event", SIDE_LOGLEVEL_ERR,
+       side_field_list(
+               side_field_optional_literal("foo", side_elem(side_type_u32())),
+       )
+);
+
+int main()
+{
+       if (side_event_enabled(event)) {
+               side_arg_define_optional(my_optional, side_arg_u64(1),
+                                       SIDE_OPTIONAL_ENABLED);
+               side_event_call(event,
+                       side_arg_list(
+                               side_arg_optional(my_optional),
+                       )
+               );
+       }
+}
diff --git a/tests/static-checker/prologue.c b/tests/static-checker/prologue.c
new file mode 100644 (file)
index 0000000..412b89c
--- /dev/null
@@ -0,0 +1,2 @@
+#define SIDE_STATIC_CHECK_ENABLE
+#include <side/trace.h>
diff --git a/tests/static-checker/run-tests b/tests/static-checker/run-tests
new file mode 100755 (executable)
index 0000000..ab47c6d
--- /dev/null
@@ -0,0 +1,49 @@
+#!/usr/bin/env bash
+
+CURDIR=$(dirname "$0")/
+TESTDIR=$CURDIR/..
+SRCDIR=$CURDIR/../..
+
+SH_TAP=1
+source "$TESTDIR/utils/utils.sh"
+
+command -v gcc > /dev/null
+
+HAVE_GCC=$(($? == 0))
+
+command -v clang > /dev/null
+
+HAVE_CLANG=$(($? == 0))
+
+function run_test() {
+       if [ $HAVE_GCC -eq 1 ]; then
+               cat prologue.c "$1" | gcc -I "$SRCDIR/include" -xc /dev/stdin -o /dev/null 2>&1 | grep --quiet -E "$2"
+               ok $? "gcc $1"
+               cat prologue.c "$1" | gcc -I "$SRCDIR/include" -xc++ /dev/stdin -o /dev/null 2>&1 | grep --quiet -E "$2"
+               ok $? "g++ $1"
+       else
+               skip 0 "gcc not present" 2
+       fi
+
+       if [ $HAVE_CLANG -eq 1 ]; then
+               cat prologue.c "$1" | clang -I "$SRCDIR/include" -xc /dev/stdin -o /dev/null 2>&1 | grep --quiet -E "$2"
+               ok $? "clang $1"
+               cat prologue.c "$1" | clang -I "$SRCDIR/include" -xc++ /dev/stdin -o /dev/null 2>&1 | grep --quiet -E "$2"
+               ok $? "clang++ $1"
+       else
+               skip 0 "clang not present" 2
+       fi
+}
+
+cd "${TESTDIR}/static-checker"
+
+TEST=6
+
+plan_tests $((4*TEST))
+
+run_test null-field.c "Null field name"
+run_test duplicated-fields.c "Duplicated field names"
+run_test argument-array-types-incompatible.c "Types incompatible"
+run_test argument-vla-types-incompatible.c "Types incompatible"
+run_test static-event-call-mismatch.c "error: conflicting types | error: invalid conversion | error: cannot initialize a variable of type"
+run_test optional-mismatch.c "error: conflicting types | error: invalid conversion | error: cannot initialize a variable of type"
diff --git a/tests/static-checker/static-event-call-mismatch.c b/tests/static-checker/static-event-call-mismatch.c
new file mode 100644 (file)
index 0000000..bc87930
--- /dev/null
@@ -0,0 +1,12 @@
+side_static_event(event, "provider", "event", SIDE_LOGLEVEL_DEBUG,
+       side_field_list(
+               side_field_u32("a"),
+               side_field_u32("b"),
+       )
+);
+
+int main(void)
+{
+       side_event(event, side_arg_list(side_arg_u32(1), side_arg_u64(2)));
+       return 0;
+}
diff --git a/tests/unit/test-sc-cxx.cpp b/tests/unit/test-sc-cxx.cpp
new file mode 100644 (file)
index 0000000..c5eacf0
--- /dev/null
@@ -0,0 +1,2 @@
+#define SIDE_STATIC_CHECK_ENABLE
+#include "test-cxx.cpp"
diff --git a/tests/unit/test-sc.c b/tests/unit/test-sc.c
new file mode 100644 (file)
index 0000000..547e6bc
--- /dev/null
@@ -0,0 +1,2 @@
+#define SIDE_STATIC_CHECK_ENABLE
+#include "test.c"
This page took 0.028132 seconds and 4 git commands to generate.