Search in sources :

Example 1 with DFAEngine

use of org.jetbrains.plugins.groovy.lang.psi.dataFlow.DFAEngine in project intellij-community by JetBrains.

the class InvalidWriteAccessSearcher method findInvalidWriteAccess.

@Nullable
public static List<ReadWriteVariableInstruction> findInvalidWriteAccess(@NotNull Instruction[] flow, @NotNull Map<String, GrVariable> variables, @NotNull Set<GrVariable> alreadyInitialized) {
    DFAEngine<MyData> engine = new DFAEngine<>(flow, new MyDFAInstance(), new MySemilattice());
    final List<MyData> dfaResult = engine.performDFAWithTimeout();
    if (dfaResult == null)
        return null;
    List<ReadWriteVariableInstruction> result = ContainerUtil.newArrayList();
    for (int i = 0; i < flow.length; i++) {
        Instruction instruction = flow[i];
        if (instruction instanceof ReadWriteVariableInstruction && ((ReadWriteVariableInstruction) instruction).isWrite()) {
            final MyData initialized = dfaResult.get(i);
            final GrVariable var = variables.get(((ReadWriteVariableInstruction) instruction).getVariableName());
            if (alreadyInitialized.contains(var)) {
                if (initialized.isInitialized(((ReadWriteVariableInstruction) instruction).getVariableName())) {
                    result.add((ReadWriteVariableInstruction) instruction);
                }
            } else {
                if (initialized.isOverInitialized(((ReadWriteVariableInstruction) instruction).getVariableName())) {
                    result.add((ReadWriteVariableInstruction) instruction);
                }
            }
        }
    }
    return result;
}
Also used : DFAEngine(org.jetbrains.plugins.groovy.lang.psi.dataFlow.DFAEngine) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) Nullable(org.jetbrains.annotations.Nullable)

Example 2 with DFAEngine

use of org.jetbrains.plugins.groovy.lang.psi.dataFlow.DFAEngine in project intellij-community by JetBrains.

the class UnusedDefInspection method check.

@Override
protected void check(@NotNull final GrControlFlowOwner owner, @NotNull final ProblemsHolder problemsHolder) {
    final Instruction[] flow = owner.getControlFlow();
    final ReachingDefinitionsDfaInstance dfaInstance = new ReachingDefinitionsDfaInstance(flow);
    final ReachingDefinitionsSemilattice lattice = new ReachingDefinitionsSemilattice();
    final DFAEngine<DefinitionMap> engine = new DFAEngine<>(flow, dfaInstance, lattice);
    final List<DefinitionMap> dfaResult = engine.performDFAWithTimeout();
    if (dfaResult == null) {
        return;
    }
    final TIntHashSet unusedDefs = new TIntHashSet();
    for (Instruction instruction : flow) {
        if (instruction instanceof ReadWriteVariableInstruction && ((ReadWriteVariableInstruction) instruction).isWrite()) {
            unusedDefs.add(instruction.num());
        }
    }
    for (int i = 0; i < dfaResult.size(); i++) {
        final Instruction instruction = flow[i];
        if (instruction instanceof ReadWriteVariableInstruction) {
            final ReadWriteVariableInstruction varInst = (ReadWriteVariableInstruction) instruction;
            if (!varInst.isWrite()) {
                final String varName = varInst.getVariableName();
                DefinitionMap e = dfaResult.get(i);
                e.forEachValue(new TObjectProcedure<TIntHashSet>() {

                    @Override
                    public boolean execute(TIntHashSet reaching) {
                        reaching.forEach(new TIntProcedure() {

                            @Override
                            public boolean execute(int defNum) {
                                final String defName = ((ReadWriteVariableInstruction) flow[defNum]).getVariableName();
                                if (varName.equals(defName)) {
                                    unusedDefs.remove(defNum);
                                }
                                return true;
                            }
                        });
                        return true;
                    }
                });
            }
        }
    }
    final Set<PsiElement> checked = ContainerUtil.newHashSet();
    unusedDefs.forEach(new TIntProcedure() {

        @Override
        public boolean execute(int num) {
            final ReadWriteVariableInstruction instruction = (ReadWriteVariableInstruction) flow[num];
            final PsiElement element = instruction.getElement();
            process(element, checked, problemsHolder, GroovyInspectionBundle.message("unused.assignment.tooltip"));
            return true;
        }
    });
    owner.accept(new GroovyRecursiveElementVisitor() {

        @Override
        public void visitVariable(@NotNull GrVariable variable) {
            if (checked.contains(variable) || variable.getInitializerGroovy() != null)
                return;
            if (ReferencesSearch.search(variable, variable.getUseScope()).findFirst() == null) {
                process(variable, checked, problemsHolder, GroovyInspectionBundle.message("unused.variable"));
            }
        }
    });
}
Also used : ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) TIntProcedure(gnu.trove.TIntProcedure) GroovyRecursiveElementVisitor(org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) ReachingDefinitionsDfaInstance(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.ReachingDefinitionsDfaInstance) TIntHashSet(gnu.trove.TIntHashSet) DFAEngine(org.jetbrains.plugins.groovy.lang.psi.dataFlow.DFAEngine) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) ReachingDefinitionsSemilattice(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.ReachingDefinitionsSemilattice) DefinitionMap(org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.DefinitionMap) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 3 with DFAEngine

