This is an archive of the discontinued LLVM Phabricator instance.

[clang][Analysis] Handle && and || against variable and its negation as tautology
ClosedPublic

Authored by hazohelet on Jun 4 2023, 3:03 AM.

Details

Summary

This patch introduces a new warning flag -Wtautological-negation-compare grouped in -Wtautological-compare that warns on the use of && or || operators against a variable and its negation.
e.g. x || !x and !x && x
This also makes the -Winfinite-recursion diagnose more cases.

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

Diff Detail

Event Timeline

hazohelet created this revision.Jun 4 2023, 3:03 AM
Herald added a project: Restricted Project. · View Herald Transcript
hazohelet requested review of this revision.Jun 4 2023, 3:03 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 4 2023, 3:03 AM

Added some more reviewers based on lines owned in CFG.cpp.

tbaeder added inline comments.Jun 8 2023, 8:42 AM
clang/lib/Analysis/CFG.cpp
1106–1107
hazohelet updated this revision to Diff 532557.Jun 19 2023, 1:01 AM
hazohelet marked an inline comment as done.

Address review comment

  • NFC stylistic change

Seems like a nice idea to me, thanks!

Do you have any numbers on how often this fires in practice, and what the true positive rate is? (Build some large-ish open source project with this, and see what it finds.)
Did you verify that this has negligible compile time impact for building a large-ish project?

With that, looks good.

clang/lib/Analysis/CFG.cpp
1089

Do you want to IgnoreParens() on the ! subexpr too?

Do you have any numbers on how often this fires in practice, and what the true positive rate is? (Build some large-ish open source project with this, and see what it finds.)

I built rui314/mold and saw no new warnings emitted from this change. I searched several repositories to find only one instance in CBLAS code that this change could be relevant.
https://github.com/Reference-LAPACK/lapack/blob/c57e36a43bb1c25c7ec15a3c84f4800942a5ca43/CBLAS/src/cblas_xerbla.c#L69-L70
I'm not sure what exactly they are doing there, but the comment (Force link of our F77 error handler) suggests it is intentional, so it could be counted as false positive. But, I don't see any reason for not doing if (0) instead.

Did you verify that this has negligible compile time impact for building a large-ish project?

About the compile time impact, I don't expect this patch to have a noticeable change. The changes in this patch does not contain heavy computations like AST traversals. At least the build times of rui314/mold by clang ToT and patched clang did not have noticeable difference.

clang/lib/Analysis/CFG.cpp
1089

I wanted to, but it looks like most codes containing parentheses in negation subexpression are macros, so now I don't think we need it.
https://sourcegraph.com/search?q=context:global+/%21%5C%28%5Ba-zA-Z_%5D%2B%5C%29/+lang:C+lang:C%2B%2B&patternType=standard&case=yes&sm=1&groupBy=repo

Generally LGTM, just a minor suggestion on slightly rewording the diagnostics and combining them.

clang/include/clang/Basic/DiagnosticSemaKinds.td
9799–9804

Let's combine these if we can. I'm not 100% certain that %select is going to like &&||| though, so if there's an issue, separate is also fine.

thakis accepted this revision.Jul 27 2023, 5:36 AM

lg with aaron's suggestion. thanks for the patch, and for your patience!

This revision is now accepted and ready to land.Jul 27 2023, 5:36 AM
hazohelet updated this revision to Diff 545213.Jul 28 2023, 9:59 AM

Address comments from Aaron

  • Reworded diagnostic message
hazohelet marked 2 inline comments as done.Jul 28 2023, 10:02 AM

Thanks for the review

clang/include/clang/Basic/DiagnosticSemaKinds.td
9799–9804

%select(&&|||)0 doesn't seem to work as we want it to, so I kept it separated

This revision was landed with ongoing or failed builds.Aug 17 2023, 1:56 AM
This revision was automatically updated to reflect the committed changes.
hazohelet marked an inline comment as done.