]> granicus.if.org Git - clang/commitdiff
Enable control flow pruning of float overflow warnings.
authorRichard Trieu <rtrieu@google.com>
Mon, 14 May 2018 23:21:48 +0000 (23:21 +0000)
committerRichard Trieu <rtrieu@google.com>
Mon, 14 May 2018 23:21:48 +0000 (23:21 +0000)
Like other conversion warnings, allow float overflow warnings to be disabled
in known dead paths of template instantiation.  This often occurs when a
template template type is a numeric type and the template will check the
range of the numeric type before performing the conversion.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@332310 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaChecking.cpp
test/SemaCXX/warn-float-conversion.cpp

index 74ecf5fef79558639c30d4dc1d2fe7783e4b8e41..f8ab8a6b385e6b2b0480de3f201676fc513bff56 100644 (file)
@@ -9673,7 +9673,8 @@ static void DiagnoseFloatingImpCast(Sema &S, Expr *E, QualType T,
     return DiagnoseImpCast(
         S, E, T, CContext,
         IsLiteral ? diag::warn_impcast_literal_float_to_integer_out_of_range
-                  : diag::warn_impcast_float_to_integer_out_of_range);
+                  : diag::warn_impcast_float_to_integer_out_of_range,
+        PruneWarnings);
 
   unsigned DiagID = 0;
   if (IsLiteral) {
index 2e89acb3d681b44e00df94e361d8f6038b9bc54c..a3d178622c7d70dbf9624a895c50abb6c97bf4a1 100644 (file)
@@ -89,4 +89,38 @@ void TestOverflow() {
 
   char e = 1.0 / 0.0;  // expected-warning{{implicit conversion of out of range value from 'double' to 'char' is undefined}}
 }
+
+
+template <typename T>
+class Check {
+ public:
+  static constexpr bool Safe();
+};
+
+template<>
+constexpr bool Check<char>::Safe() { return false; }
+
+template<>
+constexpr bool Check<float>::Safe() { return true; }
+
+template <typename T>
+T run1(T t) {
+  const float ret = 800;
+  return ret;  // expected-warning {{implicit conversion of out of range value from 'const float' to 'char' is undefined}}
+}
+
+template <typename T>
+T run2(T t) {
+  const float ret = 800;
+  if (Check<T>::Safe())
+    return ret;
+  else
+    return t;
+}
+
+void test() {
+  float a = run1(a) + run2(a);
+  char b = run1(b) + run2(b);  // expected-note {{in instantiation of function template specialization 'run1<char>' requested here}}
+}
+
 #endif  // OVERFLOW