History

patch type_to_str to handle complex function-ptr decls better

Code like: #include <signal.h> int main() { _Generic(signal, int: 0); } should fail with error: type 'extern void (*(int, void (*)(int)))(int)' does not match any association not error: type 'extern void *(int)(int, void *(int))' does not match any association [matz: fix formatting, fix function-to-pointer decay for operands of _Generic, add testcase for this]

Petr Skocik committed 8 years ago

Commit d6d3cf01 parent f0a25ca3 files · +27 −8Browse files

Unified · Split

3 files
  1. tccgen.c +17 −7
  2. tests/tests2/94_generic.c +8 −0
  3. tests/tests2/94_generic.expect +2 −1
tccgen.c+17−7
@@ -19,7 +19,6 @@ · 18 unmodified lines
1919 */
2020
2121 #include "tcc.h"
22−
2322 /********************************************************/
2423 /* global variables */
2524
@@ -2938,17 +2937,26 @@ static void type_to_str(char *buf, int buf_size, · 2912 unmodified lines
29382937 break;
29392938 case VT_FUNC:
29402939 s = type->ref;
2941− type_to_str(buf, buf_size, &s->type, varstr);
2942− pstrcat(buf, buf_size, "(");
2940+ buf1[0]=0;
2941+ if (varstr && '*' == *varstr) {
2942+ pstrcat(buf1, sizeof(buf1), "(");
2943+ pstrcat(buf1, sizeof(buf1), varstr);
2944+ pstrcat(buf1, sizeof(buf1), ")");
2945+ }
2946+ pstrcat(buf1, buf_size, "(");
29432947 sa = s->next;
29442948 while (sa != NULL) {
2945− type_to_str(buf1, sizeof(buf1), &sa->type, NULL);
2946− pstrcat(buf, buf_size, buf1);
2949+ char buf2[256];
2950+ type_to_str(buf2, sizeof(buf2), &sa->type, NULL);
2951+ pstrcat(buf1, sizeof(buf1), buf2);
29472952 sa = sa->next;
29482953 if (sa)
2949− pstrcat(buf, buf_size, ", ");
2954+ pstrcat(buf1, sizeof(buf1), ", ");
29502955 }
2951− pstrcat(buf, buf_size, ")");
2956+ if (s->f.func_type == FUNC_ELLIPSIS)
2957+ pstrcat(buf1, sizeof(buf1), ", ...");
2958+ pstrcat(buf1, sizeof(buf1), ")");
2959+ type_to_str(buf, buf_size, &s->type, buf1);
29522960 goto no_var;
29532961 case VT_PTR:
29542962 s = type->ref;
@@ -5005,6 +5013,8 @@ ST_FUNC void unary(void) · 2050 unmodified lines
50055013 skip('(');
50065014 expr_type(&controlling_type, expr_eq);
50075015 controlling_type.t &= ~(VT_CONSTANT | VT_VOLATILE | VT_ARRAY);
5016+ if ((controlling_type.t & VT_BTYPE) == VT_FUNC)
5017+ mk_pointer(&controlling_type);
50085018 for (;;) {
50095019 learn = 0;
50105020 skip(',');
tests/tests2/94_generic.c+8−0
@@ -20,6 +20,12 @@ int b_f() · 19 unmodified lines
2020 return 10;
2121 }
2222
23+typedef int (*fptr)(int);
24+int foo(int i)
25+{
26+ return i;
27+}
28+
2329 typedef int int_type1;
2430
2531 #define gen_sw(a) _Generic(a, const char *: 1, default: 8, int: 123);
@@ -60,5 +66,7 @@ int main() · 34 unmodified lines
6066 long long: "long long"));
6167 i = _Generic(l, long: 1, int: 2);
6268 printf("%d\n", i);
69+ i = _Generic(foo, fptr: 3, int: 4);
70+ printf("%d\n", i);
6371 return 0;
6472 }
tests/tests2/94_generic.expect+2−1
@@ -10,4 +10,5 @@ · 9 unmodified lines
1010 3
1111 4
1212 long
13−1
13+1
14+3

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