|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2025 SonarSource SA |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * This program is free software; you can redistribute it and/or |
| 7 | + * modify it under the terms of the Sonar Source-Available License Version 1, as published by SonarSource SA. |
| 8 | + * |
| 9 | + * This program is distributed in the hope that it will be useful, |
| 10 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 | + * See the Sonar Source-Available License for more details. |
| 13 | + * |
| 14 | + * You should have received a copy of the Sonar Source-Available License |
| 15 | + * along with this program; if not, see https://sonarsource.com/license/ssal/ |
| 16 | + */ |
| 17 | +package org.sonar.python.checks; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import javax.annotation.Nullable; |
| 23 | +import org.sonar.check.Rule; |
| 24 | +import org.sonar.plugins.python.api.PythonSubscriptionCheck; |
| 25 | +import org.sonar.plugins.python.api.SubscriptionContext; |
| 26 | +import org.sonar.plugins.python.api.tree.AwaitExpression; |
| 27 | +import org.sonar.plugins.python.api.tree.BaseTreeVisitor; |
| 28 | +import org.sonar.plugins.python.api.tree.CallExpression; |
| 29 | +import org.sonar.plugins.python.api.tree.Expression; |
| 30 | +import org.sonar.plugins.python.api.tree.ForStatement; |
| 31 | +import org.sonar.plugins.python.api.tree.Tree; |
| 32 | +import org.sonar.plugins.python.api.tree.WithItem; |
| 33 | +import org.sonar.plugins.python.api.tree.WithStatement; |
| 34 | +import org.sonar.plugins.python.api.tree.YieldExpression; |
| 35 | +import org.sonar.plugins.python.api.types.v2.TriBool; |
| 36 | +import org.sonar.python.types.v2.TypeCheckBuilder; |
| 37 | +import org.sonar.python.types.v2.TypeCheckMap; |
| 38 | + |
| 39 | +@Rule(key = "S7490") |
| 40 | +public class CancellationScopeNoCheckpointCheck extends PythonSubscriptionCheck { |
| 41 | + |
| 42 | + private static final String MESSAGE = "Add a checkpoint inside this cancellation scope."; |
| 43 | + private static final String SECONDARY_MESSAGE = "This checkpoint is not awaited."; |
| 44 | + |
| 45 | + private static final String TRIO_CHECKPOINT = "trio.lowlevel.checkpoint"; |
| 46 | + private static final Map<String, String> TYPE_WITH_FQN_MAP = Map.of( |
| 47 | + "trio.CancelScope", TRIO_CHECKPOINT, |
| 48 | + "trio.move_on_after", TRIO_CHECKPOINT, |
| 49 | + "trio.move_on_at", TRIO_CHECKPOINT |
| 50 | + ); |
| 51 | + |
| 52 | + private static final String ANYIO_CHECKPOINT = "anyio.lowlevel.checkpoint"; |
| 53 | + private static final Map<String, String> TYPE_WITH_NAME_MAP = Map.of( |
| 54 | + "asyncio.timeout", "asyncio.sleep", |
| 55 | + "anyio.CancelScope", ANYIO_CHECKPOINT, |
| 56 | + "anyio.move_on_after", ANYIO_CHECKPOINT, |
| 57 | + "anyio.move_on_at", ANYIO_CHECKPOINT |
| 58 | + ); |
| 59 | + |
| 60 | + private TypeCheckMap<TypeCheckBuilder> typeCheckMap; |
| 61 | + |
| 62 | + @Override |
| 63 | + public void initialize(Context context) { |
| 64 | + context.registerSyntaxNodeConsumer(Tree.Kind.FILE_INPUT, this::initializeTypeCheckers); |
| 65 | + context.registerSyntaxNodeConsumer(Tree.Kind.WITH_STMT, this::checkCancellationScope); |
| 66 | + } |
| 67 | + |
| 68 | + private void initializeTypeCheckers(SubscriptionContext ctx) { |
| 69 | + typeCheckMap = new TypeCheckMap<>(); |
| 70 | + |
| 71 | + TYPE_WITH_FQN_MAP.forEach((typeName, checkpointFunction) -> |
| 72 | + typeCheckMap.put( |
| 73 | + ctx.typeChecker().typeCheckBuilder().isTypeWithFqn(typeName), |
| 74 | + ctx.typeChecker().typeCheckBuilder().isTypeWithFqn(checkpointFunction) |
| 75 | + ) |
| 76 | + ); |
| 77 | + |
| 78 | + TYPE_WITH_NAME_MAP.forEach((typeName, checkpointFunction) -> |
| 79 | + typeCheckMap.put( |
| 80 | + ctx.typeChecker().typeCheckBuilder().isTypeWithName(typeName), |
| 81 | + ctx.typeChecker().typeCheckBuilder().isTypeWithName(checkpointFunction) |
| 82 | + ) |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + private void checkCancellationScope(SubscriptionContext ctx) { |
| 87 | + WithStatement withStatement = (WithStatement) ctx.syntaxNode(); |
| 88 | + |
| 89 | + for (WithItem withItem : withStatement.withItems()) { |
| 90 | + Expression test = withItem.test(); |
| 91 | + TypeCheckBuilder checkpointTypeChecker = getCheckpointTypeCheckerForScope(test); |
| 92 | + |
| 93 | + if (checkpointTypeChecker != null) { |
| 94 | + CheckpointVisitor visitor = new CheckpointVisitor(checkpointTypeChecker); |
| 95 | + withStatement.statements().accept(visitor); |
| 96 | + |
| 97 | + if (!visitor.hasCheckpoint()) { |
| 98 | + PreciseIssue issue = ctx.addIssue(test, MESSAGE); |
| 99 | + visitor.checkpointedTrees.forEach(t -> issue.secondary(t, SECONDARY_MESSAGE)); |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + @Nullable |
| 106 | + private TypeCheckBuilder getCheckpointTypeCheckerForScope(Expression expression) { |
| 107 | + if (expression.is(Tree.Kind.CALL_EXPR)) { |
| 108 | + CallExpression callExpression = (CallExpression) expression; |
| 109 | + Expression callee = callExpression.callee(); |
| 110 | + return typeCheckMap.getOptionalForType(callee.typeV2()).orElse(null); |
| 111 | + } |
| 112 | + return typeCheckMap.getOptionalForType(expression.typeV2()).orElse(null); |
| 113 | + } |
| 114 | + |
| 115 | + private static class CheckpointVisitor extends BaseTreeVisitor { |
| 116 | + private final TypeCheckBuilder checkpointTypeChecker; |
| 117 | + private boolean checkpointFound = false; |
| 118 | + private List<Tree> checkpointedTrees = new ArrayList<>(); |
| 119 | + |
| 120 | + CheckpointVisitor(TypeCheckBuilder checkpointTypeChecker) { |
| 121 | + this.checkpointTypeChecker = checkpointTypeChecker; |
| 122 | + } |
| 123 | + |
| 124 | + boolean hasCheckpoint() { |
| 125 | + return checkpointFound; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public void visitAwaitExpression(AwaitExpression awaitExpression) { |
| 130 | + checkpointFound = true; |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public void visitYieldExpression(YieldExpression yieldExpression) { |
| 135 | + checkpointFound = true; |
| 136 | + } |
| 137 | + |
| 138 | + @Override |
| 139 | + public void visitForStatement(ForStatement forStatement) { |
| 140 | + if (forStatement.isAsync()) { |
| 141 | + // async for loops implicitly create checkpoints at each iteration |
| 142 | + checkpointFound = true; |
| 143 | + } |
| 144 | + if (!checkpointFound) { |
| 145 | + super.visitForStatement(forStatement); |
| 146 | + } |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public void visitCallExpression(CallExpression callExpression) { |
| 151 | + Expression callee = callExpression.callee(); |
| 152 | + if (checkpointTypeChecker.check(callee.typeV2()) == TriBool.TRUE) { |
| 153 | + checkpointedTrees.add(callExpression); |
| 154 | + } |
| 155 | + super.visitCallExpression(callExpression); |
| 156 | + } |
| 157 | + |
| 158 | + @Override |
| 159 | + protected void scan(@Nullable Tree tree) { |
| 160 | + if (!checkpointFound && tree != null) { |
| 161 | + tree.accept(this); |
| 162 | + } |
| 163 | + } |
| 164 | + } |
| 165 | +} |
0 commit comments