Skip to content

Overzealous -Wstrict-prototypes warnings on function definitions #90596

Open
@jmarshall

Description

@jmarshall

Consider the following, foo.c:

int foo() {
    return 42;
}

Current clang (19.0.0git c3598b1) with -pedantic or -Wstrict-prototypes produces a warning for this code:

clang -c -Wstrict-prototypes foo.c 
foo.c:1:8: warning: a function declaration without a prototype is deprecated in all versions of C [-Wstrict-prototypes]
    1 | int foo() {
      |        ^
      |         void

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]

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 by writing such functions as int foo(void) { return 42; } when the void 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

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.)

Metadata

Metadata

Assignees

No one assigned

    Labels

    cclang:diagnosticsNew/improved warning or error message in Clang, but not in clang-tidy or static analyzer

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions