Find never-called functions - c++

I'm doing some refactoring in a project using Qt with Visual Studio 2008, and I'd like to know if there's a simple way to find the functions and methods that are never called?

You can try a static code analysis tool, like http://en.wikipedia.org/wiki/Cppcheck

A -Wall in your compilation options should do it. (Or -Wunused-function). Check the compilator options in VS.

I've always preferred "grep", but that may be a bit "old-school".
Visual Studio will build a call-graph for you that is helpful but not 100% reliable.
Another alternative is comment out the function and see if the project will still link.

Is there a chance to build this Qt Project using gcc? If so, you could use gcov. It tells you all methods which were called during execution. Then you could use ctags to create a list of all methods available. From these two sets you could find those, not being called.
Of cause the application should run long enough under gcov for delivering more or less complete list of used functions.
(I guess there is an easier way using linker or a compiler switch. :-))

Related

How can I parse C++ code in a prebuild event?

I have a prebuild-event tool (written in Ruby) in my C++ toolchain, that generates additional C++ code from existing C++ source code. I would like to replace this tool with a faster generator and using clang would be the best option.
Is there a way to write a C++ application that parses C++ source code of a file, so I can implement this prebuild tool in Clang? I am looking for a keyword or page with how to start. Any help is highly appreciated!
Parsing C++ is not a simple thing. Compile-time trickery and implicit semantics make it incredibly hard. Because of this I would suggest going with Clang. Its developers made it possible to use Clang as a library. Check out this guide to see different interfaces Clang has. If you want real C++ experience you might want to choose LibTooling.
I want to warn you though that in order for any C/C++ parser to work as expected they absolutely need compilation options used by the real compiler. Without include directories or macro definitions the code can make little to no sense. Basically your build system should tell your custom tool how to compile each file. The simplest approach would be using compilation database. It is a go-to solution for many Clang-based tools. However, it looks like you're making it a part of your build system, so maybe incorporating your tool and using options directly from the build system can be not such of a burden for you.
I hope this information is helpful!

Visual Studio Compile Warning: Using system operator==/!= for GUIDs

I am trying to convert a Windows driver project from using the DDK build system and Makefiles into a project that can be compiled from within Visual Studio 2012.
I've followed this guide for setting up the project options, but I am getting this warning when I try to compile:
warning : Using system operator==/!= for GUIDs
I guess there is a project configuration flag that I can set that will fix this. Any help appreciated.
This is because using ==/!= on a GUID may not yield correct results, since the GUID is a non-trivial structure type.
When compiled as C++, guiddef.h implements ==/!= using this: IsEqualCLSID on MSDN
In your case, the problem is that the DDK header is unable to implement the C++ operator overloads for ==/!= GUIDs. It looks to me like this would happen because guiddef.h is being included prior to including the DDK headers. Apparently guiddef.h and the DDK don't agree on how the ==/!= operators should be implemented, and so the DDK issues a warning.
So my guess is that all you really need to do is revisit the includes order. Also, I can't verify it since I don't have VS 2012 handy, but its quite possible that this warning itself is the only thing 'new' and that the actual behavior of the program may be unchanged from VS2008. VS2008 may very well have done the same thing but failed to issue a warniner to alert the programmer. In which case, if it worked in the old compiler then it probably should work in the new one too.
From here, that warning is given by:
#if defined(_SYS_GUID_OPERATOR_EQ_)
#define _GUID_OPERATORS_
#pragma message("WARNING: Using system operator==/!= for GUIDs")
#endif
And it looks like that can be disabled by defining _NO_SYS_GUID_OPERATOR_EQ_. So try a compiler option of /D_NO_SYS_GUID_OPERATOR_EQ_.
However, I don't recommend doing something unless you know why you're doing it. Perhaps you're supposed to supply your own overloads?

Building a C/C++ project without using Makefiles

I have a project containing C/C++ files. I'd like to build it without using make. What are my options? I'd like cross platform solutions if possible.
I've used SCons and it is very good.
SCons is an Open Source software construction tool—that is, a next-generation build tool. Think of SCons as an improved, cross-platform substitute for the classic Make utility with integrated functionality similar to autoconf/automake and compiler caches such as ccache. In short, SCons is an easier, more reliable and faster way to build software.
I've also looked at cmake but have not seriously used it.
Well, you're always going to need some way to invoke the compiler. If it's a trivial project, you can usually just stick all the .C filenames on the command line of the compiler and get some kind of output.
Or you can use a batch file / shell script instead of a makefile, but it would be less 'cross-platform' than a makefile and much less useful.
You should probably explain your motivations more clearly.
Since you're going to use Boost anyways (right?) Boost.Jam might be an option.
I already used WAF in some of my projects and it worked out quite well.
If you are familiar with python...
A common alternative is to write python scripts to compile your code.
How are you editing you code? Can that system also build it for you?
Visual Studio, Eclipse, XCode, KDevelop? :-)

Port Visual Studio C++ to Linux

