Can someone review this? Thanks.
C++ 5.[14,15]: the && and || operators produce a bool.
C has no bool type and thus the rule is different there:
C99 6.5.[13,14]: the && and || operators produce an int.
Currently clang always thinks the result is an int (the C99 behavior). It
should consider the result type bool in C++ mode.
--
Zhanyong
Index: test/SemaCXX/bool.cpp
===================================================================
--- test/SemaCXX/bool.cpp (revision 84217)
+++ test/SemaCXX/bool.cpp (working copy)
@@ -16,3 +16,15 @@
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);
+}
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp (revision 84217)
+++ lib/Sema/SemaExpr.cpp (working copy)
@@ -4807,13 +4807,15 @@
return InvalidOperands(Loc, lex, rex);
}
-inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
+inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14], C++ 5.[14,15]
Expr *&lex, Expr *&rex, SourceLocation Loc) {
UsualUnaryConversions(lex);
UsualUnaryConversions(rex);
- if (lex->getType()->isScalarType() && rex->getType()->isScalarType())
- return Context.IntTy;
+ if (lex->getType()->isScalarType() && rex->getType()->isScalarType()) {
+ // && and || produce an int in C, but produce a bool in C++.
+ return getLangOptions().CPlusPlus ? Context.BoolTy : Context.IntTy;
+ }
return InvalidOperands(Loc, lex, rex);
}
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits