]> granicus.if.org Git - clang/commitdiff
Make the tautological out of range warning use Sema::DiagRuntimeBehavior so that
authorRichard Trieu <rtrieu@google.com>
Fri, 10 Jan 2014 04:38:09 +0000 (04:38 +0000)
committerRichard Trieu <rtrieu@google.com>
Fri, 10 Jan 2014 04:38:09 +0000 (04:38 +0000)
the warning will not trigger on code protected by compile time checks.

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

lib/Sema/SemaChecking.cpp
test/SemaCXX/warn-tautological-compare.cpp [new file with mode: 0644]

index b6fc020b1de77de9344be4da7d30294aa1a55c0e..82b3da6c2e1a0039b703ee5680132a506896db11 100644 (file)
@@ -4970,9 +4970,11 @@ static void DiagnoseOutOfRangeComparison(Sema &S, BinaryOperator *E,
   else
     OS << Value;
 
-  S.Diag(E->getOperatorLoc(), diag::warn_out_of_range_compare)
-      << OS.str() << OtherT << IsTrue
-      << E->getLHS()->getSourceRange() << E->getRHS()->getSourceRange();
+  S.DiagRuntimeBehavior(E->getOperatorLoc(), E,
+                        S.PDiag(diag::warn_out_of_range_compare)
+                          << OS.str() << OtherT << IsTrue
+                          << E->getLHS()->getSourceRange()
+                          << E->getRHS()->getSourceRange());
 }
 
 /// Analyze the operands of the given comparison.  Implements the
diff --git a/test/SemaCXX/warn-tautological-compare.cpp b/test/SemaCXX/warn-tautological-compare.cpp
new file mode 100644 (file)
index 0000000..caea6bf
--- /dev/null
@@ -0,0 +1,27 @@
+// Force x86-64 because some of our heuristics are actually based
+// on integer sizes.
+
+// RUN: %clang_cc1 -triple x86_64-apple-darwin -fsyntax-only -verify -std=c++11 %s
+
+namespace RuntimeBehavior {
+  // Avoid emitting tautological compare warnings when the code already has
+  // compile time checks on variable sizes.
+
+  const int kintmax = 2147483647;
+  void test0(short x) {
+    if (sizeof(x) < sizeof(int) || x < kintmax) {}
+
+    if (x < kintmax) {}
+    // expected-warning@-1{{comparison of constant 2147483647 with expression of type 'short' is always true}}
+  }
+
+  void test1(short x) {
+    if (x < kintmax) {}
+    // expected-warning@-1{{comparison of constant 2147483647 with expression of type 'short' is always true}}
+
+    if (sizeof(x) < sizeof(int))
+      return;
+
+    if (x < kintmax) {}
+  }
+}