Commit 362b3739 authored by Sergio Costas's avatar Sergio Costas

Added test to check a crust variable is NULL or not NULL with IF

parent 7bbce441
......@@ -264,6 +264,9 @@ class crusty(object):
def _eval_condition(self,condition,var_container,function_container):
""" Evaluates a condition, and returns a list of possible branches, each one with its variables set """
var_container = self._copy_container(var_container)
condition = condition[0]
#print("Condicion: {:s}".format(condition.type))
......@@ -315,6 +318,7 @@ class crusty(object):
def _eval_function_call(self,node,var_container,function_container):
if node.name not in function_container:
self._add_error(self.MSG_CRITICAL,"Calling function '{:s}' at line {:d}, but it is not declared or defined",node.name,node.line)
return [ (self.STATUS_NOT_NULL_OR_NULL, self._copy_container(var_container), self.TYPE_NO_MATTER, node.name) ]
......@@ -440,7 +444,7 @@ class crusty(object):
def _get_next_block(self,current_block):
if current_block[0].type != "START_BLOCK":
# The block has only one statement, so it isn't a "block"
return (current_block[0],current_block[1:])
return ([current_block[0]],current_block[1:])
depth = 0
pos = 0
while True:
......@@ -448,11 +452,11 @@ class crusty(object):
depth += 1
elif current_block[pos].type == "END_BLOCK":
depth -= 1
if depth == 0:
break
pos += 1
if depth == 0:
break
# return the block with both START_BLOCK and END_BLOCK statements
return (current_block[0:pos+1],current_block[pos+1:])
return (current_block[0:pos],current_block[pos:])
def _check_variable_properties(self,variable,property):
......@@ -608,6 +612,7 @@ class crusty(object):
def _process_block2(self, tree, var_container, function_container):
node = tree[0]
#print("Tipo: {:s}; Nombre: {:s}; Linea: {:s}".format(str(node.type),str(node.name),str(node.line)))
if (node.type == "TYPE_SPECIFIER"):
if (node.enum_data is not None):
self._process_enum(node,var_container)
......@@ -640,6 +645,7 @@ class crusty(object):
return retvals
else:
return self._check_return_block(tree[1:],var_container)
return self._check_return_block(tree[1:],var_container)
if node.type == "=":
varname = node.child1[0].name
var_defined = self._find_variable(var_container,varname,node.line)
......@@ -658,10 +664,27 @@ class crusty(object):
for option in options:
retvals += self._check_return_block(tree[1:],option[1])
return retvals
#if node.type == "IF":
#retvals = self._eval_condition(node.condition,var_container,function_container)
#continue
if node.type == "IF":
tree = tree[1:]
block_true, block_other = self._get_next_block(tree)
if block_other[0].type == "ELSE":
block_false, block_other = self._get_next_block(block_other[1:])
block_true += block_other
block_false += block_other
else:
block_true += block_other
block_false = block_other
options = self._eval_condition(node.condition,var_container,function_container)
retvals = []
for option in options:
if option[0] == self.CONDITION_FALSE_TRUE:
retvals += self._check_return_block(block_true,self._copy_container(option[1]))
retvals += self._check_return_block(block_false,self._copy_container(option[1]))
elif option[0] == self.CONDITION_TRUE:
retvals += self._check_return_block(block_true,self._copy_container(option[1]))
else: # option[0] == self.CONDITION_FALSE:
retvals += self._check_return_block(block_false,self._copy_container(option[1]))
return retvals
# if node.type == "FOR":
# print("Tipo: {:s}; Nombre: {:s}".format(str(node.type),str(node.name)))
# b1,b2 = lib.get_next_block(tree[counter+1:])
......@@ -671,5 +694,5 @@ class crusty(object):
# process_nodes(b2,False,var_container,function_container, tabs)
#
# break
print("Tipo: {:s}; Nombre: {:s}".format(str(node.type),str(node.name)))
print("Tipo: {:s}; Nombre: {:s}; Linea: {:s}".format(str(node.type),str(node.name),str(node.line)))
return self._check_return_block(tree[1:],var_container)
......@@ -175,5 +175,11 @@ class Test(unittest.TestCase):
def testStructAssignmentByRef(self):
self._all_fine_test("unitest/test43.c")
def testIfEqNull(self):
self._all_fine_test("unitest/test44.c")
def testIfNoEqNull(self):
self._all_fine_test("unitest/test45.c")
if __name__ == '__main__':
unittest.main()
typedef __crust_t__ unsigned char *crust_t;
#define NULL ((void *)0)
void main(crust_t param) {
crust_t var2 = (crust_t) 5;
if (param == NULL) {
param = var2;
}
}
typedef __crust_t__ unsigned char *crust_t;
#define NULL ((void *)0)
void main(crust_t param) {
crust_t var2 = (crust_t) 5;
if (param != NULL) {
main(param);
}
param = var2;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment