cast.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // RUN: %clang_cc1 -fsyntax-only -triple x86_64-unknown-unknown %s -verify
  2. typedef struct { unsigned long bits[(((1) + (64) - 1) / (64))]; } cpumask_t;
  3. cpumask_t x;
  4. void foo() {
  5. (void)x;
  6. }
  7. void bar() {
  8. char* a;
  9. double b;
  10. b = (double)a; // expected-error {{pointer cannot be cast to type}}
  11. a = (char*)b; // expected-error {{cannot be cast to a pointer type}}
  12. }
  13. long bar1(long *next) {
  14. return (long)(*next)++;
  15. }
  16. typedef _Bool Bool;
  17. typedef int Int;
  18. typedef long Long;
  19. typedef float Float;
  20. typedef double Double;
  21. typedef _Complex int CInt;
  22. typedef _Complex long CLong;
  23. typedef _Complex float CFloat;
  24. typedef _Complex double CDouble;
  25. typedef void *VoidPtr;
  26. typedef char *CharPtr;
  27. void testBool(Bool v) {
  28. (void) (Bool) v;
  29. (void) (Int) v;
  30. (void) (Long) v;
  31. (void) (Float) v;
  32. (void) (Double) v;
  33. (void) (CInt) v;
  34. (void) (CLong) v;
  35. (void) (CFloat) v;
  36. (void) (CDouble) v;
  37. (void) (VoidPtr) v;
  38. (void) (CharPtr) v;
  39. }
  40. void testInt(Int v) {
  41. (void) (Bool) v;
  42. (void) (Int) v;
  43. (void) (Long) v;
  44. (void) (Float) v;
  45. (void) (Double) v;
  46. (void) (CInt) v;
  47. (void) (CLong) v;
  48. (void) (CFloat) v;
  49. (void) (CDouble) v;
  50. (void) (VoidPtr) v; // expected-warning{{cast to 'VoidPtr' (aka 'void *') from smaller integer type 'Int' (aka 'int')}}
  51. (void) (CharPtr) v; // expected-warning{{cast to 'CharPtr' (aka 'char *') from smaller integer type 'Int' (aka 'int')}}
  52. // Test that casts to void* can be controlled separately
  53. // from other -Wint-to-pointer-cast warnings.
  54. #pragma clang diagnostic push
  55. #pragma clang diagnostic ignored "-Wint-to-void-pointer-cast"
  56. (void) (VoidPtr) v; // no-warning
  57. (void) (CharPtr) v; // expected-warning{{cast to 'CharPtr' (aka 'char *') from smaller integer type 'Int' (aka 'int')}}
  58. #pragma clang diagnostic pop
  59. }
  60. void testLong(Long v) {
  61. (void) (Bool) v;
  62. (void) (Int) v;
  63. (void) (Long) v;
  64. (void) (Float) v;
  65. (void) (Double) v;
  66. (void) (CInt) v;
  67. (void) (CLong) v;
  68. (void) (CFloat) v;
  69. (void) (CDouble) v;
  70. (void) (VoidPtr) v;
  71. (void) (CharPtr) v;
  72. }
  73. void testFloat(Float v) {
  74. (void) (Bool) v;
  75. (void) (Int) v;
  76. (void) (Long) v;
  77. (void) (Float) v;
  78. (void) (Double) v;
  79. (void) (CInt) v;
  80. (void) (CLong) v;
  81. (void) (CFloat) v;
  82. (void) (CDouble) v;
  83. }
  84. void testDouble(Double v) {
  85. (void) (Bool) v;
  86. (void) (Int) v;
  87. (void) (Long) v;
  88. (void) (Float) v;
  89. (void) (Double) v;
  90. (void) (CInt) v;
  91. (void) (CLong) v;
  92. (void) (CFloat) v;
  93. (void) (CDouble) v;
  94. }
  95. void testCI(CInt v) {
  96. (void) (Bool) v;
  97. (void) (Int) v;
  98. (void) (Long) v;
  99. (void) (Float) v;
  100. (void) (Double) v;
  101. (void) (CInt) v;
  102. (void) (CLong) v;
  103. (void) (CFloat) v;
  104. (void) (CDouble) v;
  105. }
  106. void testCLong(CLong v) {
  107. (void) (Bool) v;
  108. (void) (Int) v;
  109. (void) (Long) v;
  110. (void) (Float) v;
  111. (void) (Double) v;
  112. (void) (CInt) v;
  113. (void) (CLong) v;
  114. (void) (CFloat) v;
  115. (void) (CDouble) v;
  116. }
  117. void testCFloat(CFloat v) {
  118. (void) (Bool) v;
  119. (void) (Int) v;
  120. (void) (Long) v;
  121. (void) (Float) v;
  122. (void) (Double) v;
  123. (void) (CInt) v;
  124. (void) (CLong) v;
  125. (void) (CFloat) v;
  126. (void) (CDouble) v;
  127. }
  128. void testCDouble(CDouble v) {
  129. (void) (Bool) v;
  130. (void) (Int) v;
  131. (void) (Long) v;
  132. (void) (Float) v;
  133. (void) (Double) v;
  134. (void) (CInt) v;
  135. (void) (CLong) v;
  136. (void) (CFloat) v;
  137. (void) (CDouble) v;
  138. }
  139. void testVoidPtr(VoidPtr v) {
  140. (void) (Bool) v;
  141. (void) (Int) v;
  142. (void) (Long) v;
  143. (void) (VoidPtr) v;
  144. (void) (CharPtr) v;
  145. }
  146. void testCharPtr(CharPtr v) {
  147. (void) (Bool) v;
  148. (void) (Int) v;
  149. (void) (Long) v;
  150. (void) (VoidPtr) v;
  151. (void) (CharPtr) v;
  152. }
  153. typedef enum { x_a, x_b } X;
  154. void *intToPointerCast2(X x) {
  155. return (void*)x;
  156. }
  157. void *intToPointerCast3() {
  158. return (void*)(1 + 3);
  159. }