|
| 1 | +/* |
| 2 | + * SonarQube Python Plugin |
| 3 | + * Copyright (C) SonarSource Sàrl |
| 4 | + * mailto:info AT sonarsource DOT com |
| 5 | + * |
| 6 | + * You can redistribute and/or modify this program under the terms of |
| 7 | + * the Sonar Source-Available License Version 1, as published by SonarSource Sàrl. |
| 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.ArrayDeque; |
| 20 | +import java.util.ArrayList; |
| 21 | +import java.util.Deque; |
| 22 | +import java.util.HashSet; |
| 23 | +import java.util.List; |
| 24 | +import java.util.Set; |
| 25 | +import javax.annotation.CheckForNull; |
| 26 | +import org.sonar.check.Rule; |
| 27 | +import org.sonar.plugins.python.api.PythonSubscriptionCheck; |
| 28 | +import org.sonar.plugins.python.api.SubscriptionContext; |
| 29 | +import org.sonar.plugins.python.api.tree.ArgList; |
| 30 | +import org.sonar.plugins.python.api.tree.Argument; |
| 31 | +import org.sonar.plugins.python.api.tree.ClassDef; |
| 32 | +import org.sonar.plugins.python.api.tree.Expression; |
| 33 | +import org.sonar.plugins.python.api.tree.RegularArgument; |
| 34 | +import org.sonar.plugins.python.api.tree.Tree; |
| 35 | +import org.sonar.plugins.python.api.types.v2.ClassType; |
| 36 | +import org.sonar.plugins.python.api.types.v2.PythonType; |
| 37 | +import org.sonar.plugins.python.api.types.v2.TypeWrapper; |
| 38 | + |
| 39 | +@Rule(key = "S8511") |
| 40 | +public class MultipleInheritanceMROConflictCheck extends PythonSubscriptionCheck { |
| 41 | + |
| 42 | + private static final String MESSAGE = "Reorder or remove base classes to fix this MRO conflict."; |
| 43 | + private static final String SECONDARY_MESSAGE = "This base class is an ancestor of another listed base class appearing after it."; |
| 44 | + |
| 45 | + @Override |
| 46 | + public void initialize(Context context) { |
| 47 | + context.registerSyntaxNodeConsumer(Tree.Kind.CLASSDEF, MultipleInheritanceMROConflictCheck::checkClassDef); |
| 48 | + } |
| 49 | + |
| 50 | + private static void checkClassDef(SubscriptionContext ctx) { |
| 51 | + ClassDef classDef = (ClassDef) ctx.syntaxNode(); |
| 52 | + ArgList argList = classDef.args(); |
| 53 | + if (argList == null) { |
| 54 | + return; |
| 55 | + } |
| 56 | + |
| 57 | + List<Expression> bases = collectPositionalBases(argList); |
| 58 | + if (bases.size() < 2) { |
| 59 | + return; |
| 60 | + } |
| 61 | + |
| 62 | + List<ClassType> types = new ArrayList<>(); |
| 63 | + for (Expression base : bases) { |
| 64 | + types.add(resolveClassType(base)); |
| 65 | + } |
| 66 | + |
| 67 | + int conflictIndex = findAncestorConflictIndex(types); |
| 68 | + if (hasMroConflict(types, conflictIndex)) { |
| 69 | + PreciseIssue issue = ctx.addIssue(classDef.name(), MESSAGE); |
| 70 | + if (conflictIndex >= 0) { |
| 71 | + issue.secondary(bases.get(conflictIndex), SECONDARY_MESSAGE); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static List<Expression> collectPositionalBases(ArgList argList) { |
| 77 | + List<Expression> bases = new ArrayList<>(); |
| 78 | + for (Argument argument : argList.arguments()) { |
| 79 | + if (argument instanceof RegularArgument regularArgument && regularArgument.keywordArgument() == null) { |
| 80 | + bases.add(regularArgument.expression()); |
| 81 | + } |
| 82 | + } |
| 83 | + return bases; |
| 84 | + } |
| 85 | + |
| 86 | + /** |
| 87 | + * Detects an MRO conflict: either via C3 when every base is fully resolved, or otherwise only |
| 88 | + * when {@link #findAncestorConflictIndex} finds an earlier base that is a strict superclass of a |
| 89 | + * later base in our type graph (same structural issue as {@code class C(A, B)} with {@code B} |
| 90 | + * subclassing {@code A}). |
| 91 | + */ |
| 92 | + private static boolean hasMroConflict(List<ClassType> types, int conflictIndex) { |
| 93 | + return isFullyResolved(types) |
| 94 | + ? !ClassType.wouldHaveValidMro(types) |
| 95 | + : (conflictIndex >= 0); |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Returns {@code true} if every base class is fully resolved (non-null, with a fully known type |
| 100 | + * hierarchy). Only when this is true can we run the complete C3 algorithm safely. |
| 101 | + */ |
| 102 | + private static boolean isFullyResolved(List<ClassType> types) { |
| 103 | + for (ClassType type : types) { |
| 104 | + if (type == null || type.hasUnresolvedHierarchy()) { |
| 105 | + return false; |
| 106 | + } |
| 107 | + } |
| 108 | + return true; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * Returns the index {@code i} of the first base class that is an ancestor of some later base |
| 113 | + * class at index {@code j > i}, or {@code -1} if no such pair exists. |
| 114 | + */ |
| 115 | + private static int findAncestorConflictIndex(List<ClassType> types) { |
| 116 | + for (int i = 0; i < types.size() - 1; i++) { |
| 117 | + if (laterBaseExtendsEarlier(types, i)) { |
| 118 | + return i; |
| 119 | + } |
| 120 | + } |
| 121 | + return -1; |
| 122 | + } |
| 123 | + |
| 124 | + private static boolean laterBaseExtendsEarlier(List<ClassType> types, int i) { |
| 125 | + ClassType typeI = types.get(i); |
| 126 | + if (typeI == null) { |
| 127 | + return false; |
| 128 | + } |
| 129 | + for (int j = i + 1; j < types.size(); j++) { |
| 130 | + ClassType typeJ = types.get(j); |
| 131 | + if (typeJ != null && isOrExtendsClass(typeJ, typeI)) { |
| 132 | + return true; |
| 133 | + } |
| 134 | + } |
| 135 | + return false; |
| 136 | + } |
| 137 | + |
| 138 | + /** |
| 139 | + * Returns {@code true} if {@code candidate} is or extends {@code ancestor} (i.e., {@code ancestor} |
| 140 | + * appears anywhere in {@code candidate}'s type hierarchy). Uses reference equality since |
| 141 | + * {@link ClassType} instances are canonical singletons within a single analysis. |
| 142 | + */ |
| 143 | + private static boolean isOrExtendsClass(ClassType candidate, ClassType ancestor) { |
| 144 | + Set<PythonType> visited = new HashSet<>(); |
| 145 | + Deque<PythonType> queue = new ArrayDeque<>(); |
| 146 | + queue.add(candidate); |
| 147 | + while (!queue.isEmpty()) { |
| 148 | + PythonType current = queue.poll(); |
| 149 | + if (!visited.add(current)) { |
| 150 | + continue; |
| 151 | + } |
| 152 | + if (current == ancestor) { |
| 153 | + return true; |
| 154 | + } |
| 155 | + if (current instanceof ClassType ct) { |
| 156 | + enqueueDirectSuperclasses(ct, queue); |
| 157 | + } |
| 158 | + } |
| 159 | + return false; |
| 160 | + } |
| 161 | + |
| 162 | + private static void enqueueDirectSuperclasses(ClassType ct, Deque<PythonType> queue) { |
| 163 | + for (TypeWrapper sw : ct.superClasses()) { |
| 164 | + queue.add(sw.type()); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + @CheckForNull |
| 169 | + private static ClassType resolveClassType(Expression expression) { |
| 170 | + PythonType type = expression.typeV2(); |
| 171 | + return (type instanceof ClassType classType) ? classType : null; |
| 172 | + } |
| 173 | +} |
0 commit comments