Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions cribro-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ dependencies {
// PIT supplies these classes when it loads the interceptor; the jar has no runtime dependency.
compileOnly(libs.pitest.entry)
testImplementation(libs.pitest.entry)
// The tests split --jvmArgs with PIT's own parser.
testImplementation(libs.pitest.command.line)
}

// The plugins read these at run time, and the Gradle plugin's jar carries this file with the classes.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ public final class ChangedLinesFile {
*/
public static final String PITEST_PROPERTY = "cribro.pitest.changedLines";

/**
* The system property naming the repository the paths of the file are relative to, set on the
* JVM that runs PIT beside {@link #PITEST_PROPERTY}, so that the interceptor reads each changed
* file where the build reads it.
*/
public static final String REPOSITORY_PROPERTY = "cribro.pitest.repository";

/**
* The name of the file, in PIT's report directory, that holds the run's changed lines that hold
* code, as {@link CodeLines#holdsCode} decides, and emit bytecode in a class PIT may mutate.
* The PIT interceptor writes it in this class's format, and the mutation report counts the
* changed lines from it.
*/
public static final String WITH_BYTECODE = "changed-lines-with-bytecode.txt";

private ChangedLinesFile() {}

/**
Expand Down
73 changes: 73 additions & 0 deletions cribro-core/src/main/java/io/github/cribrodev/CodeLines.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
/*
* Copyright (C) 2026 cribro contributors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.github.cribrodev;

import java.util.regex.Pattern;

/**
* Decides whether a line with bytecode holds code of its own, for every report that counts changed
* or exposed lines: the diff coverage block, the exposed-code block, and the mutation summary.
*
* <p>A line whose text {@link #isOnlyBracesSeparatorsAndElse is only braces, separators, and
* else}, that carries no branch, and that starts no method holds no code of its own. The compiler
* gives such a line an instruction of the statement it closes: the jump over an {@code else}
* block, the jump back to a loop's condition, a method's implicit return. Whether that
* instruction ran is reported by the lines and branches of that statement, so counting the brace as
* well would count the statement twice. A method or a lambda with an empty body has its only
* instruction on its closing brace, which is the method's first line, so that line holds code. A
* line that carries a branch holds code whatever its text, so that the branch totals keep every
* decision the compiler emitted.
*
* <p>This class is loaded both by the build and by the PIT process, so it depends on nothing but
* the JDK.
*/
public final class CodeLines {

/** A comment on a source line: a block comment, one left open at the end, or a line comment. */
private static final Pattern COMMENT = Pattern.compile("/\\*.*?\\*/|/\\*.*|//.*");

/** Braces, closing parentheses, semicolons, commas, whitespace, and the keyword else, alone. */
private static final Pattern BRACES_SEPARATORS_AND_ELSE =
Pattern.compile("(?:[\\s{}),;]|\\belse\\b)+");

private CodeLines() {}

/**
* Returns whether a line with bytecode holds code of its own.
*
* @param carriesBranch whether an instruction of the line is a branch the report counts
* @param startsMethod whether the line is the first line of a method or a lambda body
* @param source the text of the line, or an empty string where the working tree cannot show it
*/
public static boolean holdsCode(boolean carriesBranch, boolean startsMethod, String source) {
return carriesBranch || startsMethod || !isOnlyBracesSeparatorsAndElse(source);
}

/**
* Returns whether the source line holds only braces, closing parentheses, semicolons, commas,
* and the keyword {@code else} once its comments are removed, such as <code>}</code>, <code>
* } else {</code>, <code>});</code>, or <code>) {</code>.
*
* <p>A line with anything else on it gives {@code false}, such as <code>} else if (b) {</code>,
* <code>} catch (E e) {</code>, or <code>} finally {</code>. An empty line, a comment alone, and
* a line the working tree cannot show give {@code false} too: their text says nothing about the
* instructions the compiler put on them.
*/
static boolean isOnlyBracesSeparatorsAndElse(String source) {
String code = COMMENT.matcher(source).replaceAll(" ").strip();
return !code.isEmpty() && BRACES_SEPARATORS_AND_ELSE.matcher(code).matches();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import java.util.List;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/** How much of a change a test run executed, file by file. */
final class DiffCoverageReport {
Expand Down Expand Up @@ -94,7 +95,10 @@ String describe() {
static final class FileCoverage {
final String path;

/** Changed lines of this file that emit bytecode, executed or not. */
/**
* Changed lines of this file that emit bytecode and hold code, as {@link CodeLines#holdsCode}
* decides, executed or not.
*/
int executableLines;

/** Changed lines of this file the run executed. */
Expand Down Expand Up @@ -185,7 +189,10 @@ boolean isSpent() {
}
}

/** The total printed where the change reached no line of this module that emits bytecode. */
/**
* The total printed where no changed line of this module both emits bytecode and holds code, as
* {@link CodeLines#holdsCode} decides.
*/
static final String NO_EXECUTABLE_LINES =
"Total: no changed executable lines under this module's source roots.";

Expand All @@ -194,7 +201,10 @@ boolean isSpent() {
/** Changed files under a source root that no report read here mentions. */
final List<String> notInAnyReport = new ArrayList<>();

/** Changed files whose changed lines emit no bytecode, such as comments and declarations. */
/**
* Changed files whose changed lines emit no bytecode, such as comments and declarations, or hold
* no code of their own, such as a closing brace.
*/
final List<String> nothingExecutable = new ArrayList<>();

/**
Expand Down Expand Up @@ -240,6 +250,10 @@ private DiffCoverageReport() {}
* source file and gives no way to tell them apart. Each path is still printed, so that neither
* changed file leaves the report.
*
* <p>A line that holds no code of its own, as {@link CodeLines#holdsCode} decides from its
* branches, from whether JaCoCo reports it as the first line of a method, and from its {@link
* SourceLines#lenient} text, counts towards no total and is not a row.
*
* @param changed the files in the order the lists that name nothing to do print them, which
* {@link ChangedLines#underSourceRoots} orders by path
* @param sources the working tree the uncovered lines are quoted from
Expand All @@ -260,6 +274,12 @@ static DiffCoverageReport measure(
if (counters == null) {
continue;
}
if (!CodeLines.holdsCode(
counters.totalBranches > 0,
lines.startsMethod(key, number),
sources.lenient(path, number))) {
continue;
}
file.executableLines++;
if (counters.isExecuted()) {
file.executedLines++;
Expand Down Expand Up @@ -336,11 +356,36 @@ int executableLines() {
return measured.stream().mapToInt(file -> file.executableLines).sum();
}

/** Returns how many changed lines the run left unexecuted or took a branch of only one way. */
/**
* Returns how many changed lines the run left uncovered, acknowledged or not: the sum of {@link
* #notExecutedLines()} and {@link #untakenBranchLines()}.
*/
int uncoveredLines() {
return notExecutedLines() + untakenBranchLines();
}

/**
* Returns how many changed lines the run did not execute, acknowledged or not, which is the
* executable total less the executed one.
*
* <p>A line nothing ran counts here even where it has branches, since nothing took them either.
*/
int notExecutedLines() {
return (int) uncoveredRows().filter(line -> !line.executed()).count();
}

/**
* Returns how many changed lines the run executed without taking every branch outcome,
* acknowledged or not.
*/
int untakenBranchLines() {
return (int) uncoveredRows().filter(AffectedLine::executed).count();
}

/** Returns every uncovered changed line, whether it is work or acknowledged. */
private Stream<AffectedLine> uncoveredRows() {
return measured.stream()
.mapToInt(file -> file.affected.size() + file.acknowledged.size())
.sum();
.flatMap(file -> Stream.concat(file.affected.stream(), file.acknowledged.stream()));
}

/** Returns how many of the uncovered lines a comment in the source acknowledges. */
Expand Down Expand Up @@ -413,12 +458,24 @@ String render(String scope, boolean filtered, List<String> staleSources, Limits
appendBranches(out, coveredBranches(), totalBranches());
out.append('.');
// Every uncovered line counts here, printed or not: the executed share alone reports a line
// whose condition went one way as covered.
// whose condition went one way as covered. The two groups split the total, so a reader matches
// the not-executed group against the executed share and the other group against the branch
// pair. Acknowledged lines are counted in their group like any other, and the closing clause
// says how many of the total they are.
int uncovered = uncoveredLines();
out.append("\nUncovered changed code: ")
.append(uncoveredLines())
.append(uncoveredLines() == 1 ? " line" : " lines");
.append(uncovered)
.append(uncovered == 1 ? " line" : " lines");
if (uncovered > 0) {
// Both groups are printed even at zero, so that the line keeps one shape between runs.
out.append(" (")
.append(notExecutedLines())
.append(" not executed, ")
.append(untakenBranchLines())
.append(" with an untaken branch)");
}
if (acknowledgedLines() > 0) {
out.append(" (").append(acknowledgedLines()).append(" acknowledged)");
out.append(", ").append(acknowledgedLines()).append(" of them acknowledged");
}
return out.toString();
}
Expand Down
Loading
Loading