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
19 */19 */
20 20
21#include "tcc.h"21#include "tcc.h"
22
23/********************************************************/22/********************************************************/
24/* global variables */23/* global variables */
25 24
@@ -2938,17 +2937,26 @@ static void type_to_str(char *buf, int buf_size, · 2912 unmodified lines
2938 break;2937 break;
2939 case VT_FUNC:2938 case VT_FUNC:
2940 s = type->ref;2939 s = type->ref;
2941 type_to_str(buf, buf_size, &s->type, varstr);2940 buf1[0]=0;
2942 pstrcat(buf, buf_size, "(");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, "(");
2943 sa = s->next;2947 sa = s->next;
2944 while (sa != NULL) {2948 while (sa != NULL) {
2945 type_to_str(buf1, sizeof(buf1), &sa->type, NULL);2949 char buf2[256];
2946 pstrcat(buf, buf_size, buf1);2950 type_to_str(buf2, sizeof(buf2), &sa->type, NULL);
2951 pstrcat(buf1, sizeof(buf1), buf2);
2947 sa = sa->next;2952 sa = sa->next;
2948 if (sa)2953 if (sa)
2949 pstrcat(buf, buf_size, ", ");2954 pstrcat(buf1, sizeof(buf1), ", ");
2950 }2955 }
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);
2952 goto no_var;2960 goto no_var;
2953 case VT_PTR:2961 case VT_PTR:
2954 s = type->ref;2962 s = type->ref;
@@ -5005,6 +5013,8 @@ ST_FUNC void unary(void) · 2050 unmodified lines
5005 skip('(');5013 skip('(');
5006 expr_type(&controlling_type, expr_eq);5014 expr_type(&controlling_type, expr_eq);
5007 controlling_type.t &= ~(VT_CONSTANT | VT_VOLATILE | VT_ARRAY);5015 controlling_type.t &= ~(VT_CONSTANT | VT_VOLATILE | VT_ARRAY);
5016 if ((controlling_type.t & VT_BTYPE) == VT_FUNC)
5017 mk_pointer(&controlling_type);
5008 for (;;) {5018 for (;;) {
5009 learn = 0;5019 learn = 0;
5010 skip(',');5020 skip(',');
tests/tests2/94_generic.c+8−0
@@ -20,6 +20,12 @@ int b_f() · 19 unmodified lines
20 return 10;20 return 10;
21}21}
22 22
23typedef int (*fptr)(int);
24int foo(int i)
25{
26 return i;
27}
28
23typedef int int_type1;29typedef int int_type1;
24 30
25#define gen_sw(a) _Generic(a, const char *: 1, default: 8, int: 123);31#define gen_sw(a) _Generic(a, const char *: 1, default: 8, int: 123);
@@ -60,5 +66,7 @@ int main() · 34 unmodified lines
60 long long: "long long"));66 long long: "long long"));
61 i = _Generic(l, long: 1, int: 2);67 i = _Generic(l, long: 1, int: 2);
62 printf("%d\n", i);68 printf("%d\n", i);
69 i = _Generic(foo, fptr: 3, int: 4);
70 printf("%d\n", i);
63 return 0;71 return 0;
64}72}
tests/tests2/94_generic.expect+2−1
@@ -10,4 +10,5 @@ · 9 unmodified lines
103103
114114
12long12long
131131
143

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