Commit e55f2808 authored by Sergio Costas's avatar Sergio Costas

Now processes assignments from variables

parent 0d3e1de7
......@@ -11,6 +11,6 @@ if tree is None:
try:
variables,functions = lib.process_tree(tree)
lib.process_functions(tree,variables,functions)
except:
except crusty.VarNotFoundException:
pass
lib.print_errors()
......@@ -379,8 +379,21 @@ class crusty(object):
return retvals
elif assignment.type == "FUNCTION_CALL":
return self._eval_function_call(assignment,var_container,function_container)
elif assignment.type == "IDENTIFIER":
vardata = self._find_variable(var_container,assignment.name,assignment.line)
if vardata["status"] == self.STATUS_UNINITIALIZED:
self._add_error(self.MSG_ERROR,"Using the value of '{:s}' at line {:d} in an assignment, but it is uninitialized",assignment.name,assignment.line)
return [ (self.STATUS_UNINITIALIZED, self._copy_container(var_container)) ]
elif vardata["status"] == self.STATUS_FREED:
self._add_error(self.MSG_ERROR,"Using the value of '{:s}' at line {:d} in an assignment, but it was freed at line {:d}",assignment.name,assignment.line,vardata["init_line"])
return [ (self.STATUS_FREED, self._copy_container(var_container)) ]
elif vardata["status"] == self.STATUS_GLOBAL:
return [ (self.STATUS_NOT_NULL_OR_NULL, self._copy_container(var_container)) ]
newstatus = vardata["status"]
self._set_var_value(var_container,assignment.name,self.STATUS_FREED,assignment.line,True,True)
return [ (newstatus, self._copy_container(var_container)) ]
else:
print("assignment type unknown '{:s}'".format(assignment.type))
print("assignment type unknown '{:s}' at line {:d}".format(assignment.type,assignment.line))
return [ (self.STATUS_NOT_NULL_OR_NULL, self._copy_container(var_container)) ]
......@@ -442,7 +455,7 @@ class crusty(object):
def _process_enum(self,enum_node,var_container):
counter = 0
for node in enum_node.enum_data:
var_container[0][node.name] = {"properties": 0, "status":crusty.STATUS_GLOBAL, "init_line": node.line, "pointer": 0 }
var_container[0][node.name] = {"properties": 0, "status":crusty.STATUS_GLOBAL, "init_line": node.line, "pointer": 0, "enum": True }
def process_tree(self,tree):
......@@ -460,7 +473,7 @@ class crusty(object):
continue
if node.type == "VARIABLE_DEFINITION":
if not node.t_typedef:
var_container[0][node.name] = {"properties":self._get_variable_properties(node,False), "status":crusty.STATUS_GLOBAL, "init_line": None, "pointer": node.pointer }
var_container[0][node.name] = {"properties":self._get_variable_properties(node,False), "status":crusty.STATUS_GLOBAL, "init_line": None, "pointer": node.pointer, "enum": False }
continue
if (node.type == "FUNCTION_DECLARATION") or (node.type == "FUNCTION_DEFINITION"):
counter = 0
......@@ -494,7 +507,7 @@ class crusty(object):
tmpvars.insert(0,{})
if node.function_params is not None:
for element in node.function_params:
tmpvars[0][element.name] = {"properties":self._get_variable_properties(element,True), "status":crusty.STATUS_NOT_NULL_OR_NULL, "init_line": element.line, "pointer": element.pointer }
tmpvars[0][element.name] = {"properties":self._get_variable_properties(element,True), "status":crusty.STATUS_NOT_NULL_OR_NULL, "init_line": element.line, "pointer": element.pointer, "enum": False }
self._process_block(node.child1,tmpvars, function_container)
......@@ -521,7 +534,7 @@ class crusty(object):
continue
if node.type == "VARIABLE_DEFINITION":
if not node.t_typedef:
var_defined = {"properties":self._get_variable_properties(node,False), "status":crusty.STATUS_UNINITIALIZED, "init_line": None, "pointer": node.pointer }
var_defined = {"properties":self._get_variable_properties(node,False), "status":crusty.STATUS_UNINITIALIZED, "init_line": None, "pointer": node.pointer, "enum": False }
var_container[0][node.name] = var_defined
if node.assignment is not None:
var_defined["init_line"] = node.line
......
......@@ -106,5 +106,12 @@ class Test(unittest.TestCase):
def testFreedVariable(self):
self._generic_test("unitest/test21.c",False,crusty.crusty.MSG_ERROR,"Using variable '{:s}' at line {:d}, after being freed at line {:d}","param4",6,5)
def testAssignFreedVariable(self):
self._generic_test("unitest/test22.c",False,crusty.crusty.MSG_ERROR,"Using the value of '{:s}' at line {:d} in an assignment, but it was freed at line {:d}","param1",5,4)
def testAssignUninitializedVariable(self):
self._generic_test("unitest/test23.c",False,crusty.crusty.MSG_ERROR,"Using the value of '{:s}' at line {:d} in an assignment, but it is uninitialized","param4",5)
if __name__ == '__main__':
unittest.main()
typedef __crust_t__ int * crust_t;
int function(crust_t param1) {
crust_t param4 = param1;
crust_t param5 = param1;
}
typedef __crust_t__ int * crust_t;
int function() {
crust_t param4;
crust_t param5 = param4;
}
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