]> granicus.if.org Git - clang/commitdiff
Disable -Wtautological-constant-out-of-range-compare in template instantiations.
authorRichard Trieu <rtrieu@google.com>
Fri, 1 Nov 2013 21:19:43 +0000 (21:19 +0000)
committerRichard Trieu <rtrieu@google.com>
Fri, 1 Nov 2013 21:19:43 +0000 (21:19 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@193887 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaChecking.cpp
test/SemaCXX/compare.cpp

index aa9ba2c8e68d4c50db92d3db94dd1b32351f8112..772fee32cebd6a19eb080b73e1b02b0838db0bd0 100644 (file)
@@ -4806,6 +4806,10 @@ static void DiagnoseOutOfRangeComparison(Sema &S, BinaryOperator *E,
                                          Expr *Constant, Expr *Other,
                                          llvm::APSInt Value,
                                          bool RhsConstant) {
+  // Disable warning in template instantiations.
+  if (!S.ActiveTemplateInstantiations.empty())
+    return;
+
   // 0 values are handled later by CheckTrivialUnsignedComparison().
   if (Value == 0)
     return;
index feb1ccb9a20dddc5bcc2a36f6ec4ae3fd35cc58d..22f2565882c4aa7b0a734738b1bf2aae10865cee 100644 (file)
@@ -355,3 +355,29 @@ void test9(int x) {
   };
   (void)((E)x == 1);
 }
+
+namespace templates {
+  template<class T> T max();
+
+  template<> constexpr int max<int>() { return 2147483647; };
+
+  template<typename T>
+  bool less_than_max(short num, T value) {
+    const T vmax = max<T>();
+    return (vmax >= num);  // no warning
+  }
+
+  template<typename T>
+  bool less_than_max(short num) {
+    // This should trigger one warning on the template pattern, and not a
+    // warning per specialization.
+    return num < max<int>();  // expected-warning{{comparison of constant 2147483647 with expression of type 'short' is always true}}
+  }
+
+  void test10(short num, int x) {
+    less_than_max(num, x);
+    less_than_max<int>(num);
+    less_than_max<long>(num);
+    less_than_max<short>(num);
+  }
+}