Skip to content

Commit

Permalink
fix after review
Browse files Browse the repository at this point in the history
  • Loading branch information
guillaume-dequenne-sonarsource committed May 13, 2024
1 parent 68ead15 commit 7f31bba
Showing 1 changed file with 17 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@

import java.util.HashSet;
import java.util.Set;
import java.util.stream.Stream;
import org.sonar.plugins.python.api.tree.Expression;
import org.sonar.plugins.python.api.tree.Name;
import org.sonar.plugins.python.api.tree.Tree;
import org.sonar.python.semantic.v2.SymbolV2;
import org.sonar.python.semantic.v2.SymbolV2Utils;
import org.sonar.python.semantic.v2.UsageV2;
Expand All @@ -32,15 +34,17 @@

public abstract class Propagation {

final Set<SymbolV2> variableDependencies = new HashSet<>();
final Set<Propagation> dependents = new HashSet<>();
final Set<SymbolV2> variableDependencies;
final Set<Propagation> dependents;

final SymbolV2 lhsSymbol;
final Name lhsName;

protected Propagation(SymbolV2 lhsSymbol, Name lhsName) {
this.lhsSymbol = lhsSymbol;
this.lhsName = lhsName;
this.variableDependencies = new HashSet<>();
this.dependents = new HashSet<>();
}

/**
Expand All @@ -50,9 +54,7 @@ protected Propagation(SymbolV2 lhsSymbol, Name lhsName) {
boolean propagate(Set<SymbolV2> initializedVars) {
PythonType rhsType = rhsType();
if (initializedVars.add(lhsSymbol)) {
lhsSymbol.usages().stream()
.filter(u -> !SymbolV2Utils.isFunctionOrClassDeclaration(u))
.map(UsageV2::tree)
getSymbolNonDeclarationUsageTrees(lhsSymbol)
.filter(NameImpl.class::isInstance)
.map(NameImpl.class::cast)
.forEach(n -> n.typeV2(rhsType));
Expand All @@ -63,17 +65,23 @@ boolean propagate(Set<SymbolV2> initializedVars) {
return false;
}
PythonType newType = UnionType.or(rhsType, currentType);
lhsSymbol.usages()
.stream()
.filter(u -> !SymbolV2Utils.isFunctionOrClassDeclaration(u))
.map(UsageV2::tree)
getSymbolNonDeclarationUsageTrees(lhsSymbol)
.filter(NameImpl.class::isInstance)
.map(NameImpl.class::cast)
.forEach(n -> n.typeV2(newType));
return !newType.equals(currentType);
}
}

public static Stream<Tree> getSymbolNonDeclarationUsageTrees(SymbolV2 symbol) {
return symbol.usages()
.stream()
// Function and class definition names will always have FunctionType and ClassType respectively
// so they are filtered out of type propagation
.filter(u -> !SymbolV2Utils.isFunctionOrClassDeclaration(u))
.map(UsageV2::tree);
}

boolean areDependenciesReady(Set<SymbolV2> initializedVars) {
return initializedVars.containsAll(variableDependencies);
}
Expand Down

0 comments on commit 7f31bba

Please sign in to comment.