Hi Michael, On Sat, Nov 21, 2026 at 4:23 PM Michael Ackermann via Tinycc-devel <[email protected]> wrote: > > Christopher, I'll cut short a few citations: > > On 2026-11-21 15:30, Christopher Bazley wrote: > > Good morning, Michael, > > > > I haven't investigated implementing -Wnull-dereference in TCC; > > however, I think it would be complementary to _Optional rather than an > > alternative. > > Avoiding null-dereference seems a key issue to cope with. In case of > C lang that is the programmer's task to take care of. Too heavy-weight > static code analysis exist with gcc (which is where this belongs, as > compiler switch, without a language feature extension, and outside tcc due to > it's design)
Null-dereference analysis cannot infer the intended nullability contract of an independently compiled function from its declaration alone. Annotations can express such contracts, but they do not participate in C's type compatibility rules. That becomes important when function pointers are involved, particularly for type variance. > > 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? > > Occasionally it's the programmer's responsibility to ensure correctness of > control flow in C lang, even when there is no static code analysis > available supported by code instrumentation/qualifier entangled as core > language feature. C lang remained relatively stable for decades. It's always the programmer's responsibility, but tools can help. > > 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. > > OK, the programmer should know, and implement related control flow. It's not easy to know in a large codebase, even if the same programmer wrote all the code. Even if a programmer knows about all the callers or implementations of a function that currently exist, he/she cannot know about callers or implementations that have yet to be written. > > 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. > > I've lost here. At some point a test for NULL is necessary. A test for NULL for every parameter or global variable of pointer type in a C program might be appropriate for some applications, but it makes the language rather unwieldy. The inefficiency of such checks is more important to the tiny microcontrollers targeted by SDCC than to desktop CPUs, but the principle stands. > > > - 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: > > Sigh. You've forgotten the 100million lines of code transitive compile-time > dependencies inside c++ toolchain to compile those 2.7% of c++. > Remarkable in a way, because SDCC never could become self-hosting > on the target's it's compiling for (such as Z80 which i doubt ever could > host a c++ compiler sufficient to compile/bootstrap SDCC) Thanks for enlightening me. I already believed in the importance of C compilers written in C, but your explanation magnifies it. > tcc instead can be compiled by even smaller pnut-cc/mes-cc type compilers, > but not any asm-written one such as cc_x86 (see oriansj on github, myself > isn't > the expert in this) > > > > 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. > > tcc already cannot be compiled with asm-written cc_x86 (stage0-posix project) > tcc already did regress with it's complexity beyond feasibility. > (myself among many others were working on a few pieces still, and > bootstrappable > remained blocked at tcc-0.9.26 even, plus some pnut-cc related patchset which > was not yet rebased onto mob HEAD, which i'll try soon once some other > recent regressions were coped with and tcc HEAD is STABLE again, currently > it isn't) Sorry about that. It sounds frustrating. Best, Christopher _______________________________________________ Tinycc-devel mailing list [email protected] https://lists.nongnu.org/mailman/listinfo/tinycc-devel
