C++: show private field accessor hints for const accesses (PR c++/84892)
[official-gcc.git] / gcc / testsuite / g++.dg / other / accessor-fixits-1.C
blobfd46a522486c275780beb602e1c5033f2c2f941b
1 // { dg-options "-fdiagnostics-show-caret" }
3 class t1
5 public:
6   int get_color () const { return m_color; }
7   int get_shape () const { return m_shape; }
9 private:
10   int m_color;
12 protected:
13   int m_shape;
16 int test_access_t1_color (t1 &ref)
18   return ref.m_color; // { dg-error ".int t1::m_color. is private within this context" }
19   /* { dg-begin-multiline-output "" }
20    return ref.m_color;
21               ^~~~~~~
22      { dg-end-multiline-output "" } */
24   // { dg-message "declared private here" "" { target *-*-* } 10 }
25   /* { dg-begin-multiline-output "" }
26    int m_color;
27        ^~~~~~~
28      { dg-end-multiline-output "" } */
30   // { dg-message "field .int t1::m_color. can be accessed via .int t1::get_color\\(\\) const." "" { target *-*-* } .-12 }
31   /* { dg-begin-multiline-output "" }
32    return ref.m_color;
33               ^~~~~~~
34               get_color()
35      { dg-end-multiline-output "" } */
38 int test_access_const_t1_color (const t1 &ref)
40   return ref.m_color; // { dg-error ".int t1::m_color. is private within this context" }
41   /* { dg-begin-multiline-output "" }
42    return ref.m_color;
43               ^~~~~~~
44      { dg-end-multiline-output "" } */
47   /* { dg-begin-multiline-output "" }
48    int m_color;
49        ^~~~~~~
50      { dg-end-multiline-output "" } */
52   // { dg-message "field .int t1::m_color. can be accessed via .int t1::get_color\\(\\) const." "" { target *-*-* } .-12 }
53   /* { dg-begin-multiline-output "" }
54    return ref.m_color;
55               ^~~~~~~
56               get_color()
57      { dg-end-multiline-output "" } */
60 int test_access_t1_shape (t1 &ref)
62   return ref.m_shape; // { dg-error ".int t1::m_shape. is protected within this context" }
63   /* { dg-begin-multiline-output "" }
64    return ref.m_shape;
65               ^~~~~~~
66      { dg-end-multiline-output "" } */
68   // { dg-message "declared protected here" "" { target *-*-* } 13 }
69   /* { dg-begin-multiline-output "" }
70    int m_shape;
71        ^~~~~~~
72      { dg-end-multiline-output "" } */
74   // { dg-message "field .int t1::m_shape. can be accessed via .int t1::get_shape\\(\\) const." "" { target *-*-* } .-12 }
75   /* { dg-begin-multiline-output "" }
76    return ref.m_shape;
77               ^~~~~~~
78               get_shape()
79      { dg-end-multiline-output "" } */
82 int test_deref_t1_color (t1 *ptr)
84   return ptr->m_color; // { dg-error ".int t1::m_color. is private within this context" }
85   /* { dg-begin-multiline-output "" }
86    return ptr->m_color;
87                ^~~~~~~
88      { dg-end-multiline-output "" } */
91   /* { dg-begin-multiline-output "" }
92    int m_color;
93        ^~~~~~~
94      { dg-end-multiline-output "" } */
96   // { dg-message "field .int t1::m_color. can be accessed via .int t1::get_color\\(\\) const." "" { target *-*-* } .-12 }
97   /* { dg-begin-multiline-output "" }
98    return ptr->m_color;
99                ^~~~~~~
100                get_color()
101      { dg-end-multiline-output "" } */
104 int test_deref_const_t1_color (const t1 *ptr)
106   return ptr->m_color; // { dg-error ".int t1::m_color. is private within this context" }
107   /* { dg-begin-multiline-output "" }
108    return ptr->m_color;
109                ^~~~~~~
110      { dg-end-multiline-output "" } */
113   /* { dg-begin-multiline-output "" }
114    int m_color;
115        ^~~~~~~
116      { dg-end-multiline-output "" } */
118   // { dg-message "field .int t1::m_color. can be accessed via .int t1::get_color\\(\\) const." "" { target *-*-* } .-12 }
119   /* { dg-begin-multiline-output "" }
120    return ptr->m_color;
121                ^~~~~~~
122                get_color()
123      { dg-end-multiline-output "" } */
126 int test_deref_t1_shape (t1 *ptr)
128   return ptr->m_shape; // { dg-error ".int t1::m_shape. is protected within this context" }
129   /* { dg-begin-multiline-output "" }
130    return ptr->m_shape;
131                ^~~~~~~
132      { dg-end-multiline-output "" } */
135   /* { dg-begin-multiline-output "" }
136    int m_shape;
137        ^~~~~~~
138      { dg-end-multiline-output "" } */
140   // { dg-message "field .int t1::m_shape. can be accessed via .int t1::get_shape\\(\\) const." "" { target *-*-* } .-12 }
141   /* { dg-begin-multiline-output "" }
142    return ptr->m_shape;
143                ^~~~~~~
144                get_shape()
145      { dg-end-multiline-output "" } */
148 /* Example of public inheritance.  */
150 class t2 : public t1
154 int test_deref_t2_color (t2 *ptr)
156   return ptr->m_color; // { dg-error ".int t1::m_color. is private within this context" }
157   /* { dg-begin-multiline-output "" }
158    return ptr->m_color;
159                ^~~~~~~
160      { dg-end-multiline-output "" } */
163   /* { dg-begin-multiline-output "" }
164    int m_color;
165        ^~~~~~~
166      { dg-end-multiline-output "" } */
168   // { dg-message "field .int t1::m_color. can be accessed via .int t1::get_color\\(\\) const." "" { target *-*-* } .-12 }
169   /* { dg-begin-multiline-output "" }
170    return ptr->m_color;
171                ^~~~~~~
172                get_color()
173      { dg-end-multiline-output "" } */
176 /* Example of private inheritance.  */
178 class t3 : private t1
182 int test_deref_t3_color (t3 *ptr)
184   return ptr->m_color; // { dg-error ".int t1::m_color. is private within this context" }
185   /* { dg-begin-multiline-output "" }
186    return ptr->m_color;
187                ^~~~~~~
188      { dg-end-multiline-output "" } */
190   /* { dg-begin-multiline-output "" }
191    int m_color;
192        ^~~~~~~
193      { dg-end-multiline-output "" } */
195   /* We shouldn't provide a fix-it hint for this case due to the
196      private inheritance.  */
199 /* Example of non-public "accessor".  */
201 class t4
203   int m_field;
204   int get_field () { return m_field; }
207 int test_deref_t4_field (t4 *ptr)
209   return ptr->m_field; // { dg-error ".int t4::m_field. is private within this context" }
210   /* { dg-begin-multiline-output "" }
211    return ptr->m_field;
212                ^~~~~~~
213      { dg-end-multiline-output "" } */
215   /* { dg-begin-multiline-output "" }
216    int m_field;
217        ^~~~~~~
218      { dg-end-multiline-output "" } */
220   /* We shouldn't provide a fix-it hint for this case, as the accessor is
221      itself private.  */