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);
}
}
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);
}
}
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());
}
}
}
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);
}
}
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);
}
Aggregations