From d0685bf08647687267ec24fd6d696cff5f9f0753 Mon Sep 17 00:00:00 2001 From: Chinmay Garde Date: Mon, 3 Feb 2025 12:29:06 -0800 Subject: [PATCH] [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. --- engine/src/flutter/fml/logging.h | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/engine/src/flutter/fml/logging.h b/engine/src/flutter/fml/logging.h index 146c35bc88e..5f39cad59da 100644 --- a/engine/src/flutter/fml/logging.h +++ b/engine/src/flutter/fml/logging.h @@ -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_