[libcxx-commits] [libcxx] [libcxx] add Android assertion handler (PR #198831)

George Burgess IV via libcxx-commits libcxx-commits at lists.llvm.org
Wed May 27 16:48:02 PDT 2026


================
@@ -0,0 +1,58 @@
+// -*- C++ -*-
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef __LIBCPP_ANDROID_ASSERTION_HANDLER
+#define __LIBCPP_ANDROID_ASSERTION_HANDLER
+
+#include <__config>
+#include <__log_hardening_failure>
+#include <__verbose_abort>
+#include <__verbose_trap>
+
+#if !defined(_LIBCPP_HAS_NO_PRAGMA_SYSTEM_HEADER)
+#  pragma GCC system_header
+#endif
+
+#if _LIBCPP_ASSERTION_SEMANTIC == _LIBCPP_ASSERTION_SEMANTIC_IGNORE
+#  define _LIBCPP_ASSERTION_HANDLER(message) ((void)0)
+
+#elif _LIBCPP_ASSERTION_SEMANTIC == _LIBCPP_ASSERTION_SEMANTIC_OBSERVE
+#  define _LIBCPP_ASSERTION_HANDLER(message) _LIBCPP_LOG_HARDENING_FAILURE(message)
+
+#elif _LIBCPP_ASSERTION_SEMANTIC == _LIBCPP_ASSERTION_SEMANTIC_QUICK_ENFORCE
+
+// Require `nomerge` for better crash attribution.
+#  define _LIBCPP_ASSERTION_HANDLER(message) \
+  ({ [[clang::nomerge]] _LIBCPP_VERBOSE_TRAP(message); })
+
+#elif _LIBCPP_ASSERTION_SEMANTIC == _LIBCPP_ASSERTION_SEMANTIC_ENFORCE
+_LIBCPP_BEGIN_NAMESPACE_STD
+
+// Calling this saves code size compared to
+// `__libcpp_verbose_abort("%s", "error message")`.
+[[__noreturn__]] _LIBCPP_HIDE_FROM_ABI _LIBCPP_NOINLINE
+inline void __libcpp_android_verbose_abort(const char *__message) {
+  _LIBCPP_VERBOSE_ABORT("%s", __message);
+}
+_LIBCPP_END_NAMESPACE_STD
+
+#  define _LIBCPP_ASSERTION_HANDLER(message) \
+  ({ [[clang::nomerge]] ::std::__libcpp_android_verbose_abort(message); })
----------------
gburgessiv wrote:

> Since you have a separate function anyways, why not put the attribute there?

TIL you can do that - done

> Actually, is there any case where this makes a difference? The message already contains the file name and line number, so these shouldn't ever be merged, no?

The file name and line number are from the lexical callsite, no? So:

```
void bar(int);

__attribute__((flatten))
void foo(vector<int> &v) {
  bar(v[0]);
  bar(v[1]);
}
```

would merge the assertion-failure BBs without `nomerge`, since their messages will reference `vector::operator[]`

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


More information about the libcxx-commits mailing list