aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorDavid Malcolm <dmalcolm@redhat.com>2018-03-21 13:09:09 +0000
committerDavid Malcolm <dmalcolm@gcc.gnu.org>2018-03-21 13:09:09 +0000
commit2a80d3ae14f6f823e28637fd28b5808a6e4aa5ba (patch)
tree86d55601cfd0c25ca63798a94855f2a8ef645709 /gcc
parent8bb406a2916060d123a97974072ceb74ac9c92cc (diff)
downloadgcc-2a80d3ae14f6f823e28637fd28b5808a6e4aa5ba.zip
gcc-2a80d3ae14f6f823e28637fd28b5808a6e4aa5ba.tar.gz
gcc-2a80d3ae14f6f823e28637fd28b5808a6e4aa5ba.tar.bz2
C++: show private field accessor hints for const accesses (PR c++/84892)
gcc/cp/ChangeLog: PR c++/84892 * search.c (field_accessor_p): Use class_of_this_parm rather than type_of_this_parm, to check that "this" is a "const T *", rather than a "T *const". gcc/testsuite/ChangeLog: PR c++/84892 * g++.dg/other/accessor-fixits-1.C (test_access_const_t1_color): New. (test_deref_const_t1_color): New. * g++.dg/other/accessor-fixits-5.C: New testcase. From-SVN: r258716
Diffstat (limited to 'gcc')
-rw-r--r--gcc/cp/ChangeLog7
-rw-r--r--gcc/cp/search.c4
-rw-r--r--gcc/testsuite/ChangeLog8
-rw-r--r--gcc/testsuite/g++.dg/other/accessor-fixits-1.C44
-rw-r--r--gcc/testsuite/g++.dg/other/accessor-fixits-5.C33
5 files changed, 94 insertions, 2 deletions
diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog
index da19013..74bc986 100644
--- a/gcc/cp/ChangeLog
+++ b/gcc/cp/ChangeLog
@@ -1,3 +1,10 @@
+2018-03-21 David Malcolm <dmalcolm@redhat.com>
+
+ PR c++/84892
+ * search.c (field_accessor_p): Use class_of_this_parm rather than
+ type_of_this_parm, to check that "this" is a "const T *", rather
+ than a "T *const".
+
2018-03-21 Nathan Sidwell <nathan@acm.org>
* class.c (finish_struct_anon_r): Refactor, deprecate anything
diff --git a/gcc/cp/search.c b/gcc/cp/search.c
index 796209f..ddcff69 100644
--- a/gcc/cp/search.c
+++ b/gcc/cp/search.c
@@ -1747,8 +1747,8 @@ field_accessor_p (tree fn, tree field_decl, bool const_p)
that the "this" parameter is const. */
if (const_p)
{
- tree this_type = type_of_this_parm (fntype);
- if (!TYPE_READONLY (this_type))
+ tree this_class = class_of_this_parm (fntype);
+ if (!TYPE_READONLY (this_class))
return false;
}
diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog
index 64c8d97..5d62630 100644
--- a/gcc/testsuite/ChangeLog
+++ b/gcc/testsuite/ChangeLog
@@ -1,3 +1,11 @@
+2018-03-21 David Malcolm <dmalcolm@redhat.com>
+
+ PR c++/84892
+ * g++.dg/other/accessor-fixits-1.C
+ (test_access_const_t1_color): New.
+ (test_deref_const_t1_color): New.
+ * g++.dg/other/accessor-fixits-5.C: New testcase.
+
2018-03-21 Tom de Vries <tom@codesourcery.com>
PR tree-optimization/83126
diff --git a/gcc/testsuite/g++.dg/other/accessor-fixits-1.C b/gcc/testsuite/g++.dg/other/accessor-fixits-1.C
index cc96b87..fd46a52 100644
--- a/gcc/testsuite/g++.dg/other/accessor-fixits-1.C
+++ b/gcc/testsuite/g++.dg/other/accessor-fixits-1.C
@@ -35,6 +35,28 @@ int test_access_t1_color (t1 &ref)
{ dg-end-multiline-output "" } */
}
+int test_access_const_t1_color (const t1 &ref)
+{
+ return ref.m_color; // { dg-error ".int t1::m_color. is private within this context" }
+ /* { dg-begin-multiline-output "" }
+ return ref.m_color;
+ ^~~~~~~
+ { dg-end-multiline-output "" } */
+
+
+ /* { dg-begin-multiline-output "" }
+ int m_color;
+ ^~~~~~~
+ { dg-end-multiline-output "" } */
+
+ // { dg-message "field .int t1::m_color. can be accessed via .int t1::get_color\\(\\) const." "" { target *-*-* } .-12 }
+ /* { dg-begin-multiline-output "" }
+ return ref.m_color;
+ ^~~~~~~
+ get_color()
+ { dg-end-multiline-output "" } */
+}
+
int test_access_t1_shape (t1 &ref)
{
return ref.m_shape; // { dg-error ".int t1::m_shape. is protected within this context" }
@@ -79,6 +101,28 @@ int test_deref_t1_color (t1 *ptr)
{ dg-end-multiline-output "" } */
}
+int test_deref_const_t1_color (const t1 *ptr)
+{
+ return ptr->m_color; // { dg-error ".int t1::m_color. is private within this context" }
+ /* { dg-begin-multiline-output "" }
+ return ptr->m_color;
+ ^~~~~~~
+ { dg-end-multiline-output "" } */
+
+
+ /* { dg-begin-multiline-output "" }
+ int m_color;
+ ^~~~~~~
+ { dg-end-multiline-output "" } */
+
+ // { dg-message "field .int t1::m_color. can be accessed via .int t1::get_color\\(\\) const." "" { target *-*-* } .-12 }
+ /* { dg-begin-multiline-output "" }
+ return ptr->m_color;
+ ^~~~~~~
+ get_color()
+ { dg-end-multiline-output "" } */
+}
+
int test_deref_t1_shape (t1 *ptr)
{
return ptr->m_shape; // { dg-error ".int t1::m_shape. is protected within this context" }
diff --git a/gcc/testsuite/g++.dg/other/accessor-fixits-5.C b/gcc/testsuite/g++.dg/other/accessor-fixits-5.C
new file mode 100644
index 0000000..cf72d78
--- /dev/null
+++ b/gcc/testsuite/g++.dg/other/accessor-fixits-5.C
@@ -0,0 +1,33 @@
+// PR c++/84892
+// { dg-options "-fdiagnostics-show-caret" }
+
+class S {
+private:
+ bool field;
+
+public:
+ bool get_field() const {
+ return field;
+ }
+};
+
+bool thingy(const S & s) {
+ return s.field; // { dg-error "'bool S::field' is private within this context" }
+ /* { dg-begin-multiline-output "" }
+ return s.field;
+ ^~~~~
+ { dg-end-multiline-output "" } */
+
+ // { dg-message "declared private here" "" { target *-*-* } 6 }
+ /* { dg-begin-multiline-output "" }
+ bool field;
+ ^~~~~
+ { dg-end-multiline-output "" } */
+
+ // { dg-message "field 'bool S::field' can be accessed via 'bool S::get_field\\(\\) const'" "" { target *-*-* } .-12 }
+ /* { dg-begin-multiline-output "" }
+ return s.field;
+ ^~~~~
+ get_field()
+ { dg-end-multiline-output "" } */
+}