blob: 445188372f5ee63332553bd3e9f6f542c450e1cf [file] [log] [blame]
Justin Lebar18e2d822016-08-15 23:00:49 +00001// RUN: %clang_cc1 %s --std=c++11 -triple nvptx-unknown-unknown -fcuda-is-device -emit-llvm -o - -verify
2
3// Note: This test won't work with -fsyntax-only, because some of these errors
4// are emitted during codegen.
5
6#include "Inputs/cuda.h"
7
8extern "C" void host_fn() {}
Justin Lebar26bb3112016-08-16 00:48:21 +00009// expected-note@-1 {{'host_fn' declared here}}
10// expected-note@-2 {{'host_fn' declared here}}
11// expected-note@-3 {{'host_fn' declared here}}
12// expected-note@-4 {{'host_fn' declared here}}
13// expected-note@-5 {{'host_fn' declared here}}
14// expected-note@-6 {{'host_fn' declared here}}
Justin Lebar18e2d822016-08-15 23:00:49 +000015
16struct S {
17 S() {}
Justin Lebar26bb3112016-08-16 00:48:21 +000018 // expected-note@-1 {{'S' declared here}}
19 // expected-note@-2 {{'S' declared here}}
Justin Lebar18e2d822016-08-15 23:00:49 +000020 ~S() { host_fn(); }
Justin Lebar26bb3112016-08-16 00:48:21 +000021 // expected-note@-1 {{'~S' declared here}}
Justin Lebar18e2d822016-08-15 23:00:49 +000022 int x;
23};
24
25struct T {
26 __host__ __device__ void hd() { host_fn(); }
27 // expected-error@-1 {{reference to __host__ function 'host_fn' in __host__ __device__ function}}
28
29 // No error; this is (implicitly) inline and is never called, so isn't
30 // codegen'ed.
31 __host__ __device__ void hd2() { host_fn(); }
32
33 __host__ __device__ void hd3();
34
35 void h() {}
Justin Lebar26bb3112016-08-16 00:48:21 +000036 // expected-note@-1 {{'h' declared here}}
Justin Lebar18e2d822016-08-15 23:00:49 +000037};
38
39__host__ __device__ void T::hd3() {
40 host_fn();
41 // expected-error@-1 {{reference to __host__ function 'host_fn' in __host__ __device__ function}}
42}
43
44template <typename T> __host__ __device__ void hd2() { host_fn(); }
45// expected-error@-1 {{reference to __host__ function 'host_fn' in __host__ __device__ function}}
46__global__ void kernel() { hd2<int>(); }
47
48__host__ __device__ void hd() { host_fn(); }
49// expected-error@-1 {{reference to __host__ function 'host_fn' in __host__ __device__ function}}
50
51template <typename T> __host__ __device__ void hd3() { host_fn(); }
52// expected-error@-1 {{reference to __host__ function 'host_fn' in __host__ __device__ function}}
53__device__ void device_fn() { hd3<int>(); }
54
55// No error because this is never instantiated.
56template <typename T> __host__ __device__ void hd4() { host_fn(); }
57
58__host__ __device__ void local_var() {
59 S s;
60 // expected-error@-1 {{reference to __host__ function 'S' in __host__ __device__ function}}
61}
62
63__host__ __device__ void placement_new(char *ptr) {
64 ::new(ptr) S();
65 // expected-error@-1 {{reference to __host__ function 'S' in __host__ __device__ function}}
66}
67
68__host__ __device__ void explicit_destructor(S *s) {
69 s->~S();
70 // expected-error@-1 {{reference to __host__ function '~S' in __host__ __device__ function}}
71}
72
73__host__ __device__ void hd_member_fn() {
74 T t;
75 // Necessary to trigger an error on T::hd. It's (implicitly) inline, so
76 // isn't codegen'ed until we call it.
77 t.hd();
78}
79
80__host__ __device__ void h_member_fn() {
81 T t;
82 t.h();
83 // expected-error@-1 {{reference to __host__ function 'h' in __host__ __device__ function}}
84}
85
86__host__ __device__ void fn_ptr() {
87 auto* ptr = &host_fn;
88 // expected-error@-1 {{reference to __host__ function 'host_fn' in __host__ __device__ function}}
89}
90
91template <typename T>
92__host__ __device__ void fn_ptr_template() {
93 auto* ptr = &host_fn; // Not an error because the template isn't instantiated.
94}