History

Fix _Alignas

* don't accept _Alignas as type qualifier (after pointer '*'). * accept type-names within _Alignas * add testcases

Michael Matz committed 7 years ago

Commit 38a6aba1 parent fa0ef915 files · +52 −28Browse files

Unified · Split

5 files
  1. tccgen.c +17 −28
  2. tests/tests2/102_alignas.c +28 −0
  3. tests/tests2/102_alignas.expect +1 −0
  4. tests/tests2/60_errors_and_warnings.c +3 −0
  5. tests/tests2/60_errors_and_warnings.expect +3 −0
tccgen.c+17−28
@@ -4176,20 +4176,25 @@ static int parse_btype(CType *type, AttributeDef *ad) · 4175 unmodified lines
4176 goto basic_type;4176 goto basic_type;
4177 case TOK_ALIGNAS:4177 case TOK_ALIGNAS:
4178 { int n;4178 { int n;
4179 next();4179 AttributeDef ad1;
4180 /* TODO: _Alignas(type) -> _Alignas(_Alignof(type)) */4180 next();
4181 if (tok == '(') {4181 skip('(');
4182 next();4182 memset(&ad1, 0, sizeof(AttributeDef));
4183 n = expr_const();4183 if (parse_btype(&type1, &ad1)) {
4184 if (n <= 0 || (n & (n - 1)) != 0)4184 type_decl(&type1, &ad1, &n, TYPE_ABSTRACT);
4185 if (ad1.a.aligned)
4186 n = 1 << (ad1.a.aligned - 1);
4187 else
4188 type_size(&type1, &n);
4189 } else {
4190 n = expr_const();
4191 if (n <= 0 || (n & (n - 1)) != 0)
4185 tcc_error("alignment must be a positive power of two");4192 tcc_error("alignment must be a positive power of two");
4186 skip(')');4193 }
4187 } else {4194 skip(')');
4188 expect("(");4195 ad->a.aligned = exact_log2p1(n);
4189 }4196 }
4190 ad->a.aligned = exact_log2p1(n);4197 continue;
4191 }
4192 continue;
4193 case TOK_LONG:4198 case TOK_LONG:
4194 if ((t & VT_BTYPE) == VT_DOUBLE) {4199 if ((t & VT_BTYPE) == VT_DOUBLE) {
4195 t = (t & ~(VT_BTYPE|VT_LONG)) | VT_LDOUBLE;4200 t = (t & ~(VT_BTYPE|VT_LONG)) | VT_LDOUBLE;
@@ -4594,22 +4599,6 @@ static CType *type_decl(CType *type, AttributeDef *ad, int *v, int td) · 398 unmodified lines
4594 case TOK_RESTRICT2:4599 case TOK_RESTRICT2:
4595 case TOK_RESTRICT3:4600 case TOK_RESTRICT3:
4596 goto redo;4601 goto redo;
4597 case TOK_ALIGNAS:
4598 { int n;
4599 next();
4600 /* TODO: _Alignas(type) -> _Alignas(_Alignof(type)) */
4601 if (tok == '(') {
4602 next();
4603 n = expr_const();
4604 if (n <= 0 || (n & (n - 1)) != 0)
4605 tcc_error("alignment must be a positive power of two");
4606 skip(')');
4607 } else {
4608 expect("(");
4609 }
4610 ad->a.aligned = exact_log2p1(n);
4611 }
4612 break;
4613 /* XXX: clarify attribute handling */4602 /* XXX: clarify attribute handling */
4614 case TOK_ATTRIBUTE1:4603 case TOK_ATTRIBUTE1:
4615 case TOK_ATTRIBUTE2:4604 case TOK_ATTRIBUTE2:
tests/tests2/102_alignas.c+28−0
@@ -0,0 +1,28 @@
1_Alignas(16) int i1;
2int _Alignas(16) i2;
3void _Alignas(16) *p2;
4_Alignas(16) i3;
5int _Alignas(double) i4;
6int _Alignas(int) i5;
7#if 0
8/* The following are currently wrongly accepted by TCC but really shouldn't. */
9int _Alignas(int _Alignas(16)) i6; //wrong, 'int _Alignas(16)' is no type-name
10typedef int _Alignas(16) int16aligned_t; //wrong, _Alignas invalid on typedef
11int16aligned_t i7;
12#endif
13/* i8 should get an alignment of 16, because unlike _Alignas the
14 corresponding attribute _does_ apply to type-name, though not in
15 some clang versions. */
16int _Alignas(int __attribute__((aligned(16)))) i8;
17extern int printf(const char*, ...);
18#ifdef _MSC_VER
19#define alignof(x) (int)__alignof(x)
20#else
21#define alignof(x) (int)__alignof__(x)
22#endif
23int main()
24{
25 printf("%d %d %d %d\n",
26 alignof(i1), alignof(i4), alignof(i5), alignof(i8));
27 return 0;
28}
tests/tests2/102_alignas.expect+1−0
@@ -0,0 +1 @@
116 8 4
tests/tests2/60_errors_and_warnings.c+3−0
@@ -172,4 +172,7 @@ int X=1; · 171 unmodified lines
172int main(void) {172int main(void) {
173 int t[][][X];173 int t[][][X];
174}174}
175#elif defined test_invalid_alignas
176/* _Alignas is no type qualifier */
177void * _Alignas(16) p1;
175#endif178#endif
tests/tests2/60_errors_and_warnings.expect+3−0
@@ -80,3 +80,6 @@ · 79 unmodified lines
80 80
81[test_vla_1]81[test_vla_1]
8260_errors_and_warnings.c:173: error: need explicit inner array size in VLAs8260_errors_and_warnings.c:173: error: need explicit inner array size in VLAs
83
84[test_invalid_alignas]
8560_errors_and_warnings.c:177: error: identifier expected

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