summaryrefslogtreecommitdiff
path: root/libc/src/__support/libc_assert.h
blob: d8a1d5fe76787aa42aa632b39a0d8e09c8bb99b3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//===-- Definition of a libc internal assert macro --------------*- 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 LLVM_LIBC_SRC_SUPPORT_LIBC_ASSERT_H
#define LLVM_LIBC_SRC_SUPPORT_LIBC_ASSERT_H

#include "src/__support/OSUtil/io.h"
#include "src/__support/OSUtil/quick_exit.h"
#include "src/__support/integer_to_string.h"
#include "src/__support/macros/attributes.h" // For LIBC_INLINE

namespace __llvm_libc {

LIBC_INLINE void report_assertion_failure(const char *assertion,
                                          const char *filename, unsigned line,
                                          const char *funcname) {
  char line_str[IntegerToString::dec_bufsize<unsigned>()];
  IntegerToString::dec(line, line_str);
  __llvm_libc::write_to_stderr(filename);
  __llvm_libc::write_to_stderr(":");
  __llvm_libc::write_to_stderr(line_str);
  __llvm_libc::write_to_stderr(": Assertion failed: '");
  __llvm_libc::write_to_stderr(assertion);
  __llvm_libc::write_to_stderr("' in function: '");
  __llvm_libc::write_to_stderr(funcname);
  __llvm_libc::write_to_stderr("'\n");
}

} // namespace __llvm_libc

#ifdef LIBC_ASSERT
#error "Unexpected: LIBC_ASSERT macro already defined"
#endif

// The public "assert" macro calls abort on failure. Should it be same here?
// The libc intenral assert can fire from anywhere inside the libc. So, to
// avoid potential chicken-and-egg problems, it is simple to do a quick_exit
// on assertion failure instead of calling abort. We also don't want to use
// __builtin_trap as it could potentially be implemented using illegal
// instructions which can be very misleading when debugging.
#ifdef NDEBUG
#define LIBC_ASSERT(COND)                                                      \
  do {                                                                         \
  } while (false)
#else
#define LIBC_ASSERT(COND)                                                      \
  do {                                                                         \
    if (!(COND)) {                                                             \
      __llvm_libc::report_assertion_failure(#COND, __FILE__, __LINE__,         \
                                            __PRETTY_FUNCTION__);              \
      __llvm_libc::quick_exit(0xFF);                                           \
    }                                                                          \
  } while (false)
#endif // NDEBUG

#endif // LLVM_LIBC_SRC_SUPPORT_LIBC_ASSERT_H