Skip to main content
GameDev.net gamedev.net
🔒 Locked

C++ cast refactoring

Started by popsoftheyear Apr 8, 2009 at 10:07 AM 2 replies 1k views
Original Post
popsoftheyear
popsoftheyear
We recently converted our 20 yr. old project to unicode. Yikes. After fixing upwards of 10,000 errors so it would at least compile, there inevitably still many problems. A lot of these problems we won't find until something doesn't work at run time, and even then you don't always spot the issue. However, it is obvious after painstakingly fixing some of the initial bugs, that we could catch most of the problems at compile time if there was an easy way to convert all the C-style casts to C++ static_casts. Then any errors could be examined to see if there was some ignorance involved that will cause run-time problems, or if it really, really should be a reinterpret_cast (or occasionally a dynamic cast). So does anyone know of something (preferably free) that can refactor all C-style casts to say, a static_cast? I know the likelihood is slim, but just thought I'd ask, as there are 10s of thousands of them. (The ones where we cast const char * variables to LPCTSTR are the worst.....) Thanks
Kimmi
Kimmi
I had a similar issue some years ago. I wrote a little python-script to convert my c-casts to a static-cast. Maybe this could be a solution for your project, too.
I just wrote for several cases different regex-expressions and converted the search results into my stuff.
But of course I haven't fixed all my compile-bugs with this script. Some handwork were left.

Kimmi
A complicate solution may indicate a not understood problem.


[twitter]KimKulling[/twitter]
the_edd
the_edd
You might have some luck compiling with g++ (e.g. MinGW) and passing -Wold-style-cast.

For Visual C++, this page seems to imply that if you compile with /clr:oldSyntax you'll get a particular warning about using old-style casts. A bit of regex jiggery pokery will sort you out, probably.

Sneftel
Sneftel
Quote:
Original post by popsoftheyear
if there was an easy way to convert all the C-style casts to C++ static_casts.
Not a perfect one. C-style casts aren't at the highest or lowest precedence level, and in any case C is not a regular language (in the language-theoretic sense of the word) so any regex-based solution is going to fail in some cases. I concur with the_edd: Set your compiler to warn you about C-casts. IMHO, if when you run into an error, you fix all the C casts that look like they might be vaguely related, you'll have about the same success rate, and you won't have to bother with most of the casts.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.