History

tccgen.c: In parse_btype, handle type qualifiers applied to arrays.

Also add some test cases in tests/tests2/39_typedef.c.

Edmund Grimley Evans committed 10 years ago

Commit f75f89f1 parent 541b3352 files · +41 −14Browse files

Unified · Split

2 files
  1. tccgen.c +25 −14
  2. tests/tests2/39_typedef.c +16 −0
tccgen.c+25−14
@@ -3134,6 +3134,17 @@ ST_FUNC int is_btype_size(int bt) · 3133 unmodified lines
31343134 return bt == VT_SHORT || bt == VT_LONG || bt == VT_LLONG;
31353135 }
31363136
3137+/* Add type qualifiers to a type. If the type is an array then the qualifiers
3138+ are added to the element type, copied because it could be a typedef. */
3139+static void parse_btype_qualify(CType *type, int qualifiers)
3140+{
3141+ while (type->t & VT_ARRAY) {
3142+ type->ref = sym_push(SYM_FIELD, &type->ref->type, 0, type->ref->c);
3143+ type = &type->ref->type;
3144+ }
3145+ type->t |= qualifiers;
3146+}
3147+
31373148 /* return 0 if no type declaration. otherwise, return the basic type
31383149 and skip it.
31393150 */
@@ -3232,13 +3243,17 @@ static int parse_btype(CType *type, AttributeDef *ad) · 92 unmodified lines
32323243 case TOK_CONST1:
32333244 case TOK_CONST2:
32343245 case TOK_CONST3:
3235− t |= VT_CONSTANT;
3246+ type->t = t;
3247+ parse_btype_qualify(type, VT_CONSTANT);
3248+ t = type->t;
32363249 next();
32373250 break;
32383251 case TOK_VOLATILE1:
32393252 case TOK_VOLATILE2:
32403253 case TOK_VOLATILE3:
3241− t |= VT_VOLATILE;
3254+ type->t = t;
3255+ parse_btype_qualify(type, VT_VOLATILE);
3256+ t = type->t;
32423257 next();
32433258 break;
32443259 case TOK_SIGNED1:
@@ -3309,18 +3324,14 @@ static int parse_btype(CType *type, AttributeDef *ad) · 64 unmodified lines
33093324 s = sym_find(tok);
33103325 if (!s || !(s->type.t & VT_TYPEDEF))
33113326 goto the_end;
3312− t |= (s->type.t & ~VT_TYPEDEF);
3313− if ((t & VT_ARRAY) &&
3314− t & (VT_CONSTANT | VT_VOLATILE) & ~s->type.ref->type.t) {
3315− /* This is a case like "typedef int T[1]; const T x;"
3316− in which which we must make a copy of the typedef
3317− type so that we can add the type qualifiers to it. */
3318− type->ref = sym_push(SYM_FIELD, &s->type.ref->type,
3319− 0, s->type.ref->c);
3320− type->ref->type.t |= t & (VT_CONSTANT | VT_VOLATILE);
3321− }
3322− else
3323− type->ref = s->type.ref;
3327+
3328+ type->t = ((s->type.t & ~VT_TYPEDEF) |
3329+ (t & ~(VT_CONSTANT | VT_VOLATILE)));
3330+ type->ref = s->type.ref;
3331+ if (t & (VT_CONSTANT | VT_VOLATILE))
3332+ parse_btype_qualify(type, t & (VT_CONSTANT | VT_VOLATILE));
3333+ t = type->t;
3334+
33243335 if (s->r) {
33253336 /* get attributes from typedef */
33263337 if (0 == ad->a.aligned)
tests/tests2/39_typedef.c+16−0
@@ -28,4 +28,20 @@ int main() · 27 unmodified lines
2828 return 0;
2929 }
3030
31+/* "If the specification of an array type includes any type qualifiers,
32+ the element type is so-qualified, not the array type." */
33+
34+typedef int A[3];
35+extern A const ca;
36+extern const A ca;
37+extern const int ca[3];
38+
39+typedef A B[1][2];
40+extern B const cb;
41+extern const B cb;
42+extern const int cb[1][2][3];
43+
44+extern B b;
45+extern int b[1][2][3];
46+
3147 /* vim: set expandtab ts=4 sw=3 sts=3 tw=80 :*/

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