History

Fix some string literal expressions in initializers

Things like 'char x[] = { "foo"[1], 0 }' (which should initialize a two-character array 'x' with "o\0"). See added testcase.

Michael Matz committed 6 years ago

Commit 7b6931e1 parent b2d351e3 files · +97 −37Browse files

Unified · Split

3 files
  1. tcc.h +1 −1
  2. tccgen.c +52 −36
  3. tests/tcctest.c +44 −0
tcc.h+1−1
@@ -449,8 +449,8 @@ typedef union CValue { · 448 unmodified lines
449449 float f;
450450 uint64_t i;
451451 struct {
452− int size;
453452 const void *data;
453+ int size;
454454 } str;
455455 int tab[LDOUBLE_SIZE/4];
456456 } CValue;
tccgen.c+52−36
@@ -81,6 +81,7 @@ ST_DATA int func_vc; · 80 unmodified lines
8181 static int last_line_num, new_file, func_ind; /* debug info control */
8282 ST_DATA const char *funcname;
8383 ST_DATA CType int_type, func_old_type, char_pointer_type;
84+static CString initstr;
8485
8586 #if PTR_SIZE == 4
8687 #define VT_SIZE_T (VT_INT | VT_UNSIGNED)
@@ -779,6 +780,7 @@ ST_FUNC void tccgen_init(TCCState *s1) · 692 unmodified lines
779780 #ifdef precedence_parser
780781 init_prec();
781782 #endif
783+ cstr_new(&initstr);
782784 }
783785
784786 ST_FUNC int tccgen_compile(TCCState *s1)
@@ -810,6 +812,7 @@ ST_FUNC int tccgen_compile(TCCState *s1) · 25 unmodified lines
810812
811813 ST_FUNC void tccgen_finish(TCCState *s1)
812814 {
815+ cstr_free(&initstr);
813816 free_inline_functions(s1);
814817 sym_pop(&global_stack, NULL, 0);
815818 sym_pop(&local_stack, NULL, 0);
@@ -3219,7 +3222,6 @@ redo: · 2403 unmodified lines
32193222 gen_cast_s(VT_INT);
32203223 #endif
32213224 type1 = vtop[-1].type;
3222− type1.t &= ~VT_ARRAY;
32233225 if (vtop[-1].type.t & VT_VLA)
32243226 vla_runtime_pointed_size(&vtop[-1].type);
32253227 else {
@@ -3249,6 +3251,7 @@ redo: · 23 unmodified lines
32493251 {
32503252 gen_opic(op);
32513253 }
3254+ type1.t &= ~VT_ARRAY;
32523255 /* put again type if gen_opic() swaped operands */
32533256 vtop->type = type1;
32543257 }
@@ -7229,12 +7232,12 @@ static void parse_init_elem(int expr_type) · 3974 unmodified lines
72297232 /* NOTE: symbols are accepted, as well as lvalue for anon symbols
72307233 (compound literals). */
72317234 if (((vtop->r & (VT_VALMASK | VT_LVAL)) != VT_CONST
7232− && ((vtop->r & (VT_SYM|VT_LVAL)) != (VT_SYM|VT_LVAL)
7233− || vtop->sym->v < SYM_FIRST_ANOM))
7235+ && ((vtop->r & (VT_SYM|VT_LVAL)) != (VT_SYM|VT_LVAL)
7236+ || vtop->sym->v < SYM_FIRST_ANOM))
72347237 #ifdef TCC_TARGET_PE
72357238 || ((vtop->r & VT_SYM) && vtop->sym->a.dllimport)
72367239 #endif
7237− )
7240+ )
72387241 tcc_error("initializer element is not constant");
72397242 break;
72407243 case EXPR_ANY:
@@ -7442,7 +7445,7 @@ static void init_putv(CType *type, Section *sec, unsigned long c) · 201 unmodified lines
74427445 anonymous one. That is, there's no difference in vtop
74437446 between '(void *){x}' and '&(void *){x}'. Ignore
74447447 pointer typed entities here. Hopefully no real code
7445− will every use compound literals with scalar type. */
7448+ will ever use compound literals with scalar type. */
74467449 (vtop->type.t & VT_BTYPE) != VT_PTR) {
74477450 /* These come from compound literals, memcpy stuff over. */
74487451 Section *ssec;
@@ -7450,7 +7453,7 @@ static void init_putv(CType *type, Section *sec, unsigned long c) · 1 unmodified line
74507453 ElfW_Rel *rel;
74517454 esym = elfsym(vtop->sym);
74527455 ssec = tcc_state->sections[esym->st_shndx];
7453− memmove (ptr, ssec->data + esym->st_value, size);
7456+ memmove (ptr, ssec->data + esym->st_value + (int)vtop->c.i, size);
74547457 if (ssec->reloc) {
74557458 /* We need to copy over all memory contents, and that
74567459 includes relocations. Use the fact that relocs are
@@ -7593,7 +7596,7 @@ static void init_putv(CType *type, Section *sec, unsigned long c) · 136 unmodified lines
75937596 static void decl_initializer(CType *type, Section *sec, unsigned long c,
75947597 int flags)
75957598 {
7596− int len, n, no_oblock, nb, i;
7599+ int len, n, no_oblock, i;
75977600 int size1, align1;
75987601 Sym *s, *f;
75997602 Sym indexsym;
@@ -7641,41 +7644,54 @@ static void decl_initializer(CType *type, Section *sec, unsigned long c, · 41 unmodified lines
76417644 (t1->t & VT_BTYPE) == VT_INT
76427645 #endif
76437646 ) || (tok == TOK_STR && (t1->t & VT_BTYPE) == VT_BYTE)) {
7647+ int nb;
76447648 len = 0;
7649+ cstr_reset(&initstr);
7650+ if (size1 != (tok == TOK_STR ? 1 : sizeof(nwchar_t)))
7651+ tcc_error("unhandled string literal merging");
76457652 while (tok == TOK_STR || tok == TOK_LSTR) {
7646− int cstr_len, ch;
7647−
7648− /* compute maximum number of chars wanted */
7653+ if (initstr.size)
7654+ initstr.size -= size1;
76497655 if (tok == TOK_STR)
7650− cstr_len = tokc.str.size;
7656+ len += tokc.str.size;
76517657 else
7652− cstr_len = tokc.str.size / sizeof(nwchar_t);
7653− cstr_len--;
7654− nb = cstr_len;
7655− if (n >= 0 && nb > (n - len))
7656− nb = n - len;
7657− if (!(flags & DIF_SIZE_ONLY)) {
7658− if (cstr_len > nb)
7659− tcc_warning("initializer-string for array is too long");
7660− /* in order to go faster for common case (char
7661− string in global variable, we handle it
7662− specifically */
7663− if (sec && tok == TOK_STR && size1 == 1) {
7664− if (!NODATA_WANTED)
7665− memcpy(sec->data + c + len, tokc.str.data, nb);
7666− } else {
7667− for(i=0;i<nb;i++) {
7668− if (tok == TOK_STR)
7669− ch = ((unsigned char *)tokc.str.data)[i];
7670− else
7671− ch = ((nwchar_t *)tokc.str.data)[i];
7672− vpushi(ch);
7673− init_putv(t1, sec, c + (len + i) * size1);
7674− }
7658+ len += tokc.str.size / sizeof(nwchar_t);
7659+ len--;
7660+ cstr_cat(&initstr, tokc.str.data, tokc.str.size);
7661+ next();
7662+ }
7663+ if (tok != ')' && tok != '}' && tok != ',' && tok != ';'
7664+ && tok != TOK_EOF) {
7665+ /* Not a lone literal but part of a bigger expression. */
7666+ unget_tok(size1 == 1 ? TOK_STR : TOK_LSTR);
7667+ tokc.str.size = initstr.size;
7668+ tokc.str.data = initstr.data;
7669+ indexsym.c = 0;
7670+ f = &indexsym;
7671+ goto do_init_list;
7672+ }
7673+ nb = len;
7674+ if (n >= 0 && len > n)
7675+ nb = n;
7676+ if (!(flags & DIF_SIZE_ONLY)) {
7677+ if (len > nb)
7678+ tcc_warning("initializer-string for array is too long");
7679+ /* in order to go faster for common case (char
7680+ string in global variable, we handle it
7681+ specifically */
7682+ if (sec && size1 == 1) {
7683+ if (!NODATA_WANTED)
7684+ memcpy(sec->data + c, initstr.data, nb);
7685+ } else {
7686+ for(i=0;i<nb;i++) {
7687+ if (size1 == 1)
7688+ ch = ((unsigned char *)initstr.data)[i];
7689+ else
7690+ ch = ((nwchar_t *)initstr.data)[i];
7691+ vpushi(ch);
7692+ init_putv(t1, sec, c + i * size1);
76757693 }
76767694 }
7677− len += nb;
7678− next();
76797695 }
76807696 /* only add trailing zero if enough storage (no
76817697 warning in this case since it is standard) */
tests/tcctest.c+44−0
@@ -467,6 +467,48 @@ int ret(a) · 466 unmodified lines
467467 return 0;
468468 }
469469
470+char str_ag1[] = "b";
471+char str_ag2[] = { "b" };
472+/*char str_bg1[] = ("cccc"); GCC accepts this with pedantic warning, TCC not */
473+char str_ag3[] = { "ab"[1], 0 };
474+char str_ag4[2] = { "b" };
475+char str_x[2] = { "xy" "z"[2], 0 };
476+char *str_ar[] = { "one", "two" };
477+struct str_SS {unsigned char a[3], b; };
478+struct str_SS str_sinit15 = { "r" };
479+struct str_SS str_sinit16[] = { { "q" }, 2 };
480+
481+static void string_test2()
482+{
483+ char *p = "hello";
484+ char a3[2] = { "p" };
485+ char a4[2] = { "ab" "c"[2], 0 };
486+ char *pa1 = "def" + 1;
487+ char *pa2 = { "xyz" + 1 };
488+ int i = 0;
489+ struct str_SS ss = { { [0 ... 1] = 'a' }, 0 };
490+ puts("string_test2");
491+ puts(str_ag1);
492+ puts(str_ag2);
493+ /*puts(str_bg1);*/
494+ puts(str_ag3);
495+ puts(str_ag4);
496+ puts(str_x);
497+ puts(str_sinit15.a);
498+ puts(str_sinit16[0].a);
499+ puts(a3);
500+ puts(a4);
501+ puts(p);
502+ puts("world");
503+ printf("%s\n", "bla");
504+ puts(str_ar[0]);
505+ puts(str_ar[1]);
506+ puts(ss.a);
507+ puts(i >= 0 ? "one" : "two");
508+ puts(pa1);
509+ puts(pa2);
510+}
511+
470512 void ps(const char *s)
471513 {
472514 int c;
@@ -511,8 +553,10 @@ void string_test() · 38 unmodified lines
511553 num(b);
512554 b = b * 2;
513555 }
556+ string_test2();
514557 }
515558
559+
516560 void if1t(int n, int a, int b, int c)
517561 {
518562 if (a && b) printf("if1t: %d 1 %d %d\n", n, a, b);

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