brintos

brintos / llvm-project-archived public Read only

0
0
Text · 3.1 KiB · 180b789 Raw
74 lines · cpp
1// RUN: %check_clang_tidy %s bugprone-narrowing-conversions %t \2// RUN: -- -- -target x86_64-unknown-linux -fsigned-char3 4namespace floats {5 6void narrow_constant_floating_point_to_int_not_ok(double d) {7  int i = 0;8  i += 0.5;9  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from constant 'double' to 'int' [bugprone-narrowing-conversions]10  i += 0.5f;11  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from constant 'float' to 'int' [bugprone-narrowing-conversions]12  i *= 0.5f;13  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from constant 'float' to 'int' [bugprone-narrowing-conversions]14  i /= 0.5f;15  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from constant 'float' to 'int' [bugprone-narrowing-conversions]16  i += (double)0.5f;17  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from constant 'double' to 'int' [bugprone-narrowing-conversions]18  i += 2.0;19  i += 2.0f;20}21 22double operator"" _double(unsigned long long);23 24float narrow_double_to_float_return() {25  return 0.5;26}27 28void narrow_double_to_float_not_ok(double d) {29  float f;30  f = d;31  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: narrowing conversion from 'double' to 'float' [bugprone-narrowing-conversions]32  f = 15_double;33  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: narrowing conversion from 'double' to 'float' [bugprone-narrowing-conversions]34  f += d;35  // CHECK-MESSAGES: :[[@LINE-1]]:8: warning: narrowing conversion from 'double' to 'float' [bugprone-narrowing-conversions]36  f = narrow_double_to_float_return();37}38 39float narrow_float16_to_float_return(_Float16 f) {40  return f;41}42 43_Float16 narrow_float_to_float16_return(float f) {44  return f;45  // CHECK-MESSAGES: :[[@LINE-1]]:10: warning: narrowing conversion from 'float' to '_Float16' [bugprone-narrowing-conversions]46}47 48void narrow_fp_constants() {49  float f;50  f = 0.5; // [dcl.init.list] 7.2 : in-range fp constant to narrower float is not a narrowing.51 52  f = __builtin_huge_valf();  // max float is not narrowing.53  f = -__builtin_huge_valf(); // -max float is not narrowing.54  f = __builtin_inff();       // float infinity is not narrowing.55  f = __builtin_nanf("0");    // float NaN is not narrowing.56 57  f = __builtin_huge_val(); // max double is not within-range of float.58  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: narrowing conversion from constant 'double' to 'float' [bugprone-narrowing-conversions]59  f = -__builtin_huge_val(); // -max double is not within-range of float.60  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: narrowing conversion from constant 'double' to 'float' [bugprone-narrowing-conversions]61  f = __builtin_inf(); // double infinity is not within-range of float.62  // CHECK-MESSAGES: :[[@LINE-1]]:7: warning: narrowing conversion from constant 'double' to 'float' [bugprone-narrowing-conversions]63  f = __builtin_nan("0"); // double NaN is not narrowing.64}65 66double false_positive_const_qualified_cast(bool t) {67  double b = 1.0;68  constexpr double a = __builtin_huge_val();69  // PR49498 The constness difference of 'a' and 'b' results in an implicit cast.70  return t ? b : a;71}72 73} // namespace floats74