This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] add support for Demorgan conversions to readability-simplify-bool-expr
ClosedPublic

Authored by njames93 on May 2 2022, 2:37 PM.

Details

Summary

Adds support for recognising and converting boolean expressions that can be simplified using De Morgans Law.

This is a different implementation to D124650.

Fixes https://github.com/llvm/llvm-project/issues/55092

Diff Detail

Event Timeline

njames93 created this revision.May 2 2022, 2:37 PM
Herald added a project: Restricted Project. · View Herald TranscriptMay 2 2022, 2:37 PM
njames93 updated this revision to Diff 426524.May 2 2022, 2:44 PM

Small update

njames93 updated this revision to Diff 426976.May 4 2022, 5:58 AM

Add tracking info to prevented nested cases from emitting conflicting fixes, disabled for use cases like clangd.
Prevent emitting fixes in macro expansions.

njames93 published this revision for review.May 4 2022, 6:03 AM
njames93 edited the summary of this revision. (Show Details)
Herald added a project: Restricted Project. · View Herald TranscriptMay 4 2022, 6:04 AM
Herald added a subscriber: cfe-commits. · View Herald Transcript
njames93 updated this revision to Diff 427038.May 4 2022, 9:06 AM

Enhanced support for parens, both adding them in when needed as well as removing some un-needed parens.

njames93 updated this revision to Diff 427082.May 4 2022, 11:31 AM

Reimplement matchers as an ASTVisitor instead.
This massively simplifies the tracking of nested cases.

njames93 updated this revision to Diff 427087.May 4 2022, 11:34 AM

Added release notes.
Fix build issues with previous push.

njames93 updated this revision to Diff 428069.May 9 2022, 6:49 AM

Tweak fix to not remove parens if it will result in a LogicalOpParentheses warning.

njames93 updated this revision to Diff 429907.May 16 2022, 5:10 PM

Extend fix-its to also support removing parens where possible.

LGTM! Thanks for taking my idea and implementing it much better than what I had :).

This revision is now accepted and ready to land.May 18 2022, 11:15 AM
njames93 added a comment.EditedMay 18 2022, 12:01 PM

LGTM! Thanks for taking my idea and implementing it much better than what I had :).

Cheers.
Can I ask why you feel that this should be behind an option?
Also do you think there is merit in handling the case !(A && B) -> !A || !B obviously that could be behind an option.

njames93 updated this revision to Diff 430472.May 18 2022, 12:06 PM

Remove unnecessary parens in documentation as check will remove the parens when possible.

LGTM! Thanks for taking my idea and implementing it much better than what I had :).

Cheers.
Can I ask why you feel that this should be behind an option?

When I've brought it up in manual code reviews before, there were some objections.
That's why I thought it was worth adding an option to disable it. Many people aren't
even familiar with DeMorgan's Theorem/Law and might be afraid that the simplification
changes semantics, although the underlying github issue shows that this is not the case.

Also do you think there is merit in handling the case !(A && B) -> !A || !B obviously that could be behind an option.

The reason I proposed the simplification !(!A && B) -> A || !B is because the former employs
"double negatives" and provides a stumbling block for readability. All readability "checks"
are somewhat influenced by opinion and style to a certain extent, but I think the avoid
"double negative" idea is widely accepted.

In the case of !(A && B) -> !A || !B, no double negative is involved and one could argue
that it went from one negative to two, which is why I didn't include that in my original
proposal. The "goodness" of it just doesn't seem universal and personally if the check
grew to enable that case, I'd want an option to disable that behavior :).