]> granicus.if.org Git - clang/commitdiff
PR39628 Treat all non-zero values as 'true' in bool compound-assignment
authorRichard Smith <richard-llvm@metafoo.co.uk>
Mon, 12 Nov 2018 20:11:57 +0000 (20:11 +0000)
committerRichard Smith <richard-llvm@metafoo.co.uk>
Mon, 12 Nov 2018 20:11:57 +0000 (20:11 +0000)
in constant evaluation, not just odd values.

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

lib/AST/ExprConstant.cpp
test/SemaCXX/constant-expression-cxx1y.cpp

index 30800568a22a35cdbeafbb7686c345f40c267eeb..b0dc7d4a4a9593e93f655e2ab47ce11c00363612 100644 (file)
@@ -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;
 }
 
index 00df2e5c77a411df2723db71c536a4b45ed4cb89..eb555737d121d32cecc9565e85527fd58464c428 100644 (file)
@@ -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(), "");
 }