Inconsistent (missing) incompatible function pointer type warnings
| Bugzilla Link | 42120 |
| Version | trunk |
| OS | All |
| CC | @dwblaikie,@DougGregor,@zygoloid |
Extended Description
Clang marks pointer conversions that add noreturn or noescape information to function types as incompatible (while removing such information is fine), but this warning is not consistent:
$ cat test.c
void fun00(int *);
void fun01(int *) __attribute__((noreturn));
void fun10(__attribute__((noescape)) int *);
void fun11(__attribute__((noescape)) int *) __attribute__((noreturn));
// OK: safe, no warning
void (*fptr01)(int *) = &fun01;
void (*fptr02)(__attribute__((noescape)) int *) = &fun11;
void (*fptr03)(int *) __attribute__((noreturn)) = &fun11;
// OK: unsafe, warning
void (*fptr11)(int *) __attribute__((noreturn)) = &fun00;
void (*fptr12)(__attribute__((noescape)) int *) = &fun00;
void (*fptr13)(__attribute__((noescape)) int *) __attribute__((noreturn)) = &fun00;
// NOT OK: also unsafe, no warning?
void (*fptr14)(__attribute__((noescape)) int *) = &fun01;
void (*fptr15)(int *) __attribute__((noreturn)) = &fun10;
$ clang -c -x c test.c
(only cases fptr11, fptr12, fptr13 are warnings; fptr14 and fptr15 pass without warning/error)
Godbolt: https://godbolt.org/z/XjFsFB
That no warnings are issued for fptr14 and fptr15 cases seems wrong. The conversions are at least as dangerous as the fptr11 and fptr12 cases, which do get warnings.
Note that C++ behavior:
$ clang -c -x c++ test.c
(cases fptr11, fptr12, fptr12, fptr13, fptr14, fptr15 are flagged as errors)
Godbolt: https://godbolt.org/z/cVjcCQ
Usually best to send patches to llvm-commits (contributing patches discussed here: https://llvm.org/docs/Contributing.html ) - they can go a bit lost when attached to bugs.
This behavior is implemented in file clang/lib/Sema/SemaExpr.cpp near the end of function checkPointerTypesForAssignment:
if (!S.getLangOpts().CPlusPlus &&
S.IsFunctionConversion(ltrans, rtrans, ltrans))
return Sema::IncompatiblePointer;
return ConvTy;
This is very subtle. I think the intention is to check that the conversion from RHS to LHS is an invalid function conversion and then degrade the conversion type to IncompatiblePointer. But this is implementated by checking that the inverse conversion from LHS to RHS is valid (and LHS and RHS are not identical). This is an incomplete condition: my test case demonstrates that the inverse conversion is not necessarily valid for the conversion to be invalid.
I include a patch that replaces the call to IsFunctionConversion by a more direct check that the functions are incompatible. With this patch, also cases fptr14 and fptr15 of my test case are flagged with a warning.
@llvm/issue-subscribers-clang-frontend
Author: Bruno De Fraine (brunodefraine)
Extended Description
Clang marks pointer conversions that add noreturn or noescape information to function types as incompatible (while removing such information is fine), but this warning is not consistent:
$ cat test.c
void fun00(int *);
void fun01(int *) __attribute__((noreturn));
void fun10(__attribute__((noescape)) int *);
void fun11(__attribute__((noescape)) int *) __attribute__((noreturn));
// OK: safe, no warning
void (*fptr01)(int *) = &fun01;
void (*fptr02)(__attribute__((noescape)) int *) = &fun11;
void (*fptr03)(int *) __attribute__((noreturn)) = &fun11;
// OK: unsafe, warning
void (*fptr11)(int *) __attribute__((noreturn)) = &fun00;
void (*fptr12)(__attribute__((noescape)) int *) = &fun00;
void (*fptr13)(__attribute__((noescape)) int *) __attribute__((noreturn)) = &fun00;
// NOT OK: also unsafe, no warning?
void (*fptr14)(__attribute__((noescape)) int *) = &fun01;
void (*fptr15)(int *) __attribute__((noreturn)) = &fun10;
$ clang -c -x c test.c
(only cases fptr11, fptr12, fptr13 are warnings; fptr14 and fptr15 pass without warning/error)
Godbolt: https://godbolt.org/z/XjFsFB
That no warnings are issued for fptr14 and fptr15 cases seems wrong. The conversions are at least as dangerous as the fptr11 and fptr12 cases, which do get warnings.
Note that C++ behavior:
$ clang -c -x c++ test.c
(cases fptr11, fptr12, fptr12, fptr13, fptr14, fptr15 are flagged as errors)
Godbolt: https://godbolt.org/z/cVjcCQ
CC @erichkeane
Extended, up-to-date test case from #160474. All lines marked "OK??" are cases where a diagnostic is logically missing (and gcc has a diagnostic if it understand the involved attributes).
Godbolt demo: https://godbolt.org/z/f3o8KoqGo
#ifdef __clang__
// Comment-in pragma to enable strict mode
//#pragma clang diagnostic warning "-Wincompatible-function-pointer-types-strict"
#endif
enum E { A = -1, B };
// Case 1: assignment adding noreturn
int f1 (int);
int (*fp1a)(int) __attribute__((noreturn)) = &f1; // NOK
enum E (*fp1b)(int) __attribute__((noreturn)) = &f1; // OK??
int (*fp1c)() __attribute__((noreturn)) = &f1; // OK??
#ifdef __clang__
// Case 2: assignment adding noescape on arg
int f2 (int* ) __attribute__((noreturn));
int (*fp2a)(int* __attribute__((noescape))) __attribute__((noreturn)) = &f2; // NOK
int (*fp2b)(int* __attribute__((noescape))) = &f2; // OK??
// Case 3: assignment adding cfi_unchecked_callee
int f3 (int* );
int (*fp3a)(int* ) __attribute__((noreturn )) = &f3; // NOK
int (*fp3b)(int* __attribute__((noescape))) = &f3; // NOK
int (*fp3c)(int* ) __attribute__((noreturn,cfi_unchecked_callee)) = &f3; // OK??
int (*fp3d)(int* __attribute__((noescape))) __attribute__(( cfi_unchecked_callee)) = &f3; // OK??
int (*fp3e)(int* __attribute__((noescape))) __attribute__((noreturn,cfi_unchecked_callee)) = &f3; // OK??
#endif
// Case 4: assignment to function with no prototype
int f4 (int);
int (*fp4a)(int) = &f4; // OK
int (*fp4b)() = &f4; // NOK for strict?
Also, in the interim, a stricter diagnostic -Wincompatible-function-pointer-types-strict was added in commit 41ce74e6e98, this catches all the cases, but this also diagnoses differences in argument and result types that are allowed in C.