llvm-project/clang/test/Sema/builtin-dump-struct.c
Aaron Ballman 7068aa9841 Strengthen -Wint-conversion to default to an error
Clang has traditionally allowed C programs to implicitly convert
integers to pointers and pointers to integers, despite it not being
valid to do so except under special circumstances (like converting the
integer 0, which is the null pointer constant, to a pointer). In C89,
this would result in undefined behavior per 3.3.4, and in C99 this rule
was strengthened to be a constraint violation instead. Constraint
violations are most often handled as an error.

This patch changes the warning to default to an error in all C modes
(it is already an error in C++). This gives us better security posture
by calling out potential programmer mistakes in code but still allows
users who need this behavior to use -Wno-error=int-conversion to retain
the warning behavior, or -Wno-int-conversion to silence the diagnostic
entirely.

Differential Revision: https://reviews.llvm.org/D129881
2022-07-22 15:24:54 -04:00

50 lines
2.4 KiB
C

// RUN: %clang_cc1 -triple i386-unknown-unknown -fsyntax-only -fno-spell-checking -Wno-strict-prototypes -verify %s -fblocks
void invalid_uses(void) {
struct A {
};
struct A a;
void *b;
int (*goodfunc)(const char *, ...);
int (*badfunc1)(const char *);
int (*badfunc2)(int, ...);
int (*badfunc3)(void);
__builtin_dump_struct(); // expected-error {{too few arguments to function call, expected 2, have 0}}
__builtin_dump_struct(1); // expected-error {{too few arguments to function call, expected 2, have 1}}
__builtin_dump_struct(1, 2); // expected-error {{expected pointer to struct as 1st argument to '__builtin_dump_struct', found 'int'}}
__builtin_dump_struct(&a, 2); // expected-error {{expected a callable expression as 2nd argument to '__builtin_dump_struct', found 'int'}}
__builtin_dump_struct(b, goodfunc); // expected-error {{expected pointer to struct as 1st argument to '__builtin_dump_struct', found 'void *'}}
__builtin_dump_struct(&a, badfunc1); // expected-error {{too many arguments to function call, expected 1, have 2}} expected-note {{in call to printing function with arguments '("%s", "struct A")'}}
__builtin_dump_struct(&a, badfunc2); // expected-error-re 1+{{incompatible pointer to integer conversion passing 'char[{{.*}}]' to parameter of type 'int'}}
// expected-note@-1 1+{{in call to printing function with arguments '("}}
__builtin_dump_struct(&a, badfunc3); // expected-error {{too many arguments to function call, expected 0, have 2}} expected-note {{in call to printing function with arguments '("%s", "struct A")'}}
__builtin_dump_struct(a, goodfunc); // expected-error {{expected pointer to struct as 1st argument to '__builtin_dump_struct', found 'struct A'}}
}
int goodglobalfunc(const char*, ...);
void valid_uses(void) {
struct A {
};
union B {
};
int (*goodfunc)(const char *, ...);
int (*goodfunc2)();
void (*goodfunc3)(const char *, ...);
int (*goodfunc4)(char *, ...);
int (^goodblock)(const char*, ...);
struct A a;
union B b;
__builtin_dump_struct(&a, goodglobalfunc);
__builtin_dump_struct(&a, &goodglobalfunc);
__builtin_dump_struct(&a, goodfunc);
__builtin_dump_struct(&b, goodfunc);
__builtin_dump_struct(&a, goodfunc2);
__builtin_dump_struct(&a, goodfunc3);
__builtin_dump_struct(&a, goodfunc4);
__builtin_dump_struct(&a, goodblock);
}