- new function for removing inline- and block comments from one ore more lines of c code

This commit is contained in:
2017-03-02 07:12:43 +00:00
parent ea0939d74d
commit 7586365595
2 changed files with 68 additions and 5 deletions

View File

@@ -23,6 +23,7 @@
#define CFILE_PARSER_MAX_REGEX_MATCHING_GROUPS 10 #define CFILE_PARSER_MAX_REGEX_MATCHING_GROUPS 10
STATIC bool cfile_parser_initialized = false; STATIC bool cfile_parser_initialized = false;
STATIC bool blockComment = false;
STATIC uint8_t cfile_parser_functionLine = 0; STATIC uint8_t cfile_parser_functionLine = 0;
STATIC cfunction_t *cfile_parser_function = NULL; STATIC cfunction_t *cfile_parser_function = NULL;
STATIC regex_t regX; STATIC regex_t regX;
@@ -167,6 +168,66 @@ STATIC int8_t matchFunctionStart(cfunction_t *aFunction, char *aString, size_t a
return 0; 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 * @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 * @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; bool functionEnd = false;
cfunction_type_t tempFunctionType = CFUNCTION_TYPE_UNDEF; cfunction_type_t tempFunctionType = CFUNCTION_TYPE_UNDEF;
cfunction_parameter_t *tempParameter = NULL; cfunction_parameter_t *tempParameter = NULL;
char *tempChar = NULL;
// remove basic inline comments if (0 != removeComments(aLine))
tempChar = strstr(aLine, "//");
if (NULL != tempChar)
{ {
strlcpy(tempChar, "\r\n", 3); return NULL;
} }
if (cfile_parser_functionLine) if (cfile_parser_functionLine)

View File

@@ -14,6 +14,11 @@
#ifndef STUBSER_CFILE_PARSER_LOC_H_ #ifndef STUBSER_CFILE_PARSER_LOC_H_
#define 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_BASE "^[[:blank:]]*([ _\\*[:alnum:]]* +\\**)([_\\*[:alnum:]]*)[[:blank:]]*\\([[:blank:]]*([^\r\\)]*)"
#define FUNCTION_SL FUNCTION_BASE "\\)[^;]*$" #define FUNCTION_SL FUNCTION_BASE "\\)[^;]*$"
#define FUNCTION_ML_SOF FUNCTION_BASE "[\r]*$" #define FUNCTION_ML_SOF FUNCTION_BASE "[\r]*$"