From 828a786995aac5fdf0eb468736dc28571edfbbdc Mon Sep 17 00:00:00 2001 From: raiden00pl Date: Mon, 27 Jul 2026 12:20:13 +0200 Subject: [PATCH] tools/nxstyle: let an alternative branch hold statements before its braces Alternatives selected by conditional compilation share the braces that follow, and a branch may hold statements before reaching its condition. Signed-off-by: raiden00pl Assisted-by: Claude Code --- tools/nxstyle.c | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/tools/nxstyle.c b/tools/nxstyle.c index 4c58c8bedde..1f9a8e80786 100644 --- a/tools/nxstyle.c +++ b/tools/nxstyle.c @@ -1470,6 +1470,7 @@ int main(int argc, char **argv, char **envp) bool bstmtstart; /* True: A new statement begins on this line */ bool bprevstmtend; /* True: The preceding line of code ended a statement */ bool bctrlline; /* True: A control statement starts on this line */ + bool bppalt; /* True: In an alternative branch awaiting braces */ char lastcode; /* Last code character seen on this line */ char prevlastcode; /* Last code character on the preceding line */ int prevcodeindent; /* Indentation of the preceding line of code */ @@ -1601,6 +1602,7 @@ int main(int argc, char **argv, char **envp) bstmtstart = true; /* True: A statement begins on this line */ bprevstmtend = true; /* True: The preceding code ended a statement */ bctrlline = false; /* True: A control statement starts here */ + bppalt = false; /* True: Alternative branch awaiting braces */ lastcode = '\0'; /* Last code character seen on this line */ prevlastcode = '\0'; /* Last code character on the preceding line */ prevcodeindent = 0; /* Indentation of the preceding line of code */ @@ -1984,6 +1986,17 @@ int main(int argc, char **argv, char **envp) bppline = true; + /* Alternative branches share the braces that close the construct, + * and may hold statements of their own before reaching them. + */ + + if (brace_kw != NULL && + (strncmp(&line[indent], "#else", 5) == 0 || + strncmp(&line[indent], "#elif", 5) == 0)) + { + bppalt = true; + } + /* Suppress error for comment following conditional compilation */ noblank_lineno = lineno; @@ -3767,14 +3780,20 @@ int main(int argc, char **argv, char **envp) * an 'else if', or alternatives sharing the braces that follow. */ - else if (!bctrlline) + else if (!bctrlline && !bppalt) { snprintf(buffer, sizeof(buffer), "Missing braces after '%s'", brace_kw); ERROR(buffer, lineno, indent); } - brace_kw = NULL; + /* An alternative branch may hold statements first */ + + if (line[indent] == '{' || bctrlline || !bppalt) + { + brace_kw = NULL; + bppalt = false; + } } /* Has the header ended on this line? If so, see what follows */