aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRuss Cox <rsc@golang.org>2011-03-15 14:05:37 -0400
committerRuss Cox <rsc@golang.org>2011-03-15 14:05:37 -0400
commit66f09fd459cc7477c1c2f0de5a296ab84920f473 (patch)
treed00ea388e88f60ef85c489b8071a71f2f8f45486 /src
parente72156b316f158c3357856a94b5fc60564f78b50 (diff)
downloadgo-66f09fd459cc7477c1c2f0de5a296ab84920f473.tar.xz
gc: diagnose unused labels
R=ken2 CC=golang-dev https://golang.org/cl/4287047
Diffstat (limited to 'src')
-rw-r--r--src/cmd/5g/ggen.c2
-rw-r--r--src/cmd/6g/ggen.c2
-rw-r--r--src/cmd/8g/ggen.c2
-rw-r--r--src/cmd/gc/dcl.c1
-rw-r--r--src/cmd/gc/gen.c157
-rw-r--r--src/cmd/gc/go.h9
6 files changed, 106 insertions, 67 deletions
diff --git a/src/cmd/5g/ggen.c b/src/cmd/5g/ggen.c
index 182d7f147e..7197709d4d 100644
--- a/src/cmd/5g/ggen.c
+++ b/src/cmd/5g/ggen.c
@@ -32,7 +32,7 @@ compile(Node *fn)
return;
// set up domain for labels
- labellist = L;
+ clearlabels();
lno = setlineno(fn);
diff --git a/src/cmd/6g/ggen.c b/src/cmd/6g/ggen.c
index d9fa1793c6..8d89fb164e 100644
--- a/src/cmd/6g/ggen.c
+++ b/src/cmd/6g/ggen.c
@@ -32,7 +32,7 @@ compile(Node *fn)
return;
// set up domain for labels
- labellist = L;
+ clearlabels();
lno = setlineno(fn);
diff --git a/src/cmd/8g/ggen.c b/src/cmd/8g/ggen.c
index 4dcbd44895..8db5524934 100644
--- a/src/cmd/8g/ggen.c
+++ b/src/cmd/8g/ggen.c
@@ -32,7 +32,7 @@ compile(Node *fn)
return;
// set up domain for labels
- labellist = L;
+ clearlabels();
lno = setlineno(fn);
diff --git a/src/cmd/gc/dcl.c b/src/cmd/gc/dcl.c
index cbcdcbf8c7..3089a23b0d 100644
--- a/src/cmd/gc/dcl.c
+++ b/src/cmd/gc/dcl.c
@@ -22,7 +22,6 @@ dflag(void)
/*
* declaration stack & operations
*/
-static Sym* dclstack;
static void
dcopy(Sym *a, Sym *b)
diff --git a/src/cmd/gc/gen.c b/src/cmd/gc/gen.c
index 04af5a7bb1..8ad6c437de 100644
--- a/src/cmd/gc/gen.c
+++ b/src/cmd/gc/gen.c
@@ -64,62 +64,83 @@ allocparams(void)
lineno = lno;
}
+void
+clearlabels(void)
+{
+ Label *l;
+
+ for(l=labellist; l!=L; l=l->link)
+ l->sym->label = L;
+
+ labellist = L;
+ lastlabel = L;
+}
+
static void
-newlab(int op, Sym *s, Node *stmt)
+newlab(int op, Node *nlab, Node *stmt)
{
Label *lab;
+ Sym *s;
+ int32 lno;
+
+ s = nlab->left->sym;
+ lno = nlab->left->lineno;
lab = mal(sizeof(*lab));
- lab->link = labellist;
- labellist = lab;
+ if(lastlabel == nil)
+ labellist = lab;
+ else
+ lastlabel->link = lab;
+ lastlabel = lab;
+ lab->lineno = lno;
lab->sym = s;
lab->op = op;
lab->label = pc;
lab->stmt = stmt;
+ if(op == OLABEL) {
+ if(s->label != L) {
+ lineno = lno;
+ yyerror("label %S already defined at %L", s, s->label->lineno);
+ } else
+ s->label = lab;
+ }
}
void
checklabels(void)
{
- Label *l, *m;
+ Label *l;
Sym *s;
+ int lno;
-// // print the label list
-// for(l=labellist; l!=L; l=l->link) {
-// print("lab %O %S\n", l->op, l->sym);
-// }
-
+ lno = lineno;
+
+ // resolve goto using syms
for(l=labellist; l!=L; l=l->link) {
- switch(l->op) {
- case OLABEL:
- // these are definitions -
+ switch(l->op) {
+ case OGOTO:
s = l->sym;
- for(m=labellist; m!=L; m=m->link) {
- if(m->sym != s)
- continue;
- switch(m->op) {
- case OLABEL:
- // these are definitions -
- // look for redefinitions
- if(l != m)
- yyerror("label %S redefined", s);
- break;
- case OGOTO:
- // these are references -
- // patch to definition
- patch(m->label, l->label);
- m->sym = S; // mark done
- break;
- }
+ if(s->label == L) {
+ lineno = l->lineno;
+ yyerror("label %S not defined", s);
+ break;
}
+ s->label->used = 1;
+ patch(l->label, s->label->label);
+ break;
}
}
-
- // diagnostic for all undefined references
- for(l=labellist; l!=L; l=l->link)
- if(l->op == OGOTO && l->sym != S)
- yyerror("label %S not defined", l->sym);
+
+ // diagnose unused labels
+ for(l=labellist; l!=L; l=l->link) {
+ if(l->op == OLABEL && !l->used) {
+ lineno = l->lineno;
+ yyerror("label %S defined and not used", l->sym);
+ }
+ }
+
+ lineno = lno;
}
/*
@@ -171,7 +192,7 @@ gen(Node *n)
// insert no-op so that
// L:; for { }
// does not treat L as a label for the loop.
- if(labellist && labellist->label == p3)
+ if(lastlabel != L && lastlabel->label == p3)
gused(N);
break;
@@ -180,26 +201,27 @@ gen(Node *n)
break;
case OLABEL:
- newlab(OLABEL, n->left->sym, n->right);
+ newlab(OLABEL, n, n->right);
break;
case OGOTO:
- newlab(OGOTO, n->left->sym, N);
+ newlab(OGOTO, n, N);
gjmp(P);
break;
case OBREAK:
if(n->left != N) {
- for(lab=labellist; lab!=L; lab=lab->link) {
- if(lab->sym == n->left->sym) {
- if(lab->breakpc == P)
- yyerror("invalid break label %S", n->left->sym);
- gjmp(lab->breakpc);
- goto donebreak;
- }
- }
- if(lab == L)
+ lab = n->left->sym->label;
+ if(lab == L) {
yyerror("break label not defined: %S", n->left->sym);
+ break;
+ }
+ lab->used = 1;
+ if(lab->breakpc == P) {
+ yyerror("invalid break label %S", n->left->sym);
+ break;
+ }
+ gjmp(lab->breakpc);
break;
}
if(breakpc == P) {
@@ -207,30 +229,28 @@ gen(Node *n)
break;
}
gjmp(breakpc);
- donebreak:
break;
case OCONTINUE:
if(n->left != N) {
- for(lab=labellist; lab!=L; lab=lab->link) {
- if(lab->sym == n->left->sym) {
- if(lab->continpc == P)
- yyerror("invalid continue label %S", n->left->sym);
- gjmp(lab->continpc);
- goto donecont;
- }
- }
- if(lab == L)
+ lab = n->left->sym->label;
+ if(lab == L) {
yyerror("continue label not defined: %S", n->left->sym);
+ break;
+ }
+ lab->used = 1;
+ if(lab->continpc == P) {
+ yyerror("invalid continue label %S", n->left->sym);
+ break;
+ }
+ gjmp(lab->continpc);
break;
}
-
if(continpc == P) {
yyerror("continue is not in a loop");
break;
}
gjmp(continpc);
- donecont:
break;
case OFOR:
@@ -241,10 +261,11 @@ gen(Node *n)
continpc = pc;
// define break and continue labels
- if((lab = labellist) != L && lab->label == p3 && lab->op == OLABEL && lab->stmt == n) {
+ if((lab = lastlabel) != L && lab->label == p3 && lab->op == OLABEL && lab->stmt == n) {
lab->breakpc = breakpc;
lab->continpc = continpc;
- }
+ } else
+ lab = L;
gen(n->nincr); // contin: incr
patch(p1, pc); // test:
@@ -254,6 +275,10 @@ gen(Node *n)
patch(breakpc, pc); // done:
continpc = scontin;
breakpc = sbreak;
+ if(lab) {
+ lab->breakpc = P;
+ lab->continpc = P;
+ }
break;
case OIF:
@@ -274,13 +299,17 @@ gen(Node *n)
breakpc = gjmp(P); // break: goto done
// define break label
- if((lab = labellist) != L && lab->label == p3 && lab->op == OLABEL && lab->stmt == n)
+ if((lab = lastlabel) != L && lab->label == p3 && lab->op == OLABEL && lab->stmt == n)
lab->breakpc = breakpc;
+ else
+ lab = L;
patch(p1, pc); // test:
genlist(n->nbody); // switch(test) body
patch(breakpc, pc); // done:
breakpc = sbreak;
+ if(lab != L)
+ lab->breakpc = P;
break;
case OSELECT:
@@ -289,13 +318,17 @@ gen(Node *n)
breakpc = gjmp(P); // break: goto done
// define break label
- if((lab = labellist) != L && lab->label == p3 && lab->op == OLABEL && lab->stmt == n)
+ if((lab = lastlabel) != L && lab->label == p3 && lab->op == OLABEL && lab->stmt == n)
lab->breakpc = breakpc;
+ else
+ lab = L;
patch(p1, pc); // test:
genlist(n->nbody); // select() body
patch(breakpc, pc); // done:
breakpc = sbreak;
+ if(lab != L)
+ lab->breakpc = P;
break;
case OASOP:
diff --git a/src/cmd/gc/go.h b/src/cmd/gc/go.h
index b071eb2273..39c316f2e5 100644
--- a/src/cmd/gc/go.h
+++ b/src/cmd/gc/go.h
@@ -138,6 +138,7 @@ typedef struct Sym Sym;
typedef struct Node Node;
typedef struct NodeList NodeList;
typedef struct Type Type;
+typedef struct Label Label;
struct Type
{
@@ -302,11 +303,14 @@ struct Sym
Pkg* pkg;
char* name; // variable name
Node* def; // definition: ONAME OTYPE OPACK or OLITERAL
+ Label* label; // corresponding label (ephemeral)
int32 block; // blocknumber to catch redeclaration
int32 lastlineno; // last declaration for diagnostic
};
#define S ((Sym*)0)
+EXTERN Sym* dclstack;
+
struct Pkg
{
char* name;
@@ -619,20 +623,22 @@ struct Magic
typedef struct Prog Prog;
-typedef struct Label Label;
struct Label
{
uchar op; // OGOTO/OLABEL
+ uchar used;
Sym* sym;
Node* stmt;
Prog* label; // pointer to code
Prog* breakpc; // pointer to code
Prog* continpc; // pointer to code
Label* link;
+ int32 lineno;
};
#define L ((Label*)0)
EXTERN Label* labellist;
+EXTERN Label* lastlabel;
/*
* note this is the runtime representation
@@ -900,6 +906,7 @@ void allocparams(void);
void cgen_as(Node *nl, Node *nr);
void cgen_callmeth(Node *n, int proc);
void checklabels(void);
+void clearlabels(void);
int dotoffset(Node *n, int *oary, Node **nn);
void gen(Node *n);
void genlist(NodeList *l);