This is an archive of the discontinued LLVM Phabricator instance.

[clang] add warning on shifting boolean type
Needs ReviewPublic

Authored by angelomatnigoogle on Jan 10 2023, 11:06 AM.

Details

Reviewers
arsenm
shafik
Summary

Warn on operations "b << x" and "b >> x"

Github issue #28334

Diff Detail

Event Timeline

Herald added a project: Restricted Project. · View Herald TranscriptJan 10 2023, 11:06 AM
angelomatnigoogle requested review of this revision.Jan 10 2023, 11:06 AM
Herald added a project: Restricted Project. · View Herald TranscriptJan 10 2023, 11:06 AM

Hey @aaron.ballman I made some suggestions in the bug report: https://github.com/llvm/llvm-project/issues/28334 do you think this is a reasonable approach?

Thank you for working on this! It seems that precommit CI found valid failures that should be addressed:

Clang :: AST/Interp/shifts.cpp
Clang :: Analysis/svalbuilder-simplify-no-crash.c
Clang :: CXX/over/over.built/p18.cpp
Clang :: Misc/warning-flags.c

Please be sure to also add a release note to clang/docs/ReleaseNotes.rst for the new warning flag. Also, can you try running this new diagnostic against some larger C and C++ code bases to see if it discovers any false or true positives?

clang/include/clang/Basic/DiagnosticSemaKinds.td
6606–6610

Reworded and combined into one diagnostic. I also changed the diagnostic group because "dependent" has specific meaning to C++ folks.

clang/lib/Sema/SemaExpr.cpp
11948–11956

With the change to the diagnostic wording, I think this function can go away entirely.

11953

One thing I wonder about is whether we can add a fix-it to this diagnostic to rewrite the expression to the alternative form. If that's easy to do, it might be a nice addition to the diagnostic. If it's at all a pain though, I don't insist.

clang/test/Sema/warn-shift-bool.cpp
2–3
7

You should also have a test case for >>

angelomatnigoogle updated this revision to Diff 491923.EditedJan 24 2023, 2:48 PM

Consolidated shift-bool warning; fixed relevant unit tests

Thank you for the feedback, and pardon me for not getting back to this work sooner. I have adopted your code change suggestions with minor tweeks to get it working and passing the previously failing tests. Later this week I will double back and attempt to use FixItHint on this as well as to update ReleaseNotes and run clang against some larger code bases

ping?

Thank you for pinging this! I took a quick pass over the changes and they're moving in the right direction, but there are some new test failures that need addressing and some unfinished comments from the previous review.

clang/lib/Sema/SemaExpr.cpp
11955–11956

We try not to pass constant strings into diagnostics because that makes it harder for us to localize diagnostics in the future. Instead, these strings should be in the diagnostic wording itself (with a %select directive).

@angelomatnigoogle are you still working on this patch?

Pardon me for losing sight on this change. Within the week, I will revisit this change based on the current outstanding comments.