[clang] [clang] Implement -fstrict-bool (PR #160790)
John McCall via cfe-commits
cfe-commits at lists.llvm.org
Fri Sep 26 15:01:15 PDT 2025
=?utf-8?q?Félix?= Cloutier <fcloutier at apple.com>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/160790 at github.com>
================
@@ -2306,6 +2306,24 @@ are listed below.
additional function arity information (for supported targets). See
:doc:`ControlFlowIntegrity` for more details.
+.. option:: -fstrict-bool
+
+ ``bool`` values are stored to memory as 8-bit values on most targets. Under
+ ``-fstrict-bool``, it is undefined behavior for a ``bool`` value stored in
+ memory to have any other bit pattern than 0 or 1. This creates some
+ optimization opportunities for the compiler, but it enables memory
+ corruption if that assumption is violated, for instance if any other value
+ is ``memcpy``ed over a ``bool``. This is enabled by default.
+
+.. option:: -fno-strict-bool[={truncate|nonzero}]
+
+ Disable optimizations based on the assumption that all ``bool`` values,
+ which are typically represented as 8-bit integers in memory, only ever
+ contain bit patterns 0 or 1. When =truncate is specified, a ``bool`` is
+ true if its least significant bit is set, and false otherwise. When =nonzero
+ is specified, a ``bool`` is true when any bit is set, and false otherwise.
+ The default is =truncate.
----------------
rjmccall wrote:
Please put the `=` strings in backticks.
Might be worth saying, just to be perfectly clear, that Clang always stores either 0 or 1 regardless of the value of this setting.
https://github.com/llvm/llvm-project/pull/160790
More information about the cfe-commits
mailing list