From d59fbfdbdcdc7d0b350b7d7abbad9c7c3dd1b9ab Mon Sep 17 00:00:00 2001 From: LuchianMihai Date: Mon, 26 Aug 2024 16:00:01 +0300 Subject: [PATCH] tools/nxstyle: fix wrong error reports for if statements * Fix nxstyle check so it does not report error on comments on the same line as if statements * Rework if statement checks * Report warning instead of error for is statements check fail --- tools/nxstyle.c | 26 ++++++++++++++++++++++---- 1 file changed, 22 insertions(+), 4 deletions(-) diff --git a/tools/nxstyle.c b/tools/nxstyle.c index bcf796203d..30947c12ea 100644 --- a/tools/nxstyle.c +++ b/tools/nxstyle.c @@ -1225,6 +1225,7 @@ int main(int argc, char **argv, char **envp) bswitch = false; /* True: Within a switch statement */ bstring = false; /* True: Within a string */ bexternc = false; /* True: Within 'extern "C"' */ + bif = false; /* True: This line is beginning of a 'if' statement */ ppline = PPLINE_NONE; /* > 0: The next line the continuation of a * pre-processor command */ rhcomment = 0; /* Indentation of Comment to the right of code @@ -1258,7 +1259,6 @@ int main(int argc, char **argv, char **envp) bstatm = false; /* True: This line is beginning of a * statement */ bfor = false; /* REVISIT: Implies for() is all on one line */ - bif = false; /* True: This line is beginning of a 'if' statement */ /* If we are not in a comment, then this certainly is not a right-hand * comment. @@ -2260,6 +2260,13 @@ int main(int argc, char **argv, char **envp) } } + /* Allow comments on the same line as the if statement */ + + if (bif == true) + { + bif = false; + } + n++; continue; } @@ -2630,9 +2637,11 @@ int main(int argc, char **argv, char **envp) ERROR("Space precedes right parenthesis", lineno, n); } - if (bif == true && pnest == 0 && line[n + 1] != '\n') + /* Unset bif if last parenthesis is closed */ + + if (bif == true && pnest == 0) { - ERROR("If statement followed by garbage", lineno, n); + bif = false; } } break; @@ -3105,7 +3114,16 @@ int main(int argc, char **argv, char **envp) if (m > 1 && isspace((int)line[m - 1]) && line[m - 1] != '\n' && line[m - 1] != '\r') { - ERROR("Dangling whitespace at the end of line", lineno, m); + /* Report warning on if statement only is pnest is 0 + * This takes into consideration the multiline if statement. + */ + + if (bif == true && pnest == 0) + { + WARN("If statement followed by garbage", lineno, n); + } + + ERROR("Dangling whitespace at the end of line", lineno, m); } /* The line width is determined by the location of the final