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
41764176 goto basic_type;
41774177 case TOK_ALIGNAS:
41784178 { int n;
4179− next();
4180− /* TODO: _Alignas(type) -> _Alignas(_Alignof(type)) */
4181− if (tok == '(') {
4182− next();
4183− n = expr_const();
4184− if (n <= 0 || (n & (n - 1)) != 0)
4179+ AttributeDef ad1;
4180+ next();
4181+ skip('(');
4182+ memset(&ad1, 0, sizeof(AttributeDef));
4183+ if (parse_btype(&type1, &ad1)) {
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)
41854192 tcc_error("alignment must be a positive power of two");
4186− skip(')');
4187− } else {
4188− expect("(");
4193+ }
4194+ skip(')');
4195+ ad->a.aligned = exact_log2p1(n);
41894196 }
4190− ad->a.aligned = exact_log2p1(n);
4191− }
4192− continue;
4197+ continue;
41934198 case TOK_LONG:
41944199 if ((t & VT_BTYPE) == VT_DOUBLE) {
41954200 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
45944599 case TOK_RESTRICT2:
45954600 case TOK_RESTRICT3:
45964601 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;
46134602 /* XXX: clarify attribute handling */
46144603 case TOK_ATTRIBUTE1:
46154604 case TOK_ATTRIBUTE2:
tests/tests2/102_alignas.c+28−0
@@ -0,0 +1,28 @@
1+_Alignas(16) int i1;
2+int _Alignas(16) i2;
3+void _Alignas(16) *p2;
4+_Alignas(16) i3;
5+int _Alignas(double) i4;
6+int _Alignas(int) i5;
7+#if 0
8+/* The following are currently wrongly accepted by TCC but really shouldn't. */
9+int _Alignas(int _Alignas(16)) i6; //wrong, 'int _Alignas(16)' is no type-name
10+typedef int _Alignas(16) int16aligned_t; //wrong, _Alignas invalid on typedef
11+int16aligned_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. */
16+int _Alignas(int __attribute__((aligned(16)))) i8;
17+extern 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
23+int 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 @@
1+16 8 4
tests/tests2/60_errors_and_warnings.c+3−0
@@ -172,4 +172,7 @@ int X=1; · 171 unmodified lines
172172 int main(void) {
173173 int t[][][X];
174174 }
175+#elif defined test_invalid_alignas
176+/* _Alignas is no type qualifier */
177+void * _Alignas(16) p1;
175178 #endif
tests/tests2/60_errors_and_warnings.expect+3−0
@@ -80,3 +80,6 @@ · 79 unmodified lines
8080
8181 [test_vla_1]
8282 60_errors_and_warnings.c:173: error: need explicit inner array size in VLAs
83+
84+[test_invalid_alignas]
85+60_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