[llvm] [libcxx] [libc++][hardening] Rework macros for enabling the hardening mode. (PR #70575)

Louis Dionne via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 31 13:51:04 PDT 2023


================
@@ -269,38 +272,32 @@
 //
 // - `_LIBCPP_ASSERT_UNCATEGORIZED` -- for assertions that haven't been properly classified yet.
 
-#  ifndef _LIBCPP_ENABLE_HARDENED_MODE
-#    define _LIBCPP_ENABLE_HARDENED_MODE _LIBCPP_ENABLE_HARDENED_MODE_DEFAULT
-#  endif
-#  if _LIBCPP_ENABLE_HARDENED_MODE != 0 && _LIBCPP_ENABLE_HARDENED_MODE != 1
-#    error "_LIBCPP_ENABLE_HARDENED_MODE must be set to 0 or 1."
-#  endif
-
-#  ifndef _LIBCPP_ENABLE_SAFE_MODE
-#    define _LIBCPP_ENABLE_SAFE_MODE _LIBCPP_ENABLE_SAFE_MODE_DEFAULT
-#  endif
-#  if _LIBCPP_ENABLE_SAFE_MODE != 0 && _LIBCPP_ENABLE_SAFE_MODE != 1
-#    error "_LIBCPP_ENABLE_SAFE_MODE must be set to 0 or 1."
-#  endif
+// clang-format off
+#  define _LIBCPP_HARDENING_MODE_UNCHECKED  (1 << 1)
----------------
ldionne wrote:

I think this is great. If this were not the preprocessor, we'd have something like a strong enum and there would be no reason for unchecked to be 0 either. By doing this, we will also catch the mistake in case someone does e.g. `-D_LIBCPP_HARDENING_MODE` without providing a value. If we had a 0 value in our enums, then it would be equivalent to that.

https://github.com/llvm/llvm-project/pull/70575


More information about the llvm-commits mailing list