[PATCH] D124650: [clang-tidy] Simplify boolean expressions by DeMorgan's theorem

Richard via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Mon May 2 09:55:15 PDT 2022


LegalizeAdulthood marked 2 inline comments as done.
LegalizeAdulthood added inline comments.


================
Comment at: clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp:598-599
+  auto UnlessNotLHS = unless(hasLHS(NotOp));
+  // match !(!a || b)
+  Finder->addMatcher(unaryOperator(Not, hasUnaryOperand(binaryOperator(
+                                            Or, UnlessNotRHS, NotLHS, RHS)))
----------------
njames93 wrote:
> LegalizeAdulthood wrote:
> > njames93 wrote:
> > > Maybe I'm overthinking this, but how come you don't need the match on the ParenExpr?
> > > Is there some traversal mode?
> > How can you have a binaryOperator as the child of unaryOperator without parens?  The precedence doesn't allow it otherwise.
> Its more the point that the AST has the ParenExpr
> ```lang=c++
> !(a || !b);
> ```
> ```
> `-UnaryOperator <col:12, col:21> 'bool' prefix '!' cannot overflow
>   `-ParenExpr <col:13, col:21> 'bool'
>     `-BinaryOperator <col:14, col:20> 'bool' '||'
>       |-ImplicitCastExpr <col:14> 'bool' <LValueToRValue>
>       | `-DeclRefExpr <col:14> 'bool' lvalue ParmVar 0x55a0e17465d0 'a' 'bool'
>       `-UnaryOperator <col:19, col:20> 'bool' prefix '!' cannot overflow
>         `-ImplicitCastExpr <col:20> 'bool' <LValueToRValue>
>           `-DeclRefExpr <col:20> 'bool' lvalue ParmVar 0x55a0e1746648 'b' 'bool'
> ```
I've noticed that some matchers peek through parens, that must be what's going on here that allows it to match without explicitly matching the parenExpr.


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

https://reviews.llvm.org/D124650



More information about the cfe-commits mailing list