Good morning, Michael,
On Sat, Nov 21, 2026 at 12:39 PM Michael Ackermann via Tinycc-devel
<[email protected]> wrote:
>
> Good Morning Christopher,
>
> thanks for the quick response, and good to see a brief discussion started,
> staying focused on technical detail, which i'll try to keep it at even though
> obviously we have different perspectives onto language feature extensions
> added to C-lang.
>
> I would like to keep a few more notes, in general:
> - tcc is a single-pass compiler design (gcc, llvm, and SDCC? aren't);
> i don't know if -Wnull-dereference could be implemented with tcc because
> of mentioned design, and in principle i tend to prefer compiler diagnostics
> implemented without additional language-features/code-instrumentation added
> (which was common for MISRA-C related diagnostics i think);
> since _Optional feature remains a draft had the feasibility of
> -Wnull-dereference been considered for tcc?
I haven't investigated implementing -Wnull-dereference in TCC;
however, I think it would be complementary to _Optional rather than an
alternative.
A compiler can diagnose dereferencing a pointer that is known to be
null, but what should it do when the pointer's value is unknown?
Assuming that every pointer might be null would produce too many
warnings, whereas assuming that unknown pointers are non-null would
miss potentially dangerous dereferences.
The _Optional qualifier provides information that helps resolve this
ambiguity: it distinguishes pointers whose referenced objects may
legitimately be absent from those whose interfaces require an object
to be present. A compiler can use that distinction to decide when an
unknown pointer value warrants a diagnostic.
That is particularly useful for a small compiler. Even without
flow-sensitive analysis, TCC can diagnose incompatible pointer
conversions and questionable calls across translation-unit boundaries.
A future -Wnull-dereference implementation could use the same type
information to make its own diagnostics more useful.
> - mentioning SDCC as a reference compiler is inappropriate because this
> compiler's many internals itself are implemented in C++, not C nor leave
> alone
In fact, SDCC is predominantly written in C. In its src directory,
97.3% of the code (excluding comments and blank lines) is C, according
to cloc:
~/sdcc-code/sdcc $ cloc src --include-lang=C,C++
340 text files.
293 unique files.
180 files ignored.
github.com/AlDanial/cloc v 2.12 T=0.89 s (179.7 files/s, 307848.2 lines/s)
-------------------------------------------------------------------------------
Language files blank comment code
-------------------------------------------------------------------------------
C 150 34697 33027 199305
C++ 10 1117 468 5547
-------------------------------------------------------------------------------
SUM: 160 35814 33495 204852
-------------------------------------------------------------------------------
See also
https://sourceforge.net/p/sdcc/wiki/Language%20dialects%20used%20in%20SDCC/
In any case, I cited SDCC for its experience implementing and using
_Optional, not as a model for TCC's architecture.
> Assembler (there is C compilers written in ASM just recently again for
> very good reasons); i wouldn't want to imagine a C-compiler such as tcc
> being re-written in ASM, because tcc itself grew in size and complexity
> beyond
> and at least one sufficiently capable C-Compiler written in ASM is
> necessary.
> - therefore i would too recommend to keep "compiler bootstrapping" in mind
> which tcc was and still is the one known compiler to me which fits into
> it's role, meaning it can compile gcc-4.x AND it can be compiled by much
> simpler compilers itself (pnut-cc, mes-cc, or stage0-posix cc_x86 written
> in ASM even); the more complicated tcc itself becomes feasiblity of
> bootstrapping complicates ever more (tcc already cannot be compiled by
> asm-written cc_x86 for example with a shortenend compiler bootstrapping
> chain
> and if you've seen the ones from rust-lang, c++ and Java this certainly
> isn't an example to follow along); long story short there is good reasons
> why
> C-lang remained stable for decades and kept relatively simple sith less
> language features added; As said it's a different perspective onto C-lang.
> C language and it's compilers are highly critical for system-bootstrapping,
> ever since these existed, even when ISO/IEC never could codify any standard
> for bootstrapping procedure (another hint: chaos and complexity among
> assemblers).
> There is a pinciple tradeoff between bootstrapping and language features
> added
> (discussed before here on the list).
I understand the importance of bootstrapping, but I think there is a
crucial distinction between the language features that TCC supports
and the language features required to compile TCC. Supporting
_Optional adds a very small amount of implementation complexity, but
does not require a more sophisticated compiler to bootstrap TCC.
> - finally _Optional language extension isn't the only i remain sceptical
> about.
> pointer semantics in C are clear, these can be NULL and must be tested for
> carefully, with or without a language extension for this.
In that respect, I think _Optional is honest because it does not
directly state whether or not a pointer can be null. This is similar
to how the absence of a const qualifier on a referenced type does not
guarantee that the referenced object is mutable, nor does presence of
const necessarily mean that a referenced object is immutable.
> Friendly Greetings
> Michael
Best,
Christopher
> On 2026-11-21 10:17, Christopher Bazley wrote:
> > 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
>
> --
>
> _______________________________________________
> 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