use of org.jetbrains.plugins.groovy.lang.psi.dataFlow.DFAEngine in project intellij-community by JetBrains.

the class ControlFlowBuilderUtil method getReadsWithoutPriorWrites.

public static ReadWriteVariableInstruction[] getReadsWithoutPriorWrites(Instruction[] flow, boolean onlyFirstRead) {
    DFAEngine<ReadBeforeWriteState> engine = new DFAEngine<>(flow, new ReadBeforeWriteInstance(buildNamesIndex(flow), onlyFirstRead), ReadBeforeWriteSemilattice.INSTANCE);
    List<ReadBeforeWriteState> dfaResult = engine.performDFAWithTimeout();
    if (dfaResult == null) {
        return ReadWriteVariableInstruction.EMPTY_ARRAY;
    }
    List<ReadWriteVariableInstruction> result = new ArrayList<>();
    BitSet reads = dfaResult.get(dfaResult.size() - 1).getReads();
    for (int i = reads.nextSetBit(0); i >= 0; i = reads.nextSetBit(i + 1)) {
        if (i == Integer.MAX_VALUE)
            break;
        result.add((ReadWriteVariableInstruction) flow[i]);
    }
    return result.toArray(ReadWriteVariableInstruction.EMPTY_ARRAY);
}
Also used : DFAEngine(org.jetbrains.plugins.groovy.lang.psi.dataFlow.DFAEngine) ReadBeforeWriteInstance(org.jetbrains.plugins.groovy.lang.psi.dataFlow.readWrite.ReadBeforeWriteInstance) ReadBeforeWriteState(org.jetbrains.plugins.groovy.lang.psi.dataFlow.readWrite.ReadBeforeWriteState) ArrayList(java.util.ArrayList) BitSet(java.util.BitSet)

Aggregations

DFAEngine (org.jetbrains.plugins.groovy.lang.psi.dataFlow.DFAEngine)3 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)2 Instruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction)2 ReadWriteVariableInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction)2 PsiElement (com.intellij.psi.PsiElement)1 TIntHashSet (gnu.trove.TIntHashSet)1 TIntProcedure (gnu.trove.TIntProcedure)1 ArrayList (java.util.ArrayList)1 BitSet (java.util.BitSet)1 Nullable (org.jetbrains.annotations.Nullable)1 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)1 GroovyRecursiveElementVisitor (org.jetbrains.plugins.groovy.lang.psi.GroovyRecursiveElementVisitor)1 DefinitionMap (org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.DefinitionMap)1 ReachingDefinitionsDfaInstance (org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.ReachingDefinitionsDfaInstance)1 ReachingDefinitionsSemilattice (org.jetbrains.plugins.groovy.lang.psi.dataFlow.reachingDefs.ReachingDefinitionsSemilattice)1 ReadBeforeWriteInstance (org.jetbrains.plugins.groovy.lang.psi.dataFlow.readWrite.ReadBeforeWriteInstance)1 ReadBeforeWriteState (org.jetbrains.plugins.groovy.lang.psi.dataFlow.readWrite.ReadBeforeWriteState)1