Jump to content
  • Advertisement
Sign in to follow this  
MENTAL

C++ Validation Tools?

This topic is 4830 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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!!!

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!