Skip to content

Commit

Permalink
Branch coverage instrumentation work-in-progress, handles for loops, …
Browse files Browse the repository at this point in the history
…while loops and do-while loops.

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=135128136
  • Loading branch information
zhihan authored and blickly committed Oct 5, 2016
1 parent 98ffc24 commit 0e9730c
Show file tree
Hide file tree
Showing 10 changed files with 194 additions and 12 deletions.
Expand Up @@ -63,7 +63,10 @@ public void visit(NodeTraversal traversal, Node node, Node parent) {
for (DiGraph.DiGraphEdge<Node, ControlFlowGraph.Branch> outEdge : cfg.getOutEdges(node)) { for (DiGraph.DiGraphEdge<Node, ControlFlowGraph.Branch> outEdge : cfg.getOutEdges(node)) {
if (outEdge.getValue() == ControlFlowGraph.Branch.ON_FALSE) { if (outEdge.getValue() == ControlFlowGraph.Branch.ON_FALSE) {
Node destination = outEdge.getDestination().getValue(); Node destination = outEdge.getDestination().getValue();
if (destination.isBlock() && destination.getParent().isIf()) { if (destination != null
&& destination.isBlock()
&& destination.getParent() != null
&& destination.getParent().isIf()) {
hasDefaultBlock = true; hasDefaultBlock = true;
} }
break; break;
Expand All @@ -76,8 +79,38 @@ public void visit(NodeTraversal traversal, Node node, Node parent) {
instrumentationData.put( instrumentationData.put(
fileName, new FileInstrumentationData(fileName, createArrayName(traversal))); fileName, new FileInstrumentationData(fileName, createArrayName(traversal)));
} }
processBranchInfo(node, instrumentationData.get(fileName)); processBranchInfo(node, instrumentationData.get(fileName), getChildrenBlocks(node));
} else if (node.isFor() || node.isWhile() || node.isDo()) {
List<Node> blocks = getChildrenBlocks(node);
ControlFlowGraph<Node> cfg = traversal.getControlFlowGraph();
for (DiGraph.DiGraphEdge<Node, ControlFlowGraph.Branch> outEdge : cfg.getOutEdges(node)) {
if (outEdge.getValue() == ControlFlowGraph.Branch.ON_FALSE) {
Node destination = outEdge.getDestination().getValue();
if (destination.isBlock()) {
blocks.add(destination);
} else {
Node exitBlock = IR.block();
destination.getParent().addChildBefore(exitBlock, destination);
blocks.add(exitBlock);
}
}
}
if (!instrumentationData.containsKey(fileName)) {
instrumentationData.put(
fileName, new FileInstrumentationData(fileName, createArrayName(traversal)));
}
processBranchInfo(node, instrumentationData.get(fileName), blocks);
}
}

private List<Node> getChildrenBlocks(Node node) {
List<Node> blocks = new ArrayList<>();
for (Node child : node.children()) {
if (child.isBlock()) {
blocks.add(child);
}
} }
return blocks;
} }


/** /**
Expand All @@ -103,7 +136,7 @@ private void instrumentBranchCoverage(NodeTraversal traversal, FileInstrumentati
/** /**
* Create an assignment to the branch coverage data for the given index into the array. * Create an assignment to the branch coverage data for the given index into the array.
* *
* @return the newly constructed assingment node. * @return the newly constructed assignment node.
*/ */
private Node newBranchInstrumentationNode(NodeTraversal traversal, Node node, int idx) { private Node newBranchInstrumentationNode(NodeTraversal traversal, Node node, int idx) {
String arrayName = createArrayName(traversal); String arrayName = createArrayName(traversal);
Expand All @@ -120,18 +153,16 @@ private Node newBranchInstrumentationNode(NodeTraversal traversal, Node node, in
return exprNode.useSourceInfoIfMissingFromForTree(node); return exprNode.useSourceInfoIfMissingFromForTree(node);
} }


/** Add branch instrumentation information for each children block. */ /** Add branch instrumentation information for each block. */
private void processBranchInfo(Node node, FileInstrumentationData data) { private void processBranchInfo(Node branchNode, FileInstrumentationData data, List<Node> blocks) {
int lineNumber = node.getLineno(); int lineNumber = branchNode.getLineno();
data.setBranchPresent(lineNumber); data.setBranchPresent(lineNumber);


// Instrument for each childern // Instrument for each block
int numBranches = 0; int numBranches = 0;
for (Node child : node.children()) { for (Node child : blocks) {
if (child.isBlock()) { data.putBranchNode(lineNumber, numBranches + 1, child);
data.putBranchNode(lineNumber, numBranches + 1, child); numBranches++;
numBranches++;
}
} }
data.addBranches(lineNumber, numBranches); data.addBranches(lineNumber, numBranches);
} }
Expand Down
Expand Up @@ -94,4 +94,50 @@ public void testIfElseBranch() throws Exception {
compareIfElseBranch(LanguageMode.ECMASCRIPT6); compareIfElseBranch(LanguageMode.ECMASCRIPT6);
} }


