This is an archive of the discontinued LLVM Phabricator instance.

[clang-tidy] new check cppcoreguidelines-pro-type-reinterpret-cast
ClosedPublic

Authored by mgehre on Sep 30 2015, 3:42 PM.

Details

Summary

This check flags all uses of reinterpret_cast in C++ code.

Use of these casts can violate type safety and cause the program to
access a variable that is actually of type X to be accessed as if it
were of an unrelated type Z.

This rule is part of the "Type safety" profile of the C++ Core
Guidelines, see
https://github.com/isocpp/CppCoreGuidelines/blob/master/CppCoreGuidelines.md#-type1-dont-use-reinterpret_cast.

Diff Detail

Event Timeline

mgehre updated this revision to Diff 36155.Sep 30 2015, 3:42 PM
mgehre retitled this revision from to [clang-tidy] new check misc-no-reinterpret-cast.
mgehre updated this object.
mgehre added a subscriber: cfe-commits.
vsk added a subscriber: vsk.Sep 30 2015, 4:33 PM

The patch lgtm. Has there been a discussion on cfe-dev about adopting these guidelines?

I count well over 500 uses of reinterpret_cast in llvm+clang, so we might not all be on the same page on this.

If you check AvoidCStyleCastsCheck.cpp, it also suggest what kind of cast should you use to replace a C style cast. Probably, in some cases, the developers might be able to change the reinterpret_cast to something more type safe. It would be nice to make a suggestion in those cases.

aaron.ballman added a subscriber: aaron.ballman.

As a slightly more broad question: I think we should have a user-customizable way to categorize these checks so that you can enable/disable them with finer-grained control. Some of the existing checkers already cover the Cpp guidelines, and we'll likely be adding plenty more. There's quite likely overlap with Google and LLVM checkers, etc. It would be really nice if we had a way to say: -checks=-*, CppGuidelines or -checks=-*, CERT, etc.

