Commit 9971a8b2 authored by Thomas Jahns's avatar Thomas Jahns 🤸
Browse files

Put recurring code for regex compilation into function.

parent 5ba81982
...@@ -153,6 +153,9 @@ enum { ...@@ -153,6 +153,9 @@ enum {
static inline size_t static inline size_t
compress_whitespace(size_t len, char str[]); compress_whitespace(size_t len, char str[]);
static int
reCompile(regex_t *restrict RE, const char *restrict REstring,
char * restrict *restrict lineBuf, size_t * restrict lineBufSize);
static size_t static size_t
symRegexCompile(size_t numSyms, struct symbol symList[], symRegexCompile(size_t numSyms, struct symbol symList[],
char **line, size_t *lineBufSize); char **line, size_t *lineBufSize);
...@@ -215,37 +218,17 @@ static void fortran_interface(char *fname, char *fnameinc, char *fnameint) ...@@ -215,37 +218,17 @@ static void fortran_interface(char *fname, char *fnameinc, char *fnameint)
/* compile comment regular expression */ /* compile comment regular expression */
regex_t commentRE; regex_t commentRE;
{ {
int errcode; static const char commentREString[] = "^"WS"*/\\*"WS"*(.*"NWS")"WS"*\\*/";
char commentREString[] = "^"WS"*/\\*"WS"*(.*"NWS")"WS"*\\*/"; if (reCompile(&commentRE, commentREString, &line, &lineBufSize))
if ((errcode = regcomp(&commentRE, commentREString, REG_EXTENDED))) exit(EXIT_FAILURE);
{
line = realloc(line, lineBufSize = 1024);
if (line)
{
regerror(errcode, &commentRE, line, lineBufSize);
fprintf(stderr, "Error compiling regular expression: %s: %s\n",
commentREString, line);
exit(EXIT_FAILURE);
}
}
} }
/* compile documentation comment regular expression */ /* compile documentation comment regular expression */
regex_t docCommentRE; regex_t docCommentRE;
{ {
int errcode; static const char docCommentREString[] = "^"WS"*/\\*"WS"*"SYMRE":"
char docCommentREString[] = "^"WS"*/\\*"WS"*"SYMRE":"
WS"*("NWS".*"NWS")"WS"*\\*/"; WS"*("NWS".*"NWS")"WS"*\\*/";
if ((errcode = regcomp(&docCommentRE, docCommentREString, REG_EXTENDED))) if (reCompile(&docCommentRE, docCommentREString, &line, &lineBufSize))
{ exit(EXIT_FAILURE);
line = realloc(line, lineBufSize = 1024);
if (line)
{
regerror(errcode, &commentRE, line, lineBufSize);
fprintf(stderr, "Error compiling regular expression: %s: %s\n",
docCommentREString, line);
exit(EXIT_FAILURE);
}
}
} }
/* fortran include */ /* fortran include */
...@@ -774,26 +757,37 @@ symRegexCompile(size_t numSyms, struct symbol symList[], ...@@ -774,26 +757,37 @@ symRegexCompile(size_t numSyms, struct symbol symList[],
size_t maxMatch = 0; size_t maxMatch = 0;
for (size_t sym = 0; sym < numSyms; ++sym) for (size_t sym = 0; sym < numSyms; ++sym)
{ {
int errcode; if (reCompile(&symList[sym].preg, symList[sym].parseRE,
if ((errcode = regcomp(&symList[sym].preg, symList[sym].parseRE, line, lineBufSize))
REG_EXTENDED))) exit(EXIT_FAILURE);
{
if (*lineBufSize < REGEX_MAX_ERRSTRLEN)
line = realloc(line, *lineBufSize = REGEX_MAX_ERRSTRLEN);
if (line)
{
regerror(errcode, &symList[sym].preg, *line, *lineBufSize);
fprintf(stderr, "Error compiling regular expression: %s: %s\n",
symList[sym].parseRE, *line);
}
exit(EXIT_FAILURE);
}
if (symList[sym].nameMatch > maxMatch) if (symList[sym].nameMatch > maxMatch)
maxMatch = symList[sym].nameMatch; maxMatch = symList[sym].nameMatch;
} }
return maxMatch; return maxMatch;
} }
static int
reCompile(regex_t *restrict RE, const char *restrict REstring,
char * restrict *restrict lineBuf, size_t * restrict lineBufSize)
{
int errcode;
if ((errcode = regcomp(RE, REstring, REG_EXTENDED)))
{
char *restrict line;
size_t resize;
if (*lineBufSize < REGEX_MAX_ERRSTRLEN
&& (line = realloc(*lineBuf, resize = REGEX_MAX_ERRSTRLEN)))
{
*lineBuf = line;
*lineBufSize = resize;
regerror(errcode, RE, line, *lineBufSize);
fprintf(stderr, "Error compiling regular expression: %s: %s\n",
REstring, *lineBuf);
}
}
return errcode;
}
/* emit conversion code for MPI_Comm argument */ /* emit conversion code for MPI_Comm argument */
static int cfMPICommConvert(FILE *outfp, const char *argName, static int cfMPICommConvert(FILE *outfp, const char *argName,
size_t argNameLen, enum conversionType part) size_t argNameLen, enum conversionType part)
......
Supports Markdown
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