diff options
author | Anders Carlsson <andersca@mac.com> | 2009-10-16 01:44:21 +0000 |
---|---|---|
committer | Anders Carlsson <andersca@mac.com> | 2009-10-16 01:44:21 +0000 |
commit | 04905014fd854979ac12fc31a2b25fca37a93eb4 (patch) | |
tree | def4a211b222e9ef9797fa9be5d4e2f70278da60 | |
parent | 24ee804962ecef23eb040083eb668f9fa202e4c0 (diff) |
The result type of logical || and && is bool in C++. Fixes PR5206.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@84231 91177308-0d34-0410-b5e6-96231b3b80d8
-rw-r--r-- | lib/Sema/SemaExpr.cpp | 13 | ||||
-rw-r--r-- | test/SemaCXX/bool.cpp | 12 |
2 files changed, 22 insertions, 3 deletions
diff --git a/lib/Sema/SemaExpr.cpp b/lib/Sema/SemaExpr.cpp index a946500660..d818a5027e 100644 --- a/lib/Sema/SemaExpr.cpp +++ b/lib/Sema/SemaExpr.cpp @@ -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 diff --git a/test/SemaCXX/bool.cpp b/test/SemaCXX/bool.cpp index bc44c73d8c..259c09c6cb 100644 --- a/test/SemaCXX/bool.cpp +++ b/test/SemaCXX/bool.cpp @@ -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); +} |