Skip to content
Snippets Groups Projects
Commit a236f706 authored by David Malcolm's avatar David Malcolm
Browse files

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: default avatarDavid Malcolm <dmalcolm@redhat.com>
parent f8eda60e
No related branches found
No related tags found
No related merge requests found
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment