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: 1 addition & 1 deletion cribro-core/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ plugins {
id("cribro.publish")
}

description = "Git diff parsing, JaCoCo report reading, and the PIT interceptor shared by the cribro Gradle and Maven plugins"
description = "Git diff parsing, JaCoCo report reading, and the PIT interceptor shared by the cribro Gradle and Maven plugins; not an API, as its classes change with each plugin release"

dependencies {
// PIT supplies these classes when it loads the interceptor; the jar has no runtime dependency.
Expand Down
211 changes: 211 additions & 0 deletions cribro-core/src/main/java/io/github/cribrodev/EquivalentMarkers.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,211 @@
/*
* 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.ArrayList;
import java.util.Arrays;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* The {@code mutant: equivalent} markers of one source line, such as {@code // mutant: equivalent
* RemoveConditionalMutator_EQUAL_ELSE#2 -- annotate only receives class and array types}.
*
* <p>A marker names one or more mutants of the line it sits on, by the ids the mutation report
* prints, separated by commas, and gives the reason after {@code --}. A marker starts at a comment
* opener, a {@code //} or a {@code /} followed by one or more {@code *}, where that opener starts a
* comment or lies inside a line comment, even where the line comment only quotes the syntax. Any
* other text, such as a string or anything inside a block comment, starts no marker. The reason
* runs to the end of the comment the marker is in or of the line, whichever comes first, except
* that a {@code /*} marker inside a line comment ends at the first {@code *}{@code /} before that.
* An id may name the mutator with its package, as PIT does, and the package is dropped.
*
* <p>Each line is read with the whole file around it, as {@link SourceFileKey#regions} splits it,
* so a line inside a text block, a Kotlin raw string, or a block comment opened on an earlier line
* is read as part of it.
*/
final class EquivalentMarkers {

/**
* The start of a marker: a comment opener, then {@code mutant: equivalent}. The run of {@code *}
* after a {@code /} is matched possessively, so a long run takes linear time.
*/
private static final Pattern START =
Pattern.compile("(//|/\\*++)\\s*mutant:\\s*equivalent(?![\\w-])");

/** The {@code --} that ends the ids and starts the reason, with whitespace on both sides. */
private static final Pattern REASON = Pattern.compile("\\s--\\s");

/** One id, as the report prints it: any run of characters but whitespace and commas. */
private static final Pattern ID = Pattern.compile("[^\\s,]+");

/** How a marker is written, for a reader whose marker does not parse. */
static final String FORM = "// mutant: equivalent <id> -- <reason>";

private final Map<String, String> reasons;
private final List<String> problems;
private final String code;
private final String text;

private EquivalentMarkers(
Map<String, String> reasons, List<String> problems, String code, String text) {
this.reasons = reasons;
this.problems = problems;
this.code = code;
this.text = text;
}

/**
* Reads the markers of each line of a file, which may hold none.
*
* @param lines every line of the file, from the first
* @param kotlin whether the file is Kotlin, whose strings hold {@code ${...}} templates
* @return the markers of each line, in the order of the lines
*/
static List<EquivalentMarkers> parse(List<String> lines, boolean kotlin) {
SourceFileKey.Region[] regions = SourceFileKey.regions(String.join("\n", lines), kotlin);
List<EquivalentMarkers> parsed = new ArrayList<>(lines.size());
int start = 0;
for (String line : lines) {
parsed.add(parse(line, Arrays.copyOfRange(regions, start, start + line.length())));
start += line.length() + 1;
}
return parsed;
}

/**
* Reads the markers of a file that has one line.
*
* @param kotlin whether the line is Kotlin, whose strings hold {@code ${...}} templates
*/
static EquivalentMarkers parse(String line, boolean kotlin) {
return parse(List.of(line), kotlin).get(0);
}

/**
* Reads the markers of one line.
*
* @param regions the region each character of the line lies in
*/
private static EquivalentMarkers parse(String line, SourceFileKey.Region[] regions) {
Map<String, String> reasons = new LinkedHashMap<>();
List<String> problems = new ArrayList<>();
StringBuilder code = new StringBuilder();
Matcher start = START.matcher(line);
int copied = 0;
int from = 0;
while (from < line.length() && start.find(from)) {
if (!opensMarker(regions[start.start()])) {
from = start.start() + 1;
continue;
}
int bodyEnd = start.end();
int markerEnd;
if (regions[start.start()] == SourceFileKey.Region.LINE_COMMENT) {
while (bodyEnd < line.length() && regions[bodyEnd] == SourceFileKey.Region.LINE_COMMENT) {
bodyEnd++;
}
markerEnd = bodyEnd;
// A /* inside a line comment opens no comment, so its marker ends at the first */.
int close = start.group(1).equals("//") ? -1 : line.indexOf("*/", start.end());
if (close >= 0 && close + 2 <= bodyEnd) {
bodyEnd = close;
markerEnd = close + 2;
}
} else {
while (bodyEnd < line.length() && regions[bodyEnd] == SourceFileKey.Region.BLOCK_COMMENT) {
bodyEnd++;
}
markerEnd = bodyEnd;
while (markerEnd < line.length()
&& regions[markerEnd] == SourceFileKey.Region.BLOCK_COMMENT_END) {
markerEnd++;
}
}
code.append(line, copied, start.start());
copied = markerEnd;
from = markerEnd;
if (!readBody(line.substring(start.end(), bodyEnd), reasons)) {
problems.add("the marker does not parse; write it as " + FORM);
}
}
code.append(line.substring(Math.min(copied, line.length())));
return new EquivalentMarkers(reasons, problems, code.toString().strip(), line.strip());
}

/** Returns whether a marker may start at a comment opener that lies in the given region. */
private static boolean opensMarker(SourceFileKey.Region region) {
return region == SourceFileKey.Region.LINE_COMMENT
|| region == SourceFileKey.Region.BLOCK_COMMENT_START;
}

/**
* Adds the ids the body of one marker names, each with its reason, and returns whether the body
* holds at least one id, the {@code --}, and a reason.
*/
private static boolean readBody(String body, Map<String, String> reasons) {
Matcher separator = REASON.matcher(body);
if (!separator.find()) {
return false;
}
String reason = body.substring(separator.end()).strip();
String[] ids = body.substring(0, separator.start()).split(",", -1);
if (reason.isEmpty()) {
return false;
}
List<String> named = new ArrayList<>();
for (String id : ids) {
String trimmed = id.strip();
if (!ID.matcher(trimmed).matches()) {
return false;
}
int hash = trimmed.indexOf('#');
int packageEnd = trimmed.lastIndexOf('.', hash < 0 ? trimmed.length() : hash);
String name = trimmed.substring(packageEnd + 1);
if (name.isEmpty() || name.startsWith("#")) {
return false;
}
named.add(name);
}
for (String id : named) {
reasons.putIfAbsent(id, reason);
}
return true;
}

/** Returns the reason given for each id the markers name, in the order they name them. */
Map<String, String> reasons() {
return reasons;
}

/** Returns a sentence for each marker that does not parse. */
List<String> problems() {
return problems;
}

/** Returns the line without its markers, stripped, as the lists of mutants quote it. */
String code() {
return code;
}

/** Returns the line with its markers, stripped, as the list of markers to correct quotes it. */
String text() {
return text;
}
}
58 changes: 50 additions & 8 deletions cribro-core/src/main/java/io/github/cribrodev/MutantsFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,10 @@
* Writes and reads the mutants of one PIT run as a text file, one mutant per line.
*
* <p>A line holds tab-separated fields: the status PIT gave the mutant, the mutated class, the
* method, the source file the class records, the line number, the description, and then the tests
* PIT recorded as covering the mutant, one per field. A tab or a line break inside a field is
* written as a space.
* method, its descriptor, the source file the class records, the line number, the mutator, the
* index of the first instruction the mutant changes, the description, and then the tests PIT
* recorded as covering the mutant, one per field. A tab or a line break inside a field is written
* as a space.
*
* <p>The PIT listener in {@code io.github.cribrodev.pitest} writes the file inside the PIT
* process and the build reads it, so this class depends on nothing but the JDK.
Expand All @@ -52,24 +53,33 @@ public static final class Mutant {
private final String status;
private final String mutatedClass;
private final String method;
private final String methodDescriptor;
private final String sourceFile;
private final int lineNumber;
private final String mutator;
private final int firstIndex;
private final String description;
private final List<String> tests;

public Mutant(
String status,
String mutatedClass,
String method,
String methodDescriptor,
String sourceFile,
int lineNumber,
String mutator,
int firstIndex,
String description,
List<String> tests) {
this.status = status;
this.mutatedClass = mutatedClass;
this.method = method;
this.methodDescriptor = methodDescriptor;
this.sourceFile = sourceFile;
this.lineNumber = lineNumber;
this.mutator = mutator;
this.firstIndex = firstIndex;
this.description = description;
this.tests = tests;
}
Expand All @@ -88,6 +98,11 @@ public String method() {
return method;
}

/** Returns the descriptor of the method, such as {@code (I)Z}. */
public String methodDescriptor() {
return methodDescriptor;
}

public String sourceFile() {
return sourceFile;
}
Expand All @@ -96,6 +111,23 @@ public int lineNumber() {
return lineNumber;
}

/**
* Returns the mutator as PIT names it, such as {@code
* org.pitest.mutationtest.engine.gregor.mutators.RemoveConditionalMutator_EQUAL_ELSE}.
*/
public String mutator() {
return mutator;
}

/**
* Returns the index of the first instruction the mutant changes, counted from the start of its
* method; of two mutants in one method, the one with the lower index comes first in its
* bytecode.
*/
public int firstIndex() {
return firstIndex;
}

public String description() {
return description;
}
Expand All @@ -106,6 +138,9 @@ public List<String> tests() {
}
}

/** The fields every line holds before the tests. */
private static final int FIXED_FIELDS = 9;

private MutantsFile() {}

/** Returns the line the file holds for one mutant, without the line break. */
Expand All @@ -114,8 +149,11 @@ public static String line(Mutant mutant) {
fields.add(mutant.status());
fields.add(mutant.mutatedClass());
fields.add(mutant.method());
fields.add(mutant.methodDescriptor());
fields.add(mutant.sourceFile());
fields.add(Integer.toString(mutant.lineNumber()));
fields.add(mutant.mutator());
fields.add(Integer.toString(mutant.firstIndex()));
fields.add(mutant.description());
fields.addAll(mutant.tests());
List<String> cleaned = new ArrayList<>();
Expand All @@ -133,18 +171,22 @@ public static List<Mutant> read(Path file) throws IOException {
continue;
}
String[] fields = line.split("\t", -1);
if (fields.length < 6) {
throw new IOException("Fewer than 6 fields in " + file + " line: " + line);
if (fields.length < FIXED_FIELDS) {
throw new IOException(
"Fewer than " + FIXED_FIELDS + " fields in " + file + " line: " + line);
}
mutants.add(
new Mutant(
fields[0],
fields[1],
fields[2],
fields[3],
Integer.parseInt(fields[4]),
fields[5],
List.copyOf(Arrays.asList(fields).subList(6, fields.length))));
fields[4],
Integer.parseInt(fields[5]),
fields[6],
Integer.parseInt(fields[7]),
fields[8],
List.copyOf(Arrays.asList(fields).subList(FIXED_FIELDS, fields.length))));
}
return mutants;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,7 @@ public static String report(
repository,
sourceDirs,
ChangedLinesFile.readPaths(changedLines.toPath()),
ChangedLinesFile.read(changedLines.toPath()),
withBytecode.isFile() ? ChangedLinesFile.read(withBytecode.toPath()) : null);
return report.writeAndRender(
repository,
Expand Down
Loading
Loading