summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlan Coopersmith <alan.coopersmith@oracle.com>2022-12-06 17:06:49 -0800
committerAlan Coopersmith <alan.coopersmith@oracle.com>2023-01-01 14:34:56 -0800
commit2bb1bd63e2e766ce281a61cadbffad7a12d5fd18 (patch)
treeddfc6fe3d1faf808f982be2bccd90e32e3b0e74c
parent78139e4cfe37c56bb8e06a2574c7d476d9607af9 (diff)
Variable scope reduction as suggested by cppcheck
Signed-off-by: Alan Coopersmith <alan.coopersmith@oracle.com>
-rw-r--r--ifparser.c4
-rw-r--r--include.c10
-rw-r--r--parse.c36
-rw-r--r--pr.c11
4 files changed, 33 insertions, 28 deletions
diff --git a/ifparser.c b/ifparser.c
index c53d6d1..631b4f0 100644
--- a/ifparser.c
+++ b/ifparser.c
@@ -202,7 +202,7 @@ parse_character(IfParser *g, const char *cp, long *valp)
static const char *
parse_value(IfParser *g, const char *cp, long *valp)
{
- const char *var, *varend;
+ const char *var;
*valp = 0;
@@ -286,6 +286,8 @@ parse_value(IfParser *g, const char *cp, long *valp)
else if (!isvarfirstletter(*cp))
return CALLFUNC(g, handle_error) (g, cp, "variable or number");
else {
+ const char *varend;
+
DO(cp = parse_variable(g, cp, &var));
varend = cp;
SKIPSPACE(cp);
diff --git a/include.c b/include.c
index 08c0ee4..9b1a9ae 100644
--- a/include.c
+++ b/include.c
@@ -164,8 +164,6 @@ newinclude(const char *newfile, const char *incstring)
void
included_by(struct inclist *ip, struct inclist *newfile)
{
- int i;
-
if (ip == NULL)
return;
/*
@@ -179,7 +177,7 @@ included_by(struct inclist *ip, struct inclist *newfile)
ip->i_merged = malloc(sizeof(boolean) * ip->i_listlen);
}
else {
- for (i = 0; i < ip->i_listlen; i++)
+ for (int i = 0; i < ip->i_listlen; i++)
if (ip->i_list[i] == newfile) {
i = strlen(newfile->i_file);
if (!(ip->i_flags & INCLUDED_SYM) &&
@@ -225,7 +223,6 @@ static const char *
find_full_inc_path(const char *file, const char *include, int type)
{
static char path[BUFSIZ];
- const char **pp, *p;
struct stat st;
if (inclistnext == inclist) {
@@ -247,6 +244,8 @@ find_full_inc_path(const char *file, const char *include, int type)
* in the directory of the file being parsed.
*/
if ((type == INCLUDEDOT) || (type == INCLUDENEXTDOT)) {
+ const char *p;
+
for (p = file + strlen(file); p > file; p--)
if (*p == '/')
break;
@@ -272,9 +271,8 @@ find_full_inc_path(const char *file, const char *include, int type)
*/
if ((type == INCLUDE) || (type == INCLUDEDOT))
includedirsnext = includedirs;
- pp = includedirsnext;
- for (; *pp; pp++) {
+ for (const char **pp = includedirsnext; *pp; pp++) {
snprintf(path, sizeof(path), "%s/%s", *pp, include);
remove_dotdot(path);
if (stat(path, &st) == 0 && !S_ISDIR(st.st_mode)) {
diff --git a/parse.c b/parse.c
index 2e4b369..fe10c01 100644
--- a/parse.c
+++ b/parse.c
@@ -38,10 +38,11 @@ gobble(struct filepointer *filep, struct inclist *file,
struct inclist *file_red)
{
char *line;
- int type;
while ((line = getnextline(filep))) {
- switch (type = deftype(line, filep, file_red, file, FALSE)) {
+ int type = deftype(line, filep, file_red, file, FALSE);
+
+ switch (type) {
case IF:
case IFFALSE:
case IFGUESSFALSE:
@@ -247,9 +248,7 @@ deftype(char *line, struct filepointer *filep,
struct symtab **
fdefined(const char *symbol, struct inclist *file, struct inclist **srcfile)
{
- struct inclist **ip;
struct symtab **val;
- int i;
static int recurse_lvl = 0;
if (file->i_flags & DEFCHECKED)
@@ -260,13 +259,17 @@ fdefined(const char *symbol, struct inclist *file, struct inclist **srcfile)
debug(1, ("%s defined in %s as %s\n",
symbol, file->i_file, (*val)->s_value));
if (val == NULL && file->i_list) {
- for (ip = file->i_list, i = 0; i < file->i_listlen; i++, ip++)
+ struct inclist **ip;
+ int i;
+
+ for (ip = file->i_list, i = 0; i < file->i_listlen; i++, ip++) {
if (file->i_merged[i] == FALSE) {
val = fdefined(symbol, *ip, srcfile);
file->i_merged[i] = merge2defines(file, *ip);
if (val != NULL)
break;
}
+ }
}
else if (val != NULL && srcfile != NULL)
*srcfile = file;
@@ -448,12 +451,10 @@ slookup(const char *symbol, struct inclist *file)
static int
merge2defines(struct inclist *file1, struct inclist *file2)
{
- int i;
-
if ((file1 == NULL) || (file2 == NULL) || !(file2->i_flags & FINISHED))
return 0;
- for (i = 0; i < file2->i_listlen; i++)
+ for (int i = 0; i < file2->i_listlen; i++)
if (file2->i_merged[i] == FALSE)
return 0;
@@ -524,14 +525,12 @@ int
find_includes(struct filepointer *filep, struct inclist *file,
struct inclist *file_red, int recursion, boolean failOK)
{
- struct inclist *inclistp;
- const char **includedirsp;
char *line;
- int type;
- boolean recfailOK;
while ((line = getnextline(filep))) {
- switch (type = deftype(line, filep, file_red, file, TRUE)) {
+ int type = deftype(line, filep, file_red, file, TRUE);
+
+ switch (type) {
case IF:
doif:
type = find_includes(filep, file, file_red, recursion + 1, failOK);
@@ -544,6 +543,9 @@ find_includes(struct filepointer *filep, struct inclist *file,
case IFFALSE:
case IFGUESSFALSE:
doiffalse:
+ {
+ boolean recfailOK;
+
if (type == IFGUESSFALSE || type == ELIFGUESSFALSE)
recfailOK = TRUE;
else
@@ -555,6 +557,7 @@ find_includes(struct filepointer *filep, struct inclist *file,
goto doif;
else if ((type == ELIFFALSE) || (type == ELIFGUESSFALSE))
goto doiffalse;
+ }
break;
case IFDEF:
case IFNDEF:
@@ -613,13 +616,16 @@ find_includes(struct filepointer *filep, struct inclist *file,
case INCLUDEDOT:
case INCLUDENEXT:
case INCLUDENEXTDOT:
- inclistp = inclistnext;
- includedirsp = includedirsnext;
+ {
+ struct inclist *inclistp = inclistnext;
+ const char **includedirsp = includedirsnext;
+
debug(2, ("%s, reading %s, includes %s\n",
file_red->i_file, file->i_file, line));
add_include(filep, file, file_red, line, type, failOK);
inclistnext = inclistp;
includedirsnext = includedirsp;
+ }
break;
case ERROR:
case WARNING:
diff --git a/pr.c b/pr.c
index 32ad246..5c5b8b8 100644
--- a/pr.c
+++ b/pr.c
@@ -32,7 +32,6 @@ add_include(struct filepointer *filep, struct inclist *file,
boolean failOK)
{
struct inclist *newfile;
- struct filepointer *content;
/*
* First decide what the pathname of this include file really is.
@@ -55,6 +54,8 @@ add_include(struct filepointer *filep, struct inclist *file,
if (newfile) {
included_by(file, newfile);
if (!(newfile->i_flags & SEARCHED)) {
+ struct filepointer *content;
+
newfile->i_flags |= SEARCHED;
content = getfile(newfile->i_file);
find_includes(content, newfile, file_red, 0, failOK);
@@ -106,7 +107,7 @@ pr(struct inclist *ip, const char *file, const char *base)
{
static const char *lastfile;
static int current_len;
- int len, i;
+ int len;
const char *quoted;
char quotebuf[BUFSIZ];
@@ -131,20 +132,18 @@ pr(struct inclist *ip, const char *file, const char *base)
ip->i_flags |= NOTIFIED;
lastfile = NULL;
printf("\n# %s includes:", ip->i_file);
- for (i = 0; i < ip->i_listlen; i++)
+ for (int i = 0; i < ip->i_listlen; i++)
printf("\n#\t%s", ip->i_list[i]->i_incstring);
}
void
recursive_pr_include(struct inclist *head, const char *file, const char *base)
{
- int i;
-
if (head->i_flags & MARKED)
return;
head->i_flags |= MARKED;
if (head->i_file != file)
pr(head, file, base);
- for (i = 0; i < head->i_listlen; i++)
+ for (int i = 0; i < head->i_listlen; i++)
recursive_pr_include(head->i_list[i], file, base);
}