Debian Codegraph @

clang/test/SemaObjCXX/objc-boxed-expressions-nsvalue.mm

raw
1 // RUN: %clang_cc1 -fsyntax-only -triple x86_64-apple-macosx10.9 -verify %s
2
3 #define BOXABLE __attribute__((objc_boxable))
4
5 typedef struct BOXABLE _NSPoint {
6 int dummy;
7 } NSPoint;
8
9 typedef struct BOXABLE _NSSize {
10 int dummy;
11 } NSSize;
12
13 typedef struct BOXABLE _NSRect {
14 int dummy;
15 } NSRect;
16
17 typedef struct BOXABLE _CGPoint {
18 int dummy;
19 } CGPoint;
20
21 typedef struct BOXABLE _CGSize {
22 int dummy;
23 } CGSize;
24
25 typedef struct BOXABLE _CGRect {
26 int dummy;
27 } CGRect;
28
29 typedef struct BOXABLE _NSRange {
30 int dummy;
31 } NSRange;
32
33 typedef struct BOXABLE _NSEdgeInsets {
34 int dummy;
35 } NSEdgeInsets;
36
37 typedef struct BOXABLE _NSEdgeInsets NSEdgeInsets;
38
39 typedef struct _SomeStruct {
40 double d;
41 } SomeStruct;
42
43 struct BOXABLE NonTriviallyCopyable {
44 double d;
45 NonTriviallyCopyable() {}
46 NonTriviallyCopyable(const NonTriviallyCopyable &obj) {}
47 };
48
49 void checkNSValueDiagnostic() {
50 NSRect rect;
51 id value = @(rect); // expected-error{{definition of class NSValue must be available to use Objective-C boxed expressions}}
52 }
53
54 @interface NSValue
55 + (NSValue *)valueWithBytes:(const void *)value objCType:(const char *)type;
56 @end
57
58 int main() {
59 NSPoint ns_point;
60 id ns_point_value = @(ns_point);
61
62 NSSize ns_size;
63 id ns_size_value = @(ns_size);
64
65 NSRect ns_rect;
66 id ns_rect_value = @(ns_rect);
67
68 CGPoint cg_point;
69 id cg_point_value = @(cg_point);
70
71 CGSize cg_size;
72 id cg_size_value = @(cg_size);
73
74 CGRect cg_rect;
75 id cg_rect_value = @(cg_rect);
76
77 NSRange ns_range;
78 id ns_range_value = @(ns_range);
79
80 NSEdgeInsets edge_insets;
81 id edge_insets_object = @(edge_insets);
82
83 SomeStruct s;
84 id err = @(s); // expected-error{{illegal type 'SomeStruct' (aka 'struct _SomeStruct') used in a boxed expression}}
85
86 NonTriviallyCopyable ntc;
87 id ntcErr = @(ntc); // expected-error{{non-trivially copyable type 'NonTriviallyCopyable' cannot be used in a boxed expression}}
88 }
89
90 CGRect getRect() {
91 CGRect r;
92 return r;
93 }
94
95 SomeStruct getSomeStruct() {
96 SomeStruct s;
97 return s;
98 }
99
100 void rvalue() {
101 id rv_rect = @(getRect());
102 id rv_some_struct = @(getSomeStruct()); // expected-error {{illegal type 'SomeStruct' (aka 'struct _SomeStruct') used in a boxed expression}}
103 }
104
105 template <class T> id box(T value) { return @(value); } // expected-error{{non-trivially copyable type 'NonTriviallyCopyable' cannot be used in a boxed expression}}
106 void test_template_1(NSRect rect, NonTriviallyCopyable ntc) {
107 id x = box(rect);
108 id y = box(ntc); // expected-note{{in instantiation of function template specialization 'box<NonTriviallyCopyable>' requested here}}
109 }
110
111 template <unsigned i> id boxRect(NSRect rect) { return @(rect); }
112 template <unsigned i> id boxNTC(NonTriviallyCopyable ntc) { return @(ntc); } // expected-error{{non-trivially copyable type 'NonTriviallyCopyable' cannot be used in a boxed expression}}
113 void test_template_2(NSRect rect, NonTriviallyCopyable ntc) {
114 id x = boxRect<0>(rect);
115 id y = boxNTC<0>(ntc);
116 }
117
118