[SERVER-15368] Banish C-style casts under src/mongo Created: 24/Sep/14  Updated: 27/Oct/21  Resolved: 27/Oct/21

Status: Closed
Project: Core Server
Component/s: Internal Code
Affects Version/s: None
Fix Version/s: features we're not sure of

Type: Improvement Priority: Major - P3
Reporter: Andrew Morrow (Inactive) Assignee: DO NOT USE - Backlog - Platform Team
Resolution: Won't Do Votes: 0
Labels: None
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified

Participants:

 Description   

C-style casts are, for very good reasons, prohibited by our coding guidelines (see http://google-styleguide.googlecode.com/svn/trunk/cppguide.html#Casting), yet they are pervasive in our codebase.

There is a compiler flag, -Wold-style-casts, that can be used to prohibit their usage. However, we cannot enable that flag until we remove all of the existing C-style casts.

To do so, we either need to

  • Use a linter that can detect them, like clang-tidy, and fix them up manually.
  • Write a clang tool that does source to source transformations and re-writes the C style casts to the appropriate C++ style cast.

To make -Wold-style-casts effective, we will probably also need to figure out how to make the compiler consider headers coming from src/third_party to be 'system' headers, for which warnings are not issued. We probably want to do this anyway since we frequently encounter warnings in third_party code. Currently, we end up doing global suppressions for those warnings, but we would not need to do so if we treated src/third_party as a system header.



 Comments   
Comment by Githook User [ 07/Oct/14 ]

Author:

{u'username': u'andy10gen', u'name': u'Andy Schwerin', u'email': u'schwerin@mongodb.com'}

Message: SERVER-15368 Remove C-style bool and void casts in assert utilities and likely macros.

Also, move MONGO_likely/MONGO_unlikely to platform/compiler.h out of bson/inline_decls.h.
Branch: master
https://github.com/mongodb/mongo/commit/64f8c8c82fc0bb109abf561301360d9a61b9825d

Comment by Asya Kamsky [ 03/Oct/14 ]

Here is the result set after pruning out various C++ casts for now, and anything inside macros (for now). We're down to 431 instances of C-style casts.

https://coverity.mongodb.com/reports.htm#v10007/p10035/

Comment by Asya Kamsky [ 02/Oct/14 ]

I ran a checker flagging all occurrences of manual casts, was a little shocked by over 25K occurrences till I realized the counts included all the third party code.

For our own code, it's just over 8,000 defects in about 600 files, which still seems like a lot...

Comment by Asya Kamsky [ 29/Sep/14 ]

Let me see if I can do it in the next few days - we only show src/mongo defects in the UI for all checkers though, so that would be no different here.

Comment by Andrew Morrow (Inactive) [ 25/Sep/14 ]

asya@mongodb.com That might be very useful if it is easy to do as a one-off. We would need to restrict or filter the report to only apply to src/mongo so we don't get overwhelmed with data about boost and v8, etc.

Comment by Asya Kamsky [ 25/Sep/14 ]

I think can run a checker in Coverity that flags all such casts - I'm pretty sure that was one of the standard SDK example checkers...

Generated at Thu Feb 08 03:37:49 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.