RFR: 8366699: Replace TestNoNULL with a simpler mechanism to forbid using NULL in HotSpot code

Julian Waters jwaters at openjdk.org
Tue Sep 2 15:01:56 UTC 2025


TestNoNULL was introduced as a way to forbid using the raw NULL macro in HotSpot code. It works well, but a newer way to achieve the same result by poisoning the macro expansion itself would be more accurate given it could properly use the compiler's lexer and parser to detect NULL being introduced. Normally the way typically envisioned to poison macros by redefining them as an unusable sequence of tokens would be too strong and bleed over into third party code, but there is a way to do so in a controllable fashion. This proposes to implement a simple mechanism to replace TestNoNULL within HotSpot's source code itself, and retire TestNoNULL in favour of this new system.

For Visual C++ this is simple, it contains support for using a pragma to poison a macro name, and poisoning a macro causes warnings when it is used. These warnings can be switched off by pragmas whenever we need them to be, making for a perfect disabling system when NULL is used in third party code.

For the other 2 big compilers, there exists a similar pragma to disable using a macro, but it is too strong, and unlike Visual C++ is a compile error, meaning there is absolutely no way to turn it off whatsoever. Fortunately there is a simple trick one can leverage: Both compilers have a warning pragma that can be used in this case. All that has to be done is redefining the macro in question to expand into nullptr, but also expand into a pragma that contains this warning message. As for disabling it whenever third party code is around, both compilers conveniently have a push and pop macro pragma that can save and restore macro state, which they implemented for compatibility with Visual C++ and are handy for this purpose. Since redefining Standard header macros is not allowed in C++ we can instead target an implementation detail within the headers: NULL expands to __null, so we can define that as a macro for our purposes instead.

Currently using GitHub Actions to test the changes, this is not the final state of the Pull Request.

-------------

Commit messages:
 - Deprecate NULL in compilerWarnings_visCPP.hpp
 - Define __null in compilerWarnings_gcc.hpp
 - Delete test/hotspot/jtreg/sources/TestNoNULL.java

Changes: https://git.openjdk.org/jdk/pull/27054/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=27054&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8366699
  Stats: 158 lines in 3 files changed: 4 ins; 153 del; 1 mod
  Patch: https://git.openjdk.org/jdk/pull/27054.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/27054/head:pull/27054

PR: https://git.openjdk.org/jdk/pull/27054


More information about the hotspot-dev mailing list