PR16678.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. // RUN: %clang_cc1 -DX32TYPE=ULONG -triple powerpc-unknown-linux-gnu -std=c89 -x c %s -verify
  2. // RUN: %clang_cc1 -DX32TYPE=ULONG -triple powerpc-unknown-linux-gnu -std=iso9899:199409 -x c %s -verify
  3. // RUN: %clang_cc1 -DX32TYPE=ULONG -triple powerpc-unknown-linux-gnu -std=c++98 -x c++ %s -verify
  4. // RUN: %clang_cc1 -DX32TYPE=LLONG -triple powerpc-unknown-linux-gnu -std=c99 -x c %s -verify
  5. // RUN: %clang_cc1 -DX32TYPE=LLONG -triple powerpc-unknown-linux-gnu -std=c11 -x c %s -verify
  6. // RUN: %clang_cc1 -DX32TYPE=LLONG -triple powerpc-unknown-linux-gnu -std=c++11 -x c++ %s -verify
  7. // RUN: %clang_cc1 -DX32TYPE=LLONG -triple powerpc-unknown-linux-gnu -std=c++1y -x c++ %s -verify
  8. // RUN: %clang_cc1 -DX32TYPE=LLONG -triple powerpc-unknown-linux-gnu -std=c++1z -x c++ %s -verify
  9. // RUN: %clang_cc1 -DX64TYPE=ULONG -triple powerpc64-unknown-linux-gnu -std=c89 -x c %s -verify
  10. // RUN: %clang_cc1 -DX64TYPE=ULONG -triple powerpc64-unknown-linux-gnu -std=iso9899:199409 -x c %s -verify
  11. // RUN: %clang_cc1 -DX64TYPE=ULONG -triple powerpc64-unknown-linux-gnu -std=c++98 -x c++ %s -verify
  12. // RUN: %clang_cc1 -DX64TYPE=ULLONG -triple powerpc64-unknown-linux-gnu -std=c99 -x c %s -verify
  13. // RUN: %clang_cc1 -DX64TYPE=ULLONG -triple powerpc64-unknown-linux-gnu -std=c11 -x c %s -verify
  14. // RUN: %clang_cc1 -DX64TYPE=ULLONG -triple powerpc64-unknown-linux-gnu -std=c++11 -x c++ %s -verify
  15. // RUN: %clang_cc1 -DX64TYPE=ULLONG -triple powerpc64-unknown-linux-gnu -std=c++1y -x c++ %s -verify
  16. // RUN: %clang_cc1 -DX64TYPE=ULLONG -triple powerpc64-unknown-linux-gnu -std=c++1z -x c++ %s -verify
  17. #ifdef X64TYPE
  18. #define X32TYPE long
  19. #endif
  20. #define IS_ULONG_ULONG 1
  21. #define IS_ULONG2(X) IS_ULONG_##X
  22. #define IS_ULONG(X) IS_ULONG2(X)
  23. #if !defined(X64TYPE) && !IS_ULONG(X32TYPE)
  24. // expected-no-diagnostics
  25. #endif
  26. typedef unsigned long ULONG;
  27. typedef long long LLONG;
  28. typedef unsigned long long ULLONG;
  29. /******************************************************************************
  30. * Test 2^31 as a decimal literal with no suffix and with the "l" and "L" cases.
  31. ******************************************************************************/
  32. extern X32TYPE x32;
  33. extern __typeof__(2147483648) x32;
  34. extern __typeof__(2147483648l) x32;
  35. extern __typeof__(2147483648L) x32;
  36. #if IS_ULONG(X32TYPE)
  37. #if !__cplusplus
  38. /******************************************************************************
  39. * Under pre-C99 ISO C, unsigned long is attempted for decimal integer literals
  40. * that do not have a suffix containing "u" or "U" if the literal does not fit
  41. * within the range of int or long. See 6.1.3.2 paragraph 5.
  42. ******************************************************************************/
  43. // expected-warning@39 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C89; this literal will have type 'long long' in C99 onwards}}
  44. // expected-warning@40 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C89; this literal will have type 'long long' in C99 onwards}}
  45. // expected-warning@41 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C89; this literal will have type 'long long' in C99 onwards}}
  46. #else
  47. /******************************************************************************
  48. * Under pre-C++11 ISO C++, the same holds if the literal contains an "l" or "L"
  49. * in its suffix; otherwise, the behavior is undefined. See 2.13.1 [lex.icon]
  50. * paragraph 2.
  51. ******************************************************************************/
  52. // expected-warning@39 {{integer literal is too large to be represented in type 'long' and is subject to undefined behavior under C++98, interpreting as 'unsigned long'; this literal will have type 'long long' in C++11 onwards}}
  53. // expected-warning@40 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C++98; this literal will have type 'long long' in C++11 onwards}}
  54. // expected-warning@41 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C++98; this literal will have type 'long long' in C++11 onwards}}
  55. #endif
  56. #endif
  57. #ifdef X64TYPE
  58. /******************************************************************************
  59. * Test 2^63 as a decimal literal with no suffix and with the "l" and "L" cases.
  60. ******************************************************************************/
  61. extern X64TYPE x64;
  62. extern __typeof__(9223372036854775808) x64;
  63. extern __typeof__(9223372036854775808l) x64;
  64. extern __typeof__(9223372036854775808L) x64;
  65. #if IS_ULONG(X64TYPE)
  66. #if !__cplusplus
  67. /******************************************************************************
  68. * Under pre-C99 ISO C, unsigned long is attempted for decimal integer literals
  69. * that do not have a suffix containing "u" or "U" if the literal does not fit
  70. * within the range of int or long. See 6.1.3.2 paragraph 5.
  71. ******************************************************************************/
  72. // expected-warning@74 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C89; this literal will be ill-formed in C99 onwards}}
  73. // expected-warning@75 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C89; this literal will be ill-formed in C99 onwards}}
  74. // expected-warning@76 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C89; this literal will be ill-formed in C99 onwards}}
  75. #else
  76. /******************************************************************************
  77. * Under pre-C++11 ISO C++, the same holds if the literal contains an "l" or "L"
  78. * in its suffix; otherwise, the behavior is undefined. See 2.13.1 [lex.icon]
  79. * paragraph 2.
  80. ******************************************************************************/
  81. // expected-warning@74 {{integer literal is too large to be represented in type 'long' and is subject to undefined behavior under C++98, interpreting as 'unsigned long'; this literal will be ill-formed in C++11 onwards}}
  82. // expected-warning@75 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C++98; this literal will be ill-formed in C++11 onwards}}
  83. // expected-warning@76 {{integer literal is too large to be represented in type 'long', interpreting as 'unsigned long' per C++98; this literal will be ill-formed in C++11 onwards}}
  84. #endif
  85. #else
  86. /******************************************************************************
  87. * The status quo in C99/C++11-and-later modes for the literals in question is
  88. * to interpret them as unsigned as an extension.
  89. ******************************************************************************/
  90. // expected-warning@74 {{integer literal is too large to be represented in a signed integer type, interpreting as unsigned}}
  91. // expected-warning@75 {{integer literal is too large to be represented in a signed integer type, interpreting as unsigned}}
  92. // expected-warning@76 {{integer literal is too large to be represented in a signed integer type, interpreting as unsigned}}
  93. #endif
  94. #endif
  95. /******************************************************************************
  96. * Test preprocessor arithmetic with 2^31 as a decimal literal with no suffix
  97. * and with the "l" and "L" cases.
  98. ******************************************************************************/
  99. #if !IS_ULONG(X32TYPE)
  100. /******************************************************************************
  101. * If the literal is signed without need for the modified range of the signed
  102. * integer types within the controlling constant expression for conditional
  103. * inclusion, then it will also be signed with said modified range.
  104. ******************************************************************************/
  105. #define EXPR(X) ((X - X) - 1 < 0)
  106. #else
  107. /******************************************************************************
  108. * Strictly speaking, in pre-C99/C++11 ISO C/C++, the preprocessor arithmetic is
  109. * evaluated with the range of long/unsigned long; however, both Clang and GCC
  110. * evaluate using 64-bits even when long/unsigned long are 32-bits outside of
  111. * preprocessing.
  112. *
  113. * If the range used becomes 32-bits, then this test will enforce the treatment
  114. * as unsigned of the literals in question.
  115. *
  116. * Note:
  117. * Under pre-C99/C++11 ISO C/C++, whether the interpretation of the literal is
  118. * affected by the modified range of the signed and unsigned integer types
  119. * within the controlling constant expression for conditional inclusion is
  120. * unclear.
  121. ******************************************************************************/
  122. #define PP_LONG_MAX ((0ul - 1ul) >> 1)
  123. #define EXPR(X) \
  124. (PP_LONG_MAX >= 0x80000000 || (X - X) - 1 > 0) // either 2^31 fits into a
  125. // preprocessor "long" or the
  126. // literals in question are
  127. // unsigned
  128. #endif
  129. #if !(EXPR(2147483648) && EXPR(2147483648l) && EXPR(2147483648L))
  130. #error Unexpected signedness or conversion behavior
  131. #endif