From bc949d0da695d84547381fcb38723e15d174215a Mon Sep 17 00:00:00 2001 From: Igor Loginov Date: Mon, 11 Aug 2025 14:46:15 +0300 Subject: [PATCH] Fix SetTest Issue: https://gitee.com/openharmony/arkcompiler_ets_frontend/issues/ICSFOB Signed-off-by: Igor Loginov --- ets2panda/ir/statements/ifStatement.cpp | 8 ++ ets2panda/ir/statements/ifStatement.h | 5 +- ets2panda/test/unit/plugin/CMakeLists.txt | 1 + ...d_to_state_check_if_statement_set_test.cpp | 128 ++++++++++++++++++ 4 files changed, 138 insertions(+), 4 deletions(-) create mode 100644 ets2panda/test/unit/plugin/plugin_proceed_to_state_check_if_statement_set_test.cpp diff --git a/ets2panda/ir/statements/ifStatement.cpp b/ets2panda/ir/statements/ifStatement.cpp index fb33121bbc..e21df9c470 100644 --- a/ets2panda/ir/statements/ifStatement.cpp +++ b/ets2panda/ir/statements/ifStatement.cpp @@ -20,6 +20,14 @@ #include "compiler/core/pandagen.h" namespace ark::es2panda::ir { +void IfStatement::SetTest(Expression *test) noexcept +{ + if (test != nullptr) { + test->SetParent(this); + } + test_ = test; +} + void IfStatement::TransformChildren(const NodeTransformer &cb, std::string_view transformationName) { if (auto *transformedNode = cb(test_); test_ != transformedNode) { diff --git a/ets2panda/ir/statements/ifStatement.h b/ets2panda/ir/statements/ifStatement.h index 8d6eaffbaa..5b778bf4d3 100644 --- a/ets2panda/ir/statements/ifStatement.h +++ b/ets2panda/ir/statements/ifStatement.h @@ -49,10 +49,7 @@ public: return test_; } - void SetTest(Expression *test) noexcept - { - test_ = test; - } + void SetTest(Expression *test) noexcept; [[nodiscard]] const Statement *Consequent() const noexcept { diff --git a/ets2panda/test/unit/plugin/CMakeLists.txt b/ets2panda/test/unit/plugin/CMakeLists.txt index e0fa2fe3f5..e9de77b51e 100644 --- a/ets2panda/test/unit/plugin/CMakeLists.txt +++ b/ets2panda/test/unit/plugin/CMakeLists.txt @@ -47,6 +47,7 @@ set(PLUGIN_TESTS "plugin_proceed_to_state_update_statements_lambda compile.ets ${RUNTIME_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_change_func runtime_change_func_call.ets ${RUNTIME_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_change_call_lambda compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" + "plugin_proceed_to_state_check_if_statement_set_test compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_test_annotation_change compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_proceed_to_state_test_class_decl_annotation compile.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" "plugin_check_manual_capi compile_with_external_source.ets ${COMPILE_MODE} cpp ${EXECUTABLE_PLUGIN}" diff --git a/ets2panda/test/unit/plugin/plugin_proceed_to_state_check_if_statement_set_test.cpp b/ets2panda/test/unit/plugin/plugin_proceed_to_state_check_if_statement_set_test.cpp new file mode 100644 index 0000000000..16edd47cc6 --- /dev/null +++ b/ets2panda/test/unit/plugin/plugin_proceed_to_state_check_if_statement_set_test.cpp @@ -0,0 +1,128 @@ +/** + * 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 "public/es2panda_lib.h" +#include "util.h" + +// NOLINTBEGIN + +static std::string source = R"( +class C { + static prop: int = 14 +} + +const x = 14 + +if (false) { + console.log(1) +} else { + console.log(2) +} +)"; + +static es2panda_Impl *impl = nullptr; +static es2panda_Config *config = nullptr; +static es2panda_Context *context = nullptr; + +static int countApplied = 0; + +void SetIfStatementTest(es2panda_AstNode *node, [[maybe_unused]] void *arg) +{ + if (impl->IsIfStatement(node)) { + std::string variableName = "x"; + auto *memForVariableName = static_cast(impl->AllocMemory(context, variableName.size() + 1, 1)); + std::copy_n(variableName.c_str(), variableName.size() + 1, memForVariableName); + es2panda_AstNode *variableIdent = GetImpl()->CreateIdentifier1(context, memForVariableName); + + std::string className = "C"; + auto *memForClassName = static_cast(impl->AllocMemory(context, className.size() + 1, 1)); + std::copy_n(className.c_str(), className.size() + 1, memForClassName); + es2panda_AstNode *classIdent = GetImpl()->CreateIdentifier1(context, memForClassName); + + std::string properyName = "prop"; + auto *memForProperyName = static_cast(impl->AllocMemory(context, properyName.size() + 1, 1)); + std::copy_n(properyName.c_str(), properyName.size() + 1, memForProperyName); + es2panda_AstNode *propertyIdent = GetImpl()->CreateIdentifier1(context, memForProperyName); + + es2panda_AstNode *memberExpression = GetImpl()->CreateMemberExpression( + context, classIdent, propertyIdent, Es2pandaMemberExpressionKind::MEMBER_EXPRESSION_KIND_PROPERTY_ACCESS, + false, false); + GetImpl()->AstNodeSetParent(context, classIdent, memberExpression); + GetImpl()->AstNodeSetParent(context, propertyIdent, memberExpression); + + es2panda_AstNode *testExpression = GetImpl()->CreateBinaryExpression( + context, variableIdent, memberExpression, Es2pandaTokenType::TOKEN_TYPE_PUNCTUATOR_EQUAL); + GetImpl()->AstNodeSetParent(context, variableIdent, testExpression); + GetImpl()->AstNodeSetParent(context, memberExpression, testExpression); + + GetImpl()->IfStatementSetTest(context, node, testExpression); + countApplied++; + } +} + +int main(int argc, char **argv) +{ + if (argc < MIN_ARGC) { + return INVALID_ARGC_ERROR_CODE; + } + + if (GetImpl() == nullptr) { + return NULLPTR_IMPL_ERROR_CODE; + } + impl = GetImpl(); + + const char **args = const_cast(&(argv[1])); + config = impl->CreateConfig(argc - 1, args); + context = impl->CreateContextFromString(config, source.data(), argv[argc - 1]); + if (context == nullptr) { + return NULLPTR_CONTEXT_ERROR_CODE; + } + + impl->ProceedToState(context, ES2PANDA_STATE_CHECKED); + CheckForErrors("CHECKED", context); + if (impl->ContextState(context) == ES2PANDA_STATE_ERROR) { + return PROCEED_ERROR_CODE; + } + + auto *program = impl->ContextProgram(context); + es2panda_AstNode *ast = impl->ProgramAst(context, program); + + impl->AstNodeForEach(ast, SetIfStatementTest, nullptr); + if (countApplied == 0) { + return TEST_ERROR_CODE; + } + + impl->AstNodeRecheck(context, ast); + CheckForErrors("RECHECKED", context); + if (impl->ContextState(context) == ES2PANDA_STATE_ERROR) { + return PROCEED_ERROR_CODE; + } + + impl->ProceedToState(context, ES2PANDA_STATE_BIN_GENERATED); + CheckForErrors("BIN", context); + if (impl->ContextState(context) == ES2PANDA_STATE_ERROR) { + return PROCEED_ERROR_CODE; + } + + impl->DestroyContext(context); + impl->DestroyConfig(config); + + return 0; +} + +// NOLINTEND \ No newline at end of file -- Gitee