Updated Line number to be a field of the Node of the CFG
This commit is contained in:
@@ -40,52 +40,13 @@ public class CFGBuilder {
|
||||
|
||||
ControlFlowGraph cfg = cfgs.get(0);
|
||||
|
||||
extractLineNumbers(cfg, tree);
|
||||
|
||||
System.out.println("CFG built: " + cfg.nodes().size() + " nodes, " + cfg.edges().size() + " edges");
|
||||
|
||||
return cfg;
|
||||
}
|
||||
|
||||
private void extractLineNumbers(ControlFlowGraph cfg, ParseTree tree) {
|
||||
Map<String, Integer> labelToLine = new HashMap<>();
|
||||
collectLineNumbers(tree, labelToLine);
|
||||
|
||||
for (Node node : cfg.nodes()) {
|
||||
String label = node.label();
|
||||
Integer line = labelToLine.get(label);
|
||||
|
||||
if (line == null) {
|
||||
for (Map.Entry<String, Integer> entry : labelToLine.entrySet()) {
|
||||
if (label.contains(entry.getKey()) || entry.getKey().contains(label)) {
|
||||
line = entry.getValue();
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
nodeToLine.put(node, line != null ? line : -1);
|
||||
}
|
||||
}
|
||||
|
||||
private void collectLineNumbers(ParseTree tree, Map<String, Integer> map) {
|
||||
if (tree instanceof ParserRuleContext) {
|
||||
ParserRuleContext ctx = (ParserRuleContext) tree;
|
||||
if (ctx.start != null) {
|
||||
String text = ctx.getText();
|
||||
if (text != null && text.length() < 200 && text.length() > 0) {
|
||||
map.put(text, ctx.start.getLine());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for (int i = 0; i < tree.getChildCount(); i++) {
|
||||
collectLineNumbers(tree.getChild(i), map);
|
||||
}
|
||||
}
|
||||
|
||||
public int getLineNumber(Node node) {
|
||||
return nodeToLine.getOrDefault(node, -1);
|
||||
return node.getLineNumber();
|
||||
}
|
||||
|
||||
public List<Integer> getAllLineNumbers() {
|
||||
|
||||
@@ -16,13 +16,23 @@ public class Node {
|
||||
private Set<Edge> outEdges = new HashSet<>();
|
||||
private String label;
|
||||
|
||||
private int lineNumber;
|
||||
|
||||
Node(String label) {
|
||||
if(label == null || label.length() < 1)
|
||||
if (label == null || label.length() < 1)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
this.label = label;
|
||||
}
|
||||
|
||||
public void setLineNumber(int lineNumber) {
|
||||
this.lineNumber = lineNumber;
|
||||
}
|
||||
|
||||
public int getLineNumber() {
|
||||
return this.lineNumber;
|
||||
}
|
||||
|
||||
/**
|
||||
* Accesses the label for this node.
|
||||
*
|
||||
@@ -63,20 +73,21 @@ public class Node {
|
||||
* Adds the indicated edge to this node as an in-edge.
|
||||
*
|
||||
* @param edge
|
||||
* The edge to be added as an in-edge
|
||||
* The edge to be added as an in-edge
|
||||
* @throws IllegalArgumentException
|
||||
* If the edge is null.
|
||||
* If the edge is null.
|
||||
* @throws IllegalStateException
|
||||
* If the target node of the edge is not identical to this node.
|
||||
* If the target node of the edge is not
|
||||
* identical to this node.
|
||||
*/
|
||||
public void addInEdge(Edge edge) {
|
||||
if(edge == null)
|
||||
if (edge == null)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
if(edge.target() != this)
|
||||
if (edge.target() != this)
|
||||
throw new IllegalStateException();
|
||||
|
||||
if(edge.source().graph() == null || edge.target().graph() == null)
|
||||
if (edge.source().graph() == null || edge.target().graph() == null)
|
||||
throw new IllegalStateException();
|
||||
|
||||
inEdges.add(edge);
|
||||
@@ -86,31 +97,31 @@ public class Node {
|
||||
* Adds the indicated edge to this node as an out-edge.
|
||||
*
|
||||
* @param edge
|
||||
* The edge to be added as an out-edge
|
||||
* The edge to be added as an out-edge
|
||||
* @throws IllegalArgumentException
|
||||
* If the edge is null.
|
||||
* If the edge is null.
|
||||
* @throws IllegalStateException
|
||||
* If the source node of the edge is not identical to this node.
|
||||
* If the source node of the edge is not
|
||||
* identical to this node.
|
||||
*/
|
||||
public void addOutEdge(Edge edge) {
|
||||
if(edge == null)
|
||||
if (edge == null)
|
||||
throw new IllegalArgumentException();
|
||||
|
||||
if(edge.source() != this)
|
||||
if (edge.source() != this)
|
||||
throw new IllegalStateException();
|
||||
|
||||
if(edge.source().graph() == null || (edge.target() != null && edge.target().graph() == null))
|
||||
if (edge.source().graph() == null || (edge.target() != null && edge.target().graph() == null))
|
||||
throw new IllegalStateException();
|
||||
|
||||
outEdges.add(edge);
|
||||
}
|
||||
|
||||
void setGraph(ControlFlowGraph graph) {
|
||||
if(this.graph != null) {
|
||||
if(graph == null)
|
||||
if (this.graph != null) {
|
||||
if (graph == null)
|
||||
this.graph = null;
|
||||
}
|
||||
else if(graph == null)
|
||||
} else if (graph == null)
|
||||
throw new IllegalArgumentException();
|
||||
else
|
||||
this.graph = graph;
|
||||
@@ -123,8 +134,8 @@ public class Node {
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if(obj instanceof Node) {
|
||||
Node other = (Node)obj;
|
||||
if (obj instanceof Node) {
|
||||
Node other = (Node) obj;
|
||||
|
||||
return label.equals(other.label);
|
||||
}
|
||||
@@ -142,8 +153,8 @@ public class Node {
|
||||
|
||||
boolean start = true;
|
||||
|
||||
for(Edge edge : inEdges) {
|
||||
if(start)
|
||||
for (Edge edge : inEdges) {
|
||||
if (start)
|
||||
start = false;
|
||||
else
|
||||
sb.append(", ");
|
||||
@@ -155,8 +166,8 @@ public class Node {
|
||||
|
||||
start = true;
|
||||
|
||||
for(Edge edge : outEdges) {
|
||||
if(start)
|
||||
for (Edge edge : outEdges) {
|
||||
if (start)
|
||||
start = false;
|
||||
else
|
||||
sb.append(", ");
|
||||
|
||||
@@ -132,8 +132,8 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
private String getCurrentName() {
|
||||
StringBuilder sb = new StringBuilder();
|
||||
|
||||
for(String name : nameStack) {
|
||||
if(sb.length() == 0)
|
||||
for (String name : nameStack) {
|
||||
if (sb.length() == 0)
|
||||
sb.append(name);
|
||||
else {
|
||||
sb.append('.');
|
||||
@@ -164,7 +164,7 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
int childCount = arg0.getChildCount();
|
||||
WorkingGraph result = null;
|
||||
|
||||
for(int i = 0; i < childCount; i++)
|
||||
for (int i = 0; i < childCount; i++)
|
||||
result = visit(arg0.getChild(i));
|
||||
|
||||
return result;
|
||||
@@ -344,11 +344,13 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
WorkingGraph s = new WorkingGraph();
|
||||
|
||||
s.node = g.buildNode(nodeCounter++ + ": block");
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
s.edges.add(g.buildEdge(s.node, null, EdgeLabel.BLANK));
|
||||
|
||||
List<BlockStatementContext> statements = ctx.blockStatement();
|
||||
|
||||
for(int i = 0, size = statements.size(); i < size; i++)
|
||||
for (int i = 0, size = statements.size(); i < size; i++)
|
||||
s.connect(visitBlockStatement(statements.get(i)));
|
||||
|
||||
return s;
|
||||
@@ -360,11 +362,13 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
WorkingGraph s = new WorkingGraph();
|
||||
|
||||
s.node = g.buildNode(nodeCounter++ + ": block");
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
s.edges.add(g.buildEdge(s.node, null, EdgeLabel.BLANK));
|
||||
|
||||
List<BlockStatementContext> statements = ctx.blockStatement();
|
||||
|
||||
for(int i = 0, size = statements.size(); i < size; i++) {
|
||||
for (int i = 0, size = statements.size(); i < size; i++) {
|
||||
WorkingGraph bs = visitBlockStatement(statements.get(i));
|
||||
|
||||
bs.edges.add(g.buildEdge(bs.node, null, EdgeLabel.THROWN));
|
||||
@@ -380,18 +384,20 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
WorkingGraph s = new WorkingGraph();
|
||||
|
||||
s.node = g.buildNode(nodeCounter++ + ": block");
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
s.edges.add(g.buildEdge(s.node, null, EdgeLabel.BLANK));
|
||||
|
||||
if(delayedThrown)
|
||||
if (delayedThrown)
|
||||
s.edges.add(g.buildEdge(s.node, null, EdgeLabel.THROWN_DELAYED));
|
||||
|
||||
List<BlockStatementContext> statements = ctx.blockStatement();
|
||||
|
||||
for(int i = 0, size = statements.size(); i < size; i++) {
|
||||
for (int i = 0, size = statements.size(); i < size; i++) {
|
||||
WorkingGraph bs = visitBlockStatement(statements.get(i));
|
||||
|
||||
if(delayedThrown)
|
||||
if(i == size - 1)
|
||||
if (delayedThrown)
|
||||
if (i == size - 1)
|
||||
bs.edges.add(g.buildEdge(bs.node, null, EdgeLabel.THROWN));
|
||||
else
|
||||
bs.edges.add(g.buildEdge(bs.node, null, EdgeLabel.THROWN_DELAYED));
|
||||
@@ -410,11 +416,11 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
ControlFlowGraph getGraph(RuleContext ctx) {
|
||||
RuleContext current = ctx;
|
||||
|
||||
while(current != null && !(current instanceof MemberDeclarationContext)
|
||||
&& !(current instanceof ClassBodyDeclarationContext) && !(current instanceof InterfaceBodyContext))
|
||||
while (current != null && !(current instanceof MemberDeclarationContext)
|
||||
&& !(current instanceof ClassBodyDeclarationContext) && !(current instanceof InterfaceBodyContext))
|
||||
current = current.getParent();
|
||||
|
||||
if(current != null)
|
||||
if (current != null)
|
||||
return graphMap.get(current);
|
||||
|
||||
return null;
|
||||
@@ -427,7 +433,8 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
|
||||
s.node = g.buildNode(nodeCounter++ + ": " + ctx.accept(new TreePrinter()));
|
||||
s.edges.add(g.buildEdge(s.node, null, EdgeLabel.BLANK));
|
||||
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
return s;
|
||||
}
|
||||
|
||||
@@ -452,16 +459,18 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
WorkingGraph s = new WorkingGraph();
|
||||
|
||||
s.node = g.buildNode(nodeCounter++ + ": " + ctx.accept(new TreePrinter()));
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
|
||||
IdentifierContext ic = ctx.identifier();
|
||||
String label = "";
|
||||
|
||||
if(ic != null)
|
||||
if (ic != null)
|
||||
label = ic.getText().intern();
|
||||
|
||||
List<Edge> list = s.breakEdges.get(label);
|
||||
|
||||
if(list == null) {
|
||||
if (list == null) {
|
||||
list = new ArrayList<>();
|
||||
s.breakEdges.put(label, list);
|
||||
}
|
||||
@@ -477,16 +486,18 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
WorkingGraph s = new WorkingGraph();
|
||||
|
||||
s.node = g.buildNode(nodeCounter++ + ": " + ctx.accept(new TreePrinter()));
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
|
||||
IdentifierContext ic = ctx.identifier();
|
||||
String label = "";
|
||||
|
||||
if(ic != null)
|
||||
if (ic != null)
|
||||
label = ic.getText();
|
||||
|
||||
List<Edge> list = s.continueEdges.get(label);
|
||||
|
||||
if(list == null) {
|
||||
if (list == null) {
|
||||
list = new ArrayList<>();
|
||||
s.continueEdges.put(label, list);
|
||||
}
|
||||
@@ -502,18 +513,20 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
WorkingGraph s = new WorkingGraph();
|
||||
|
||||
s.node = g.buildNode(nodeCounter++ + ": do");
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
|
||||
WorkingGraph doBody = visitStatement(ctx.statement());
|
||||
|
||||
g.buildEdge(s.node, doBody.node, EdgeLabel.BLANK);
|
||||
|
||||
Node whileExpression = g
|
||||
.buildNode(nodeCounter++ + ": while " + ctx.parenthesizedExpression().accept(new TreePrinter()) + "; ");
|
||||
.buildNode(nodeCounter++ + ": while " + ctx.parenthesizedExpression().accept(new TreePrinter()) + "; ");
|
||||
|
||||
g.buildEdge(whileExpression, s.node, EdgeLabel.TRUE);
|
||||
|
||||
for(Edge edge : doBody.edges) {
|
||||
if(edge.label() == EdgeLabel.THROWN)
|
||||
for (Edge edge : doBody.edges) {
|
||||
if (edge.label() == EdgeLabel.THROWN)
|
||||
s.edges.add(edge);
|
||||
else
|
||||
edge.target = whileExpression;
|
||||
@@ -523,15 +536,15 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
|
||||
List<Edge> nonLabelledBreakEdges = doBody.breakEdges.get("");
|
||||
|
||||
if(nonLabelledBreakEdges != null) {
|
||||
if (nonLabelledBreakEdges != null) {
|
||||
s.edges.addAll(nonLabelledBreakEdges);
|
||||
s.breakEdges.remove("");
|
||||
}
|
||||
|
||||
List<Edge> nonLabelledContinueEdges = doBody.continueEdges.get("");
|
||||
|
||||
if(nonLabelledContinueEdges != null) {
|
||||
for(Edge edge : nonLabelledContinueEdges) {
|
||||
if (nonLabelledContinueEdges != null) {
|
||||
for (Edge edge : nonLabelledContinueEdges) {
|
||||
edge.setTarget(whileExpression);
|
||||
s.edges.add(edge);
|
||||
}
|
||||
@@ -550,6 +563,8 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
WorkingGraph s = new WorkingGraph();
|
||||
|
||||
s.node = g.buildNode(nodeCounter++ + ": if " + ctx.parenthesizedExpression().accept(new TreePrinter()));
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
|
||||
WorkingGraph trueNode = visitStatement(ctx.statement());
|
||||
|
||||
@@ -558,13 +573,12 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
|
||||
ElseClauseContext elseClause = ctx.elseClause();
|
||||
|
||||
if(elseClause != null) {
|
||||
if (elseClause != null) {
|
||||
WorkingGraph elseNode = visitElseClause(elseClause);
|
||||
|
||||
s.edges.add(g.buildEdge(s.node, elseNode.node, EdgeLabel.FALSE));
|
||||
s.connect(elseNode);
|
||||
}
|
||||
else
|
||||
} else
|
||||
s.edges.add(g.buildEdge(s.node, null, EdgeLabel.FALSE));
|
||||
|
||||
return s;
|
||||
@@ -577,18 +591,18 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
WorkingGraph s = visitStatement(sc);
|
||||
List<Edge> breakEdges = s.breakEdges.get(label);
|
||||
|
||||
if(breakEdges != null) {
|
||||
if (breakEdges != null) {
|
||||
s.breakEdges.remove(label);
|
||||
s.edges.addAll(breakEdges);
|
||||
}
|
||||
|
||||
List<Edge> continueEdges = s.continueEdges.get(label);
|
||||
|
||||
if(continueEdges != null) {
|
||||
if (continueEdges != null) {
|
||||
s.continueEdges.remove(label);
|
||||
|
||||
if(sc.forStatement() != null || sc.whileStatement() != null || sc.doStatement() != null)
|
||||
for(Edge continueEdge : continueEdges)
|
||||
if (sc.forStatement() != null || sc.whileStatement() != null || sc.doStatement() != null)
|
||||
for (Edge continueEdge : continueEdges)
|
||||
continueEdge.setTarget(s.node);
|
||||
}
|
||||
|
||||
@@ -606,36 +620,38 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
WorkingGraph s = new WorkingGraph();
|
||||
|
||||
s.node = g.buildNode(nodeCounter++ + ": try");
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
s.edges.add(g.buildEdge(s.node, null, EdgeLabel.BLANK));
|
||||
|
||||
WorkingGraph tryBody = visitTryBlock(tryBlock);
|
||||
|
||||
s.connect(tryBody);
|
||||
|
||||
for(CatchClauseContext catch_ : catchClauses) {
|
||||
for (CatchClauseContext catch_ : catchClauses) {
|
||||
WorkingGraph catchClause = visitCatchClause(catch_);
|
||||
|
||||
for(Edge edge : s.edges) {
|
||||
if(edge.label() == EdgeLabel.THROWN && edge.target() == null)
|
||||
for (Edge edge : s.edges) {
|
||||
if (edge.label() == EdgeLabel.THROWN && edge.target() == null)
|
||||
edge.setTarget(catchClause.node);
|
||||
}
|
||||
|
||||
s.edges.addAll(catchClause.edges);
|
||||
}
|
||||
|
||||
if(finallyClause != null) {
|
||||
if (finallyClause != null) {
|
||||
boolean delayedThrown = false;
|
||||
|
||||
for(Edge edge : s.edges)
|
||||
if(edge.label() == EdgeLabel.THROWN && edge.target() == null) {
|
||||
for (Edge edge : s.edges)
|
||||
if (edge.label() == EdgeLabel.THROWN && edge.target() == null) {
|
||||
delayedThrown = true;
|
||||
break;
|
||||
}
|
||||
|
||||
WorkingGraph finallyBody = visitFinallyClause(finallyClause, delayedThrown);
|
||||
|
||||
for(Edge edge : s.edges)
|
||||
if(edge.target() == null)
|
||||
for (Edge edge : s.edges)
|
||||
if (edge.target() == null)
|
||||
edge.setTarget(finallyBody.node);
|
||||
|
||||
s.edges.addAll(finallyBody.edges);
|
||||
@@ -658,11 +674,13 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
sb.append(forInit == null ? "; " : forInit.accept(new TreePrinter()) + "; ");
|
||||
sb.append(expression == null ? "; " : expression.accept(new TreePrinter()) + "; ");
|
||||
|
||||
if(forUpdate != null)
|
||||
if (forUpdate != null)
|
||||
sb.append(forUpdate.accept(new TreePrinter()));
|
||||
|
||||
sb.append(")");
|
||||
s.node = g.buildNode(sb.toString());
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
|
||||
WorkingGraph forBody = visitStatement(ctx.statement());
|
||||
|
||||
@@ -670,8 +688,8 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
s.edges.add(g.buildEdge(s.node, forBody.node, EdgeLabel.TRUE));
|
||||
s.edges.add(g.buildEdge(s.node, null, EdgeLabel.FALSE));
|
||||
|
||||
for(Edge edge : forBody.edges) {
|
||||
if(edge.target() == null && edge.label() != EdgeLabel.THROWN)
|
||||
for (Edge edge : forBody.edges) {
|
||||
if (edge.target() == null && edge.label() != EdgeLabel.THROWN)
|
||||
edge.setTarget(s.node);
|
||||
}
|
||||
// s.edges.addAll(forBody.edges);
|
||||
@@ -680,15 +698,15 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
|
||||
List<Edge> nonLabelledBreakEdges = forBody.breakEdges.get("");
|
||||
|
||||
if(nonLabelledBreakEdges != null) {
|
||||
if (nonLabelledBreakEdges != null) {
|
||||
s.edges.addAll(nonLabelledBreakEdges);
|
||||
s.breakEdges.remove("");
|
||||
}
|
||||
|
||||
List<Edge> nonLabelledContinueEdges = forBody.continueEdges.get("");
|
||||
|
||||
if(nonLabelledContinueEdges != null) {
|
||||
for(Edge edge : nonLabelledContinueEdges) {
|
||||
if (nonLabelledContinueEdges != null) {
|
||||
for (Edge edge : nonLabelledContinueEdges) {
|
||||
edge.setTarget(s.node);
|
||||
s.edges.add(edge);
|
||||
}
|
||||
@@ -706,6 +724,8 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
ParenthesizedExpressionContext condition = ctx.parenthesizedExpression();
|
||||
|
||||
s.node = g.buildNode(nodeCounter++ + ": while " + condition.accept(new TreePrinter()));
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
|
||||
WorkingGraph whileBody = visitStatement(ctx.statement());
|
||||
|
||||
@@ -713,8 +733,8 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
g.buildEdge(s.node, whileBody.node, EdgeLabel.TRUE);
|
||||
s.edges.add(g.buildEdge(s.node, null, EdgeLabel.FALSE));
|
||||
|
||||
for(Edge edge : whileBody.edges) {
|
||||
if(edge.label() == EdgeLabel.THROWN)
|
||||
for (Edge edge : whileBody.edges) {
|
||||
if (edge.label() == EdgeLabel.THROWN)
|
||||
s.edges.add(edge);
|
||||
else
|
||||
edge.target = s.node;
|
||||
@@ -724,15 +744,15 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
|
||||
List<Edge> nonLabelledBreakEdges = whileBody.breakEdges.get("");
|
||||
|
||||
if(nonLabelledBreakEdges != null) {
|
||||
if (nonLabelledBreakEdges != null) {
|
||||
s.edges.addAll(nonLabelledBreakEdges);
|
||||
s.breakEdges.remove("");
|
||||
}
|
||||
|
||||
List<Edge> nonLabelledContinueEdges = whileBody.continueEdges.get("");
|
||||
|
||||
if(nonLabelledContinueEdges != null) {
|
||||
for(Edge edge : nonLabelledContinueEdges) {
|
||||
if (nonLabelledContinueEdges != null) {
|
||||
for (Edge edge : nonLabelledContinueEdges) {
|
||||
edge.setTarget(s.node);
|
||||
s.edges.add(edge);
|
||||
}
|
||||
@@ -750,22 +770,24 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
List<SwitchBlockStatementGroupContext> statementGroups = ctx.switchBlockStatementGroup();
|
||||
|
||||
s.node = g.buildNode(nodeCounter++ + ": switch " + ctx.parenthesizedExpression().accept(new TreePrinter()));
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
|
||||
if(!statementGroups.isEmpty())
|
||||
for(SwitchBlockStatementGroupContext group : statementGroups) {
|
||||
if (!statementGroups.isEmpty())
|
||||
for (SwitchBlockStatementGroupContext group : statementGroups) {
|
||||
List<SwitchLabelContext> labels = group.switchLabel();
|
||||
List<BlockStatementContext> statements = group.blockStatement();
|
||||
|
||||
WorkingGraph first = visitBlockStatement(statements.get(0));
|
||||
TreePrinter printer = new TreePrinter();
|
||||
|
||||
for(SwitchLabelContext label : labels)
|
||||
for (SwitchLabelContext label : labels)
|
||||
s.edges.add(g.buildEdge(s.node, first.node, EdgeLabel.CASE, label.accept(printer)));
|
||||
|
||||
for(int i = 1, size = statements.size(); i < size; i++) {
|
||||
for (int i = 1, size = statements.size(); i < size; i++) {
|
||||
WorkingGraph later = visitBlockStatement(statements.get(i));
|
||||
|
||||
for(List<Edge> list : later.breakEdges.values())
|
||||
for (List<Edge> list : later.breakEdges.values())
|
||||
first.edges.addAll(list);
|
||||
|
||||
later.breakEdges.clear();
|
||||
@@ -779,7 +801,7 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
|
||||
List<SwitchLabelContext> extraLabels = ctx.switchLabel();
|
||||
|
||||
for(SwitchLabelContext label : extraLabels)
|
||||
for (SwitchLabelContext label : extraLabels)
|
||||
s.edges.add(g.buildEdge(s.node, null, EdgeLabel.CASE, label.accept(new TreePrinter())));
|
||||
|
||||
return s;
|
||||
@@ -791,7 +813,9 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
WorkingGraph s = new WorkingGraph();
|
||||
|
||||
s.node = g
|
||||
.buildNode(nodeCounter++ + ": synchronized " + ctx.parenthesizedExpression().accept(new TreePrinter()));
|
||||
.buildNode(nodeCounter++ + ": synchronized " + ctx.parenthesizedExpression().accept(new TreePrinter()));
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
s.edges.add(g.buildEdge(s.node, null, EdgeLabel.BLANK));
|
||||
|
||||
WorkingGraph block = visitBlock(ctx.block());
|
||||
@@ -807,6 +831,8 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
WorkingGraph s = new WorkingGraph();
|
||||
|
||||
s.node = g.buildNode(nodeCounter++ + ": " + ctx.accept(new TreePrinter()));
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
s.edges.add(g.buildEdge(s.node, null, EdgeLabel.BLANK));
|
||||
|
||||
return s;
|
||||
@@ -818,6 +844,8 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
WorkingGraph s = new WorkingGraph();
|
||||
|
||||
s.node = g.buildNode(nodeCounter++ + ": " + ctx.accept(new TreePrinter()));
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
s.edges.add(g.buildEdge(s.node, null, EdgeLabel.THROWN));
|
||||
|
||||
return s;
|
||||
@@ -829,6 +857,8 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
WorkingGraph s = new WorkingGraph();
|
||||
|
||||
s.node = g.buildNode(nodeCounter++ + ": *EMPTY* ; ");
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
s.edges.add(g.buildEdge(s.node, null, EdgeLabel.BLANK));
|
||||
|
||||
return s;
|
||||
@@ -840,6 +870,8 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
WorkingGraph s = new WorkingGraph();
|
||||
|
||||
s.node = g.buildNode(nodeCounter++ + ": " + ctx.accept(new TreePrinter()));
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
s.edges.add(g.buildEdge(s.node, null, EdgeLabel.BLANK));
|
||||
|
||||
return s;
|
||||
@@ -868,7 +900,9 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
WorkingGraph s = new WorkingGraph();
|
||||
|
||||
s.node = g
|
||||
.buildNode(nodeCounter++ + ": catch ( " + ctx.formalParameter().type().accept(new TreePrinter()) + ")");
|
||||
.buildNode(nodeCounter++ + ": catch ( " + ctx.formalParameter().type().accept(new TreePrinter()) + ")");
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
|
||||
WorkingGraph body = visitBlock(ctx.block());
|
||||
|
||||
@@ -934,10 +968,10 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
public WorkingGraph visitCompilationUnit(CompilationUnitContext ctx) {
|
||||
QualifiedIdentifierContext pkg = ctx.qualifiedIdentifier();
|
||||
|
||||
if(pkg != null)
|
||||
if (pkg != null)
|
||||
nameStack.push(pkg.accept(new TreePrinter()));
|
||||
|
||||
for(TypeDeclarationContext typeDeclaration : ctx.typeDeclaration())
|
||||
for (TypeDeclarationContext typeDeclaration : ctx.typeDeclaration())
|
||||
typeDeclaration.accept(this);
|
||||
|
||||
return null;
|
||||
@@ -1008,10 +1042,10 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
List<FormalParameterContext> formals = ctx.formalParameter();
|
||||
int size = formals.size();
|
||||
|
||||
if(size > 0) {
|
||||
if (size > 0) {
|
||||
sb.append(formals.get(0).type().accept(new TreePrinter()));
|
||||
|
||||
for(int i = 1; i < size; i++) {
|
||||
for (int i = 1; i < size; i++) {
|
||||
sb.append(", ");
|
||||
sb.append(formals.get(i).type().accept(new TreePrinter()));
|
||||
}
|
||||
@@ -1024,7 +1058,7 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
|
||||
@Override
|
||||
public WorkingGraph visitMethodDeclaration(MethodDeclarationContext ctx) {
|
||||
if(ctx.block() != null) {
|
||||
if (ctx.block() != null) {
|
||||
String identifier = ctx.identifier().getText();
|
||||
String signature = getSignature(identifier, ctx.formalParameters());
|
||||
ControlFlowGraph g = new ControlFlowGraph(getCurrentName() + "." + signature);
|
||||
@@ -1035,21 +1069,21 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
|
||||
g.buildEdge(g.entry, s.node, EdgeLabel.BLANK);
|
||||
|
||||
for(Edge edge : s.edges) {
|
||||
for (Edge edge : s.edges) {
|
||||
EdgeLabel label = edge.label();
|
||||
|
||||
if(edge.target() == null) {
|
||||
if(label == EdgeLabel.THROWN)
|
||||
if (edge.target() == null) {
|
||||
if (label == EdgeLabel.THROWN)
|
||||
edge.setTarget(g.abruptExit);
|
||||
else
|
||||
edge.setTarget(g.normalExit);
|
||||
}
|
||||
}
|
||||
|
||||
if(!s.breakEdges.isEmpty())
|
||||
if (!s.breakEdges.isEmpty())
|
||||
throw new IllegalStateException("WorkingGraph still contains break edges: " + s);
|
||||
|
||||
if(!s.continueEdges.isEmpty())
|
||||
if (!s.continueEdges.isEmpty())
|
||||
throw new IllegalStateException("WorkingGraph still contains continue edges: " + s);
|
||||
}
|
||||
|
||||
@@ -1125,21 +1159,21 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
|
||||
g.buildEdge(g.entry, s.node, EdgeLabel.BLANK);
|
||||
|
||||
for(Edge edge : s.edges) {
|
||||
for (Edge edge : s.edges) {
|
||||
EdgeLabel label = edge.label();
|
||||
|
||||
if(edge.target() == null) {
|
||||
if(label == EdgeLabel.THROWN)
|
||||
if (edge.target() == null) {
|
||||
if (label == EdgeLabel.THROWN)
|
||||
edge.setTarget(g.abruptExit);
|
||||
else
|
||||
edge.setTarget(g.normalExit);
|
||||
}
|
||||
}
|
||||
|
||||
if(!s.breakEdges.isEmpty())
|
||||
if (!s.breakEdges.isEmpty())
|
||||
throw new IllegalStateException("WorkingGraph still contains break edges: " + s);
|
||||
|
||||
if(!s.continueEdges.isEmpty())
|
||||
if (!s.continueEdges.isEmpty())
|
||||
throw new IllegalStateException("WorkingGraph still contains continue edges: " + s);
|
||||
|
||||
return null;
|
||||
@@ -1155,21 +1189,21 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
|
||||
g.buildEdge(g.entry, s.node, EdgeLabel.BLANK);
|
||||
|
||||
for(Edge edge : s.edges) {
|
||||
for (Edge edge : s.edges) {
|
||||
EdgeLabel label = edge.label();
|
||||
|
||||
if(edge.target() == null) {
|
||||
if(label == EdgeLabel.THROWN)
|
||||
if (edge.target() == null) {
|
||||
if (label == EdgeLabel.THROWN)
|
||||
edge.setTarget(g.abruptExit);
|
||||
else
|
||||
edge.setTarget(g.normalExit);
|
||||
}
|
||||
}
|
||||
|
||||
if(!s.breakEdges.isEmpty())
|
||||
if (!s.breakEdges.isEmpty())
|
||||
throw new IllegalStateException("WorkingGraph still contains break edges: " + s);
|
||||
|
||||
if(!s.continueEdges.isEmpty())
|
||||
if (!s.continueEdges.isEmpty())
|
||||
throw new IllegalStateException("WorkingGraph still contains continue edges: " + s);
|
||||
|
||||
return null;
|
||||
@@ -1204,21 +1238,21 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
|
||||
g.buildEdge(g.entry, s.node, EdgeLabel.BLANK);
|
||||
|
||||
for(Edge edge : s.edges) {
|
||||
for (Edge edge : s.edges) {
|
||||
EdgeLabel label = edge.label();
|
||||
|
||||
if(edge.target() == null) {
|
||||
if(label == EdgeLabel.THROWN)
|
||||
if (edge.target() == null) {
|
||||
if (label == EdgeLabel.THROWN)
|
||||
edge.setTarget(g.abruptExit);
|
||||
else
|
||||
edge.setTarget(g.normalExit);
|
||||
}
|
||||
}
|
||||
|
||||
if(!s.breakEdges.isEmpty())
|
||||
if (!s.breakEdges.isEmpty())
|
||||
throw new IllegalStateException("WorkingGraph still contains break edges: " + s);
|
||||
|
||||
if(!s.continueEdges.isEmpty())
|
||||
if (!s.continueEdges.isEmpty())
|
||||
throw new IllegalStateException("WorkingGraph still contains continue edges: " + s);
|
||||
|
||||
return null;
|
||||
@@ -1230,17 +1264,19 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
WorkingGraph s = new WorkingGraph();
|
||||
|
||||
s.node = g.buildNode(nodeCounter++ + ": body");
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
s.edges.add(g.buildEdge(s.node, null, EdgeLabel.BLANK));
|
||||
|
||||
WorkingGraph body = null;
|
||||
ExplicitConstructorInvocationContext eci = ctx.explicitConstructorInvocation();
|
||||
|
||||
if(eci != null) {
|
||||
if (eci != null) {
|
||||
body = visitExplicitConstructorInvocation(eci);
|
||||
s.connect(body);
|
||||
}
|
||||
|
||||
for(BlockStatementContext statement : ctx.blockStatement()) {
|
||||
for (BlockStatementContext statement : ctx.blockStatement()) {
|
||||
body = visitBlockStatement(statement);
|
||||
s.connect(body);
|
||||
}
|
||||
@@ -1254,6 +1290,8 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
WorkingGraph s = new WorkingGraph();
|
||||
|
||||
s.node = g.buildNode(nodeCounter++ + ": " + ctx.accept(new TreePrinter()));
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
s.edges.add(g.buildEdge(s.node, null, EdgeLabel.BLANK));
|
||||
|
||||
return s;
|
||||
@@ -1288,6 +1326,8 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
WorkingGraph s = new WorkingGraph();
|
||||
|
||||
s.node = g.buildNode(nodeCounter++ + ": else");
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
|
||||
WorkingGraph body = visitStatement(ctx.statement());
|
||||
|
||||
@@ -1309,12 +1349,14 @@ public class NodeBuilder implements Java1_4ParserVisitor<WorkingGraph> {
|
||||
WorkingGraph s = new WorkingGraph();
|
||||
|
||||
s.node = g.buildNode(nodeCounter++ + ": finally");
|
||||
// addlinenumber
|
||||
s.node.setLineNumber(ctx.getStart().getLine());
|
||||
|
||||
WorkingGraph body = visitFinallyBlock(ctx.block(), delayedThrown);
|
||||
|
||||
s.edges.add(g.buildEdge(s.node, body.node, EdgeLabel.BLANK));
|
||||
|
||||
if(delayedThrown)
|
||||
if (delayedThrown)
|
||||
s.edges.add(g.buildEdge(s.node, body.node, EdgeLabel.THROWN_DELAYED));
|
||||
|
||||
return s;
|
||||
@@ -1328,31 +1370,31 @@ class WorkingGraph {
|
||||
public Map<String, List<Edge>> continueEdges = new HashMap<>();
|
||||
|
||||
public void connect(WorkingGraph s) {
|
||||
if(s == null)
|
||||
if (s == null)
|
||||
return;
|
||||
|
||||
for(Edge edge : edges) {
|
||||
if(edge.label() != EdgeLabel.THROWN && edge.target == null)
|
||||
for (Edge edge : edges) {
|
||||
if (edge.label() != EdgeLabel.THROWN && edge.target == null)
|
||||
edge.setTarget(s.node);
|
||||
}
|
||||
|
||||
edges.addAll(s.edges);
|
||||
|
||||
for(String label : s.breakEdges.keySet()) {
|
||||
for (String label : s.breakEdges.keySet()) {
|
||||
List<Edge> sEdges = s.breakEdges.get(label);
|
||||
List<Edge> thisEdges = breakEdges.get(label);
|
||||
|
||||
if(thisEdges == null)
|
||||
if (thisEdges == null)
|
||||
breakEdges.put(label, sEdges);
|
||||
else
|
||||
thisEdges.addAll(sEdges);
|
||||
}
|
||||
|
||||
for(String label : s.continueEdges.keySet()) {
|
||||
for (String label : s.continueEdges.keySet()) {
|
||||
List<Edge> sEdges = s.continueEdges.get(label);
|
||||
List<Edge> thisEdges = continueEdges.get(label);
|
||||
|
||||
if(thisEdges == null)
|
||||
if (thisEdges == null)
|
||||
continueEdges.put(label, sEdges);
|
||||
else
|
||||
thisEdges.addAll(sEdges);
|
||||
|
||||
Reference in New Issue
Block a user