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
6832 tcc_tcov_check_line (tcc_state, 0), tcc_tcov_block_begin (tcc_state);6832 tcc_tcov_check_line (tcc_state, 0), tcc_tcov_block_begin (tcc_state);
6833 6833
6834 if (t == TOK_IF) {6834 if (t == TOK_IF) {
6835 //new_scope(&o); //?? breaks tests2.122
6835 skip('(');6836 skip('(');
6836 gexpr();6837 gexpr();
6837 skip(')');6838 skip(')');
@@ -6846,8 +6847,10 @@ again: · 8 unmodified lines
6846 } else {6847 } else {
6847 gsym(a);6848 gsym(a);
6848 }6849 }
6850 //prev_scope(&o,0); //?? breaks tests2.122
6849 6851
6850 } else if (t == TOK_WHILE) {6852 } else if (t == TOK_WHILE) {
6853 new_scope(&o);
6851 d = gind();6854 d = gind();
6852 skip('(');6855 skip('(');
6853 gexpr();6856 gexpr();
@@ -6858,7 +6861,7 @@ again: · 4 unmodified lines
6858 gjmp_addr(d);6861 gjmp_addr(d);
6859 gsym_addr(b, d);6862 gsym_addr(b, d);
6860 gsym(a);6863 gsym(a);
6861 6864 prev_scope(&o,0);
6862 } else if (t == '{') {6865 } else if (t == '{') {
6863 new_scope(&o);6866 new_scope(&o);
6864 6867
@@ -6970,6 +6973,7 @@ again: · 105 unmodified lines
6970 prev_scope(&o, 0);6973 prev_scope(&o, 0);
6971 6974
6972 } else if (t == TOK_DO) {6975 } else if (t == TOK_DO) {
6976 new_scope(&o);
6973 a = b = 0;6977 a = b = 0;
6974 d = gind();6978 d = gind();
6975 lblock(&a, &b);6979 lblock(&a, &b);
@@ -6979,6 +6983,7 @@ again: · 3 unmodified lines
6979 gexpr();6983 gexpr();
6980 skip(')');6984 skip(')');
6981 skip(';');6985 skip(';');
6986 prev_scope(&o,0);
6982 c = gvtst(0, 0);6987 c = gvtst(0, 0);
6983 gsym_addr(c, d);6988 gsym_addr(c, d);
6984 gsym(a);6989 gsym(a);
@@ -6986,6 +6991,7 @@ again: · 1 unmodified line
6986 } else if (t == TOK_SWITCH) {6991 } else if (t == TOK_SWITCH) {
6987 struct switch_t *sw;6992 struct switch_t *sw;
6988 6993
6994 new_scope(&o);
6989 sw = tcc_mallocz(sizeof *sw);6995 sw = tcc_mallocz(sizeof *sw);
6990 sw->bsym = &a;6996 sw->bsym = &a;
6991 sw->scope = cur_scope;6997 sw->scope = cur_scope;
@@ -7031,6 +7037,7 @@ again: · 39 unmodified lines
7031 dynarray_reset(&sw->p, &sw->n);7037 dynarray_reset(&sw->p, &sw->n);
7032 cur_switch = sw->prev;7038 cur_switch = sw->prev;
7033 tcc_free(sw);7039 tcc_free(sw);
7040 prev_scope(&o,0);
7034 7041
7035 } else if (t == TOK_CASE) {7042 } else if (t == TOK_CASE) {
7036 struct case_t *cr = tcc_malloc(sizeof(struct case_t));7043 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>
2enum{ in = 0};
3#define myassert(X) do{ if(!X) printf("%d: assertion failed\n", __LINE__); }while(0)
4int 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