Search in sources :

Example 1 with Warning

use of fr.inria.spoon.dataflow.warning.Warning in project spoon by INRIA.

the class AlwaysTrueFalseChecker method check.

private void check(CtExpression<?> expression, boolean isLoopCondition) {
    Expr conditionExpr = (Expr) expression.getMetadata("value");
    CtTypeReference<?> expressionType = getActualType(expression);
    // Unboxing conversion
    if (!expressionType.isPrimitive() && isCalculable(expressionType)) {
        conditionExpr = getMemory().read(expressionType.unbox(), (IntExpr) conditionExpr);
    }
    ConditionStatus conditionStatus = checkCond((BoolExpr) conditionExpr);
    if (conditionStatus == ConditionStatus.ALWAYS_TRUE && !isLoopCondition) {
        addWarning(new Warning(expression, WarningKind.ALWAYS_TRUE));
    } else if (conditionStatus == ConditionStatus.ALWAYS_FALSE) {
        addWarning(new Warning(expression, WarningKind.ALWAYS_FALSE));
    }
}
Also used : Warning(fr.inria.spoon.dataflow.warning.Warning) IntExpr(com.microsoft.z3.IntExpr) BoolExpr(com.microsoft.z3.BoolExpr) Expr(com.microsoft.z3.Expr) IntExpr(com.microsoft.z3.IntExpr) ConditionStatus(fr.inria.spoon.dataflow.misc.ConditionStatus)

Example 2 with Warning

use of fr.inria.spoon.dataflow.warning.Warning in project spoon by INRIA.

the class TestRunner method test.

public void test(File file) throws IOException {
    Launcher launcher = new Launcher();
    launcher.getEnvironment().setNoClasspath(false);
    launcher.getEnvironment().setCommentEnabled(false);
    launcher.getEnvironment().setComplianceLevel(10);
    launcher.addInputResource(file.getAbsolutePath());
    CtModel model = launcher.buildModel();
    List<String> lines = Files.readAllLines(file.toPath(), Charset.defaultCharset());
    CheckersScanner scanner = new CheckersScanner(launcher.getFactory());
    model.getAllTypes().forEach(scanner::scan);
    List<Warning> warnings = scanner.getWarnings();
    for (Warning warn : warnings) {
        if (!lines.get(warn.position.getLine() - 1).contains("//@" + warn.kind.name())) {
            fail(String.format("ADDITIONAL WARNING '%s' AT (%s:%d)", warn.kind.name(), warn.position.getFile().getAbsolutePath(), warn.position.getLine()));
        }
    }
    for (int i = 0; i < lines.size(); i++) {
        String line = lines.get(i);
        List<String> commentWarnings = getCommentWarnings(line);
        int lineNumber = i + 1;
        for (String commentWarning : commentWarnings) {
            boolean isMissing = warnings.stream().noneMatch(w -> w.position.getLine() == lineNumber && w.kind.name().equals(commentWarning));
            if (isMissing) {
                fail(String.format("MISSING WARNING '%s' AT (%s:%d)", commentWarning, file.getAbsolutePath(), lineNumber));
            }
        }
    }
}
Also used : Warning(fr.inria.spoon.dataflow.warning.Warning) Launcher(spoon.Launcher) CheckersScanner(fr.inria.spoon.dataflow.scanners.CheckersScanner) CtModel(spoon.reflect.CtModel)

Aggregations

Warning (fr.inria.spoon.dataflow.warning.Warning)2 BoolExpr (com.microsoft.z3.BoolExpr)1 Expr (com.microsoft.z3.Expr)1 IntExpr (com.microsoft.z3.IntExpr)1 ConditionStatus (fr.inria.spoon.dataflow.misc.ConditionStatus)1 CheckersScanner (fr.inria.spoon.dataflow.scanners.CheckersScanner)1 Launcher (spoon.Launcher)1 CtModel (spoon.reflect.CtModel)1