[PATCH] D66043: Add to -Wparentheses case of bitwise-and ("&") and bitwise-or ("|") verses conditional operator ("?:")

JF Bastien via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Aug 12 16:11:29 PDT 2019


jfb added inline comments.


================
Comment at: test/Sema/parentheses.c:156
+
+  (void)(x ^ b ? 1 : 2);  // no warning, ^ is often used as logical xor
+  (void)(x || b ? 1 : 2);  // no warning, logical operator
----------------
rtrieu wrote:
> jfb wrote:
> > rtrieu wrote:
> > > jfb wrote:
> > > > I don't understand why `^` is different. What do you mean by "often used as a logical xor`?
> > > In C++, there's the bitwise operators |, &, and ^.  And there's the logical operators || and &&.  Since there's no ^^ for a logical-xor, many people will just use the bitwise-xor ^ instead.  Since this isn't warning on logical operators, it makes sense to exclude the bitwise-xor that is often used as logical-xor.
> > So code is often buggy when it uses `|` and `&` as diagnosed by this patch, but is rarely buggy when it uses `^`?
> That's correct.  From my testing, &&, || and ^ all had low bug finding rates and didn't make sense to include into this warning while | and & had a high bug finding rate.
OK thanks for clarifying. Could you explain this instead of "logical xor"? It seems useful to know when reading your code that in your experience `^` wasn't a source of bugs as much as the others.


CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D66043/new/

https://reviews.llvm.org/D66043





More information about the cfe-commits mailing list