diff options
author | Richard Smith <richard-llvm@metafoo.co.uk> | 2018-11-12 20:11:57 +0000 |
---|---|---|
committer | Richard Smith <richard-llvm@metafoo.co.uk> | 2018-11-12 20:11:57 +0000 |
commit | bd844e0de7aca247ec0a1a6ecd2742d99a86d15d (patch) | |
tree | 52be3d6035113a40dc3b16167fe9954fefc0f1d0 | |
parent | b8d8db30ea54a9394e5387410d98fcaae6cd7336 (diff) | |
download | bcm5719-llvm-bd844e0de7aca247ec0a1a6ecd2742d99a86d15d.tar.gz bcm5719-llvm-bd844e0de7aca247ec0a1a6ecd2742d99a86d15d.zip |
PR39628 Treat all non-zero values as 'true' in bool compound-assignment
in constant evaluation, not just odd values.
llvm-svn: 346699
-rw-r--r-- | clang/lib/AST/ExprConstant.cpp | 5 | ||||
-rw-r--r-- | clang/test/SemaCXX/constant-expression-cxx1y.cpp | 26 |
2 files changed, 28 insertions, 3 deletions
diff --git a/clang/lib/AST/ExprConstant.cpp b/clang/lib/AST/ExprConstant.cpp index 30800568a22..b0dc7d4a4a9 100644 --- a/clang/lib/AST/ExprConstant.cpp +++ b/clang/lib/AST/ExprConstant.cpp @@ -2074,11 +2074,12 @@ static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E, QualType DestType, QualType SrcType, const APSInt &Value) { unsigned DestWidth = Info.Ctx.getIntWidth(DestType); - APSInt Result = Value; // Figure out if this is a truncate, extend or noop cast. // If the input is signed, do a sign extend, noop, or truncate. - Result = Result.extOrTrunc(DestWidth); + APSInt Result = Value.extOrTrunc(DestWidth); Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType()); + if (DestType->isBooleanType()) + Result = Value.getBoolValue(); return Result; } diff --git a/clang/test/SemaCXX/constant-expression-cxx1y.cpp b/clang/test/SemaCXX/constant-expression-cxx1y.cpp index 00df2e5c77a..eb555737d12 100644 --- a/clang/test/SemaCXX/constant-expression-cxx1y.cpp +++ b/clang/test/SemaCXX/constant-expression-cxx1y.cpp @@ -374,6 +374,30 @@ namespace compound_assign { } static_assert(test_float(), ""); + constexpr bool test_bool() { + bool b = false; + b |= 2; + if (b != true) return false; + b <<= 1; + if (b != true) return false; + b *= 2; + if (b != true) return false; + b -= 1; + if (b != false) return false; + b -= 1; + if (b != true) return false; + b += -1; + if (b != false) return false; + b += 1; + if (b != true) return false; + b += 1; + if (b != true) return false; + b ^= b; + if (b != false) return false; + return true; + } + static_assert(test_bool(), ""); + constexpr bool test_ptr() { int arr[123] = {}; int *p = arr; @@ -879,7 +903,7 @@ namespace Bitfields { --a.n; --a.u; a.n = -a.n * 3; - return a.b == false && a.n == 3 && a.u == 31; + return a.b == true && a.n == 3 && a.u == 31; } static_assert(test(), ""); } |