Commit 80362e28 authored by Sergio Costas's avatar Sergio Costas

Now only takes into account parameters and return values that are pointers,...

Now only takes into account parameters and return values that are pointers, never pointers to pointers
parent 0adfefa4
......@@ -317,7 +317,7 @@ class crusty(object):
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) ]
function_data = self._find_function(function_container,node.name,node.line)
retval_type = self.TYPE_CRUST if ((function_data[0] & self.PROPERTY_CRUST_VARIABLE) != 0) else self.TYPE_NO_CRUST
retval_type = self.TYPE_CRUST if (((function_data[0][0] & self.PROPERTY_CRUST_VARIABLE) != 0) and (function_data[0][1] == 1)) else self.TYPE_NO_CRUST
if ((node.function_params is None) and ((len(function_data)) > 1)):
self._add_error(self.MSG_CRITICAL,"Calling function '{:s}' at line {:d} with an incorrect number of parameters",node.name,node.line)
return [ (self.STATUS_NOT_NULL_OR_NULL, self._copy_container(var_container), retval_type, node.name) ]
......@@ -331,15 +331,15 @@ class crusty(object):
for pos in range(len(node.function_params)):
variable = node.function_params[pos]
if variable.type != "IDENTIFIER":
if ((function_data[pos+1] & self.PROPERTY_CRUST_VARIABLE) != 0) and (not variable.t_null):
if ((function_data[pos+1][0] & self.PROPERTY_CRUST_VARIABLE) != 0) and (function_data[pos+1][1] == 1) and (not variable.t_null):
self._add_error(self.MSG_CRITICAL,"Expected a __crust_t__ variable at parameter {:d} when calling function '{:s}' at line {:d}",pos+1,node.name,node.line)
error_found = True
continue
node_var = self._find_variable(newvar_container,variable.name,node.line)
var_is_crusty = self._check_variable_properties(node_var,self.PROPERTY_CRUST_VARIABLE)
param_is_crusty = (function_data[pos+1] & self.PROPERTY_CRUST_VARIABLE) != 0
param_is_borrowed = (function_data[pos+1] & self.PROPERTY_BORROWED) != 0
param_is_recycled = (function_data[pos+1] & self.PROPERTY_RECYCLED) != 0
param_is_crusty = ((function_data[pos+1][0] & self.PROPERTY_CRUST_VARIABLE) != 0) and (function_data[pos+1][1] == 1)
param_is_borrowed = ((function_data[pos+1][0] & self.PROPERTY_BORROWED) != 0) and (function_data[pos+1][1] == 1)
param_is_recycled = ((function_data[pos+1][0] & self.PROPERTY_RECYCLED) != 0) and (function_data[pos+1][1] == 1)
if (not var_is_crusty) and param_is_crusty:
self._add_error(self.MSG_CRITICAL,"Expected a __crust_t__ variable at parameter {:d} when calling function '{:s}' at line {:d}, but passed non __crust_t__ variable",pos+1,node.name,node.line)
error_found = True
......@@ -522,10 +522,10 @@ class crusty(object):
self._add_error(self.MSG_CRITICAL,"The function '{:s}', at line {:d}, has a RECYCLE parameter, but doesn't return a CRUST pointer",node.name,node.line)
if not node.t_typedef:
# the first parameter is the return value
parameters = [self._get_variable_properties(node,False,True)]
parameters = [ (self._get_variable_properties(node,False,True), node.pointer) ]
if node.function_params is not None:
for parameter in node.function_params:
parameters.append( self._get_variable_properties(parameter,True) )
parameters.append( (self._get_variable_properties(parameter,True), parameter.pointer) )
function_container[node.name] = parameters
var_container[0][node.name] = { "properties":0, "status":crusty.STATUS_FUNCTION, "init_line": None, "pointer": 1, "enum": False }
continue
......@@ -616,7 +616,7 @@ class crusty(object):
continue
if node.type == "FUNCTION_CALL":
function_params = self._find_function(function_container,node.name,node.line)
if (function_params[0] & self.PROPERTY_CRUST_VARIABLE) != 0:
if ((function_params[0][0] & self.PROPERTY_CRUST_VARIABLE) != 0) and (function_params[0][1] == 1):
self._add_error(self.MSG_ERROR,"Calling the function '{:s}' at line {:d}, but ignoring the crust-type return value",node.name,node.line)
options = self._eval_function_call(node,var_container,function_container)
if len(options) > 1:
......
LIB ?= no
LIB ?= yes
ifneq ($(LIB),no)
CC = gcc -c -DLIBRARY -fpic
LN = gcc -g -shared -o crusty.so
......
......@@ -148,6 +148,8 @@ class Test(unittest.TestCase):
def testMultiAssign(self):
self._generic_test("unitest/test34.c",False,[ (crusty.crusty.MSG_CRITICAL,"Assigning a non-crust value to the crust variable '{:s}' at line {:d}","b",6), (crusty.crusty.MSG_CRITICAL,"Assigning a non-crust value to the crust variable '{:s}' at line {:d}","a",6) ])
def testAmpPointer(self):
self._all_fine_test("unitest/test35.c")
if __name__ == '__main__':
unittest.main()
......@@ -2,5 +2,5 @@ typedef __crust_t__ unsigned char *crust_t;
int function(crust_t param3) {
crust_t retval = function3(3,5);
crust_t retval = function3(3,5); // ERROR: function3 doesn't exist
}
void function(__crust_t__ int);
void function(__crust_t__ int); // ERROR: the parameter is not a pointer
void function(__crust_t__ int parameter);
void function(__crust_t__ int parameter); // ERROR: named parameter is not a pointer
......@@ -2,5 +2,5 @@ typedef __crust_t__ unsigned char crust_t;
int function() {
crust_t retval;
crust_t retval; // ERROR: the variable has crust type, but it is not a pointer
}
__crust_t__ int function() {
__crust_t__ int function() { // ERROR: retval is crust, but it is not a pointer
}
__crust_recycle__ int *var;
__crust_recycle__ int *var; // ERROR: a variable can be only __crust_t__, never __crust_recycle__ or __crust_borrow__
__crust_t__ int * function(__crust_borrow__ __crust_recycle__ int *);
__crust_t__ int * function(__crust_borrow__ __crust_recycle__ int *); // ERROR: a parameter can't be __crust_borrow__ and __crust_recycle__ at the same time
__crust_t__ int * function(__crust_borrow__ __crust_recycle__ int *param);
__crust_t__ int * function(__crust_borrow__ __crust_recycle__ int *param); // ERROR: a named parameter can't be __crust_borrow__ and __crust_recycle__ at the same time
int * function(__crust_recycle__ int *param);
int * function(__crust_recycle__ int *param); // ERROR: a function with a __crust_recycle__ parameter must return a __crust_t__ value
__crust_t__ int * function(__crust_recycle__ int *param, __crust_recycle__ int *param2);
__crust_t__ int * function(__crust_recycle__ int *param, __crust_recycle__ int *param2); // ERROR: there must be at most one __crust_recycle__ parameter
#define NULL ((void *)0)
void main() {
var = 5;
var = 5; // ERROR: this variable is unknown
}
......@@ -2,5 +2,5 @@ typedef __crust_t__ unsigned char *crust_t;
void function(crust_t param1) {
crust_t param4;
int retval = (param4 == NULL) ? 1 : 0;
int retval = (param4 == NULL) ? 1 : 0; // ERROR: conditional depends on an uninitialized variable
}
......@@ -3,5 +3,5 @@ typedef __crust_t__ unsigned char *crust_t;
int function(crust_t param1) {
crust_t param4 = NULL;
int retval = function(param4);
int retval = (param4 == NULL) ? 1 : 0;
int retval = (param4 == NULL) ? 1 : 0; // ERROR: condition depends on a freed variable
}
......@@ -2,5 +2,5 @@ typedef __crust_t__ int * crust_t;
int function(crust_t param1) {
crust_t param4 = param1;
crust_t param5 = param1;
crust_t param5 = param1; // ERROR: assigning a previously freed variable (due to assign in line 4)
}
......@@ -2,5 +2,5 @@ typedef __crust_t__ int * crust_t;
int function() {
crust_t param4;
crust_t param5 = param4;
crust_t param5 = param4; // ERROR: assigning an uninitialized variable
}
......@@ -2,5 +2,5 @@ typedef __crust_t__ int * crust_t;
int function() {
int * param4 = NULL;
crust_t param5 = param4;
crust_t param5 = param4; // ERROR: assigning non-crust variable to crust variable
}
......@@ -2,5 +2,5 @@ typedef __crust_t__ int * crust_t;
int function() {
crust_t param4 = NULL;
int * param5 = param4;
int * param5 = param4; // ERROR: assigning crust variable to non-crust variable
}
......@@ -3,5 +3,5 @@ typedef __crust_t__ unsigned char *crust_t;
int function(crust_t param3) {
int v = 5;
crust_t *param = (crust_t) v;
crust_t *param = (crust_t) v; // This is legal
}
......@@ -2,5 +2,5 @@ typedef __crust_t__ unsigned char *crust_t;
int function(crust_t param3) {
crust_t *param = 5;
crust_t *param = 5; // ERROR: assigning a constant (non-crust) to a crust variable
}
......@@ -2,5 +2,5 @@ typedef __crust_t__ unsigned char *crust_t;
crust_t function(crust_t param3) {
function(NULL);
function(NULL); // ERROR: the function returns a crust_t pointer, but it isn't stored
}
......@@ -2,5 +2,5 @@ typedef __crust_t__ unsigned char *crust_t;
int function(crust_t param3) {
crust_t param4;
function(param4);
function(param4); // ERROR: calling a function with an uninitialized parameter
}
......@@ -2,5 +2,5 @@ typedef __crust_t__ unsigned char *crust_t;
int function(crust_t param1, crust_t param2) {
crust_t param3 = param1;
param3 = param2;
param3 = param2; // ERROR: param3 is overwriten by another variable without being NULL
}
......@@ -3,5 +3,5 @@ typedef __crust_t__ unsigned char *crust_t;
int function(crust_t param3) {
function(param3);
function(param3);
function(param3); // ERROR: calling a function with a freed parameter
}
......@@ -3,5 +3,5 @@ typedef __crust_t__ unsigned char *crust_t;
int function(crust_t param3) {
int *param = 1;
function(param);
function(param); // ERROR: calling a function passing a non-crust value as a crust parameter
}
......@@ -3,5 +3,5 @@ typedef __crust_t__ unsigned char *crust_t;
int function(int param3) {
crust_t *param = NULL;
function(param);
function(param); // ERROR: passing a crust variable as a non-crust parameter
}
......@@ -2,5 +2,5 @@ typedef __crust_t__ unsigned char *crust_t;
int function(crust_t param3) {
function(2);
function(2); // ERROR: calling a function passing a constant (non-crust) value as a crust parameter
}
......@@ -3,5 +3,5 @@ typedef __crust_t__ unsigned char *crust_t;
void main() {
crust_t a,b;
a = b = 5;
a = b = 5; // ERROR: assigning a non-crust value to two crust variables
}
typedef __crust_t__ unsigned char *crust_t;
int function(crust_t *param3) {
crust_t param4;
int retval = function(&param4); // this is legal
}
......@@ -2,5 +2,5 @@ typedef __crust_t__ unsigned char *crust_t;
int function(crust_t param3) {
crust_t param4;
int retval = function(param4);
int retval = function(param4); // ERROR: param4 is used before being assigned
}
......@@ -3,5 +3,5 @@ typedef __crust_t__ unsigned char *crust_t;
int function(crust_t param3) {
int retval = function(param3);
int retval2 = function(param3);
int retval2 = function(param3); // ERROR: param3 was freed at line 5, when is passed to "function"
}
......@@ -3,5 +3,5 @@ typedef __crust_t__ unsigned char *crust_t;
int function(crust_t param3) {
int *param = 1;
int retval = function(param);
int retval = function(param); // ERROR: passed non-crust variable as a crust parameter
}
......@@ -3,5 +3,5 @@ typedef __crust_t__ unsigned char *crust_t;
crust_t function(int param3) {
crust_t *param = NULL;
crust_t retval = function(param);
crust_t retval = function(param); // ERROR: passed a crust variable to a non-crust param
}
......@@ -2,5 +2,5 @@ typedef __crust_t__ unsigned char *crust_t;
int function(crust_t param3) {
int retval = function(2);
int retval = function(2); // ERROR: passed a constant (non-crust) as a crust parameter
}
......@@ -2,5 +2,5 @@ typedef __crust_t__ unsigned char *crust_t;
int function(crust_t param3) {
int retval = function(3,5);
int retval = function(3,5); // ERROR: incorrect number of parameters
}
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