Search in sources :

Example 1 with Instruction

use of com.intellij.codeInspection.dataFlow.instructions.Instruction in project intellij-community by JetBrains.

the class ControlFlow method toString.

public String toString() {
    StringBuilder result = new StringBuilder();
    final List<Instruction> instructions = myInstructions;
    for (int i = 0; i < instructions.size(); i++) {
        Instruction instruction = instructions.get(i);
        result.append(Integer.toString(i)).append(": ").append(instruction.toString());
        result.append("\n");
    }
    return result.toString();
}
Also used : FlushVariableInstruction(com.intellij.codeInspection.dataFlow.instructions.FlushVariableInstruction) Instruction(com.intellij.codeInspection.dataFlow.instructions.Instruction)

Example 2 with Instruction

use of com.intellij.codeInspection.dataFlow.instructions.Instruction in project intellij-community by JetBrains.

the class ExtractMethodProcessor method isNullInferred.

private boolean isNullInferred(String exprText, boolean trueSet) {
    final PsiCodeBlock block = myElementFactory.createCodeBlockFromText("{}", myElements[0]);
    for (PsiElement element : myElements) {
        block.add(element);
    }
    final PsiIfStatement statementFromText = (PsiIfStatement) myElementFactory.createStatementFromText("if (" + exprText + " == null);", null);
    block.add(statementFromText);
    final StandardDataFlowRunner dfaRunner = new StandardDataFlowRunner();
    final StandardInstructionVisitor visitor = new StandardInstructionVisitor();
    final RunnerResult rc = dfaRunner.analyzeMethod(block, visitor);
    if (rc == RunnerResult.OK) {
        final Pair<Set<Instruction>, Set<Instruction>> expressions = dfaRunner.getConstConditionalExpressions();
        final Set<Instruction> set = trueSet ? expressions.getFirst() : expressions.getSecond();
        for (Instruction instruction : set) {
            if (instruction instanceof BranchingInstruction) {
                if (((BranchingInstruction) instruction).getPsiAnchor().getText().equals(statementFromText.getCondition().getText())) {
                    return true;
                }
            }
        }
    }
    return false;
}
Also used : BranchingInstruction(com.intellij.codeInspection.dataFlow.instructions.BranchingInstruction) BranchingInstruction(com.intellij.codeInspection.dataFlow.instructions.BranchingInstruction) Instruction(com.intellij.codeInspection.dataFlow.instructions.Instruction)

Example 3 with Instruction

use of com.intellij.codeInspection.dataFlow.instructions.Instruction in project intellij-community by JetBrains.

the class LoopAnalyzer method calcInLoop.

static int[] calcInLoop(ControlFlow controlFlow) {
    // loop[i] = loop number(strongly connected component number) of i-th instruction or 0 if outside loop
    final int[] loop = new int[controlFlow.getInstructionCount()];
    MyGraph graph = new MyGraph(controlFlow);
    final DFSTBuilder<Instruction> builder = new DFSTBuilder<>(graph);
    TIntArrayList sccs = builder.getSCCs();
    sccs.forEach(new TIntProcedure() {

        private int myTNumber;

        private int component;

        @Override
        public boolean execute(int size) {
            int value = size > 1 ? ++component : 0;
            for (int i = 0; i < size; i++) {
                Instruction instruction = builder.getNodeByTNumber(myTNumber + i);
                loop[instruction.getIndex()] = value;
            }
            myTNumber += size;
            return true;
        }
    });
    return loop;
}
Also used : TIntProcedure(gnu.trove.TIntProcedure) DFSTBuilder(com.intellij.util.graph.DFSTBuilder) ConditionalGotoInstruction(com.intellij.codeInspection.dataFlow.instructions.ConditionalGotoInstruction) GotoInstruction(com.intellij.codeInspection.dataFlow.instructions.GotoInstruction) Instruction(com.intellij.codeInspection.dataFlow.instructions.Instruction) TIntArrayList(gnu.trove.TIntArrayList)

Example 4 with Instruction

use of com.intellij.codeInspection.dataFlow.instructions.Instruction in project intellij-community by JetBrains.

the class StateQueue method getNextInstructionStates.

@NotNull
List<DfaInstructionState> getNextInstructionStates(Set<Instruction> joinInstructions) {
    DfaInstructionState state = myQueue.poll();
    final Instruction instruction = state.getInstruction();
    mySet.remove(Pair.create(instruction, state.getMemoryState()));
    DfaInstructionState next = myQueue.peek();
    if (next == null || next.compareTo(state) != 0)
        return Collections.singletonList(state);
    List<DfaMemoryStateImpl> memoryStates = ContainerUtil.newArrayList();
    memoryStates.add((DfaMemoryStateImpl) state.getMemoryState());
    while (!myQueue.isEmpty() && myQueue.peek().compareTo(state) == 0) {
        DfaMemoryState anotherState = myQueue.poll().getMemoryState();
        mySet.remove(Pair.create(instruction, anotherState));
        memoryStates.add((DfaMemoryStateImpl) anotherState);
    }
    if (memoryStates.size() > 1 && joinInstructions.contains(instruction)) {
        MultiMap<Object, DfaMemoryStateImpl> groups = MultiMap.create();
        for (DfaMemoryStateImpl memoryState : memoryStates) {
            groups.putValue(memoryState.getSuperficialKey(), memoryState);
        }
        memoryStates = ContainerUtil.newArrayList();
        for (Map.Entry<Object, Collection<DfaMemoryStateImpl>> entry : groups.entrySet()) {
            memoryStates.addAll(mergeGroup((List<DfaMemoryStateImpl>) entry.getValue()));
        }
    }
    return ContainerUtil.map(memoryStates, state1 -> new DfaInstructionState(instruction, state1));
}
Also used : Instruction(com.intellij.codeInspection.dataFlow.instructions.Instruction) MultiMap(com.intellij.util.containers.MultiMap) NotNull(org.jetbrains.annotations.NotNull)

Aggregations

Instruction (com.intellij.codeInspection.dataFlow.instructions.Instruction)4 BranchingInstruction (com.intellij.codeInspection.dataFlow.instructions.BranchingInstruction)1 ConditionalGotoInstruction (com.intellij.codeInspection.dataFlow.instructions.ConditionalGotoInstruction)1 FlushVariableInstruction (com.intellij.codeInspection.dataFlow.instructions.FlushVariableInstruction)1 GotoInstruction (com.intellij.codeInspection.dataFlow.instructions.GotoInstruction)1 MultiMap (com.intellij.util.containers.MultiMap)1 DFSTBuilder (com.intellij.util.graph.DFSTBuilder)1 TIntArrayList (gnu.trove.TIntArrayList)1 TIntProcedure (gnu.trove.TIntProcedure)1 NotNull (org.jetbrains.annotations.NotNull)1