]> granicus.if.org Git - clang/commitdiff
The result type of logical || and && is bool in C++. Fixes PR5206.
authorAnders Carlsson <andersca@mac.com>
Fri, 16 Oct 2009 01:44:21 +0000 (01:44 +0000)
committerAnders Carlsson <andersca@mac.com>
Fri, 16 Oct 2009 01:44:21 +0000 (01:44 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@84231 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Sema/SemaExpr.cpp
test/SemaCXX/bool.cpp

index a946500660e8f8f7ee38851a2eaccc558fe26759..d818a5027e61a20296b07d8d90ccbe8144b886bb 100644 (file)
@@ -4812,9 +4812,16 @@ inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
   UsualUnaryConversions(lex);
   UsualUnaryConversions(rex);
 
-  if (lex->getType()->isScalarType() && rex->getType()->isScalarType())
-    return Context.IntTy;
-  return InvalidOperands(Loc, lex, rex);
+  if (!lex->getType()->isScalarType() || !rex->getType()->isScalarType())
+    return InvalidOperands(Loc, lex, rex);
+    
+  if (Context.getLangOptions().CPlusPlus) {
+    // C++ [expr.log.and]p2
+    // C++ [expr.log.or]p2
+    return Context.BoolTy;
+  }
+
+  return Context.IntTy;
 }
 
 /// IsReadonlyProperty - Verify that otherwise a valid l-value expression
index bc44c73d8cace4b06470575e5666350d213cd2f6..259c09c6cb8330ed14e3099f66dcb1e628b4461e 100644 (file)
@@ -16,3 +16,15 @@ void test(bool b)
 
   bool *b1 = (int *)0; // expected-error{{expected 'bool *'}}
 }
+
+// static_assert_arg_is_bool(x) compiles only if x is a bool.
+template <typename T>
+void static_assert_arg_is_bool(T x) {
+  bool* p = &x;
+}
+
+void test2() {
+  int n = 2;
+  static_assert_arg_is_bool(n && 4);
+  static_assert_arg_is_bool(n || 5);
+}