Skip to content

Commit

Permalink
tools/nxstyle: fix wrong error reports for if statements
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
LuchianMihai authored and xiaoxiang781216 committed Aug 28, 2024
1 parent b14dc8f commit d59fbfd
Showing 1 changed file with 22 additions and 4 deletions.
26 changes: 22 additions & 4 deletions tools/nxstyle.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit d59fbfd

Please sign in to comment.