Expand the coverage of the warning for constants on the RHS of logical operands:

return f() || -1;

where the user meant to write '|'.

This bootstraps without any additional warnings.

Patch by Richard Trieu.

git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@132327 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Chandler Carruth 2011-05-31 05:41:42 +00:00
Родитель 049f6d0239
Коммит 0683a1418b
5 изменённых файлов: 59 добавлений и 8 удалений

Просмотреть файл

@ -7797,13 +7797,15 @@ inline QualType Sema::CheckLogicalOperands( // C99 6.5.[13,14]
// If the RHS can be constant folded, and if it constant folds to something
// that isn't 0 or 1 (which indicate a potential logical operation that
// happened to fold to true/false) then warn.
// Parens on the RHS are ignored.
Expr::EvalResult Result;
if (rex.get()->Evaluate(Result, Context) && !Result.HasSideEffects &&
Result.Val.getInt() != 0 && Result.Val.getInt() != 1) {
Diag(Loc, diag::warn_logical_instead_of_bitwise)
<< rex.get()->getSourceRange()
<< (Opc == BO_LAnd ? "&&" : "||")
<< (Opc == BO_LAnd ? "&" : "|");
if (rex.get()->Evaluate(Result, Context) && !Result.HasSideEffects)
if ((getLangOptions().Bool && !rex.get()->getType()->isBooleanType()) ||
(Result.Val.getInt() != 0 && Result.Val.getInt() != 1)) {
Diag(Loc, diag::warn_logical_instead_of_bitwise)
<< rex.get()->getSourceRange()
<< (Opc == BO_LAnd ? "&&" : "||")
<< (Opc == BO_LAnd ? "&" : "|");
}
}

Просмотреть файл

@ -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;

Просмотреть файл

@ -189,6 +189,24 @@ int test20(int x) {
// no warning, this is an idiom for "true" in old C style.
return x && (signed char)1;
return x || 0;
return x || 1;
return x || -1; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
return x || 5; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
return x && 0;
return x && 1;
return x && -1; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
return x && 5; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
return x || (0);
return x || (1);
return x || (-1); // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
return x || (5); // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
return x && (0);
return x && (1);
return x && (-1); // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
return x && (5); // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
}
struct Test21; // expected-note 2 {{forward declaration}}

Просмотреть файл

@ -32,3 +32,34 @@ namespace test1 {
bar(x += E_zero); // expected-error {{incompatible type}}
}
}
int test2(int x) {
return x && 4; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
return x && sizeof(int) == 4; // no warning, RHS is logical op.
return x && true;
return x && false;
return x || true;
return x || false;
return x && (unsigned)0; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
return x || (unsigned)1; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
return x || 0; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
return x || 1; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
return x || -1; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
return x || 5; // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
return x && 0; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
return x && 1; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
return x && -1; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
return x && 5; // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
return x || (0); // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
return x || (1); // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
return x || (-1); // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
return x || (5); // expected-warning {{use of logical || with constant operand; switch to bitwise | or remove constant}}
return x && (0); // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
return x && (1); // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
return x && (-1); // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
return x && (5); // expected-warning {{use of logical && with constant operand; switch to bitwise & or remove constant}}
}

Просмотреть файл

@ -8,7 +8,7 @@ void test() {
}
int n = 3;
switch (n && 1) { // expected-warning {{bool}}
switch (n && true) { // expected-warning {{bool}}
case 1:
break;
}