Search in sources :

Example 51 with ProgramState

use of org.sonar.java.se.ProgramState 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 52 with ProgramState

use of org.sonar.java.se.ProgramState 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)

Example 53 with ProgramState

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

the class NonNullSetToNullCheck method isUndefinedOrNull.

private static boolean isUndefinedOrNull(CheckerContext context, Symbol symbol) {
    ProgramState programState = context.getState();
    SymbolicValue value = programState.getValue(symbol);
    return value == null;
}
Also used : ProgramState(org.sonar.java.se.ProgramState) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Example 54 with ProgramState

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

the class ExceptionalYieldChecker method flowsForMethodArguments.

private static Set<Flow> flowsForMethodArguments(ExplodedGraph.Node node, MethodInvocationTree mit, int parameterCausingExceptionIndex) {
    ProgramState programState = node.programState;
    List<ProgramState.SymbolicValueSymbol> arguments = Lists.reverse(programState.peekValuesAndSymbols(mit.arguments().size()));
    SymbolicValue parameterCausingExceptionSV = arguments.get(parameterCausingExceptionIndex).symbolicValue();
    Set<SymbolicValue> argSymbolicValues = new LinkedHashSet<>();
    Set<Symbol> argSymbols = new LinkedHashSet<>();
    arguments.stream().filter(svs -> parameterCausingExceptionSV == svs.symbolicValue() || hasConstraintOtherThanNonNull(svs, programState)).forEach(svs -> {
        argSymbolicValues.add(svs.symbolicValue());
        Symbol symbol = svs.symbol();
        if (symbol != null) {
            argSymbols.add(symbol);
        }
    });
    List<Class<? extends Constraint>> domains = domainsFromArguments(programState, argSymbolicValues);
    return FlowComputation.flow(node, argSymbolicValues, c -> true, c -> false, domains, argSymbols);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) ProgramState(org.sonar.java.se.ProgramState) ConstraintsByDomain(org.sonar.java.se.constraint.ConstraintsByDomain) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) Lists(com.google.common.collect.Lists) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue) LinkedHashSet(java.util.LinkedHashSet) ExceptionalCheckBasedYield(org.sonar.java.se.xproc.ExceptionalCheckBasedYield) ImmutableSet(com.google.common.collect.ImmutableSet) Collection(java.util.Collection) Set(java.util.Set) ExplodedGraph(org.sonar.java.se.ExplodedGraph) Tree(org.sonar.plugins.java.api.tree.Tree) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) Collectors(java.util.stream.Collectors) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) Objects(java.util.Objects) List(java.util.List) Flow(org.sonar.java.se.Flow) FlowComputation(org.sonar.java.se.FlowComputation) Constraint(org.sonar.java.se.constraint.Constraint) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Collections(java.util.Collections) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ObjectConstraint(org.sonar.java.se.constraint.ObjectConstraint) Constraint(org.sonar.java.se.constraint.Constraint) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ProgramState(org.sonar.java.se.ProgramState) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Example 55 with ProgramState

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

the class NullDereferenceCheck method setNullConstraint.

private static List<ProgramState> setNullConstraint(CheckerContext context, Tree syntaxNode) {
    SymbolicValue val = context.getState().peekValue();
    if (syntaxNode.is(Tree.Kind.METHOD_INVOCATION) && isAnnotatedCheckForNull((MethodInvocationTree) syntaxNode)) {
        Preconditions.checkNotNull(val);
        List<ProgramState> states = new ArrayList<>();
        states.addAll(val.setConstraint(context.getState(), ObjectConstraint.NULL));
        states.addAll(val.setConstraint(context.getState(), ObjectConstraint.NOT_NULL));
        return states;
    }
    return Lists.newArrayList(context.getState());
}
Also used : MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) ArrayList(java.util.ArrayList) ProgramState(org.sonar.java.se.ProgramState) SymbolicValue(org.sonar.java.se.symbolicvalues.SymbolicValue)

Aggregations

ProgramState (org.sonar.java.se.ProgramState)105 SymbolicValue (org.sonar.java.se.symbolicvalues.SymbolicValue)72 Test (org.junit.Test)71 Instruction (org.sonar.java.bytecode.cfg.Instruction)61 RelationalSymbolicValue (org.sonar.java.se.symbolicvalues.RelationalSymbolicValue)52 BinarySymbolicValue (org.sonar.java.se.symbolicvalues.BinarySymbolicValue)45 BooleanConstraint (org.sonar.java.se.constraint.BooleanConstraint)33 ObjectConstraint (org.sonar.java.se.constraint.ObjectConstraint)32 ProgramPoint (org.sonar.java.se.ProgramPoint)30 Constraint (org.sonar.java.se.constraint.Constraint)30 TypedConstraint (org.sonar.java.se.constraint.TypedConstraint)25 ConstraintsByDomain (org.sonar.java.se.constraint.ConstraintsByDomain)11 List (java.util.List)9 Collectors (java.util.stream.Collectors)8 Type (org.sonar.plugins.java.api.semantic.Type)8 VisibleForTesting (com.google.common.annotations.VisibleForTesting)7 ExplodedGraph (org.sonar.java.se.ExplodedGraph)7 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)7 Lists (com.google.common.collect.Lists)6 ArrayList (java.util.ArrayList)6