[Diagnostics] Warn if '<<' in bool context with -Wint-in-bool-context (GCC compatibility)
Extracted from D63082, addressed review comments related to a warning message.
llvm-svn: 372612
diff --git a/clang/lib/Sema/SemaChecking.cpp b/clang/lib/Sema/SemaChecking.cpp
index 7aeace5..cbe7e9a 100644
--- a/clang/lib/Sema/SemaChecking.cpp
+++ b/clang/lib/Sema/SemaChecking.cpp
@@ -11256,10 +11256,16 @@
return true;
}
-static void DiagnoseIntInBoolContext(Sema &S, const Expr *E) {
+static void DiagnoseIntInBoolContext(Sema &S, Expr *E) {
E = E->IgnoreParenImpCasts();
SourceLocation ExprLoc = E->getExprLoc();
+ if (const auto *BO = dyn_cast<BinaryOperator>(E)) {
+ BinaryOperator::Opcode Opc = BO->getOpcode();
+ if (Opc == BO_Shl)
+ S.Diag(ExprLoc, diag::warn_left_shift_in_bool_context) << E;
+ }
+
if (const auto *CO = dyn_cast<ConditionalOperator>(E)) {
const auto *LHS = dyn_cast<IntegerLiteral>(CO->getTrueExpr());
if (!LHS) {
@@ -11585,6 +11591,9 @@
S.DiscardMisalignedMemberAddress(Target, E);
+ if (Target->isBooleanType())
+ DiagnoseIntInBoolContext(S, E);
+
if (!Source->isIntegerType() || !Target->isIntegerType())
return;