use of org.checkerframework.dataflow.expression.JavaExpression in project checker-framework by typetools.
the class SameLenAnnotatedTypeFactory method getAnnotatedTypeLhs.
// Handles case "user-written SameLen"
@Override
public AnnotatedTypeMirror getAnnotatedTypeLhs(Tree tree) {
AnnotatedTypeMirror atm = super.getAnnotatedTypeLhs(tree);
if (tree.getKind() == Tree.Kind.VARIABLE) {
AnnotationMirror sameLenAnno = atm.getAnnotation(SameLen.class);
if (sameLenAnno != null) {
JavaExpression je = JavaExpression.fromVariableTree((VariableTree) tree);
String varName = je.toString();
List<String> exprs = AnnotationUtils.getElementValueArray(sameLenAnno, sameLenValueElement, String.class);
exprs.remove(varName);
if (exprs.isEmpty()) {
atm.replaceAnnotation(UNKNOWN);
} else {
atm.replaceAnnotation(createSameLen(exprs));
}
}
}
return atm;
}
use of org.checkerframework.dataflow.expression.JavaExpression in project checker-framework by typetools.
the class SameLenTransfer method addInformationFromPreconditions.
/**
* Overridden to ensure that SameLen annotations on method parameters are symmetric.
*/
@Override
protected void addInformationFromPreconditions(CFStore info, AnnotatedTypeFactory factory, UnderlyingAST.CFGMethod method, MethodTree methodTree, ExecutableElement methodElement) {
super.addInformationFromPreconditions(info, factory, method, methodTree, methodElement);
List<? extends VariableTree> paramTrees = methodTree.getParameters();
int numParams = paramTrees.size();
List<String> paramNames = new ArrayList<>(numParams);
List<AnnotatedTypeMirror> params = new ArrayList<>(numParams);
for (VariableTree tree : paramTrees) {
paramNames.add(tree.getName().toString());
params.add(aTypeFactory.getAnnotatedType(tree));
}
for (int index = 0; index < numParams; index++) {
// If the parameter has a samelen annotation, then look for other parameters in that
// annotation and propagate default the other annotation so that it is symmetric.
AnnotatedTypeMirror atm = params.get(index);
AnnotationMirror sameLenAnno = atm.getAnnotation(SameLen.class);
if (sameLenAnno == null) {
continue;
}
List<String> values = AnnotationUtils.getElementValueArray(sameLenAnno, aTypeFactory.sameLenValueElement, String.class);
for (String value : values) {
int otherParamIndex = paramNames.indexOf(value);
if (otherParamIndex == -1) {
continue;
}
// the SameLen value is in the list of params, so modify the type of
// that param in the store
AnnotationMirror newSameLen = aTypeFactory.createSameLen(Collections.singletonList(paramNames.get(index)));
JavaExpression otherParamRec = JavaExpression.fromVariableTree(paramTrees.get(otherParamIndex));
info.insertValuePermitNondeterministic(otherParamRec, newSameLen);
}
}
}
use of org.checkerframework.dataflow.expression.JavaExpression in project checker-framework by typetools.
the class SameLenVisitor method commonAssignmentCheck.
/**
* Merges SameLen annotations, then calls super.
*
* <p>{@inheritDoc}
*/
@Override
protected void commonAssignmentCheck(AnnotatedTypeMirror varType, AnnotatedTypeMirror valueType, Tree valueTree, @CompilerMessageKey String errorKey, Object... extraArgs) {
if (IndexUtil.isSequenceType(valueType.getUnderlyingType()) && TreeUtils.isExpressionTree(valueTree) && // if both annotations are @PolySameLen, there is nothing to do
!(valueType.hasAnnotation(PolySameLen.class) && varType.hasAnnotation(PolySameLen.class))) {
JavaExpression rhs = JavaExpression.fromTree((ExpressionTree) valueTree);
if (rhs != null && SameLenAnnotatedTypeFactory.mayAppearInSameLen(rhs)) {
String rhsExpr = rhs.toString();
AnnotationMirror sameLenAnno = valueType.getAnnotation(SameLen.class);
Collection<String> exprs;
if (sameLenAnno == null) {
exprs = Collections.singletonList(rhsExpr);
} else {
exprs = new TreeSet<>(AnnotationUtils.getElementValueArray(sameLenAnno, atypeFactory.sameLenValueElement, String.class));
exprs.add(rhsExpr);
}
AnnotationMirror newSameLen = atypeFactory.createSameLen(exprs);
valueType.replaceAnnotation(newSameLen);
}
}
super.commonAssignmentCheck(varType, valueType, valueTree, errorKey, extraArgs);
}
use of org.checkerframework.dataflow.expression.JavaExpression in project checker-framework by typetools.
the class OffsetEquation method createOffsetFromNode.
/**
* Updates an offset equation from a Node.
*
* @param node the Node from which to create an offset equation
* @param factory an AnnotationTypeFactory
* @param eq an OffsetEquation to update
* @param op '+' or '-'
*/
private static void createOffsetFromNode(Node node, AnnotationProvider factory, OffsetEquation eq, char op) {
JavaExpression je = JavaExpression.fromNode(node);
if (je instanceof Unknown || je == null) {
if (node instanceof NumericalAdditionNode) {
createOffsetFromNode(((NumericalAdditionNode) node).getLeftOperand(), factory, eq, op);
createOffsetFromNode(((NumericalAdditionNode) node).getRightOperand(), factory, eq, op);
} else if (node instanceof NumericalSubtractionNode) {
createOffsetFromNode(((NumericalSubtractionNode) node).getLeftOperand(), factory, eq, op);
char other = op == '+' ? '-' : '+';
createOffsetFromNode(((NumericalSubtractionNode) node).getRightOperand(), factory, eq, other);
} else {
eq.error = node.toString();
}
} else {
eq.addTerm(op, je.toString());
}
}
use of org.checkerframework.dataflow.expression.JavaExpression in project checker-framework by typetools.
the class UpperBoundAnnotatedTypeFactory method fromLessThanOrEqual.
private UBQualifier fromLessThanOrEqual(Tree tree, TreePath treePath, List<String> lessThanExpressions) {
UBQualifier ubQualifier = null;
for (String expression : lessThanExpressions) {
Pair<JavaExpression, String> exprAndOffset;
try {
exprAndOffset = getExpressionAndOffsetFromJavaExpressionString(expression, treePath);
} catch (JavaExpressionParseException e) {
exprAndOffset = null;
}
if (exprAndOffset == null) {
continue;
}
JavaExpression je = exprAndOffset.first;
String offset = exprAndOffset.second;
if (!CFAbstractStore.canInsertJavaExpression(je)) {
continue;
}
CFStore store = getStoreBefore(tree);
CFValue value = store.getValue(je);
if (value != null && value.getAnnotations().size() == 1) {
UBQualifier newUBQ = UBQualifier.createUBQualifier(qualHierarchy.findAnnotationInHierarchy(value.getAnnotations(), UNKNOWN), AnnotatedTypeFactory.negateConstant(offset), (IndexChecker) checker);
if (ubQualifier == null) {
ubQualifier = newUBQ;
} else {
ubQualifier = ubQualifier.glb(newUBQ);
}
}
}
return ubQualifier;
}
Aggregations