Skip to content

Commit

Permalink
compilers: cpp: wire up stdlib assertions
Browse files Browse the repository at this point in the history
None of the options set here affect ABI and are intended for detecting constraint
violations.

For GCC, we simply need to set -D_GLIBCXX_ASSERTIONS.

For Clang, the situation is far more complicated:
* LLVM 18 uses a 'hardened mode' (https://libcxx.llvm.org/Hardening.html).
  There are several levels of severity available here. I've chosen
  _LIBCPP_HARDENING_MODE_EXTENSIVE as the strongest-but-one. The strongest
  one (_DEBUG) doesn't affect ABI still but is reserved for stldebug.

* LLVM 15 uses a similar approach to libstdc++ called '_LIBCPP_ENABLE_ASSERTIONS'

Note that LLVM 17 while in development had fully deprecated _LIBCPP_ENABLE_ASSERTIONS
in favour of hardened, but changed its mind last-minute: https://discourse.llvm.org/t/rfc-hardening-in-libc/73925/4.

Signed-off-by: Sam James <[email protected]>
Signed-off-by: Eli Schwartz <[email protected]>
  • Loading branch information
thesamesam authored and eli-schwartz committed Jan 9, 2024
1 parent c41fbf5 commit 9009847
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions docs/markdown/snippets/cpp-debug.md
Original file line number Diff line number Diff line change
@@ -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.
22 changes: 22 additions & 0 deletions mesonbuild/compilers/cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down Expand Up @@ -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)]

Expand Down

0 comments on commit 9009847

Please sign in to comment.