[Diagnostics] Bitwise negation of a boolean expr always evaluates to true; warn with -Wbool-operation

Requested here:
http://lists.llvm.org/pipermail/cfe-dev/2019-October/063452.html

llvm-svn: 373614
This commit is contained in:
David Bolvansky 2019-10-03 15:17:59 +00:00
parent 1fae74480b
commit b4ee523ffc
2 changed files with 23 additions and 1 deletions

View File

@ -13470,7 +13470,6 @@ ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc,
if (Input.isInvalid()) if (Input.isInvalid())
return ExprError(); return ExprError();
resultType = Input.get()->getType(); resultType = Input.get()->getType();
if (resultType->isDependentType()) if (resultType->isDependentType())
break; break;
// C99 6.5.3.3p1. We allow complex int and float as a GCC extension. // C99 6.5.3.3p1. We allow complex int and float as a GCC extension.
@ -13478,6 +13477,9 @@ ExprResult Sema::CreateBuiltinUnaryOp(SourceLocation OpLoc,
// C99 does not support '~' for complex conjugation. // C99 does not support '~' for complex conjugation.
Diag(OpLoc, diag::ext_integer_complement_complex) Diag(OpLoc, diag::ext_integer_complement_complex)
<< resultType << Input.get()->getSourceRange(); << resultType << Input.get()->getSourceRange();
else if (Input.get()->IgnoreParenImpCasts()->getType()->isBooleanType())
Diag(OpLoc, diag::warn_bitwise_negation_bool)
<< FixItHint::CreateReplacement(OpLoc, "!");
else if (resultType->hasIntegerRepresentation()) else if (resultType->hasIntegerRepresentation())
break; break;
else if (resultType->isExtVectorType() && Context.getLangOpts().OpenCL) { else if (resultType->isExtVectorType() && Context.getLangOpts().OpenCL) {

View File

@ -0,0 +1,20 @@
// RUN: %clang_cc1 -x c -fsyntax-only -verify -Wbool-operation %s
// RUN: %clang_cc1 -x c -fsyntax-only -verify %s
// RUN: %clang_cc1 -x c -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
// RUN: %clang_cc1 -x c++ -fsyntax-only -verify -Wbool-operation %s
// RUN: %clang_cc1 -x c++ -fsyntax-only -verify %s
// RUN: %clang_cc1 -x c++ -fsyntax-only -fdiagnostics-parseable-fixits %s 2>&1 | FileCheck %s
#ifdef __cplusplus
typedef bool boolean;
#else
typedef _Bool boolean;
#endif
void test(boolean b, int i) {
b = ~b; // expected-warning {{bitwise negation of a boolean expression always evaluates to 'true'}}
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:8}:"!"
b = ~(b); // expected-warning {{bitwise negation of a boolean expression always evaluates to 'true'}}
// CHECK: fix-it:"{{.*}}":{[[@LINE-1]]:7-[[@LINE-1]]:8}:"!"
b = ~i;
}