History

Create scopes for switches/whiles/do-whiles

The C standard has required this since at least C99. A new scope should also be created for ifs, but that's currently breaking tests2/122_vla_reuse.

Petr Skocik committed 3 years ago

Commit ccc16511 parent 29ae3ed3 files · +53 −1Browse files

Unified · Split

3 files
  1. tccgen.c +8 −1
  2. tests/tests2/129_scopes.c +45 −0
  3. tests/tests2/129_scopes.expect +0 −0
tccgen.c+8−1
@@ -6832,6 +6832,7 @@ again: · 6831 unmodified lines
68326832 tcc_tcov_check_line (tcc_state, 0), tcc_tcov_block_begin (tcc_state);
68336833
68346834 if (t == TOK_IF) {
6835+ //new_scope(&o); //?? breaks tests2.122
68356836 skip('(');
68366837 gexpr();
68376838 skip(')');
@@ -6846,8 +6847,10 @@ again: · 8 unmodified lines
68466847 } else {
68476848 gsym(a);
68486849 }
6850+ //prev_scope(&o,0); //?? breaks tests2.122
68496851
68506852 } else if (t == TOK_WHILE) {
6853+ new_scope(&o);
68516854 d = gind();
68526855 skip('(');
68536856 gexpr();
@@ -6858,7 +6861,7 @@ again: · 4 unmodified lines
68586861 gjmp_addr(d);
68596862 gsym_addr(b, d);
68606863 gsym(a);
6861−
6864+ prev_scope(&o,0);
68626865 } else if (t == '{') {
68636866 new_scope(&o);
68646867
@@ -6970,6 +6973,7 @@ again: · 105 unmodified lines
69706973 prev_scope(&o, 0);
69716974
69726975 } else if (t == TOK_DO) {
6976+ new_scope(&o);
69736977 a = b = 0;
69746978 d = gind();
69756979 lblock(&a, &b);
@@ -6979,6 +6983,7 @@ again: · 3 unmodified lines
69796983 gexpr();
69806984 skip(')');
69816985 skip(';');
6986+ prev_scope(&o,0);
69826987 c = gvtst(0, 0);
69836988 gsym_addr(c, d);
69846989 gsym(a);
@@ -6986,6 +6991,7 @@ again: · 1 unmodified line
69866991 } else if (t == TOK_SWITCH) {
69876992 struct switch_t *sw;
69886993
6994+ new_scope(&o);
69896995 sw = tcc_mallocz(sizeof *sw);
69906996 sw->bsym = &a;
69916997 sw->scope = cur_scope;
@@ -7031,6 +7037,7 @@ again: · 39 unmodified lines
70317037 dynarray_reset(&sw->p, &sw->n);
70327038 cur_switch = sw->prev;
70337039 tcc_free(sw);
7040+ prev_scope(&o,0);
70347041
70357042 } else if (t == TOK_CASE) {
70367043 struct case_t *cr = tcc_malloc(sizeof(struct case_t));
tests/tests2/129_scopes.c+45−0
@@ -0,0 +1,45 @@
1+#include <stdio.h>
2+enum{ in = 0};
3+#define myassert(X) do{ if(!X) printf("%d: assertion failed\n", __LINE__); }while(0)
4+int main(){
5+ #if 0
6+ {
7+ myassert(!in);
8+ if(sizeof(enum{in=1})) myassert(in);
9+ myassert(!in); //OOPS
10+ }
11+ #endif
12+ {
13+ myassert(!in);
14+ switch(sizeof(enum{in=1})) { default: myassert(in); }
15+ myassert(!in); //OOPS
16+ }
17+ {
18+ myassert(!in);
19+ while(sizeof(enum{in=1})) { myassert(in); break; }
20+ myassert(!in); //OOPS
21+ }
22+ {
23+ myassert(!in);
24+ do{ myassert(!in);}while(0*sizeof(enum{in=1}));
25+ myassert(!in); //OOPS
26+ }
27+
28+ {
29+ myassert(!in);
30+ for(sizeof(enum{in=1});;){ myassert(in); break; }
31+ myassert(!in); //OK
32+ }
33+ {
34+ myassert(!in);
35+ for(;;sizeof(enum{in=1})){ myassert(in); break; }
36+ myassert(!in); //OK
37+ }
38+ {
39+ myassert(!in);
40+ for(;sizeof(enum{in=1});){ myassert(in); break; }
41+ myassert(!in); //OK
42+ }
43+
44+}
45+
tests/tests2/129_scopes.expect+0−0

Empty file added.

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t