xref: /llvm-project/clang/test/SemaCXX/ms-property.cpp (revision db93ef14aef9c572e02bc842762bc4d0278148f9)
1 // RUN: %clang_cc1 -ast-print -verify -triple=x86_64-pc-win32 -fms-compatibility %s -o - | FileCheck %s
2 // RUN: %clang_cc1 -triple=x86_64-pc-win32 -fms-compatibility -emit-pch -o %t -verify %s
3 // RUN: %clang_cc1 -triple=x86_64-pc-win32 -fms-compatibility -include-pch %t %s -ast-print -o - | FileCheck %s
4 // RUN: %clang_cc1 -fdeclspec -fsyntax-only -verify %s -std=c++23
5 
6 #ifndef HEADER
7 #define HEADER
8 
9 class Test1 {
10 private:
11   int x_;
12 
13 public:
14   Test1(int x) : x_(x) {}
15   __declspec(property(get = get_x)) int X;
16   int get_x() const { return x_; }
17   static Test1 *GetTest1() { return new Test1(10); }
18 };
19 
20 class S {
21 public:
22   __declspec(property(get=GetX,put=PutX)) int x[];
23   int GetX(int i, int j) { return i+j; }
24   void PutX(int i, int j, int k) { j = i = k; }
25   __declspec(property(get=GetY,put=PutY)) int y[][];
26   int GetY(int i, int j) { return i+j; }
27   void PutY(int i, int j, int k) { j = i = k; }
28   __declspec(property(get=GetZ,put=PutZ)) int z[][][];
29   int GetZ(int i, int j, int k);
30   void PutZ(int i, int j, int k, int val);
31 };
32 
33 template <typename T>
34 class St {
35 public:
36   __declspec(property(get=GetX,put=PutX)) T x[];
37   T GetX(T i, T j) { return i+j; }
38   T PutX(T i, T j, T k) { return j = i = k; }
39   __declspec(property(get=GetY,put=PutY)) T y[][];
40   T GetY(T i, T j) { return i+j; }
41   T PutY(T i, T j, T k) { return j = i = k; }
42   __declspec(property(get=GetZ,put=PutZ)) T z[][][];
43   T GetZ(T i, T j, T k) { return i+j+k; }
44   T PutZ(T i, T j, T k, T v) { return j = i = k = v; }
45   ~St() { x[0][0] = x[1][1]; y[0][0] = x[1][1]; z[0][1][2] = z[2][1][0]; }
46 };
47 
48 // CHECK: this->x[0][0] = this->x[1][1];
49 // CHECK: this->y[0][0] = this->x[1][1];
50 // CHECK: this->z[0][1][2] = this->z[2][1][0];
51 // CHECK: this->x[0][0] = this->x[1][1];
52 // CHECK: this->y[0][0] = this->x[1][1];
53 // CHECK: this->z[0][1][2] = this->z[2][1][0];
54 
55 // CHECK-LABEL: main
56 int main(int argc, char **argv) {
57   S *p1 = 0;
58   St<float> *p2 = 0;
59   // CHECK: St<int> a;
60   St<int> a;
61   // CHECK-NEXT: int j = (p1->x)[223][11];
62   int j = (p1->x)[223][11];
63   // CHECK-NEXT: (p1->x[23])[1] = j;
64   (p1->x[23])[1] = j;
65   // CHECK-NEXT: int k = (p1->y)[223][11];
66   int k = (p1->y)[223][11];
67   // CHECK-NEXT: (p1->y[23])[1] = k;
68   (p1->y[23])[1] = k;
69   // CHECK-NEXT: int k3 = p1->z[1][2][3];
70   int k3 = p1->z[1][2][3];
71   // CHECK-NEXT: p1->z[0][2][1] = k3;
72   p1->z[0][2][1] = k3;
73   // CHECK-NEXT: float j1 = (p2->x[223][11]);
74   float j1 = (p2->x[223][11]);
75   // CHECK-NEXT: ((p2->x)[23])[1] = j1;
76   ((p2->x)[23])[1] = j1;
77   // CHECK-NEXT: float k1 = (p2->y[223][11]);
78   float k1 = (p2->y[223][11]);
79   // CHECK-NEXT: ((p2->y)[23])[1] = k1;
80   ((p2->y)[23])[1] = k1;
81   // CHECK-NEXT: ++(((p2->x)[23])[1]);
82   ++(((p2->x)[23])[1]);
83   // CHECK-NEXT: j1 = ((p2->x)[23])[1] = j1;
84   j1 = ((p2->x)[23])[1] = j1;
85   // CHECK-NEXT: return Test1::GetTest1()->X;
86   return Test1::GetTest1()->X;
87 }
88 
89 struct X {
90   int implicit_object_member_function() { return 0; }
91   static int static_member_function() { return 0; }
92 
93   __declspec(property(get=implicit_object_member_function)) int imp;
94   __declspec(property(get=static_member_function)) int st;
95 
96 #if __cplusplus >= 202302L
97   int explicit_object_member_function(this X self) { return 0; }
98   __declspec(property(get=explicit_object_member_function)) int exp;
99 #endif
100 };
101 
102 [[nodiscard]] X get_x();
103 void f() {
104   (void) get_x().imp;
105   (void) get_x().st;
106   // expected-warning@-1 {{ignoring return value of function declared with 'nodiscard' attribute}}
107 #if __cplusplus >= 202302L
108   (void) get_x().exp;
109 #endif
110 }
111 
112 #if __cplusplus >= 202302L
113 struct Y {
114   Y() = default;
115   Y(const Y&) = delete;
116   int explicit_object_member_function(this Y) { return 0; }
117   __declspec(property(get = explicit_object_member_function)) int prop;
118 };
119 void g() {
120   (void) Y().prop;
121 }
122 #endif
123 
124 #endif // HEADER
125