[FML] Make logging available in constexpr contexts. (#162343)

Asking if the logs should be emitted and killing the process (say on
unreachable statements) wasn't constexpr. However we managed to use
these in constexpr contexts. As I understand, this was because of
https://www.open-std.org/jtc1/sc22/wg21/docs/papers/2022/p2448r2.html
which is available in C++23. This technique makes the methods constexpr
safe in C++17.
This commit is contained in:
Chinmay Garde 2025-02-03 12:29:06 -08:00 committed by GitHub
parent 2f2bda3504
commit d0685bf086
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -60,8 +60,27 @@ int GetVlogVerbosity();
// kLogFatal and above is always true.
bool ShouldCreateLogMessage(LogSeverity severity);
constexpr bool ShouldCreateLogMessageConstexpr(LogSeverity severity,
bool true_arg) {
if (true_arg) {
return ShouldCreateLogMessage(severity);
}
return false;
}
[[noreturn]] void KillProcess();
[[noreturn]] constexpr void KillProcessConstexpr(bool true_arg) {
if (true_arg) {
KillProcess();
}
#if defined(_MSC_VER) && !defined(__clang__)
__assume(false);
#else // defined(_MSC_VER) && !defined(__clang__)
__builtin_unreachable();
#endif // defined(_MSC_VER) && !defined(__clang__)
}
} // namespace fml
#define FML_LOG_STREAM(severity) \
@ -77,7 +96,7 @@ bool ShouldCreateLogMessage(LogSeverity severity);
::fml::LogMessage(::fml::kLogFatal, 0, 0, nullptr).stream()
#define FML_LOG_IS_ON(severity) \
(::fml::ShouldCreateLogMessage(::fml::LOG_##severity))
(::fml::ShouldCreateLogMessageConstexpr(::fml::LOG_##severity, true))
#define FML_LOG(severity) \
FML_LAZY_STREAM(FML_LOG_STREAM(severity), FML_LOG_IS_ON(severity))
@ -109,7 +128,7 @@ bool ShouldCreateLogMessage(LogSeverity severity);
#define FML_UNREACHABLE() \
{ \
FML_LOG(ERROR) << "Reached unreachable code."; \
::fml::KillProcess(); \
::fml::KillProcessConstexpr(true); \
}
#endif // FLUTTER_FML_LOGGING_H_