use of org.sonar.java.cfg.CFG 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.CFG 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.CFG 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);
}
use of org.sonar.java.cfg.CFG in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_enqueuing_exceptional_yields2.
@Test
public void test_enqueuing_exceptional_yields2() {
BytecodeCFG cfg = SETestUtils.bytecodeCFG(TRY_WRONG_CATCH_SIGNATURE, squidClassLoader);
BytecodeCFG.Block b2 = cfg.blocks().get(2);
walker.programState = ProgramState.EMPTY_STATE.stackValue(new SymbolicValue()).stackValue(new SymbolicValue());
walker.programPosition = new ProgramPoint(b2).next().next();
walker.executeInstruction(b2.elements().get(3));
assertThat(walker.workList).hasSize(3);
assertThat(walker.workList.pop().programState.exitValue()).isNotNull().isInstanceOf(SymbolicValue.ExceptionalSymbolicValue.class).extracting(sv -> ((SymbolicValue.ExceptionalSymbolicValue) sv).exceptionType().fullyQualifiedName()).containsExactly("java.lang.IllegalStateException");
assertThat(walker.workList.pop().programState.exitValue()).isNull();
}
use of org.sonar.java.cfg.CFG in project sonar-java by SonarSource.
the class BytecodeEGWalkerExecuteTest method test_tableswitch.
@Test
public void test_tableswitch() throws Exception {
Instructions instr = new Instructions();
instr.visitVarInsn(ILOAD, 0);
Label l0 = new Label();
Label l1 = new Label();
Label l2 = new Label();
Label l3 = new Label();
instr.visitTableSwitchInsn(0, 2, l3, new Label[] { l0, l1, l2 });
instr.visitLabel(l0);
instr.visitInsn(ICONST_0);
instr.visitVarInsn(ISTORE, 1);
instr.visitJumpInsn(GOTO, l3);
instr.visitLabel(l1);
instr.visitInsn(ICONST_0);
instr.visitVarInsn(ISTORE, 2);
instr.visitJumpInsn(GOTO, l3);
instr.visitLabel(l2);
instr.visitInsn(ICONST_0);
instr.visitVarInsn(ISTORE, 3);
instr.visitLabel(l3);
instr.visitInsn(RETURN);
BytecodeCFG cfg = instr.cfg();
CFG.IBlock<Instruction> entry = cfg.entry();
BytecodeEGWalker walker = new BytecodeEGWalker(null, null);
walker.programState = ProgramState.EMPTY_STATE.stackValue(new SymbolicValue());
walker.handleBlockExit(new ProgramPoint(entry));
assertThat(walker.workList).hasSize(entry.successors().size());
walker.workList.forEach(node -> {
assertThat(node.programState.peekValue()).isNull();
assertThat(entry.successors().contains(node.programPoint.block)).isTrue();
});
}
Aggregations