From d85e5f7c8c2df4766f2bbf92d7ea9898db5317a7 Mon Sep 17 00:00:00 2001 From: Richard Trieu Date: Fri, 10 Jan 2014 04:38:09 +0000 Subject: [PATCH] Make the tautological out of range warning use Sema::DiagRuntimeBehavior so that 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 | 8 ++++--- test/SemaCXX/warn-tautological-compare.cpp | 27 ++++++++++++++++++++++ 2 files changed, 32 insertions(+), 3 deletions(-) create mode 100644 test/SemaCXX/warn-tautological-compare.cpp diff --git a/lib/Sema/SemaChecking.cpp b/lib/Sema/SemaChecking.cpp index b6fc020b1d..82b3da6c2e 100644 --- a/lib/Sema/SemaChecking.cpp +++ b/lib/Sema/SemaChecking.cpp @@ -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 index 0000000000..caea6bf86e --- /dev/null +++ b/test/SemaCXX/warn-tautological-compare.cpp @@ -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) {} + } +} -- 2.49.0