-
Notifications
You must be signed in to change notification settings - Fork 10.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Overzealous -Wstrict-prototypes warnings on function definitions #90596
Comments
@llvm/issue-subscribers-c Author: John Marshall (jmarshall)
Consider the following, _foo.c_:
int foo() {
return 42;
} Current clang (19.0.0git c3598b1) with
The discussion in [RFC] Enabling -Wstrict-prototypes by default in C largely focusses on encouraging users to write declarations as The diagnostic message above is somewhat confusing as this is a function definition, not just a function declaration. More importantly, I believe there is no confusion about a function definition with an empty parameter list. In particular C17 N2176 §6.7.6.3/14 says [similar text appears in earlier versions; emphasis added] > An identifier list declares only the identifiers of the parameters of the function. An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters. The empty list in a function declarator that is not part of a definition of that function specifies that no information about the number or types of the parameters is supplied. [Footnote 147: See “future language directions” (6.11.6).] I would take this to mean that a function definition with an empty parameter list effectively provides a prototype, and therefore that the warning is spurious in this case. It seems unfortunate to encourage users to hypercorrect their code to define such functions as I also tried to find the chapter and verse of the standard saying that this scenario is “deprecated in all versions of C”. I guess this is based on §6.11.6, which says > The use of function declarators with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature. Based on the two “…empty list…” sentences in the paragraph quoted above whose footnote refers to §6.11.6 and the fact that the obsolescent feature we're trying to get rid of in §6.11.6 is non-prototype declarations, I think this sentence intends only to refer to the latter and should perhaps read > The use of function declarators (that are not part of definitions of the respective functions) with empty parentheses (not prototype-format parameter type declarators) is an obsolescent feature. in which case it would not provide motivation for emitting this warning on function definitions. (I did not see a defect report to this effect or otherwise relating to §6.11.6.) |
int foo() {
return 42;
}
int bar(void) {
return 42;
} These still declare functions with two different types - It's still desireable for there to be a declaration with int foo() { return 42; }
int bar(void) { return 42; }
int f(int(int));
int main(void) {
f(foo); // Compatible
// f(bar); // Error
} And §6.5.2.2 function call semantics are defined in terms of the type of the function. §6.5.2.2/8 specifically mentions function definitions without a prototype. int main(void) {
foo(1); // Compiles but UB
// bar(1); // Error
} So the |
Thank you, that's very informative. (Though now I barely understand what the “An empty list in a function declarator that is part of a definition of that function specifies that the function has no parameters” sentence is there for…) |
There are several cases to consider.
does not provide a prototype for a function because it does not provide a
Neither of these declarations provide a prototype (per the same citations as above). Code between the declaration at (1) and the definition at (2) can call Finally, this gem:
The declaration at (1) provides a prototype, the definition at (2) does not provide a prototype, but gets one anyway because of composite type rules (see C17 6.2.7p3). Unlike the previous example, calls to It's technically a false positive to diagnose in that case, but I don't think we'll ever fix that bug because of the difficulties it presents to do so. We want to still catch cases like |
Hello,
I actually hit that exact issue here, and while the wording was initially confusing, I did get to the bottom of it. A nicer worded error would be nice though, since it led me to believe that clang wasn't seeing the initial declaration (which could have been possible due to Just wanted to provide a data point on encountering this issue in the wild. The explanations here are very helpful. |
Consider the following, foo.c:
Current clang (19.0.0git c3598b1) with
-pedantic
or-Wstrict-prototypes
produces a warning for this code:The discussion in [RFC] Enabling -Wstrict-prototypes by default in C largely focusses on encouraging users to write declarations as
int foo(void);
but I am interested in the application of this warning to function definitions.The diagnostic message above is somewhat confusing as this is a function definition, not just a function declaration. More importantly, I believe there is no confusion about a function definition with an empty parameter list. In particular C17 N2176 §6.7.6.3/14 says [similar text appears in earlier versions; emphasis added]
I would take this to mean that a function definition with an empty parameter list effectively provides a prototype, and therefore that the warning is spurious in this case. It seems unfortunate to encourage users to hypercorrect their code by writing such functions as
int foo(void) { return 42; }
when thevoid
doesn't add anything in a function definition.I also tried to find the chapter and verse of the standard saying that this scenario is “deprecated in all versions of C”. I guess this is based on §6.11.6, which says
Based on the two “…empty list…” sentences in the paragraph quoted above whose footnote refers to §6.11.6 and the fact that the obsolescent feature we're trying to get rid of in §6.11.6 is non-prototype declarations, I think this sentence intends only to refer to the latter and should perhaps read
in which case it would not provide motivation for emitting this warning on function definitions. (I did not see a defect report to this effect or otherwise relating to §6.11.6.)
The text was updated successfully, but these errors were encountered: