[PATCH] D140860: [Diagnostics][NFC] Fix -Wlogical-op-parentheses warning inconsistency for const and constexpr values

Takuya Shimizu via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon Jan 2 16:41:00 PST 2023


hazohelet created this revision.
hazohelet added a project: clang.
Herald added a project: All.
hazohelet requested review of this revision.

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 '||' [-Wlogical-op-parentheses]

  const bool t = true;
  bool tt = true || false && t; // No warning

  const bool t = false;
  bool tt = true || false && t; // Warning: '&&' within '||' [-Wlogical-op-parentheses]

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.


https://reviews.llvm.org/D140860

Files:
  clang/lib/AST/ExprConstant.cpp
  clang/lib/Sema/SemaExpr.cpp
  clang/test/Sema/logical-op-parentheses.c
  clang/test/Sema/logical-op-parentheses.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D140860.485892.patch
Type: text/x-patch
Size: 7012 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230103/7575b5cc/attachment-0001.bin>


More information about the cfe-commits mailing list