[libcxx-commits] [libcxx] [libc++] Automatically detect the libc++ hardening mode from the test suite (PR #172505)

via libcxx-commits libcxx-commits at lists.llvm.org
Tue Dec 16 07:53:05 PST 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-libcxx

Author: Louis Dionne (ldionne)

<details>
<summary>Changes</summary>

This prevents hardcoding the hardening mode via compiler flags, and allows testing what the default hardening mode is on platforms that set it to something that isn't `none`. Otherwise, a platform setting a default (which is done via -DLIBCXX_HARDENING_MODE=mode at CMake configuration time) would end up passing `-D_LIBCPP_HARDENING_MODE=mode` to the compiler, which does not allow checking what the default mode is.

---
Full diff: https://github.com/llvm/llvm-project/pull/172505.diff


3 Files Affected:

- (modified) libcxx/test/CMakeLists.txt (-2) 
- (modified) libcxx/utils/libcxx/test/features/libcxx_macros.py (+17-1) 
- (modified) libcxx/utils/libcxx/test/params.py (-1) 


``````````diff
diff --git a/libcxx/test/CMakeLists.txt b/libcxx/test/CMakeLists.txt
index 6294319815b42..39d383922e1df 100644
--- a/libcxx/test/CMakeLists.txt
+++ b/libcxx/test/CMakeLists.txt
@@ -41,8 +41,6 @@ if (NOT LIBCXX_ENABLE_RTTI)
   serialize_lit_param(SERIALIZED_LIT_PARAMS enable_rtti False)
 endif()
 
-serialize_lit_string_param(SERIALIZED_LIT_PARAMS hardening_mode "${LIBCXX_HARDENING_MODE}")
-
 if (CMAKE_CXX_COMPILER_TARGET)
   serialize_lit_string_param(SERIALIZED_LIT_PARAMS target_triple "${CMAKE_CXX_COMPILER_TARGET}")
 else()
diff --git a/libcxx/utils/libcxx/test/features/libcxx_macros.py b/libcxx/utils/libcxx/test/features/libcxx_macros.py
index 7a465f2e87866..50a4ab9d300e2 100644
--- a/libcxx/utils/libcxx/test/features/libcxx_macros.py
+++ b/libcxx/utils/libcxx/test/features/libcxx_macros.py
@@ -6,7 +6,7 @@
 #
 # ===----------------------------------------------------------------------===##
 
-from libcxx.test.dsl import Feature, compilerMacros
+from libcxx.test.dsl import Feature, compilerMacros, programSucceeds
 
 features = []
 
@@ -74,3 +74,19 @@
             and compilerMacros(cfg)[m] == "0",
         )
     )
+
+for mode in ("none", "fast", "extensive", "debug"):
+    check_program = f"""
+        #include <cassert>
+        #if defined(_LIBCPP_HARDENING_MODE)
+            int main(int, char**) {{ return _LIBCPP_HARDENING_MODE == _LIBCPP_HARDENING_MODE_{mode.upper()} ? 0 : 1; }}
+        #else
+            int main(int, char**) {{ return 0; }}
+        #endif
+    """
+    features.append(
+        Feature(
+            name=f"libcpp-hardening-mode={mode}",
+            when=lambda cfg: programSucceeds(cfg, check_program)
+        )
+    )
diff --git a/libcxx/utils/libcxx/test/params.py b/libcxx/utils/libcxx/test/params.py
index 299aa28777fd5..b0e0c48873eb3 100644
--- a/libcxx/utils/libcxx/test/params.py
+++ b/libcxx/utils/libcxx/test/params.py
@@ -419,7 +419,6 @@ def getSuitableClangTidy(cfg):
                 AddCompileFlag("-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_FAST")      if hardening_mode == "fast" else None,
                 AddCompileFlag("-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE") if hardening_mode == "extensive" else None,
                 AddCompileFlag("-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_DEBUG")     if hardening_mode == "debug" else None,
-                AddFeature("libcpp-hardening-mode={}".format(hardening_mode))               if hardening_mode != "undefined" else None,
             ],
         ),
     ),

``````````

</details>


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


More information about the libcxx-commits mailing list