Skip to content

Commit cac04c4

Browse files
authored
Add FML_UNREACHABLE to declare points in code that should never be reached. (#21941)
A version of this macro is present in most code-bases. The use of this macro must satisfy two requirements: 1: If reached, the process must be terminated in all runtime modes and at all optimization levels. 2: If the compiler requires a value to be returned from the function, encountering this macro should not make the compiler insist on a return value (since the process is about to die anyway). We used to have a version of this macro that wasn't widely used and didn't satisfy the two requirements. I have removed the same and another unused macro in fml/logging.h Fixes #68164.
1 parent 2874fcc commit cac04c4

File tree

5 files changed

+46
-8
lines changed

5 files changed

+46
-8
lines changed

flow/layers/child_scene_layer.cc

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,7 @@ void ChildSceneLayer::Preroll(PrerollContext* context, const SkMatrix& matrix) {
2222
CheckForChildLayerBelow(context);
2323
}
2424

25-
void ChildSceneLayer::Paint(PaintContext& context) const {
26-
FML_NOTREACHED();
27-
}
25+
void ChildSceneLayer::Paint(PaintContext& context) const {}
2826

2927
void ChildSceneLayer::UpdateScene(SceneUpdateContext& context) {
3028
TRACE_EVENT0("flutter", "ChildSceneLayer::UpdateScene");

fml/BUILD.gn

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ if (enable_unittests) {
249249
"command_line_unittest.cc",
250250
"file_unittest.cc",
251251
"hash_combine_unittests.cc",
252+
"logging_unittests.cc",
252253
"memory/ref_counted_unittest.cc",
253254
"memory/task_runner_checker_unittest.cc",
254255
"memory/weak_ptr_unittest.cc",

fml/logging.cc

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ LogMessage::~LogMessage() {
9393
#endif
9494

9595
if (severity_ >= LOG_FATAL) {
96-
abort();
96+
KillProcess();
9797
}
9898
}
9999

@@ -105,4 +105,8 @@ bool ShouldCreateLogMessage(LogSeverity severity) {
105105
return severity >= GetMinLogLevel();
106106
}
107107

108+
void KillProcess() {
109+
abort();
110+
}
111+
108112
} // namespace fml

fml/logging.h

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,8 @@ int GetVlogVerbosity();
4343
// LOG_FATAL and above is always true.
4444
bool ShouldCreateLogMessage(LogSeverity severity);
4545

46+
[[noreturn]] void KillProcess();
47+
4648
} // namespace fml
4749

4850
#define FML_LOG_STREAM(severity) \
@@ -87,9 +89,10 @@ bool ShouldCreateLogMessage(LogSeverity severity);
8789
#define FML_DCHECK(condition) FML_EAT_STREAM_PARAMETERS(condition)
8890
#endif
8991

90-
#define FML_NOTREACHED() FML_DCHECK(false)
91-
92-
#define FML_NOTIMPLEMENTED() \
93-
FML_LOG(ERROR) << "Not implemented in: " << __PRETTY_FUNCTION__
92+
#define FML_UNREACHABLE() \
93+
{ \
94+
FML_LOG(ERROR) << "Reached unreachable code."; \
95+
::fml::KillProcess(); \
96+
}
9497

9598
#endif // FLUTTER_FML_LOGGING_H_

fml/logging_unittests.cc

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Copyright 2013 The Flutter Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
#include "flutter/fml/logging.h"
6+
#include "gtest/gtest.h"
7+
8+
namespace fml {
9+
namespace testing {
10+
11+
int UnreachableScopeWithoutReturnDoesNotMakeCompilerMad() {
12+
KillProcess();
13+
// return 0; <--- Missing but compiler is fine.
14+
}
15+
16+
int UnreachableScopeWithMacroWithoutReturnDoesNotMakeCompilerMad() {
17+
FML_UNREACHABLE();
18+
// return 0; <--- Missing but compiler is fine.
19+
}
20+
21+
TEST(LoggingTest, UnreachableKillProcess) {
22+
::testing::FLAGS_gtest_death_test_style = "threadsafe";
23+
ASSERT_DEATH(KillProcess(), "");
24+
}
25+
26+
TEST(LoggingTest, UnreachableKillProcessWithMacro) {
27+
::testing::FLAGS_gtest_death_test_style = "threadsafe";
28+
ASSERT_DEATH({ FML_UNREACHABLE(); }, "");
29+
}
30+
31+
} // namespace testing
32+
} // namespace fml

0 commit comments

Comments
 (0)