Toplevel entrypoints for classes/traits/interfaces
[hiphop-php.git] / hphp / util / assertions.cpp
blob234203b8a144468e98593ca81f0a2516dcab5fc3
1 /*
2 +----------------------------------------------------------------------+
3 | HipHop for PHP |
4 +----------------------------------------------------------------------+
5 | Copyright (c) 2010-present Facebook, Inc. (http://www.facebook.com) |
6 +----------------------------------------------------------------------+
7 | This source file is subject to version 3.01 of the PHP license, |
8 | that is bundled with this package in the file LICENSE, and is |
9 | available through the world-wide-web at the following url: |
10 | http://www.php.net/license/3_01.txt |
11 | If you did not receive a copy of the PHP license and are unable to |
12 | obtain it through the world-wide-web, please send a note to |
13 | license@php.net so we can mail you a copy immediately. |
14 +----------------------------------------------------------------------+
17 #include "hphp/util/assertions.h"
19 #include <folly/Format.h>
21 #include <cstdio>
22 #include <functional>
23 #include <iostream>
24 #include <string>
25 #include <vector>
27 namespace HPHP {
28 ///////////////////////////////////////////////////////////////////////////////
30 static bool s_assert_failed{false};
32 __thread AssertDetailImpl* AssertDetailImpl::s_head = nullptr;
34 ///////////////////////////////////////////////////////////////////////////////
36 std::pair<std::string,std::string>
37 AssertDetailImpl::log_one(const AssertDetailImpl* adi, const char* name) {
38 auto title = folly::sformat("{:-^80}\n", name);
39 auto msg = adi->run();
41 fprintf(stderr, "\n%s%s\n", title.c_str(), msg.c_str());
42 return { std::move(title), std::move(msg) };
45 bool AssertDetailImpl::readAndRemove(std::string& msg) {
46 if (!s_head) return false;
47 auto const head = s_head;
48 auto const name = head->m_name;
49 head->m_name = nullptr;
50 s_head = head->m_next;
52 auto pair = log_one(head, name);
53 msg = std::move(pair.first) + std::move(pair.second) + "\n";
54 return true;
57 //////////////////////////////////////////////////////////////////////
59 void assert_fail(const char* e, const char* file,
60 unsigned int line, const char* func,
61 const std::string& msg) {
62 // If we re-enter this function it's because we hit a second assertion while
63 // processing the first. The second assertion is meaningless, so just
64 // short-circuit straight to abort().
65 if (s_assert_failed) std::abort();
66 s_assert_failed = true;
68 auto const assertion = folly::sformat("{}:{}: {}: assertion `{}' failed.",
69 file, line, func, e);
71 fprintf(stderr, "\nAssertion failure: %s\n\n%s\n",
72 assertion.c_str(), msg.c_str());
74 SCOPE_ASSERT_DETAIL("Assertion Failure") { return assertion; };
76 if (msg.empty()) std::abort();
78 SCOPE_ASSERT_DETAIL("Assertion Message") { return msg; };
79 std::abort();
82 ///////////////////////////////////////////////////////////////////////////////