-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheckused.c
62 lines (57 loc) · 1.56 KB
/
checkused.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <stdio.h>
#include <string.h>
#include "data.h"
#include "auxiliary.h"
static int errors;
static void mark(AstNode *tree){
AstNode *n;
AstNode *children;
/*Loop through the entire chain of children*/
for(n = tree; n != NULL; n = n->chain){
children = n->children;
if(n->tag == VarDecl)
children = n->node.VarDecl.expression;
else if(n->tag == Parameter)
children = NULL;
else if(n->tag == DefineFunction && strcmp(n->node.DefineFunction.identifier->node.Identifier.symbol->name, "setup") != 0)
children = n->children->chain; /* skip the identifier */
else if(n->tag == DefineTask)
children = n->children->chain; /* skip the identifier */
else if(n->tag == Identifier)
/*If a variable is used, mark it as used*/
n->node.Identifier.symbol->used++;
mark(children);
}
}
static void check(AstNode *tree){
AstNode *n;
/*Loop through the entire chain of children*/
for(n = tree; n != NULL; n = n->chain){
/*If one of the variables are not marked as used, switch over their type and return an error*/
if(n->tag == Identifier && n->node.Identifier.symbol->used == 0){
char *kind;
Symbol *sym = n->node.Identifier.symbol;
switch(sym->type->tag){
case FunctionTypeTag:
kind = "function";
break;
case TaskTypeTag:
kind = "task";
break;
default:
kind = "variable";
break;
}
eprintf(n->linenum, "Unused %s: %s\n", kind, sym->name);
errors++;
}
/*Special case*/
if(n->tag != MessageIdentifier)
check(n->children);
}
}
int checkused(AstNode *tree){
mark(tree);
check(tree);
return errors;
}