Use for-each more in gdb

There are some loops in gdb that use ARRAY_SIZE (or a wordier
equivalent) to loop over a static array.  This patch changes some of
these to use foreach instead.

Regression tested on x86-64 Fedora 34.
This commit is contained in:
Tom Tromey
2021-12-03 14:45:37 -07:00
parent 621f8c42d3
commit 696d6f4d5c
10 changed files with 106 additions and 122 deletions

View File

@ -12202,7 +12202,6 @@ static std::string
ada_exception_catchpoint_cond_string (const char *excep_string, ada_exception_catchpoint_cond_string (const char *excep_string,
enum ada_exception_catchpoint_kind ex) enum ada_exception_catchpoint_kind ex)
{ {
int i;
bool is_standard_exc = false; bool is_standard_exc = false;
std::string result; std::string result;
@ -12235,9 +12234,9 @@ ada_exception_catchpoint_cond_string (const char *excep_string,
breakpoint condition is to use its fully-qualified named: breakpoint condition is to use its fully-qualified named:
e.g. my_package.constraint_error. */ e.g. my_package.constraint_error. */
for (i = 0; i < sizeof (standard_exc) / sizeof (char *); i++) for (const char *name : standard_exc)
{ {
if (strcmp (standard_exc [i], excep_string) == 0) if (strcmp (name, excep_string) == 0)
{ {
is_standard_exc = true; is_standard_exc = true;
break; break;
@ -12467,13 +12466,11 @@ ada_is_exception_sym (struct symbol *sym)
static int static int
ada_is_non_standard_exception_sym (struct symbol *sym) ada_is_non_standard_exception_sym (struct symbol *sym)
{ {
int i;
if (!ada_is_exception_sym (sym)) if (!ada_is_exception_sym (sym))
return 0; return 0;
for (i = 0; i < ARRAY_SIZE (standard_exc); i++) for (const char *name : standard_exc)
if (strcmp (sym->linkage_name (), standard_exc[i]) == 0) if (strcmp (sym->linkage_name (), name) == 0)
return 0; /* A standard exception. */ return 0; /* A standard exception. */
/* Numeric_Error is also a standard exception, so exclude it. /* Numeric_Error is also a standard exception, so exclude it.
@ -12538,20 +12535,17 @@ static void
ada_add_standard_exceptions (compiled_regex *preg, ada_add_standard_exceptions (compiled_regex *preg,
std::vector<ada_exc_info> *exceptions) std::vector<ada_exc_info> *exceptions)
{ {
int i; for (const char *name : standard_exc)
for (i = 0; i < ARRAY_SIZE (standard_exc); i++)
{ {
if (preg == NULL if (preg == NULL || preg->exec (name, 0, NULL, 0) == 0)
|| preg->exec (standard_exc[i], 0, NULL, 0) == 0)
{ {
struct bound_minimal_symbol msymbol struct bound_minimal_symbol msymbol
= ada_lookup_simple_minsym (standard_exc[i]); = ada_lookup_simple_minsym (name);
if (msymbol.minsym != NULL) if (msymbol.minsym != NULL)
{ {
struct ada_exc_info info struct ada_exc_info info
= {standard_exc[i], BMSYMBOL_VALUE_ADDRESS (msymbol)}; = {name, BMSYMBOL_VALUE_ADDRESS (msymbol)};
exceptions->push_back (info); exceptions->push_back (info);
} }

View File

@ -68,7 +68,7 @@ bcache::expand_hash_table ()
so we roughly double the table size each time. After we fall off so we roughly double the table size each time. After we fall off
the end of this table, we just double. Don't laugh --- there have the end of this table, we just double. Don't laugh --- there have
been executables sighted with a gigabyte of debug info. */ been executables sighted with a gigabyte of debug info. */
static unsigned long sizes[] = { static const unsigned long sizes[] = {
1021, 2053, 4099, 8191, 16381, 32771, 1021, 2053, 4099, 8191, 16381, 32771,
65537, 131071, 262144, 524287, 1048573, 2097143, 65537, 131071, 262144, 524287, 1048573, 2097143,
4194301, 8388617, 16777213, 33554467, 67108859, 134217757, 4194301, 8388617, 16777213, 33554467, 67108859, 134217757,
@ -85,10 +85,10 @@ bcache::expand_hash_table ()
/* Find the next size. */ /* Find the next size. */
new_num_buckets = m_num_buckets * 2; new_num_buckets = m_num_buckets * 2;
for (i = 0; i < (sizeof (sizes) / sizeof (sizes[0])); i++) for (unsigned long a_size : sizes)
if (sizes[i] > m_num_buckets) if (a_size > m_num_buckets)
{ {
new_num_buckets = sizes[i]; new_num_buckets = a_size;
break; break;
} }

View File

@ -2644,7 +2644,6 @@ lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
{ {
int c; int c;
int namelen; int namelen;
unsigned int i;
const char *tokstart; const char *tokstart;
bool saw_structop = last_was_structop; bool saw_structop = last_was_structop;
@ -2667,33 +2666,33 @@ lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
tokstart = pstate->lexptr; tokstart = pstate->lexptr;
/* See if it is a special token of length 3. */ /* See if it is a special token of length 3. */
for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++) for (const auto &token : tokentab3)
if (strncmp (tokstart, tokentab3[i].oper, 3) == 0) if (strncmp (tokstart, token.oper, 3) == 0)
{ {
if ((tokentab3[i].flags & FLAG_CXX) != 0 if ((token.flags & FLAG_CXX) != 0
&& par_state->language ()->la_language != language_cplus) && par_state->language ()->la_language != language_cplus)
break; break;
gdb_assert ((tokentab3[i].flags & FLAG_C) == 0); gdb_assert ((token.flags & FLAG_C) == 0);
pstate->lexptr += 3; pstate->lexptr += 3;
yylval.opcode = tokentab3[i].opcode; yylval.opcode = token.opcode;
return tokentab3[i].token; return token.token;
} }
/* See if it is a special token of length 2. */ /* See if it is a special token of length 2. */
for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++) for (const auto &token : tokentab2)
if (strncmp (tokstart, tokentab2[i].oper, 2) == 0) if (strncmp (tokstart, token.oper, 2) == 0)
{ {
if ((tokentab2[i].flags & FLAG_CXX) != 0 if ((token.flags & FLAG_CXX) != 0
&& par_state->language ()->la_language != language_cplus) && par_state->language ()->la_language != language_cplus)
break; break;
gdb_assert ((tokentab2[i].flags & FLAG_C) == 0); gdb_assert ((token.flags & FLAG_C) == 0);
pstate->lexptr += 2; pstate->lexptr += 2;
yylval.opcode = tokentab2[i].opcode; yylval.opcode = token.opcode;
if (tokentab2[i].token == ARROW) if (token.token == ARROW)
last_was_structop = 1; last_was_structop = 1;
return tokentab2[i].token; return token.token;
} }
switch (c = *tokstart) switch (c = *tokstart)
@ -2979,18 +2978,18 @@ lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
/* Catch specific keywords. */ /* Catch specific keywords. */
std::string copy = copy_name (yylval.sval); std::string copy = copy_name (yylval.sval);
for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++) for (const auto &token : ident_tokens)
if (copy == ident_tokens[i].oper) if (copy == token.oper)
{ {
if ((ident_tokens[i].flags & FLAG_CXX) != 0 if ((token.flags & FLAG_CXX) != 0
&& par_state->language ()->la_language != language_cplus) && par_state->language ()->la_language != language_cplus)
break; break;
if ((ident_tokens[i].flags & FLAG_C) != 0 if ((token.flags & FLAG_C) != 0
&& par_state->language ()->la_language != language_c && par_state->language ()->la_language != language_c
&& par_state->language ()->la_language != language_objc) && par_state->language ()->la_language != language_objc)
break; break;
if ((ident_tokens[i].flags & FLAG_SHADOW) != 0) if ((token.flags & FLAG_SHADOW) != 0)
{ {
struct field_of_this_result is_a_field_of_this; struct field_of_this_result is_a_field_of_this;
@ -3009,8 +3008,8 @@ lex_one_token (struct parser_state *par_state, bool *is_quoted_name)
/* It is ok to always set this, even though we don't always /* It is ok to always set this, even though we don't always
strictly need to. */ strictly need to. */
yylval.opcode = ident_tokens[i].opcode; yylval.opcode = token.opcode;
return ident_tokens[i].token; return token.token;
} }
if (*tokstart == '$') if (*tokstart == '$')

View File

@ -147,9 +147,9 @@ inspect_type (struct demangle_parse_info *info,
name[ret_comp->u.s_name.len] = '\0'; name[ret_comp->u.s_name.len] = '\0';
/* Ignore any typedefs that should not be substituted. */ /* Ignore any typedefs that should not be substituted. */
for (int i = 0; i < ARRAY_SIZE (ignore_typedefs); ++i) for (const char *ignorable : ignore_typedefs)
{ {
if (strcmp (name, ignore_typedefs[i]) == 0) if (strcmp (name, ignorable) == 0)
return 0; return 0;
} }

View File

@ -1027,7 +1027,6 @@ lex_one_token (struct parser_state *par_state)
{ {
int c; int c;
int namelen; int namelen;
unsigned int i;
const char *tokstart; const char *tokstart;
int saw_structop = last_was_structop; int saw_structop = last_was_structop;
@ -1039,21 +1038,21 @@ lex_one_token (struct parser_state *par_state)
tokstart = pstate->lexptr; tokstart = pstate->lexptr;
/* See if it is a special token of length 3. */ /* See if it is a special token of length 3. */
for (i = 0; i < sizeof tokentab3 / sizeof tokentab3[0]; i++) for (const auto &token : tokentab3)
if (strncmp (tokstart, tokentab3[i].oper, 3) == 0) if (strncmp (tokstart, token.oper, 3) == 0)
{ {
pstate->lexptr += 3; pstate->lexptr += 3;
yylval.opcode = tokentab3[i].opcode; yylval.opcode = token.opcode;
return tokentab3[i].token; return token.token;
} }
/* See if it is a special token of length 2. */ /* See if it is a special token of length 2. */
for (i = 0; i < sizeof tokentab2 / sizeof tokentab2[0]; i++) for (const auto &token : tokentab2)
if (strncmp (tokstart, tokentab2[i].oper, 2) == 0) if (strncmp (tokstart, token.oper, 2) == 0)
{ {
pstate->lexptr += 2; pstate->lexptr += 2;
yylval.opcode = tokentab2[i].opcode; yylval.opcode = token.opcode;
return tokentab2[i].token; return token.token;
} }
switch (c = *tokstart) switch (c = *tokstart)
@ -1274,13 +1273,13 @@ lex_one_token (struct parser_state *par_state)
/* Catch specific keywords. */ /* Catch specific keywords. */
std::string copy = copy_name (yylval.sval); std::string copy = copy_name (yylval.sval);
for (i = 0; i < sizeof ident_tokens / sizeof ident_tokens[0]; i++) for (const auto &token : ident_tokens)
if (copy == ident_tokens[i].oper) if (copy == token.oper)
{ {
/* It is ok to always set this, even though we don't always /* It is ok to always set this, even though we don't always
strictly need to. */ strictly need to. */
yylval.opcode = ident_tokens[i].opcode; yylval.opcode = token.opcode;
return ident_tokens[i].token; return token.token;
} }
if (*tokstart == '$') if (*tokstart == '$')

View File

@ -1269,27 +1269,27 @@ yylex (void)
if (*pstate->lexptr == '.') if (*pstate->lexptr == '.')
{ {
for (int i = 0; i < ARRAY_SIZE (boolean_values); i++) for (const auto &candidate : boolean_values)
{ {
if (strncasecmp (tokstart, boolean_values[i].name, if (strncasecmp (tokstart, candidate.name,
strlen (boolean_values[i].name)) == 0) strlen (candidate.name)) == 0)
{ {
pstate->lexptr += strlen (boolean_values[i].name); pstate->lexptr += strlen (candidate.name);
yylval.lval = boolean_values[i].value; yylval.lval = candidate.value;
return BOOLEAN_LITERAL; return BOOLEAN_LITERAL;
} }
} }
} }
/* See if it is a Fortran operator. */ /* See if it is a Fortran operator. */
for (int i = 0; i < ARRAY_SIZE (fortran_operators); i++) for (const auto &candidate : fortran_operators)
if (strncasecmp (tokstart, fortran_operators[i].oper, if (strncasecmp (tokstart, candidate.oper,
strlen (fortran_operators[i].oper)) == 0) strlen (candidate.oper)) == 0)
{ {
gdb_assert (!fortran_operators[i].case_sensitive); gdb_assert (!candidate.case_sensitive);
pstate->lexptr += strlen (fortran_operators[i].oper); pstate->lexptr += strlen (candidate.oper);
yylval.opcode = fortran_operators[i].opcode; yylval.opcode = candidate.opcode;
return fortran_operators[i].token; return candidate.token;
} }
switch (c = *tokstart) switch (c = *tokstart)
@ -1452,15 +1452,15 @@ yylex (void)
/* Catch specific keywords. */ /* Catch specific keywords. */
for (int i = 0; i < ARRAY_SIZE (f77_keywords); i++) for (const auto &keyword : f77_keywords)
if (strlen (f77_keywords[i].oper) == namelen if (strlen (keyword.oper) == namelen
&& ((!f77_keywords[i].case_sensitive && ((!keyword.case_sensitive
&& strncasecmp (tokstart, f77_keywords[i].oper, namelen) == 0) && strncasecmp (tokstart, keyword.oper, namelen) == 0)
|| (f77_keywords[i].case_sensitive || (keyword.case_sensitive
&& strncmp (tokstart, f77_keywords[i].oper, namelen) == 0))) && strncmp (tokstart, keyword.oper, namelen) == 0)))
{ {
yylval.opcode = f77_keywords[i].opcode; yylval.opcode = keyword.opcode;
return f77_keywords[i].token; return keyword.token;
} }
yylval.sval.ptr = tokstart; yylval.sval.ptr = tokstart;
@ -1475,7 +1475,7 @@ yylex (void)
{ {
std::string tmp = copy_name (yylval.sval); std::string tmp = copy_name (yylval.sval);
struct block_symbol result; struct block_symbol result;
enum domain_enum_tag lookup_domains[] = const enum domain_enum_tag lookup_domains[] =
{ {
STRUCT_DOMAIN, STRUCT_DOMAIN,
VAR_DOMAIN, VAR_DOMAIN,
@ -1483,10 +1483,10 @@ yylex (void)
}; };
int hextype; int hextype;
for (int i = 0; i < ARRAY_SIZE (lookup_domains); ++i) for (const auto &domain : lookup_domains)
{ {
result = lookup_symbol (tmp.c_str (), pstate->expression_context_block, result = lookup_symbol (tmp.c_str (), pstate->expression_context_block,
lookup_domains[i], NULL); domain, NULL);
if (result.symbol && SYMBOL_CLASS (result.symbol) == LOC_TYPEDEF) if (result.symbol && SYMBOL_CLASS (result.symbol) == LOC_TYPEDEF)
{ {
yylval.tsym.type = SYMBOL_TYPE (result.symbol); yylval.tsym.type = SYMBOL_TYPE (result.symbol);

View File

@ -1018,7 +1018,6 @@ lex_one_token (struct parser_state *par_state)
{ {
int c; int c;
int namelen; int namelen;
unsigned int i;
const char *tokstart; const char *tokstart;
int saw_structop = last_was_structop; int saw_structop = last_was_structop;
@ -1030,23 +1029,23 @@ lex_one_token (struct parser_state *par_state)
tokstart = par_state->lexptr; tokstart = par_state->lexptr;
/* See if it is a special token of length 3. */ /* See if it is a special token of length 3. */
for (i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++) for (const auto &token : tokentab3)
if (strncmp (tokstart, tokentab3[i].oper, 3) == 0) if (strncmp (tokstart, token.oper, 3) == 0)
{ {
par_state->lexptr += 3; par_state->lexptr += 3;
yylval.opcode = tokentab3[i].opcode; yylval.opcode = token.opcode;
return tokentab3[i].token; return token.token;
} }
/* See if it is a special token of length 2. */ /* See if it is a special token of length 2. */
for (i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++) for (const auto &token : tokentab2)
if (strncmp (tokstart, tokentab2[i].oper, 2) == 0) if (strncmp (tokstart, token.oper, 2) == 0)
{ {
par_state->lexptr += 2; par_state->lexptr += 2;
yylval.opcode = tokentab2[i].opcode; yylval.opcode = token.opcode;
/* NOTE: -> doesn't exist in Go, so we don't need to watch for /* NOTE: -> doesn't exist in Go, so we don't need to watch for
setting last_was_structop here. */ setting last_was_structop here. */
return tokentab2[i].token; return token.token;
} }
switch (c = *tokstart) switch (c = *tokstart)
@ -1270,13 +1269,13 @@ lex_one_token (struct parser_state *par_state)
/* Catch specific keywords. */ /* Catch specific keywords. */
std::string copy = copy_name (yylval.sval); std::string copy = copy_name (yylval.sval);
for (i = 0; i < sizeof (ident_tokens) / sizeof (ident_tokens[0]); i++) for (const auto &token : ident_tokens)
if (copy == ident_tokens[i].oper) if (copy == token.oper)
{ {
/* It is ok to always set this, even though we don't always /* It is ok to always set this, even though we don't always
strictly need to. */ strictly need to. */
yylval.opcode = ident_tokens[i].opcode; yylval.opcode = token.opcode;
return ident_tokens[i].token; return token.token;
} }
if (*tokstart == '$') if (*tokstart == '$')

View File

@ -1107,28 +1107,28 @@ yylex (void)
/* See if it is a special token of length 3. */ /* See if it is a special token of length 3. */
if (explen > 2) if (explen > 2)
for (int i = 0; i < sizeof (tokentab3) / sizeof (tokentab3[0]); i++) for (const auto &token : tokentab3)
if (strncasecmp (tokstart, tokentab3[i].oper, 3) == 0 if (strncasecmp (tokstart, token.oper, 3) == 0
&& (!isalpha (tokentab3[i].oper[0]) || explen == 3 && (!isalpha (token.oper[0]) || explen == 3
|| (!isalpha (tokstart[3]) || (!isalpha (tokstart[3])
&& !isdigit (tokstart[3]) && tokstart[3] != '_'))) && !isdigit (tokstart[3]) && tokstart[3] != '_')))
{ {
pstate->lexptr += 3; pstate->lexptr += 3;
yylval.opcode = tokentab3[i].opcode; yylval.opcode = token.opcode;
return tokentab3[i].token; return token.token;
} }
/* See if it is a special token of length 2. */ /* See if it is a special token of length 2. */
if (explen > 1) if (explen > 1)
for (int i = 0; i < sizeof (tokentab2) / sizeof (tokentab2[0]); i++) for (const auto &token : tokentab2)
if (strncasecmp (tokstart, tokentab2[i].oper, 2) == 0 if (strncasecmp (tokstart, token.oper, 2) == 0
&& (!isalpha (tokentab2[i].oper[0]) || explen == 2 && (!isalpha (token.oper[0]) || explen == 2
|| (!isalpha (tokstart[2]) || (!isalpha (tokstart[2])
&& !isdigit (tokstart[2]) && tokstart[2] != '_'))) && !isdigit (tokstart[2]) && tokstart[2] != '_')))
{ {
pstate->lexptr += 2; pstate->lexptr += 2;
yylval.opcode = tokentab2[i].opcode; yylval.opcode = token.opcode;
return tokentab2[i].token; return token.token;
} }
switch (c = *tokstart) switch (c = *tokstart)

View File

@ -756,7 +756,6 @@ rust_parser::lex_identifier ()
{ {
unsigned int length; unsigned int length;
const struct token_info *token; const struct token_info *token;
int i;
int is_gdb_var = pstate->lexptr[0] == '$'; int is_gdb_var = pstate->lexptr[0] == '$';
bool is_raw = false; bool is_raw = false;
@ -787,12 +786,12 @@ rust_parser::lex_identifier ()
token = NULL; token = NULL;
if (!is_raw) if (!is_raw)
{ {
for (i = 0; i < ARRAY_SIZE (identifier_tokens); ++i) for (const auto &candidate : identifier_tokens)
{ {
if (length == strlen (identifier_tokens[i].name) if (length == strlen (candidate.name)
&& strncmp (identifier_tokens[i].name, start, length) == 0) && strncmp (candidate.name, start, length) == 0)
{ {
token = &identifier_tokens[i]; token = &candidate;
break; break;
} }
} }
@ -845,15 +844,14 @@ int
rust_parser::lex_operator () rust_parser::lex_operator ()
{ {
const struct token_info *token = NULL; const struct token_info *token = NULL;
int i;
for (i = 0; i < ARRAY_SIZE (operator_tokens); ++i) for (const auto &candidate : operator_tokens)
{ {
if (strncmp (operator_tokens[i].name, pstate->lexptr, if (strncmp (candidate.name, pstate->lexptr,
strlen (operator_tokens[i].name)) == 0) strlen (candidate.name)) == 0)
{ {
pstate->lexptr += strlen (operator_tokens[i].name); pstate->lexptr += strlen (candidate.name);
token = &operator_tokens[i]; token = &candidate;
break; break;
} }
} }
@ -2246,8 +2244,6 @@ rust_lex_test_push_back (rust_parser *parser)
static void static void
rust_lex_tests (void) rust_lex_tests (void)
{ {
int i;
/* Set up dummy "parser", so that rust_type works. */ /* Set up dummy "parser", so that rust_type works. */
struct parser_state ps (language_def (language_rust), target_gdbarch (), struct parser_state ps (language_def (language_rust), target_gdbarch (),
nullptr, 0, 0, nullptr, 0, nullptr, false); nullptr, 0, 0, nullptr, 0, nullptr, false);
@ -2342,13 +2338,11 @@ rust_lex_tests (void)
rust_lex_stringish_test (&parser, "br####\"\\x73tring\"####", "\\x73tring", rust_lex_stringish_test (&parser, "br####\"\\x73tring\"####", "\\x73tring",
BYTESTRING); BYTESTRING);
for (i = 0; i < ARRAY_SIZE (identifier_tokens); ++i) for (const auto &candidate : identifier_tokens)
rust_lex_test_one (&parser, identifier_tokens[i].name, rust_lex_test_one (&parser, candidate.name, candidate.value);
identifier_tokens[i].value);
for (i = 0; i < ARRAY_SIZE (operator_tokens); ++i) for (const auto &candidate : operator_tokens)
rust_lex_test_one (&parser, operator_tokens[i].name, rust_lex_test_one (&parser, candidate.name, candidate.value);
operator_tokens[i].value);
rust_lex_test_completion (&parser); rust_lex_test_completion (&parser);
rust_lex_test_push_back (&parser); rust_lex_test_push_back (&parser);

View File

@ -6375,13 +6375,12 @@ producer_is_realview (const char *producer)
"ARM/Thumb C/C++ Compiler, RVCT", "ARM/Thumb C/C++ Compiler, RVCT",
"ARM C/C++ Compiler, RVCT" "ARM C/C++ Compiler, RVCT"
}; };
int i;
if (producer == NULL) if (producer == NULL)
return false; return false;
for (i = 0; i < ARRAY_SIZE (arm_idents); i++) for (const char *ident : arm_idents)
if (startswith (producer, arm_idents[i])) if (startswith (producer, ident))
return true; return true;
return false; return false;