|
| 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.plugins.python; |
| 18 | + |
| 19 | +import java.util.ArrayList; |
| 20 | +import java.util.Comparator; |
| 21 | +import java.util.List; |
| 22 | +import org.sonar.api.batch.sensor.symbol.NewSymbol; |
| 23 | +import org.sonar.api.batch.sensor.symbol.NewSymbolTable; |
| 24 | +import org.sonar.plugins.python.api.symbols.Symbol; |
| 25 | +import org.sonar.plugins.python.api.symbols.Usage; |
| 26 | +import org.sonar.plugins.python.api.tree.BaseTreeVisitor; |
| 27 | +import org.sonar.plugins.python.api.tree.ClassDef; |
| 28 | +import org.sonar.plugins.python.api.tree.ComprehensionExpression; |
| 29 | +import org.sonar.plugins.python.api.tree.DictCompExpression; |
| 30 | +import org.sonar.plugins.python.api.tree.FileInput; |
| 31 | +import org.sonar.plugins.python.api.tree.FunctionDef; |
| 32 | +import org.sonar.plugins.python.api.tree.LambdaExpression; |
| 33 | +import org.sonar.plugins.python.api.tree.Tree; |
| 34 | + |
| 35 | +public class NewSymbolsCollector { |
| 36 | + private final Object monitor; |
| 37 | + |
| 38 | + public NewSymbolsCollector(Object monitor) { |
| 39 | + this.monitor = monitor; |
| 40 | + } |
| 41 | + |
| 42 | + public void collect(NewSymbolTable newSymbolTable, FileInput fileInput) { |
| 43 | + var visitor = new SymbolsCollectingVisitor(newSymbolTable); |
| 44 | + visitor.visitFileInput(fileInput); |
| 45 | + save(newSymbolTable); |
| 46 | + } |
| 47 | + |
| 48 | + private void save(NewSymbolTable newSymbolTable) { |
| 49 | + synchronized (monitor) { |
| 50 | + newSymbolTable.save(); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + private static class SymbolsCollectingVisitor extends BaseTreeVisitor { |
| 55 | + private final NewSymbolTable newSymbolTable; |
| 56 | + |
| 57 | + public SymbolsCollectingVisitor(NewSymbolTable newSymbolTable) { |
| 58 | + this.newSymbolTable = newSymbolTable; |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public void visitClassDef(ClassDef classDef) { |
| 63 | + classDef.classFields().forEach(this::handleSymbol); |
| 64 | + classDef.instanceFields().forEach(this::handleSymbol); |
| 65 | + super.visitClassDef(classDef); |
| 66 | + } |
| 67 | + |
| 68 | + @Override |
| 69 | + public void visitFunctionDef(FunctionDef functionDef) { |
| 70 | + functionDef.localVariables().forEach(this::handleSymbol); |
| 71 | + super.visitFunctionDef(functionDef); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public void visitLambda(LambdaExpression lambdaExpression) { |
| 76 | + lambdaExpression.localVariables().forEach(this::handleSymbol); |
| 77 | + super.visitLambda(lambdaExpression); |
| 78 | + } |
| 79 | + |
| 80 | + @Override |
| 81 | + public void visitPyListOrSetCompExpression(ComprehensionExpression tree) { |
| 82 | + tree.localVariables().forEach(this::handleSymbol); |
| 83 | + super.visitPyListOrSetCompExpression(tree); |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public void visitDictCompExpression(DictCompExpression tree) { |
| 88 | + tree.localVariables().forEach(this::handleSymbol); |
| 89 | + super.visitDictCompExpression(tree); |
| 90 | + } |
| 91 | + |
| 92 | + @Override |
| 93 | + public void visitFileInput(FileInput fileInput) { |
| 94 | + fileInput.globalVariables().forEach(this::handleSymbol); |
| 95 | + super.visitFileInput(fileInput); |
| 96 | + } |
| 97 | + |
| 98 | + private void handleSymbol(Symbol symbol) { |
| 99 | + if (symbol.usages().isEmpty()) { |
| 100 | + // global symbols might not have any usages |
| 101 | + return; |
| 102 | + } |
| 103 | + List<Usage> usages = new ArrayList<>(symbol.usages()); |
| 104 | + usages.sort(Comparator.comparingInt(u -> u.tree().firstToken().line())); |
| 105 | + Tree firstUsageTree = usages.get(0).tree(); |
| 106 | + NewSymbol newSymbol = newSymbolTable.newSymbol(firstUsageTree.firstToken().line(), firstUsageTree.firstToken().column(), |
| 107 | + firstUsageTree.lastToken().line(), firstUsageTree.lastToken().column() + firstUsageTree.lastToken().value().length()); |
| 108 | + for (int i = 1; i < usages.size(); i++) { |
| 109 | + Tree usageTree = usages.get(i).tree(); |
| 110 | + newSymbol.newReference(usageTree.firstToken().line(), usageTree.firstToken().column(), |
| 111 | + usageTree.lastToken().line(), usageTree.lastToken().column() + usageTree.lastToken().value().length()); |
| 112 | + } |
| 113 | + } |
| 114 | + } |
| 115 | +} |
0 commit comments