diff options
| author | _ov4 <ov4@ov4.org> | 2026-10-22 13:23:04 +0000 |
|---|---|---|
| committer | _ov4 <ov4@ov4.org> | 2026-10-22 13:23:04 +0000 |
| commit | eaad9c34403f05372c1544d7ef522c847dfea0ba (patch) | |
| tree | 4cd2dbaadecbfafe7855ad8ae03c046a8947697d /tests | |
| parent | 628270661843c4d61a2b5bc656daeb3099850e64 (diff) | |
test(extern): check variadic promotions with va_arg
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/i/extern-c-variadic/case.json | 5 | ||||
| -rw-r--r-- | tests/i/extern-c-variadic/expected.txt | 2 | ||||
| -rw-r--r-- | tests/i/extern-c-variadic/lib.c | 20 | ||||
| -rw-r--r-- | tests/i/extern-c-variadic/notes.md | 7 | ||||
| -rw-r--r-- | tests/i/extern-c-variadic/program.cps | 11 |
5 files changed, 45 insertions, 0 deletions
diff --git a/tests/i/extern-c-variadic/case.json b/tests/i/extern-c-variadic/case.json new file mode 100644 index 0000000..1ce5970 --- /dev/null +++ b/tests/i/extern-c-variadic/case.json @@ -0,0 +1,5 @@ +{ + "objects": [ + {"source": "lib.c", "output": "lib.o"} + ] +} diff --git a/tests/i/extern-c-variadic/expected.txt b/tests/i/extern-c-variadic/expected.txt new file mode 100644 index 0000000..8920df8 --- /dev/null +++ b/tests/i/extern-c-variadic/expected.txt @@ -0,0 +1,2 @@ +65794 +3.750000 diff --git a/tests/i/extern-c-variadic/lib.c b/tests/i/extern-c-variadic/lib.c new file mode 100644 index 0000000..8f723a5 --- /dev/null +++ b/tests/i/extern-c-variadic/lib.c @@ -0,0 +1,20 @@ +#include <stdarg.h> + +/* Each argument after n arrives promoted, so va_arg reads int or double. */ +int sum_ints(int n, ...) { + va_list ap; + int total = 0; + va_start(ap, n); + for (int i = 0; i < n; ++i) total += va_arg(ap, int); + va_end(ap); + return total; +} + +double sum_doubles(int n, ...) { + va_list ap; + double total = 0; + va_start(ap, n); + for (int i = 0; i < n; ++i) total += va_arg(ap, double); + va_end(ap); + return total; +} diff --git a/tests/i/extern-c-variadic/notes.md b/tests/i/extern-c-variadic/notes.md new file mode 100644 index 0000000..4820ecd --- /dev/null +++ b/tests/i/extern-c-variadic/notes.md @@ -0,0 +1,7 @@ +# extern-c-variadic + +Human review: pending. Expected output was independently traced by AI, not captured from cpsc. + +Basis: compiler extension (EXTERN "C", INCLUDE_OBJ of a user object); C11 6.5.2.2p6-7 default argument promotions and 7.16.1.1 va_arg. + +Trace: the harness compiles `lib.c` with the host C compiler. sum_ints reads 5 arguments with va_arg(ap, int), so each must arrive promoted to int: unsigned char 200 gives 200, short -7 gives -7, unsigned short 65535 gives 65535 (int is wider than short), _Bool TRUE gives 1 and char 'A' gives 65. 200 - 7 + 65535 + 1 + 65 = 65794. sum_doubles reads 2 arguments with va_arg(ap, double): float 1.5 is promoted to double 1.5, and double 2.25 is passed unchanged. 1.5 + 2.25 = 3.75, printed like other C doubles as `3.750000`. diff --git a/tests/i/extern-c-variadic/program.cps b/tests/i/extern-c-variadic/program.cps new file mode 100644 index 0000000..2f2ea43 --- /dev/null +++ b/tests/i/extern-c-variadic/program.cps @@ -0,0 +1,11 @@ +// Variadic EXTERN arguments arrive with C default promotions applied. +INCLUDE_OBJ "lib.o" +TYPE UChar = C_TYPE("unsigned char") +TYPE CShort = C_TYPE("short") +TYPE UShort = C_TYPE("unsigned short") +TYPE CBool = C_TYPE("_Bool") +TYPE CFloat = C_TYPE("float") +EXTERN "C" EXTERN_FUNCTION sum_ints(C_INT, ...) RETURNS C_INT +EXTERN "C" EXTERN_FUNCTION sum_doubles(C_INT, ...) RETURNS C_DOUBLE +OUTPUT sum_ints((C_INT)5, (UChar)200, (CShort)-7, (UShort)65535, (CBool)TRUE, (C_CHAR)'A') +OUTPUT sum_doubles((C_INT)2, (CFloat)1.5, (C_DOUBLE)2.25) |
