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
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package testSuite;

import liquidjava.specification.Refinement;
import liquidjava.specification.StateRefinement;

public class ErrorRefinementDeclarationPositions {
@Refinement("_ > 10")
private int field = 11;

@StateRefinement(from = "true", to = "true")
@Refinement(value = "_ > 20", msg = "result must exceed twenty")
int result() {
return 0; // Expect: Refinement Error
}

void parameter(@Refinement("_ > 30") int value) {
}

void check() {
parameter(0); // Expect: Refinement Error
}

void local() {
@Refinement("_ > 40")
int local = 41;
local = 0; // Expect: Refinement Error
}

void field() {
field = 0; // Expect: Refinement Error
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import java.lang.annotation.Annotation;

import liquidjava.utils.constants.Keys;
import liquidjava.utils.Utils;
import spoon.reflect.code.CtComment;
import spoon.reflect.cu.SourcePosition;
import spoon.reflect.declaration.CtAnnotation;
Expand Down Expand Up @@ -46,8 +46,7 @@ public static PlacementInCode createPlacement(CtElement elem) {
}
}
String elemText = elemCopy.toString();
SourcePosition annotationPosition = elem.getMetadata(Keys.REFINEMENT_POSITION)instanceof SourcePosition p ? p
: elem.getPosition();
SourcePosition annotationPosition = Utils.getRefinementPosition(elem);
return new PlacementInCode(elemText, elem.getPosition(), annotationPosition);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ public Optional<Predicate> getRefinementFromAnnotation(CtElement element) throws
if (an.contentEquals("liquidjava.specification.Refinement")) {
String value = getStringFromAnnotation(ann.getValue("value"));
ref = Optional.of(value);
element.putMetadata(Keys.REFINEMENT_POSITION, Utils.getLJAnnotationPosition(element, value));

} else if (an.contentEquals("liquidjava.specification.RefinementPredicate")) {
CtExpression<String> rawValue = ann.getValue("value");
Expand Down Expand Up @@ -340,8 +339,7 @@ Optional<CtAnnotation<?>> getExternalRefinement(CtInterface<?> intrface) {
public void checkVariableRefinements(Predicate refinementFound, String simpleName, CtTypeReference<?> type,
CtElement usage, CtElement variable) throws LJError {
Optional<Predicate> expectedType = getRefinementFromAnnotation(variable);
SourcePosition declarationPosition = variable.getMetadata(Keys.REFINEMENT_POSITION)instanceof SourcePosition p
? p : variable.getPosition();
SourcePosition declarationPosition = Utils.getRefinementPosition(variable);
Predicate cEt;
RefinedVariable mainRV = null;
if (context.hasVariable(simpleName)) {
Expand Down
9 changes: 9 additions & 0 deletions liquidjava-verifier/src/main/java/liquidjava/utils/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Stream;
Expand Down Expand Up @@ -63,6 +64,14 @@ public static String getFile(CtElement element) {
return pos.getFile().getAbsolutePath();
}

public static SourcePosition getRefinementPosition(CtElement element) {
return getLiquidJavaAnnotations(element)
.filter(annotation -> annotation.getAnnotationType().getQualifiedName()
.equals("liquidjava.specification.Refinement"))
.map(annotation -> getAnnotationValuePosition(annotation.getValue("value"))).filter(Objects::nonNull)
.findFirst().orElse(null);
}

// Get the position of the annotation with the given value
public static SourcePosition getLJAnnotationPosition(CtElement element, String value) {
String quotedValue = "\"" + value + "\"";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

public final class Keys {
public static final String REFINEMENT = "refinement";
public static final String REFINEMENT_POSITION = "refinement_position";
public static final String REFINEMENT_SAT_CHECK = "refinement_sat_check";
public static final String TARGET = "target";
public static final String RETURN_VAR_NAME = "return_var_name";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package liquidjava.api.tests;

import static org.junit.jupiter.api.Assertions.*;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.List;

import org.junit.jupiter.api.Test;

import liquidjava.api.CommandLineLauncher;
import liquidjava.diagnostics.Diagnostics;
import liquidjava.diagnostics.errors.LJError;
import spoon.reflect.cu.SourcePosition;

class TestDeclarationPositions {
@Test
void smtUnknownPointsToReturnPredicate() throws IOException {
assertDeclarations("ErrorSMTUnknown.java", "_ > 2.0");
}

@Test
void refinementErrorPointsToReturnPredicate() throws IOException {
assertDeclarations("ErrorIdentity.java", "_ > 0");
}

@Test
void smtUnknownPointsToStatePredicate() throws IOException {
assertDeclarations("ErrorSMTUnknownState.java", "amount(this) < limit");
}

@Test
void stateErrorPointsToStatePredicate() throws IOException {
assertDeclarations("ErrorUnconstrainedStateRefinement.java", "ready(this)");
}

@Test
void declarationsCoverReturnsParametersLocalsAndFields() throws IOException {
assertDeclarations("ErrorRefinementDeclarationPositions.java", "_ > 20", "_ > 30", "_ > 40", "_ > 10");
}

private static void assertDeclarations(String file, String... predicates) throws IOException {
Path path = Path.of("../liquidjava-example/src/main/java/testSuite/", file).toRealPath();
String source = Files.readString(path);
CommandLineLauncher.launch(path.toString());
List<LJError> errors = Diagnostics.getInstance().getErrors().stream()
.sorted(Comparator.comparingInt(error -> error.getPosition().getSourceStart())).toList();

assertEquals(predicates.length, errors.size());
for (int i = 0; i < predicates.length; i++) {
LJError error = errors.get(i);
assertPredicatePosition(error.getDeclarationPosition(), path, source, predicates[i]);
}
}

private static void assertPredicatePosition(SourcePosition position, Path file, String source, String predicate)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we should be careful with these string checks cause we might decide change the erro message and then we have to change all these tests. Do you think theres a way to make it less dependendt?

throws IOException {
assertNotNull(position, "Missing refinement declaration position");
assertTrue(position.isValidPosition(), "Invalid refinement declaration position");
int start = source.indexOf("\"" + predicate + "\"") + 1;
assertTrue(start > 0, "Predicate not found in fixture: " + predicate);
assertEquals(start, position.getSourceStart());
assertEquals(start + predicate.length() - 1, position.getSourceEnd());
assertEquals(file, position.getFile().toPath().toRealPath());
}

}
Loading