2017-08-17 09:06:26 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-08-17 09:06:26 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-08-17 09:06:26 +00:00
|
|
|
*/
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <cstdlib>
|
|
|
|
|
2019-09-13 10:24:06 +00:00
|
|
|
// Ensures/Expects.
|
|
|
|
#include <gsl/gsl_assert>
|
|
|
|
|
2017-08-17 09:06:26 +00:00
|
|
|
namespace base {
|
|
|
|
namespace assertion {
|
|
|
|
|
|
|
|
// Client must define that method.
|
|
|
|
void log(const char *message, const char *file, int line);
|
|
|
|
|
|
|
|
// Release build assertions.
|
2017-09-05 17:46:16 +00:00
|
|
|
inline constexpr void noop() {
|
2017-08-17 09:06:26 +00:00
|
|
|
}
|
|
|
|
|
2019-01-10 07:09:13 +00:00
|
|
|
[[noreturn]] inline void fail(
|
|
|
|
const char *message,
|
|
|
|
const char *file,
|
|
|
|
int line) {
|
2017-08-17 09:06:26 +00:00
|
|
|
log(message, file, line);
|
|
|
|
|
|
|
|
// Crash with access violation and generate crash report.
|
|
|
|
volatile auto nullptr_value = (int*)nullptr;
|
|
|
|
*nullptr_value = 0;
|
|
|
|
|
|
|
|
// Silent the possible failure to comply noreturn warning.
|
|
|
|
std::abort();
|
|
|
|
}
|
|
|
|
|
2019-01-10 07:09:13 +00:00
|
|
|
constexpr const char* extract_basename(const char* path, size_t size) {
|
|
|
|
while (size != 0 && path[size - 1] != '/' && path[size - 1] != '\\') {
|
|
|
|
--size;
|
|
|
|
}
|
|
|
|
return path + size;
|
2017-08-17 09:06:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace assertion
|
|
|
|
} // namespace base
|
|
|
|
|
2019-01-10 07:09:13 +00:00
|
|
|
#if defined(__clang__) || defined(__GNUC__)
|
|
|
|
#define AssertUnlikelyHelper(x) __builtin_expect(!!(x), 0)
|
|
|
|
#else
|
|
|
|
#define AssertUnlikelyHelper(x) (!!(x))
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#define AssertValidationCondition(condition, message, file, line)\
|
|
|
|
((AssertUnlikelyHelper(!(condition)))\
|
|
|
|
? ::base::assertion::fail(message, file, line)\
|
|
|
|
: ::base::assertion::noop())
|
|
|
|
|
|
|
|
#define SOURCE_FILE_BASENAME (::base::assertion::extract_basename(\
|
|
|
|
__FILE__,\
|
|
|
|
sizeof(__FILE__)))
|
|
|
|
|
|
|
|
#define AssertCustom(condition, message) (AssertValidationCondition(\
|
|
|
|
condition,\
|
|
|
|
message,\
|
|
|
|
SOURCE_FILE_BASENAME,\
|
|
|
|
__LINE__))
|
2017-08-17 09:06:26 +00:00
|
|
|
#define Assert(condition) AssertCustom(condition, "\"" #condition "\"")
|
|
|
|
|
|
|
|
// Define our own versions of Expects() and Ensures().
|
|
|
|
// Let them crash with reports and logging.
|
|
|
|
#ifdef Expects
|
|
|
|
#undef Expects
|
|
|
|
#endif // Expects
|
2019-01-10 07:09:13 +00:00
|
|
|
#define Expects(condition) (AssertValidationCondition(\
|
|
|
|
condition,\
|
|
|
|
"\"" #condition "\"",\
|
|
|
|
SOURCE_FILE_BASENAME,\
|
|
|
|
__LINE__))
|
2017-08-17 09:06:26 +00:00
|
|
|
|
|
|
|
#ifdef Ensures
|
|
|
|
#undef Ensures
|
|
|
|
#endif // Ensures
|
2019-01-10 07:09:13 +00:00
|
|
|
#define Ensures(condition) (AssertValidationCondition(\
|
|
|
|
condition,\
|
|
|
|
"\"" #condition "\"",\
|
|
|
|
SOURCE_FILE_BASENAME,\
|
|
|
|
__LINE__))
|
2017-08-17 09:06:26 +00:00
|
|
|
|
|
|
|
#ifdef Unexpected
|
|
|
|
#undef Unexpected
|
|
|
|
#endif // Unexpected
|
2019-01-10 07:09:13 +00:00
|
|
|
#define Unexpected(message) (::base::assertion::fail(\
|
|
|
|
"Unexpected: " message,\
|
|
|
|
SOURCE_FILE_BASENAME,\
|
|
|
|
__LINE__))
|
2018-09-27 20:31:48 +00:00
|
|
|
|
|
|
|
#ifdef _DEBUG
|
|
|
|
#define AssertIsDebug(...)
|
|
|
|
#endif // _DEBUG
|