Skip to content

Commit d59fbfd

Browse files
tinnedkarmaxiaoxiang781216
authored andcommitted
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
1 parent b14dc8f commit d59fbfd

File tree

1 file changed

+22
-4
lines changed

1 file changed

+22
-4
lines changed

tools/nxstyle.c

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1225,6 +1225,7 @@ int main(int argc, char **argv, char **envp)
12251225
bswitch = false; /* True: Within a switch statement */
12261226
bstring = false; /* True: Within a string */
12271227
bexternc = false; /* True: Within 'extern "C"' */
1228+
bif = false; /* True: This line is beginning of a 'if' statement */
12281229
ppline = PPLINE_NONE; /* > 0: The next line the continuation of a
12291230
* pre-processor command */
12301231
rhcomment = 0; /* Indentation of Comment to the right of code
@@ -1258,7 +1259,6 @@ int main(int argc, char **argv, char **envp)
12581259
bstatm = false; /* True: This line is beginning of a
12591260
* statement */
12601261
bfor = false; /* REVISIT: Implies for() is all on one line */
1261-
bif = false; /* True: This line is beginning of a 'if' statement */
12621262

12631263
/* If we are not in a comment, then this certainly is not a right-hand
12641264
* comment.
@@ -2260,6 +2260,13 @@ int main(int argc, char **argv, char **envp)
22602260
}
22612261
}
22622262

2263+
/* Allow comments on the same line as the if statement */
2264+
2265+
if (bif == true)
2266+
{
2267+
bif = false;
2268+
}
2269+
22632270
n++;
22642271
continue;
22652272
}
@@ -2630,9 +2637,11 @@ int main(int argc, char **argv, char **envp)
26302637
ERROR("Space precedes right parenthesis", lineno, n);
26312638
}
26322639

2633-
if (bif == true && pnest == 0 && line[n + 1] != '\n')
2640+
/* Unset bif if last parenthesis is closed */
2641+
2642+
if (bif == true && pnest == 0)
26342643
{
2635-
ERROR("If statement followed by garbage", lineno, n);
2644+
bif = false;
26362645
}
26372646
}
26382647
break;
@@ -3105,7 +3114,16 @@ int main(int argc, char **argv, char **envp)
31053114
if (m > 1 && isspace((int)line[m - 1]) &&
31063115
line[m - 1] != '\n' && line[m - 1] != '\r')
31073116
{
3108-
ERROR("Dangling whitespace at the end of line", lineno, m);
3117+
/* Report warning on if statement only is pnest is 0
3118+
* This takes into consideration the multiline if statement.
3119+
*/
3120+
3121+
if (bif == true && pnest == 0)
3122+
{
3123+
WARN("If statement followed by garbage", lineno, n);
3124+
}
3125+
3126+
ERROR("Dangling whitespace at the end of line", lineno, m);
31093127
}
31103128

31113129
/* The line width is determined by the location of the final

0 commit comments

Comments
 (0)