Menu ▾ ▴

#4137 _Countof accepts void operands without a diagnostic

open
nobody
None
Front-end
5
18 hours ago
18 hours ago
No

1: Sample code that reproduces the problem.

unsigned f(void) { return _Countof(void); }

2: Exact command used to run SDCC on this sample code.

sdcc -mz80 --std=c2y --nostdinc -S countof-void.c -o output.asm

3: SDCC version tested.

SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/r4k/r5k/r6k/sm83/tlcs90/ez80/z80n/r800/ds390/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15 TD- 4.6.4 #0 (Linux)
published under GNU General Public License (GPL)

Built from clean official SVN r16981 sources on Linux.

4: Observed and expected behaviour.

Every invalid example tested is silently accepted on all 19 C2Y configurations. The valid array test cases also compile cleanly.

WG14 N3886,
6.5.4.5 paragraph 1, specifies the relevant constraint:

The_Countof operator shall only be applied to an expression that has a complete array type, or to
the parenthesized name of such a type.

The void type is neither complete nor an array. SDCC silently accepts void, a typedef for void and an expression of void type, and generates a count of zero. Known-size array types and array expressions are valid counter-examples. This is a _Countof constraint, distinct from
the extension that gives sizeof(void) a value.

5: Additional checks.

Compiler Explorer: SDCC trunk and GCC 16.2 side by side.
Both panes use the sample above. SDCC uses -mz80 --std=c2y; GCC uses
-std=c2y -pedantic-errors -fsyntax-only. Checked 7 October 2026: GCC
diagnoses the constraint violation; SDCC accepts it without diagnostics. The CE trunk build reports SDCC 4.6.4 #16974.

GCC 16.2.0 (x86-64, Compiler Explorer cg162), with -std=c2y -pedantic-errors -fsyntax-only, diagnoses all three invalid cases and accepts both valid array test cases.

Case Expected SDCC Z80 C2Y
countof-void.c Invalid Accepted silently
countof-void-typedef.c Invalid Accepted silently
countof-void-expression.c Invalid Accepted silently
countof-array-control.c Valid Accepted silently
countof-object-control.c Valid Accepted silently

Configurations tested: MCS51 default/large/medium/stack-auto, DS390, Z80, Z180, Rabbit 2000/4000, SM83, TLCS-90, HC08, S08, STM8 default/large, PDK13/14/15, and eZ80. These are translation checks, not runtime tests.

Implementation observation: The COUNTOF branch in src/SDCCast.c diagnoses a zero element count only when the operand is not void. That explicit void exemption bypasses
E_COUNTOF_INVALID_TYPE.

Discussion


Log in to post a comment.