annotate clang/test/SemaCXX/alignof.cpp @ 222:81f6424ef0e3 llvm-original

LLVM original branch
author Shinji KONO <kono@ie.u-ryukyu.ac.jp>
date Sun, 18 Jul 2021 22:10:01 +0900
parents 79ff65ed7e25
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
150
anatofuz
parents:
diff changeset
1 // RUN: %clang_cc1 -std=c++11 -fsyntax-only -verify %s
anatofuz
parents:
diff changeset
2
anatofuz
parents:
diff changeset
3 // rdar://13784901
anatofuz
parents:
diff changeset
4
anatofuz
parents:
diff changeset
5 struct S0 {
anatofuz
parents:
diff changeset
6 int x;
anatofuz
parents:
diff changeset
7 static const int test0 = __alignof__(x); // expected-error {{invalid application of 'alignof' to a field of a class still being defined}}
anatofuz
parents:
diff changeset
8 static const int test1 = __alignof__(S0::x); // expected-error {{invalid application of 'alignof' to a field of a class still being defined}}
anatofuz
parents:
diff changeset
9 auto test2() -> char(&)[__alignof__(x)]; // expected-error {{invalid application of 'alignof' to a field of a class still being defined}}
anatofuz
parents:
diff changeset
10 };
anatofuz
parents:
diff changeset
11
anatofuz
parents:
diff changeset
12 struct S1; // expected-note 6 {{forward declaration}}
anatofuz
parents:
diff changeset
13 extern S1 s1;
anatofuz
parents:
diff changeset
14 const int test3 = __alignof__(s1); // expected-error {{invalid application of '__alignof' to an incomplete type 'S1'}}
anatofuz
parents:
diff changeset
15
anatofuz
parents:
diff changeset
16 struct S2 {
anatofuz
parents:
diff changeset
17 S2();
anatofuz
parents:
diff changeset
18 S1 &s;
anatofuz
parents:
diff changeset
19 int x;
anatofuz
parents:
diff changeset
20
anatofuz
parents:
diff changeset
21 int test4 = __alignof__(x); // ok
anatofuz
parents:
diff changeset
22 int test5 = __alignof__(s); // expected-error {{invalid application of '__alignof' to an incomplete type 'S1'}}
anatofuz
parents:
diff changeset
23 };
anatofuz
parents:
diff changeset
24
anatofuz
parents:
diff changeset
25 const int test6 = __alignof__(S2::x);
anatofuz
parents:
diff changeset
26 const int test7 = __alignof__(S2::s); // expected-error {{invalid application of '__alignof' to an incomplete type 'S1'}}
anatofuz
parents:
diff changeset
27
anatofuz
parents:
diff changeset
28 // Arguably, these should fail like the S1 cases do: the alignment of
anatofuz
parents:
diff changeset
29 // 's2.x' should depend on the alignment of both x-within-S2 and
anatofuz
parents:
diff changeset
30 // s2-within-S3 and thus require 'S3' to be complete. If we start
anatofuz
parents:
diff changeset
31 // doing the appropriate recursive walk to do that, we should make
anatofuz
parents:
diff changeset
32 // sure that these cases don't explode.
anatofuz
parents:
diff changeset
33 struct S3 {
anatofuz
parents:
diff changeset
34 S2 s2;
anatofuz
parents:
diff changeset
35
anatofuz
parents:
diff changeset
36 static const int test8 = __alignof__(s2.x);
anatofuz
parents:
diff changeset
37 static const int test9 = __alignof__(s2.s); // expected-error {{invalid application of '__alignof' to an incomplete type 'S1'}}
anatofuz
parents:
diff changeset
38 auto test10() -> char(&)[__alignof__(s2.x)];
anatofuz
parents:
diff changeset
39 static const int test11 = __alignof__(S3::s2.x);
anatofuz
parents:
diff changeset
40 static const int test12 = __alignof__(S3::s2.s); // expected-error {{invalid application of '__alignof' to an incomplete type 'S1'}}
anatofuz
parents:
diff changeset
41 auto test13() -> char(&)[__alignof__(s2.x)];
anatofuz
parents:
diff changeset
42 };
anatofuz
parents:
diff changeset
43
anatofuz
parents:
diff changeset
44 // Same reasoning as S3.
anatofuz
parents:
diff changeset
45 struct S4 {
anatofuz
parents:
diff changeset
46 union {
anatofuz
parents:
diff changeset
47 int x;
anatofuz
parents:
diff changeset
48 };
anatofuz
parents:
diff changeset
49 static const int test0 = __alignof__(x);
anatofuz
parents:
diff changeset
50 static const int test1 = __alignof__(S0::x);
anatofuz
parents:
diff changeset
51 auto test2() -> char(&)[__alignof__(x)];
anatofuz
parents:
diff changeset
52 };
anatofuz
parents:
diff changeset
53
anatofuz
parents:
diff changeset
54 // Regression test for asking for the alignment of a field within an invalid
anatofuz
parents:
diff changeset
55 // record.
anatofuz
parents:
diff changeset
56 struct S5 {
anatofuz
parents:
diff changeset
57 S1 s; // expected-error {{incomplete type}}
anatofuz
parents:
diff changeset
58 int x;
anatofuz
parents:
diff changeset
59 };
anatofuz
parents:
diff changeset
60 const int test8 = __alignof__(S5::x);
anatofuz
parents:
diff changeset
61
anatofuz
parents:
diff changeset
62 int test14[2];
anatofuz
parents:
diff changeset
63
anatofuz
parents:
diff changeset
64 static_assert(alignof(test14) == 4, "foo"); // expected-warning {{'alignof' applied to an expression is a GNU extension}}
anatofuz
parents:
diff changeset
65
anatofuz
parents:
diff changeset
66 // PR19992
anatofuz
parents:
diff changeset
67 static_assert(alignof(int[]) == alignof(int), ""); // ok
anatofuz
parents:
diff changeset
68
anatofuz
parents:
diff changeset
69 namespace alignof_array_expr {
anatofuz
parents:
diff changeset
70 alignas(32) extern int n[];
anatofuz
parents:
diff changeset
71 static_assert(alignof(n) == 32, ""); // expected-warning {{GNU extension}}
anatofuz
parents:
diff changeset
72
anatofuz
parents:
diff changeset
73 template<int> struct S {
anatofuz
parents:
diff changeset
74 static int a[];
anatofuz
parents:
diff changeset
75 };
anatofuz
parents:
diff changeset
76 template<int N> int S<N>::a[N];
anatofuz
parents:
diff changeset
77 // ok, does not complete type of S<-1>::a
anatofuz
parents:
diff changeset
78 static_assert(alignof(S<-1>::a) == alignof(int), ""); // expected-warning {{GNU extension}}
anatofuz
parents:
diff changeset
79 }
anatofuz
parents:
diff changeset
80
anatofuz
parents:
diff changeset
81 template <typename T> void n(T) {
anatofuz
parents:
diff changeset
82 alignas(T) int T1;
anatofuz
parents:
diff changeset
83 char k[__alignof__(T1)];
anatofuz
parents:
diff changeset
84 static_assert(sizeof(k) == alignof(long long), "");
anatofuz
parents:
diff changeset
85 }
anatofuz
parents:
diff changeset
86 template void n(long long);
anatofuz
parents:
diff changeset
87
anatofuz
parents:
diff changeset
88 namespace PR22042 {
anatofuz
parents:
diff changeset
89 template <typename T>
anatofuz
parents:
diff changeset
90 void Fun(T A) {
anatofuz
parents:
diff changeset
91 typedef int __attribute__((__aligned__(A))) T1; // expected-error {{requested alignment is dependent but declaration is not dependent}}
anatofuz
parents:
diff changeset
92 int k1[__alignof__(T1)];
anatofuz
parents:
diff changeset
93 }
anatofuz
parents:
diff changeset
94
anatofuz
parents:
diff changeset
95 template <int N>
anatofuz
parents:
diff changeset
96 struct S {
anatofuz
parents:
diff changeset
97 typedef __attribute__((aligned(N))) int Field[sizeof(N)]; // expected-error {{requested alignment is dependent but declaration is not dependent}}
anatofuz
parents:
diff changeset
98 };
anatofuz
parents:
diff changeset
99 }
anatofuz
parents:
diff changeset
100
anatofuz
parents:
diff changeset
101 typedef int __attribute__((aligned(16))) aligned_int;
anatofuz
parents:
diff changeset
102 template <typename>
anatofuz
parents:
diff changeset
103 using template_alias = aligned_int;
anatofuz
parents:
diff changeset
104 static_assert(alignof(template_alias<void>) == 16, "Expected alignment of 16" );
221
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
105
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
106 struct PR47138 {
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
107 invalid_type a; // expected-error {{unknown type}}
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
108 };
79ff65ed7e25 LLVM12 Original
Shinji KONO <kono@ie.u-ryukyu.ac.jp>
parents: 150
diff changeset
109 static_assert(__alignof__(PR47138) == 1, ""); // Don't crash.