Search in sources :

Example 6 with Instruction

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

the class GroovyConstructorUsagesSearcher method processGroovyConstructorUsages.

private static boolean processGroovyConstructorUsages(GrCodeReferenceElement element, final Processor<GrNewExpression> newExpressionProcessor, final LiteralConstructorSearcher literalProcessor) {
    PsiElement parent = element.getParent();
    if (parent instanceof GrAnonymousClassDefinition) {
        parent = parent.getParent();
    }
    if (parent instanceof GrNewExpression) {
        return newExpressionProcessor.process((GrNewExpression) parent);
    }
    if (parent instanceof GrTypeElement) {
        final GrTypeElement typeElement = (GrTypeElement) parent;
        final PsiElement grandpa = typeElement.getParent();
        if (grandpa instanceof GrVariableDeclaration) {
            final GrVariable[] vars = ((GrVariableDeclaration) grandpa).getVariables();
            if (vars.length == 1) {
                final GrVariable variable = vars[0];
                if (!checkLiteralInstantiation(variable.getInitializerGroovy(), literalProcessor)) {
                    return false;
                }
            }
        } else if (grandpa instanceof GrMethod) {
            final GrMethod method = (GrMethod) grandpa;
            if (typeElement == method.getReturnTypeElementGroovy()) {
                ControlFlowUtils.visitAllExitPoints(method.getBlock(), new ControlFlowUtils.ExitPointVisitor() {

                    @Override
                    public boolean visitExitPoint(Instruction instruction, @Nullable GrExpression returnValue) {
                        if (!checkLiteralInstantiation(returnValue, literalProcessor)) {
                            return false;
                        }
                        return true;
                    }
                });
            }
        } else if (grandpa instanceof GrTypeCastExpression) {
            final GrTypeCastExpression cast = (GrTypeCastExpression) grandpa;
            if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
                return false;
            }
        } else if (grandpa instanceof GrSafeCastExpression) {
            final GrSafeCastExpression cast = (GrSafeCastExpression) grandpa;
            if (cast.getCastTypeElement() == typeElement && !checkLiteralInstantiation(cast.getOperand(), literalProcessor)) {
                return false;
            }
        }
    }
    return true;
}
Also used : GrTypeElement(org.jetbrains.plugins.groovy.lang.psi.api.types.GrTypeElement) GrTypeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrTypeCastExpression) GrAnonymousClassDefinition(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.GrAnonymousClassDefinition) GrMethod(org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod) GrSafeCastExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrSafeCastExpression) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) GrNewExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrNewExpression) GrVariable(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable) GrVariableDeclaration(org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariableDeclaration) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) Nullable(org.jetbrains.annotations.Nullable)

Example 7 with Instruction

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

the class UnassignedVariableAccessInspection method check.

@Override
protected void check(@NotNull GrControlFlowOwner owner, @NotNull ProblemsHolder problemsHolder) {
    Instruction[] flow = owner.getControlFlow();
    ReadWriteVariableInstruction[] reads = ControlFlowBuilderUtil.getReadsWithoutPriorWrites(flow, true);
    for (ReadWriteVariableInstruction read : reads) {
        PsiElement element = read.getElement();
        if (element instanceof GroovyPsiElement && !(element instanceof GrClosableBlock)) {
            String name = read.getVariableName();
            GroovyPsiElement property = ResolveUtil.resolveProperty((GroovyPsiElement) element, name);
            if (property != null && !(property instanceof PsiParameter) && !(property instanceof PsiField) && PsiTreeUtil.isAncestor(owner, property, false) && !(myIgnoreBooleanExpressions && isBooleanCheck(element))) {
                problemsHolder.registerProblem(element, GroovyInspectionBundle.message("unassigned.access.tooltip", name));
            }
        }
    }
}
Also used : GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) PsiParameter(com.intellij.psi.PsiParameter) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) PsiField(com.intellij.psi.PsiField) GrClosableBlock(org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)

Example 8 with Instruction

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

the class ControlFlowUtils method findAccess.

/**
   * searches for next or previous write access to local variable
   * @param local variable to analyze
   * @param place place to start searching
   * @param ahead if true search for next write. if false searches for previous write
   * @return all write instructions leading to (or preceding) the place
   */
public static List<ReadWriteVariableInstruction> findAccess(GrVariable local, final PsiElement place, boolean ahead, boolean writeAccessOnly) {
    LOG.assertTrue(!(local instanceof GrField), local.getClass());
    final GrControlFlowOwner owner = findControlFlowOwner(place);
    assert owner != null;
    final Instruction cur = findInstruction(place, owner.getControlFlow());
    if (cur == null) {
        throw new IllegalArgumentException("place is not in the flow");
    }
    return findAccess(local, ahead, writeAccessOnly, cur);
}
Also used : GrControlFlowOwner(org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) MaybeReturnInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.MaybeReturnInstruction) AfterCallInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.AfterCallInstruction) ThrowingInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ThrowingInstruction) IfEndInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction)

Example 9 with Instruction

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

the class ControlFlowUtils method inferWriteAccessMap.

