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

Louis Dionne via libcxx-commits libcxx-commits at lists.llvm.org
Tue Dec 16 07:52:32 PST 2025


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

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.

>From 9cad974814c003848b2a46922e01b75dcce980e8 Mon Sep 17 00:00:00 2001
From: Louis Dionne <ldionne.2 at gmail.com>
Date: Tue, 16 Dec 2025 10:44:23 -0500
Subject: [PATCH] [libc++] Automatically detect the libc++ hardening mode from
 the test suite

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.
---
 libcxx/test/CMakeLists.txt                     |  2 --
 .../libcxx/test/features/libcxx_macros.py      | 18 +++++++++++++++++-
 libcxx/utils/libcxx/test/params.py             |  1 -
 3 files changed, 17 insertions(+), 4 deletions(-)

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,
             ],
         ),
     ),



More information about the libcxx-commits mailing list