Skip to content

Commit 728520f

Browse files
committed
Move Assignment#computeDependencies(...) to AstBasedPropagation
1 parent 73f9603 commit 728520f

File tree

3 files changed

+30
-20
lines changed

3 files changed

+30
-20
lines changed

python-frontend/src/main/java/org/sonar/python/semantic/v2/types/Assignment.java

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,11 @@
1616
*/
1717
package org.sonar.python.semantic.v2.types;
1818

19-
import java.util.ArrayDeque;
20-
import java.util.Deque;
2119
import java.util.Map;
2220
import java.util.Set;
2321
import org.sonar.plugins.python.api.tree.Expression;
2422
import org.sonar.plugins.python.api.tree.Name;
2523
import org.sonar.python.semantic.v2.SymbolV2;
26-
import org.sonar.python.types.HasTypeDependencies;
2724
import org.sonar.python.types.v2.PythonType;
2825

2926
public class Assignment extends Propagation {
@@ -37,22 +34,6 @@ public Assignment(SymbolV2 lhsSymbol, Name lhsName, Expression rhs, Map<SymbolV2
3734
this.propagationsByLhs = propagationsByLhs;
3835
}
3936

40-
void computeDependencies(Set<SymbolV2> trackedVars) {
41-
Deque<Expression> workList = new ArrayDeque<>();
42-
workList.push(rhs);
43-
while (!workList.isEmpty()) {
44-
Expression e = workList.pop();
45-
if (e instanceof Name name) {
46-
SymbolV2 symbol = name.symbolV2();
47-
if (symbol != null && trackedVars.contains(symbol)) {
48-
variableDependencies.add(symbol);
49-
propagationsByLhs.get(symbol).forEach(a -> a.dependents.add(this));
50-
}
51-
} else if (e instanceof HasTypeDependencies hasTypeDependencies) {
52-
workList.addAll(hasTypeDependencies.typeDependencies());
53-
}
54-
}
55-
}
5637

5738
public Name lhsName() {
5839
return lhsName;

python-frontend/src/main/java/org/sonar/python/semantic/v2/types/AstBasedPropagation.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919
*/
2020
package org.sonar.python.semantic.v2.types;
2121

22+
import java.util.ArrayDeque;
23+
import java.util.Deque;
2224
import java.util.HashSet;
2325
import java.util.Iterator;
2426
import java.util.Map;
@@ -36,6 +38,7 @@
3638
import org.sonar.python.semantic.v2.UsageV2;
3739
import org.sonar.python.tree.NameImpl;
3840
import org.sonar.python.tree.TreeUtils;
41+
import org.sonar.python.types.HasTypeDependencies;
3942
import org.sonar.python.types.v2.PythonType;
4043
import org.sonar.python.types.v2.UnionType;
4144

@@ -65,11 +68,28 @@ private void computePropagationDependencies(Set<SymbolV2> trackedVars) {
6568
props.stream()
6669
.filter(Assignment.class::isInstance)
6770
.map(Assignment.class::cast)
68-
.forEach(a -> a.computeDependencies(trackedVars));
71+
.forEach(assignment -> computeDependencies(assignment, trackedVars));
6972
}
7073
});
7174
}
7275

76+
private void computeDependencies(Assignment assignment, Set<SymbolV2> trackedVars) {
77+
Deque<Expression> workList = new ArrayDeque<>();
78+
workList.push(assignment.rhs());
79+
while (!workList.isEmpty()) {
80+
Expression e = workList.pop();
81+
if (e instanceof Name name) {
82+
SymbolV2 symbol = name.symbolV2();
83+
if (symbol != null && trackedVars.contains(symbol)) {
84+
assignment.addVariableDependencies(symbol);
85+
propagationsByLhs.get(symbol).forEach(propagation -> propagation.addDependent(assignment));
86+
}
87+
} else if (e instanceof HasTypeDependencies hasTypeDependencies) {
88+
workList.addAll(hasTypeDependencies.typeDependencies());
89+
}
90+
}
91+
}
92+
7393
private Set<Propagation> getTrackedPropagation(Set<SymbolV2> trackedVars) {
7494
Set<Propagation> trackedPropagations = new HashSet<>();
7595
propagationsByLhs.forEach((lhs, propagations) -> {

python-frontend/src/main/java/org/sonar/python/semantic/v2/types/Propagation.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,13 @@ Set<Propagation> dependents() {
5959
public SymbolV2 lhsSymbol() {
6060
return lhsSymbol;
6161
}
62+
63+
public void addVariableDependencies(SymbolV2 dependency) {
64+
variableDependencies.add(dependency);
65+
}
66+
67+
public void addDependent(Propagation dependent) {
68+
dependents.add(dependent);
69+
}
70+
6271
}

0 commit comments

Comments
 (0)