private void compareForLoopBranch(LanguageMode mode) throws Exception {
GoldenFileComparer.compileAndCompare(
"CoverageInstrumentationPassTest/ForLoopBranchGolden.jsdata",
branchOptions(mode),
"CoverageInstrumentationPassTest/ForLoopBranch.jsdata");
}

public void testForLoopBranch() throws Exception {
compareForLoopBranch(LanguageMode.ECMASCRIPT5);
compareForLoopBranch(LanguageMode.ECMASCRIPT6);
}

private void compareDoWhileLoopBranch(LanguageMode mode) throws Exception {
GoldenFileComparer.compileAndCompare(
"CoverageInstrumentationPassTest/DoWhileLoopBranchGolden.jsdata",
branchOptions(mode),
"CoverageInstrumentationPassTest/DoWhileLoopBranch.jsdata");
}

public void testDoWhileLoopBranch() throws Exception {
compareDoWhileLoopBranch(LanguageMode.ECMASCRIPT6);
}

private void compareDoWhileLoopMultiLineBranch(LanguageMode mode) throws Exception {
GoldenFileComparer.compileAndCompare(
"CoverageInstrumentationPassTest/DoWhileLoopBranchGolden.jsdata",
branchOptions(mode),
"CoverageInstrumentationPassTest/DoWhileLoopBranch.jsdata");
}

public void testDoWhileLoopMultiLineBranch() throws Exception {
compareDoWhileLoopBranch(LanguageMode.ECMASCRIPT6);
}

private void compareWhileLoopBranch(LanguageMode mode) throws Exception {
GoldenFileComparer.compileAndCompare(
"CoverageInstrumentationPassTest/WhileLoopBranchGolden.jsdata",
branchOptions(mode),
"CoverageInstrumentationPassTest/WhileLoopBranch.jsdata");
}

