This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[Bug c++/77711] Add fix-it hints for missing parentheses in member function call


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77711

--- Comment #3 from Jonathan Wakely <redi at gcc dot gnu.org> ---
With:

--- a/gcc/cp/typeck.c
+++ b/gcc/cp/typeck.c
@@ -1823,6 +1823,7 @@ invalid_nonstatic_memfn_p (location_t loc, tree expr,
tsubst_flags_t complain)
   /* Don't enforce this in MS mode.  */
   if (flag_ms_extensions)
     return false;
+  tree origexpr = expr;
   if (is_overloaded_fn (expr) && !really_overloaded_fn (expr))
     expr = get_first_fn (expr);
   if (DECL_NONSTATIC_MEMBER_FUNCTION_P (expr))
@@ -1831,8 +1832,18 @@ invalid_nonstatic_memfn_p (location_t loc, tree expr,
tsubst_flags_t complain)
        {
          if (DECL_P (expr))
            {
-             error_at (loc, "invalid use of non-static member function %qD",
-                       expr);
+              tree args = TREE_CHAIN (TYPE_ARG_TYPES (TREE_TYPE (expr)));
+              if (list_length (args) == 1)
+              {
+                gcc_rich_location richloc (loc);
+                richloc.add_fixit_insert_before(loc, "()");
+                error_at_rich_loc (&richloc, "invalid use of non-static "
+                                   "member function %qD; did you mean "
+                                   "%<%E()%>?", expr, origexpr);
+              }
+              else
+                error_at (loc, "invalid use of non-static member function
%qD",
+                          expr);
              inform (DECL_SOURCE_LOCATION (expr), "declared here");
            }
          else

the first example gives:

call.cc: In function ‘int main()’:
call.cc:19:14: error: invalid use of non-static member function ‘int A::foo()’;
did you mean ‘a.A::foo()’?
   bar( a.foo );
              ^
              ()
call.cc:2:7: note: declared here
   int foo();
       ^~~

Which is OK, but not great.

It should say a.foo() not a.A::foo().

The fixit is correct, but it would be better if we had a rich_location covering
a.foo rather than a non-rich one pointing to the next token (PR77777). In this
specific example inserting () at the suggested location would match the GNU
coding standards (with a space before the parentheses) but nobody writes C++
like that, and it's not correct if there's any more whitespace before the next
token.

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]