Search in sources :

Example 1 with DfaValue

use of com.intellij.codeInspection.dataFlow.value.DfaValue in project intellij-community by JetBrains.

the class SwapInstruction method accept.

@Override
public DfaInstructionState[] accept(DataFlowRunner runner, DfaMemoryState stateBefore, InstructionVisitor visitor) {
    final DfaValue a = stateBefore.pop();
    final DfaValue b = stateBefore.pop();
    stateBefore.push(a);
    stateBefore.push(b);
    Instruction nextInstruction = runner.getInstruction(getIndex() + 1);
    return new DfaInstructionState[] { new DfaInstructionState(nextInstruction, stateBefore) };
}
Also used : DfaInstructionState(com.intellij.codeInspection.dataFlow.DfaInstructionState) DfaValue(com.intellij.codeInspection.dataFlow.value.DfaValue)

Example 2 with DfaValue

use of com.intellij.codeInspection.dataFlow.value.DfaValue in project intellij-community by JetBrains.

the class EqClass method toString.

@Override
public String toString() {
    StringBuilder buf = new StringBuilder();
    buf.append("(");
    for (int i = 0; i < size(); i++) {
        if (i > 0)
            buf.append(", ");
        int value = get(i);
        DfaValue dfaValue = myFactory.getValue(value);
        buf.append(dfaValue);
    }
    buf.append(")");
    return buf.toString();
}
Also used : DfaValue(com.intellij.codeInspection.dataFlow.value.DfaValue)

Example 3 with DfaValue

use of com.intellij.codeInspection.dataFlow.value.DfaValue in project intellij-community by JetBrains.

the class InstructionVisitor method visitConditionalGoto.

public DfaInstructionState[] visitConditionalGoto(ConditionalGotoInstruction instruction, DataFlowRunner runner, DfaMemoryState memState) {
    DfaValue cond = memState.pop();
    DfaValue condTrue;
    DfaValue condFalse;
    if (instruction.isNegated()) {
        condFalse = cond;
        condTrue = cond.createNegated();
    } else {
        condTrue = cond;
        condFalse = cond.createNegated();
    }
    if (condTrue == runner.getFactory().getConstFactory().getTrue()) {
        markBranchReachable(instruction, true);
        return new DfaInstructionState[] { new DfaInstructionState(runner.getInstruction(instruction.getOffset()), memState) };
    }
    if (condFalse == runner.getFactory().getConstFactory().getTrue()) {
        markBranchReachable(instruction, false);
        return nextInstruction(instruction, runner, memState);
    }
    ArrayList<DfaInstructionState> result = new ArrayList<>();
    DfaMemoryState thenState = memState.createCopy();
    DfaMemoryState elseState = memState.createCopy();
    if (thenState.applyCondition(condTrue)) {
        result.add(new DfaInstructionState(runner.getInstruction(instruction.getOffset()), thenState));
        markBranchReachable(instruction, true);
    }
    if (elseState.applyCondition(condFalse)) {
        result.add(new DfaInstructionState(runner.getInstruction(instruction.getIndex() + 1), elseState));
        markBranchReachable(instruction, false);
    }
    return result.toArray(new DfaInstructionState[result.size()]);
}
Also used : ArrayList(java.util.ArrayList) DfaValue(com.intellij.codeInspection.dataFlow.value.DfaValue)

Example 4 with DfaValue

use of com.intellij.codeInspection.dataFlow.value.DfaValue in project intellij-community by JetBrains.

the class InstructionVisitor method visitNot.

public DfaInstructionState[] visitNot(NotInstruction instruction, DataFlowRunner runner, DfaMemoryState memState) {
    DfaValue dfaValue = memState.pop();
    dfaValue = dfaValue.createNegated();
    memState.push(dfaValue);
    return nextInstruction(instruction, runner, memState);
}
Also used : DfaValue(com.intellij.codeInspection.dataFlow.value.DfaValue)

Example 5 with DfaValue

use of com.intellij.codeInspection.dataFlow.value.DfaValue in project intellij-community by JetBrains.

the class DfaUtil method inferMethodNullity.

@NotNull
public static Nullness inferMethodNullity(PsiMethod method) {
    final PsiCodeBlock body = method.getBody();
    if (body == null || PsiUtil.resolveClassInType(method.getReturnType()) == null) {
        return Nullness.UNKNOWN;
    }
    final AtomicBoolean hasNulls = new AtomicBoolean();
    final AtomicBoolean hasNotNulls = new AtomicBoolean();
    final AtomicBoolean hasUnknowns = new AtomicBoolean();
    final StandardDataFlowRunner dfaRunner = new StandardDataFlowRunner();
    final RunnerResult rc = dfaRunner.analyzeMethod(body, new StandardInstructionVisitor() {

        @Override
        public DfaInstructionState[] visitCheckReturnValue(CheckReturnValueInstruction instruction, DataFlowRunner runner, DfaMemoryState memState) {
            DfaValue returned = memState.peek();
            if (memState.isNull(returned)) {
                hasNulls.set(true);
            } else if (memState.isNotNull(returned)) {
                hasNotNulls.set(true);
            } else {
                hasUnknowns.set(true);
            }
            return super.visitCheckReturnValue(instruction, runner, memState);
        }
    });
    if (rc == RunnerResult.OK) {
        if (hasNulls.get()) {
            return InferenceFromSourceUtil.suppressNullable(method) ? Nullness.UNKNOWN : Nullness.NULLABLE;
        }
        if (hasNotNulls.get() && !hasUnknowns.get()) {
            return Nullness.NOT_NULL;
        }
    }
    return Nullness.UNKNOWN;
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) DfaValue(com.intellij.codeInspection.dataFlow.value.DfaValue) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

DfaValue (com.intellij.codeInspection.dataFlow.value.DfaValue)7 ArrayList (java.util.ArrayList)2 DfaInstructionState (com.intellij.codeInspection.dataFlow.DfaInstructionState)1 DfaConstValue (com.intellij.codeInspection.dataFlow.value.DfaConstValue)1 TIntProcedure (gnu.trove.TIntProcedure)1 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)1 NotNull (org.jetbrains.annotations.NotNull)1