|
| 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.semantic.v2.callgraph; |
| 18 | + |
| 19 | +import java.util.Optional; |
| 20 | +import java.util.Set; |
| 21 | +import java.util.stream.Collectors; |
| 22 | +import org.sonar.plugins.python.api.tree.BaseTreeVisitor; |
| 23 | +import org.sonar.plugins.python.api.tree.CallExpression; |
| 24 | +import org.sonar.plugins.python.api.tree.FileInput; |
| 25 | +import org.sonar.plugins.python.api.tree.FunctionDef; |
| 26 | +import org.sonar.plugins.python.api.tree.Tree; |
| 27 | +import org.sonar.plugins.python.api.types.v2.FunctionType; |
| 28 | +import org.sonar.plugins.python.api.types.v2.ModuleType; |
| 29 | +import org.sonar.plugins.python.api.types.v2.PythonType; |
| 30 | +import org.sonar.plugins.python.api.types.v2.UnionType; |
| 31 | +import org.sonar.plugins.python.api.types.v2.UnknownType.UnresolvedImportType; |
| 32 | +import org.sonar.python.tree.TreeUtils; |
| 33 | + |
| 34 | +public class CallGraphCollector { |
| 35 | + private CallGraphCollector() { |
| 36 | + } |
| 37 | + |
| 38 | + public static CallGraph collectCallGraph(FileInput rootTree) { |
| 39 | + var visitor = new Visitor(); |
| 40 | + rootTree.accept(visitor); |
| 41 | + return visitor.build(); |
| 42 | + } |
| 43 | + |
| 44 | + private static class Visitor extends BaseTreeVisitor { |
| 45 | + private final CallGraph.Builder callGraphBuilder = new CallGraph.Builder(); |
| 46 | + |
| 47 | + @Override |
| 48 | + public void visitCallExpression(CallExpression callExpr) { |
| 49 | + super.visitCallExpression(callExpr); |
| 50 | + getCalledFunctionFqn(callExpr).ifPresent(calledFunctionFqn -> |
| 51 | + getEnclosedFunctionFqn(callExpr).ifPresent(enclosedFunctionFqn -> |
| 52 | + callGraphBuilder.addUsage(enclosedFunctionFqn, calledFunctionFqn) |
| 53 | + ) |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + private static Optional<String> getCalledFunctionFqn(CallExpression callExpr) { |
| 58 | + var calleeType = callExpr.callee().typeV2(); |
| 59 | + return CallGraphCollector.getFqn(calleeType); |
| 60 | + } |
| 61 | + |
| 62 | + private static Optional<String> getEnclosedFunctionFqn(CallExpression callExpr) { |
| 63 | + Tree enclosingFuncDefTree = TreeUtils.firstAncestorOfKind(callExpr, Tree.Kind.FUNCDEF, Tree.Kind.LAMBDA); |
| 64 | + if(enclosingFuncDefTree instanceof FunctionDef enclosingFunctionDef) { |
| 65 | + return CallGraphCollector.getFqn(enclosingFunctionDef.name().typeV2()); |
| 66 | + } |
| 67 | + // lambdas are not supported; thus returning empty |
| 68 | + return Optional.empty(); |
| 69 | + } |
| 70 | + |
| 71 | + public CallGraph build() { |
| 72 | + return callGraphBuilder.build(); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private static Optional<String> getFqn(PythonType type) { |
| 77 | + if(type instanceof FunctionType functionType) { |
| 78 | + return Optional.of(functionType.fullyQualifiedName()); |
| 79 | + } else if (type instanceof ModuleType moduleType) { |
| 80 | + return Optional.of(moduleType.fullyQualifiedName()); |
| 81 | + } else if(type instanceof UnresolvedImportType unresolvedImportType) { |
| 82 | + return Optional.of(unresolvedImportType.importPath()); |
| 83 | + } else if(type instanceof UnionType unionType) { |
| 84 | + Set<String> unionFqnSet = unionType.candidates().stream() |
| 85 | + .flatMap(candidate -> getFqn(candidate).stream()) |
| 86 | + .collect(Collectors.toSet()); |
| 87 | + |
| 88 | + if (unionFqnSet.size() == 1) { |
| 89 | + return unionFqnSet.stream().findFirst(); |
| 90 | + } else { |
| 91 | + // Multiple candidates, cannot determine a single FQN |
| 92 | + return Optional.empty(); |
| 93 | + } |
| 94 | + } else { |
| 95 | + return Optional.empty(); |
| 96 | + } |
| 97 | + } |
| 98 | +} |
0 commit comments