jamieschmeiser wrote:

> This seems like a very complicated solution.
> 
> If the AIX system headers want to define NULL, should we just let them? Just 
> `#include_next <stddef.h>` to ensure clang's stddef.h doesn't define NULL in 
> an inconsistent way.

Just doing an `#include_next <stddef.h>` is a dangerous solution.  For C, at 
least, the clang header `__stddef_null.h` makes it very clear that the expected 
definition of NULL is ((void*)0).  However, if one just lets the platform 
decide on the definition of NULL, which the standard indicates can be 0 or 
((void*)0), there can be unexpected consequences should 0 be chosen.  On AIX in 
64 bit mode, and I suspect on other platforms, an int is 4 bytes long while a 
pointer is 8 bytes long, which could lead to subtle bugs (eg sizeof(NULL) or 
_Alignof(NULL) may be different for int and void* on some platforms).  There 
may be an implicit understanding within the compiler that the C null pointer 
constant will be type void* (I'm not saying there is, and I hope there isn't) 
which may be problematic for those platforms using 0.  It is safer to enforce 
what the compiler is expecting.  It may also aid in portability of code across 
platforms.

The standard mandates that certain headers define NULL but what if the 
`#include_next` defines it as 0 and uses it somehow before returning?  Then 
there is inconsistency within the header.  I force the clang definition before 
the `#include_next` to try to ensure as much consistency as possible since it 
is reasonable to hope that the definition of NULL is guarded since many headers 
are mandated to define it but there is only so much one can do...  Afterwards, 
it is again forced in case it was changed.  This is done universally for those 
files that are mandated to define NULL.  For those non-mandated system headers 
that also define it on Aix, it is conditionally forced.  I would suggest other 
platforms to do similarly.

https://github.com/llvm/llvm-project/pull/149176
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to