Skip to content

Commit 90377ae

Browse files
committed
Javadoc and formatting
1 parent f5cd7a8 commit 90377ae

File tree

12 files changed

+85
-55
lines changed

12 files changed

+85
-55
lines changed

lisa/src/main/java/it/unive/lisa/outputs/DotCFG.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
package it.unive.lisa.outputs;
22

3-
import java.io.Reader;
4-
import java.util.function.Function;
5-
6-
import org.graphstream.graph.implementations.MultiGraph;
7-
83
import it.unive.lisa.program.cfg.CFG;
94
import it.unive.lisa.program.cfg.edge.Edge;
105
import it.unive.lisa.program.cfg.edge.FalseEdge;
116
import it.unive.lisa.program.cfg.edge.TrueEdge;
127
import it.unive.lisa.program.cfg.statement.Statement;
8+
import java.io.Reader;
9+
import java.util.function.Function;
10+
import org.graphstream.graph.implementations.MultiGraph;
1311

1412
/**
1513
* An {@link DotGraph} built from a {@link CFG}. Instances of this class can be

lisa/src/main/java/it/unive/lisa/program/Unit.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ public void validateAndFinalize() throws ProgramValidationException {
368368
throw new ProgramValidationException(
369369
cfg.getDescriptor().getSignature() + " is duplicated within unit " + this);
370370
}
371-
371+
372372
for (CFG cfg : getAllCFGs())
373373
cfg.validate();
374374
}

lisa/src/main/java/it/unive/lisa/program/cfg/CFG.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,5 @@
11
package it.unive.lisa.program.cfg;
22

3-
import java.util.Collection;
4-
import java.util.HashMap;
5-
import java.util.HashSet;
6-
import java.util.LinkedList;
7-
import java.util.List;
8-
import java.util.Map;
9-
import java.util.Map.Entry;
10-
import java.util.function.Function;
11-
import java.util.stream.Collectors;
12-
13-
import org.apache.commons.lang3.tuple.Pair;
14-
import org.apache.logging.log4j.LogManager;
15-
import org.apache.logging.log4j.Logger;
16-
173
import it.unive.lisa.analysis.AbstractState;
184
import it.unive.lisa.analysis.AnalysisState;
195
import it.unive.lisa.analysis.CFGWithAnalysisResults;
@@ -39,6 +25,18 @@
3925
import it.unive.lisa.util.datastructures.graph.FixpointGraph;
4026
import it.unive.lisa.util.workset.FIFOWorkingSet;
4127
import it.unive.lisa.util.workset.WorkingSet;
28+
import java.util.Collection;
29+
import java.util.HashMap;
30+
import java.util.HashSet;
31+
import java.util.LinkedList;
32+
import java.util.List;
33+
import java.util.Map;
34+
import java.util.Map.Entry;
35+
import java.util.function.Function;
36+
import java.util.stream.Collectors;
37+
import org.apache.commons.lang3.tuple.Pair;
38+
import org.apache.logging.log4j.LogManager;
39+
import org.apache.logging.log4j.Logger;
4240

4341
/**
4442
* A control flow graph, that has {@link Statement}s as nodes and {@link Edge}s
@@ -870,6 +868,22 @@ public String toString() {
870868
};
871869
}
872870

871+
/**
872+
* Validates this cfg, ensuring that the code contained in it is well
873+
* formed. This method checks that:
874+
* <ul>
875+
* <li>each {@link Edge} is connected to {@link Statement}s contained in the
876+
* cfg</li>
877+
* <li>all {@link Statement}s with no ingoing edges are marked as
878+
* entrypoints of the cfg (i.e. no deadcode)</li>
879+
* <li>all {@link Statement}s that stop the execution (according to
880+
* {@link Statement#stopsExecution()}) do not have outgoing edges</li>
881+
* <li>all entrypoints are effectively part of this cfg</li>
882+
* </ul>
883+
*
884+
* @throws ProgramValidationException if one of the aforementioned checks
885+
* fail
886+
*/
873887
public void validate() throws ProgramValidationException {
874888
Collection<Statement> nodes = adjacencyMatrix.getNodes();
875889

lisa/src/main/java/it/unive/lisa/program/cfg/statement/Ret.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public int setOffset(int offset) {
5555
public boolean stopsExecution() {
5656
return true;
5757
}
58-
58+
5959
@Override
6060
public int hashCode() {
6161
return super.hashCode() ^ getClass().getName().hashCode();

lisa/src/main/java/it/unive/lisa/program/cfg/statement/Return.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public final String toString() {
5959
public boolean stopsExecution() {
6060
return true;
6161
}
62-
62+
6363
@Override
6464
public final Identifier getMetaVariable() {
6565
return new ValueIdentifier(getExpression().getRuntimeTypes(),

lisa/src/main/java/it/unive/lisa/program/cfg/statement/Statement.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,11 +62,25 @@ public final CFG getCFG() {
6262
public final int getOffset() {
6363
return offset;
6464
}
65-
65+
66+
/**
67+
* Whether or not this statement stops the execution of the containing cfg,
68+
* either by throwing an error or returning a value. To distinguish
69+
* error-raising halting statements and normal ones, use
70+
* {@link #throwsError()}.
71+
*
72+
* @return {@code true} only if that condition holds
73+
*/
6674
public boolean stopsExecution() {
6775
return false;
6876
}
69-
77+
78+
/**
79+
* Whether or not this statement throws an error, halting the normal
80+
* execution of the containing cfg.
81+
*
82+
* @return {@code true} only if that condition holds
83+
*/
7084
public boolean throwsError() {
7185
return false;
7286
}

lisa/src/main/java/it/unive/lisa/program/cfg/statement/Throw.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,12 @@ public final String toString() {
5656
public boolean stopsExecution() {
5757
return true;
5858
}
59-
59+
6060
@Override
6161
public boolean throwsError() {
6262
return true;
6363
}
64-
64+
6565
@Override
6666
public <A extends AbstractState<A, H, V>,
6767
H extends HeapDomain<H>,

lisa/src/main/java/it/unive/lisa/util/datastructures/graph/AdjacencyMatrix.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,11 @@ public final Collection<N> predecessorsOf(N node) {
184184
* simplified has an outgoing edge that is not simplifiable, according to
185185
* {@link Edge#canBeSimplified()}.
186186
*
187-
* @param targets the set of the {@link Node}s that needs to be simplified
187+
* @param targets the set of the {@link Node}s that needs to be
188+
* simplified
189+
* @param entrypoints the collection of {@link Node}s that are considered as
190+
* entrypoints of the graph built over this adjacency
191+
* matrix
188192
*
189193
* @throws UnsupportedOperationException if there exists at least one node
190194
* being simplified with an
@@ -195,8 +199,8 @@ public synchronized void simplify(Set<N> targets, Collection<N> entrypoints) {
195199
ExternalSet<E> ingoing = matrix.get(t).getLeft();
196200
ExternalSet<E> outgoing = matrix.get(t).getRight();
197201
boolean entry = entrypoints.contains(t);
198-
199-
if (ingoing.isEmpty() && !outgoing.isEmpty())
202+
203+
if (ingoing.isEmpty() && !outgoing.isEmpty())
200204
// this is a source node
201205
for (E out : outgoing) {
202206
if (!out.canBeSimplified())
@@ -218,28 +222,29 @@ else if (!ingoing.isEmpty() && outgoing.isEmpty())
218222
// remove the edge
219223
matrix.get(in.getSource()).getRight().remove(in);
220224
}
221-
else
225+
else
222226
// normal intermediate edge
223-
for (E in : ingoing)
227+
for (E in : ingoing)
224228
for (E out : outgoing) {
225229
if (!out.canBeSimplified())
226230
throw new UnsupportedOperationException(
227231
"Cannot simplify an edge with class " + out.getClass().getSimpleName());
228-
229-
// replicate the edge from ingoing.source to outgoing.dest
232+
233+
// replicate the edge from ingoing.source to
234+
// outgoing.dest
230235
E _new = in.newInstance(in.getSource(), out.getDestination());
231-
236+
232237
// swap the ingoing edge
233238
matrix.get(in.getSource()).getRight().remove(in);
234239
matrix.get(in.getSource()).getRight().add(_new);
235-
240+
236241
// swap the outgoing edge
237242
matrix.get(out.getDestination()).getLeft().remove(out);
238243
matrix.get(out.getDestination()).getLeft().add(_new);
239244
}
240245

241246
// remove the simplified node
242-
if (entry)
247+
if (entry)
243248
entrypoints.remove(t);
244249
matrix.remove(t);
245250
}

lisa/src/test/java/it/unive/lisa/test/AnalysisTest.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,20 @@
44
import static org.junit.Assert.assertTrue;
55
import static org.junit.Assert.fail;
66

7-
import java.io.File;
8-
import java.io.FileNotFoundException;
9-
import java.io.FileReader;
10-
import java.io.IOException;
11-
import java.nio.file.Path;
12-
import java.nio.file.Paths;
13-
14-
import org.apache.commons.io.FileUtils;
15-
167
import it.unive.lisa.AnalysisException;
178
import it.unive.lisa.LiSA;
189
import it.unive.lisa.analysis.AbstractState;
1910
import it.unive.lisa.outputs.JsonReport;
2011
import it.unive.lisa.program.Program;
2112
import it.unive.lisa.test.imp.IMPFrontend;
2213
import it.unive.lisa.test.imp.ParsingException;
14+
import java.io.File;
15+
import java.io.FileNotFoundException;
16+
import java.io.FileReader;
17+
import java.io.IOException;
18+
import java.nio.file.Path;
19+
import java.nio.file.Paths;
20+
import org.apache.commons.io.FileUtils;
2321

2422
public abstract class AnalysisTest {
2523

lisa/src/test/java/it/unive/lisa/test/cfg/CFGSimplificationTest.java

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import static org.junit.Assert.assertTrue;
44

5-
import org.junit.Test;
6-
75
import it.unive.lisa.analysis.AbstractState;
86
import it.unive.lisa.analysis.AnalysisState;
97
import it.unive.lisa.analysis.HeapDomain;
@@ -27,6 +25,7 @@
2725
import it.unive.lisa.program.cfg.statement.VariableRef;
2826
import it.unive.lisa.symbolic.SymbolicExpression;
2927
import it.unive.lisa.type.Untyped;
28+
import org.junit.Test;
3029

3130
public class CFGSimplificationTest {
3231

@@ -232,8 +231,10 @@ public void testSimplificationAtTheStart() throws ProgramValidationException {
232231
public void testSimplificationAtTheEnd() throws ProgramValidationException {
233232
CompilationUnit unit = new CompilationUnit(null, -1, -1, "foo", false);
234233
CFG first = new CFG(new CFGDescriptor(unit, false, "foo"));
235-
Assignment assign1 = new Assignment(first, new VariableRef(first, "x"), new Literal(first, 5, Untyped.INSTANCE));
236-
Assignment assign2 = new Assignment(first, new VariableRef(first, "y"), new Literal(first, 50, Untyped.INSTANCE));
234+
Assignment assign1 = new Assignment(first, new VariableRef(first, "x"),
235+
new Literal(first, 5, Untyped.INSTANCE));
236+
Assignment assign2 = new Assignment(first, new VariableRef(first, "y"),
237+
new Literal(first, 50, Untyped.INSTANCE));
237238
NoOp end = new NoOp(first);
238239
first.addNode(assign1, true);
239240
first.addNode(assign2);
@@ -263,8 +264,10 @@ public void testSimplificationAtTheEndWithBranch() throws ProgramValidationExcep
263264
CFG first = new CFG(new CFGDescriptor(unit, false, "foo"));
264265
Assignment assign1 = new Assignment(first, new VariableRef(first, "b"),
265266
new Literal(first, true, Untyped.INSTANCE));
266-
Assignment assign2 = new Assignment(first, new VariableRef(first, "x"), new Literal(first, 5, Untyped.INSTANCE));
267-
Assignment assign3 = new Assignment(first, new VariableRef(first, "y"), new Literal(first, 50, Untyped.INSTANCE));
267+
Assignment assign2 = new Assignment(first, new VariableRef(first, "x"),
268+
new Literal(first, 5, Untyped.INSTANCE));
269+
Assignment assign3 = new Assignment(first, new VariableRef(first, "y"),
270+
new Literal(first, 50, Untyped.INSTANCE));
268271
NoOp end = new NoOp(first);
269272
first.addNode(end);
270273
first.addNode(assign1, true);

0 commit comments

Comments
 (0)