From 5afef8b119ac69393900a8be42cc6b69ad0bcbd0 Mon Sep 17 00:00:00 2001 From: David Malcolm Date: Wed, 21 Mar 2018 18:21:39 +0000 Subject: [PATCH] C++: show private field accessor hints with -g and optimization (PR c++/84994) gcc/cp/ChangeLog: PR c++/84994 * constexpr.c (constexpr_fn_retval): Make non-"static". * cp-tree.h (constexpr_fn_retval): New decl. * search.c (direct_accessor_p): Update leading comment. (reference_accessor_p): Likewise. (field_accessor_p): Replace check that function body is a RETURN_EXPR with a call to constexpr_fn_retval. Fix indentation of "field_type" decl. gcc/testsuite/ChangeLog: PR c++/84994 * g++.dg/other/accessor-fixits-1.C: Move to... * g++.dg/torture/accessor-fixits-1.C: ...here. * g++.dg/other/accessor-fixits-2.C: Move to... * g++.dg/torture/accessor-fixits-2.C: ...here. * g++.dg/other/accessor-fixits-3.C: Move to... * g++.dg/torture/accessor-fixits-3.C: ...here. * g++.dg/other/accessor-fixits-4.C: Move to... * g++.dg/torture/accessor-fixits-4.C: ...here. * g++.dg/other/accessor-fixits-5.C: Move to... * g++.dg/torture/accessor-fixits-5.C: ...here. * g++.dg/torture/accessor-fixits-6.C: New testcase. * g++.dg/torture/accessor-fixits-7.C: New testcase. * g++.dg/torture/accessor-fixits-8.C: New testcase. From-SVN: r258731 --- gcc/cp/ChangeLog | 11 +++++++ gcc/cp/constexpr.c | 2 +- gcc/cp/cp-tree.h | 1 + gcc/cp/search.c | 21 +++++++------ gcc/testsuite/ChangeLog | 17 ++++++++++ .../{other => torture}/accessor-fixits-1.C | 0 .../{other => torture}/accessor-fixits-2.C | 0 .../{other => torture}/accessor-fixits-3.C | 0 .../{other => torture}/accessor-fixits-4.C | 0 .../{other => torture}/accessor-fixits-5.C | 0 .../g++.dg/torture/accessor-fixits-6.C | 22 +++++++++++++ .../g++.dg/torture/accessor-fixits-7.C | 22 +++++++++++++ .../g++.dg/torture/accessor-fixits-8.C | 31 +++++++++++++++++++ 13 files changed, 116 insertions(+), 11 deletions(-) rename gcc/testsuite/g++.dg/{other => torture}/accessor-fixits-1.C (100%) rename gcc/testsuite/g++.dg/{other => torture}/accessor-fixits-2.C (100%) rename gcc/testsuite/g++.dg/{other => torture}/accessor-fixits-3.C (100%) rename gcc/testsuite/g++.dg/{other => torture}/accessor-fixits-4.C (100%) rename gcc/testsuite/g++.dg/{other => torture}/accessor-fixits-5.C (100%) create mode 100644 gcc/testsuite/g++.dg/torture/accessor-fixits-6.C create mode 100644 gcc/testsuite/g++.dg/torture/accessor-fixits-7.C create mode 100644 gcc/testsuite/g++.dg/torture/accessor-fixits-8.C diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index 8fc9314ac0f..c8b6b1703bd 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,14 @@ +2018-03-21 David Malcolm + + PR c++/84994 + * constexpr.c (constexpr_fn_retval): Make non-"static". + * cp-tree.h (constexpr_fn_retval): New decl. + * search.c (direct_accessor_p): Update leading comment. + (reference_accessor_p): Likewise. + (field_accessor_p): Replace check that function body is a + RETURN_EXPR with a call to constexpr_fn_retval. Fix + indentation of "field_type" decl. + 2018-03-21 Nathan Sidwell PR c++/84804 diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index 9e813bf0bcd..bebd9f5b5d0 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -657,7 +657,7 @@ get_function_named_in_call (tree t) return value if suitable, error_mark_node for a statement not allowed in a constexpr function, or NULL_TREE if no return value was found. */ -static tree +tree constexpr_fn_retval (tree body) { switch (TREE_CODE (body)) diff --git a/gcc/cp/cp-tree.h b/gcc/cp/cp-tree.h index 4df16e2e1ca..d5382c2604f 100644 --- a/gcc/cp/cp-tree.h +++ b/gcc/cp/cp-tree.h @@ -7424,6 +7424,7 @@ extern bool literal_type_p (tree); extern tree register_constexpr_fundef (tree, tree); extern bool is_valid_constexpr_fn (tree, bool); extern bool check_constexpr_ctor_body (tree, tree, bool); +extern tree constexpr_fn_retval (tree); extern tree ensure_literal_type_for_constexpr_object (tree); extern bool potential_constant_expression (tree); extern bool is_constant_expression (tree); diff --git a/gcc/cp/search.c b/gcc/cp/search.c index ddcff69ae6f..b4366102e23 100644 --- a/gcc/cp/search.c +++ b/gcc/cp/search.c @@ -1657,8 +1657,7 @@ field_access_p (tree component_ref, tree field_decl, tree field_type) Specifically, a simple accessor within struct S of the form: T get_field () { return m_field; } - should have a DECL_SAVED_TREE of the form: - - >>. */ + >>>>. */ static bool direct_accessor_p (tree init_expr, tree field_decl, tree field_type) @@ -1690,8 +1689,7 @@ direct_accessor_p (tree init_expr, tree field_decl, tree field_type) Specifically, a simple accessor within struct S of the form: T& get_field () { return m_field; } - should have a DECL_SAVED_TREE of the form: - + + PR c++/84994 + * g++.dg/other/accessor-fixits-1.C: Move to... + * g++.dg/torture/accessor-fixits-1.C: ...here. + * g++.dg/other/accessor-fixits-2.C: Move to... + * g++.dg/torture/accessor-fixits-2.C: ...here. + * g++.dg/other/accessor-fixits-3.C: Move to... + * g++.dg/torture/accessor-fixits-3.C: ...here. + * g++.dg/other/accessor-fixits-4.C: Move to... + * g++.dg/torture/accessor-fixits-4.C: ...here. + * g++.dg/other/accessor-fixits-5.C: Move to... + * g++.dg/torture/accessor-fixits-5.C: ...here. + * g++.dg/torture/accessor-fixits-6.C: New testcase. + * g++.dg/torture/accessor-fixits-7.C: New testcase. + * g++.dg/torture/accessor-fixits-8.C: New testcase. + 2018-03-21 Rainer Orth * gcc.dg/tree-ssa/pr84512.c: xfail on 64-bit SPARC. diff --git a/gcc/testsuite/g++.dg/other/accessor-fixits-1.C b/gcc/testsuite/g++.dg/torture/accessor-fixits-1.C similarity index 100% rename from gcc/testsuite/g++.dg/other/accessor-fixits-1.C rename to gcc/testsuite/g++.dg/torture/accessor-fixits-1.C diff --git a/gcc/testsuite/g++.dg/other/accessor-fixits-2.C b/gcc/testsuite/g++.dg/torture/accessor-fixits-2.C similarity index 100% rename from gcc/testsuite/g++.dg/other/accessor-fixits-2.C rename to gcc/testsuite/g++.dg/torture/accessor-fixits-2.C diff --git a/gcc/testsuite/g++.dg/other/accessor-fixits-3.C b/gcc/testsuite/g++.dg/torture/accessor-fixits-3.C similarity index 100% rename from gcc/testsuite/g++.dg/other/accessor-fixits-3.C rename to gcc/testsuite/g++.dg/torture/accessor-fixits-3.C diff --git a/gcc/testsuite/g++.dg/other/accessor-fixits-4.C b/gcc/testsuite/g++.dg/torture/accessor-fixits-4.C similarity index 100% rename from gcc/testsuite/g++.dg/other/accessor-fixits-4.C rename to gcc/testsuite/g++.dg/torture/accessor-fixits-4.C diff --git a/gcc/testsuite/g++.dg/other/accessor-fixits-5.C b/gcc/testsuite/g++.dg/torture/accessor-fixits-5.C similarity index 100% rename from gcc/testsuite/g++.dg/other/accessor-fixits-5.C rename to gcc/testsuite/g++.dg/torture/accessor-fixits-5.C diff --git a/gcc/testsuite/g++.dg/torture/accessor-fixits-6.C b/gcc/testsuite/g++.dg/torture/accessor-fixits-6.C new file mode 100644 index 00000000000..ae2f180a8f2 --- /dev/null +++ b/gcc/testsuite/g++.dg/torture/accessor-fixits-6.C @@ -0,0 +1,22 @@ +// PR c++/84994 +/* Ensure that fix-it hints are offered at every optimization level, even when + "-g" is enabled (coverage for every optimization level without -g is given + by the other cases within g++.dg/torture/accessor-fixits-*.C). */ +// { dg-additional-options "-g" } + +class foo +{ +public: + double get_ratio() const { return m_ratio; } + +private: + double m_ratio; // { dg-line field_decl } +}; + +void test(foo *ptr) +{ + if (ptr->m_ratio >= 0.5) // { dg-error "'double foo::m_ratio' is private within this context" } + ; + // { dg-message "declared private here" "" { target *-*-* } field_decl } + // { dg-message "'double foo::m_ratio' can be accessed via 'double foo::get_ratio\\(\\) const'" "" { target *-*-* } .-3 } +} diff --git a/gcc/testsuite/g++.dg/torture/accessor-fixits-7.C b/gcc/testsuite/g++.dg/torture/accessor-fixits-7.C new file mode 100644 index 00000000000..3b5babdc513 --- /dev/null +++ b/gcc/testsuite/g++.dg/torture/accessor-fixits-7.C @@ -0,0 +1,22 @@ +class foo +{ +public: + double get_ratio() const; + +private: + double m_ratio; // { dg-line field_decl } +}; + +double +foo::get_ratio() const +{ + return m_ratio; +} + +void test(foo *ptr) +{ + if (ptr->m_ratio >= 0.5) // { dg-error "'double foo::m_ratio' is private within this context" } + ; + // { dg-message "declared private here" "" { target *-*-* } field_decl } + // { dg-message "'double foo::m_ratio' can be accessed via 'double foo::get_ratio\\(\\) const'" "" { target *-*-* } .-3 } +} diff --git a/gcc/testsuite/g++.dg/torture/accessor-fixits-8.C b/gcc/testsuite/g++.dg/torture/accessor-fixits-8.C new file mode 100644 index 00000000000..1338b7d1349 --- /dev/null +++ b/gcc/testsuite/g++.dg/torture/accessor-fixits-8.C @@ -0,0 +1,31 @@ +// { dg-options "-fdiagnostics-show-caret" } + +class t1 +{ +public: + int get_doubled_field () const { return m_field * 2; } + int get_guarded_field_1 () const { if (m_field) return m_field; else return 42; } + int get_guarded_field_2 () const { return m_field ? m_field : 42; } + int with_unreachable () const { __builtin_unreachable (); return m_field; } + void no_return () { } + +private: + int m_field; // { dg-line field_decl } +}; + +int test (t1 *ptr) +{ + return ptr->m_field; // { dg-error ".int t1::m_field. is private within this context" } + /* { dg-begin-multiline-output "" } + return ptr->m_field; + ^~~~~~~ + { dg-end-multiline-output "" } */ + + // { dg-message "declared private here" "" { target *-*-* } field_decl } + /* { dg-begin-multiline-output "" } + int m_field; + ^~~~~~~ + { dg-end-multiline-output "" } */ + + /* We shouldn't issue a suggestion: none of the member functions are suitable returns. */ +} -- 2.30.2