Path: blob/main/contrib/llvm-project/libc/src/__support/libc_assert.h
213766 views
//===-- Definition of a libc internal assert macro --------------*- C++ -*-===//1//2// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.3// See https://llvm.org/LICENSE.txt for license information.4// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception5//6//===----------------------------------------------------------------------===//78#ifndef LLVM_LIBC_SRC___SUPPORT_LIBC_ASSERT_H9#define LLVM_LIBC_SRC___SUPPORT_LIBC_ASSERT_H1011#if defined(LIBC_COPT_USE_C_ASSERT) || !defined(LIBC_FULL_BUILD)1213// The build is configured to just use the public <assert.h> API14// for libc's internal assertions.1516#include <assert.h>1718#define LIBC_ASSERT(COND) assert(COND)1920#else // Not LIBC_COPT_USE_C_ASSERT2122#include "src/__support/OSUtil/exit.h"23#include "src/__support/OSUtil/io.h"24#include "src/__support/integer_to_string.h"25#include "src/__support/macros/attributes.h" // For LIBC_INLINE26#include "src/__support/macros/config.h"27#include "src/__support/macros/optimization.h" // For LIBC_UNLIKELY2829namespace LIBC_NAMESPACE_DECL {3031// This is intended to be removed in a future patch to use a similar design to32// below, but it's necessary for the external assert.33LIBC_INLINE void report_assertion_failure(const char *assertion,34const char *filename, unsigned line,35const char *funcname) {36const IntegerToString<unsigned> line_buffer(line);37write_to_stderr(filename);38write_to_stderr(":");39write_to_stderr(line_buffer.view());40write_to_stderr(": Assertion failed: '");41write_to_stderr(assertion);42write_to_stderr("' in function: '");43write_to_stderr(funcname);44write_to_stderr("'\n");45}4647} // namespace LIBC_NAMESPACE_DECL4849#ifdef LIBC_ASSERT50#error "Unexpected: LIBC_ASSERT macro already defined"51#endif5253// The public "assert" macro calls abort on failure. Should it be same here?54// The libc internal assert can fire from anywhere inside the libc. So, to55// avoid potential chicken-and-egg problems, it is simple to do an exit56// on assertion failure instead of calling abort. We also don't want to use57// __builtin_trap as it could potentially be implemented using illegal58// instructions which can be very misleading when debugging.59#ifdef NDEBUG60#define LIBC_ASSERT(COND) \61do { \62} while (false)63#else6465// Convert __LINE__ to a string using macros. The indirection is necessary66// because otherwise it will turn "__LINE__" into a string, not its value. The67// value is evaluated in the indirection step.68#define __LIBC_MACRO_TO_STR(x) #x69#define __LIBC_MACRO_TO_STR_INDIR(y) __LIBC_MACRO_TO_STR(y)70#define __LIBC_LINE_STR__ __LIBC_MACRO_TO_STR_INDIR(__LINE__)7172#define LIBC_ASSERT(COND) \73do { \74if (LIBC_UNLIKELY(!(COND))) { \75LIBC_NAMESPACE::write_to_stderr(__FILE__ ":" __LIBC_LINE_STR__ \76": Assertion failed: '" #COND \77"' in function: '"); \78LIBC_NAMESPACE::write_to_stderr(__PRETTY_FUNCTION__); \79LIBC_NAMESPACE::write_to_stderr("'\n"); \80LIBC_NAMESPACE::internal::exit(0xFF); \81} \82} while (false)83#endif // NDEBUG8485#endif // LIBC_COPT_USE_C_ASSERT8687#endif // LLVM_LIBC_SRC___SUPPORT_LIBC_ASSERT_H888990