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();
}
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;
}
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;
}
Aggregations