(I'm not suggesting this as part of this patch, but I think it is an idea we should consider exploring because style guidelines abound: the new C++ ones, MISRA, CERT, joint strike fighter, etc. User-customizable categorization would really help for this sort of thing. This would help assuage my issue with the checker being on by default in misc-* -- it could be off in misc-* but on in cppcoreguidelines-*, for instance.)

~Aaron

clang-tidy/misc/NoReinterpretCastCheck.cpp
29 ↗(On Diff #36155)

I am worried about the amount of chattiness for this diagnostic and the fact that it does not provide the user with any idea as to what to do instead. The C-style cast checker will suggest that users don't use C-style casts, which suggests there's no way to appease this checker. The style guide suggests using variant, but that is not a workable solution for projects that don't have a variant type.

FWIW, I've run into the same thing for CERT rules like:

DCL50-CPP. Do not define a C-style variadic function
MSC50-CPP. Do not use std::rand() for generating pseudorandom numbers (to a lesser extent, because <random> exists now.)

I don't have a particularly good solution to the issue though. I'm not opposed to the checker, but I am opposed to the checker being on by default for misc-* checkers.

alexfh edited edge metadata.Oct 1 2015, 5:58 AM

As a slightly more broad question: I think we should have a user-customizable way to categorize these checks so that you can enable/disable them with finer-grained control. Some of the existing checkers already cover the Cpp guidelines, and we'll likely be adding plenty more. There's quite likely overlap with Google and LLVM checkers, etc. It would be really nice if we had a way to say: -checks=-*, CppGuidelines or -checks=-*, CERT, etc.

(I'm not suggesting this as part of this patch, but I think it is an idea we should consider exploring because style guidelines abound: the new C++ ones, MISRA, CERT, joint strike fighter, etc. User-customizable categorization would really help for this sort of thing. This would help assuage my issue with the checker being on by default in misc-* -- it could be off in misc-* but on in cppcoreguidelines-*, for instance.)

~Aaron

One way we could get CppCoreGuidelines checks available for easy enabling as a whole is to create a separate module and register all relevant checks there with names relevant to the CppCoreGuidelines (e.g. register the clang::tidy::google::ExplicitConstructorCheck there as "cppcoreguidelines-rc-explicit"). If a checks needs to be slightly modified in order to be closer to a specific rule, we might add some check options and configure proper defaults in the CppCoreGuidelinesModule::getModuleOptions(). If a larger change in behavior is needed, we could inherit from existing checks as well.

alexfh added a comment.Oct 1 2015, 6:00 AM
In D13313#256982, @vsk wrote:

The patch lgtm. Has there been a discussion on cfe-dev about adopting these guidelines?

I count well over 500 uses of reinterpret_cast in llvm+clang, so we might not all be on the same page on this.

I don't think LLVM can adopt a significant part of C++ Core Guidelines. Nevertheless, it makes sense to have these rules implemented as clang-tidy checks for the projects that decide to adopt them.

alexfh added a comment.Oct 1 2015, 6:15 AM

A high-level comment: the "misc" module is the place for checks that we didn't find (or create) a better category for. Here it's clear that we need a separate category, so we need a CppCoreGuidelinesModule and the cppcoreguidelines- check prefix. I also suggest using anchor names (converted to lower case) in the document as check names, this one would be cppcoreguidelines-pro-type-reinterpretcast.

test/clang-tidy/misc-no-reinterpret-cast.cpp
7 ↗(On Diff #36155)

Please add a newline at the end of file to pacify various diff tools.

aaron.ballman edited edge metadata.Oct 1 2015, 8:17 AM

As a slightly more broad question: I think we should have a user-customizable way to categorize these checks so that you can enable/disable them with finer-grained control. Some of the existing checkers already cover the Cpp guidelines, and we'll likely be adding plenty more. There's quite likely overlap with Google and LLVM checkers, etc. It would be really nice if we had a way to say: -checks=-*, CppGuidelines or -checks=-*, CERT, etc.

(I'm not suggesting this as part of this patch, but I think it is an idea we should consider exploring because style guidelines abound: the new C++ ones, MISRA, CERT, joint strike fighter, etc. User-customizable categorization would really help for this sort of thing. This would help assuage my issue with the checker being on by default in misc-* -- it could be off in misc-* but on in cppcoreguidelines-*, for instance.)

~Aaron

One way we could get CppCoreGuidelines checks available for easy enabling as a whole is to create a separate module and register all relevant checks there with names relevant to the CppCoreGuidelines (e.g. register the clang::tidy::google::ExplicitConstructorCheck there as "cppcoreguidelines-rc-explicit"). If a checks needs to be slightly modified in order to be closer to a specific rule, we might add some check options and configure proper defaults in the CppCoreGuidelinesModule::getModuleOptions(). If a larger change in behavior is needed, we could inherit from existing checks as well.

That's a good idea that I hadn't considered before. I may give this a shot for the CERT checks as well. Thank you for the suggestion!

~Aaron

Eugene.Zelenko set the repository for this revision to rL LLVM.
mgehre updated this revision to Diff 36291.Oct 1 2015, 1:26 PM

Renamed the check to cppcoreguidelines-pro-type-reinterpret-cast

mgehre updated this revision to Diff 36294.Oct 1 2015, 1:51 PM

Fix add_new_check.py for capitalization of CppCoreGuidelinesTidyModule.cpp

mgehre updated this revision to Diff 36296.Oct 1 2015, 2:04 PM

Read 'Check' suffix on ProTypeReinterpretCastCheck

mgehre retitled this revision from [clang-tidy] new check misc-no-reinterpret-cast to [clang-tidy] new check cppcoreguidelines-pro-type-reinterpret-cast.
aaron.ballman accepted this revision.Oct 2 2015, 5:57 AM
aaron.ballman edited edge metadata.

LGTM with one minor nit. Thank you for working on this!

~Aaron

clang-tidy/add_new_check.py
150

Thank you for pointing this out, I hadn't realized we had a helper for this, let alone that it does the wrong thing for the new module I was working on. :-P

clang-tidy/cppcoreguidelines/ProTypeReinterpretCastCheck.cpp
31

I don't think we need the parenthetical any longer because the name of the checker will already be displayed to the user with the diagnostic.

This revision is now accepted and ready to land.Oct 2 2015, 5:57 AM
mgehre updated this revision to Diff 36371.Oct 2 2015, 9:18 AM
mgehre edited edge metadata.

Rebased
Removed "(C++ Core Guidelines, rule Type.1)" from diagnostic

Can someone please commit this?

mgehre updated this revision to Diff 36372.Oct 2 2015, 9:20 AM

Shot to fast, fixed test.

Now it's ready

Does not apply cleanly to ToT; can you rebase?

~Aaron

mgehre updated this revision to Diff 36399.Oct 2 2015, 2:26 PM

rebased

mgehre added a comment.Oct 2 2015, 2:27 PM

I'm not sure what you mean by ToT. I rebased this against master @ http://llvm.org/git/clang-tools-extra.git

mgehre updated this revision to Diff 36557.Oct 5 2015, 2:33 PM

Rebased

mgehre updated this revision to Diff 36561.Oct 5 2015, 2:40 PM

"arc diff" produced a invalid patch file (due to some file being "moved"?)
This patch is manually generated by git format-patch. I checked that it applies cleanly against svn trunk.

aaron.ballman closed this revision.Oct 6 2015, 6:33 AM

Thank you for the patch! I've committed it in r249399.

~Aaron