Search in sources :

Example 1 with BlockBuilder

use of org.sonar.ucfg.UCFGBuilder.BlockBuilder in project sonar-java by SonarSource.

the class UCFGJavaVisitor method buildUCfg.

private UCFG buildUCfg(MethodTree methodTree, CFG cfg) {
    String signature = signatureFor(methodTree.symbol());
    IdentifierGenerator idGenerator = new IdentifierGenerator(methodTree);
    UCFGBuilder builder = UCFGBuilder.createUCFGForMethod(signature);
    methodTree.parameters().stream().map(p -> idGenerator.lookupIdFor(p.symbol())).map(UCFGBuilder::variableWithId).forEach(builder::addMethodParam);
    BlockBuilder entryBlockBuilder = buildBasicBlock(cfg.entry(), methodTree, idGenerator);
    if (getAnnotatedStringParameters(methodTree).count() > 0) {
        builder.addStartingBlock(buildParameterAnnotationsBlock(methodTree, idGenerator, cfg));
        builder.addBasicBlock(entryBlockBuilder);
    } else {
        builder.addStartingBlock(entryBlockBuilder);
    }
    cfg.blocks().stream().filter(b -> !b.equals(cfg.entry())).forEach(b -> builder.addBasicBlock(buildBasicBlock(b, methodTree, idGenerator)));
    return builder.build();
}
Also used : JavaSymbol(org.sonar.java.resolve.JavaSymbol) Label(org.sonar.ucfg.Label) BaseTreeVisitor(org.sonar.plugins.java.api.tree.BaseTreeVisitor) JavaFileScanner(org.sonar.plugins.java.api.JavaFileScanner) Loggers(org.sonar.api.utils.log.Loggers) MemberSelectExpressionTree(org.sonar.plugins.java.api.tree.MemberSelectExpressionTree) PLUS(org.sonar.plugins.java.api.tree.Tree.Kind.PLUS) Map(java.util.Map) SyntaxToken(org.sonar.plugins.java.api.tree.SyntaxToken) Set(java.util.Set) Collectors(java.util.stream.Collectors) JavaFileScannerContext(org.sonar.plugins.java.api.JavaFileScannerContext) Sets(com.google.common.collect.Sets) List(java.util.List) UCFGtoProtobuf(org.sonar.ucfg.UCFGtoProtobuf) Stream(java.util.stream.Stream) MEMBER_SELECT(org.sonar.plugins.java.api.tree.Tree.Kind.MEMBER_SELECT) UCFGBuilder.variableWithId(org.sonar.ucfg.UCFGBuilder.variableWithId) AssignmentExpressionTree(org.sonar.plugins.java.api.tree.AssignmentExpressionTree) IdentifierTree(org.sonar.plugins.java.api.tree.IdentifierTree) Symbol(org.sonar.plugins.java.api.semantic.Symbol) ASSIGNMENT(org.sonar.plugins.java.api.tree.Tree.Kind.ASSIGNMENT) PLUS_ASSIGNMENT(org.sonar.plugins.java.api.tree.Tree.Kind.PLUS_ASSIGNMENT) UCFG(org.sonar.ucfg.UCFG) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ExpressionTree(org.sonar.plugins.java.api.tree.ExpressionTree) HashSet(java.util.HashSet) BlockBuilder(org.sonar.ucfg.UCFGBuilder.BlockBuilder) VariableTree(org.sonar.plugins.java.api.tree.VariableTree) UCFGBuilder(org.sonar.ucfg.UCFGBuilder) LiteralUtils(org.sonar.java.model.LiteralUtils) Nullable(javax.annotation.Nullable) Logger(org.sonar.api.utils.log.Logger) 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) UCFGBuilder.call(org.sonar.ucfg.UCFGBuilder.call) METHOD_INVOCATION(org.sonar.plugins.java.api.tree.Tree.Kind.METHOD_INVOCATION) Tree(org.sonar.plugins.java.api.tree.Tree) Type(org.sonar.plugins.java.api.semantic.Type) File(java.io.File) MethodInvocationTree(org.sonar.plugins.java.api.tree.MethodInvocationTree) LocationInFile(org.sonar.ucfg.LocationInFile) CFG(org.sonar.java.cfg.CFG) VariableReadExtractor(org.sonar.java.cfg.VariableReadExtractor) Preconditions(com.google.common.base.Preconditions) Expression(org.sonar.ucfg.Expression) UCFGBuilder.constant(org.sonar.ucfg.UCFGBuilder.constant) MethodTree(org.sonar.plugins.java.api.tree.MethodTree) UCFGBuilder(org.sonar.ucfg.UCFGBuilder) BlockBuilder(org.sonar.ucfg.UCFGBuilder.BlockBuilder)

Example 2 with BlockBuilder

use of org.sonar.ucfg.UCFGBuilder.BlockBuilder 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 3 with BlockBuilder

use of org.sonar.ucfg.UCFGBuilder.BlockBuilder in project sonar-java by SonarSource.

the class UCFGJavaVisitor method buildParameterAnnotationsBlock.

private BlockBuilder buildParameterAnnotationsBlock(MethodTree methodTree, IdentifierGenerator idGenerator, CFG cfg) {
    LocationInFile parametersLocation = location(methodTree.openParenToken(), methodTree.closeParenToken());
    UCFGBuilder.BlockBuilder blockBuilder = UCFGBuilder.newBasicBlock("paramAnnotations", parametersLocation);
    getAnnotatedStringParameters(methodTree).forEach(parameter -> buildBlockForParameter(parameter, blockBuilder, idGenerator));
    Label nextBlockLabel = UCFGBuilder.createLabel(Integer.toString(cfg.entry().id()));
    blockBuilder.jumpTo(nextBlockLabel);
    return blockBuilder;
}
Also used : UCFGBuilder(org.sonar.ucfg.UCFGBuilder) Label(org.sonar.ucfg.Label) LocationInFile(org.sonar.ucfg.LocationInFile) BlockBuilder(org.sonar.ucfg.UCFGBuilder.BlockBuilder)

Aggregations

Label (org.sonar.ucfg.Label)3 UCFGBuilder (org.sonar.ucfg.UCFGBuilder)3 BlockBuilder (org.sonar.ucfg.UCFGBuilder.BlockBuilder)3 AnnotationTree (org.sonar.plugins.java.api.tree.AnnotationTree)2 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 IdentifierTree (org.sonar.plugins.java.api.tree.IdentifierTree)2 LiteralTree (org.sonar.plugins.java.api.tree.LiteralTree)2 MemberSelectExpressionTree (org.sonar.plugins.java.api.tree.MemberSelectExpressionTree)2 MethodInvocationTree (org.sonar.plugins.java.api.tree.MethodInvocationTree)2 MethodTree (org.sonar.plugins.java.api.tree.MethodTree)2 ReturnStatementTree (org.sonar.plugins.java.api.tree.ReturnStatementTree)2 Tree (org.sonar.plugins.java.api.tree.Tree)2 VariableTree (org.sonar.plugins.java.api.tree.VariableTree)2 Expression (org.sonar.ucfg.Expression)2 LocationInFile (org.sonar.ucfg.LocationInFile)2 Preconditions (com.google.common.base.Preconditions)1 Sets (com.google.common.collect.Sets)1 File (java.io.File)1