[PATCH] D66043: Add to -Wparentheses case of bitwise-and ("&") and bitwise-or ("|") verses conditional operator ("?:")
Richard Trieu via Phabricator via cfe-commits
cfe-commits at lists.llvm.org
Mon Aug 12 19:38:01 PDT 2019
rtrieu updated this revision to Diff 214753.
rtrieu added a comment.
Update comments to explain why bitwise-xor is treated as a logical operator.
CHANGES SINCE LAST ACTION
https://reviews.llvm.org/D66043/new/
https://reviews.llvm.org/D66043
Files:
lib/Sema/SemaExpr.cpp
test/Sema/parentheses.c
Index: test/Sema/parentheses.c
===================================================================
--- test/Sema/parentheses.c
+++ test/Sema/parentheses.c
@@ -144,6 +144,23 @@
(void)(x + y > 0 ? 1 : 2); // no warning
(void)(x + (y > 0) ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '+'}} expected-note 2{{place parentheses}}
+
+ (void)(x | b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '|'}} expected-note 2{{place parentheses}}
+ (void)(x & b ? 1 : 2); // expected-warning {{operator '?:' has lower precedence than '&'}} expected-note 2{{place parentheses}}
+
+ (void)((x | b) ? 1 : 2); // no warning, has parentheses
+ (void)(x | (b ? 1 : 2)); // no warning, has parentheses
+ (void)((x & b) ? 1 : 2); // no warning, has parentheses
+ (void)(x & (b ? 1 : 2)); // no warning, has parentheses
+
+ // Only warn on uses of the bitwise operators, and not the logical operators.
+ // The bitwise operators are more likely to be bugs while the logical
+ // operators are more likely to be used correctly. Since there is no
+ // explicit logical-xor operator, the bitwise-xor is commonly used instead.
+ // For this warning, treat the bitwise-xor as if it were a logical operator.
+ (void)(x ^ b ? 1 : 2); // no warning, ^ is often used as logical xor
+ (void)(x || b ? 1 : 2); // no warning, logical operator
+ (void)(x && b ? 1 : 2); // no warning, logical operator
}
// RUN: not %clang_cc1 -fsyntax-only -Wparentheses -Werror -fdiagnostics-show-option %s 2>&1 | FileCheck %s -check-prefix=CHECK-FLAG
Index: lib/Sema/SemaExpr.cpp
===================================================================
--- lib/Sema/SemaExpr.cpp
+++ lib/Sema/SemaExpr.cpp
@@ -7485,7 +7485,12 @@
static bool IsArithmeticOp(BinaryOperatorKind Opc) {
return BinaryOperator::isAdditiveOp(Opc) ||
BinaryOperator::isMultiplicativeOp(Opc) ||
- BinaryOperator::isShiftOp(Opc);
+ BinaryOperator::isShiftOp(Opc) || Opc == BO_And || Opc == BO_Or;
+ // This only checks for bitwise-or and bitwise-and, but not bitwise-xor and
+ // not any of the logical operators. Bitwise-xor is commonly used as a
+ // logical-xor because there is no logical-xor operator. The logical
+ // operators, including uses of xor, have a high false positive rate for
+ // precedence warnings.
}
/// IsArithmeticBinaryExpr - Returns true if E is an arithmetic binary
-------------- next part --------------
A non-text attachment was scrubbed...
Name: D66043.214753.patch
Type: text/x-patch
Size: 2435 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190813/be529521/attachment.bin>
More information about the cfe-commits
mailing list