Menu

#4003 Wrong diagnostic message when adding 0 to a pointer to an _Optional type

open
nobody
None
other
5
2026-06-06
2026-06-06
No

1: Sample code that reproduces the problem.

int *black(_Optional int *poi)
{
  return 0 + poi; // recommended diagnostic
}

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

sdcc --stack-auto -c black.c

3: SDCC version tested (type "sdcc -v" to find it)

SDCC : mcs51/z80/z180/r2k/r2ka/r3ka/r4k/r5k/r6k/sm83/tlcs90/ez80/z80n/r800/ds390/pic16/pic14/TININative/ds400/hc08/s08/stm8/pdk13/pdk14/pdk15/mos6502/mos65c02/f8/f8l TD- 4.5.24 #16456 (Mac OS X ppc)

4: Copy of the error message or incorrect output, or a clear description of the observed versus expected behavior.

The above example is taken from the Recommended Practice part of subsection 6.5.2 "Type qualifiers" in the _Optional TS.

Implementations that perform data-flow analysis are encouraged to produce a diagnostic
message if one operand has type pointer to optional-qualified type, the other operand has integer type, the operands are evaluated, and analysis cannot prove that no path exists on which the value of the pointer operand is a null pointer.
A diagnostic is encouraged regardless of whether the expression that is added to or subtracted from a pointer is an integer constant expression with value zero, because the referenced type of the result of the + or - operator is not optional-qualified.

SDCC does not produce the expected diagnostic message about the arithmetic operation (possibly the same issue as #4002) but instead produces what looks like a wrong diagnostic message about the return statement:

black.c:3: warning 196: pointer target lost _Optional qualifier

The diagnostic message about the return statement is a bug:

The return expressions of the amber, green and black functions do not violate a constraint on assignment because the + and - operators remove the _Optional qualifier that applied to the type of their pointer operand poi from the type of their result.

It is a variant of the same diagnostic message that would be produced if 'poi' instead pointed to a const-qualified type. Furthermore, making the return statement conditional on 'poi' being non-null does not prevent the diagnostic message about a lost qualifier from being produced.

Discussion


Log in to post a comment.

Auth0 Logo