[llvm] draft: Enable libc++ hardening mode for LLVM_ENABLE_ASSERTIONS (PR #130243)

Brian Cain via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 6 20:53:02 PST 2025


https://github.com/androm3da created https://github.com/llvm/llvm-project/pull/130243

Previously, this logic would apply a change to `LIBCXX_HARDENING_MODE`.  But `LIBCXX_HARDENING_MODE` is for changing the behavior of the libc++ build for its target, not for specifying the behavior of the projects described in `LLVM_ENABLE_PROJECTS`.  The just-built C++ library is not used to build these projects.

Now, LLVM_ENABLE_ASSERTIONS + LLVM_ENABLE_EXPENSIVE_CHECKS implies _LIBCPP_HARDENING_MODE_DEBUG and LLVM_ENABLE_ASSERTIONS + !LLVM_ENABLE_EXPENSIVE_CHECKS implies _LIBCPP_HARDENING_MODE_EXTENSIVE.

>From 1150601239dba59e79d2f6c6cb094deab3d44027 Mon Sep 17 00:00:00 2001
From: Brian Cain <brian.cain at oss.qualcomm.com>
Date: Thu, 6 Mar 2025 20:43:26 -0800
Subject: [PATCH] draft: Enable libc++ hardening mode for
 LLVM_ENABLE_ASSERTIONS

Previously, this logic would apply a change to `LIBCXX_HARDENING_MODE`.  But
`LIBCXX_HARDENING_MODE` is for changing the behavior of the libc++ build for
its target, not for specifying the behavior of the projects described
in `LLVM_ENABLE_PROJECTS`.  The just-built C++ library is not used to
build these projects.

Now, LLVM_ENABLE_ASSERTIONS + LLVM_ENABLE_EXPENSIVE_CHECKS implies
_LIBCPP_HARDENING_MODE_DEBUG and LLVM_ENABLE_ASSERTIONS + !LLVM_ENABLE_EXPENSIVE_CHECKS
implies _LIBCPP_HARDENING_MODE_EXTENSIVE.
---
 llvm/cmake/modules/HandleLLVMOptions.cmake | 11 ++++++-----
 1 file changed, 6 insertions(+), 5 deletions(-)

diff --git a/llvm/cmake/modules/HandleLLVMOptions.cmake b/llvm/cmake/modules/HandleLLVMOptions.cmake
index 5db06ccdadbeb..33303d3ff624c 100644
--- a/llvm/cmake/modules/HandleLLVMOptions.cmake
+++ b/llvm/cmake/modules/HandleLLVMOptions.cmake
@@ -136,11 +136,12 @@ if( LLVM_ENABLE_ASSERTIONS )
   # Enable assertions in libstdc++.
   add_compile_definitions(_GLIBCXX_ASSERTIONS)
   # Cautiously enable the extensive hardening mode in libc++.
-  if((DEFINED LIBCXX_HARDENING_MODE) AND
-     (NOT LIBCXX_HARDENING_MODE STREQUAL "extensive"))
-    message(WARNING "LLVM_ENABLE_ASSERTIONS implies LIBCXX_HARDENING_MODE \"extensive\" but is overriden from command line with value \"${LIBCXX_HARDENING_MODE}\".")
-  else()
-    set(LIBCXX_HARDENING_MODE "extensive")
+  if (LLVM_ENABLE_LIBCXX AND NOT LLVM_ENABLE_EXPENSIVE_CHECKS)
+    append("-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE"
+          CMAKE_CXX_FLAGS)
+  elseif (LLVM_ENABLE_LIBCXX AND LLVM_ENABLE_EXPENSIVE_CHECKS)
+    append("-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG"
+          CMAKE_CXX_FLAGS)
   endif()
 endif()
 



More information about the llvm-commits mailing list