Make the following simplifications of boolean expressions:
!(!a || b) => ( a && !b) !( a || !b) => (!a && b) !(!a || !b) => ( a && b) !(!a && b) => ( a || !b) !( a && !b) => (!a || b) !(!a && !b) => ( a || b)
Fixes #55092
Paths
| Differential D124650
[clang-tidy] Simplify boolean expressions by DeMorgan's theorem AbandonedPublic Authored by LegalizeAdulthood on Apr 28 2022, 5:29 PM.
Details
Summary Make the following simplifications of boolean expressions: !(!a || b) => ( a && !b) !( a || !b) => (!a && b) !(!a || !b) => ( a && b) !(!a && b) => ( a || !b) !( a && !b) => (!a || b) !(!a && !b) => ( a || b) Fixes #55092
Diff Detail
Event TimelineComment Actions General question: There is the question of whether or not the replacement should have ()s around the whole Eugene.Zelenko added inline comments.
LegalizeAdulthood marked an inline comment as done. Comment ActionsSort changes by check name in docs Comment Actions
I suppose it's better to be safe than sorry, so I'll update the check to surround the replacement
Comment Actions Testing on VTK revealed this change: - this->SliceMapper->SetBackground((this->Background && - !(this->SliceFacesCamera && this->InternalResampleToScreenPixels && - !this->SeparateWindowLevelOperation))); + this->SliceMapper->SetBackground( + (this->Background && + (!this->SliceFacesCamera && this->InternalResampleToScreenPixels || + this->SeparateWindowLevelOperation))); Which is incorrect. So more improvement is needed for the case of !(x && y && !z) or !(x || y || !z) is encountered.
Comment Actions I also noticed that some of the simplifications being performed resulted in !(x || !y) coming out of other parts of the simplifier, so it should be the case that running this check on your code should result in no further changes to your code if you run the check again. LegalizeAdulthood added inline comments.
LegalizeAdulthood added inline comments.
Comment Actions
Hey, that looks great at a glance. I need to prepare for Utah C++ Programmers presentation on Wednesday, so I won't be able to revisit these recent reviews until next weekend probably.
Revision Contents
Diff 425935 clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.h
clang-tools-extra/clang-tidy/readability/SimplifyBooleanExprCheck.cpp
clang-tools-extra/docs/ReleaseNotes.rst
clang-tools-extra/docs/clang-tidy/checks/readability-simplify-boolean-expr.rst
clang-tools-extra/test/clang-tidy/checkers/readability-simplify-bool-expr-demorgan.cpp
|
This whole implementation would be alot simpler(and likely faster) if you matched on the generic case then in the check callback work out what replacement you need.