Search in sources :

Example 1 with LiveVariables

use of org.sonar.java.cfg.LiveVariables in project sonar-java by SonarSource.

the class ParameterReassignedToCheck method visitForEachStatement.

@Override
public void visitForEachStatement(ForEachStatement tree) {
    CFG cfg = CFG.buildCFG(Collections.singletonList(tree), true);
    Symbol var = tree.variable().symbol();
    boolean liveVar = true;
    if (var.owner().isMethodSymbol()) {
        cfg.setMethodSymbol((Symbol.MethodSymbol) var.owner());
        LiveVariables analyze = LiveVariables.analyze(cfg);
        Set<Symbol> live = analyze.getOut(cfg.reversedBlocks().get(1));
        liveVar = live.contains(var);
    }
    if (!liveVar) {
        variables.add(var);
    }
    super.visitForEachStatement(tree);
    if (!liveVar) {
        variables.remove(var);
    }
}
Also used : CFG(org.sonar.java.cfg.CFG) LiveVariables(org.sonar.java.cfg.LiveVariables) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 2 with LiveVariables

use of org.sonar.java.cfg.LiveVariables in project sonar-java by SonarSource.

the class DeadStoreCheck method visitNode.

@Override
public void visitNode(Tree tree) {
    if (!hasSemantic()) {
        return;
    }
    MethodTree methodTree = (MethodTree) tree;
    if (methodTree.block() == null) {
        return;
    }
    // TODO(npe) Exclude try statements with finally as CFG is incorrect for those and lead to false positive
    if (hasTryFinallyWithLocalVar(methodTree.block(), methodTree.symbol())) {
        return;
    }
    Symbol.MethodSymbol methodSymbol = methodTree.symbol();
    CFG cfg = CFG.build(methodTree);
    LiveVariables liveVariables = LiveVariables.analyze(cfg);
    // Liveness analysis provides information only for block boundaries, so we should do analysis between elements within blocks
    for (CFG.Block block : cfg.blocks()) {
        checkElements(block, liveVariables.getOut(block), methodSymbol);
    }
}
Also used : MethodTree(org.sonar.plugins.java.api.tree.MethodTree) CFG(org.sonar.java.cfg.CFG) LiveVariables(org.sonar.java.cfg.LiveVariables) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 3 with LiveVariables

use of org.sonar.java.cfg.LiveVariables in project sonar-java by SonarSource.

the class ParameterReassignedToCheck method visitMethod.

@Override
public void visitMethod(MethodTree tree) {
    BlockTree block = tree.block();
    if (block == null) {
        return;
    }
    CFG cfg = CFG.build(tree);
    LiveVariables analyze = LiveVariables.analyze(cfg);
    Set<Symbol> live = analyze.getIn(cfg.entry());
    for (VariableTree parameterTree : tree.parameters()) {
        if (!live.contains(parameterTree.symbol())) {
            variables.add(parameterTree.symbol());
        }
    }
    super.visitMethod(tree);
    for (VariableTree parameterTree : tree.parameters()) {
        if (!live.contains(parameterTree.symbol())) {
            variables.remove(parameterTree.symbol());
        }
    }
}
Also used : CFG(org.sonar.java.cfg.CFG) LiveVariables(org.sonar.java.cfg.LiveVariables) Symbol(org.sonar.plugins.java.api.semantic.Symbol) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) BlockTree(org.sonar.plugins.java.api.tree.BlockTree)

Example 4 with LiveVariables

use of org.sonar.java.cfg.LiveVariables in project sonar-java by SonarSource.

the class ParameterReassignedToCheck method visitCatch.

@Override
public void visitCatch(CatchTree tree) {
    CFG cfg = CFG.buildCFG(tree.block().body(), true);
    Symbol var = tree.parameter().symbol();
    boolean liveVar = true;
    if (var.owner().isMethodSymbol()) {
        cfg.setMethodSymbol((Symbol.MethodSymbol) var.owner());
        LiveVariables analyze = LiveVariables.analyze(cfg);
        Set<Symbol> live = analyze.getIn(cfg.entry());
        liveVar = live.contains(var);
    }
    if (!liveVar) {
        variables.add(var);
    }
    super.visitCatch(tree);
    if (!liveVar) {
        variables.remove(var);
    }
}
Also used : CFG(org.sonar.java.cfg.CFG) LiveVariables(org.sonar.java.cfg.LiveVariables) Symbol(org.sonar.plugins.java.api.semantic.Symbol)

Example 5 with LiveVariables

use of org.sonar.java.cfg.LiveVariables in project sonar-java by SonarSource.

the class PrivateFieldUsedLocallyCheck method isLiveInMethodEntry.

private static boolean isLiveInMethodEntry(Symbol privateFieldSymbol, MethodTree methodTree) {
    CFG cfg = CFG.build(methodTree);
    LiveVariables liveVariables = LiveVariables.analyzeWithFields(cfg);
    return liveVariables.getIn(cfg.entry()).contains(privateFieldSymbol);
}
Also used : CFG(org.sonar.java.cfg.CFG) LiveVariables(org.sonar.java.cfg.LiveVariables)

Aggregations

CFG (org.sonar.java.cfg.CFG)5 LiveVariables (org.sonar.java.cfg.LiveVariables)5 Symbol (org.sonar.plugins.java.api.semantic.Symbol)4 BlockTree (org.sonar.plugins.java.api.tree.BlockTree)1 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)1 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)1