ANDROID: libsubcmd: Hoist iterator variable declarations in parse_options_subcommand()

When applying HOSTCFLAGS to the libsubcmd build to resolve a separate
issue, which contain '-std=gnu89' prior to commit 1e24078113ae ("Kbuild:
use -std=gnu11 for KBUILD_USERCFLAGS"), the following warning/error
occurs:

  parse-options.c:643:8: error: GCC does not allow variable declarations in for loop initializers before C99 [-Werror,-Wgcc-compat]
                  for (int i = 0; subcommands[i]; i++) {
                       ^
  parse-options.c:669:9: error: GCC does not allow variable declarations in for loop initializers before C99 [-Werror,-Wgcc-compat]
                          for (int i = 0; subcommands[i]; i++)
                               ^

This issue was never visible upstream, as commit 1e24078113ae ("Kbuild:
use -std=gnu11 for KBUILD_USERCFLAGS") was applied before
commit 13e07691a16f ("tools/resolve_btfids: Alter how HOSTCC is
forced"). Prior to the latter change, there was no '-std=' flag passed
to the libsubcmd build, so the default standard value was used, which
may be newer than C99.

To resolve this issue to unblock applying HOSTCFLAGS to libsubcmd, just
hoist the declarations out of the for loops.

Bug: 301145081
Link: https://github.com/ClangBuiltLinux/linux/issues/1929
Signed-off-by: Nathan Chancellor <nathan@kernel.org>
Change-Id: I41f17964f3d0822b026f6ae8f06a4d49bc7f15a9
(cherry picked from https://android-review.googlesource.com/q/commit:8f75dab19c30be65c740bd4abf9587714b3305dc)
This commit is contained in:
Nathan Chancellor 2023-09-11 09:44:05 -07:00 committed by Treehugger Robot
parent 1fed2de8e6
commit 7098f838f2

View File

@ -637,9 +637,11 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o
/* build usage string if it's not provided */
if (subcommands && !usagestr[0]) {
int i;
astrcatf(&buf, "%s %s [<options>] {", subcmd_config.exec_name, argv[0]);
for (int i = 0; subcommands[i]; i++) {
for (i = 0; subcommands[i]; i++) {
if (i)
astrcat(&buf, "|");
astrcat(&buf, subcommands[i]);
@ -665,7 +667,9 @@ int parse_options_subcommand(int argc, const char **argv, const struct option *o
exit(130);
case PARSE_OPT_LIST_SUBCMDS:
if (subcommands) {
for (int i = 0; subcommands[i]; i++)
int i;
for (i = 0; subcommands[i]; i++)
printf("%s ", subcommands[i]);
}
putchar('\n');