From f2fc8861fe241683ccc4bf026fbeeefe6ba1e04e Mon Sep 17 00:00:00 2001 From: Kimplul Date: Wed, 23 Jul 2025 18:35:18 +0300 Subject: check const types with static_assert --- src/main.c | 29 ++++++++++++++++++----------- tests/Makefile | 2 ++ tests/xfail/Makefile | 15 +++++++++++++++ tests/xfail/val.c | 18 ++++++++++++++++++ 4 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 tests/xfail/Makefile create mode 100644 tests/xfail/val.c diff --git a/src/main.c b/src/main.c index e801130..917a68c 100644 --- a/src/main.c +++ b/src/main.c @@ -723,10 +723,12 @@ static void expand_instance(char *id, char *instance) if (tokens_len(&type_params) != tokens_len(&type_args)) { fprintf(stderr, "mismatch type params vs args\n"); - drop_tokens(&type_traits); - drop_tokens(&type_params); - drop_tokens(&type_args); - goto cleanup; + abort(); + } + + if (tokens_len(&type_traits) != tokens_len(&type_args)) { + fprintf(stderr, "mismatch type traits vs args\n"); + abort(); } for (size_t i = 0; i < tokens_len(&type_params); ++i) { @@ -754,20 +756,25 @@ static void expand_instance(char *id, char *instance) if (tokens_len(&const_params) != tokens_len(&const_args)) { fprintf(stderr, "mismatch const params vs args\n"); - drop_tokens(&const_types); - drop_tokens(&const_params); - drop_tokens(&const_args); - goto cleanup; + abort(); + } + + if (tokens_len(&const_types) != tokens_len(&const_args)) { + fprintf(stderr, "mismatch const types vs args\n"); + abort(); } for (size_t i = 0; i < tokens_len(&const_params); ++i) { + char *type = *tokens_at(&const_types, i); char *name = *tokens_at(&const_params, i); - char *type = *tokens_at(&const_args, i); + char *arg = *tokens_at(&const_args, i); - body = replace_id(body, name, type); + printf("_Static_assert(_Generic((%s), %s: 1, default: 0));\n", arg, type); + + body = replace_id(body, name, arg); if (super) - super = replace_id(super, name, type); + super = replace_id(super, name, arg); } if (super) diff --git a/tests/Makefile b/tests/Makefile index bcc91d3..1041e61 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,5 +1,7 @@ all: $(MAKE) -C xok + $(MAKE) -C xfail clean: $(MAKE) -C xok clean + $(MAKE) -C xfail clean diff --git a/tests/xfail/Makefile b/tests/xfail/Makefile new file mode 100644 index 0000000..b38feae --- /dev/null +++ b/tests/xfail/Makefile @@ -0,0 +1,15 @@ +FILES != echo *.c +TARGETS := $(FILES:%.c=build/%) + +all: $(TARGETS) + +build/%: %.c ../../ngc1 + @mkdir -p build + @gcc -E $< -o $@.i + @(../../ngc1 $@.i > $@.c 2>/dev/null && gcc $@.c -o $@ 2>/dev/null) \ + && echo "FAIL $@" \ + || echo "XFAIL $@" + +.PHONY: clean +clean: + $(RM) -rf build diff --git a/tests/xfail/val.c b/tests/xfail/val.c new file mode 100644 index 0000000..d231a4a --- /dev/null +++ b/tests/xfail/val.c @@ -0,0 +1,18 @@ +#include + +typedef tmpl[](int v){ + int <>_ok() + { + return v; + } +}; + +/* float vs int, should fail */ +typedef inst = tmpl[](20.0); + +int main() +{ + int ok = inst_ok(); + if (ok == 20) + puts("OK"); +} -- cgit v1.2.3