warn-bad-function-cast.c 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. // RUN: %clang_cc1 %s -fsyntax-only -Wno-unused-value -Wbad-function-cast -triple x86_64-unknown-unknown -verify
  2. // rdar://9103192
  3. void vf(void);
  4. int if1(void);
  5. char if2(void);
  6. long if3(void);
  7. float rf1(void);
  8. double rf2(void);
  9. _Complex double cf(void);
  10. enum e { E1 } ef(void);
  11. _Bool bf(void);
  12. char *pf1(void);
  13. int *pf2(void);
  14. void
  15. foo(void)
  16. {
  17. /* Casts to void types are always OK. */
  18. (void)vf();
  19. (void)if1();
  20. (void)cf();
  21. (const void)bf();
  22. /* Casts to the same type or similar types are OK. */
  23. (int)if1();
  24. (long)if2();
  25. (char)if3();
  26. (float)rf1();
  27. (long double)rf2();
  28. (_Complex float)cf();
  29. (enum f { F1 })ef();
  30. (_Bool)bf();
  31. (void *)pf1();
  32. (char *)pf2();
  33. /* All following casts issue warning */
  34. (float)if1(); /* expected-warning {{cast from function call of type 'int' to non-matching type 'float'}} */
  35. (double)if2(); /* expected-warning {{cast from function call of type 'char' to non-matching type 'double'}} */
  36. (_Bool)if3(); /* expected-warning {{cast from function call of type 'long' to non-matching type '_Bool'}} */
  37. (int)rf1(); /* expected-warning {{cast from function call of type 'float' to non-matching type 'int'}} */
  38. (long)rf2(); /* expected-warning {{cast from function call of type 'double' to non-matching type 'long'}} */
  39. (double)cf(); /* expected-warning {{cast from function call of type '_Complex double' to non-matching type 'double'}} */
  40. (int)ef(); /* expected-warning {{cast from function call of type 'enum e' to non-matching type 'int'}} */
  41. (int)bf(); /* expected-warning {{cast from function call of type '_Bool' to non-matching type 'int'}} */
  42. (__SIZE_TYPE__)pf1(); /* expected-warning {{cast from function call of type 'char *' to non-matching type 'unsigned long'}} */
  43. (__PTRDIFF_TYPE__)pf2(); /* expected-warning {{cast from function call of type 'int *' to non-matching type 'long'}} */
  44. }