diff --git a/docs/markdown/snippets/cpp-debug.md b/docs/markdown/snippets/cpp-debug.md new file mode 100644 index 000000000000..2a62b93994b4 --- /dev/null +++ b/docs/markdown/snippets/cpp-debug.md @@ -0,0 +1,10 @@ +## `ndebug` setting now controls C++ stdlib assertions + +The `ndebug` setting, if disabled, now passes preprocessor defines to enable +debugging assertions within the C++ standard library. + +For GCC, `-D_GLIBCXX_ASSERTIONS=1` is set. + +For Clang, `-D_GLIBCXX_ASSERTIONS=1` is set to cover libstdc++ usage, +and `-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE` or +`-D_LIBCPP_ENABLE_ASSERTIONS=1` is used depending on the Clang version. diff --git a/mesonbuild/compilers/cpp.py b/mesonbuild/compilers/cpp.py index 226605390356..f6a03caf218b 100644 --- a/mesonbuild/compilers/cpp.py +++ b/mesonbuild/compilers/cpp.py @@ -293,6 +293,20 @@ def get_option_link_args(self, options: 'KeyedOptionDictType') -> T.List[str]: return libs return [] + def get_assert_args(self, disable: bool) -> T.List[str]: + args: T.List[str] = [] + if disable: + return ['-DNDEBUG'] + + # Clang supports both libstdc++ and libc++ + args.append('-D_GLIBCXX_ASSERTIONS=1') + if version_compare(self.version, '>=18'): + args.append('-D_LIBCPP_HARDENING_MODE=_LIBCPP_HARDENING_MODE_EXTENSIVE') + elif version_compare(self.version, '>=15'): + args.append('-D_LIBCPP_ENABLE_ASSERTIONS=1') + + return args + class ArmLtdClangCPPCompiler(ClangCPPCompiler): @@ -462,6 +476,14 @@ def get_option_link_args(self, options: 'KeyedOptionDictType') -> T.List[str]: return libs return [] + def get_assert_args(self, disable: bool) -> T.List[str]: + if disable: + return ['-DNDEBUG'] + + # XXX: This needs updating if/when GCC starts to support libc++. + # It currently only does so via an experimental configure arg. + return ['-D_GLIBCXX_ASSERTIONS=1'] + def get_pch_use_args(self, pch_dir: str, header: str) -> T.List[str]: return ['-fpch-preprocess', '-include', os.path.basename(header)]