use of org.sonar.ucfg.Expression in project sonar-java by SonarSource.
the class UCFGJavaVisitor method buildBasicBlock.
private UCFGBuilder.BlockBuilder buildBasicBlock(CFG.Block javaBlock, MethodTree methodTree, IdentifierGenerator idGenerator) {
UCFGBuilder.BlockBuilder blockBuilder = UCFGBuilder.newBasicBlock(String.valueOf(javaBlock.id()), location(javaBlock));
javaBlock.elements().forEach(e -> buildCall(e, blockBuilder, idGenerator));
Tree terminator = javaBlock.terminator();
if (terminator != null && terminator.is(Tree.Kind.RETURN_STATEMENT)) {
ExpressionTree returnedExpression = ((ReturnStatementTree) terminator).expression();
Expression retExpr = constant(IdentifierGenerator.CONST);
if (methodTree.returnType() != null && isString(methodTree.returnType().symbolType())) {
retExpr = idGenerator.lookupExpressionFor(returnedExpression);
}
blockBuilder.ret(retExpr, location(terminator));
return blockBuilder;
}
Set<CFG.Block> successors = javaBlock.successors();
if (!successors.isEmpty()) {
blockBuilder.jumpTo(successors.stream().map(b -> UCFGBuilder.createLabel(Integer.toString(b.id()))).toArray(Label[]::new));
return blockBuilder;
}
Preconditions.checkState(javaBlock.id() == 0);
blockBuilder.ret(constant("implicit return"), location(methodTree.lastToken()));
return blockBuilder;
}
use of org.sonar.ucfg.Expression in project sonar-java by SonarSource.
the class UCFGJavaVisitor method buildCall.
private void buildCall(Tree element, UCFGBuilder.BlockBuilder blockBuilder, IdentifierGenerator idGenerator) {
if (isStringVarDecl(element)) {
VariableTree variableTree = (VariableTree) element;
String lhs = idGenerator.lookupIdFor(variableTree.simpleName());
if (!idGenerator.isConst(lhs)) {
ExpressionTree initializer = variableTree.initializer();
String source = idGenerator.lookupIdFor(initializer);
blockBuilder.assignTo(variableWithId(lhs), UCFGBuilder.call("__id").withArgs(variableWithId(source)), location(element));
}
return;
}
if (element.is(METHOD_INVOCATION)) {
MethodInvocationTree methodInvocationTree = (MethodInvocationTree) element;
buildMethodInvocation(blockBuilder, idGenerator, methodInvocationTree);
} else if (element.is(PLUS, PLUS_ASSIGNMENT, ASSIGNMENT) && isString(((ExpressionTree) element).symbolType())) {
if (element.is(PLUS)) {
BinaryExpressionTree binaryExpressionTree = (BinaryExpressionTree) element;
Expression lhs = idGenerator.lookupExpressionFor(binaryExpressionTree.leftOperand());
Expression rhs = idGenerator.lookupExpressionFor(binaryExpressionTree.rightOperand());
Expression.Variable var = variableWithId(idGenerator.newIdFor(binaryExpressionTree));
blockBuilder.assignTo(var, call("__concat").withArgs(lhs, rhs), location(element));
} else if (element.is(PLUS_ASSIGNMENT)) {
Expression var = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).variable());
Expression expr = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).expression());
if (!var.isConstant()) {
idGenerator.varForExpression(element, ((Expression.Variable) var).id());
blockBuilder.assignTo((Expression.Variable) var, call("__concat").withArgs(var, expr), location(element));
}
} else if (element.is(ASSIGNMENT)) {
Expression var = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).variable());
Expression expr = idGenerator.lookupExpressionFor(((AssignmentExpressionTree) element).expression());
if (!var.isConstant()) {
blockBuilder.assignTo((Expression.Variable) var, call("__id").withArgs(expr), location(element));
}
}
}
}
Aggregations