Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_scalar_children(self):
b1 = c_ast.BinaryOp(
op='+',
left=c_ast.Constant(type='int', value='6'),
right=c_ast.ID(name='joe'))
cv = self.ConstantVisitor()
cv.visit(b1)
self.assertEqual(cv.values, ['6'])
b2 = c_ast.BinaryOp(
op='*',
left=c_ast.Constant(type='int', value='111'),
right=b1)
b3 = c_ast.BinaryOp(
op='^',
left=b2,
right=b1)
cv = self.ConstantVisitor()
cv.visit(b3)
self.assertEqual(cv.values, ['111', '6', '6'])
b1 = c_ast.BinaryOp(
op='+',
left=c_ast.Constant(type='int', value='6'),
right=c_ast.ID(name='joe'))
cv = self.ConstantVisitor()
cv.visit(b1)
self.assertEqual(cv.values, ['6'])
b2 = c_ast.BinaryOp(
op='*',
left=c_ast.Constant(type='int', value='111'),
right=b1)
b3 = c_ast.BinaryOp(
op='^',
left=b2,
right=b1)
cv = self.ConstantVisitor()
cv.visit(b3)
self.assertEqual(cv.values, ['111', '6', '6'])
def test_repr(self):
c1 = c_ast.Constant(type='float', value='5.6')
c2 = c_ast.Constant(type='char', value='t')
b1 = c_ast.BinaryOp(
op='+',
left=c1,
right=c2)
b2 = c_ast.BinaryOp(
op='-',
left=b1,
right=c2)
comp = c_ast.Compound(
block_items=[b1, b2, c1, c2])
expected = ("Compound(block_items=[BinaryOp(op='+',\n"
" left=Constant(type='float',\n"
" value='5.6'\n"
" ),\n"
def test_repr(self):
c1 = c_ast.Constant(type='float', value='5.6')
c2 = c_ast.Constant(type='char', value='t')
b1 = c_ast.BinaryOp(
op='+',
left=c1,
right=c2)
b2 = c_ast.BinaryOp(
op='-',
left=b1,
right=c2)
comp = c_ast.Compound(
block_items=[b1, b2, c1, c2])
expected = ("Compound(block_items=[BinaryOp(op='+',\n"
" left=Constant(type='float',\n"
" value='5.6'\n"
" ),\n"
" right=Constant(type='char',\n"
" value='t'\n"
" )\n"
" ),\n"
" BinaryOp(op='-',\n"
def plus1(node: ca.Node) -> ca.BinaryOp:
return ca.BinaryOp("+", node, ca.Constant("int", "1"))
return -self._parse_constant(exprnode.expr)
# load previously defined int constant
if (isinstance(exprnode, pycparser.c_ast.ID) and
exprnode.name in self._int_constants):
return self._int_constants[exprnode.name]
#
if (isinstance(exprnode, pycparser.c_ast.ID) and
exprnode.name == '__dotdotdotarray__'):
if partial_length_ok:
self._partial_length = True
return '...'
raise FFIError(":%d: unsupported '[...]' here, cannot derive "
"the actual array length in this context"
% exprnode.coord.line)
#
if (isinstance(exprnode, pycparser.c_ast.BinaryOp) and
exprnode.op == '+'):
return (self._parse_constant(exprnode.left) +
self._parse_constant(exprnode.right))
#
if (isinstance(exprnode, pycparser.c_ast.BinaryOp) and
exprnode.op == '-'):
return (self._parse_constant(exprnode.left) -
self._parse_constant(exprnode.right))
#
raise FFIError(":%d: unsupported expression: expected a "
"simple numeric constant" % exprnode.coord.line)
def randomize_associative_binop(left: ca.Node, right: ca.BinaryOp) -> ca.BinaryOp:
"""Try moving parentheses to the left side sometimes (sadly, it seems to matter)"""
if random.choice([True, False]) and right.op in ["+", "-"]:
# ((a + b) - c)
return ca.BinaryOp(
right.op, ca.BinaryOp("+", left, right.left), right.right
)
else:
# (a + (b - c))
return ca.BinaryOp("+", left, right)
#
if (isinstance(exprnode, pycparser.c_ast.ID) and
exprnode.name == '__dotdotdotarray__'):
if partial_length_ok:
self._partial_length = True
return '...'
raise FFIError(":%d: unsupported '[...]' here, cannot derive "
"the actual array length in this context"
% exprnode.coord.line)
#
if (isinstance(exprnode, pycparser.c_ast.BinaryOp) and
exprnode.op == '+'):
return (self._parse_constant(exprnode.left) +
self._parse_constant(exprnode.right))
#
if (isinstance(exprnode, pycparser.c_ast.BinaryOp) and
exprnode.op == '-'):
return (self._parse_constant(exprnode.left) -
self._parse_constant(exprnode.right))
#
raise FFIError(":%d: unsupported expression: expected a "
"simple numeric constant" % exprnode.coord.line)
| binary_expression LT binary_expression
| binary_expression LE binary_expression
| binary_expression GE binary_expression
| binary_expression GT binary_expression
| binary_expression EQ binary_expression
| binary_expression NE binary_expression
| binary_expression AND binary_expression
| binary_expression OR binary_expression
| binary_expression XOR binary_expression
| binary_expression LAND binary_expression
| binary_expression LOR binary_expression
"""
if len(p) == 2:
p[0] = p[1]
else:
p[0] = c_ast.BinaryOp(p[2], p[1], p[3], p[1].coord)