From 6b4f3f41379c7a56d8085dc2db1ff0d40aa63349 Mon Sep 17 00:00:00 2001 From: Martin Winkler Date: Mon, 13 Mar 2017 14:00:56 +0000 Subject: [PATCH] - stub variable specific initialization and check --- src/stubser/cfile_parser_loc.h | 2 +- src/stubser/stubser.c | 94 +++++++++++++++++++++++++++++++--- src/stubser/stubser_loc.h | 2 + 3 files changed, 89 insertions(+), 9 deletions(-) diff --git a/src/stubser/cfile_parser_loc.h b/src/stubser/cfile_parser_loc.h index 533b673..eb85ec9 100644 --- a/src/stubser/cfile_parser_loc.h +++ b/src/stubser/cfile_parser_loc.h @@ -25,7 +25,7 @@ #define CPARS_PREFIX_STATIC_S "static" #define CPARS_PREFIX_EXTERN_S "extern" -#define CPARS_EXPRESSION_BASE "^[[:blank:]]*([ _\\*[:alnum:]]* +\\**)([_\\*[:alnum:]]+)" +#define CPARS_EXPRESSION_BASE "^[[:blank:]]*([ _\\*[:alnum:]]* +\\**)([_\\*[:alnum:]\\[\\]]+)" #define FUNCTION_BASE CPARS_EXPRESSION_BASE "[[:blank:]]*\\([[:blank:]]*" #define CPARS_REGEX_FUNCTION FUNCTION_BASE "(.*)\\)[[:blank:]\\{\\};]+[[:blank:]]*" diff --git a/src/stubser/stubser.c b/src/stubser/stubser.c index dc8584a..384a545 100644 --- a/src/stubser/stubser.c +++ b/src/stubser/stubser.c @@ -38,6 +38,71 @@ STATIC bool isDatatypeVoid(char *aType) return true; } +STATIC bool isDatatypeStandard(cfile_variable_t *aVariable) +{ + regex_t regXvariable; + + if (0 > regcomp(®Xvariable, STUB_REGEX_STD_DATATYPE, (REG_EXTENDED))) + { + perror("Error regex\n"); + return false; + } + + if (0 == regexec(®Xvariable, aVariable->dataType, 0, NULL, 0)) + { + return true; + } + + return false; +} + +STATIC void createVariableSpecificInit(const char* aVariableTemplate, FILE *aFile, cfile_variable_t *aVariable) +{ + if (NULL != strstr(aVariable->dataType, "const")) + { + return; + } + + if (NULL != strstr(aVariable->dataType, "*")) + { + fprintf(aFile, aVariableTemplate, aVariable->name); + fprintf(aFile, " = NULL;" NEWLINES); + } + else if (isDatatypeStandard(aVariable)) + { + fprintf(aFile, aVariableTemplate, aVariable->name); + fprintf(aFile, " = 0x55;" NEWLINES); + } + else + { + fprintf(aFile, "\t(void) memset(&"); + fprintf(aFile, aVariableTemplate, aVariable->name); + fprintf(aFile, ",0x55,sizeof(%s)); /* %s */" NEWLINES, aVariable->name, aVariable->dataType); + } +} + +STATIC void createVariableSpecificCheck(FILE *aFile, cfile_variable_t *aVariable) +{ + if (NULL != strstr(aVariable->dataType, "const")) + { + return; + } + + if (NULL != strstr(aVariable->dataType, "*")) + { + fprintf(aFile, "\tCU_ASSERT_PTR_EQUAL(%s, "STUBVARIABLE_EXTENDED_S1");"NEWLINES, aVariable->name, aVariable->name); + } + else if (isDatatypeStandard(aVariable)) + { + fprintf(aFile, "\tCU_ASSERT_EQUAL(%s, "STUBVARIABLE_EXTENDED_S1");"NEWLINES, aVariable->name, aVariable->name); + } + else + { + fprintf(aFile, "\tCU_ASSERT_MEMORY_CHECK(\"%s\", &%s, &"STUBVARIABLE_EXTENDED_S1", sizeof(%s));"NEWLINES, aVariable->name, aVariable->name, aVariable->name, + aVariable->name); + } +} + STATIC int8_t createStubExpected(char *aNoSuffix, FILE *aFile, FILE *aHeader, cfile_variableList_t *aList) { cfile_variable_t *work = NULL; @@ -46,6 +111,11 @@ STATIC int8_t createStubExpected(char *aNoSuffix, FILE *aFile, FILE *aHeader, cf return -1; } + if (0 == aList->amount) + { + return 0; + } + work = aList->head; while (work) { @@ -70,8 +140,7 @@ STATIC int8_t createStubExpected(char *aNoSuffix, FILE *aFile, FILE *aHeader, cf work = work->next; continue; } - // TODO type specific initialization - fprintf(aFile, "\t"STUBVARIABLE_EXTENDED_S1" = 0x55;"NEWLINES, work->name); + createVariableSpecificInit("\t"STUBVARIABLE_EXTENDED_S1, aFile, work); work = work->next; } @@ -88,6 +157,11 @@ STATIC int8_t createStubGlobals(char *aNoSuffix, FILE *aFile, FILE *aHeader, cfi return -1; } + if (0 == aList->amount) + { + return 0; + } + work = aList->head; while (work) { @@ -111,8 +185,7 @@ STATIC int8_t createStubGlobals(char *aNoSuffix, FILE *aFile, FILE *aHeader, cfi work = work->next; continue; } - // TODO type specific initialization - fprintf(aFile, "\t%s = 0x55;"NEWLINES, work->name); + createVariableSpecificInit("\t%s", aFile, work); work = work->next; } @@ -135,6 +208,11 @@ STATIC int8_t createStubCheck(char *aNoSuffix, FILE *aFile, FILE *aHeader, cfile return -1; } + if (0 == aList->amount) + { + return 0; + } + fprintf(aHeader, "void "STUBFUNCTION_CHECK_S1"();"NEWLINES NEWLINES, aNoSuffix); fprintf(aFile, "void "STUBFUNCTION_CHECK_S1"()"NEWLINES"{"NEWLINES, aNoSuffix); @@ -146,8 +224,7 @@ STATIC int8_t createStubCheck(char *aNoSuffix, FILE *aFile, FILE *aHeader, cfile work = work->next; continue; } - // TODO type specific initialization - fprintf(aFile, "\tCU_ASSERT_EQUAL(%s, "STUBVARIABLE_EXTENDED_S1");"NEWLINES, work->name, work->name); + createVariableSpecificCheck(aFile, work); work = work->next; } @@ -208,7 +285,7 @@ STATIC int8_t createStubFunctionBlock(FILE *aFile, FILE *aHeader, cfunction_t *a aFunction->name); NEWLINE(aFile); - // stub function init header +// stub function init header fprintf(aHeader, "void init_%s(uint8_t "STUBINIT_PARAM_INSTANCE_S", uint8_t "STUBINIT_PARAM_CALLTEST_S, aFunction->name); if (!isDatatypeVoid(aFunction->dataType)) { @@ -338,7 +415,7 @@ STATIC void createUserHeader(char *aOutput, char *aNoSuffix) return; } - // user header doesn't exist +// user header doesn't exist cheader = fopen(cHeaderName, "w"); if (NULL == cheader) { @@ -403,6 +480,7 @@ STATIC int8_t createStub(char *aOutput, char *aNoSuffix, cfile_t *aCfile) fprintf(cfile, " * @details"NEWLINES" * This is a stub for CUnit.\\n"NEWLINES); fprintf(cfile, " * - generated by stubser -"NEWLINES" */" NEWLINES NEWLINES); fprintf(cfile, "#include "NEWLINES); + fprintf(cfile, "#include \"xasserts.h\""NEWLINES); fprintf(cfile, "#include \"xtypes.h\""NEWLINES); fprintf(cfile, "#include \"stub.h\""NEWLINES); fprintf(cfile, "#include \"stub_%s_user.h\""NEWLINES, aNoSuffix); diff --git a/src/stubser/stubser_loc.h b/src/stubser/stubser_loc.h index 2037700..81b3574 100644 --- a/src/stubser/stubser_loc.h +++ b/src/stubser/stubser_loc.h @@ -54,4 +54,6 @@ #define STUB_CONSOLE_PARSE_OK_S1 STUB_CONSOLE_GOBACK STUB_CONSOLE_OK " %s parsing..." #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_REGEX_STD_DATATYPE "u*int[0-9]{1,2}_t|uchar" #endif /* STUBSER_STUBSER_LOC_H_ */