Hi Michael,
On Sat, 21 Nov 2026 01:51:11 +0000, Michael Ackermann via Tinycc-devel
<[email protected]> wrote:
> i can't grasp myself how this is different to -Wnull-dereference
> known with gcc (not implying such diagnostics should be or could be
> implemented
> with tcc nor that it was any better or worse than _Optional added)
> Hence please don't take offense when a non-expert such as myself is asking.
>
> and i can't fully grasp how _Optional helps with a pointer must be tested
> for if(ptr != NULL) _before_ dereferencing, in any case, with or without
> _Optional. Not sure of the semantics either. A pointer in C can be NULL
> and it must be tested for.
Thanks for asking. Consider a function declared in a header and
defined in another translation unit:
void update(int *value, _Optional int *old_value);
void f(void)
{
int value = 42;
update(&value, 0); /* OK */
update(0, &value); /* warning */
}
Suppose update() requires an object to update, but the caller may omit
the object in which it would receive the old value. With
-Wnull-to-nonoptional (mob@205f264b9e01b47), TCC can diagnose the
transposed arguments in the second call from the declaration alone.
GCC's -Wnull-dereference looks for an execution path on which a null
pointer is dereferenced. There is no dereference in f() for it to
examine. GCC's -Wnonnull is a closer comparison. GCC can warn at the
call if the declaration has a nonnull attribute on the first
parameter.
As for testing before dereferencing, the caller already knows that
&value points to an object. Inside update(), the author knows from the
declaration that the caller may have omitted old_value and can
test it before writing through it. The usual C idiom still applies.
_Optional introduces neither a wrapper object nor a run-time test.
> In Java i had seen some syntatic sugar with Optional<> implementation
> for null-testing with instance1?.instance2.?instance3 etc... which actually
> could improve readability of source code a bit.
> But i cannot yet envision an equivalent for C-lang and if at all this was
> desirable and how mentioned Java-specific implementation relates to C, if at
> all
> Maybe i missed something.
SDCC diagnoses missing null tests of pointers to _Optional objects,
using flow-sensitive analysis that is efficient and reproduceable in
comparison to path-sensitive analysis that has to be limited in depth
for reasons of practicality. One of the main things I stand up for at
WG14 is the needs of smaller compilers and the machines that they run
on.
The following example shows that TCC can compile the same source code
and produce a still-useful subset of the diagnostic messages produced
by SDCC:
void pr_value(int *value)
{
printf("%d", *value);
}
void update(int *value, _Optional int *old_value)
{
*old_value = *value; // SDCC warning
pr_value(old_value); // SDCC & TCC warning
(*value)++;
}
See https://godbolt.org/z/haG6asqPz
(The feature is already in SDCC 4.6.0; trunk improves analysis still further.)
> I am not sure either, if compile-time diagnostics must be implemented as
> language feature, if those could be implemented as compiler-feature
> (-Wnull-dereference, don't know if that's reliable). Referring to
I agree that many useful diagnostics, including checks for MISRA C
rules, can be implemented without changing the language. A qualifier
can serve a broader purpose. In the case of _Optional, it lets a
declaration express whether a referenced object may be absent. That
distinction matters, for example, when deciding which functions can
safely be used as callbacks.
If the caller always supplies an object, it is safe to register a
callback that also accepts null. In the other direction, if the caller
may pass null, it would be a mistake to register a callback that
requires an object. The useful place to catch that mistake is at
registration, even if the caller and the callback are compiled
separately.
That is an example of type variance. For C's rules on function pointer
conversions to distinguish those cases, the distinction must be
present in the function types. There is strong support in WG14 for
enhancing type variance in C2y. On whether to pursue something along
the lines of N3674, the vote was 19 in favour, none against and 8
abstentions. The minutes record "strong direction".
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3674.pdf
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3814.htm
> -Wwrite-strings seems not plausible, since to my knowledge this did not
> introduce a new language feature equivalent to _Optional.
> I would propose compile-time diagnostics should be distinguished from
> language-features added, in particular when a diagnostic can be implemented
> without adding language-features. For example MISRA C defines a set of
> criteria
> compiler diagnostics (static code analysis) can test for _without_ adding
> language features. Adding language features aren't desirable or necessary
> always. It's up to ISO/IEC to decide.
I mentioned -Wwrite-strings because I find the two warnings useful for
a similar reason. One draws my attention to 'char *p = "abc";', where
I would usually want 'const char *p'. The other draws my
attention to 'int *p = 0;', where, if the null initialiser is
intentional, I would usually want '_Optional int *p'. "Analogous"
would have been a better word than "equivalent".
On the separate question of language changes, I regard -Wwrite-strings
as more intrusive than -Wnull-to-nonoptional. Enabling -Wwrite-strings
actually changes the type of array created from string literals in
existing C code. In contrast, use of _Optional in specific
declarations is a matter of choice, and enabling -Wnull-to-nonoptional
changes only diagnostics.
> Did _Optional proposal already surpass "draft" status?
> In any case tcc won't error when hitting _Optional qualifier,
> if that was accepted by ISO/IEC for C-lang as-is.
_Optional is the subject of ISO/IEC DTS 26716. It is still a draft
Technical Specification. The published C standard does not include it.
> C-lang by design is not Java nor rust to follow along. And C-lang was
> relatively stable for decades. Would be curious of a second opinion on this.
Idiomatic C99 programs look very different from idiomatic C89
programs. The changes in C11 and C23 are more subtle, but still
useful.
I think _Optional fits the existing C model. It expresses a property
of the referenced type through a qualifier, and the programmer can use
the usual C idioms to handle a null pointer. Nor does it introduce new
grammar.
> If _Optional qualifier does not break anything and does not obfuscate the
> source-code of tcc, it's a none issue then.
TCC's support for _Optional lets it compile more code written for
SDCC. Where the code does not depend on the type distinction, you can
erase the qualifier by compiling with -D_Optional=. You can do that
for the whole program or for selected translation units, provided
declarations of the same function or object still have compatible
types.
You can also erase it just while including a header:
#define _Optional
#include "foo.h"
#undef _Optional
So, I would say use of _Optional breaks less than most new language
features. (Compare to compound literals, variably-modified types,
designated initialisers, named loops/switches, etc.)
Thanks,
Christopher
> On 2026-11-20 21:12, Christopher Bazley wrote:
> > Dear TCC developers,
> >
> > I pushed 205f264b9e01 to mob earlier. It adds -Wnull-to-nonoptional,
> > which warns when a null pointer constant is implicitly converted to a
> > pointer whose target type lacks the _Optional qualifier. The warning
> > is off by default because such conversions are commonplace; however,
> > in a program that uses _Optional, it is invaluable (equivalent to
> > -Wwrite-strings for conversions to a pointer whose target type lacks
> > the const qualifier).
> >
> > Annex B of ISO/IEC DTS 26716 (WG14 N3951) lists this conversion as one
> > for which an implementation can usefully warn. For example, with the
> > warning enabled, "int *p = 0" is diagnosed and "_Optional int *p = 0"
> > is not.
> >
> > The compiler changes are small: 1 line added in libtcc.c, 2 in tcc.h,
> > and 25 added and 2 removed in tccgen.c. Most of the work beyond the
> > check in verify_assign_cast() is there to get cases like this right:
> >
> > _Optional int *f(int ready, int *p)
> > {
> > return ready ? p : 0;
> > }
> >
> > Warning as soon as TCC sees the null pointer constant would be
> > premature because the return type is pointer-to-optional; if the
> > return type were "int *" then the same expression should trigger a
> > warning. TCC therefore remembers the null pointer conversion (nulls =
> > 1) until it knows the destination type. Constant folding must preserve
> > the distinction between (void *)0, which is a null pointer constant
> > (nulls = 0), and (void *)(int *)0, which is not (nulls = -1).
> >
> > If TCC has already cleared a local aggregate, it skips the store for
> > an explicit initialiser such as .p = 0, so the patch checks the
> > pointer conversion beforehand (by calling verify_assign_cast).
> >
> > The tests add 95 lines and the documentation adds 8.
> >
> > Thanks,
> > Christopher
> >
> > _______________________________________________
> > Tinycc-devel mailing list
> > [email protected]
> > https://lists.nongnu.org/mailman/listinfo/tinycc-devel
>
> --
_______________________________________________
Tinycc-devel mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/tinycc-devel