[clang] Implement lifetime analysis for lifetime_capture_by(X) (#115921)
[llvm-project.git] / clang / test / SemaCXX / literal-type.cpp
blob88535c169fe01c3eaeca304997d582434bb50ed9
1 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
2 // RUN: %clang_cc1 -fsyntax-only -verify -std=c++20 %s
5 static_assert(__is_literal(int), "fail");
6 static_assert(__is_literal_type(int), "fail"); // alternate spelling for GCC
7 static_assert(__is_literal(void*), "fail");
8 enum E { E1 };
9 static_assert(__is_literal(E), "fail");
10 static_assert(__is_literal(decltype(E1)), "fail");
11 typedef int IAR[10];
12 static_assert(__is_literal(IAR), "fail");
13 typedef int Vector __attribute__((vector_size(16)));
14 typedef int VectorExt __attribute__((ext_vector_type(4)));
15 static_assert(__is_literal(Vector), "fail");
16 static_assert(__is_literal(VectorExt), "fail");
18 // C++0x [basic.types]p10:
19 // A type is a literal type if it is:
20 // [...]
21 // -- a class type that has all of the following properties:
22 // -- it has a trivial destructor
23 // -- every constructor call and full-expression in the
24 // brace-or-equal-initializers for non-static data members (if an) is
25 // a constant expression,
26 // -- it is an aggregate type or has at least one constexpr constructor
27 // or constructor template that is not a copy or move constructor, and
28 // [DR1452 adds class types with trivial default constructors to
29 // this list]
30 // -- it has all non-static data members and base classes of literal
31 // types
32 struct Empty {};
33 struct LiteralType {
34 int x;
35 E e;
36 IAR arr;
37 Empty empty;
38 int method();
40 struct HasDtor { ~HasDtor(); };
42 class NonAggregate { int x; };
43 struct NonLiteral { NonLiteral(); };
44 struct HasNonLiteralBase : NonLiteral {};
45 struct HasNonLiteralMember { HasDtor x; };
47 static_assert(__is_literal(Empty), "fail");
48 static_assert(__is_literal(LiteralType), "fail");
49 static_assert(__is_literal(NonAggregate), "fail");
50 static_assert(!__is_literal(NonLiteral), "fail");
51 static_assert(!__is_literal(HasDtor), "fail");
52 static_assert(!__is_literal(HasNonLiteralBase), "fail");
53 static_assert(!__is_literal(HasNonLiteralMember), "fail");
55 // DR1361 removes the brace-or-equal-initializer bullet so that we can allow:
56 extern int f(); // expected-note {{here}}
57 struct HasNonConstExprMemInit {
58 int x = f(); // expected-note {{non-constexpr function}}
59 constexpr HasNonConstExprMemInit() {} // expected-error {{never produces a constant expression}}
60 constexpr HasNonConstExprMemInit(int y) : x(y) {} // ok
62 static_assert(__is_literal(HasNonConstExprMemInit), "fail");
64 class HasConstExprCtor {
65 int x;
66 public:
67 constexpr HasConstExprCtor(int x) : x(x) {}
69 template <typename T> class HasConstExprCtorTemplate {
70 T x;
71 public:
72 template <typename U> constexpr HasConstExprCtorTemplate(U y) : x(y) {}
74 template <typename T> class HasConstExprCtorT {
75 constexpr HasConstExprCtorT(T) {}
77 static_assert(__is_literal(HasConstExprCtor), "fail");
78 static_assert(__is_literal(HasConstExprCtorTemplate<int>), "fail");
79 static_assert(__is_literal(HasConstExprCtorT<NonLiteral>), "fail");
82 #if __cplusplus >= 202003L
83 namespace GH77924 {
85 struct A { A(); };
86 template <class T>
87 struct opt {
88 union Data {
89 constexpr Data() : x{} {}
90 constexpr ~Data() {}
91 char x;
92 T data;
95 constexpr opt() : data{} {}
96 constexpr ~opt() { if (engaged) data.data.~T(); }
97 Data data;
98 bool engaged = false;
101 consteval void foo() {
102 opt<A> a;
105 void test() {
106 foo();
110 #endif