constant-conversion.c 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. // RUN: %clang_cc1 -fsyntax-only -verify -triple x86_64-apple-darwin %s
  2. // This file tests -Wconstant-conversion, a subcategory of -Wconversion
  3. // which is on by default.
  4. // rdar://problem/6792488
  5. void test_6792488(void) {
  6. int x = 0x3ff0000000000000U; // expected-warning {{implicit conversion from 'unsigned long' to 'int' changes value from 4607182418800017408 to 0}}
  7. }
  8. void test_7809123(void) {
  9. struct { int i5 : 5; } a;
  10. a.i5 = 36; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 36 to 4}}
  11. }
  12. void test() {
  13. struct { int bit : 1; } a;
  14. a.bit = 1; // shouldn't warn
  15. }
  16. enum Test2 { K_zero, K_one };
  17. enum Test2 test2(enum Test2 *t) {
  18. *t = 20;
  19. return 10; // shouldn't warn
  20. }
  21. void test3() {
  22. struct A {
  23. unsigned int foo : 2;
  24. int bar : 2;
  25. };
  26. struct A a = { 0, 10 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to -2}}
  27. struct A b[] = { 0, 10, 0, 0 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to -2}}
  28. struct A c[] = {{10, 0}}; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
  29. struct A d = (struct A) { 10, 0 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
  30. struct A e = { .foo = 10 }; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 10 to 2}}
  31. }
  32. void test4() {
  33. struct A {
  34. char c : 2;
  35. } a;
  36. a.c = 0x101; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 257 to 1}}
  37. }
  38. void test5() {
  39. struct A {
  40. _Bool b : 1;
  41. } a;
  42. // Don't warn about this implicit conversion to bool, or at least
  43. // don't warn about it just because it's a bitfield.
  44. a.b = 100;
  45. }
  46. void test6() {
  47. // Test that unreachable code doesn't trigger the truncation warning.
  48. unsigned char x = 0 ? 65535 : 1; // no-warning
  49. unsigned char y = 1 ? 65535 : 1; // expected-warning {{changes value}}
  50. }
  51. void test7() {
  52. struct {
  53. unsigned int twoBits1:2;
  54. unsigned int twoBits2:2;
  55. unsigned int reserved:28;
  56. } f;
  57. f.twoBits1 = ~1; // expected-warning {{implicit truncation from 'int' to bitfield changes value from -2 to 2}}
  58. f.twoBits2 = ~2; // expected-warning {{implicit truncation from 'int' to bitfield changes value from -3 to 1}}
  59. f.twoBits1 &= ~1; // no-warning
  60. f.twoBits2 &= ~2; // no-warning
  61. }
  62. void test8() {
  63. enum E { A, B, C };
  64. struct { enum E x : 1; } f;
  65. f.x = C; // expected-warning {{implicit truncation from 'int' to bitfield changes value from 2 to 0}}
  66. }