llvm-project/clang/test/CodeGen/2002-07-14-MiscTests3.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

183 lines
3.2 KiB
C

// RUN: %clang_cc1 -Wno-implicit-function-declaration -w -emit-llvm %s -o /dev/null
void *malloc(unsigned);
int puts(const char *s);
struct FunStructTest {
int Test1;
char *Pointer;
int Array[12];
};
struct SubStruct {
short X, Y;
};
struct Quad {
int w;
struct SubStruct SS;
struct SubStruct *SSP;
char c;
int y;
};
struct Quad GlobalQuad = { 4, {1, 2}, 0, 3, 156 };
typedef int (*FuncPtr)(int);
unsigned PtrFunc(int (*Func)(int), int X) {
return Func(X);
}
char PtrFunc2(FuncPtr FuncTab[30], int Num) {
return FuncTab[Num]('b');
}
extern char SmallArgs2(char w, char x, long long Zrrk, char y, char z);
extern int SomeFunc(void);
char SmallArgs(char w, char x, char y, char z) {
SomeFunc();
return SmallArgs2(w-1, x+1, y, z, w);
}
static int F0(struct Quad Q, int i) { /* Pass Q by value */
struct Quad R;
if (i) R.SS = Q.SS;
Q.SSP = &R.SS;
Q.w = Q.y = Q.c = 1;
return Q.SS.Y + i + R.y - Q.c;
}
int F1(struct Quad *Q, int i) { /* Pass Q by address */
struct Quad R;
#if 0
if (i) R.SS = Q->SS;
#else
if (i) R = *Q;
#endif
Q->w = Q->y = Q->c = 1;
return Q->SS.Y+i+R.y-Q->c;
}
int BadFunc(float Val) {
int Result;
if (Val > 12.345) Result = 4;
return Result; /* Test use of undefined value */
}
int RealFunc(void) {
return SomeUndefinedFunction(1, 4, 5);
}
extern int EF1(int *, char *, int *);
int Func(int Param, long long Param2) {
int Result = Param;
{{{{
char c; int X;
EF1(&Result, &c, &X);
}}}
{ // c & X are duplicate names!
char c; int X;
EF1(&Result, &c, &X);
}
}
return Result;
}
short FunFunc(long long x, char z) {
return x+z;
}
unsigned castTest(int X) { return X; }
double TestAdd(double X, float Y) {
return X+Y+.5;
}
int func(int i, int j) {
while (i != 20)
i += 2;
j += func(2, i);
return (i * 3 + j*2)*j;
}
int SumArray(int Array[], int Num) {
int i, Result = 0;
for (i = 0; i < Num; ++i)
Result += Array[i];
return Result;
}
int ArrayParam(int Values[100]) {
return EF1((int*)Values[50], (char*)1, &Values[50]);
}
int ArrayToSum(void) {
int A[100], i;
for (i = 0; i < 100; ++i)
A[i] = i*4;
return A[A[0]]; //SumArray(A, 100);
}
int ExternFunc(long long, unsigned*, short, unsigned char);
int main(int argc, char *argv[]) {
unsigned i;
puts("Hello world!\n");
ExternFunc(-1, 0, (short)argc, 2);
//func(argc, argc);
for (i = 0; i < 10; i++)
puts(argv[3]);
return 0;
}
double MathFunc(double X, double Y, double Z,
double AA, double BB, double CC, double DD,
double EE, double FF, double GG, double HH,
double aAA, double aBB, double aCC, double aDD,
double aEE, double aFF) {
return X + Y + Z + AA + BB + CC + DD + EE + FF + GG + HH
+ aAA + aBB + aCC + aDD + aEE + aFF;
}
void strcpy(char *s1, char *s2) {
while (*s1++ = *s2++);
}
void strcat(char *s1, char *s2) {
while (*s1++);
s1--;
while (*s1++ = *s2++);
}
int strcmp(char *s1, char *s2) {
while (*s1++ == *s2++);
if (*s1 == 0) {
if (*s2 == 0) {
return 0;
} else {
return -1;
}
} else {
if (*s2 == 0) {
return 1;
} else {
return (*(--s1) - *(--s2));
}
}
}