|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) 2011-2024 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 GNU Lesser General Public |
| 8 | + * License as published by the Free Software Foundation; either |
| 9 | + * version 3 of the License, or (at your option) any later version. |
| 10 | + * |
| 11 | + * This program is distributed in the hope that it will be useful, |
| 12 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | + * Lesser General Public License for more details. |
| 15 | + * |
| 16 | + * You should have received a copy of the GNU Lesser General Public License |
| 17 | + * along with this program; if not, write to the Free Software Foundation, |
| 18 | + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 19 | + */ |
| 20 | +package org.sonar.python.checks; |
| 21 | + |
| 22 | +import java.util.Arrays; |
| 23 | +import java.util.HashSet; |
| 24 | +import java.util.Map; |
| 25 | +import java.util.Optional; |
| 26 | +import java.util.Set; |
| 27 | +import org.sonar.check.Rule; |
| 28 | +import org.sonar.plugins.python.api.PythonSubscriptionCheck; |
| 29 | +import org.sonar.plugins.python.api.SubscriptionContext; |
| 30 | +import org.sonar.plugins.python.api.quickfix.PythonQuickFix; |
| 31 | +import org.sonar.plugins.python.api.symbols.Symbol; |
| 32 | +import org.sonar.plugins.python.api.tree.CallExpression; |
| 33 | +import org.sonar.plugins.python.api.tree.Tree; |
| 34 | +import org.sonar.python.checks.utils.Expressions; |
| 35 | +import org.sonar.python.quickfix.TextEditUtils; |
| 36 | +import org.sonar.python.tree.TreeUtils; |
| 37 | + |
| 38 | +@Rule(key = "S6929") |
| 39 | +public class TfPyTorchSpecifyReductionAxisCheck extends PythonSubscriptionCheck { |
| 40 | + |
| 41 | + private static final Set<String> TF_REDUCTION_FUNCTIONS = new HashSet<>(Arrays.asList("reduce_all", "reduce_mean", "reduce_any", |
| 42 | + "reduce_euclidean_norm", "reduce_logsumexp", |
| 43 | + "reduce_max", "reduce_min", "reduce_prod", "reduce_std", "reduce_sum", "reduce_variance")); |
| 44 | + private static final Set<String> TF_REDUCTION_FUNCTIONS_FQN = new HashSet<>(); |
| 45 | + private static final String TF_MESSAGE = "Provide a value for the axis argument."; |
| 46 | + public static final String AXIS_PARAMETER = "axis"; |
| 47 | + public static final int AXIS_PARAMETER_POSITION = 1; |
| 48 | + |
| 49 | + static { |
| 50 | + for (String reductionFunction : TF_REDUCTION_FUNCTIONS) { |
| 51 | + TF_REDUCTION_FUNCTIONS_FQN.add("tensorflow.math." + reductionFunction); |
| 52 | + TF_REDUCTION_FUNCTIONS_FQN.add("tensorflow.tf." + reductionFunction); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private static final String PY_TORCH_MESSAGE = "Provide a value for the dim argument."; |
| 57 | + public static final String DIM_PARAMETER = "dim"; |
| 58 | + public static final int NO_POSITIONAL_ARG = -1; |
| 59 | + |
| 60 | + /** |
| 61 | + * Contains a list of reduction functions with a {@code dim} parameter and the position of the dim argument in that function. |
| 62 | + */ |
| 63 | + private static final Map<String, Integer> PY_TORCH_REDUCTION_FUNCTIONS_DIM_POS = Map.ofEntries( |
| 64 | + Map.entry("torch.argmin", 1), |
| 65 | + Map.entry("torch.aminmax", NO_POSITIONAL_ARG), |
| 66 | + Map.entry("torch.nanmean", 1), |
| 67 | + Map.entry("torch.mode", 1), |
| 68 | + Map.entry("torch.norm", 2), |
| 69 | + Map.entry("torch.quantile", 2), |
| 70 | + Map.entry("torch.nanquantile", 2), |
| 71 | + Map.entry("torch.std", 1), |
| 72 | + Map.entry("torch.std_mean", 1), |
| 73 | + Map.entry("torch.unique", 4), |
| 74 | + Map.entry("torch.unique_consecutive", 3), |
| 75 | + Map.entry("torch.var", 1), |
| 76 | + Map.entry("torch.var_mean", 1), |
| 77 | + Map.entry("torch.count_nonzero", 1) |
| 78 | + ); |
| 79 | + |
| 80 | + @Override |
| 81 | + public void initialize(Context context) { |
| 82 | + context.registerSyntaxNodeConsumer(Tree.Kind.CALL_EXPR, TfPyTorchSpecifyReductionAxisCheck::checkCallExpr); |
| 83 | + } |
| 84 | + |
| 85 | + private static void checkCallExpr(SubscriptionContext context) { |
| 86 | + CallExpression callExpression = (CallExpression) context.syntaxNode(); |
| 87 | + Symbol symbol = callExpression.calleeSymbol(); |
| 88 | + if (symbol != null && !Expressions.containsSpreadOperator(callExpression.arguments())) { |
| 89 | + if (isTfReductionMissingAxisArg(symbol, callExpression)) { |
| 90 | + PreciseIssue issue = context.addIssue(callExpression.callee(), TF_MESSAGE); |
| 91 | + createTfQuickFix(callExpression).ifPresent(issue::addQuickFix); |
| 92 | + } |
| 93 | + |
| 94 | + if (isPyTorchReductionMissingDimArg(symbol, callExpression)) { |
| 95 | + context.addIssue(callExpression.callee(), PY_TORCH_MESSAGE); |
| 96 | + } |
| 97 | + } |
| 98 | + } |
| 99 | + |
| 100 | + private static Optional<PythonQuickFix> createTfQuickFix(CallExpression callExpression) { |
| 101 | + if(callExpression.arguments().isEmpty()) { |
| 102 | + return Optional.empty(); |
| 103 | + } |
| 104 | + return Optional.of(PythonQuickFix.newQuickFix("Add axis parameter", TextEditUtils.insertBefore(callExpression.rightPar(), ", axis=None"))); |
| 105 | + } |
| 106 | + |
| 107 | + private static boolean isTfReductionMissingAxisArg(Symbol symbol, CallExpression callExpression) { |
| 108 | + return TF_REDUCTION_FUNCTIONS_FQN.contains(symbol.fullyQualifiedName()) |
| 109 | + && TreeUtils.nthArgumentOrKeyword(AXIS_PARAMETER_POSITION, AXIS_PARAMETER, callExpression.arguments()) == null; |
| 110 | + } |
| 111 | + |
| 112 | + private static boolean isPyTorchReductionMissingDimArg(Symbol symbol, CallExpression callExpression) { |
| 113 | + String fqn = symbol.fullyQualifiedName(); |
| 114 | + return fqn != null && PY_TORCH_REDUCTION_FUNCTIONS_DIM_POS.containsKey(fqn) |
| 115 | + && TreeUtils.nthArgumentOrKeyword(PY_TORCH_REDUCTION_FUNCTIONS_DIM_POS.get(fqn), DIM_PARAMETER, callExpression.arguments()) == null; |
| 116 | + } |
| 117 | +} |
0 commit comments