From: Olivier Dion Date: Wed, 23 Oct 2024 18:46:05 +0000 (-0400) Subject: Add static checker tests X-Git-Url: http://drtracing.org/?a=commitdiff_plain;h=f87c7062b9a0fdb45f20b5ea134fc6bf7fa7c7f0;p=libside.git Add static checker tests 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 Signed-off-by: Mathieu Desnoyers --- diff --git a/.gitignore b/.gitignore index 4eac5b0..ad46cd3 100644 --- a/.gitignore +++ b/.gitignore @@ -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 diff --git a/configure.ac b/configure.ac index 2af34dd..6a040cf 100644 --- a/configure.ac +++ b/configure.ac @@ -212,6 +212,7 @@ AC_CONFIG_FILES([ src/Makefile src/libside.pc tests/Makefile + tests/static-checker/Makefile tests/utils/Makefile ]) diff --git a/tests/Makefile.am b/tests/Makefile.am index 9d8702c..152efee 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 index 0000000..1634557 --- /dev/null +++ b/tests/static-checker/Makefile.am @@ -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 index 0000000..73e6df4 --- /dev/null +++ b/tests/static-checker/argument-array-types-incompatible.c @@ -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 index 0000000..bec4a92 --- /dev/null +++ b/tests/static-checker/argument-vla-types-incompatible.c @@ -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 index 0000000..6730b1d --- /dev/null +++ b/tests/static-checker/duplicated-fields.c @@ -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 index 0000000..74cec3c --- /dev/null +++ b/tests/static-checker/null-field.c @@ -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 index 0000000..9195cf3 --- /dev/null +++ b/tests/static-checker/optional-mismatch.c @@ -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 index 0000000..412b89c --- /dev/null +++ b/tests/static-checker/prologue.c @@ -0,0 +1,2 @@ +#define SIDE_STATIC_CHECK_ENABLE +#include diff --git a/tests/static-checker/run-tests b/tests/static-checker/run-tests new file mode 100755 index 0000000..ab47c6d --- /dev/null +++ b/tests/static-checker/run-tests @@ -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 index 0000000..bc87930 --- /dev/null +++ b/tests/static-checker/static-event-call-mismatch.c @@ -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 index 0000000..c5eacf0 --- /dev/null +++ b/tests/unit/test-sc-cxx.cpp @@ -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 index 0000000..547e6bc --- /dev/null +++ b/tests/unit/test-sc.c @@ -0,0 +1,2 @@ +#define SIDE_STATIC_CHECK_ENABLE +#include "test.c"