Skip to content

Commit

Permalink
tools/nxstyle: handle case statement
Browse files Browse the repository at this point in the history
fix nxstyle so it throw error if case statement is not on new line
  • Loading branch information
LuchianMihai authored and hartmannathan committed Feb 21, 2025
1 parent eadfb5a commit fd6d804
Showing 1 changed file with 43 additions and 18 deletions.
61 changes: 43 additions & 18 deletions tools/nxstyle.c
Original file line number Diff line number Diff line change
Expand Up @@ -1133,6 +1133,7 @@ int main(int argc, char **argv, char **envp)
bool bfor; /* True: This line is beginning of a 'for' statement */
bool bif; /* True: This line is beginning of a 'if' statement */
bool bswitch; /* True: Within a switch statement */
bool bcase; /* True: Within a case statement of a switch */
bool bstring; /* True: Within a string */
bool bquote; /* True: Backslash quoted character next */
bool bblank; /* Used to verify block comment terminator */
Expand Down Expand Up @@ -1269,6 +1270,7 @@ int main(int argc, char **argv, char **envp)
bcrs = false; /* True: Carriage return found on the line */
bfunctions = false; /* True: In private or public functions */
bswitch = false; /* True: Within a switch statement */
bcase = false; /* True: Within a case statement of a switch */
bstring = false; /* True: Within a string */
bexternc = false; /* True: Within 'extern "C"' */
bif = false; /* True: This line is beginning of a 'if' statement */
Expand Down Expand Up @@ -2004,22 +2006,11 @@ int main(int argc, char **argv, char **envp)
*/

else if (strncmp(&line[indent], "break ", 6) == 0 ||
strncmp(&line[indent], "case ", 5) == 0 ||
#if 0 /* Part of switch */
strncmp(&line[indent], "case ", 5) == 0 ||
#endif
strncmp(&line[indent], "continue ", 9) == 0 ||

#if 0 /* Part of switch */
strncmp(&line[indent], "default ", 8) == 0 ||
#endif
strncmp(&line[indent], "do ", 3) == 0 ||
strncmp(&line[indent], "else ", 5) == 0 ||
strncmp(&line[indent], "goto ", 5) == 0 ||
strncmp(&line[indent], "return ", 7) == 0 ||
#if 0 /* Doesn't follow pattern */
strncmp(&line[indent], "switch ", 7) == 0 ||
#endif
strncmp(&line[indent], "while ", 6) == 0)
{
bstatm = true;
Expand All @@ -2042,6 +2033,29 @@ int main(int argc, char **argv, char **envp)
{
bswitch = true;
}
else if (strncmp(&line[indent], "switch(", 7) == 0)
{
ERROR("Missing whitespace after keyword", lineno, n);
bswitch = true;
}
else if (strncmp(&line[indent], "case ", 5) == 0)
{
bcase = true;
}
else if (strncmp(&line[indent], "case(", 5) == 0)
{
ERROR("Missing whitespace after keyword", lineno, n);
bcase = true;
}
else if (strncmp(&line[indent], "default ", 8) == 0)
{
ERROR("Missing whitespace after keyword", lineno, n);
bcase = true;
}
else if (strncmp(&line[indent], "default:", 8) == 0)
{
bcase = true;
}

/* Also check for C keywords with missing white space */

Expand All @@ -2063,11 +2077,6 @@ int main(int argc, char **argv, char **envp)
bfor = true;
bstatm = true;
}
else if (strncmp(&line[indent], "switch(", 7) == 0)
{
ERROR("Missing whitespace after keyword", lineno, n);
bswitch = true;
}
}

/* STEP 3: Parse each character on the line */
Expand Down Expand Up @@ -2731,9 +2740,26 @@ int main(int argc, char **argv, char **envp)
}
}
break;
case ':':
{
if (bcase == true)
{
char *ndx = &line[n + 1];
while ((int)isspace(*ndx))
{
ndx++;
}

/* Semi-colon may terminate a declaration */
if (*ndx != '\0' && *ndx != '/')
{
ERROR("Case statement should be on a new line",
lineno, n);
}

bcase = false;
}
}
break;
case ',':
{
if (!isspace((int)line[n + 1]))
Expand Down Expand Up @@ -3072,7 +3098,6 @@ int main(int argc, char **argv, char **envp)
}

break;

case '^':

/* ^= */
Expand Down

0 comments on commit fd6d804

Please sign in to comment.