Fix -Wlogical-op-parentheses warning inconsistency for const and constexpr values
authorTakuya Shimizu <shimizu2486@gmail.com>
Fri, 13 Jan 2023 13:20:14 +0000 (08:20 -0500)
committerAaron Ballman <aaron@aaronballman.com>
Fri, 13 Jan 2023 13:21:41 +0000 (08:21 -0500)
commit84e3fdc019412adc09b18a49063e006bd6e7efaa
tree4d4d219a50dcf82fd3990633d53888913db40c0e
parenteaa1f46f11f523104be54de058e812c9d7059819
Fix -Wlogical-op-parentheses warning inconsistency for const and constexpr values

When using the && operator within a || operator, both Clang and GCC
produce a warning for potentially confusing operator precedence.
However, Clang avoids this warning for certain patterns, such as
a && b || 0 or a || b && 1, where the operator precedence of && and ||
does not change the result.

However, this behavior appears inconsistent when using the const or
constexpr qualifiers. For example:

bool t = true;
bool tt = true || false && t; // Warning: '&&' within '||'
const bool t = true;
bool tt = true || false && t; // No warning
const bool t = false;
bool tt = true || false && t; // Warning: '&&' within '||'

The second example does not produce a warning because
true || false && t matches the a || b && 1 pattern, while the third one
does not match any of them.

This behavior can lead to the lack of warnings for complicated
constexpr expressions. Clang should only suppress this warning when
literal values are placed in the place of t in the examples above.

This patch adds the literal-or-not check to fix the inconsistent
warnings for && within || when using const or constexpr.
clang/docs/ReleaseNotes.rst
clang/lib/Sema/SemaExpr.cpp
clang/test/Sema/logical-op-parentheses.c