diff --git a/src/stubser/cfile_parser.c b/src/stubser/cfile_parser.c index 9815225..8c3af2d 100644 --- a/src/stubser/cfile_parser.c +++ b/src/stubser/cfile_parser.c @@ -23,6 +23,7 @@ #define CFILE_PARSER_MAX_REGEX_MATCHING_GROUPS 10 STATIC bool cfile_parser_initialized = false; +STATIC bool blockComment = false; STATIC uint8_t cfile_parser_functionLine = 0; STATIC cfunction_t *cfile_parser_function = NULL; STATIC regex_t regX; @@ -167,6 +168,66 @@ STATIC int8_t matchFunctionStart(cfunction_t *aFunction, char *aString, size_t a return 0; } +/*! + * @brief Remove comments from given line + * @param [in] *aLine terminated string + * @param [out] param1 paramdescription1 + * @retval 0 comments removed + * @retval 1 line within block comment (no c evaluation needed) + * @retval -1 NULL pointer detected + */ +int8_t removeComments(char *aLine) +{ + bool emptyLine = false; + char *comment = NULL; + char *commentEnd = NULL; + + if (NULL == aLine) + { + perror("Null pointer"); + return -1; + } + + // remove basic inline comments + comment = strstr(aLine, CPARS_COMMENT_INLINE); + if (NULL != comment) + { + strlcpy(comment, CPARS_LINE_ENDING, strlen(CPARS_LINE_ENDING) + 1); + comment = NULL; + } + + // remove block comments (also across multiple lines) + if (blockComment) + { + emptyLine = true; + comment = aLine; + } + else + { + comment = strstr(aLine, CPARS_COMMENT_BLOCK_START); + } + while (comment) + { + commentEnd = strstr(comment, CPARS_COMMENT_BLOCK_END); + if (NULL != commentEnd) + { + // copy character without block comment ending (strlen(CPARS_COMMENT_BLOCK_END)) + strlcpy(comment, commentEnd + strlen(CPARS_COMMENT_BLOCK_END), strlen(commentEnd + strlen(CPARS_COMMENT_BLOCK_END)) + 1); + comment = strstr(aLine, "/*"); + blockComment = false; + emptyLine = false; + } + else + { + strlcpy(comment, CPARS_LINE_ENDING, strlen(CPARS_LINE_ENDING) + 1); + blockComment = true; + comment = NULL; + } + } + + return (int8_t) emptyLine; +} + /*! * @brief Process a string (e.g. from getline()) if it is a single or multiple line function definition * @todo closing parenthesis within parameter list will break the detection @@ -181,13 +242,10 @@ STATIC cfunction_t* cfile_parser_evaluateLine(char *aLine) bool functionEnd = false; cfunction_type_t tempFunctionType = CFUNCTION_TYPE_UNDEF; cfunction_parameter_t *tempParameter = NULL; - char *tempChar = NULL; - // remove basic inline comments - tempChar = strstr(aLine, "//"); - if (NULL != tempChar) + if (0 != removeComments(aLine)) { - strlcpy(tempChar, "\r\n", 3); + return NULL; } if (cfile_parser_functionLine) diff --git a/src/stubser/cfile_parser_loc.h b/src/stubser/cfile_parser_loc.h index 775efbd..f49bed1 100644 --- a/src/stubser/cfile_parser_loc.h +++ b/src/stubser/cfile_parser_loc.h @@ -14,6 +14,11 @@ #ifndef STUBSER_CFILE_PARSER_LOC_H_ #define STUBSER_CFILE_PARSER_LOC_H_ +#define CPARS_LINE_ENDING "\r\n" +#define CPARS_COMMENT_INLINE "//" +#define CPARS_COMMENT_BLOCK_START "/*" +#define CPARS_COMMENT_BLOCK_END "*/" + #define FUNCTION_BASE "^[[:blank:]]*([ _\\*[:alnum:]]* +\\**)([_\\*[:alnum:]]*)[[:blank:]]*\\([[:blank:]]*([^\r\\)]*)" #define FUNCTION_SL FUNCTION_BASE "\\)[^;]*$" #define FUNCTION_ML_SOF FUNCTION_BASE "[\r]*$"