xref: /llvm-project/clang/test/SemaCUDA/constexpr-variables.cu (revision 7e59223ac4b045178c287a56154113d5989572f4)
1 // RUN: %clang_cc1 -std=c++14 %s -triple nvptx64-nvidia-cuda \
2 // RUN:   -fcuda-is-device -verify -fsyntax-only
3 // RUN: %clang_cc1 -std=c++17 %s -triple nvptx64-nvidia-cuda \
4 // RUN:   -fcuda-is-device -verify -fsyntax-only
5 // RUN: %clang_cc1 -std=c++14 %s \
6 // RUN:   -triple x86_64-unknown-linux-gnu -verify -fsyntax-only
7 // RUN: %clang_cc1 -std=c++17 %s \
8 // RUN:   -triple x86_64-unknown-linux-gnu -verify -fsyntax-only
9 #include "Inputs/cuda.h"
10 
11 template<typename T>
foo(const T ** a)12 __host__ __device__ void foo(const T **a) {
13   // expected-note@-1 {{declared here}}
14   static const T b = sizeof(a);
15   static constexpr T c = sizeof(a);
16   const T d = sizeof(a);
17   constexpr T e = sizeof(a);
18   constexpr T f = **a;
19   // expected-error@-1 {{constexpr variable 'f' must be initialized by a constant expression}}
20   // expected-note@-2 {{}}
21   a[0] = &b;
22   a[1] = &c;
23   a[2] = &d;
24   a[3] = &e;
25 }
26 
device_fun(const int ** a)27 __device__ void device_fun(const int **a) {
28   // expected-note@-1 {{declared here}}
29   constexpr int b = sizeof(a);
30   static constexpr int c = sizeof(a);
31   constexpr int d = **a;
32   // expected-error@-1 {{constexpr variable 'd' must be initialized by a constant expression}}
33   // expected-note@-2 {{}}
34   a[0] = &b;
35   a[1] = &c;
36   foo(a);
37   // expected-note@-1 {{in instantiation of function template specialization 'foo<int>' requested here}}
38 }
39 
host_fun(const int ** a)40 void host_fun(const int **a) {
41   // expected-note@-1 {{declared here}}
42   constexpr int b = sizeof(a);
43   static constexpr int c = sizeof(a);
44   constexpr int d = **a;
45   // expected-error@-1 {{constexpr variable 'd' must be initialized by a constant expression}}
46   // expected-note@-2 {{}}
47   a[0] = &b;
48   a[1] = &c;
49   foo(a);
50 }
51 
host_device_fun(const int ** a)52 __host__ __device__ void host_device_fun(const int **a) {
53   // expected-note@-1 {{declared here}}
54   constexpr int b = sizeof(a);
55   static constexpr int c = sizeof(a);
56   constexpr int d = **a;
57   // expected-error@-1 {{constexpr variable 'd' must be initialized by a constant expression}}
58   // expected-note@-2 {{}}
59   a[0] = &b;
60   a[1] = &c;
61   foo(a);
62 }
63 
64 template <class T>
65 struct A {
66   explicit A() = default;
67 };
68 template <class T>
69 constexpr A<T> a{};
70 
71 struct B {
72   static constexpr bool value = true;
73 };
74 
75 template<typename T>
76 struct C {
77   static constexpr bool value = T::value;
78 };
79 
80 __constant__ const bool &x = C<B>::value;
81