llvm-project/clang/test/CodeGen/conditional.c
Aaron Ballman 7d644e1215 [C11/C2x] Change the behavior of the implicit function declaration warning
C89 had a questionable feature where the compiler would implicitly
declare a function that the user called but was never previously
declared. The resulting function would be globally declared as
extern int func(); -- a function without a prototype which accepts zero
or more arguments.

C99 removed support for this questionable feature due to severe
security concerns. However, there was no deprecation period; C89 had
the feature, C99 didn't. So Clang (and GCC) both supported the
functionality as an extension in C99 and later modes.

C2x no longer supports that function signature as it now requires all
functions to have a prototype, and given the known security issues with
the feature, continuing to support it as an extension is not tenable.

This patch changes the diagnostic behavior for the
-Wimplicit-function-declaration warning group depending on the language
mode in effect. We continue to warn by default in C89 mode (due to the
feature being dangerous to use). However, because this feature will not
be supported in C2x mode, we've diagnosed it as being invalid for so
long, the security concerns with the feature, and the trivial
workaround for users (declare the function), we now default the
extension warning to an error in C99-C17 mode. This still gives users
an easy workaround if they are extensively using the extension in those
modes (they can disable the warning or use -Wno-error to downgrade the
error), but the new diagnostic makes it more clear that this feature is
not supported and should be avoided. In C2x mode, we no longer allow an
implicit function to be defined and treat the situation the same as any
other lookup failure.

Differential Revision: https://reviews.llvm.org/D122983
2022-04-20 11:30:12 -04:00

76 lines
1.2 KiB
C

// RUN: %clang_cc1 -emit-llvm %s -o - | FileCheck %s
float test1(int cond, float a, float b) {
return cond ? a : b;
}
double test2(int cond, float a, double b) {
return cond ? a : b;
}
void f(void);
void test3(void){
1 ? f() : (void)0;
}
void test4(void) {
int i; short j;
float* k = 1 ? &i : &j;
}
void test5(void) {
const int* cip;
void* vp;
cip = 0 ? vp : cip;
}
void test6(void);
void test7(int);
void* test8(void) {return 1 ? test6 : test7;}
void _efree(void *ptr);
void free(void *ptr);
void _php_stream_free3(void) {
(1 ? free(0) : _efree(0));
}
void _php_stream_free4(void) {
1 ? _efree(0) : free(0);
}
// PR5526
struct test9 { int a; };
void* test9spare(void);
void test9(struct test9 *p) {
p ? p : test9spare();
}
// CHECK: @test10
// CHECK: select i1 {{.*}}, i32 4, i32 5
int test10(int c) {
return c ? 4 : 5;
}
enum { Gronk = 5 };
// rdar://9289603
// CHECK: @test11
// CHECK: select i1 {{.*}}, i32 4, i32 5
int test11(int c) {
return c ? 4 : Gronk;
}
// CHECK: @test12
// CHECK: select i1 {{.*}}, double 4.0{{.*}}, double 2.0
double test12(int c) {
return c ? 4.0 : 2.0;
}
// CHECK: @test13
// CHECK: call {{.*}} @f2(
int f2(void);
void test13(void) {
f2() ? (void)0 : (void)0;
}