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

--- Comment #13 from GCC Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by David Malcolm <dmalc...@gcc.gnu.org>:

https://gcc.gnu.org/g:a236f70617213343f3075ee43e8d9f5882dca400

commit r15-6838-ga236f70617213343f3075ee43e8d9f5882dca400
Author: David Malcolm <dmalc...@redhat.com>
Date:   Sun Jan 12 13:46:31 2025 -0500

    c: UX improvements to 'too {few,many} arguments' errors (v5) [PR118112]

    Consider this case of a bad call to a callback function (perhaps
    due to C23 changing the meaning of () in function decls):

    struct p {
            int (*bar)();
    };

    void baz() {
        struct p q;
        q.bar(1);
    }

    Before this patch the C frontend emits:

    t.c: In function 'baz':
    t.c:7:5: error: too many arguments to function 'q.bar'
        7 |     q.bar(1);
          |     ^

    which doesn't give the user much help in terms of knowing what
    was expected, and where the relevant declaration is.

    With this patch the C frontend emits:

    t.c: In function 'baz':
    t.c:7:5: error: too many arguments to function 'q.bar'; expected 0, have 1
        7 |     q.bar(1);
          |     ^     ~
    t.c:2:15: note: declared here
        2 |         int (*bar)();
          |               ^~~

    (showing the expected vs actual counts, the pertinent field decl, and
    underlining the first extraneous argument at the callsite)

    Similarly, the patch also updates the "too few arguments" case to also
    show expected vs actual counts.  Doing so requires a tweak to the
    wording to say "at least" for the case of variadic fns where
    previously the C FE emitted e.g.:

    s.c: In function 'test':
    s.c:5:3: error: too few arguments to function 'callee'
        5 |   callee ();
          |   ^~~~~~
    s.c:1:6: note: declared here
        1 | void callee (const char *, ...);
          |      ^~~~~~

    with this patch it emits:

    s.c: In function 'test':
    s.c:5:3: error: too few arguments to function 'callee'; expected at least
1, have 0
        5 |   callee ();
          |   ^~~~~~
    s.c:1:6: note: declared here
        1 | void callee (const char *, ...);
          |      ^~~~~~

    gcc/c/ChangeLog:
            PR c/118112
            * c-typeck.cc (inform_declaration): Add "function_expr" param and
            use it for cases where we couldn't show the function decl to show
            field decls for callbacks.
            (build_function_call_vec): Add missing auto_diagnostic_group.
            Update for new param of inform_declaration.
            (convert_arguments): Likewise.  For the "too many arguments" case
            add the expected vs actual counts to the message, and if we have
            it, add the location_t of the first surplus param as a secondary
            location within the diagnostic.  For the "too few arguments" case,
            determine the minimum number of arguments required and add the
            expected vs actual counts to the message, tweaking it to "at least"
            for variadic functions.

    gcc/testsuite/ChangeLog:
            PR c/118112
            * gcc.dg/too-few-arguments.c: New test.
            * gcc.dg/too-many-arguments.c: New test.

    Signed-off-by: David Malcolm <dmalc...@redhat.com>

Reply via email to