From 4be105c98a9c7e083cd878ee1751e11160b97b4a Mon Sep 17 00:00:00 2001 From: Aaron Ballman Date: Fri, 13 May 2022 08:25:40 -0400 Subject: [PATCH] Silence some false positive -Wstrict-prototype warnings Before issuing the warning about use of a strict prototype, check if the declarator is required to have a prototype through some other means determined at parse time. This silences false positives in OpenCL code (where the functions are forced to have a prototype) and block literal expressions. --- clang/lib/Sema/SemaType.cpp | 6 ++++-- clang/test/Parser/opencl-kernel.cl | 5 +++-- clang/test/Sema/block-return.c | 2 +- clang/test/Sema/warn-strict-prototypes.m | 2 +- clang/test/SemaOpenCL/address-spaces.cl | 8 ++++---- .../test/SemaOpenCL/cl20-device-side-enqueue.cl | 16 ++-------------- clang/test/SemaOpenCL/func.cl | 4 ++-- 7 files changed, 17 insertions(+), 26 deletions(-) diff --git a/clang/lib/Sema/SemaType.cpp b/clang/lib/Sema/SemaType.cpp index 0bb352e914a8..05a9b56f7d8f 100644 --- a/clang/lib/Sema/SemaType.cpp +++ b/clang/lib/Sema/SemaType.cpp @@ -5575,8 +5575,10 @@ static TypeSourceInfo *GetFullTypeForDeclarator(TypeProcessingState &state, const DeclaratorChunk::FunctionTypeInfo &FTI = DeclType.Fun; // We suppress the warning when there's no LParen location, as this // indicates the declaration was an implicit declaration, which gets - // warned about separately via -Wimplicit-function-declaration. - if (FTI.NumParams == 0 && !FTI.isVariadic && FTI.getLParenLoc().isValid()) + // warned about separately via -Wimplicit-function-declaration. We also + // suppress the warning when we know the function has a prototype. + if (!FTI.hasPrototype && FTI.NumParams == 0 && !FTI.isVariadic && + FTI.getLParenLoc().isValid()) S.Diag(DeclType.Loc, diag::warn_strict_prototypes) << IsBlock << FixItHint::CreateInsertion(FTI.getRParenLoc(), "void"); diff --git a/clang/test/Parser/opencl-kernel.cl b/clang/test/Parser/opencl-kernel.cl index e509ed59a9ea..01c7ed6b5ab6 100644 --- a/clang/test/Parser/opencl-kernel.cl +++ b/clang/test/Parser/opencl-kernel.cl @@ -1,9 +1,10 @@ // RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only +// expected-no-diagnostics -__kernel void test() // expected-warning {{a function declaration without a prototype is deprecated in all versions of C}} +__kernel void test() { } -kernel void test1() // expected-warning {{a function declaration without a prototype is deprecated in all versions of C}} +kernel void test1() { } diff --git a/clang/test/Sema/block-return.c b/clang/test/Sema/block-return.c index 3dcfe2dfea4e..1ac050bfb4b2 100644 --- a/clang/test/Sema/block-return.c +++ b/clang/test/Sema/block-return.c @@ -100,7 +100,7 @@ bptr foo5(int j) { int (*funcptr3[5])(long); int sz8 = sizeof(^int (*[5])(long) {return funcptr3;}); // expected-error {{block cannot return array type}} expected-warning {{incompatible pointer to integer conversion}} int sz9 = sizeof(^int(*())()[3]{ }); // expected-error {{function cannot return array type}} - // expected-warning@-1 2 {{a function declaration without a prototype is deprecated in all versions of C}} + // expected-warning@-1 {{a function declaration without a prototype is deprecated in all versions of C}} void foo6(void) { int (^b)(int) __attribute__((noreturn)); diff --git a/clang/test/Sema/warn-strict-prototypes.m b/clang/test/Sema/warn-strict-prototypes.m index edb8bc6ea388..38e4ffaba767 100644 --- a/clang/test/Sema/warn-strict-prototypes.m +++ b/clang/test/Sema/warn-strict-prototypes.m @@ -17,7 +17,7 @@ void foo() { // expected-warning {{a function declaration without a prototype is // FIXME: this should say "a block declaration" instead, but block literal // expressions do not track their full declarator information, so we don't // know it's a block when diagnosing. - void (^block2)(void) = ^void() { // expected-warning {{a function declaration without a prototype is deprecated in all versions of C}} + void (^block2)(void) = ^void() { }; void (^block3)(void) = ^ { // no warning }; diff --git a/clang/test/SemaOpenCL/address-spaces.cl b/clang/test/SemaOpenCL/address-spaces.cl index 03f303575964..41f5a3365325 100644 --- a/clang/test/SemaOpenCL/address-spaces.cl +++ b/clang/test/SemaOpenCL/address-spaces.cl @@ -1,6 +1,6 @@ -// RUN: %clang_cc1 %s -verify=expected,c -pedantic -fsyntax-only -// RUN: %clang_cc1 %s -cl-std=CL2.0 -verify=expected,c -pedantic -fsyntax-only -// RUN: %clang_cc1 %s -cl-std=CL3.0 -cl-ext=+__opencl_c_generic_address_space -verify=expected,c -pedantic -fsyntax-only +// RUN: %clang_cc1 %s -verify=expected -pedantic -fsyntax-only +// RUN: %clang_cc1 %s -cl-std=CL2.0 -verify=expected -pedantic -fsyntax-only +// RUN: %clang_cc1 %s -cl-std=CL3.0 -cl-ext=+__opencl_c_generic_address_space -verify=expected -pedantic -fsyntax-only // RUN: %clang_cc1 %s -cl-std=clc++1.0 -verify -pedantic -fsyntax-only // RUN: %clang_cc1 %s -cl-std=clc++2021 -cl-ext=+__opencl_c_generic_address_space -verify -pedantic -fsyntax-only @@ -251,7 +251,7 @@ void func_multiple_addr(void) { void func_with_array_param(const unsigned data[16]); -__kernel void k() { // c-warning {{a function declaration without a prototype is deprecated in all versions of C}} +__kernel void k() { unsigned data[16]; func_with_array_param(data); } diff --git a/clang/test/SemaOpenCL/cl20-device-side-enqueue.cl b/clang/test/SemaOpenCL/cl20-device-side-enqueue.cl index 7e3bcb5290f1..36b901fc5f29 100644 --- a/clang/test/SemaOpenCL/cl20-device-side-enqueue.cl +++ b/clang/test/SemaOpenCL/cl20-device-side-enqueue.cl @@ -223,22 +223,16 @@ kernel void work_group_size_tests(void) { kernel void foo(global unsigned int *buf) { ndrange_t n; - // FIXME: this should be diagnosed as a block instead of a function, but - // block literals don't track the ^ as part of their declarator. - buf[0] = get_kernel_max_sub_group_size_for_ndrange(n, ^(){}); // expected-warning {{a function declaration without a prototype is deprecated in all versions of C}} + buf[0] = get_kernel_max_sub_group_size_for_ndrange(n, ^(){}); buf[0] = get_kernel_max_sub_group_size_for_ndrange(0, ^(){}); // expected-error{{illegal call to 'get_kernel_max_sub_group_size_for_ndrange', expected 'ndrange_t' argument type}} - // expected-warning@-1 {{a function declaration without a prototype is deprecated in all versions of C}} buf[0] = get_kernel_max_sub_group_size_for_ndrange(n, 1); // expected-error{{illegal call to 'get_kernel_max_sub_group_size_for_ndrange', expected block argument type}} } kernel void bar(global unsigned int *buf) { __private ndrange_t n; - // FIXME: this should be diagnosed as a block instead of a function, but - // block literals don't track the ^ as part of their declarator. - buf[0] = get_kernel_sub_group_count_for_ndrange(n, ^(){}); // expected-warning {{a function declaration without a prototype is deprecated in all versions of C}} + buf[0] = get_kernel_sub_group_count_for_ndrange(n, ^(){}); buf[0] = get_kernel_sub_group_count_for_ndrange(0, ^(){}); // expected-error{{illegal call to 'get_kernel_sub_group_count_for_ndrange', expected 'ndrange_t' argument type}} - // expected-warning@-1 {{a function declaration without a prototype is deprecated in all versions of C}} buf[0] = get_kernel_sub_group_count_for_ndrange(n, 1); // expected-error{{illegal call to 'get_kernel_sub_group_count_for_ndrange', expected block argument type}} } @@ -247,18 +241,12 @@ kernel void bar(global unsigned int *buf) kernel void foo1(global unsigned int *buf) { ndrange_t n; - // FIXME: this should be diagnosed as a block instead of a function, but - // block literals don't track the ^ as part of their declarator. buf[0] = get_kernel_max_sub_group_size_for_ndrange(n, ^(){}); // expected-error {{use of declaration 'get_kernel_max_sub_group_size_for_ndrange' requires cl_khr_subgroups or __opencl_c_subgroups support}} - // expected-warning@-1 {{a function declaration without a prototype is deprecated in all versions of C}} } kernel void bar1(global unsigned int *buf) { ndrange_t n; - // FIXME: this should be diagnosed as a block instead of a function, but - // block literals don't track the ^ as part of their declarator. buf[0] = get_kernel_sub_group_count_for_ndrange(n, ^(){}); // expected-error {{use of declaration 'get_kernel_sub_group_count_for_ndrange' requires cl_khr_subgroups or __opencl_c_subgroups support}} - // expected-warning@-1 {{a function declaration without a prototype is deprecated in all versions of C}} } #endif // ifdef cl_khr_subgroups diff --git a/clang/test/SemaOpenCL/func.cl b/clang/test/SemaOpenCL/func.cl index b11de505d73d..233e82f24497 100644 --- a/clang/test/SemaOpenCL/func.cl +++ b/clang/test/SemaOpenCL/func.cl @@ -43,9 +43,9 @@ void foo(void*); #endif // Expect no diagnostics for an empty parameter list. -void bar(); // expected-warning {{a function declaration without a prototype is deprecated in all versions of C}} +void bar(); -void bar() // expected-warning {{a function declaration without a prototype is deprecated in all versions of C}} +void bar() { // declaring a function pointer is an error void (*fptr)(int);