aaronpuchert created this revision. aaronpuchert added reviewers: arphaman, bruno, rsmith. Herald added subscribers: cfe-commits, dexonsmith. Herald added a project: clang.
Zero-parameter K&R definitions specify that the function has no parameters, but they are still not prototypes, so calling the function with the wrong number of parameters is just a warning, not an error. The C11 standard doesn't seem to directly define what a prototype is, but it can be inferred from 6.9.1p7: "If the declarator includes a parameter type list, the list also specifies the types of all the parameters; such a declarator also serves as a function prototype for later calls to the same function in the same translation unit." This refers to 6.7.6.3p5: "If, in the declaration “T D1 <https://reviews.llvm.org/D1>”, D1 <https://reviews.llvm.org/D1> has the form D(parameter-type-list) or D(identifier-list_opt) [...]". Later in 6.11.7 it also refers only to the parameter-type-list variant as prototype: "The use of function definitions with separate parameter identifier and declaration lists (not prototype-format parameter type and identifier declarators) is an obsolescent feature." We already correctly treat an empty parameter list as non-prototype declaration, so we can just take that information. GCC also warns about this with -Wstrict-prototypes. This shouldn't affect C++, because there all FunctionType's are FunctionProtoTypes. I added a simple test for that. Repository: rG LLVM Github Monorepo https://reviews.llvm.org/D66919 Files: clang/lib/Sema/SemaDecl.cpp clang/test/Sema/warn-strict-prototypes.c clang/test/Sema/warn-strict-prototypes.cpp clang/test/Sema/warn-strict-prototypes.m Index: clang/test/Sema/warn-strict-prototypes.m =================================================================== --- clang/test/Sema/warn-strict-prototypes.m +++ clang/test/Sema/warn-strict-prototypes.m @@ -10,7 +10,7 @@ @end -void foo() { +void foo() { // expected-warning {{this old-style function definition is not preceded by a prototype}} void (^block)() = // expected-warning {{this block declaration is not a prototype}} ^void(int arg) { // no warning }; Index: clang/test/Sema/warn-strict-prototypes.cpp =================================================================== --- /dev/null +++ clang/test/Sema/warn-strict-prototypes.cpp @@ -0,0 +1,8 @@ +// RUN: %clang_cc1 -verify -fsyntax-only -Wstrict-prototypes %s +// expected-no-diagnostics + +void decl(); +void decl_void(void); + +void def() {} +void def_void(void) {} Index: clang/test/Sema/warn-strict-prototypes.c =================================================================== --- clang/test/Sema/warn-strict-prototypes.c +++ clang/test/Sema/warn-strict-prototypes.c @@ -7,9 +7,9 @@ // function declaration with 0 params void foo2(void); -// function definition with 0 params(for both cases), -// valid according to 6.7.5.3/14 -void foo1() {} +// function definition with 0 params, no prototype. +void foo1() {} // expected-warning {{this old-style function definition is not preceded by a prototype}} +// function definition with 0 params, prototype. void foo2(void) {} // function type typedef unspecified params Index: clang/lib/Sema/SemaDecl.cpp =================================================================== --- clang/lib/Sema/SemaDecl.cpp +++ clang/lib/Sema/SemaDecl.cpp @@ -13395,11 +13395,7 @@ // Warn if K&R function is defined without a previous declaration. // This warning is issued only if the definition itself does not provide // a prototype. Only K&R definitions do not provide a prototype. - // An empty list in a function declarator that is part of a definition - // of that function specifies that the function has no parameters - // (C99 6.7.5.3p14) - if (!FD->hasWrittenPrototype() && FD->getNumParams() > 0 && - !LangOpts.CPlusPlus) { + if (!FD->hasWrittenPrototype()) { TypeSourceInfo *TI = FD->getTypeSourceInfo(); TypeLoc TL = TI->getTypeLoc(); FunctionTypeLoc FTL = TL.getAsAdjusted<FunctionTypeLoc>();
Index: clang/test/Sema/warn-strict-prototypes.m =================================================================== --- clang/test/Sema/warn-strict-prototypes.m +++ clang/test/Sema/warn-strict-prototypes.m @@ -10,7 +10,7 @@ @end -void foo() { +void foo() { // expected-warning {{this old-style function definition is not preceded by a prototype}} void (^block)() = // expected-warning {{this block declaration is not a prototype}} ^void(int arg) { // no warning }; Index: clang/test/Sema/warn-strict-prototypes.cpp =================================================================== --- /dev/null +++ clang/test/Sema/warn-strict-prototypes.cpp @@ -0,0 +1,8 @@ +// RUN: %clang_cc1 -verify -fsyntax-only -Wstrict-prototypes %s +// expected-no-diagnostics + +void decl(); +void decl_void(void); + +void def() {} +void def_void(void) {} Index: clang/test/Sema/warn-strict-prototypes.c =================================================================== --- clang/test/Sema/warn-strict-prototypes.c +++ clang/test/Sema/warn-strict-prototypes.c @@ -7,9 +7,9 @@ // function declaration with 0 params void foo2(void); -// function definition with 0 params(for both cases), -// valid according to 6.7.5.3/14 -void foo1() {} +// function definition with 0 params, no prototype. +void foo1() {} // expected-warning {{this old-style function definition is not preceded by a prototype}} +// function definition with 0 params, prototype. void foo2(void) {} // function type typedef unspecified params Index: clang/lib/Sema/SemaDecl.cpp =================================================================== --- clang/lib/Sema/SemaDecl.cpp +++ clang/lib/Sema/SemaDecl.cpp @@ -13395,11 +13395,7 @@ // Warn if K&R function is defined without a previous declaration. // This warning is issued only if the definition itself does not provide // a prototype. Only K&R definitions do not provide a prototype. - // An empty list in a function declarator that is part of a definition - // of that function specifies that the function has no parameters - // (C99 6.7.5.3p14) - if (!FD->hasWrittenPrototype() && FD->getNumParams() > 0 && - !LangOpts.CPlusPlus) { + if (!FD->hasWrittenPrototype()) { TypeSourceInfo *TI = FD->getTypeSourceInfo(); TypeLoc TL = TI->getTypeLoc(); FunctionTypeLoc FTL = TL.getAsAdjusted<FunctionTypeLoc>();
_______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits