Search in sources :

Example 76 with SymbolicValue

use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.

the class ConstraintManager method createBinarySymbolicValue.

public SymbolicValue createBinarySymbolicValue(Tree syntaxNode, List<ProgramState.SymbolicValueSymbol> computedFrom) {
    SymbolicValue result;
    switch(syntaxNode.kind()) {
        case EQUAL_TO:
            result = createRelationalSymbolicValue(Kind.EQUAL, computedFrom);
            break;
        case NOT_EQUAL_TO:
            result = createRelationalSymbolicValue(Kind.NOT_EQUAL, computedFrom);
            break;
        case LESS_THAN:
            result = createRelationalSymbolicValue(Kind.LESS_THAN, computedFrom);
            break;
        case LESS_THAN_OR_EQUAL_TO:
            result = createRelationalSymbolicValue(Kind.GREATER_THAN_OR_EQUAL, Lists.reverse(computedFrom));
            break;
        case GREATER_THAN:
            result = createRelationalSymbolicValue(Kind.LESS_THAN, Lists.reverse(computedFrom));
            break;
        case GREATER_THAN_OR_EQUAL_TO:
            result = createRelationalSymbolicValue(Kind.GREATER_THAN_OR_EQUAL, computedFrom);
            break;
        case AND:
        case AND_ASSIGNMENT:
            result = new SymbolicValue.AndSymbolicValue();
            result.computedFrom(computedFrom);
            break;
        case OR:
        case OR_ASSIGNMENT:
            result = new SymbolicValue.OrSymbolicValue();
            result.computedFrom(computedFrom);
            break;
        case XOR:
        case XOR_ASSIGNMENT:
            result = new SymbolicValue.XorSymbolicValue();
            result.computedFrom(computedFrom);
            break;
        default:
            result = createDefaultSymbolicValue();
            result.computedFrom(computedFrom);
    }
    return result;
}
Also used : RelationalSymbolicValue(org.sonar.java.se.symbolicvalues.RelationalSymbolicValue) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Example 77 with SymbolicValue

use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.

the class StreamNotConsumedCheck method checkEndOfExecutionPath.

@Override
public void checkEndOfExecutionPath(CheckerContext context, ConstraintManager constraintManager) {
    if (context.getState().exitValue() instanceof SymbolicValue.ExceptionalSymbolicValue) {
        // don't report when exiting on exception
        return;
    }
    ProgramState state = context.getState();
    List<SymbolicValue> notConsumed = state.getValuesWithConstraints(NOT_CONSUMED);
    notConsumed.forEach(sv -> {
        Set<Flow> flows = FlowComputation.flow(context.getNode(), Collections.singleton(sv), NOT_CONSUMED::equals, NOT_CONSUMED::equals, Collections.singletonList(StreamConsumedCheck.StreamPipelineConstraint.class), Collections.emptySet());
        Flow flow = flows.iterator().next();
        JavaFileScannerContext.Location location = flow.elements().get(0);
        reportIssue(location.syntaxNode, "Refactor the code so this stream pipeline is used.");
    });
}
Also used : NOT_CONSUMED(org.sonar.java.se.checks.StreamConsumedCheck.StreamPipelineConstraint.NOT_CONSUMED) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) ProgramState(org.sonar.java.se.ProgramState) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) Flow(org.sonar.java.se.Flow)

Example 78 with SymbolicValue

use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.

the class UnclosedResourcesCheck method symbolicValuesToReport.

private static Set<SymbolicValue> symbolicValuesToReport(CheckerContext context) {
    List<SymbolicValue> openSymbolicValues = context.getState().getValuesWithConstraints(OPEN);
    Set<SymbolicValue> svToReport = new HashSet<>(openSymbolicValues);
    // report only outermost OPEN symbolic value
    for (SymbolicValue openSymbolicValue : openSymbolicValues) {
        if (openSymbolicValue instanceof ResourceWrapperSymbolicValue) {
            svToReport.remove(openSymbolicValue.wrappedValue());
        }
    }
    return svToReport;
}
Also used : SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) HashSet(java.util.HashSet)

Example 79 with SymbolicValue

use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.

the class InvariantReturnCheck method checkEndOfExecutionPath.

@Override
public void checkEndOfExecutionPath(CheckerContext context, ConstraintManager constraintManager) {
    if (context.getState().exitingOnRuntimeException()) {
        return;
    }
    MethodInvariantContext methodInvariantContext = methodInvariantContexts.peek();
    if (!methodInvariantContext.methodToCheck) {
        return;
    }
    SymbolicValue exitValue = context.getState().exitValue();
    if (exitValue != null) {
        methodInvariantContext.endPaths++;
        methodInvariantContext.symbolicValues.add(exitValue);
        ConstraintsByDomain constraints = context.getState().getConstraints(exitValue);
        if (constraints != null) {
            constraints.forEach(methodInvariantContext.methodConstraints::put);
        } else {
            // Relational SV or NOT SV : we can't say anything.
            methodInvariantContext.avoidRaisingConstraintIssue = true;
        }
    }
}
Also used : ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Example 80 with SymbolicValue

use of org.sonar.java.se.symbolicvalues.SymbolicValue in project sonar-java by SonarSource.

the class MapComputeIfAbsentOrPresentCheck method checkPostStatement.

@Override
public ProgramState checkPostStatement(CheckerContext context, Tree syntaxNode) {
    if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION)) {
        MethodInvocationTree mit = (MethodInvocationTree) syntaxNode;
        if (MAP_GET.matches(mit)) {
            ProgramState psBeforeInvocation = context.getNode().programState;
            ProgramState psAfterInvocation = context.getState();
            SymbolicValue keySV = psBeforeInvocation.peekValue(0);
            SymbolicValue mapSV = psBeforeInvocation.peekValue(1);
            SymbolicValue valueSV = psAfterInvocation.peekValue();
            mapGetInvocations.put(mapSV, new MapGetInvocation(valueSV, keySV, mit));
        }
    }
    return super.checkPostStatement(context, syntaxNode);
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ProgramState(org.sonar.java.se.ProgramState) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Aggregations

SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)132 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)94 Test (org.junit.Test)79 ProgramState (org.sonar.java.se.ProgramState)74 BinarySymbolicValue (org.sonar.java.se.symbolicvalues.BinarySymbolicValue)55 Instruction (org.sonar.java.bytecode.cfg.Instruction)52 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)38 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)36 ProgramPoint (org.sonar.java.se.ProgramPoint)30 Constraint (org.sonar.java.se.constraint.Constraint)29 TypedConstraint (org.sonar.java.se.constraint.TypedConstraint)22 Type (org.sonar.plugins.java.api.semantic.Type)18 Symbol (org.sonar.plugins.java.api.semantic.Symbol)17 JavaSymbol (org.sonar.java.resolve.JavaSymbol)16 ConstraintsByDomain (org.sonar.java.se.constraint.ConstraintsByDomain)16 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)15 List (java.util.List)13 Collectors (java.util.stream.Collectors)11 VisibleForTesting (com.google.common.annotations.VisibleForTesting)10 Lists (com.google.common.collect.Lists)10