[cfe-commits] r108260 - in /cfe/trunk: include/clang/Basic/DiagnosticSemaKinds.td lib/Sema/Sema.h lib/Sema/SemaExpr.cpp test/CodeGenCXX/static-init-2.cpp test/Sema/exprs.c test/Sema/i-c-e.c test/Sema/switch.c test/SemaCXX/bool.cpp test/SemaCXX/switch.cpp
Chris Lattner
sabre at nondot.org
Tue Jul 13 12:41:32 PDT 2010
Author: lattner
Date: Tue Jul 13 14:41:32 2010
New Revision: 108260
URL: http://llvm.org/viewvc/llvm-project?rev=108260&view=rev
Log:
Add a warning to catch a bug recently caught by code review, like this:
t2.c:2:12: warning: use of logical && with constant operand; switch to bitwise &
or remove constant [-Wlogical-bitwise-confusion]
return x && 4;
^ ~
wording improvement suggestions are welcome.
Modified:
cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
cfe/trunk/lib/Sema/Sema.h
cfe/trunk/lib/Sema/SemaExpr.cpp
cfe/trunk/test/CodeGenCXX/static-init-2.cpp
cfe/trunk/test/Sema/exprs.c
cfe/trunk/test/Sema/i-c-e.c
cfe/trunk/test/Sema/switch.c
cfe/trunk/test/SemaCXX/bool.cpp
cfe/trunk/test/SemaCXX/switch.cpp
Modified: cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td?rev=108260&r1=108259&r2=108260&view=diff
==============================================================================
--- cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td (original)
+++ cfe/trunk/include/clang/Basic/DiagnosticSemaKinds.td Tue Jul 13 14:41:32 2010
@@ -1986,6 +1986,10 @@
"place parentheses around the %0 expression to evaluate it first">;
def note_precedence_bitwise_silence : Note<
"place parentheses around the %0 expression to silence this warning">;
+
+def warn_logical_instead_of_bitwise : Warning<
+ "use of logical %0 with constant operand; switch to bitwise %1 or "
+ "remove constant">, InGroup<DiagGroup<"logical-bitwise-confusion">>;
def err_sizeof_nonfragile_interface : Error<
"invalid application of '%select{alignof|sizeof}1' to interface %0 in "
Modified: cfe/trunk/lib/Sema/Sema.h
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/Sema.h?rev=108260&r1=108259&r2=108260&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/Sema.h (original)
+++ cfe/trunk/lib/Sema/Sema.h Tue Jul 13 14:41:32 2010
@@ -4353,7 +4353,7 @@
QualType CheckBitwiseOperands( // C99 6.5.[10...12]
Expr *&lex, Expr *&rex, SourceLocation OpLoc, bool isCompAssign = false);
QualType CheckLogicalOperands( // C99 6.5.[13,14]
- Expr *&lex, Expr *&rex, SourceLocation OpLoc);
+ Expr *&lex, Expr *&rex, SourceLocation OpLoc, unsigned Opc);
// CheckAssignmentOperands is used for both simple and compound assignment.
// For simple assignment, pass both expressions and a null converted type.
// For compound assignment, pass both expressions and the converted type.
Modified: cfe/trunk/lib/Sema/SemaExpr.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/lib/Sema/SemaExpr.cpp?rev=108260&r1=108259&r2=108260&view=diff
==============================================================================
--- cfe/trunk/lib/Sema/SemaExpr.cpp (original)
+++ cfe/trunk/lib/Sema/SemaExpr.cpp Tue Jul 13 14:41:32 2010
@@ -5735,7 +5735,21 @@
}
inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
- Expr *&lex, Expr *&rex, SourceLocation Loc) {
+ Expr *&lex, Expr *&rex, SourceLocation Loc, unsigned Opc) {
+
+ // Diagnose cases where the user write a logical and/or but probably meant a
+ // bitwise one. We do this when the LHS is a non-bool integer and the RHS
+ // is a constant.
+ if (lex->getType()->isIntegerType() && !lex->getType()->isBooleanType() &&
+ rex->getType()->isIntegerType() && rex->isEvaluatable(Context) &&
+ !Loc.isMacroID())
+ Diag(Loc, diag::warn_logical_instead_of_bitwise)
+ << rex->getSourceRange()
+ << (Opc == BinaryOperator::LAnd ? "&&" : "||")
+ << (Opc == BinaryOperator::LAnd ? "&" : "|");
+
+
+
if (!Context.getLangOptions().CPlusPlus) {
UsualUnaryConversions(lex);
UsualUnaryConversions(rex);
@@ -6363,7 +6377,7 @@
break;
case BinaryOperator::LAnd:
case BinaryOperator::LOr:
- ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc);
+ ResultTy = CheckLogicalOperands(lhs, rhs, OpLoc, Opc);
break;
case BinaryOperator::MulAssign:
case BinaryOperator::DivAssign:
Modified: cfe/trunk/test/CodeGenCXX/static-init-2.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/CodeGenCXX/static-init-2.cpp?rev=108260&r1=108259&r2=108260&view=diff
==============================================================================
--- cfe/trunk/test/CodeGenCXX/static-init-2.cpp (original)
+++ cfe/trunk/test/CodeGenCXX/static-init-2.cpp Tue Jul 13 14:41:32 2010
@@ -3,4 +3,4 @@
// Make sure we don't crash generating y; its value is constant, but the
// initializer has side effects, so EmitConstantExpr should fail.
int x();
-int y = x() && 0;
+int y = x() && 0; // expected-warning {{use of logical && with constant operand}}
Modified: cfe/trunk/test/Sema/exprs.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/exprs.c?rev=108260&r1=108259&r2=108260&view=diff
==============================================================================
--- cfe/trunk/test/Sema/exprs.c (original)
+++ cfe/trunk/test/Sema/exprs.c Tue Jul 13 14:41:32 2010
@@ -142,3 +142,6 @@
*(volatile int*)0 = 0; // Ok.
}
+int test20(int x) {
+ return x && 4; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
+}
Modified: cfe/trunk/test/Sema/i-c-e.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/i-c-e.c?rev=108260&r1=108259&r2=108260&view=diff
==============================================================================
--- cfe/trunk/test/Sema/i-c-e.c (original)
+++ cfe/trunk/test/Sema/i-c-e.c Tue Jul 13 14:41:32 2010
@@ -51,7 +51,8 @@
// Comma tests
int comma1[0?1,2:3]; // expected-warning {{expression result unused}}
-int comma2[1||(1,2)]; // expected-warning {{expression result unused}}
+int comma2[1||(1,2)]; // expected-warning {{expression result unused}} \
+ // expected-warning {{use of logical || with constant operand}}
int comma3[(1,2)]; // expected-warning {{size of static array must be an integer constant expression}} \
// expected-warning {{expression result unused}}
Modified: cfe/trunk/test/Sema/switch.c
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/Sema/switch.c?rev=108260&r1=108259&r2=108260&view=diff
==============================================================================
--- cfe/trunk/test/Sema/switch.c (original)
+++ cfe/trunk/test/Sema/switch.c Tue Jul 13 14:41:32 2010
@@ -50,12 +50,14 @@
}
switch (cond) {
- case g() && 0: // expected-error {{expression is not an integer constant expression}} // expected-note {{subexpression not valid in an integer constant expression}}
+ case g() && 0: // expected-error {{expression is not an integer constant expression}} // expected-note {{subexpression not valid in an integer constant expression}} \
+ expected-warning {{use of logical && with constant operand}}
break;
}
switch (cond) {
- case 0 ... g() || 1: // expected-error {{expression is not an integer constant expression}} // expected-note {{subexpression not valid in an integer constant expression}}
+ case 0 ... g() || 1: // expected-error {{expression is not an integer constant expression}} // expected-note {{subexpression not valid in an integer constant expression}} \\
+ expected-warning {{use of logical || with constant operand}}
break;
}
}
Modified: cfe/trunk/test/SemaCXX/bool.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/bool.cpp?rev=108260&r1=108259&r2=108260&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/bool.cpp (original)
+++ cfe/trunk/test/SemaCXX/bool.cpp Tue Jul 13 14:41:32 2010
@@ -25,6 +25,6 @@
void test2() {
int n = 2;
- static_assert_arg_is_bool(n && 4);
- static_assert_arg_is_bool(n || 5);
+ static_assert_arg_is_bool(n && 4); // expected-warning {{use of logical && with constant operand}}
+ static_assert_arg_is_bool(n || 5); // expected-warning {{use of logical || with constant operand}}
}
Modified: cfe/trunk/test/SemaCXX/switch.cpp
URL: http://llvm.org/viewvc/llvm-project/cfe/trunk/test/SemaCXX/switch.cpp?rev=108260&r1=108259&r2=108260&view=diff
==============================================================================
--- cfe/trunk/test/SemaCXX/switch.cpp (original)
+++ cfe/trunk/test/SemaCXX/switch.cpp Tue Jul 13 14:41:32 2010
@@ -8,7 +8,8 @@
}
int n = 3;
- switch (n && 1) { // expected-warning {{bool}}
+ switch (n && 1) { // expected-warning {{bool}} \
+ // expected-warning {{use of logical && with constant operand}}
case 1:
break;
}
More information about the cfe-commits
mailing list