Philipp Klaus Krause schreef op 2025-02-26 11:22:
Dear SDCC users,

would an _Optional qualifier (see N3422 -
https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3422.pdf for details)
be useful to you?

Basically, it would be a way for programmers to explicitly specify that
a pointer might be null.

This would allow the compiler to warn on missing null pointer checks, e.g.:

void f(_Optional char *p)
{
        *p = 0; // Warning here
}

void g(_Optional char *p)
{
        if(p)
                *p = 0; // No warning here
}

Since _Optional is essentially an opt-in mechanism, we would not get any
false warnings on existing code.

Philipp

Is there also a counterpart like _Required or _NonNull to indicate that the pointer must already be checked to be non-null? It would have to be used in
the prototype of course. It could climb up the calling tree to a point
where the check is performed only once.

E.g.

void f(_NonNull char *p);
{
    *p = 0;     // No warning here
}

void g(char *p)
{
    f(p++);     // Warning here
    if (p)
    {
        f(p++); // No warning here
        f(p++); // No warning here
        f(p++); // No warning here
    }
}

For backward compatibility the choice to warn for neither keywords can be
done through pedantic setting.

Maarten


_______________________________________________
Sdcc-user mailing list
Sdcc-user@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sdcc-user

Reply via email to