Search in sources :

Example 1 with Expression

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;
}
Also used : UCFGBuilder(org.sonar.ucfg.UCFGBuilder) Expression(org.sonar.ucfg.Expression) Label(org.sonar.ucfg.Label) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) AnnotationTree(org.sonar.plugins.java.api.tree.AnnotationTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) LiteralTree(org.sonar.plugins.java.api.tree.LiteralTree) Tree(org.sonar.plugins.java.api.tree.Tree) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) ReturnStatementTree(org.sonar.plugins.java.api.tree.ReturnStatementTree) BlockBuilder(org.sonar.ucfg.UCFGBuilder.BlockBuilder)

Example 2 with Expression

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));
            }
        }
    }
}
Also used : Expression(org.sonar.ucfg.Expression) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) BinaryExpressionTree(org.sonar.plugins.java.api.tree.BinaryExpressionTree) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree)

Aggregations

AssignmentExpressionTree (org.sonar.plugins.java.api.tree.AssignmentExpressionTree)2 BinaryExpressionTree (org.sonar.plugins.java.api.tree.BinaryExpressionTree)2 ExpressionTree (org.sonar.plugins.java.api.tree.ExpressionTree)2 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)2 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)2 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)2 Expression (org.sonar.ucfg.Expression)2 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)1 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)1 LiteralTree (org.sonar.plugins.java.api.tree.LiteralTree)1 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)1 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)1 Tree (org.sonar.plugins.java.api.tree.Tree)1 Label (org.sonar.ucfg.Label)1 UCFGBuilder (org.sonar.ucfg.UCFGBuilder)1 BlockBuilder (org.sonar.ucfg.UCFGBuilder.BlockBuilder)1