Skip to content

Commit 6cefdfe

Browse files
c-mitacopybara-github
authored andcommitted
Update Bazel's branch coverage analyzer for Jacoco changes.
We have to specify the Jacoco version to be used in this change because the default is still 0.8.11 (which is incompatible with the updates). The default can only be changed after this has been submitted. Progresses #27615 PiperOrigin-RevId: 839714713 Change-Id: I6ece745aadbf228ab957960e54204053c073cc7d
1 parent ede8e21 commit 6cefdfe

File tree

6 files changed

+82
-39
lines changed

6 files changed

+82
-39
lines changed

src/java_tools/buildjar/java/com/google/devtools/build/buildjar/BUILD

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ java_library(
106106
"//third_party:error_prone_annotations",
107107
"//third_party:guava",
108108
"//third_party:jsr305",
109-
"//third_party/java/jacoco:core",
109+
"//third_party/java/jacoco:core-0.8.14",
110110
],
111111
)
112112

src/java_tools/junitrunner/java/com/google/testing/coverage/BUILD

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ java_binary(
2424
":JacocoCoverageLib",
2525
":bitfield",
2626
"//third_party:guava",
27-
"//third_party/java/jacoco:blaze-agent",
28-
"//third_party/java/jacoco:core",
29-
"//third_party/java/jacoco:report",
27+
"//third_party/java/jacoco:blaze-agent-0.8.14",
28+
"//third_party/java/jacoco:core-0.8.14",
29+
"//third_party/java/jacoco:report-0.8.14",
3030
],
3131
)
3232

@@ -46,9 +46,9 @@ java_library(
4646
deps = [
4747
":bitfield",
4848
"//third_party:guava",
49-
"//third_party/java/jacoco:blaze-agent",
50-
"//third_party/java/jacoco:core",
51-
"//third_party/java/jacoco:report",
49+
"//third_party/java/jacoco:blaze-agent-0.8.14",
50+
"//third_party/java/jacoco:core-0.8.14",
51+
"//third_party/java/jacoco:report-0.8.14",
5252
],
5353
)
5454

src/java_tools/junitrunner/java/com/google/testing/coverage/BranchExp.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ public void setBranchAtIndex(int index, CovExp exp) {
6868
hasValue = false;
6969
}
7070

