C++ Validation Tools?

Started by
12 comments, last by MENTAL 18 years, 7 months ago
Does anyone know of any good (and free) C++ source code validation programs, that can scan a load of source files and give reports on the following: 1) Unused functions/classes (i.e. static code coverage). 2) Missing pure-virtual function implementations. 3) Err... that's it :). (2) is probably the most important at the moment (just fixed a few nasty errors due to forgetfulness), and 1) is just to make sure I don't write code I never use. thanks in advance
Advertisement
what's a "missing pure virtual function implementation"??

The compiler wont' let you instantiate a pure virtual class so if your code compiles aren't you always are guarenteed to have a concrete version of any method you need to call???

As for one. vs.net's linker in release mode will let you know about any unused functions it can discard.

If you can afford, and wait for, vs.net then you may be inluck as it has tools for 1) built in!!

Cheers
Chris
CheersChris
by missing pure virtual function, I mean a derived class that has failed to implement a pure virtual function in it's base class. As I am using an abstract factory and only accessing the classes through a pointer to the base class, calling the missing function will throw a nasty run-time exception.
afaik, if u use pure virtual functions.. a derived class MUST provide an implementation, else the compiler will give you an error. You theoretically never get to a runtime error, unless the implementation itself is erroneous. That's a whole different problem altogether :P
- To learn, we share... Give some to take some -
yes I know the derived class must provide an implementation - what I was looking for was a tool that would alert me when I have forgotten to write one :).

An example:
class Base{public:    virtual void SomeFunc = 0;};class Derived : public Base{};Base *base = new Derived;base->SomeFunc ();    <--- runtime error.
new Derived; is a compile time error. The tool which detects this error is the compiler. If your compiler doesn't detect this error, get a real compiler! (VC, gcc, cw ...)

edit1: unless your base class is in a separate library to your derived class, in which case I would expect a link time error to an undefined v-table.

edit2: what compiler are you using, anyway?
The compiler will tell you... it will give you an error on trying to instantiate 'new Derived'.

The only case you'd be able to call a pure virtual function would be in constructors and destructors (since the state of the object is that of the type you're contructing/deconstructing). The compiler would probably give you a warning or even error here as well, unless you call through some proxy function.
You can get a "pure virtual functions not implemented run time error" if you call them in the "wrong place".
The wrong place can be the base class ctor; in this case your derived class will not override the function and you get a null pointer!
This is the standard C++ behaviour (in fact when you enter the base ctor the derived part is not created yet)
I dont know if are there compilers that stop you to do this; VC6 compiler does not report any error for example.
But this is an error equal to use a pointer without initialize it!!!
Quote:Original post by MENTAL
yes I know the derived class must provide an implementation - what I was looking for was a tool that would alert me when I have forgotten to write one :).

An example:
*** Source Snippet Removed ***


Ahh MENTAL, my vs.net compiler gives an error

C2259 'Derived': cannot instantiate abstract class.


Do an msdn search for C2259. Compilers might be smarter than you think:)

Cheers
Chris
CheersChris
Quote:Original post by blizzard999
You can get a "pure virtual functions not implemented run time error" if you call them in the "wrong place".
The wrong place can be the base class ctor; in this case your derived class will not override the function and you get a null pointer!


You can't expect virtual funtions to work in a constructor anyway. Since the base class has it's constructor called before the derived classes, you're basically trying to use functions on an object that doesn't even exist yet.

#include <iostream>struct Base {  Base() { doSomething(); }  virtual void doSomething() const = 0; };struct Derived: public Base {  virtual void doSomething() const {} };int main() {  Derived foo; }


g++ was nice enough to give me this error:
foo.cpp: In constructor `Base::Base()':foo.cpp:5: error: abstract virtual `virtual void Base::doSomething() const'   called from constructor

Chess is played by three people. Two people play the game; the third provides moral support for the pawns. The object of the game is to kill your opponent by flinging captured pieces at his head. Since the only piece that can be killed is a pawn, the two armies agree to meet in a pawn-infested area (or even a pawn shop) and kill as many pawns as possible in the crossfire. If the game goes on for an hour, one player may legally attempt to gouge out the other player's eyes with his King.

This topic is closed to new replies.

Advertisement