Skip to content

Commit

Permalink
compiler: -Wundefined to warn about undefined variables (WIP)
Browse files Browse the repository at this point in the history
Limitations:
- complains about iterative calculations like  avg = A*avg+(1-A)*VALUE
- no way to explicitly initialize variables to zero (making the warning
  go away)
  • Loading branch information
wpwrak committed Jan 12, 2012
1 parent edecc6e commit cb6d92a
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/compiler/idgen
Expand Up @@ -50,6 +50,7 @@ sed 's/#.*//;/^ *$/d' $1 | sort | {
.fpvm_sym = { .name = "$1" },
.pfv_idx = $2,
.pvv_idx = $3,
.flags = SF_SYSTEM,
},
EOF
i=`expr $i + 1`
Expand Down
5 changes: 5 additions & 0 deletions src/compiler/parser.y
Expand Up @@ -33,6 +33,7 @@


int warn_section = 0;
int warn_undefined = 0;

struct yyParser;
static void yy_parse_failed(struct yyParser *yypParser);
Expand Down Expand Up @@ -255,6 +256,7 @@ assignments ::= assignments assignment.
assignments ::= .

assignment ::= ident(I) TOK_ASSIGN expr(N) opt_semi. {
I->sym->flags |= SF_ASSIGNED;
state->error = state->comm->assign_default(state->comm, I->sym, N);
free(I);
if(state->error) {
Expand Down Expand Up @@ -477,6 +479,9 @@ primary_expr(N) ::= TOK_CONSTANT(C). {
}

primary_expr(N) ::= ident(I). {
if(warn_undefined && !(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);
free(I);
}
Expand Down
1 change: 1 addition & 0 deletions src/compiler/parser_helper.h
Expand Up @@ -46,6 +46,7 @@ struct parser_comm {
};

extern int warn_section;
extern int warn_undefined;

const char *parse(const char *expr, int start_token, struct parser_comm *comm);
void parse_free_one(struct ast_node *node);
Expand Down
4 changes: 3 additions & 1 deletion src/compiler/ptest/ptest.c
Expand Up @@ -333,7 +333,7 @@ static void usage(const char *name)
" -n runs run compilation repeatedly (default: run only once)\n"
" -q quiet operation\n"
" -s dump symbol table after parsing (only if -c is not set)\n"
" -Wwarning enable compiler warning (one of: section)\n"
" -Wwarning enable compiler warning (one of: section, undefined)\n"
, name);
exit(1);
}
Expand Down Expand Up @@ -368,6 +368,8 @@ int main(int argc, char **argv)
case 'W':
if (!strcmp(optarg, "section"))
warn_section = 1;
else if (!strcmp(optarg, "undefined"))
warn_undefined = 1;
else
usage(*argv);
break;
Expand Down
4 changes: 3 additions & 1 deletion src/compiler/symtab.h
Expand Up @@ -14,7 +14,9 @@
#include <fpvm/symbol.h>


#define SF_FIXED 0
#define SF_SYSTEM (1 << 0)
#define SF_ASSIGNED (1 << 1)
#define SF_FIXED SF_SYSTEM

struct sym {
struct fpvm_sym fpvm_sym;
Expand Down
46 changes: 46 additions & 0 deletions src/compiler/test/wundef
@@ -0,0 +1,46 @@
#!/bin/sh
. ./Common

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

ptest "-Wundefined: variable defined per-frame" -q -Wundefined <<EOF
per_frame:
foo = 1
bar = foo
per_vertex:
bar = foo
EOF
expect <<EOF
EOF

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

ptest "-Wundefined: variable defined per-vertex" -q -Wundefined <<EOF
per_vertex:
foo = 1
bar = foo
EOF
expect <<EOF
EOF

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

ptest "-Wundefined: variable undefined in per-frame" -q -Wundefined <<EOF
per_frame:
bar = foo
EOF
expect <<EOF
accessing undefined variable foo
EOF

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

ptest "-Wundefined: variable undefined in per-vertex" -q -Wundefined <<EOF
per_vertex:
bar = foo
EOF
expect <<EOF
accessing undefined variable foo
EOF

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

0 comments on commit cb6d92a

Please sign in to comment.