71+
/** Returns the expression at a given index for this branch. */
72+
public CovExp getBranchAtIndex(int index) {
73+
return branches.get(index);
74+
}
75+
7176
/** Expands the current branch set to the new size */
7277
private void extendBranches(int size) {
7378
if (branches.size() < size) {

src/java_tools/junitrunner/java/com/google/testing/coverage/MethodProbesMapper.java

Lines changed: 59 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,20 @@
1414

1515
package com.google.testing.coverage;
1616

17-
import static java.util.Comparator.comparing;
1817

1918
import java.util.ArrayList;
19+
import java.util.Collection;
2020
import java.util.HashMap;
2121
import java.util.HashSet;
22+
import java.util.Iterator;
2223
import java.util.List;
2324
import java.util.Map;
24-
import java.util.Set;
2525
import java.util.TreeMap;
2626
import org.jacoco.core.internal.analysis.filter.IFilter;
2727
import org.jacoco.core.internal.analysis.filter.IFilterContext;
2828
import org.jacoco.core.internal.analysis.filter.IFilterOutput;
29+
import org.jacoco.core.internal.analysis.filter.Replacements;
30+
import org.jacoco.core.internal.analysis.filter.Replacements.InstructionBranch;
2931
import org.jacoco.core.internal.flow.IFrame;
3032
import org.jacoco.core.internal.flow.LabelInfo;
3133
import org.jacoco.core.internal.flow.MethodProbesVisitor;
@@ -68,15 +70,13 @@ public class MethodProbesMapper extends MethodProbesVisitor implements IFilterOu
6870
private List<Label> currentLabels = new ArrayList<>();
6971
private AbstractInsnNode currentInstructionNode = null;
7072
private final Map<AbstractInsnNode, Instruction> instructionMap = new HashMap<>();
71-
private int instructionNodeIndex = 0;
72-
private final Map<AbstractInsnNode, Integer> instructionNodeIndexMap = new HashMap<>();
7373

7474
// Filtering
7575
private final IFilter filter;
7676
private final IFilterContext filterContext;
7777
private final HashSet<AbstractInsnNode> ignored = new HashSet<>();
7878
private final Map<AbstractInsnNode, AbstractInsnNode> unioned = new HashMap<>();
79-
private final Map<AbstractInsnNode, Set<AbstractInsnNode>> branchReplacements = new HashMap<>();
79+
private final Map<AbstractInsnNode, Replacements> branchReplacements = new HashMap<>();
8080

8181
// Result
8282
private Map<Integer, BranchExp> lineToBranchExp = new TreeMap<>();
@@ -89,7 +89,7 @@ public Map<Integer, BranchExp> result() {
8989
//
9090
// These values are built up during the visitor methods. They will be used to compute
9191
// the final results.
92-
private final List<Instruction> instructions = new ArrayList<>();
92+
private final InstructionSet instructions = new InstructionSet();
9393
private final List<Jump> jumps = new ArrayList<>();
9494
private final List<Instruction> probedInstructions = new ArrayList<>();
9595
private final Map<Label, Instruction> labelToInsn = new HashMap<>();
@@ -106,7 +106,9 @@ public void accept(MethodNode methodNode, MethodVisitor methodVisitor) {
106106
currentInstructionNode = i;
107107
i.accept(methodVisitor);
108108
}
109-
filter.filter(methodNode, filterContext, this);
109+
if (filter != null) {
110+
filter.filter(methodNode, filterContext, this);
111+
}
110112
methodVisitor.visitEnd();
111113
}
112114

@@ -124,8 +126,6 @@ private void visitInsn() {
124126
currentLabels.clear(); // Update states
125127
lastInstruction = instruction;
126128
instructionMap.put(currentInstructionNode, instruction);
127-
instructionNodeIndexMap.put(currentInstructionNode, instructionNodeIndex);
128-
instructionNodeIndex++;
129129
}
130130

131131
// Plain visitors: called from adapter when no probe is needed
@@ -325,17 +325,26 @@ public void visitEnd() {
325325
}
326326

327327
// Handle branch replacements
328-
for (Map.Entry<AbstractInsnNode, Set<AbstractInsnNode>> entry : branchReplacements.entrySet()) {
329-
// The replacement set is not ordered deterministically and we require it to be so to be able
330-
// to merge multiple coverage reports later on. We use the order in which we encountered
331-
// nodes to determine the order of branches for the new BranchExp.
332-
ArrayList<AbstractInsnNode> replacements = new ArrayList<>(entry.getValue());
333-
replacements.sort(comparing(instructionNodeIndexMap::get));
334-
BranchExp newBranch = new BranchExp(new ArrayList<>());
335-
for (AbstractInsnNode replacement : replacements) {
336-
newBranch.add(instructionMap.get(replacement).branchExp);
328+
for (Map.Entry<AbstractInsnNode, Replacements> entry : branchReplacements.entrySet()) {
329+
BranchExp newBranchExp = BranchExp.initializeEmptyBranches();
330+
int branchIndex = 0;
331+
for (Collection<InstructionBranch> replacements : entry.getValue().values()) {
332+
BranchExp subExp = BranchExp.initializeEmptyBranches();
333+
int subBranchIndex = 0;
334+
for (InstructionBranch replacement : replacements) {
335+
BranchExp branchExp = instructionMap.get(replacement.instruction).branchExp;
336+
subExp.setBranchAtIndex(subBranchIndex, branchExp.getBranchAtIndex(replacement.branch));
337+
subBranchIndex++;
338+
}
339+
newBranchExp.setBranchAtIndex(branchIndex, subExp);
340+
branchIndex++;
337341
}
338-
instructionMap.get(entry.getKey()).branchExp = newBranch;
342+
Instruction oldInsn = instructionMap.get(entry.getKey());
343+
Instruction newInsn = new Instruction(oldInsn.line);
344+
newInsn.logicalBranches = branchIndex;
345+
newInsn.branchExp = newBranchExp;
346+
instructionMap.put(entry.getKey(), newInsn);
347+
instructions.replace(oldInsn, newInsn);
339348
}
340349

341350
HashSet<Instruction> ignoredInstructions = new HashSet<>();
@@ -390,8 +399,8 @@ public void merge(AbstractInsnNode i1, AbstractInsnNode i2) {
390399
}
391400

392401
@Override
393-
public void replaceBranches(AbstractInsnNode source, Set<AbstractInsnNode> newTargets) {
394-
branchReplacements.put(source, newTargets);
402+
public void replaceBranches(AbstractInsnNode source, Replacements replacements) {
403+
branchReplacements.put(source, replacements);
395404
}
396405

397406
private AbstractInsnNode findRepresentative(AbstractInsnNode node) {
@@ -471,4 +480,33 @@ static void wireBranchPredecessors(Instruction root) {
471480
}
472481
}
473482
}
483+
484+
/**
485+
* Permit efficient replacement of one instruction with another while preserving original
486+
* insertion order. A replacement instruction takes the place of the old instruction for iteration
487+
* order.
488+
*/
489+
private static class InstructionSet implements Iterable<Instruction> {
490+
491+
private final List<Instruction> instructions = new ArrayList<>();
492+
493+
private final Map<Instruction, Integer> instructionIndex = new HashMap<>();
494+
495+
void add(Instruction instruction) {
496+
instructionIndex.put(instruction, instructions.size());
497+
instructions.add(instruction);
498+
}
499+
500+
void replace(Instruction oldInstruction, Instruction newInstruction) {
501+
int index = instructionIndex.get(oldInstruction);
502+
instructions.set(index, newInstruction);
503+
instructionIndex.put(newInstruction, index);
504+
instructionIndex.remove(oldInstruction);
505+
}
506+
507+
@Override
508+
public Iterator<Instruction> iterator() {
509+
return instructions.iterator();
510+
}
511+
}
474512
}

src/java_tools/junitrunner/javatests/com/google/testing/coverage/BUILD

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ java_test(
2323
"//third_party:junit4",
2424
"//third_party:mockito",
2525
"//third_party:truth",
26-
"//third_party/java/jacoco:core",
27-
"//third_party/java/jacoco:report",
26+
"//third_party/java/jacoco:core-0.8.14",
27+
"//third_party/java/jacoco:report-0.8.14",
2828
],
2929
)
3030

tools/jdk/BUILD.java_tools

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,14 @@ filegroup(
6666

6767
java_import(
6868
name = "jacoco-agent",
69-
jars = ["java_tools/third_party/java/jacoco/org.jacoco.agent-0.8.11.jar"],
70-
srcjar = "java_tools/third_party/java/jacoco/org.jacoco.agent-0.8.11-sources.jar",
69+
jars = ["java_tools/third_party/java/jacoco/org.jacoco.agent-0.8.14.jar"],
70+
srcjar = "java_tools/third_party/java/jacoco/org.jacoco.agent-0.8.14-sources.jar",
7171
)
7272

7373
java_import(
7474
name = "jacoco-core",
75-
jars = ["java_tools/third_party/java/jacoco/org.jacoco.core-0.8.11.jar"],
76-
srcjar = "java_tools/third_party/java/jacoco/org.jacoco.core-0.8.11-sources.jar",
75+
jars = ["java_tools/third_party/java/jacoco/org.jacoco.core-0.8.14.jar"],
76+
srcjar = "java_tools/third_party/java/jacoco/org.jacoco.core-0.8.14-sources.jar",
7777
exports = [
7878
":asm",
7979
":asm-commons",
@@ -83,13 +83,13 @@ java_import(
8383

8484
filegroup(
8585
name = "jacoco-core-jars",
86-
srcs = ["java_tools/third_party/java/jacoco/org.jacoco.core-0.8.11.jar"],
86+
srcs = ["java_tools/third_party/java/jacoco/org.jacoco.core-0.8.14.jar"],
8787
)
8888

8989
java_import(
9090
name = "jacoco-report",
91-
jars = ["java_tools/third_party/java/jacoco/org.jacoco.report-0.8.11.jar"],
92-
srcjar = "java_tools/third_party/java/jacoco/org.jacoco.report-0.8.11-sources.jar",
91+
jars = ["java_tools/third_party/java/jacoco/org.jacoco.report-0.8.14.jar"],
92+
srcjar = "java_tools/third_party/java/jacoco/org.jacoco.report-0.8.14-sources.jar",
9393
exports = [
9494
":asm",
9595
":jacoco-core",
@@ -98,12 +98,12 @@ java_import(
9898

9999
java_import(
100100
name = "bazel-jacoco-agent",
101-
jars = ["java_tools/third_party/java/jacoco/jacocoagent-0.8.11.jar"],
101+
jars = ["java_tools/third_party/java/jacoco/jacocoagent-0.8.14.jar"],
102102
)
103103

104104
java_import(
105105
name = "bazel-jacoco-agent-neverlink",
106-
jars = ["java_tools/third_party/java/jacoco/jacocoagent-0.8.11.jar"],
106+
jars = ["java_tools/third_party/java/jacoco/jacocoagent-0.8.14.jar"],
107107
neverlink = 1,
108108
)
109109

0 commit comments

Comments
 (0)