integer-overflow.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // RUN: %clang_cc1 %s -verify -fsyntax-only
  2. typedef unsigned long long uint64_t;
  3. typedef unsigned long long uint32_t;
  4. uint64_t f0(uint64_t);
  5. uint64_t f1(uint64_t, uint32_t);
  6. uint64_t f2(uint64_t, ...);
  7. static const uint64_t overflow = 1 * 4608 * 1024 * 1024; // expected-warning {{overflow in expression; result is 536870912 with type 'int'}}
  8. uint64_t check_integer_overflows(int i) {
  9. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  10. uint64_t overflow = 4608 * 1024 * 1024,
  11. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  12. overflow2 = (uint64_t)(4608 * 1024 * 1024),
  13. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  14. overflow3 = (uint64_t)(4608 * 1024 * 1024 * i),
  15. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  16. overflow4 = (1ULL * ((4608) * ((1024) * (1024))) + 2ULL),
  17. // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
  18. multi_overflow = (uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024));
  19. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  20. overflow += overflow2 = overflow3 = (uint64_t)(4608 * 1024 * 1024);
  21. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  22. overflow += overflow2 = overflow3 = 4608 * 1024 * 1024;
  23. uint64_t not_overflow = 4608 * 1024 * 1024ULL;
  24. uint64_t not_overflow2 = (1ULL * ((uint64_t)(4608) * (1024 * 1024)) + 2ULL);
  25. // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
  26. overflow = 4608 * 1024 * 1024 ? 4608 * 1024 * 1024 : 0;
  27. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  28. overflow = 0 ? 0 : 4608 * 1024 * 1024;
  29. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  30. if (4608 * 1024 * 1024)
  31. return 0;
  32. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  33. if ((uint64_t)(4608 * 1024 * 1024))
  34. return 1;
  35. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  36. if ((uint64_t)(4608 * 1024 * 1024))
  37. return 2;
  38. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  39. if ((uint64_t)(4608 * 1024 * 1024 * i))
  40. return 3;
  41. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  42. if ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL))
  43. return 4;
  44. // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
  45. if ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)))
  46. return 5;
  47. switch (i) {
  48. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  49. case 4608 * 1024 * 1024:
  50. return 6;
  51. // expected-warning@+1 {{overflow in expression; result is 537919488 with type 'int'}}
  52. case (uint64_t)(4609 * 1024 * 1024):
  53. return 7;
  54. // expected-error@+1 {{expression is not an integer constant expression}}
  55. case ((uint64_t)(4608 * 1024 * 1024 * i)):
  56. return 8;
  57. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  58. case ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL)):
  59. return 9;
  60. // expected-warning@+2 2{{overflow in expression; result is 536870912 with type 'int'}}
  61. // expected-warning@+1 {{overflow converting case value to switch condition type (288230376151711744 to 0)}}
  62. case ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024))):
  63. return 10;
  64. }
  65. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  66. while (4608 * 1024 * 1024);
  67. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  68. while ((uint64_t)(4608 * 1024 * 1024));
  69. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  70. while ((uint64_t)(4608 * 1024 * 1024));
  71. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  72. while ((uint64_t)(4608 * 1024 * 1024 * i));
  73. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  74. while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));
  75. // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
  76. while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
  77. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  78. do { } while (4608 * 1024 * 1024);
  79. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  80. do { } while ((uint64_t)(4608 * 1024 * 1024));
  81. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  82. do { } while ((uint64_t)(4608 * 1024 * 1024));
  83. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  84. do { } while ((uint64_t)(4608 * 1024 * 1024 * i));
  85. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  86. do { } while ((1ULL * ((4608) * ((1024) * (1024))) + 2ULL));
  87. // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
  88. do { } while ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
  89. // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
  90. // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
  91. // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
  92. for (uint64_t i = 4608 * 1024 * 1024;
  93. (uint64_t)(4608 * 1024 * 1024);
  94. i += (uint64_t)(4608 * 1024 * 1024 * i));
  95. // expected-warning@+3 {{overflow in expression; result is 536870912 with type 'int'}}
  96. // expected-warning@+3 2{{overflow in expression; result is 536870912 with type 'int'}}
  97. // expected-warning@+3 2{{overflow in expression; result is 536870912 with type 'int'}}
  98. for (uint64_t i = (1ULL * ((4608) * ((1024) * (1024))) + 2ULL);
  99. ((uint64_t)((uint64_t)(4608 * 1024 * 1024) * (uint64_t)(4608 * 1024 * 1024)));
  100. i = ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024))));
  101. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  102. _Complex long long x = 4608 * 1024 * 1024;
  103. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  104. (__real__ x) = 4608 * 1024 * 1024;
  105. // expected-warning@+1 {{overflow in expression; result is 536870912 with type 'int'}}
  106. (__imag__ x) = 4608 * 1024 * 1024;
  107. // expected-warning@+4 {{overflow in expression; result is 536870912 with type 'int'}}
  108. // expected-warning@+3 {{array index 536870912 is past the end of the array (which contains 10 elements)}}
  109. // expected-note@+1 {{array 'a' declared here}}
  110. uint64_t a[10];
  111. a[4608 * 1024 * 1024] = 1i;
  112. // expected-warning@+1 2{{overflow in expression; result is 536870912 with type 'int'}}
  113. return ((4608 * 1024 * 1024) + ((uint64_t)(4608 * 1024 * 1024)));
  114. }