diff --git a/src/stubser/stubser.c b/src/stubser/stubser.c index 00ff39b..5a52076 100644 --- a/src/stubser/stubser.c +++ b/src/stubser/stubser.c @@ -136,7 +136,7 @@ STATIC void createVariableSpecificCheck(FILE *aFile, cfile_variable_t *aVariable { char *tempChar = NULL; - if (NULL != strstr(aVariable->dataType, "const")) + if (NULL != strstr(aVariable->dataType, STUB_DATATYPE_CONST)) { return; } @@ -198,7 +198,9 @@ STATIC void createParameterSpecificCheck(FILE *aFile, cfunction_t *aFunction, cf fprintf(aFile, "\t"STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1" = (%s) "STUBDEFAULT_VALUE_POINTER_S";" NEWLINES, aFunction->name, aFunction->name, aParameterIndex, aParameter->dataType); } - else if (NULL != strstr(aParameter->dataType, "*") && NULL != strstr(aParameter->dataType, "char")) // TODO how to handle uchar (-> signed warning for strlen) + else if (NULL != strstr(aParameter->dataType, "*") // + && NULL != strstr(aParameter->dataType, "char") // handle char* as strings + && NULL == strstr(aParameter->dataType, "uchar")) // don't handle uchar (-> signed warning for strlen) { // char array with string comparison fprintf(aFile, "\tif (" // @@ -213,7 +215,7 @@ STATIC void createParameterSpecificCheck(FILE *aFile, cfunction_t *aFunction, cf fprintf(aFile, ", strlen("STUBINIT_PARAM_PARAMETER_S1"));"NEWLINES, aParameterIndex); fprintf(aFile, "\t}" NEWLINES); // injection only for non constant and non NULL parameter - if (NULL == strstr(aParameter->dataType, "const")) + if (NULL == strstr(aParameter->dataType, STUB_DATATYPE_CONST)) { fprintf(aFile, "\telse if (NULL != " STUBINIT_PARAM_PARAMETER_S1 ")" NEWLINES "\t{" NEWLINES, aParameterIndex); fprintf(aFile, "\t\tmemcpy(" STUBINIT_PARAM_PARAMETER_S1 ", "STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1", " // @@ -242,7 +244,7 @@ STATIC void createParameterSpecificCheck(FILE *aFile, cfunction_t *aFunction, cf fprintf(aFile, ");" NEWLINES); fprintf(aFile, "\t}" NEWLINES); // injection only for non constant and non NULL parameter - if (NULL == strstr(aParameter->dataType, "const")) + if (NULL == strstr(aParameter->dataType, STUB_DATATYPE_CONST)) { fprintf(aFile, "\telse if (NULL != " STUBINIT_PARAM_PARAMETER_S1 ")" NEWLINES "\t{" NEWLINES, aParameterIndex); fprintf(aFile, "\t\tmemcpy(" STUBINIT_PARAM_PARAMETER_S1 ", "STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1", " // @@ -251,8 +253,11 @@ STATIC void createParameterSpecificCheck(FILE *aFile, cfunction_t *aFunction, cf fprintf(aFile, ");" NEWLINES); fprintf(aFile, "\t}" NEWLINES); } - fprintf(aFile, "\t"STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1" = (%s) "STUBDEFAULT_VALUE_POINTER_S";" NEWLINES, aFunction->name, - aFunction->name, aParameterIndex, aParameter->dataType); + fprintf(aFile, "\t"STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1" = (%s%s) "STUBDEFAULT_VALUE_POINTER_S";" NEWLINES, // + aFunction->name, // STUBPARAMETER_STRUCT_S1 + aFunction->name, // STUBINSTANCE_S1 + aParameterIndex, // STUBPARAMETER_PARAM_S1, + aParameter->dataType, (NULL == strstr(aParameter->dataType, "*") ? "*" : "")); // cast array only parameter to be pointer fprintf(aFile, "\t"STUBPARAMETER_STRUCT_S1"["STUBINSTANCE_S1"]."STUBPARAMETER_PARAM_S1"_size = "STUBDEFAULT_VALUE_POINTERSIZE_S";" NEWLINES, aFunction->name, aFunction->name, aParameterIndex); } @@ -288,7 +293,7 @@ STATIC int8_t createStubExpected(char *aNoSuffix, FILE *aFile, FILE *aHeader, cf work = aList->head; while (work) { - if (!CVARIABLE_CHECK_TYPE(work->type, CVARIABLE_TYPE_REGULAR) || NULL != strstr(work->dataType, "const")) + if (!CVARIABLE_CHECK_TYPE(work->type, CVARIABLE_TYPE_REGULAR) || NULL != strstr(work->dataType, STUB_DATATYPE_CONST)) { work = work->next; continue; @@ -413,6 +418,7 @@ STATIC int8_t createStubFunctionBlock(FILE *aFile, FILE *aHeader, cfunction_t *a { cfunction_parameter_t *parameter = NULL; char parameterIndex = STUBDEFAULT_PARAMETER_INDEX_CHAR; + char *dataTypeTemp = NULL; bool pointerAvailable = false; bool variableParameterAvailable = false; @@ -469,39 +475,67 @@ STATIC int8_t createStubFunctionBlock(FILE *aFile, FILE *aHeader, cfunction_t *a } parameter = aFunction->parameter.head; parameterIndex = STUBDEFAULT_PARAMETER_INDEX_CHAR; - // TODO remove "const" from expected structure + while (parameter) { + char *constPos = NULL; + uint8_t constOffset = 0; + // skip parameter without dataType if (NULL == parameter->dataType) { parameter = parameter->next; continue; } + + (void) xmallocStrlcpy(&dataTypeTemp, parameter->dataType, strlen(parameter->dataType)); + + // remove trailing "const" for pointer and leading "const" for regular variables from expected structure + while (NULL != (constPos = strstr(dataTypeTemp + constOffset, STUB_DATATYPE_CONST))) + { + if (NULL == parameter->array && NULL == strstr(parameter->dataType, "*")) + { + // remove all const + memset(constPos, ' ', strlen(STUB_DATATYPE_CONST)); + } + else if (constPos == dataTypeTemp) + { + constOffset = strlen(STUB_DATATYPE_CONST); + } + else + { + // remove trailing const + memset(constPos, ' ', strlen(STUB_DATATYPE_CONST)); + } + } + xStringTrim(dataTypeTemp, strlen(dataTypeTemp)); + if (CPARAMETER_TYPE_FUNCPTR == parameter->type) { - fprintf(aFile, "\t%s (*"STUBPARAMETER_PARAM_S1")%s; /*!< @brief \"%s\" */" NEWLINES, NCHECK(parameter->dataType), parameterIndex++, + fprintf(aFile, "\t%s (*"STUBPARAMETER_PARAM_S1")%s; /*!< @brief \"%s\" */" NEWLINES, dataTypeTemp, parameterIndex++, NCHECK(parameter->array), NCHECK(parameter->name)); } else if (parameter->array) { - fprintf(aFile, "\t%s *"STUBPARAMETER_PARAM_S1"; /*!< @brief \"%s\" */" NEWLINES, NCHECK(parameter->dataType), parameterIndex, NCHECK(parameter->name)); + fprintf(aFile, "\t%s *"STUBPARAMETER_PARAM_S1"; /*!< @brief \"%s\" */" NEWLINES, dataTypeTemp, parameterIndex, NCHECK(parameter->name)); fprintf(aFile, "\tuint32_t "STUBPARAMETER_PARAM_S1"_size; /*!< @brief \"%s\" size to copy in case of parameter injection */" NEWLINES, parameterIndex++, NCHECK(parameter->name)); } - else if (NULL != strstr(parameter->dataType, "*")) + else if (NULL != strstr(dataTypeTemp, "*")) { - fprintf(aFile, "\t%s "STUBPARAMETER_PARAM_S1"; /*!< @brief \"%s\" */" NEWLINES, NCHECK(parameter->dataType), parameterIndex, NCHECK(parameter->name)); + fprintf(aFile, "\t%s "STUBPARAMETER_PARAM_S1"; /*!< @brief \"%s\" */" NEWLINES, dataTypeTemp, parameterIndex, NCHECK(parameter->name)); fprintf(aFile, "\tuint32_t "STUBPARAMETER_PARAM_S1"_size; /*!< @brief \"%s\" size to copy in case of parameter injection */" NEWLINES, parameterIndex++, NCHECK(parameter->name)); } else if (CPARAMETER_TYPE_REGULAR == parameter->type) { - fprintf(aFile, "\t%s "STUBPARAMETER_PARAM_S1"; /*!< @brief \"%s\" */" NEWLINES, NCHECK(parameter->dataType), parameterIndex++, + fprintf(aFile, "\t%s "STUBPARAMETER_PARAM_S1"; /*!< @brief \"%s\" */" NEWLINES, dataTypeTemp, parameterIndex++, NCHECK(parameter->name)); } + parameter = parameter->next; } + xfree((void**) &dataTypeTemp); fprintf(aFile, "} "STUBPARAMETER_TYPEDEF_S1";" NEWLINES, aFunction->name); NEWLINE(aFile); @@ -530,10 +564,12 @@ STATIC int8_t createStubFunctionBlock(FILE *aFile, FILE *aHeader, cfunction_t *a continue; } // initialize _size parameter to 1 (default to check addresses) - if (CPARAMETER_TYPE_FUNCPTR != parameter->type && (parameter->array || NULL != strstr(parameter->dataType, "*"))) // TODO cast array parameter to be pointer + if (CPARAMETER_TYPE_FUNCPTR != parameter->type && (parameter->array || NULL != strstr(parameter->dataType, "*"))) { - fprintf(aFile, "\t[0 ... %u]."STUBPARAMETER_PARAM_S1" = (%s) "STUBDEFAULT_VALUE_POINTER_S"," NEWLINES, STUB_INSTANCES_AMOUNT - 1, parameterIndex, - parameter->dataType); + fprintf(aFile, "\t[0 ... %u]."STUBPARAMETER_PARAM_S1" = (%s%s) "STUBDEFAULT_VALUE_POINTER_S"," NEWLINES, // + STUB_INSTANCES_AMOUNT - 1, parameterIndex, //STUBDEFAULT_VALUE_POINTER_S + parameter->dataType, // + (NULL == strstr(parameter->dataType, "*") ? "*" : "")); // cast array only parameter to be pointer fprintf(aFile, "\t[0 ... %u]."STUBPARAMETER_PARAM_S1"_size = 1," NEWLINES, STUB_INSTANCES_AMOUNT - 1, parameterIndex); } else @@ -741,8 +777,10 @@ STATIC int8_t createStubFunctionBlock(FILE *aFile, FILE *aHeader, cfunction_t *a // initialize _size parameter to 1 (default to check addresses) if (CPARAMETER_TYPE_FUNCPTR != parameter->type && (parameter->array || NULL != strstr(parameter->dataType, "*"))) { - fprintf(aFile, "\t\t"STUBPARAMETER_STRUCT_S1"["STUBINIT_PARAM_INSTANCE_S"]."STUBPARAMETER_PARAM_S1" = (%s) "STUBDEFAULT_VALUE_POINTER_S";" NEWLINES, - aFunction->name, parameterIndex, parameter->dataType); + fprintf(aFile, "\t\t"STUBPARAMETER_STRUCT_S1"["STUBINIT_PARAM_INSTANCE_S"]."STUBPARAMETER_PARAM_S1" = (%s%s) "STUBDEFAULT_VALUE_POINTER_S";" NEWLINES, + aFunction->name, parameterIndex, // + parameter->dataType, + (NULL == strstr(parameter->dataType, "*") ? "*" : "")); // cast array only parameter to be pointer fprintf(aFile, "\t\t"STUBPARAMETER_STRUCT_S1"["STUBINIT_PARAM_INSTANCE_S"]."STUBPARAMETER_PARAM_S1"_size = "STUBDEFAULT_VALUE_POINTERSIZE_S";" NEWLINES, aFunction->name, parameterIndex); @@ -762,7 +800,7 @@ STATIC int8_t createStubFunctionBlock(FILE *aFile, FILE *aHeader, cfunction_t *a NEWLINE(aFile); // stub function - fprintf(aFile, "%s %s %s(", NCHECK(aFunction->prefix), NCHECK(aFunction->dataType), NCHECK(aFunction->name)); // todo remove leading " " when prefix is missing + fprintf(aFile, "%s%s%s %s(", NCHECK(aFunction->prefix), (NULL == aFunction->prefix ? "" : " "), NCHECK(aFunction->dataType), NCHECK(aFunction->name)); parameter = aFunction->parameter.head; parameterIndex = STUBDEFAULT_PARAMETER_INDEX_CHAR; while (parameter) diff --git a/src/stubser/stubser_loc.h b/src/stubser/stubser_loc.h index 9b8cae5..36a5fbe 100644 --- a/src/stubser/stubser_loc.h +++ b/src/stubser/stubser_loc.h @@ -71,5 +71,6 @@ #define STUB_CONSOLE_CREATE_ERROR_S2 "...stub creation failed\n"STUB_CONSOLE_FAIL " %s creation failed (%d)\n" #define STUB_CONSOLE_CREATE_OK_S1 "...stub created > %s\n" +#define STUB_DATATYPE_CONST "const" #define STUB_REGEX_STD_DATATYPE "(^|[^_[:alnum:]])([ui]+nt[0-9]{1,2}_t|char|uchar|word[12346]{2}|int[12346]{0,2}|byte)([^_[:alnum:]]|$)" #endif /* STUBSER_STUBSER_LOC_H_ */