Skip to content

Commit e5792f3

Browse files
committed
Small Propagation refactoring
1 parent 728520f commit e5792f3

File tree

8 files changed

+19
-36
lines changed

8 files changed

+19
-36
lines changed

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

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

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

2624
public class Assignment extends Propagation {
2725

28-
Expression rhs;
29-
Map<SymbolV2, Set<Propagation>> propagationsByLhs;
26+
private Expression rhs;
3027

31-
public Assignment(SymbolV2 lhsSymbol, Name lhsName, Expression rhs, Map<SymbolV2, Set<Propagation>> propagationsByLhs) {
28+
public Assignment(SymbolV2 lhsSymbol, Name lhsName, Expression rhs) {
3229
super(lhsSymbol, lhsName);
3330
this.rhs = rhs;
34-
this.propagationsByLhs = propagationsByLhs;
35-
}
36-
37-
38-
public Name lhsName() {
39-
return lhsName;
4031
}
4132

4233
@Override

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ public boolean propagate(Propagation propagation, Set<SymbolV2> initializedVars)
141141

142142
private void propagateTypeToUsages(Propagation propagation, PythonType newType) {
143143
Tree scopeTree = propagation.scopeTree(propagation.lhsName());
144-
getSymbolNonDeclarationUsageTrees(propagation.lhsSymbol)
144+
getSymbolNonDeclarationUsageTrees(propagation.lhsSymbol())
145145
.filter(NameImpl.class::isInstance)
146146
.map(NameImpl.class::cast)
147147
// Avoid propagation to usages in nested scopes, as this may lead to FPs

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,17 +32,14 @@ public Definition(SymbolV2 symbol, Name name) {
3232
super(symbol, name);
3333
}
3434

35-
public Name lhsName() {
36-
return lhsName;
37-
}
3835

3936
@Override
4037
public PythonType rhsType() {
41-
return lhsName.typeV2();
38+
return lhsName().typeV2();
4239
}
4340

4441
@Override
4542
Tree scopeTree(Name name) {
46-
return TreeUtils.firstAncestor(name, t -> !t.equals(lhsName.parent()) && t.is(Tree.Kind.FUNCDEF, Tree.Kind.FILE_INPUT, Tree.Kind.CLASSDEF));
43+
return TreeUtils.firstAncestor(name, t -> !t.equals(lhsName().parent()) && t.is(Tree.Kind.FUNCDEF, Tree.Kind.FILE_INPUT, Tree.Kind.CLASSDEF));
4744
}
4845
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ private void handleDefinitions(Statement definitionStatement, TypeInferenceProgr
180180
.ifPresent(definitions -> definitions.forEach(d -> {
181181
SymbolV2 symbol = d.lhsSymbol();
182182
if (trackedVars.contains(symbol)) {
183-
programState.setTypes(symbol, Set.of(d.lhsName.typeV2()));
183+
programState.setTypes(symbol, Set.of(d.lhsName().typeV2()));
184184
}
185185
}));
186186
}

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,7 @@
1717
package org.sonar.python.semantic.v2.types;
1818

1919
import java.util.Collection;
20-
import java.util.Map;
2120
import java.util.Optional;
22-
import java.util.Set;
2321
import java.util.stream.Stream;
2422
import org.sonar.plugins.python.api.tree.Expression;
2523
import org.sonar.plugins.python.api.tree.Name;
@@ -29,8 +27,8 @@
2927
import org.sonar.python.types.v2.TriBool;
3028

3129
public class LoopAssignment extends Assignment {
32-
public LoopAssignment(SymbolV2 lhsSymbol, Name lhsName, Expression rhs, Map<SymbolV2, Set<Propagation>> propagationsByLhs) {
33-
super(lhsSymbol, lhsName, rhs, propagationsByLhs);
30+
public LoopAssignment(SymbolV2 lhsSymbol, Name lhsName, Expression rhs) {
31+
super(lhsSymbol, lhsName, rhs);
3432
}
3533

3634
@Override

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,6 @@ protected ParameterDefinition(SymbolV2 lhsSymbol, Name lhsName) {
2626
super(lhsSymbol, lhsName);
2727
}
2828

29-
@Override
30-
public Name lhsName() {
31-
return lhsName;
32-
}
3329

3430
@Override
3531
public PythonType rhsType() {

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

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,11 @@
2525
import org.sonar.python.types.v2.PythonType;
2626

2727
public abstract class Propagation {
28+
private final Set<SymbolV2> variableDependencies;
29+
private final Set<Propagation> dependents;
2830

29-
final Set<SymbolV2> variableDependencies;
30-
final Set<Propagation> dependents;
31-
32-
final SymbolV2 lhsSymbol;
33-
final Name lhsName;
31+
private final SymbolV2 lhsSymbol;
32+
private final Name lhsName;
3433

3534
protected Propagation(SymbolV2 lhsSymbol, Name lhsName) {
3635
this.lhsSymbol = lhsSymbol;
@@ -52,7 +51,9 @@ Set<Propagation> dependents() {
5251
return dependents;
5352
}
5453

55-
public abstract Name lhsName();
54+
public Name lhsName() {
55+
return lhsName;
56+
}
5657

5758
public abstract PythonType rhsType();
5859

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -172,9 +172,9 @@ public void visitForStatement(ForStatement forStatement) {
172172
.findFirst()
173173
.filter(NameImpl.class::isInstance)
174174
.map(NameImpl.class::cast)
175-
.ifPresent(i -> {
176-
var symbol = i.symbolV2();
177-
var assignment = new LoopAssignment(symbol, i, rhsExpression, propagationsByLhs);
175+
.ifPresent(name -> {
176+
var symbol = name.symbolV2();
177+
var assignment = new LoopAssignment(symbol, name, rhsExpression);
178178
assignmentsByAssignmentStatement.put(forStatement, assignment);
179179
propagationsByLhs.computeIfAbsent(symbol, s -> new HashSet<>()).add(assignment);
180180
})
@@ -191,7 +191,7 @@ private void processAssignment(Statement assignmentStatement, Expression lhsExpr
191191
if (symbol == null) {
192192
return;
193193
}
194-
var assignment = new Assignment(symbol, lhs, rhsExpression, propagationsByLhs);
194+
var assignment = new Assignment(symbol, lhs, rhsExpression);
195195
assignmentsByAssignmentStatement.put(assignmentStatement, assignment);
196196
propagationsByLhs.computeIfAbsent(symbol, s -> new HashSet<>()).add(assignment);
197197
}

0 commit comments

Comments
 (0)