Bug 95445 - diagnose incompatible calls to functions declared without prototype
Summary: diagnose incompatible calls to functions declared without prototype
Status: UNCONFIRMED
Alias: None
Product: gcc
Classification: Unclassified
Component: c (show other bugs)
Version: 10.0
: P3 enhancement
Target Milestone: ---
Assignee: Not yet assigned to anyone
URL:
Keywords: diagnostic
Depends on:
Blocks: new-warning, new_warning
  Show dependency treegraph
 
Reported: 2020-05-30 22:32 UTC by Martin Sebor
Modified: 2023-05-12 19:42 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Sebor 2020-05-30 22:32:07 UTC
GCC silently accepts calls to built-in functions declared without a prototype as long as the arguments match the expected types (based on the prototype hardcoded into GCC), but issues warnings for calls where the arguments don't match.

But GCC makes no attempt to make sure that in calls to other (i.e., non-built-in) functions declared without prototype provided arguments have the same type across all the calls.  Diagnosing mismatches as suggested in the comments below would help detect bugs caused by their incompatibily.

This enhancement is different from pr92212 which is about calls to K&R functions defined in the same translation unit.

$ cat x.c && gcc -O2 -S -Wall x.c
char* strchr ();

char* f0 (const char *s)
{
  return strchr (s, 'x');    // no warning (okay)
}

char* f1 (const char *s)
{
  return strchr (s, "y");    // warning (good)
}

void g ();

void h (void)
{
  g (1);                     // (presumably) okay, f's type is 'void (int)'
  g ("foo");                 // should be diagnosed
  g (1, "bar");              // ditto
}
x.c: In function ‘f1’:
x.c:10:21: warning: passing argument 2 of ‘strchr’ makes integer from pointer without a cast [-Wint-conversion]
   10 |   return strchr (s, "y");    // warning (good)
      |                     ^~~
      |                     |
      |                     char *
x.c:1:7: note: expected ‘int’ but argument is of type ‘char *’
    1 | char* strchr ();
      |       ^~~~~~
Comment 1 Eric Gallager 2021-10-27 05:21:02 UTC
Is this like clang's -Wknr-promoted-parameter? https://clang.llvm.org/docs/DiagnosticsReference.html#wknr-promoted-parameter
Comment 2 Florian Weimer 2023-05-12 15:54:03 UTC
Current Clang warns like this:

t.c:17:5: warning: passing arguments to 'g' without a prototype is deprecated in all versions of C and is not supported in C2x [-Wdeprecated-non-prototype]
  g (1);                     // (presumably) okay, f's type is 'void (int)'
    ^
t.c:18:5: warning: passing arguments to 'g' without a prototype is deprecated in all versions of C and is not supported in C2x [-Wdeprecated-non-prototype]
  g ("foo");                 // should be diagnosed
    ^
t.c:19:5: warning: passing arguments to 'g' without a prototype is deprecated in all versions of C and is not supported in C2x [-Wdeprecated-non-prototype]
  g (1, "bar");              // ditto
    ^

Maybe that's sufficient?

Trying to infer the type of g from its uses seems to be rather questionable. For K&R projects, GCC already has -flto -Wlto-type-mismatch, which can diagnose violations across translation units.
Comment 3 Sam James 2023-05-12 16:29:03 UTC
I don't think we need to go beyond that (I don't really care about the uses).

That'll make this a dupe of/depends on PR108694?