From f1844ade005748f8f2267063242ad89d1d843527 Mon Sep 17 00:00:00 2001 From: Byoungchan Lee Date: Fri, 31 May 2024 06:21:20 +0900 Subject: [PATCH] Replace several NSAssert with FML_CHECK/DCHECK to unblock the build (flutter/engine#53048) In https://github.com/flutter/buildroot/pull/860, disabling `NSAssert` in release build was added, and https://github.com/flutter/engine/pull/53005 tries to roll the change into the engine. However, the change is blocked due to several compilation errors in the engine. This is due to the fact that Google's `DCHECK`(in flutter engine, `FML_DCHECK`) does not make unused variable warning even in release build, while traditional assertion macro like `assert` or `NSAssert` does. For example, the following code will be expanded like this: code: ```c++ FML_DCHECK(status == kCVReturnSuccess && pxbuffer != nullptr) << "Failed to create pixel buffer"; ``` expanded code: ```c++ true || (status == kCVReturnSuccess && pxbuffer != nullptr) ? (void)0 :: : fml::LogMessageVoidify() & ::fml::LogMessage(::fml::kLogFatal, 0, 0, nullptr).stream() ``` However, equivalent code with `NSAssert` will make unused variable warning, since the expanded code doesn't have any reference to the variable: code: ```c++ NSAssert(status == kCVReturnSuccess && pxbuffer != NULL, @"Failed to create pixel buffer."); ``` expanded code: ```c++ do { } while (0) ``` To unblock the build, this CL replaces several `NSAssert` with `FML_DCHECK` in the engine codebase. This CL isn't aimed to replace all `NSAssert` with `FML_DCHECK` since discussions in https://github.com/flutter/buildroot/pull/860 are not finalized whether to replace all `NSAssert` with `FML_DCHECK` or not. [C++, Objective-C, Java style guides]: https://github.com/flutter/engine/blob/main/CONTRIBUTING.md#style --- .../framework/Source/FlutterEmbedderExternalTextureTest.mm | 2 +- .../macos/framework/Source/FlutterEmbedderKeyResponder.mm | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEmbedderExternalTextureTest.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEmbedderExternalTextureTest.mm index 75d1b16627..1d0a6563c1 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEmbedderExternalTextureTest.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEmbedderExternalTextureTest.mm @@ -59,7 +59,7 @@ CVPixelBufferRef pxbuffer = NULL; CVReturn status = CVPixelBufferCreate(kCFAllocatorDefault, _width, _width, _pixelFormatType, (__bridge CFDictionaryRef)options, &pxbuffer); - NSAssert(status == kCVReturnSuccess && pxbuffer != NULL, @"Failed to create pixel buffer."); + FML_CHECK(status == kCVReturnSuccess && pxbuffer != nullptr) << "Failed to create pixel buffer"; return pxbuffer; } diff --git a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEmbedderKeyResponder.mm b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEmbedderKeyResponder.mm index 37f67f94ee..5feacb1d65 100644 --- a/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEmbedderKeyResponder.mm +++ b/engine/src/flutter/shell/platform/darwin/macos/framework/Source/FlutterEmbedderKeyResponder.mm @@ -167,9 +167,10 @@ static uint64_t GetLogicalKeyForEvent(NSEvent* event, uint64_t physicalKey) { uint32_t* keyLabel = DecodeUtf16(keyLabelUtf16, &keyLabelLength); if (keyLabelLength == 1) { uint32_t keyLabelChar = *keyLabel; - NSCAssert(!IsControlCharacter(keyLabelChar) && !IsUnprintableKey(keyLabelChar), - @"Unexpected control or unprintable keylabel 0x%x", keyLabelChar); - NSCAssert(keyLabelChar <= 0x10FFFF, @"Out of range keylabel 0x%x", keyLabelChar); + FML_DCHECK(!IsControlCharacter(keyLabelChar) && !IsUnprintableKey(keyLabelChar)) + << "Unexpected control or unprintable keylabel 0x" << std::hex << keyLabelChar; + FML_DCHECK(keyLabelChar <= 0x10FFFF) + << "Out of range keylabel 0x" << std::hex << keyLabelChar; character = keyLabelChar; } delete[] keyLabel;