Skip to content

Commit

Permalink
compiler: -Wundefined now also works for old-style patches
Browse files Browse the repository at this point in the history
Old style assumes everything is implicitly initialized to zero.
We can still report identifiers that are never assigned to, though.

New style allows explicit initialization and thus can report
uninitialized variables immediately when they're read.
  • Loading branch information
wpwrak committed Jan 12, 2012
1 parent 068cfc9 commit 1f53fb2
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 1 deletion.
12 changes: 11 additions & 1 deletion src/compiler/parser.y
Expand Up @@ -228,6 +228,14 @@ start ::= TOK_START_EXPR expr(N). {
}

start ::= TOK_START_ASSIGN sections. {
if(warn_undefined && state->style == old_style) {
const struct sym *sym;

forall_syms(sym)
if(!(sym->flags & (SF_SYSTEM | SF_ASSIGNED)))
printf("variable %s is only read, never set\n",
sym->fpvm_sym.name);
}
state->success = 1;
}

Expand Down Expand Up @@ -274,6 +282,7 @@ assignment ::= ident(I) TOK_ASSIGN expr(N) opt_semi. {
FAIL;
return;
}
IS_STYLE(new_style);
} else {
state->error =
state->comm->assign_default(state->comm, I->sym, N);
Expand Down Expand Up @@ -499,7 +508,8 @@ primary_expr(N) ::= TOK_CONSTANT(C). {
}

primary_expr(N) ::= ident(I). {
if(warn_undefined && !(I->sym->flags & (SF_SYSTEM | SF_ASSIGNED)))
if(warn_undefined && state->style == new_style &&
!(I->sym->flags & (SF_SYSTEM | SF_ASSIGNED)))
printf("accessing undefined variable %s\n",
I->sym->fpvm_sym.name);
N = node(I->token, I->sym, NULL, NULL, NULL);
Expand Down
19 changes: 19 additions & 0 deletions src/compiler/test/wundef
Expand Up @@ -55,4 +55,23 @@ expect <<EOF
accessing undefined variable foo
EOF

#------------------------------------------------------------------------------

ptest "-Wundefined: old-style read-modify-write" -q -Wundefined <<EOF
per_vertex =
foo = foo
EOF
expect <<EOF
EOF

#------------------------------------------------------------------------------

ptest "-Wundefined: old-style undefined" -q -Wundefined <<EOF
per_vertex =
foo = bar
EOF
expect <<EOF
variable bar is only read, never set
EOF

###############################################################################

0 comments on commit 1f53fb2

Please sign in to comment.