From 768f1f516222bfedab185544bdbfae7e2b7a34e4 Mon Sep 17 00:00:00 2001 From: ctw-ian Date: Fri, 8 Sep 2023 15:35:52 +0800 Subject: [PATCH] Fix dead-code after return jumping to invalid address Issue:https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/I7ZQPN Signed-off-by: ctw-ian Change-Id: If799378b67e8e6479ddd6606a0a8b6f5c59ddcec --- es2panda/compiler/core/function.cpp | 8 ++--- ...e-after-return-missing-symbol-expected.txt | 1 + .../dead-code-after-return-missing-symbol.js | 29 +++++++++++++++++++ 3 files changed, 34 insertions(+), 4 deletions(-) create mode 100644 es2panda/test/compiler/js/dead-code-after-return-missing-symbol-expected.txt create mode 100644 es2panda/test/compiler/js/dead-code-after-return-missing-symbol.js diff --git a/es2panda/compiler/core/function.cpp b/es2panda/compiler/core/function.cpp index f85b20e3bb5..5d9a674a950 100644 --- a/es2panda/compiler/core/function.cpp +++ b/es2panda/compiler/core/function.cpp @@ -33,7 +33,7 @@ namespace panda::es2panda::compiler { static void CompileSourceBlock(PandaGen *pg, const ir::BlockStatement *block) { - bool hasReturn = false; + bool endReturn = false; const auto &statements = block->Statements(); pg->SetFirstStmt(statements.empty() ? block : statements.front()); @@ -41,12 +41,12 @@ static void CompileSourceBlock(PandaGen *pg, const ir::BlockStatement *block) for (const auto *stmt : statements) { stmt->Compile(pg); - if (stmt->IsReturnStatement()) { - hasReturn = true; + if (stmt->IsReturnStatement() && (stmt == statements[statements.size() - 1])) { + endReturn = true; } } - if (hasReturn) { + if (endReturn) { return; } diff --git a/es2panda/test/compiler/js/dead-code-after-return-missing-symbol-expected.txt b/es2panda/test/compiler/js/dead-code-after-return-missing-symbol-expected.txt new file mode 100644 index 00000000000..d00491fd7e5 --- /dev/null +++ b/es2panda/test/compiler/js/dead-code-after-return-missing-symbol-expected.txt @@ -0,0 +1 @@ +1 diff --git a/es2panda/test/compiler/js/dead-code-after-return-missing-symbol.js b/es2panda/test/compiler/js/dead-code-after-return-missing-symbol.js new file mode 100644 index 00000000000..32696777801 --- /dev/null +++ b/es2panda/test/compiler/js/dead-code-after-return-missing-symbol.js @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + + +function f() {} + +function f0() { + print(1); + return; + try { + f(); + } catch (e) { + + } +} + +f0(); -- Gitee