@NotNull
public static List<BitSet> inferWriteAccessMap(final Instruction[] flow, final GrVariable var) {
    final Semilattice<BitSet> sem = new Semilattice<BitSet>() {

        @NotNull
        @Override
        public BitSet join(@NotNull List<BitSet> ins) {
            BitSet result = new BitSet(flow.length);
            for (BitSet set : ins) {
                result.or(set);
            }
            return result;
        }

        @Override
        public boolean eq(@NotNull BitSet e1, @NotNull BitSet e2) {
            return e1.equals(e2);
        }
    };
    DfaInstance<BitSet> dfa = new DfaInstance<BitSet>() {

        @Override
        public void fun(@NotNull BitSet bitSet, @NotNull Instruction instruction) {
            if (!(instruction instanceof ReadWriteVariableInstruction))
                return;
            if (!((ReadWriteVariableInstruction) instruction).isWrite())
                return;
            final PsiElement element = instruction.getElement();
            if (element instanceof GrVariable && element != var)
                return;
            if (element instanceof GrReferenceExpression) {
                final GrReferenceExpression ref = (GrReferenceExpression) element;
                if (ref.isQualified() || ref.resolve() != var) {
                    return;
                }
            }
            if (!((ReadWriteVariableInstruction) instruction).getVariableName().equals(var.getName())) {
                return;
            }
            bitSet.clear();
            bitSet.set(instruction.num());
        }

        @NotNull
        @Override
        public BitSet initial() {
            return new BitSet(flow.length);
        }
    };
    return new DFAEngine<>(flow, dfa, sem).performForceDFA();
}
Also used : ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) MaybeReturnInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.MaybeReturnInstruction) AfterCallInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.AfterCallInstruction) ThrowingInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ThrowingInstruction) IfEndInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) NotNull(org.jetbrains.annotations.NotNull) GrReferenceExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression) DfaInstance(org.jetbrains.plugins.groovy.lang.psi.dataFlow.DfaInstance) Semilattice(org.jetbrains.plugins.groovy.lang.psi.dataFlow.Semilattice) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) NotNull(org.jetbrains.annotations.NotNull)

Example 10 with Instruction

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

the class ControlFlowUtils method visitAllExitPointsInner.

private static boolean visitAllExitPointsInner(Instruction last, Instruction first, boolean[] visited, ExitPointVisitor visitor) {
    if (first == last)
        return true;
    if (last instanceof AfterCallInstruction) {
        visited[last.num()] = true;
        return visitAllExitPointsInner(((AfterCallInstruction) last).myCall, first, visited, visitor);
    }
    if (last instanceof MaybeReturnInstruction) {
        return visitor.visitExitPoint(last, (GrExpression) last.getElement());
    } else if (last instanceof IfEndInstruction) {
        visited[last.num()] = true;
        for (Instruction instruction : last.allPredecessors()) {
            if (!visitAllExitPointsInner(instruction, first, visited, visitor))
                return false;
        }
        return true;
    } else if (last instanceof ThrowingInstruction) {
        PsiElement element = last.getElement();
        if (!(element instanceof GrThrowStatement || element instanceof GrAssertStatement))
            return true;
    }
    PsiElement element = last.getElement();
    if (element != null) {
        final GrExpression returnValue;
        if (element instanceof GrReturnStatement) {
            returnValue = ((GrReturnStatement) element).getReturnValue();
        } else if (element instanceof GrExpression && PsiUtil.isExpressionStatement(element)) {
            returnValue = (GrExpression) element;
        } else {
            returnValue = null;
        }
        return visitor.visitExitPoint(last, returnValue);
    }
    visited[last.num()] = true;
    for (Instruction pred : last.allPredecessors()) {
        if (!visited[pred.num()]) {
            if (!visitAllExitPointsInner(pred, first, visited, visitor))
                return false;
        }
    }
    return true;
}
Also used : MaybeReturnInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.MaybeReturnInstruction) GrExpression(org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression) AfterCallInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.AfterCallInstruction) Instruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction) MaybeReturnInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.MaybeReturnInstruction) AfterCallInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.AfterCallInstruction) ThrowingInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ThrowingInstruction) IfEndInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction) ReadWriteVariableInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction) ThrowingInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ThrowingInstruction) PsiElement(com.intellij.psi.PsiElement) GroovyPsiElement(org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement) IfEndInstruction(org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction)

Aggregations

Instruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.Instruction)27 ReadWriteVariableInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.ReadWriteVariableInstruction)18 PsiElement (com.intellij.psi.PsiElement)10 Nullable (org.jetbrains.annotations.Nullable)8 GrControlFlowOwner (org.jetbrains.plugins.groovy.lang.psi.GrControlFlowOwner)7 GrExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrExpression)7 IfEndInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.IfEndInstruction)7 NotNull (org.jetbrains.annotations.NotNull)6 GroovyPsiElement (org.jetbrains.plugins.groovy.lang.psi.GroovyPsiElement)6 GrMethod (org.jetbrains.plugins.groovy.lang.psi.api.statements.typedef.members.GrMethod)6 GrVariable (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrVariable)5 GrOpenBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrOpenBlock)5 AfterCallInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.AfterCallInstruction)5 MaybeReturnInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.MaybeReturnInstruction)5 GrClosableBlock (org.jetbrains.plugins.groovy.lang.psi.api.statements.blocks.GrClosableBlock)4 GrReferenceExpression (org.jetbrains.plugins.groovy.lang.psi.api.statements.expressions.GrReferenceExpression)4 ThrowingInstruction (org.jetbrains.plugins.groovy.lang.psi.controlFlow.impl.ThrowingInstruction)4 BitSet (java.util.BitSet)3 ControlFlowUtils (org.jetbrains.plugins.groovy.codeInspection.utils.ControlFlowUtils)3 GrStatement (org.jetbrains.plugins.groovy.lang.psi.api.statements.GrStatement)3