13af6ab5fSopenharmony_ci/* 23af6ab5fSopenharmony_ci * Copyright (c) 2024 Huawei Device Co., Ltd. 33af6ab5fSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 43af6ab5fSopenharmony_ci * you may not use this file except in compliance with the License. 53af6ab5fSopenharmony_ci * You may obtain a copy of the License at 63af6ab5fSopenharmony_ci * 73af6ab5fSopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 83af6ab5fSopenharmony_ci * 93af6ab5fSopenharmony_ci * Unless required by applicable law or agreed to in writing, software 103af6ab5fSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 113af6ab5fSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 123af6ab5fSopenharmony_ci * See the License for the specific language governing permissions and 133af6ab5fSopenharmony_ci * limitations under the License. 143af6ab5fSopenharmony_ci */ 153af6ab5fSopenharmony_ci 163af6ab5fSopenharmony_ci#include "checkInfiniteLoop.h" 173af6ab5fSopenharmony_ci#include "ir/statements/forUpdateStatement.h" 183af6ab5fSopenharmony_ci#include "checker/types/type.h" 193af6ab5fSopenharmony_ci#include "ir/statements/doWhileStatement.h" 203af6ab5fSopenharmony_ci#include "ir/statements/whileStatement.h" 213af6ab5fSopenharmony_ci 223af6ab5fSopenharmony_cinamespace ark::es2panda::compiler::ast_verifier { 233af6ab5fSopenharmony_ci 243af6ab5fSopenharmony_ci[[nodiscard]] CheckResult CheckInfiniteLoop::operator()(CheckContext &ctx, const ir::AstNode *ast) 253af6ab5fSopenharmony_ci{ 263af6ab5fSopenharmony_ci if (ast->IsDoWhileStatement()) { 273af6ab5fSopenharmony_ci return HandleDoWhileStatement(ctx, ast->AsDoWhileStatement()); 283af6ab5fSopenharmony_ci } 293af6ab5fSopenharmony_ci 303af6ab5fSopenharmony_ci if (ast->IsWhileStatement()) { 313af6ab5fSopenharmony_ci return HandleWhileStatement(ctx, ast->AsWhileStatement()); 323af6ab5fSopenharmony_ci } 333af6ab5fSopenharmony_ci 343af6ab5fSopenharmony_ci if (ast->IsForUpdateStatement()) { 353af6ab5fSopenharmony_ci return HandleForUpdateStatement(ctx, ast->AsForUpdateStatement()); 363af6ab5fSopenharmony_ci } 373af6ab5fSopenharmony_ci 383af6ab5fSopenharmony_ci return {CheckDecision::CORRECT, CheckAction::CONTINUE}; 393af6ab5fSopenharmony_ci} 403af6ab5fSopenharmony_ci 413af6ab5fSopenharmony_cibool CheckInfiniteLoop::ConditionIsAlwaysTrue(const ir::Expression *const test) const 423af6ab5fSopenharmony_ci{ 433af6ab5fSopenharmony_ci ASSERT(test); 443af6ab5fSopenharmony_ci auto const *const type = test->TsType(); 453af6ab5fSopenharmony_ci if (type == nullptr) { 463af6ab5fSopenharmony_ci return false; 473af6ab5fSopenharmony_ci } 483af6ab5fSopenharmony_ci 493af6ab5fSopenharmony_ci if (!type->IsConditionalExprType()) { 503af6ab5fSopenharmony_ci // Cannot be tested for truthiness 513af6ab5fSopenharmony_ci return false; 523af6ab5fSopenharmony_ci } 533af6ab5fSopenharmony_ci 543af6ab5fSopenharmony_ci const auto [constant, truthy] = type->ResolveConditionExpr(); 553af6ab5fSopenharmony_ci return (constant && truthy); 563af6ab5fSopenharmony_ci} 573af6ab5fSopenharmony_ci 583af6ab5fSopenharmony_cibool CheckInfiniteLoop::HasBreakOrReturnStatement(const ir::Statement *const body) const 593af6ab5fSopenharmony_ci{ 603af6ab5fSopenharmony_ci ASSERT(body); 613af6ab5fSopenharmony_ci bool hasExit = body->IsBreakStatement() || body->IsReturnStatement(); 623af6ab5fSopenharmony_ci body->IterateRecursively( 633af6ab5fSopenharmony_ci [&hasExit](ir::AstNode *child) { hasExit |= child->IsBreakStatement() || child->IsReturnStatement(); }); 643af6ab5fSopenharmony_ci 653af6ab5fSopenharmony_ci return hasExit; 663af6ab5fSopenharmony_ci} 673af6ab5fSopenharmony_ci 683af6ab5fSopenharmony_ci[[nodiscard]] CheckResult CheckInfiniteLoop::HandleWhileStatement(CheckContext &ctx, 693af6ab5fSopenharmony_ci const ir::WhileStatement *const stmt) const 703af6ab5fSopenharmony_ci{ 713af6ab5fSopenharmony_ci auto const *body = stmt->Body(); 723af6ab5fSopenharmony_ci auto const *test = stmt->Test(); 733af6ab5fSopenharmony_ci if ((body == nullptr) || (test == nullptr)) { 743af6ab5fSopenharmony_ci return {CheckDecision::CORRECT, CheckAction::CONTINUE}; 753af6ab5fSopenharmony_ci } 763af6ab5fSopenharmony_ci 773af6ab5fSopenharmony_ci if (ConditionIsAlwaysTrue(test)) { 783af6ab5fSopenharmony_ci if (!HasBreakOrReturnStatement(body)) { 793af6ab5fSopenharmony_ci ctx.AddCheckMessage("INFINITE LOOP", *stmt, stmt->Start()); 803af6ab5fSopenharmony_ci } 813af6ab5fSopenharmony_ci } 823af6ab5fSopenharmony_ci 833af6ab5fSopenharmony_ci return {CheckDecision::CORRECT, CheckAction::CONTINUE}; 843af6ab5fSopenharmony_ci} 853af6ab5fSopenharmony_ci 863af6ab5fSopenharmony_ci[[nodiscard]] CheckResult CheckInfiniteLoop::HandleDoWhileStatement(CheckContext &ctx, 873af6ab5fSopenharmony_ci const ir::DoWhileStatement *const stmt) const 883af6ab5fSopenharmony_ci{ 893af6ab5fSopenharmony_ci auto const *body = stmt->Body(); 903af6ab5fSopenharmony_ci auto const *test = stmt->Test(); 913af6ab5fSopenharmony_ci if ((body == nullptr) || (test == nullptr)) { 923af6ab5fSopenharmony_ci return {CheckDecision::CORRECT, CheckAction::CONTINUE}; 933af6ab5fSopenharmony_ci } 943af6ab5fSopenharmony_ci 953af6ab5fSopenharmony_ci if (ConditionIsAlwaysTrue(test)) { 963af6ab5fSopenharmony_ci if (!HasBreakOrReturnStatement(body)) { 973af6ab5fSopenharmony_ci ctx.AddCheckMessage("INFINITE LOOP", *stmt, stmt->Start()); 983af6ab5fSopenharmony_ci } 993af6ab5fSopenharmony_ci } 1003af6ab5fSopenharmony_ci 1013af6ab5fSopenharmony_ci return {CheckDecision::CORRECT, CheckAction::CONTINUE}; 1023af6ab5fSopenharmony_ci} 1033af6ab5fSopenharmony_ci 1043af6ab5fSopenharmony_ci[[nodiscard]] CheckResult CheckInfiniteLoop::HandleForUpdateStatement(CheckContext &ctx, 1053af6ab5fSopenharmony_ci const ir::ForUpdateStatement *const stmt) const 1063af6ab5fSopenharmony_ci{ 1073af6ab5fSopenharmony_ci auto const *body = stmt->Body(); 1083af6ab5fSopenharmony_ci if (body == nullptr) { 1093af6ab5fSopenharmony_ci // Body existence is checked in ForLoopCorrectlyInitialized 1103af6ab5fSopenharmony_ci return {CheckDecision::CORRECT, CheckAction::CONTINUE}; 1113af6ab5fSopenharmony_ci } 1123af6ab5fSopenharmony_ci 1133af6ab5fSopenharmony_ci // Test can be null for for-update statements 1143af6ab5fSopenharmony_ci auto const *test = stmt->Test(); 1153af6ab5fSopenharmony_ci if (test == nullptr || ConditionIsAlwaysTrue(test)) { 1163af6ab5fSopenharmony_ci if (!HasBreakOrReturnStatement(body)) { 1173af6ab5fSopenharmony_ci ctx.AddCheckMessage("INFINITE LOOP", *stmt, stmt->Start()); 1183af6ab5fSopenharmony_ci return {CheckDecision::CORRECT, CheckAction::CONTINUE}; 1193af6ab5fSopenharmony_ci } 1203af6ab5fSopenharmony_ci } 1213af6ab5fSopenharmony_ci 1223af6ab5fSopenharmony_ci return {CheckDecision::CORRECT, CheckAction::CONTINUE}; 1233af6ab5fSopenharmony_ci} 1243af6ab5fSopenharmony_ci} // namespace ark::es2panda::compiler::ast_verifier 125