This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] Allow opt-in or out of some commonly occuring patterns in NarrowingConversionsCheck.
ClosedPublic

Authored by Stephen on Mar 29 2021, 3:03 PM.

Details

Summary

Within clang-tidy's NarrowingConversionsCheck.

  • Allow opt-out of some common occurring patterns, such as:
    • Implicit casts between types of equivalent bit widths.
    • Implicit casts occurring from the return of a ::size() method.
    • Implicit casts on size_type and difference_type.
  • Allow opt-in of errors within template instantiations.

This will help projects adopt these guidelines iteratively.
Developed in conjunction with Yitzhak Mandelbaum (ymandel).

Diff Detail

Event Timeline

Stephen created this revision.Mar 29 2021, 3:03 PM
Stephen requested review of this revision.Mar 29 2021, 3:03 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 29 2021, 3:03 PM
Stephen updated this revision to Diff 333994.Mar 29 2021, 3:09 PM
Stephen retitled this revision from Within clang-tidy's NarrowingConversionsCheck. * Allow opt-out of some common occurring patterns. * Allow opt-in of errors within template instantiations. to Allow opt-in or out of some commonly occuring patterns in NarrowingConversionsCheck..
Stephen edited the summary of this revision. (Show Details)

Updated change description and improved formatting.

ymandel retitled this revision from Allow opt-in or out of some commonly occuring patterns in NarrowingConversionsCheck. to [clang-tidy] Allow opt-in or out of some commonly occuring patterns in NarrowingConversionsCheck..Mar 30 2021, 7:41 AM
ymandel added reviewers: aaron.ballman, ymandel.
ymandel added a subscriber: cfe-commits.
ymandel accepted this revision.Apr 13 2021, 12:09 PM

But please wait for additional "Accept" from either hokein or aaron.ballman. Thanks!

This revision is now accepted and ready to land.Apr 13 2021, 12:09 PM

Thanks! I think the changes generally LG, but I did have a question that might generate a small change.

clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp
86

Should this also support size_t and ptrdiff_t given that those are usually the same as size_type and difference_type?

a small suggestion to simplify the tests.

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-narrowing-conversions-equivalentbitwidth-off.cpp
1 ↗(On Diff #333994)

instead of having multiple individual test files, I think we can group them into a single file, you can use the flag -check-suffix=, like

// RUN: %check_clang_tidy -check-suffix=TESTCASE1 ...
// RUN: %check_clang_tidy -check-suffix=TESTCASE2 ...


// some warning code for test-case1
CHECK-MESSAGES-TESTCASE1: ....
Stephen updated this revision to Diff 338272.Apr 16 2021, 8:36 PM
  • Add ability to pass semi-colon separated list of types to ignore, instead of assuming only size_type and difference_type.
  • Remove ability to avoid warnings on ::size() method, since it's subsumed by allowing narrowing conversions of size_t.
  • Coalesce on/off option tests into per-option test files.
Stephen requested review of this revision.Apr 16 2021, 8:51 PM

Thanks for the review! I think I've addressed the comments -- hopefully I kept it inline with what you're thinking. Please have another look at your leisure.

clang-tools-extra/clang-tidy/cppcoreguidelines/NarrowingConversionsCheck.cpp
86

Thanks, yeah, that's a good idea. I was somewhat apprehensive to ignore all conversions involving size_t, but it's probably ok. Now that we are, we don't need the ::size() method stuff, so I removed that.

And it seemed like I might as well make the list of types configurable, so I rolled that in here too. So it's a bigger change that you maybe had intended, though hopefully not too far off the mark.

clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-narrowing-conversions-equivalentbitwidth-off.cpp
1 ↗(On Diff #333994)

Thanks! Besides cutting down the number of files, it really helped clarify the distinction in behavior with the warning on/off.

I coalesced the on/off pairs into a single, but kept the options tested in separate files, as that seemed to be the pattern in place.

Let me know if you'd prefer to squeeze them into fewer.

Stephen updated this revision to Diff 338273.Apr 16 2021, 8:54 PM

(fix patch to include all local changes)

ymandel accepted this revision.Apr 29 2021, 10:36 AM
ymandel added inline comments.
clang-tools-extra/docs/clang-tidy/checks/cppcoreguidelines-narrowing-conversions.rst
51

nit: add dash "semicolon-separated"

This revision is now accepted and ready to land.Apr 29 2021, 10:36 AM
Stephen updated this revision to Diff 342409.May 3 2021, 8:45 AM
Stephen marked 2 inline comments as done.

Within clang-tidy's NarrowingConversionsCheck.

  • Allow opt-out of some common occurring patterns, such as:
    • Implicit casts between types of equivalent bit widths.
    • Implicit casts occurring from the return of a ::size() method.
    • Implicit casts on size_type and difference_type.
  • Allow opt-in of errors within template instantiations.

This will help projects adopt these guidelines iteratively.
Developed in conjunction with Yitzhak Mandelbaum (ymandel).

Stephen requested review of this revision.May 3 2021, 8:53 AM

Thanks, ymandel!

Anything else, aaron.ballman or hokein?

njames93 added inline comments.
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-narrowing-conversions-ignoreconversionfromtypes-option.cpp
13

This is breaking tests on windows, It seems like its pre-defined to be unsigned long long on windows.
Simplest fix is to rename size_t to something else.
I guess changing the typedef to unsigned long long will change behaviour of tests.
Failing that an // UNSUPPORTED: system-windows directive at the top of the file would also work.

Stephen updated this revision to Diff 342472.May 3 2021, 11:10 AM
  1. Updating D99543: [clang-tidy] Allow opt-in or out of some commonly occuring patterns in NarrowingConversionsCheck. #
  2. Enter a brief description of the changes included in this update.
  3. The first line is used as subject, next lines as comment. #
  4. If you intended to create a new revision, use:
  5. $ arc diff --create

Within clang-tidy's NarrowingConversionsCheck.

  • Allow opt-out of some common occurring patterns, such as:
    • Implicit casts between types of equivalent bit widths.
    • Implicit casts occurring from the return of a ::size() method.
    • Implicit casts on size_type and difference_type.
  • Allow opt-in of errors within template instantiations.

This will help projects adopt these guidelines iteratively.
Developed in conjunction with Yitzhak Mandelbaum (ymandel).

Stephen marked an inline comment as done.May 3 2021, 11:14 AM
Stephen added inline comments.
clang-tools-extra/test/clang-tidy/checkers/cppcoreguidelines-narrowing-conversions-ignoreconversionfromtypes-option.cpp
13

Thanks, njames93. I've switched to custom type names to avoid these sorts of conflicts. I've also removed the difference_type test, since it didn't test distinct functionality from size_type, once they use custom names.

Sorry for the inconvenience.

hokein accepted this revision.May 12 2021, 11:22 AM

thanks, looks good. Sorry for the delay.

This revision is now accepted and ready to land.May 12 2021, 11:22 AM