We have a not very complicated but big (i.e. lots of files) Visual Studio C++ Win32 Console written in C++0x standard in VS2010.
It does not use any non standard code or anything (Hopefully!).
I now wanna port it to Linux.
Which way is the quickest way to do it?
autoconf?
old-fashioned make file?
any other solution?
I would use regular make but keep it simple with default rules as much as possible. Add in dependencies as you go along.
EDIT: As in interim step, build it with mingw so that you can avoid the whole API porting issue until you have a working build in your new build mechanism.
If your console app calls win32 API functions then you have a choice between modifying all the source where it is used or writing a module that implements those functions.
In prior porting efforts of this type I tried it both ways and the latter was easier. I ended up writing only about 18 to 20 shim functions.
It was successful enough that I ended up writing an OS abstraction layer that was used on many projects that simply let me compile on Windows native, cygwin, Linux, VxWorks, etc. with trivial changes to one or two files.
(p.s. Any interest in an open source version of a C++ based OS abstraction layer? I was thinking of releasing an unencumbered version of it to the world if there's sufficient interest. It's mostly useful where BOOST is too heavy -- i.e. embedded projects.)
Most probably you don't need autoconf (and I suggest you don't touch it, unless you love pain), because you are not trying to be portable to a dozen of Unix flavours.
Roll your Makefiles manually. It shouldn't be too difficult if you have a set of shared rules and have minimal Makefiles that just specify source files and compile options.
Use GCC 4.5 as it supports more C++0x features.
You can export a make file from Visual Studio.
Update: Actually you can't anymore, unless you have VC6 lying around
STAY AWAY FROM AUTO* and configure. These are horrible IMHO.
If you can somehow get a VS 8 or 9 vcproj/sln, you can use this. I have not used it, so I can't give any advice.
If you're up to manual conversion, I would suggest something like CMake, as it's pretty easy to get ready fast enough, even for large projects.
If the project has a simple layout, you could have success using Qt 4's qmake like this:
qmake -project
It will output a qmake .pro file, which can be converted into a makefile on many platforms (using qmake). This should work okay, but not perfectly. Alternatively, you can install the Qt plugin for VS, and let it generate the pro file from an existing VS project. It will make your build system depend on Qt4's qmake, which is probably not what you want.
There are of course other things like cmake, but they will all require manual intervention.
The fastest way to do it?
g++ *.cpp -o myapp
Seriously, depending on your needs, even generating a makefile might be overkill. If you're just interested in a quick and dirty "let's see if we can get a working program on Linux", just throw your code files at g++ and see what happens.

Dead code identification (C++)

I have a large legacy C++ project compiled under Visual Studio 2008. I know there is a reasonably amount of 'dead' code that is not accessed anywhere -- methods that are not called, whole classes that are not used.
I'm looking for a tool that will identify this by static analysis.
This question: Dead code detection in legacy C/C++ project suggests using code coverage tools. This isn't an option as the test coverage just isn't high enough.
It also mentions a -Wunreachable-code. option to gcc. I'd like something similar for Visual Studio. We already use the linker's /OPT:REF option to remove redundant code, but this doesn't report the dead code at a useful level (when used with /VERBOSE there are over 100,000 lines, including a lot from libraries).
Are there any better options that work well with a Visual Studio project?
I know that Gimpel's Lint products (PC-Lint and Flexelint) will identify unreachable code and unused / unreferenced modules.
They both fall in the category of static analysis tools.
I have no affiliation w/ Gimpel, just a satisfied long-term customer.
You'll want something along the lines of QA-C++ (http://www.programmingresearch.com/QACPP_MAIN.html), also see http://en.wikipedia.org/wiki/List_of_tools_for_static_code_analysis for similar products.
You're looking for a static code analysis tool that detects unreachable code; many coding guidelines (such as MISRA-C++, if I'm not mistaken) require that no unreachable code exists. An analysis tool geared specifically to enforce such a guideline would be your best bet.
And you'll like be able to find other uses for the tool as well.
I dont know Visual C, and had also recommended the -Wunreachable-code specific coverage tools. As solution for your situation I would try the following:
Make with ctags (or similar programm) a list of all your symbols in your source
Enable in your compiler the dead code elimination (I would assume it defaults to on)
Enable your whole-program/link time optimizations (so he knows that not used functions in your moduls are not required by other externals and get discarded)
Take the symbols from your binary and compare them with the symbols from 1.
Another approach could be some call graph generating tool (e.g. doxygen).
I suggest you use a couple approaches:
1. GCC has some useful compilation flags:
-Wunused-function
-Wunused-label
-Wunused-value
-Wunused-variable
-Wunused-parameter
-Wunused-but-set-parameter
2. Cppcheck has some useful features like:
--enable=unusedFunction
3. Use static analyzer as was suggest before.
One approach that works for me - with Delphi - is to enable debugging, and run your program under the debugger.
When a Delphi program is run under the debugger, the IDE shows in the margin which lines of code can be set as breakpoints. Code which is truly dead - i.e., has been stripped out by the linker/compiler is obvious as breakpoints can't be set there.
Some additional notes, as commenters seem to misunderstand this:
a: You don't need to try setting a breakpoint on each line. Just open up the source file in the IDE, and quickly scroll through it. Dead code is easily spotted.
b: This is NOT a 'code coverage' check. You don't need to run the application to see if it reaches the lines.
c: I'm not familiar enough VS2008 so can't say if this suggestion will work.
Either
1) MSVC's under-used in built static analysis tool.
2) The MSVC marketplace has lots of tools including support for most free tools, including CppCheck
You will need the latest version of Visual Studio for market place applications, but the free "Community Edition" has very lenient licencing.
Write a script that randomly deletes a function (from the source code) and recompiles everything from scratch. If it still compiles - that function was dead code.