Skip to content

Commit

Permalink
Showing 5 changed files with 50 additions and 9 deletions.
8 changes: 4 additions & 4 deletions test/truffle/integration/coverage/test.rb
Original file line number Diff line number Diff line change
@@ -6,15 +6,15 @@
# GNU General Public License version 2
# GNU Lesser General Public License version 2.1

# TODO CS 19-Feb-16 Not compliant with MRI - here as a regression test

require 'coverage'

Coverage.start

require_relative 'subject.rb'

data = Coverage.result[File.join(File.dirname(__FILE__), 'subject.rb')]
expected = [0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 0, 2, 20, 0, 0, 2, 0, 4, 2, 0, 0, 2, 4, 0, 0, 2, 2, 0, 2] # Doubled line counts
result = Coverage.result
key = result.keys.select { |k| k.end_with?('subject.rb') }.first
data = result[key]
expected = [nil, nil, nil, nil, nil, nil, nil, nil, 1, 1, nil, 1, 10, nil, nil, 1, nil, 1, 1, nil, nil, 1, 2, nil, nil, 1, 1, nil, 1]

raise 'coverage data not as expected' unless data == expected
Original file line number Diff line number Diff line change
@@ -55,6 +55,7 @@
import org.jruby.truffle.language.loader.SourceLoader;
import org.jruby.truffle.language.methods.InternalMethod;
import org.jruby.truffle.platform.Graal;
import org.jruby.truffle.stdlib.CoverageManager;
import org.jruby.truffle.tools.SimpleShell;
import org.jruby.util.ByteList;
import org.jruby.util.Memo;
@@ -334,7 +335,11 @@ private Object[] lineCountsStore(long[] array) {
final Object[] store = new Object[array.length];

for (int n = 0; n < array.length; n++) {
store[n] = array[n];
if (array[n] == CoverageManager.NO_CODE) {
store[n] = nil();
} else {
store[n] = array[n];
}
}

return store;
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@

import java.io.IOException;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
Original file line number Diff line number Diff line change
@@ -1770,7 +1770,7 @@ public RubyNode visitIfNode(org.jruby.ast.IfNode node) {
ret = sequence(context, sourceSection, Arrays.asList(condition, new NilLiteralNode(context, sourceSection, true)));
}

return addNewlineIfNeeded(node, ret);
return ret; // no addNewlineIfNeeded(node, ret) as the condition will already have a newline
}

@Override
@@ -3154,6 +3154,9 @@ private void copyNewline(org.jruby.ast.Node from, org.jruby.ast.Node to) {
private RubyNode addNewlineIfNeeded(org.jruby.ast.Node jrubyNode, RubyNode node) {
if (jrubyNode.isNewline()) {
final SourceSection current = node.getEncapsulatingSourceSection();
if (context.getCoverageManager() != null) {
context.getCoverageManager().setLineHasCode(current.getLineLocation());
}
node.clearSourceSection();
node.assignSourceSection(current.withTags(AttachmentsManager.LINE_TAG, TraceManager.LINE_TAG, CoverageManager.LINE_TAG));
}
Original file line number Diff line number Diff line change
@@ -17,11 +17,13 @@
import com.oracle.truffle.api.instrumentation.ExecutionEventNodeFactory;
import com.oracle.truffle.api.instrumentation.Instrumenter;
import com.oracle.truffle.api.instrumentation.SourceSectionFilter;
import com.oracle.truffle.api.source.LineLocation;
import com.oracle.truffle.api.source.Source;
import com.oracle.truffle.api.source.SourceSection;
import org.jruby.truffle.RubyContext;

import java.io.PrintStream;
import java.util.BitSet;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
@@ -31,11 +33,14 @@ public class CoverageManager {

public static final String LINE_TAG = "org.jruby.truffle.coverage.line";

public static final long NO_CODE = -1;

private final Instrumenter instrumenter;

private boolean enabled = false;

private final Map<Source, AtomicLongArray> counters = new ConcurrentHashMap<>();
private final Map<Source, BitSet> codeMap = new HashMap<>();

public CoverageManager(RubyContext context, Instrumenter instrumenter) {
this.instrumenter = instrumenter;
@@ -45,6 +50,17 @@ public CoverageManager(RubyContext context, Instrumenter instrumenter) {
}
}

public synchronized void setLineHasCode(LineLocation line) {
BitSet bitmap = codeMap.get(line.getSource());

if (bitmap == null) {
bitmap = new BitSet(line.getSource().getLineCount());
codeMap.put(line.getSource(), bitmap);
}

bitmap.set(line.getLineNumber() - 1);
}

public void enable() {
if (enabled) {
throw new UnsupportedOperationException();
@@ -94,10 +110,16 @@ public Map<Source, long[]> getCounts() {
final Map<Source, long[]> counts = new HashMap<>();

for (Map.Entry<Source, AtomicLongArray> entry : counters.entrySet()) {
final BitSet hasCode = codeMap.get(entry.getKey());

final long[] array = new long[entry.getValue().length()];

for (int n = 0; n < array.length; n++) {
array[n] = entry.getValue().get(n);
if (hasCode != null && hasCode.get(n)) {
array[n] = entry.getValue().get(n);
} else {
array[n] = NO_CODE;
}
}

counts.put(entry.getKey(), array);
@@ -108,7 +130,9 @@ public Map<Source, long[]> getCounts() {

public void print(PrintStream out) {
for (Map.Entry<Source, AtomicLongArray> entry : counters.entrySet()) {
out.print(entry.getKey().getName());
final BitSet hasCode = codeMap.get(entry.getKey());

out.println(entry.getKey().getName());

for (int n = 0; n < entry.getValue().length(); n++) {
String line = entry.getKey().getCode(n + 1);
@@ -117,7 +141,15 @@ public void print(PrintStream out) {
line = line.substring(0, 60);
}

out.printf(" % 12d %s%n", entry.getValue().get(n), line);
out.print(" ");

if (hasCode != null && hasCode.get(n)) {
out.printf("% 12d", entry.getValue().get(n));
} else {
out.print(" -");
}

out.printf(" %s%n", line);
}
}
}

0 comments on commit fcab39f

Please sign in to comment.