use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class BytecodeEGWalkerTest method unchecked_exceptions_should_be_enqueued.
@Test
public void unchecked_exceptions_should_be_enqueued() {
MethodBehavior mb = getMethodBehavior(ExceptionEnqueue.class, "test(Lorg/sonar/java/bytecode/se/testdata/ExceptionEnqueue;)Ljava/lang/Object;");
List<Constraint> resultConstraint = mb.happyPathYields().map(y -> y.resultConstraint().get(ObjectConstraint.class)).collect(Collectors.toList());
assertThat(resultConstraint).contains(ObjectConstraint.NOT_NULL, ObjectConstraint.NULL);
List<String> exceptions = mb.exceptionalPathYields().map(y -> y.exceptionType(semanticModel).fullyQualifiedName()).collect(Collectors.toList());
assertThat(exceptions).contains("org.sonar.java.bytecode.se.testdata.ExceptionEnqueue$ExceptionCatch", "org.sonar.java.bytecode.se.testdata.ExceptionEnqueue$ThrowableCatch", "org.sonar.java.bytecode.se.testdata.ExceptionEnqueue$ErrorCatch");
}
use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class ProgramState method addConstraintTransitively.
public ProgramState addConstraintTransitively(SymbolicValue symbolicValue, Constraint constraint) {
List<SymbolicValue> transitiveSymbolicValues = knownRelations().stream().filter(rsv -> rsv.isEquality() && (rsv.getLeftOp() == symbolicValue || rsv.getRightOp() == symbolicValue)).map(rsv -> rsv.getLeftOp() == symbolicValue ? rsv.getRightOp() : rsv.getLeftOp()).collect(Collectors.toList());
ProgramState ps = addConstraint(symbolicValue, constraint);
for (SymbolicValue sv : transitiveSymbolicValues) {
ps = ps.addConstraint(sv, constraint);
}
return ps;
}
use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class ExceptionalYieldChecker method flowsForMethodArguments.
private static Set<Flow> flowsForMethodArguments(ExplodedGraph.Node node, MethodInvocationTree mit, int parameterCausingExceptionIndex) {
ProgramState programState = node.programState;
List<ProgramState.SymbolicValueSymbol> arguments = Lists.reverse(programState.peekValuesAndSymbols(mit.arguments().size()));
SymbolicValue parameterCausingExceptionSV = arguments.get(parameterCausingExceptionIndex).symbolicValue();
Set<SymbolicValue> argSymbolicValues = new LinkedHashSet<>();
Set<Symbol> argSymbols = new LinkedHashSet<>();
arguments.stream().filter(svs -> parameterCausingExceptionSV == svs.symbolicValue() || hasConstraintOtherThanNonNull(svs, programState)).forEach(svs -> {
argSymbolicValues.add(svs.symbolicValue());
Symbol symbol = svs.symbol();
if (symbol != null) {
argSymbols.add(symbol);
}
});
List<Class<? extends Constraint>> domains = domainsFromArguments(programState, argSymbolicValues);
return FlowComputation.flow(node, argSymbolicValues, c -> true, c -> false, domains, argSymbols);
}
use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class RelationalSymbolicValue method copyConstraintFromTo.
private List<ProgramState> copyConstraintFromTo(SymbolicValue from, SymbolicValue to, ProgramState programState, Set<RelationalSymbolicValue> knownRelations) {
List<ProgramState> states = new ArrayList<>();
states.add(programState);
ConstraintsByDomain leftConstraints = programState.getConstraints(from);
if (leftConstraints == null) {
return states;
}
leftConstraints.forEach((d, c) -> {
Constraint constraint = c.copyOver(kind);
if (constraint != null) {
List<ProgramState> newStates = applyConstraint(constraint, to, states, knownRelations);
states.clear();
states.addAll(newStates);
}
});
return states;
}
use of org.sonar.java.se.constraint.Constraint in project sonar-java by SonarSource.
the class ExceptionalCheckBasedYield method applicableOnVarArgs.
private boolean applicableOnVarArgs(List<Type> invocationTypes) {
int numberParametersYield = parametersConstraints.size();
int numberArgumentsInCall = invocationTypes.size();
if (numberParametersYield > numberArgumentsInCall) {
// VarArgs method called without variadic parameter
return true;
}
ConstraintsByDomain lastParamConstraint = parametersConstraints.get(numberParametersYield - 1);
if (lastParamConstraint.isEmpty()) {
// no constraint on the last parameter on yield side
return true;
}
if (numberParametersYield != numberArgumentsInCall) {
// there is a constraint on last parameter, but varArgs method called with multiple arguments in variadic part
return false;
}
// compatible number of parameters, type must be compatible on arguments side
Type lastArgumentType = invocationTypes.get(numberArgumentsInCall - 1);
return !isMethodVarargs || (lastArgumentType.isArray() || lastArgumentType.is("<nulltype>"));
}
Aggregations