From 311cb2b3221f6253ef86580151662f6c6788a320 Mon Sep 17 00:00:00 2001
From: Richard Trieu <rtrieu@google.com>
Date: Fri, 1 Nov 2013 21:19:43 +0000
Subject: [PATCH] Disable -Wtautological-constant-out-of-range-compare in
 template instantiations.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@193887 91177308-0d34-0410-b5e6-96231b3b80d8
---
 lib/Sema/SemaChecking.cpp |  4 ++++
 test/SemaCXX/compare.cpp  | 26 ++++++++++++++++++++++++++
 2 files changed, 30 insertions(+)

diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp
index aa9ba2c8e6..772fee32ce 100644
--- a/lib/Sema/SemaChecking.cpp
+++ b/lib/Sema/SemaChecking.cpp
@@ -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;
diff --git a/test/SemaCXX/compare.cpp b/test/SemaCXX/compare.cpp
index feb1ccb9a2..22f2565882 100644
--- a/test/SemaCXX/compare.cpp
+++ b/test/SemaCXX/compare.cpp
@@ -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);
+  }
+}
-- 
2.40.0