public void testWhileLoopBranch() throws Exception {
compareWhileLoopBranch(LanguageMode.ECMASCRIPT5);
compareWhileLoopBranch(LanguageMode.ECMASCRIPT6);
}

} }
@@ -0,0 +1,7 @@
function AlwaysTrue(x){
var i = 0;
do {
return true;
} while (i < x);
return false;
}
@@ -0,0 +1,18 @@
var __jscov = window.top.__jscov || (window.top.__jscov = {fileNames:[], branchPresent:[], branchesInLine:[], branchesTaken:[]});
var JSCompiler_lcov_branch_data_CoverageInstrumentationPassTest_DoWhileLoopBranch_jsdata = [];
__jscov.branchesTaken.push(JSCompiler_lcov_branch_data_CoverageInstrumentationPassTest_DoWhileLoopBranch_jsdata);
__jscov.branchPresent.push("04");
var JSCompiler_lcov_branchesInLine = [];
JSCompiler_lcov_branchesInLine[0] = 2;
__jscov.branchesInLine.push(JSCompiler_lcov_branchesInLine);
__jscov.fileNames.push("CoverageInstrumentationPassTest/DoWhileLoopBranch.jsdata");
function AlwaysTrue(x) {
var i = 0;
do {
JSCompiler_lcov_branch_data_CoverageInstrumentationPassTest_DoWhileLoopBranch_jsdata[0] = true;
return true;
} while (i < x);
JSCompiler_lcov_branch_data_CoverageInstrumentationPassTest_DoWhileLoopBranch_jsdata[1] = true;
return false;
}
;
@@ -0,0 +1,10 @@
function AlwaysTrue(x){
var i = 0;
do {
i++;
i++;
i++;
return true;
} while (i < x);
return false;
}
@@ -0,0 +1,21 @@
var __jscov = window.top.__jscov || (window.top.__jscov = {fileNames:[], branchPresent:[], branchesInLine:[], branchesTaken:[]});
var JSCompiler_lcov_branch_data_CoverageInstrumentationPassTest_DoWhileLoopBranch_jsdata = [];
__jscov.branchesTaken.push(JSCompiler_lcov_branch_data_CoverageInstrumentationPassTest_DoWhileLoopBranch_jsdata);
__jscov.branchPresent.push("04");
var JSCompiler_lcov_branchesInLine = [];
JSCompiler_lcov_branchesInLine[0] = 2;
__jscov.branchesInLine.push(JSCompiler_lcov_branchesInLine);
__jscov.fileNames.push("CoverageInstrumentationPassTest/DoWhileLoopBranch.jsdata");
function AlwaysTrue(x) {
var i = 0;
do {
JSCompiler_lcov_branch_data_CoverageInstrumentationPassTest_DoWhileLoopBranch_jsdata[0] = true;
i++;
i++;
i++;
return true;
} while (i < x);
JSCompiler_lcov_branch_data_CoverageInstrumentationPassTest_DoWhileLoopBranch_jsdata[1] = true;
return false;
}
;
@@ -0,0 +1,8 @@
function IsPostive(x){
for (var i = 0; i < x; i++) {
return true;
}
{
return false;
}
}
@@ -0,0 +1,17 @@
var __jscov = window.top.__jscov || (window.top.__jscov = {fileNames:[], branchPresent:[], branchesInLine:[], branchesTaken:[]});
var JSCompiler_lcov_branch_data_CoverageInstrumentationPassTest_ForLoopBranch_jsdata = [];
__jscov.branchesTaken.push(JSCompiler_lcov_branch_data_CoverageInstrumentationPassTest_ForLoopBranch_jsdata);
__jscov.branchPresent.push("02");
var JSCompiler_lcov_branchesInLine = [];
JSCompiler_lcov_branchesInLine[0] = 2;
__jscov.branchesInLine.push(JSCompiler_lcov_branchesInLine);
__jscov.fileNames.push("CoverageInstrumentationPassTest/ForLoopBranch.jsdata");
function IsPostive(x) {
for (var i = 0;i < x;i++) {
JSCompiler_lcov_branch_data_CoverageInstrumentationPassTest_ForLoopBranch_jsdata[0] = true;
return true;
}
JSCompiler_lcov_branch_data_CoverageInstrumentationPassTest_ForLoopBranch_jsdata[1] = true;
return false;
}
;
@@ -0,0 +1,7 @@
function IsPostive(x){
var i = 0;
while(i < x) {
return true;
}
return false;
}
@@ -0,0 +1,17 @@
var __jscov = window.top.__jscov || (window.top.__jscov = {fileNames:[], branchPresent:[], branchesInLine:[], branchesTaken:[]});
var JSCompiler_lcov_branch_data_CoverageInstrumentationPassTest_WhileLoopBranch_jsdata = [];
__jscov.branchesTaken.push(JSCompiler_lcov_branch_data_CoverageInstrumentationPassTest_WhileLoopBranch_jsdata);
__jscov.branchPresent.push("04");
var JSCompiler_lcov_branchesInLine = [];
JSCompiler_lcov_branchesInLine[0] = 2;
__jscov.branchesInLine.push(JSCompiler_lcov_branchesInLine);
__jscov.fileNames.push("CoverageInstrumentationPassTest/WhileLoopBranch.jsdata");
function IsPostive(x) {
for (var i = 0;i < x;) {
JSCompiler_lcov_branch_data_CoverageInstrumentationPassTest_WhileLoopBranch_jsdata[0] = true;
return true;
}
JSCompiler_lcov_branch_data_CoverageInstrumentationPassTest_WhileLoopBranch_jsdata[1] = true;
return false;
}
;

0 comments on commit 0e9730c

Please sign in to comment.