From 497021908acca3aff40d145d42cff658dda64a6d Mon Sep 17 00:00:00 2001 From: xingshunxiang Date: Mon, 18 Aug 2025 10:44:08 +0800 Subject: [PATCH] Fix DumpEtsSrc for staticblock missing Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICU2OG?from=project-issue Description: in PR 7597, we remove the method of ETSGLOBAL, and move all the top level statements to static block. But in classDefinition::DumpGlobalClass, we only dump the classProperty and classMethod, so some statements in the dumper were missed. Reason: in PR 7597, we remove the method of ETSGLOBAL, and move all the top level statements to static block. But in classDefinition::DumpGlobalClass, we only dump the classProperty and classMethod, so some statements in the dumper were missed. Tests: ninja tests passed tests/tests-u-runner/runner.sh --ets-cts --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --ets-func-tests --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --astchecker --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --ets-runtime --show-progress --build-dir x64.release --processes=all passed tests/tests-u-runner/runner.sh --parser --no-js --show-progress --build-dir x64.release --processes=all passed Signed-off-by: xingshunxiang --- ets2panda/ir/base/classDefinition.cpp | 24 ++++++ ets2panda/public/es2panda_lib_impl.inc.erb | 5 ++ ets2panda/test/unit/plugin/CMakeLists.txt | 1 + ...d_to_state_dump_src_for_etsglobal_test.cpp | 73 +++++++++++++++++++ 4 files changed, 103 insertions(+) create mode 100644 ets2panda/test/unit/plugin/plugin_proceed_to_state_dump_src_for_etsglobal_test.cpp diff --git a/ets2panda/ir/base/classDefinition.cpp b/ets2panda/ir/base/classDefinition.cpp index 1c322b1984..931eafcf70 100644 --- a/ets2panda/ir/base/classDefinition.cpp +++ b/ets2panda/ir/base/classDefinition.cpp @@ -318,11 +318,16 @@ void ClassDefinition::Dump(ir::AstDumper *dumper) const void ClassDefinition::DumpGlobalClass(ir::SrcDumper *dumper) const { ES2PANDA_ASSERT(IsGlobal()); + ir::ClassStaticBlock *classStaticBlock = nullptr; for (auto elem : Body()) { if (elem->IsClassProperty()) { elem->Dump(dumper); dumper->Endl(); } + + if (elem->IsClassStaticBlock()) { + classStaticBlock = elem->AsClassStaticBlock(); + } } for (auto elem : Body()) { if (elem->IsMethodDefinition()) { @@ -330,6 +335,25 @@ void ClassDefinition::DumpGlobalClass(ir::SrcDumper *dumper) const dumper->Endl(); } } + + if (classStaticBlock == nullptr) { + return; + } + + auto bodyStmts = + classStaticBlock->Value()->AsFunctionExpression()->Function()->Body()->AsBlockStatement()->Statements(); + for (auto statement : bodyStmts) { + if (statement->IsExpressionStatement() && + statement->AsExpressionStatement()->GetExpression()->IsAssignmentExpression() && + statement->AsExpressionStatement()->GetExpression()->AsAssignmentExpression()->IsIgnoreConstAssign()) { + // skip the dummy assignment expression created for const variable decl in the class static block. + continue; + } + statement->Dump(dumper); + if (statement != bodyStmts.back()) { + dumper->Endl(); + } + } } // This method is needed by OHOS CI code checker diff --git a/ets2panda/public/es2panda_lib_impl.inc.erb b/ets2panda/public/es2panda_lib_impl.inc.erb index e2ecc21172..6e6468b21c 100644 --- a/ets2panda/public/es2panda_lib_impl.inc.erb +++ b/ets2panda/public/es2panda_lib_impl.inc.erb @@ -259,6 +259,11 @@ extern "C" <%= classData.constructor_type().lib_type_to_str() auto oriOverloads = e2pOriginal->AsMethodDefinition()->Overloads(); newE2pNode->AsMethodDefinition()->SetOverloads(std::move(oriOverloads)); +% end +% if className == "AssignmentExpression" + if (e2pOriginal->AsAssignmentExpression()->IsIgnoreConstAssign()) { + newE2pNode->SetIgnoreConstAssign(); + } % end return reinterpret_cast<<%= classData.constructor_type().lib_type_to_str() %>>(newE2pNode); diff --git a/ets2panda/test/unit/plugin/CMakeLists.txt b/ets2panda/test/unit/plugin/CMakeLists.txt index 4967dab97d..1fc0923142 100644 --- a/ets2panda/test/unit/plugin/CMakeLists.txt +++ b/ets2panda/test/unit/plugin/CMakeLists.txt @@ -116,6 +116,7 @@ set(PLUGIN_TESTS "plugin_proceed_to_state_function_dump compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "pugin_proceed_to_state_annotationUsage_source_range_access compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_optional_language compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" + "plugin_proceed_to_state_dump_src_for_etsglobal_test compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" ) set(RUNTIME_ARGUMENTS diff --git a/ets2panda/test/unit/plugin/plugin_proceed_to_state_dump_src_for_etsglobal_test.cpp b/ets2panda/test/unit/plugin/plugin_proceed_to_state_dump_src_for_etsglobal_test.cpp new file mode 100644 index 0000000000..ebfbfd98a6 --- /dev/null +++ b/ets2panda/test/unit/plugin/plugin_proceed_to_state_dump_src_for_etsglobal_test.cpp @@ -0,0 +1,73 @@ +/** + * Copyright (c) 2025 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. + */ + +#include +#include +#include +#include + +#include "os/library_loader.h" + +#include "public/es2panda_lib.h" +#include "util.h" + +// NOLINTBEGIN + +static es2panda_Impl *impl = nullptr; + +static std::string g_source = R"( +let a: int = 11; +const b: int = 666; +a = 20; +foo() +function foo() {} +)"; + +static std::string expected = R"( +let a: int; + +const b: int = 666; + +function main() {} + +function foo() {} + +a = 20; +foo(); +)"; + +int main(int argc, char **argv) +{ + if (argc < MIN_ARGC) { + return INVALID_ARGC_ERROR_CODE; + } + + impl = GetImpl(); + if (impl == nullptr) { + return NULLPTR_IMPL_ERROR_CODE; + } + + const char **args = const_cast(&(argv[1])); + auto config = impl->CreateConfig(argc - 1, args); + auto context = impl->CreateContextFromString(config, g_source.data(), argv[argc - 1]); + auto *program = impl->ContextProgram(context); + impl->ProceedToState(context, ES2PANDA_STATE_CHECKED); + auto *entryAst = impl->ProgramAst(context, program); + [[maybe_unused]] std::string actual = impl->AstNodeDumpEtsSrcConst(context, entryAst); + ASSERT(expected == actual); + return 0; +} + +// NOLINTEND \ No newline at end of file -- Gitee